f02fc55ddc03cea21c6e99a8b5b873b63ab8a70d
[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 Each item can also be a list `(not F)' where F is a function;
719 this reverses the sort order.
720
721 Ready-made functions include `gnus-article-sort-by-number',
722 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
723 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
724 and `gnus-article-sort-by-score'.
725
726 When threading is turned on, the variable `gnus-thread-sort-functions'
727 controls how articles are sorted."
728   :group 'gnus-summary-sort
729   :type '(repeat (choice (function-item gnus-article-sort-by-number)
730                          (function-item gnus-article-sort-by-author)
731                          (function-item gnus-article-sort-by-subject)
732                          (function-item gnus-article-sort-by-date)
733                          (function-item gnus-article-sort-by-score)
734                          (function-item gnus-article-sort-by-random)
735                          (function :tag "other"))))
736
737 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
738   "*List of functions used for sorting threads in the summary buffer.
739 By default, threads are sorted by article number.
740
741 Each function takes two threads and returns non-nil if the first
742 thread should be sorted before the other.  If you use more than one
743 function, the primary sort function should be the last.  You should
744 probably always include `gnus-thread-sort-by-number' in the list of
745 sorting functions -- preferably first.  Also note that sorting by date
746 is often much slower than sorting by number, and the sorting order is
747 very similar.  (Sorting by date means sorting by the time the message
748 was sent, sorting by number means sorting by arrival time.)
749
750 Each list item can also be a list `(not F)' where F is a
751 function; this specifies reversed sort order.
752
753 Ready-made functions include `gnus-thread-sort-by-number',
754 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
755 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
756 `gnus-thread-sort-by-most-recent-number',
757 `gnus-thread-sort-by-most-recent-date',
758 `gnus-thread-sort-by-random', and
759 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
760
761 When threading is turned off, the variable
762 `gnus-article-sort-functions' controls how articles are sorted."
763   :group 'gnus-summary-sort
764   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
765                          (function-item gnus-thread-sort-by-author)
766                          (function-item gnus-thread-sort-by-subject)
767                          (function-item gnus-thread-sort-by-date)
768                          (function-item gnus-thread-sort-by-score)
769                          (function-item gnus-thread-sort-by-most-recent-number)
770                          (function-item gnus-thread-sort-by-most-recent-date)
771                          (function-item gnus-thread-sort-by-random)
772                          (function-item gnus-thread-sort-by-total-score)
773                          (function :tag "other"))))
774
775 (defcustom gnus-thread-score-function '+
776   "*Function used for calculating the total score of a thread.
777
778 The function is called with the scores of the article and each
779 subthread and should then return the score of the thread.
780
781 Some functions you can use are `+', `max', or `min'."
782   :group 'gnus-summary-sort
783   :type 'function)
784
785 (defcustom gnus-summary-expunge-below nil
786   "All articles that have a score less than this variable will be expunged.
787 This variable is local to the summary buffers."
788   :group 'gnus-score-default
789   :type '(choice (const :tag "off" nil)
790                  integer))
791
792 (defcustom gnus-thread-expunge-below nil
793   "All threads that have a total score less than this variable will be expunged.
794 See `gnus-thread-score-function' for en explanation of what a
795 \"thread score\" is.
796
797 This variable is local to the summary buffers."
798   :group 'gnus-threading
799   :group 'gnus-score-default
800   :type '(choice (const :tag "off" nil)
801                  integer))
802
803 (defcustom gnus-summary-mode-hook nil
804   "*A hook for Gnus summary mode.
805 This hook is run before any variables are set in the summary buffer."
806   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
807   :group 'gnus-summary-various
808   :type 'hook)
809
810 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
811 (when (featurep 'xemacs)
812   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
813   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
814   (add-hook 'gnus-summary-mode-hook
815             'gnus-xmas-switch-horizontal-scrollbar-off))
816
817 (defcustom gnus-summary-menu-hook nil
818   "*Hook run after the creation of the summary mode menu."
819   :group 'gnus-summary-visual
820   :type 'hook)
821
822 (defcustom gnus-summary-exit-hook nil
823   "*A hook called on exit from the summary buffer.
824 It will be called with point in the group buffer."
825   :group 'gnus-summary-exit
826   :type 'hook)
827
828 (defcustom gnus-summary-prepare-hook nil
829   "*A hook called after the summary buffer has been generated.
830 If you want to modify the summary buffer, you can use this hook."
831   :group 'gnus-summary-various
832   :type 'hook)
833
834 (defcustom gnus-summary-prepared-hook nil
835   "*A hook called as the last thing after the summary buffer has been generated."
836   :group 'gnus-summary-various
837   :type 'hook)
838
839 (defcustom gnus-summary-generate-hook nil
840   "*A hook run just before generating the summary buffer.
841 This hook is commonly used to customize threading variables and the
842 like."
843   :group 'gnus-summary-various
844   :type 'hook)
845
846 (defcustom gnus-select-group-hook nil
847   "*A hook called when a newsgroup is selected.
848
849 If you'd like to simplify subjects like the
850 `gnus-summary-next-same-subject' command does, you can use the
851 following hook:
852
853  (add-hook gnus-select-group-hook
854            (lambda ()
855              (mapcar (lambda (header)
856                        (mail-header-set-subject
857                         header
858                         (gnus-simplify-subject
859                          (mail-header-subject header) 're-only)))
860                      gnus-newsgroup-headers)))"
861   :group 'gnus-group-select
862   :type 'hook)
863
864 (defcustom gnus-select-article-hook nil
865   "*A hook called when an article is selected."
866   :group 'gnus-summary-choose
867   :options '(gnus-agent-fetch-selected-article)
868   :type 'hook)
869
870 (defcustom gnus-visual-mark-article-hook
871   (list 'gnus-highlight-selected-summary)
872   "*Hook run after selecting an article in the summary buffer.
873 It is meant to be used for highlighting the article in some way.  It
874 is not run if `gnus-visual' is nil."
875   :group 'gnus-summary-visual
876   :type 'hook)
877
878 (defcustom gnus-parse-headers-hook '(gnus-summary-inherit-default-charset)
879   "*A hook called before parsing the headers."
880   :group 'gnus-various
881   :type 'hook)
882
883 (defcustom gnus-exit-group-hook nil
884   "*A hook called when exiting summary mode.
885 This hook is not called from the non-updating exit commands like `Q'."
886   :group 'gnus-various
887   :type 'hook)
888
889 (defcustom gnus-summary-update-hook
890   (list 'gnus-summary-highlight-line)
891   "*A hook called when a summary line is changed.
892 The hook will not be called if `gnus-visual' is nil.
893
894 The default function `gnus-summary-highlight-line' will
895 highlight the line according to the `gnus-summary-highlight'
896 variable."
897   :group 'gnus-summary-visual
898   :type 'hook)
899
900 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
901   "*A hook called when an article is selected for the first time.
902 The hook is intended to mark an article as read (or unread)
903 automatically when it is selected."
904   :group 'gnus-summary-choose
905   :type 'hook)
906
907 (defcustom gnus-group-no-more-groups-hook nil
908   "*A hook run when returning to group mode having no more (unread) groups."
909   :group 'gnus-group-select
910   :type 'hook)
911
912 (defcustom gnus-ps-print-hook nil
913   "*A hook run before ps-printing something from Gnus."
914   :group 'gnus-summary
915   :type 'hook)
916
917 (defcustom gnus-summary-article-move-hook nil
918   "*A hook called after an article is moved, copied, respooled, or crossposted."
919   :group 'gnus-summary
920   :type 'hook)
921
922 (defcustom gnus-summary-article-delete-hook nil
923   "*A hook called after an article is deleted."
924   :group 'gnus-summary
925   :type 'hook)
926
927 (defcustom gnus-summary-article-expire-hook nil
928   "*A hook called after an article is expired."
929   :group 'gnus-summary
930   :type 'hook)
931
932 (defcustom gnus-summary-display-arrow
933   (and (fboundp 'display-graphic-p)
934        (display-graphic-p))
935   "*If non-nil, display an arrow highlighting the current article."
936   :version "21.1"
937   :group 'gnus-summary
938   :type 'boolean)
939
940 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
941   "Face used for highlighting the current article in the summary buffer."
942   :group 'gnus-summary-visual
943   :type 'face)
944
945 (defvar gnus-tmp-downloaded nil)
946
947 (defcustom gnus-summary-highlight
948   '(((eq mark gnus-canceled-mark)
949      . gnus-summary-cancelled-face)
950     ((and uncached (> score default-high))
951      . gnus-summary-high-undownloaded-face)
952     ((and uncached (< score default-low))
953      . gnus-summary-low-undownloaded-face)
954     (uncached
955      . gnus-summary-normal-undownloaded-face)
956     ((and (> score default-high)
957           (or (eq mark gnus-dormant-mark)
958               (eq mark gnus-ticked-mark)))
959      . gnus-summary-high-ticked-face)
960     ((and (< score default-low)
961           (or (eq mark gnus-dormant-mark)
962               (eq mark gnus-ticked-mark)))
963      . gnus-summary-low-ticked-face)
964     ((or (eq mark gnus-dormant-mark)
965          (eq mark gnus-ticked-mark))
966      . gnus-summary-normal-ticked-face)
967     ((and (> score default-high) (eq mark gnus-ancient-mark))
968      . gnus-summary-high-ancient-face)
969     ((and (< score default-low) (eq mark gnus-ancient-mark))
970      . gnus-summary-low-ancient-face)
971     ((eq mark gnus-ancient-mark)
972      . gnus-summary-normal-ancient-face)
973     ((and (> score default-high) (eq mark gnus-unread-mark))
974      . gnus-summary-high-unread-face)
975     ((and (< score default-low) (eq mark gnus-unread-mark))
976      . gnus-summary-low-unread-face)
977     ((eq mark gnus-unread-mark)
978      . gnus-summary-normal-unread-face)
979     ((> score default-high)
980      . gnus-summary-high-read-face)
981     ((< score default-low)
982      . gnus-summary-low-read-face)
983     (t
984      . gnus-summary-normal-read-face))
985   "*Controls the highlighting of summary buffer lines.
986
987 A list of (FORM . FACE) pairs.  When deciding how a a particular
988 summary line should be displayed, each form is evaluated.  The content
989 of the face field after the first true form is used.  You can change
990 how those summary lines are displayed, by editing the face field.
991
992 You can use the following variables in the FORM field.
993
994 score:        The article's score
995 default:      The default article score.
996 default-high: The default score for high scored articles.
997 default-low:  The default score for low scored articles.
998 below:        The score below which articles are automatically marked as read.
999 mark:         The article's mark.
1000 uncached:     Non-nil if the article is uncached."
1001   :group 'gnus-summary-visual
1002   :type '(repeat (cons (sexp :tag "Form" nil)
1003                        face)))
1004
1005 (defcustom gnus-alter-header-function nil
1006   "Function called to allow alteration of article header structures.
1007 The function is called with one parameter, the article header vector,
1008 which it may alter in any way."
1009   :type '(choice (const :tag "None" nil)
1010                  function)
1011   :group 'gnus-summary)
1012
1013 (defvar gnus-decode-encoded-word-function
1014   (mime-find-field-decoder 'From 'nov)
1015   "Variable that says which function should be used to decode a string with encoded words.")
1016
1017 (defcustom gnus-extra-headers '(To Newsgroups)
1018   "*Extra headers to parse."
1019   :version "21.1"
1020   :group 'gnus-summary
1021   :type '(repeat symbol))
1022
1023 (defcustom gnus-ignored-from-addresses
1024   (and user-mail-address (regexp-quote user-mail-address))
1025   "*Regexp of From headers that may be suppressed in favor of To headers."
1026   :version "21.1"
1027   :group 'gnus-summary
1028   :type 'regexp)
1029
1030 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1031   "List of charsets that should be ignored.
1032 When these charsets are used in the \"charset\" parameter, the
1033 default charset will be used instead."
1034   :version "21.1"
1035   :type '(repeat symbol)
1036   :group 'gnus-charset)
1037
1038 (gnus-define-group-parameter
1039  ignored-charsets
1040  :type list
1041  :function-document
1042  "Return the ignored charsets of GROUP."
1043  :variable gnus-group-ignored-charsets-alist
1044  :variable-default
1045  '(("alt\\.chinese\\.text" iso-8859-1))
1046  :variable-document
1047  "Alist of regexps (to match group names) and charsets that should be ignored.
1048 When these charsets are used in the \"charset\" parameter, the
1049 default charset will be used instead."
1050  :variable-group gnus-charset
1051  :variable-type '(repeat (cons (regexp :tag "Group")
1052                                (repeat symbol)))
1053  :parameter-type '(choice :tag "Ignored charsets"
1054                           :value nil
1055                           (repeat (symbol)))
1056  :parameter-document       "\
1057 List of charsets that should be ignored.
1058
1059 When these charsets are used in the \"charset\" parameter, the
1060 default charset will be used instead.")
1061
1062 (defcustom gnus-group-highlight-words-alist nil
1063   "Alist of group regexps and highlight regexps.
1064 This variable uses the same syntax as `gnus-emphasis-alist'."
1065   :version "21.1"
1066   :type '(repeat (cons (regexp :tag "Group")
1067                        (repeat (list (regexp :tag "Highlight regexp")
1068                                      (number :tag "Group for entire word" 0)
1069                                      (number :tag "Group for displayed part" 0)
1070                                      (symbol :tag "Face"
1071                                              gnus-emphasis-highlight-words)))))
1072   :group 'gnus-summary-visual)
1073
1074 (defcustom gnus-use-wheel nil
1075   "Use Intelli-mouse on summary movement"
1076   :type 'boolean
1077   :group 'gnus-summary-maneuvering)
1078
1079 (defcustom gnus-wheel-scroll-amount '(5 . 1)
1080   "Amount to scroll messages by spinning the mouse wheel.
1081 This is actually a cons cell, where the first item is the amount to scroll
1082 on a normal wheel event, and the second is the amount to scroll when the
1083 wheel is moved with the shift key depressed."
1084   :type '(cons (integer :tag "Shift") integer)
1085   :group 'gnus-summary-maneuvering)
1086
1087 (defcustom gnus-wheel-edge-resistance 2
1088   "How hard it should be to change the current article
1089 by moving the mouse over the edge of the article window."
1090   :type 'integer
1091   :group 'gnus-summary-maneuvering)
1092
1093 (defcustom gnus-summary-show-article-charset-alist
1094   nil
1095   "Alist of number and charset.
1096 The article will be shown with the charset corresponding to the
1097 numbered argument.
1098 For example: ((1 . cn-gb-2312) (2 . big5))."
1099   :version "21.1"
1100   :type '(repeat (cons (number :tag "Argument" 1)
1101                        (symbol :tag "Charset")))
1102   :group 'gnus-charset)
1103
1104 (defcustom gnus-preserve-marks t
1105   "Whether marks are preserved when moving, copying and respooling messages."
1106   :version "21.1"
1107   :type 'boolean
1108   :group 'gnus-summary-marks)
1109
1110 (defcustom gnus-alter-articles-to-read-function nil
1111   "Function to be called to alter the list of articles to be selected."
1112   :type '(choice (const nil) function)
1113   :group 'gnus-summary)
1114
1115 (defcustom gnus-orphan-score nil
1116   "*All orphans get this score added.  Set in the score file."
1117   :group 'gnus-score-default
1118   :type '(choice (const nil)
1119                  integer))
1120
1121 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1122   "*A regexp to match MIME parts when saving multiple parts of a
1123 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1124 This regexp will be used by default when prompting the user for which
1125 type of files to save."
1126   :group 'gnus-summary
1127   :type 'regexp)
1128
1129 (defcustom gnus-read-all-available-headers nil
1130   "Whether Gnus should parse all headers made available to it.
1131 This is mostly relevant for slow back ends where the user may
1132 wish to widen the summary buffer to include all headers
1133 that were fetched.  Say, for nnultimate groups."
1134   :group 'gnus-summary
1135   :type '(choice boolean regexp))
1136
1137 (defcustom gnus-summary-muttprint-program "muttprint"
1138   "Command (and optional arguments) used to run Muttprint."
1139   :version "21.3"
1140   :group 'gnus-summary
1141   :type 'string)
1142
1143 (defcustom gnus-article-loose-mime nil
1144   "If non-nil, don't require MIME-Version header.
1145 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1146 supply the MIME-Version header or deliberately strip it From the mail.
1147 Set it to non-nil, Gnus will treat some articles as MIME even if
1148 the MIME-Version header is missed."
1149   :version "21.3"
1150   :type 'boolean
1151   :group 'gnus-article-mime)
1152
1153 (defcustom gnus-article-emulate-mime t
1154   "If non-nil, use MIME emulation for uuencode and the like.
1155 This means that Gnus will search message bodies for text that look
1156 like uuencoded bits, yEncoded bits, and so on, and present that using
1157 the normal Gnus MIME machinery."
1158   :type 'boolean
1159   :group 'gnus-article-mime)
1160
1161 ;;; Internal variables
1162
1163 (defvar gnus-summary-display-cache nil)
1164 (defvar gnus-article-mime-handles nil)
1165 (defvar gnus-article-decoded-p nil)
1166 (defvar gnus-article-charset nil)
1167 (defvar gnus-article-ignored-charsets nil)
1168 (defvar gnus-scores-exclude-files nil)
1169 (defvar gnus-page-broken nil)
1170
1171 (defvar gnus-original-article nil)
1172 (defvar gnus-article-internal-prepare-hook nil)
1173 (defvar gnus-newsgroup-process-stack nil)
1174
1175 (defvar gnus-thread-indent-array nil)
1176 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1177 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1178   "Function called to sort the articles within a thread after it has been gathered together.")
1179
1180 (defvar gnus-summary-save-parts-type-history nil)
1181 (defvar gnus-summary-save-parts-last-directory nil)
1182
1183 ;; Avoid highlighting in kill files.
1184 (defvar gnus-summary-inhibit-highlight nil)
1185 (defvar gnus-newsgroup-selected-overlay nil)
1186 (defvar gnus-inhibit-limiting nil)
1187 (defvar gnus-newsgroup-adaptive-score-file nil)
1188 (defvar gnus-current-score-file nil)
1189 (defvar gnus-current-move-group nil)
1190 (defvar gnus-current-copy-group nil)
1191 (defvar gnus-current-crosspost-group nil)
1192 (defvar gnus-newsgroup-display nil)
1193
1194 (defvar gnus-newsgroup-dependencies nil)
1195 (defvar gnus-newsgroup-adaptive nil)
1196 (defvar gnus-summary-display-article-function nil)
1197 (defvar gnus-summary-highlight-line-function nil
1198   "Function called after highlighting a summary line.")
1199
1200 (defvar gnus-summary-line-format-alist
1201   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1202     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1203     (?s gnus-tmp-subject-or-nil ?s)
1204     (?n gnus-tmp-name ?s)
1205     (?A (std11-address-string
1206          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1207     (?a (or (std11-full-name-string
1208              (car (mime-entity-read-field gnus-tmp-header 'From)))
1209             gnus-tmp-from) ?s)
1210     (?F gnus-tmp-from ?s)
1211     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1212     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1213     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1214     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1215     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1216     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1217     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1218     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1219     (?L gnus-tmp-lines ?s)
1220     (?O gnus-tmp-downloaded ?c)
1221     (?I gnus-tmp-indentation ?s)
1222     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1223     (?R gnus-tmp-replied ?c)
1224     (?\[ gnus-tmp-opening-bracket ?c)
1225     (?\] gnus-tmp-closing-bracket ?c)
1226     (?\> (make-string gnus-tmp-level ? ) ?s)
1227     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1228     (?i gnus-tmp-score ?d)
1229     (?z gnus-tmp-score-char ?c)
1230     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1231     (?U gnus-tmp-unread ?c)
1232     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1233         ?s)
1234     (?t (gnus-summary-number-of-articles-in-thread
1235          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1236         ?d)
1237     (?e (gnus-summary-number-of-articles-in-thread
1238          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1239         ?c)
1240     (?u gnus-tmp-user-defined ?s)
1241     (?P (gnus-pick-line-number) ?d)
1242     (?B gnus-tmp-thread-tree-header-string ?s)
1243     (user-date (gnus-user-date
1244                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1245   "An alist of format specifications that can appear in summary lines.
1246 These are paired with what variables they correspond with, along with
1247 the type of the variable (string, integer, character, etc).")
1248
1249 (defvar gnus-summary-dummy-line-format-alist
1250   `((?S gnus-tmp-subject ?s)
1251     (?N gnus-tmp-number ?d)
1252     (?u gnus-tmp-user-defined ?s)))
1253
1254 (defvar gnus-summary-mode-line-format-alist
1255   `((?G gnus-tmp-group-name ?s)
1256     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1257     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1258     (?A gnus-tmp-article-number ?d)
1259     (?Z gnus-tmp-unread-and-unselected ?s)
1260     (?V gnus-version ?s)
1261     (?U gnus-tmp-unread-and-unticked ?d)
1262     (?S gnus-tmp-subject ?s)
1263     (?e gnus-tmp-unselected ?d)
1264     (?u gnus-tmp-user-defined ?s)
1265     (?d (length gnus-newsgroup-dormant) ?d)
1266     (?t (length gnus-newsgroup-marked) ?d)
1267     (?h (length gnus-newsgroup-spam-marked) ?d)
1268     (?r (length gnus-newsgroup-reads) ?d)
1269     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1270     (?E gnus-newsgroup-expunged-tally ?d)
1271     (?s (gnus-current-score-file-nondirectory) ?s)))
1272
1273 (defvar gnus-last-search-regexp nil
1274   "Default regexp for article search command.")
1275
1276 (defvar gnus-summary-search-article-matched-data nil
1277   "Last matched data of article search command.  It is the local variable
1278 in `gnus-article-buffer' which consists of the list of start position,
1279 end position and text.")
1280
1281 (defvar gnus-last-shell-command nil
1282   "Default shell command on article.")
1283
1284 (defvar gnus-newsgroup-agentized nil
1285   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1286 (defvar gnus-newsgroup-begin nil)
1287 (defvar gnus-newsgroup-end nil)
1288 (defvar gnus-newsgroup-last-rmail nil)
1289 (defvar gnus-newsgroup-last-mail nil)
1290 (defvar gnus-newsgroup-last-folder nil)
1291 (defvar gnus-newsgroup-last-file nil)
1292 (defvar gnus-newsgroup-auto-expire nil)
1293 (defvar gnus-newsgroup-active nil)
1294
1295 (defvar gnus-newsgroup-data nil)
1296 (defvar gnus-newsgroup-data-reverse nil)
1297 (defvar gnus-newsgroup-limit nil)
1298 (defvar gnus-newsgroup-limits nil)
1299 (defvar gnus-summary-use-undownloaded-faces nil)
1300
1301 (defvar gnus-newsgroup-unreads nil
1302   "Sorted list of unread articles in the current newsgroup.")
1303
1304 (defvar gnus-newsgroup-unselected nil
1305   "Sorted list of unselected unread articles in the current newsgroup.")
1306
1307 (defvar gnus-newsgroup-reads nil
1308   "Alist of read articles and article marks in the current newsgroup.")
1309
1310 (defvar gnus-newsgroup-expunged-tally nil)
1311
1312 (defvar gnus-newsgroup-marked nil
1313   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1314
1315 (defvar gnus-newsgroup-spam-marked nil
1316   "List of ranges of articles that have been marked as spam.")
1317
1318 (defvar gnus-newsgroup-killed nil
1319   "List of ranges of articles that have been through the scoring process.")
1320
1321 (defvar gnus-newsgroup-cached nil
1322   "Sorted list of articles that come from the article cache.")
1323
1324 (defvar gnus-newsgroup-saved nil
1325   "List of articles that have been saved.")
1326
1327 (defvar gnus-newsgroup-kill-headers nil)
1328
1329 (defvar gnus-newsgroup-replied nil
1330   "List of articles that have been replied to in the current newsgroup.")
1331
1332 (defvar gnus-newsgroup-forwarded nil
1333   "List of articles that have been forwarded in the current newsgroup.")
1334
1335 (defvar gnus-newsgroup-recent nil
1336   "List of articles that have are recent in the current newsgroup.")
1337
1338 (defvar gnus-newsgroup-expirable nil
1339   "Sorted list of articles in the current newsgroup that can be expired.")
1340
1341 (defvar gnus-newsgroup-processable nil
1342   "List of articles in the current newsgroup that can be processed.")
1343
1344 (defvar gnus-newsgroup-downloadable nil
1345   "Sorted list of articles in the current newsgroup that can be processed.")
1346
1347 (defvar gnus-newsgroup-unfetched nil
1348   "Sorted list of articles in the current newsgroup whose headers have
1349 not been fetched into the agent.
1350
1351 This list will always be a subset of gnus-newsgroup-undownloaded.")
1352
1353 (defvar gnus-newsgroup-undownloaded nil
1354   "List of articles in the current newsgroup that haven't been downloaded.")
1355
1356 (defvar gnus-newsgroup-unsendable nil
1357   "List of articles in the current newsgroup that won't be sent.")
1358
1359 (defvar gnus-newsgroup-bookmarks nil
1360   "List of articles in the current newsgroup that have bookmarks.")
1361
1362 (defvar gnus-newsgroup-dormant nil
1363   "Sorted list of dormant articles in the current newsgroup.")
1364
1365 (defvar gnus-newsgroup-unseen nil
1366   "List of unseen articles in the current newsgroup.")
1367
1368 (defvar gnus-newsgroup-seen nil
1369   "Range of seen articles in the current newsgroup.")
1370
1371 (defvar gnus-newsgroup-articles nil
1372   "List of articles in the current newsgroup.")
1373
1374 (defvar gnus-newsgroup-scored nil
1375   "List of scored articles in the current newsgroup.")
1376
1377 (defvar gnus-newsgroup-incorporated nil
1378   "List of incorporated articles in the current newsgroup.")
1379
1380 (defvar gnus-newsgroup-headers nil
1381   "List of article headers in the current newsgroup.")
1382
1383 (defvar gnus-newsgroup-threads nil)
1384
1385 (defvar gnus-newsgroup-prepared nil
1386   "Whether the current group has been prepared properly.")
1387
1388 (defvar gnus-newsgroup-ancient nil
1389   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1390
1391 (defvar gnus-newsgroup-sparse nil)
1392
1393 (defvar gnus-current-article nil)
1394 (defvar gnus-article-current nil)
1395 (defvar gnus-current-headers nil)
1396 (defvar gnus-have-all-headers nil)
1397 (defvar gnus-last-article nil)
1398 (defvar gnus-newsgroup-history nil)
1399 (defvar gnus-newsgroup-charset nil)
1400 (defvar gnus-newsgroup-ephemeral-charset nil)
1401 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1402
1403 (defvar gnus-article-before-search nil)
1404
1405 (defvar gnus-summary-local-variables
1406   '(gnus-newsgroup-name
1407     gnus-newsgroup-begin gnus-newsgroup-end
1408     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1409     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1410     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1411     gnus-newsgroup-unselected gnus-newsgroup-marked
1412     gnus-newsgroup-spam-marked
1413     gnus-newsgroup-reads gnus-newsgroup-saved
1414     gnus-newsgroup-replied gnus-newsgroup-forwarded
1415     gnus-newsgroup-recent
1416     gnus-newsgroup-expirable
1417     gnus-newsgroup-processable gnus-newsgroup-killed
1418     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1419     gnus-newsgroup-unfetched
1420     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1421     gnus-newsgroup-seen gnus-newsgroup-articles
1422     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1423     gnus-newsgroup-headers gnus-newsgroup-threads
1424     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1425     gnus-current-article gnus-current-headers gnus-have-all-headers
1426     gnus-last-article gnus-article-internal-prepare-hook
1427     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1428     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1429     gnus-thread-expunge-below
1430     gnus-score-alist gnus-current-score-file
1431     (gnus-summary-expunge-below . global)
1432     (gnus-summary-mark-below . global)
1433     (gnus-orphan-score . global)
1434     gnus-newsgroup-active gnus-scores-exclude-files
1435     gnus-newsgroup-history gnus-newsgroup-ancient
1436     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1437     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1438     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1439     (gnus-newsgroup-expunged-tally . 0)
1440     gnus-cache-removable-articles gnus-newsgroup-cached
1441     gnus-newsgroup-data gnus-newsgroup-data-reverse
1442     gnus-newsgroup-limit gnus-newsgroup-limits
1443     gnus-newsgroup-charset gnus-newsgroup-display
1444     gnus-summary-use-undownloaded-faces
1445     gnus-newsgroup-incorporated)
1446   "Variables that are buffer-local to the summary buffers.")
1447
1448 (defvar gnus-newsgroup-variables nil
1449   "A list of variables that have separate values in different newsgroups.
1450 A list of newsgroup (summary buffer) local variables, or cons of
1451 variables and their default values (when the default values are not
1452 nil), that should be made global while the summary buffer is active.
1453 These variables can be used to set variables in the group parameters
1454 while still allowing them to affect operations done in other
1455 buffers. For example:
1456
1457 \(setq gnus-newsgroup-variables
1458      '(message-use-followup-to
1459        (gnus-visible-headers .
1460          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1461 ")
1462
1463 ;; Byte-compiler warning.
1464 (eval-when-compile
1465   ;; Bind features so that require will believe that gnus-sum has
1466   ;; already been loaded (avoids infinite recursion)
1467   (let ((features (cons 'gnus-sum features)))
1468     ;; Several of the declarations in gnus-sum are needed to load the
1469     ;; following files. Right now, these definitions have been
1470     ;; compiled but not defined (evaluated).  We could either do a
1471     ;; eval-and-compile about all of the declarations or evaluate the
1472     ;; source file.
1473     (if (boundp 'gnus-newsgroup-variables)
1474         nil
1475       (load "gnus-sum.el" t t t))
1476     (require 'gnus)
1477     (require 'gnus-agent)
1478     (require 'gnus-art)))
1479
1480 ;; Subject simplification.
1481
1482 (defun gnus-simplify-whitespace (str)
1483   "Remove excessive whitespace from STR."
1484   ;; Multiple spaces.
1485   (while (string-match "[ \t][ \t]+" str)
1486     (setq str (concat (substring str 0 (match-beginning 0))
1487                         " "
1488                         (substring str (match-end 0)))))
1489   ;; Leading spaces.
1490   (when (string-match "^[ \t]+" str)
1491     (setq str (substring str (match-end 0))))
1492   ;; Trailing spaces.
1493   (when (string-match "[ \t]+$" str)
1494     (setq str (substring str 0 (match-beginning 0))))
1495   str)
1496
1497 (defun gnus-simplify-all-whitespace (str)
1498   "Remove all whitespace from STR."
1499   (while (string-match "[ \t\n]+" str)
1500     (setq str (replace-match "" nil nil str)))
1501   str)
1502
1503 (defsubst gnus-simplify-subject-re (subject)
1504   "Remove \"Re:\" from subject lines."
1505   (if (string-match message-subject-re-regexp subject)
1506       (substring subject (match-end 0))
1507     subject))
1508
1509 (defun gnus-simplify-subject (subject &optional re-only)
1510   "Remove `Re:' and words in parentheses.
1511 If RE-ONLY is non-nil, strip leading `Re:'s only."
1512   (let ((case-fold-search t))           ;Ignore case.
1513     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1514     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1515       (setq subject (substring subject (match-end 0))))
1516     ;; Remove uninteresting prefixes.
1517     (when (and (not re-only)
1518                gnus-simplify-ignored-prefixes
1519                (string-match gnus-simplify-ignored-prefixes subject))
1520       (setq subject (substring subject (match-end 0))))
1521     ;; Remove words in parentheses from end.
1522     (unless re-only
1523       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1524         (setq subject (substring subject 0 (match-beginning 0)))))
1525     ;; Return subject string.
1526     subject))
1527
1528 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1529 ;; all whitespace.
1530 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1531   (goto-char (point-min))
1532   (while (re-search-forward regexp nil t)
1533     (replace-match (or newtext ""))))
1534
1535 (defun gnus-simplify-buffer-fuzzy ()
1536   "Simplify string in the buffer fuzzily.
1537 The string in the accessible portion of the current buffer is simplified.
1538 It is assumed to be a single-line subject.
1539 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1540 matter is removed.  Additional things can be deleted by setting
1541 `gnus-simplify-subject-fuzzy-regexp'."
1542   (let ((case-fold-search t)
1543         (modified-tick))
1544     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1545
1546     (while (not (eq modified-tick (buffer-modified-tick)))
1547       (setq modified-tick (buffer-modified-tick))
1548       (cond
1549        ((listp gnus-simplify-subject-fuzzy-regexp)
1550         (mapcar 'gnus-simplify-buffer-fuzzy-step
1551                 gnus-simplify-subject-fuzzy-regexp))
1552        (gnus-simplify-subject-fuzzy-regexp
1553         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1554       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1555       (gnus-simplify-buffer-fuzzy-step
1556        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1557       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1558
1559     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1560     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1561     (gnus-simplify-buffer-fuzzy-step " $")
1562     (gnus-simplify-buffer-fuzzy-step "^ +")))
1563
1564 (defun gnus-simplify-subject-fuzzy (subject)
1565   "Simplify a subject string fuzzily.
1566 See `gnus-simplify-buffer-fuzzy' for details."
1567   (save-excursion
1568     (gnus-set-work-buffer)
1569     (let ((case-fold-search t))
1570       ;; Remove uninteresting prefixes.
1571       (when (and gnus-simplify-ignored-prefixes
1572                  (string-match gnus-simplify-ignored-prefixes subject))
1573         (setq subject (substring subject (match-end 0))))
1574       (insert subject)
1575       (inline (gnus-simplify-buffer-fuzzy))
1576       (buffer-string))))
1577
1578 (defsubst gnus-simplify-subject-fully (subject)
1579   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1580   (cond
1581    (gnus-simplify-subject-functions
1582     (gnus-map-function gnus-simplify-subject-functions subject))
1583    ((null gnus-summary-gather-subject-limit)
1584     (gnus-simplify-subject-re subject))
1585    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1586     (gnus-simplify-subject-fuzzy subject))
1587    ((numberp gnus-summary-gather-subject-limit)
1588     (gnus-limit-string (gnus-simplify-subject-re subject)
1589                        gnus-summary-gather-subject-limit))
1590    (t
1591     subject)))
1592
1593 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1594   "Check whether two subjects are equal.
1595 If optional argument SIMPLE-FIRST is t, first argument is already
1596 simplified."
1597   (cond
1598    ((null simple-first)
1599     (equal (gnus-simplify-subject-fully s1)
1600            (gnus-simplify-subject-fully s2)))
1601    (t
1602     (equal s1
1603            (gnus-simplify-subject-fully s2)))))
1604
1605 (defun gnus-summary-bubble-group ()
1606   "Increase the score of the current group.
1607 This is a handy function to add to `gnus-summary-exit-hook' to
1608 increase the score of each group you read."
1609   (gnus-group-add-score gnus-newsgroup-name))
1610
1611 \f
1612 ;;;
1613 ;;; Gnus summary mode
1614 ;;;
1615
1616 (put 'gnus-summary-mode 'mode-class 'special)
1617
1618 (defvar gnus-article-commands-menu)
1619
1620 ;; Non-orthogonal keys
1621
1622 (gnus-define-keys gnus-summary-mode-map
1623   " " gnus-summary-next-page
1624   "\177" gnus-summary-prev-page
1625   [delete] gnus-summary-prev-page
1626   [backspace] gnus-summary-prev-page
1627   "\r" gnus-summary-scroll-up
1628   "\M-\r" gnus-summary-scroll-down
1629   "n" gnus-summary-next-unread-article
1630   "p" gnus-summary-prev-unread-article
1631   "N" gnus-summary-next-article
1632   "P" gnus-summary-prev-article
1633   "\M-\C-n" gnus-summary-next-same-subject
1634   "\M-\C-p" gnus-summary-prev-same-subject
1635   "\M-n" gnus-summary-next-unread-subject
1636   "\M-p" gnus-summary-prev-unread-subject
1637   "." gnus-summary-first-unread-article
1638   "," gnus-summary-best-unread-article
1639   "\M-s" gnus-summary-search-article-forward
1640   "\M-r" gnus-summary-search-article-backward
1641   "<" gnus-summary-beginning-of-article
1642   ">" gnus-summary-end-of-article
1643   "j" gnus-summary-goto-article
1644   "^" gnus-summary-refer-parent-article
1645   "\M-^" gnus-summary-refer-article
1646   "u" gnus-summary-tick-article-forward
1647   "!" gnus-summary-tick-article-forward
1648   "U" gnus-summary-tick-article-backward
1649   "d" gnus-summary-mark-as-read-forward
1650   "D" gnus-summary-mark-as-read-backward
1651   "E" gnus-summary-mark-as-expirable
1652   "\M-u" gnus-summary-clear-mark-forward
1653   "\M-U" gnus-summary-clear-mark-backward
1654   "k" gnus-summary-kill-same-subject-and-select
1655   "\C-k" gnus-summary-kill-same-subject
1656   "\M-\C-k" gnus-summary-kill-thread
1657   "\M-\C-l" gnus-summary-lower-thread
1658   "e" gnus-summary-edit-article
1659   "#" gnus-summary-mark-as-processable
1660   "\M-#" gnus-summary-unmark-as-processable
1661   "\M-\C-t" gnus-summary-toggle-threads
1662   "\M-\C-s" gnus-summary-show-thread
1663   "\M-\C-h" gnus-summary-hide-thread
1664   "\M-\C-f" gnus-summary-next-thread
1665   "\M-\C-b" gnus-summary-prev-thread
1666   [(meta down)] gnus-summary-next-thread
1667   [(meta up)] gnus-summary-prev-thread
1668   "\M-\C-u" gnus-summary-up-thread
1669   "\M-\C-d" gnus-summary-down-thread
1670   "&" gnus-summary-execute-command
1671   "c" gnus-summary-catchup-and-exit
1672   "\C-w" gnus-summary-mark-region-as-read
1673   "\C-t" gnus-summary-toggle-truncation
1674   "?" gnus-summary-mark-as-dormant
1675   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1676   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1677   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1678   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1679   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1680   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1681   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1682   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1683   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1684   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1685   "=" gnus-summary-expand-window
1686   "\C-x\C-s" gnus-summary-reselect-current-group
1687   "\M-g" gnus-summary-rescan-group
1688   "w" gnus-summary-stop-page-breaking
1689   "\C-c\C-r" gnus-summary-caesar-message
1690   "\M-t" gnus-summary-toggle-mime
1691   "f" gnus-summary-followup
1692   "F" gnus-summary-followup-with-original
1693   "C" gnus-summary-cancel-article
1694   "r" gnus-summary-reply
1695   "R" gnus-summary-reply-with-original
1696   "\C-c\C-f" gnus-summary-mail-forward
1697   "o" gnus-summary-save-article
1698   "\C-o" gnus-summary-save-article-mail
1699   "|" gnus-summary-pipe-output
1700   "\M-k" gnus-summary-edit-local-kill
1701   "\M-K" gnus-summary-edit-global-kill
1702   ;; "V" gnus-version
1703   "\C-c\C-d" gnus-summary-describe-group
1704   "q" gnus-summary-exit
1705   "Q" gnus-summary-exit-no-update
1706   "\C-c\C-i" gnus-info-find-node
1707   gnus-mouse-2 gnus-mouse-pick-article
1708   "m" gnus-summary-mail-other-window
1709   "a" gnus-summary-post-news
1710   "i" gnus-summary-news-other-window
1711   "x" gnus-summary-limit-to-unread
1712   "s" gnus-summary-isearch-article
1713   "t" gnus-summary-toggle-header
1714   "g" gnus-summary-show-article
1715   "l" gnus-summary-goto-last-article
1716   "v" gnus-summary-preview-mime-message
1717   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1718   "\C-d" gnus-summary-enter-digest-group
1719   "\M-\C-d" gnus-summary-read-document
1720   "\M-\C-e" gnus-summary-edit-parameters
1721   "\M-\C-a" gnus-summary-customize-parameters
1722   "\C-c\C-b" gnus-bug
1723   "\C-c\C-n" gnus-namazu-search
1724   "*" gnus-cache-enter-article
1725   "\M-*" gnus-cache-remove-article
1726   "\M-&" gnus-summary-universal-argument
1727   "\C-l" gnus-recenter
1728   "I" gnus-summary-increase-score
1729   "L" gnus-summary-lower-score
1730   "\M-i" gnus-symbolic-argument
1731   "h" gnus-summary-select-article-buffer
1732
1733   "V" gnus-summary-score-map
1734   "X" gnus-uu-extract-map
1735   "S" gnus-summary-send-map)
1736
1737   ;; Sort of orthogonal keymap
1738 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1739   "t" gnus-summary-tick-article-forward
1740   "!" gnus-summary-tick-article-forward
1741   "d" gnus-summary-mark-as-read-forward
1742   "r" gnus-summary-mark-as-read-forward
1743   "c" gnus-summary-clear-mark-forward
1744   " " gnus-summary-clear-mark-forward
1745   "e" gnus-summary-mark-as-expirable
1746   "x" gnus-summary-mark-as-expirable
1747   "?" gnus-summary-mark-as-dormant
1748   "b" gnus-summary-set-bookmark
1749   "B" gnus-summary-remove-bookmark
1750   "#" gnus-summary-mark-as-processable
1751   "\M-#" gnus-summary-unmark-as-processable
1752   "S" gnus-summary-limit-include-expunged
1753   "C" gnus-summary-catchup
1754   "H" gnus-summary-catchup-to-here
1755   "h" gnus-summary-catchup-from-here
1756   "\C-c" gnus-summary-catchup-all
1757   "k" gnus-summary-kill-same-subject-and-select
1758   "K" gnus-summary-kill-same-subject
1759   "P" gnus-uu-mark-map)
1760
1761 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1762   "c" gnus-summary-clear-above
1763   "u" gnus-summary-tick-above
1764   "m" gnus-summary-mark-above
1765   "k" gnus-summary-kill-below)
1766
1767 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1768   "/" gnus-summary-limit-to-subject
1769   "n" gnus-summary-limit-to-articles
1770   "w" gnus-summary-pop-limit
1771   "s" gnus-summary-limit-to-subject
1772   "a" gnus-summary-limit-to-author
1773   "u" gnus-summary-limit-to-unread
1774   "m" gnus-summary-limit-to-marks
1775   "M" gnus-summary-limit-exclude-marks
1776   "v" gnus-summary-limit-to-score
1777   "*" gnus-summary-limit-include-cached
1778   "D" gnus-summary-limit-include-dormant
1779   "T" gnus-summary-limit-include-thread
1780   "d" gnus-summary-limit-exclude-dormant
1781   "t" gnus-summary-limit-to-age
1782   "." gnus-summary-limit-to-unseen
1783   "x" gnus-summary-limit-to-extra
1784   "p" gnus-summary-limit-to-display-predicate
1785   "E" gnus-summary-limit-include-expunged
1786   "c" gnus-summary-limit-exclude-childless-dormant
1787   "C" gnus-summary-limit-mark-excluded-as-read
1788   "o" gnus-summary-insert-old-articles
1789   "N" gnus-summary-insert-new-articles
1790   "r" gnus-summary-limit-to-replied)
1791
1792 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1793   "n" gnus-summary-next-unread-article
1794   "p" gnus-summary-prev-unread-article
1795   "N" gnus-summary-next-article
1796   "P" gnus-summary-prev-article
1797   "\C-n" gnus-summary-next-same-subject
1798   "\C-p" gnus-summary-prev-same-subject
1799   "\M-n" gnus-summary-next-unread-subject
1800   "\M-p" gnus-summary-prev-unread-subject
1801   "f" gnus-summary-first-unread-article
1802   "b" gnus-summary-best-unread-article
1803   "j" gnus-summary-goto-article
1804   "g" gnus-summary-goto-subject
1805   "l" gnus-summary-goto-last-article
1806   "o" gnus-summary-pop-article)
1807
1808 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1809   "k" gnus-summary-kill-thread
1810   "l" gnus-summary-lower-thread
1811   "i" gnus-summary-raise-thread
1812   "T" gnus-summary-toggle-threads
1813   "t" gnus-summary-rethread-current
1814   "^" gnus-summary-reparent-thread
1815   "s" gnus-summary-show-thread
1816   "S" gnus-summary-show-all-threads
1817   "h" gnus-summary-hide-thread
1818   "H" gnus-summary-hide-all-threads
1819   "n" gnus-summary-next-thread
1820   "p" gnus-summary-prev-thread
1821   "u" gnus-summary-up-thread
1822   "o" gnus-summary-top-thread
1823   "d" gnus-summary-down-thread
1824   "#" gnus-uu-mark-thread
1825   "\M-#" gnus-uu-unmark-thread)
1826
1827 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1828   "g" gnus-summary-prepare
1829   "c" gnus-summary-insert-cached-articles
1830   "d" gnus-summary-insert-dormant-articles)
1831
1832 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1833   "c" gnus-summary-catchup-and-exit
1834   "C" gnus-summary-catchup-all-and-exit
1835   "E" gnus-summary-exit-no-update
1836   "J" gnus-summary-jump-to-other-group
1837   "Q" gnus-summary-exit
1838   "Z" gnus-summary-exit
1839   "n" gnus-summary-catchup-and-goto-next-group
1840   "R" gnus-summary-reselect-current-group
1841   "G" gnus-summary-rescan-group
1842   "N" gnus-summary-next-group
1843   "s" gnus-summary-save-newsrc
1844   "P" gnus-summary-prev-group)
1845
1846 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1847   " " gnus-summary-next-page
1848   "n" gnus-summary-next-page
1849   "\177" gnus-summary-prev-page
1850   [delete] gnus-summary-prev-page
1851   "p" gnus-summary-prev-page
1852   "\r" gnus-summary-scroll-up
1853   "\M-\r" gnus-summary-scroll-down
1854   "<" gnus-summary-beginning-of-article
1855   ">" gnus-summary-end-of-article
1856   "b" gnus-summary-beginning-of-article
1857   "e" gnus-summary-end-of-article
1858   "^" gnus-summary-refer-parent-article
1859   "r" gnus-summary-refer-parent-article
1860   "D" gnus-summary-enter-digest-group
1861   "R" gnus-summary-refer-references
1862   "T" gnus-summary-refer-thread
1863   "g" gnus-summary-show-article
1864   "s" gnus-summary-isearch-article
1865   "P" gnus-summary-print-article
1866   "M" gnus-mailing-list-insinuate
1867   "t" gnus-article-babel)
1868
1869 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1870   "b" gnus-article-add-buttons
1871   "B" gnus-article-add-buttons-to-head
1872   "o" gnus-article-treat-overstrike
1873   "e" gnus-article-emphasize
1874   "w" gnus-article-fill-cited-article
1875   "Q" gnus-article-fill-long-lines
1876   "C" gnus-article-capitalize-sentences
1877   "c" gnus-article-remove-cr
1878   "Z" gnus-article-decode-HZ
1879   "A" gnus-article-treat-ansi-sequences
1880   "h" gnus-article-wash-html
1881   "u" gnus-article-unsplit-urls
1882   "f" gnus-article-display-x-face
1883   "l" gnus-summary-stop-page-breaking
1884   "r" gnus-summary-caesar-message
1885   "m" gnus-summary-morse-message
1886   "t" gnus-summary-toggle-header
1887   "g" gnus-treat-smiley
1888   "v" gnus-summary-verbose-headers
1889   "m" gnus-summary-toggle-mime
1890   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1891   "p" gnus-article-verify-x-pgp-sig
1892   "d" gnus-article-treat-dumbquotes)
1893
1894 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1895   ;; mnemonic: deuglif*Y*
1896   "u" gnus-article-outlook-unwrap-lines
1897   "a" gnus-article-outlook-repair-attribution
1898   "c" gnus-article-outlook-rearrange-citation
1899   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1900
1901 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1902   "a" gnus-article-hide
1903   "h" gnus-article-hide-headers
1904   "b" gnus-article-hide-boring-headers
1905   "s" gnus-article-hide-signature
1906   "c" gnus-article-hide-citation
1907   "C" gnus-article-hide-citation-in-followups
1908   "l" gnus-article-hide-list-identifiers
1909   "B" gnus-article-strip-banner
1910   "P" gnus-article-hide-pem
1911   "\C-c" gnus-article-hide-citation-maybe)
1912
1913 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1914   "a" gnus-article-highlight
1915   "h" gnus-article-highlight-headers
1916   "c" gnus-article-highlight-citation
1917   "s" gnus-article-highlight-signature)
1918
1919 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1920   "f" gnus-article-treat-fold-headers
1921   "u" gnus-article-treat-unfold-headers
1922   "n" gnus-article-treat-fold-newsgroups)
1923
1924 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1925   "x" gnus-article-display-x-face
1926   "d" gnus-article-display-face
1927   "s" gnus-treat-smiley
1928   "D" gnus-article-remove-images
1929   "f" gnus-treat-from-picon
1930   "m" gnus-treat-mail-picon
1931   "n" gnus-treat-newsgroups-picon)
1932
1933 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1934   "z" gnus-article-date-ut
1935   "u" gnus-article-date-ut
1936   "l" gnus-article-date-local
1937   "p" gnus-article-date-english
1938   "e" gnus-article-date-lapsed
1939   "o" gnus-article-date-original
1940   "i" gnus-article-date-iso8601
1941   "s" gnus-article-date-user)
1942
1943 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1944   "t" gnus-article-remove-trailing-blank-lines
1945   "l" gnus-article-strip-leading-blank-lines
1946   "m" gnus-article-strip-multiple-blank-lines
1947   "a" gnus-article-strip-blank-lines
1948   "A" gnus-article-strip-all-blank-lines
1949   "s" gnus-article-strip-leading-space
1950   "e" gnus-article-strip-trailing-space
1951   "w" gnus-article-remove-leading-whitespace)
1952
1953 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1954   "v" gnus-version
1955   "f" gnus-summary-fetch-faq
1956   "d" gnus-summary-describe-group
1957   "h" gnus-summary-describe-briefly
1958   "i" gnus-info-find-node
1959   "c" gnus-group-fetch-charter
1960   "C" gnus-group-fetch-control)
1961
1962 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1963   "e" gnus-summary-expire-articles
1964   "\M-\C-e" gnus-summary-expire-articles-now
1965   "\177" gnus-summary-delete-article
1966   [delete] gnus-summary-delete-article
1967   [backspace] gnus-summary-delete-article
1968   "m" gnus-summary-move-article
1969   "r" gnus-summary-respool-article
1970   "w" gnus-summary-edit-article
1971   "c" gnus-summary-copy-article
1972   "B" gnus-summary-crosspost-article
1973   "q" gnus-summary-respool-query
1974   "t" gnus-summary-respool-trace
1975   "i" gnus-summary-import-article
1976   "I" gnus-summary-create-article
1977   "p" gnus-summary-article-posted-p)
1978
1979 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1980   "o" gnus-summary-save-article
1981   "m" gnus-summary-save-article-mail
1982   "F" gnus-summary-write-article-file
1983   "r" gnus-summary-save-article-rmail
1984   "f" gnus-summary-save-article-file
1985   "b" gnus-summary-save-article-body-file
1986   "h" gnus-summary-save-article-folder
1987   "v" gnus-summary-save-article-vm
1988   "p" gnus-summary-pipe-output
1989   "P" gnus-summary-muttprint
1990   "s" gnus-soup-add-article)
1991
1992 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1993   "b" gnus-summary-display-buttonized
1994   "m" gnus-summary-repair-multipart
1995   "v" gnus-article-view-part
1996   "o" gnus-article-save-part
1997   "c" gnus-article-copy-part
1998   "C" gnus-article-view-part-as-charset
1999   "e" gnus-article-view-part-externally
2000   "E" gnus-article-encrypt-body
2001   "i" gnus-article-inline-part
2002   "|" gnus-article-pipe-part)
2003
2004 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2005   "p" gnus-summary-mark-as-processable
2006   "u" gnus-summary-unmark-as-processable
2007   "U" gnus-summary-unmark-all-processable
2008   "v" gnus-uu-mark-over
2009   "s" gnus-uu-mark-series
2010   "r" gnus-uu-mark-region
2011   "g" gnus-uu-unmark-region
2012   "R" gnus-uu-mark-by-regexp
2013   "G" gnus-uu-unmark-by-regexp
2014   "t" gnus-uu-mark-thread
2015   "T" gnus-uu-unmark-thread
2016   "a" gnus-uu-mark-all
2017   "b" gnus-uu-mark-buffer
2018   "S" gnus-uu-mark-sparse
2019   "k" gnus-summary-kill-process-mark
2020   "y" gnus-summary-yank-process-mark
2021   "w" gnus-summary-save-process-mark
2022   "i" gnus-uu-invert-processable)
2023
2024 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2025   ;;"x" gnus-uu-extract-any
2026   "m" gnus-summary-save-parts
2027   "u" gnus-uu-decode-uu
2028   "U" gnus-uu-decode-uu-and-save
2029   "s" gnus-uu-decode-unshar
2030   "S" gnus-uu-decode-unshar-and-save
2031   "o" gnus-uu-decode-save
2032   "O" gnus-uu-decode-save
2033   "b" gnus-uu-decode-binhex
2034   "B" gnus-uu-decode-binhex
2035   "p" gnus-uu-decode-postscript
2036   "P" gnus-uu-decode-postscript-and-save)
2037
2038 (gnus-define-keys
2039     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2040   "u" gnus-uu-decode-uu-view
2041   "U" gnus-uu-decode-uu-and-save-view
2042   "s" gnus-uu-decode-unshar-view
2043   "S" gnus-uu-decode-unshar-and-save-view
2044   "o" gnus-uu-decode-save-view
2045   "O" gnus-uu-decode-save-view
2046   "b" gnus-uu-decode-binhex-view
2047   "B" gnus-uu-decode-binhex-view
2048   "p" gnus-uu-decode-postscript-view
2049   "P" gnus-uu-decode-postscript-and-save-view)
2050
2051 (defvar gnus-article-post-menu nil)
2052
2053 (defconst gnus-summary-menu-maxlen 20)
2054
2055 (defun gnus-summary-menu-split (menu)
2056   ;; If we have lots of elements, divide them into groups of 20
2057   ;; and make a pane (or submenu) for each one.
2058   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2059       (let ((menu menu) sublists next
2060             (i 1))
2061         (while menu
2062           ;; Pull off the next gnus-summary-menu-maxlen elements
2063           ;; and make them the next element of sublist.
2064           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2065           (if next
2066               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2067                       nil))
2068           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2069                                              (aref (car (last menu)) 0)) menu)
2070                                sublists))
2071           (setq i (1+ i))
2072           (setq menu next))
2073         (nreverse sublists))
2074     ;; Few elements--put them all in one pane.
2075     menu))
2076
2077 (defun gnus-summary-make-menu-bar ()
2078   (gnus-turn-off-edit-menu 'summary)
2079
2080   (unless (boundp 'gnus-summary-misc-menu)
2081
2082     (easy-menu-define
2083      gnus-summary-kill-menu gnus-summary-mode-map ""
2084      (cons
2085       "Score"
2086       (nconc
2087        (list
2088         ["Customize" gnus-score-customize t])
2089        (gnus-make-score-map 'increase)
2090        (gnus-make-score-map 'lower)
2091        '(("Mark"
2092           ["Kill below" gnus-summary-kill-below t]
2093           ["Mark above" gnus-summary-mark-above t]
2094           ["Tick above" gnus-summary-tick-above t]
2095           ["Clear above" gnus-summary-clear-above t])
2096          ["Current score" gnus-summary-current-score t]
2097          ["Set score" gnus-summary-set-score t]
2098          ["Switch current score file..." gnus-score-change-score-file t]
2099          ["Set mark below..." gnus-score-set-mark-below t]
2100          ["Set expunge below..." gnus-score-set-expunge-below t]
2101          ["Edit current score file" gnus-score-edit-current-scores t]
2102          ["Edit score file" gnus-score-edit-file t]
2103          ["Trace score" gnus-score-find-trace t]
2104          ["Find words" gnus-score-find-favourite-words t]
2105          ["Rescore buffer" gnus-summary-rescore t]
2106          ["Increase score..." gnus-summary-increase-score t]
2107          ["Lower score..." gnus-summary-lower-score t]))))
2108
2109     ;; Define both the Article menu in the summary buffer and the
2110     ;; equivalent Commands menu in the article buffer here for
2111     ;; consistency.
2112     (let ((innards
2113            `(("Hide"
2114               ["All" gnus-article-hide t]
2115               ["Headers" gnus-article-hide-headers t]
2116               ["Signature" gnus-article-hide-signature t]
2117               ["Citation" gnus-article-hide-citation t]
2118               ["List identifiers" gnus-article-hide-list-identifiers t]
2119               ["Banner" gnus-article-strip-banner t]
2120               ["Boring headers" gnus-article-hide-boring-headers t])
2121              ("Highlight"
2122               ["All" gnus-article-highlight t]
2123               ["Headers" gnus-article-highlight-headers t]
2124               ["Signature" gnus-article-highlight-signature t]
2125               ["Citation" gnus-article-highlight-citation t])
2126              ("Date"
2127               ["Local" gnus-article-date-local t]
2128               ["ISO8601" gnus-article-date-iso8601 t]
2129               ["UT" gnus-article-date-ut t]
2130               ["Original" gnus-article-date-original t]
2131               ["Lapsed" gnus-article-date-lapsed t]
2132               ["User-defined" gnus-article-date-user t])
2133              ("Display"
2134               ["Remove images" gnus-article-remove-images t]
2135               ["Toggle smiley" gnus-treat-smiley t]
2136               ["Show X-Face" gnus-article-display-x-face t]
2137               ["Show picons in From" gnus-treat-from-picon t]
2138               ["Show picons in mail headers" gnus-treat-mail-picon t]
2139               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2140               ("View as different encoding"
2141                ,@(gnus-summary-menu-split
2142                   (mapcar
2143                    (lambda (cs)
2144                      ;; Since easymenu under Emacs doesn't allow
2145                      ;; lambda forms for menu commands, we should
2146                      ;; provide intern'ed function symbols.
2147                      (let ((command (intern (format "\
2148 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2149                        (fset command
2150                              `(lambda ()
2151                                 (interactive)
2152                                 (let ((gnus-summary-show-article-charset-alist
2153                                        '((1 . ,cs))))
2154                                   (gnus-summary-show-article 1))))
2155                        `[,(symbol-name cs) ,command t]))
2156                    (sort (if (fboundp 'coding-system-list)
2157                              (coding-system-list)
2158                            ;;(mapcar 'car mm-mime-mule-charset-alist)
2159                            )
2160                          'string<)))))
2161              ("Washing"
2162               ("Remove Blanks"
2163                ["Leading" gnus-article-strip-leading-blank-lines t]
2164                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2165                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2166                ["All of the above" gnus-article-strip-blank-lines t]
2167                ["All" gnus-article-strip-all-blank-lines t]
2168                ["Leading space" gnus-article-strip-leading-space t]
2169                ["Trailing space" gnus-article-strip-trailing-space t]
2170                ["Leading space in headers"
2171                 gnus-article-remove-leading-whitespace t])
2172               ["Overstrike" gnus-article-treat-overstrike t]
2173               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2174               ["Emphasis" gnus-article-emphasize t]
2175               ["Word wrap" gnus-article-fill-cited-article t]
2176               ["Fill long lines" gnus-article-fill-long-lines t]
2177               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2178               ["Remove CR" gnus-article-remove-cr t]
2179               ["Rot 13" gnus-summary-caesar-message
2180                ,@(if (featurep 'xemacs) '(t)
2181                    '(:help "\"Caesar rotate\" article by 13"))]
2182               ["Morse decode" gnus-summary-morse-message t]
2183               ["Unix pipe..." gnus-summary-pipe-message t]
2184               ["Add buttons" gnus-article-add-buttons t]
2185               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2186               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2187               ["Toggle MIME" gnus-summary-toggle-mime t]
2188               ["Verbose header" gnus-summary-verbose-headers t]
2189               ["Toggle header" gnus-summary-toggle-header t]
2190               ["Unfold headers" gnus-article-treat-unfold-headers t]
2191               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2192               ["Html" gnus-article-wash-html t]
2193               ["Unsplit URLs" gnus-article-unsplit-urls t]
2194               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2195               ["Decode HZ" gnus-article-decode-HZ t]
2196               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2197               ("(Outlook) Deuglify"
2198                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2199                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2200                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2201                ["Full (Outlook) deuglify"
2202                 gnus-article-outlook-deuglify-article t])
2203               )
2204              ("Output"
2205               ["Save in default format..." gnus-summary-save-article
2206                ,@(if (featurep 'xemacs) '(t)
2207                    '(:help "Save article using default method"))]
2208               ["Save in file..." gnus-summary-save-article-file
2209                ,@(if (featurep 'xemacs) '(t)
2210                    '(:help "Save article in file"))]
2211               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2212               ["Save in MH folder..." gnus-summary-save-article-folder t]
2213               ["Save in VM folder..." gnus-summary-save-article-vm t]
2214               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2215               ["Save body in file..." gnus-summary-save-article-body-file t]
2216               ["Pipe through a filter..." gnus-summary-pipe-output t]
2217               ["Add to SOUP packet" gnus-soup-add-article t]
2218               ["Print with Muttprint..." gnus-summary-muttprint t]
2219               ["Print" gnus-summary-print-article t])
2220              ("Backend"
2221               ["Respool article..." gnus-summary-respool-article t]
2222               ["Move article..." gnus-summary-move-article
2223                (gnus-check-backend-function
2224                 'request-move-article gnus-newsgroup-name)]
2225               ["Copy article..." gnus-summary-copy-article t]
2226               ["Crosspost article..." gnus-summary-crosspost-article
2227                (gnus-check-backend-function
2228                 'request-replace-article gnus-newsgroup-name)]
2229               ["Import file..." gnus-summary-import-article
2230                (gnus-check-backend-function
2231                 'request-accept-article gnus-newsgroup-name)]
2232               ["Create article..." gnus-summary-create-article
2233                (gnus-check-backend-function
2234                 'request-accept-article gnus-newsgroup-name)]
2235               ["Check if posted" gnus-summary-article-posted-p t]
2236               ["Edit article" gnus-summary-edit-article
2237                (not (gnus-group-read-only-p))]
2238               ["Delete article" gnus-summary-delete-article
2239                (gnus-check-backend-function
2240                 'request-expire-articles gnus-newsgroup-name)]
2241               ["Query respool" gnus-summary-respool-query t]
2242               ["Trace respool" gnus-summary-respool-trace t]
2243               ["Delete expirable articles" gnus-summary-expire-articles-now
2244                (gnus-check-backend-function
2245                 'request-expire-articles gnus-newsgroup-name)])
2246              ("Extract"
2247               ["Uudecode" gnus-uu-decode-uu
2248                ,@(if (featurep 'xemacs) '(t)
2249                    '(:help "Decode uuencoded article(s)"))]
2250               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2251               ["Unshar" gnus-uu-decode-unshar t]
2252               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2253               ["Save" gnus-uu-decode-save t]
2254               ["Binhex" gnus-uu-decode-binhex t]
2255               ["Postscript" gnus-uu-decode-postscript t])
2256              ("Cache"
2257               ["Enter article" gnus-cache-enter-article t]
2258               ["Remove article" gnus-cache-remove-article t])
2259              ["Translate" gnus-article-babel t]
2260              ["Select article buffer" gnus-summary-select-article-buffer t]
2261              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2262              ["Isearch article..." gnus-summary-isearch-article t]
2263              ["Beginning of the article" gnus-summary-beginning-of-article t]
2264              ["End of the article" gnus-summary-end-of-article t]
2265              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2266              ["Fetch referenced articles" gnus-summary-refer-references t]
2267              ["Fetch current thread" gnus-summary-refer-thread t]
2268              ["Fetch article with id..." gnus-summary-refer-article t]
2269              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2270              ["Redisplay" gnus-summary-show-article t]
2271              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2272       (easy-menu-define
2273        gnus-summary-article-menu gnus-summary-mode-map ""
2274        (cons "Article" innards))
2275
2276       (if (not (keymapp gnus-summary-article-menu))
2277           (easy-menu-define
2278            gnus-article-commands-menu gnus-article-mode-map ""
2279            (cons "Commands" innards))
2280         ;; in Emacs, don't share menu.
2281         (setq gnus-article-commands-menu
2282               (copy-keymap gnus-summary-article-menu))
2283         (define-key gnus-article-mode-map [menu-bar commands]
2284           (cons "Commands" gnus-article-commands-menu))))
2285
2286     (easy-menu-define
2287      gnus-summary-thread-menu gnus-summary-mode-map ""
2288      '("Threads"
2289        ["Find all messages in thread" gnus-summary-refer-thread t]
2290        ["Toggle threading" gnus-summary-toggle-threads t]
2291        ["Hide threads" gnus-summary-hide-all-threads t]
2292        ["Show threads" gnus-summary-show-all-threads t]
2293        ["Hide thread" gnus-summary-hide-thread t]
2294        ["Show thread" gnus-summary-show-thread t]
2295        ["Go to next thread" gnus-summary-next-thread t]
2296        ["Go to previous thread" gnus-summary-prev-thread t]
2297        ["Go down thread" gnus-summary-down-thread t]
2298        ["Go up thread" gnus-summary-up-thread t]
2299        ["Top of thread" gnus-summary-top-thread t]
2300        ["Mark thread as read" gnus-summary-kill-thread t]
2301        ["Lower thread score" gnus-summary-lower-thread t]
2302        ["Raise thread score" gnus-summary-raise-thread t]
2303        ["Rethread current" gnus-summary-rethread-current t]))
2304
2305     (easy-menu-define
2306      gnus-summary-post-menu gnus-summary-mode-map ""
2307      `("Post"
2308        ["Send a message (mail or news)" gnus-summary-post-news
2309         ,@(if (featurep 'xemacs) '(t)
2310             '(:help "Post an article"))]
2311        ["Followup" gnus-summary-followup
2312         ,@(if (featurep 'xemacs) '(t)
2313             '(:help "Post followup to this article"))]
2314        ["Followup and yank" gnus-summary-followup-with-original
2315         ,@(if (featurep 'xemacs) '(t)
2316             '(:help "Post followup to this article, quoting its contents"))]
2317        ["Supersede article" gnus-summary-supersede-article t]
2318        ["Cancel article" gnus-summary-cancel-article
2319         ,@(if (featurep 'xemacs) '(t)
2320             '(:help "Cancel an article you posted"))]
2321        ["Reply" gnus-summary-reply t]
2322        ["Reply and yank" gnus-summary-reply-with-original t]
2323        ["Wide reply" gnus-summary-wide-reply t]
2324        ["Wide reply and yank" gnus-summary-wide-reply-with-original
2325         ,@(if (featurep 'xemacs) '(t)
2326             '(:help "Mail a reply, quoting this article"))]
2327        ["Very wide reply" gnus-summary-very-wide-reply t]
2328        ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2329         ,@(if (featurep 'xemacs) '(t)
2330             '(:help "Mail a very wide reply, quoting this article"))]
2331        ["Mail forward" gnus-summary-mail-forward t]
2332        ["Post forward" gnus-summary-post-forward t]
2333        ["Digest and mail" gnus-summary-digest-mail-forward t]
2334        ["Digest and post" gnus-summary-digest-post-forward t]
2335        ["Resend message" gnus-summary-resend-message t]
2336        ["Resend message edit" gnus-summary-resend-message-edit t]
2337        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2338        ["Send a mail" gnus-summary-mail-other-window t]
2339        ["Create a local message" gnus-summary-news-other-window t]
2340        ["Uuencode and post" gnus-uu-post-news
2341         ,@(if (featurep 'xemacs) '(t)
2342             '(:help "Post a uuencoded article"))]
2343        ["Followup via news" gnus-summary-followup-to-mail t]
2344        ["Followup via news and yank"
2345         gnus-summary-followup-to-mail-with-original t]
2346        ;;("Draft"
2347        ;;["Send" gnus-summary-send-draft t]
2348        ;;["Send bounced" gnus-resend-bounced-mail t])
2349        ))
2350
2351     (cond
2352      ((not (keymapp gnus-summary-post-menu))
2353       (setq gnus-article-post-menu gnus-summary-post-menu))
2354      ((not gnus-article-post-menu)
2355       ;; Don't share post menu.
2356       (setq gnus-article-post-menu
2357             (copy-keymap gnus-summary-post-menu))))
2358     (define-key gnus-article-mode-map [menu-bar post]
2359       (cons "Post" gnus-article-post-menu))
2360
2361     (easy-menu-define
2362      gnus-summary-misc-menu gnus-summary-mode-map ""
2363      `("Gnus"
2364        ("Mark Read"
2365         ["Mark as read" gnus-summary-mark-as-read-forward t]
2366         ["Mark same subject and select"
2367          gnus-summary-kill-same-subject-and-select t]
2368         ["Mark same subject" gnus-summary-kill-same-subject t]
2369         ["Catchup" gnus-summary-catchup
2370          ,@(if (featurep 'xemacs) '(t)
2371              '(:help "Mark unread articles in this group as read"))]
2372         ["Catchup all" gnus-summary-catchup-all t]
2373         ["Catchup to here" gnus-summary-catchup-to-here t]
2374         ["Catchup from here" gnus-summary-catchup-from-here t]
2375         ["Catchup region" gnus-summary-mark-region-as-read
2376          (gnus-mark-active-p)]
2377         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2378        ("Mark Various"
2379         ["Tick" gnus-summary-tick-article-forward t]
2380         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2381         ["Remove marks" gnus-summary-clear-mark-forward t]
2382         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2383         ["Set bookmark" gnus-summary-set-bookmark t]
2384         ["Remove bookmark" gnus-summary-remove-bookmark t])
2385        ("Limit to"
2386         ["Marks..." gnus-summary-limit-to-marks t]
2387         ["Subject..." gnus-summary-limit-to-subject t]
2388         ["Author..." gnus-summary-limit-to-author t]
2389         ["Age..." gnus-summary-limit-to-age t]
2390         ["Extra..." gnus-summary-limit-to-extra t]
2391         ["Score..." gnus-summary-limit-to-score t]
2392         ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2393         ["Unread" gnus-summary-limit-to-unread t]
2394         ["Unseen" gnus-summary-limit-to-unseen t]
2395         ["Replied" gnus-summary-limit-to-replied t]
2396         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2397         ["Next articles" gnus-summary-limit-to-articles t]
2398         ["Pop limit" gnus-summary-pop-limit t]
2399         ["Show dormant" gnus-summary-limit-include-dormant t]
2400         ["Hide childless dormant"
2401          gnus-summary-limit-exclude-childless-dormant t]
2402         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2403         ["Hide marked" gnus-summary-limit-exclude-marks t]
2404         ["Show expunged" gnus-summary-limit-include-expunged t])
2405        ("Process Mark"
2406         ["Set mark" gnus-summary-mark-as-processable t]
2407         ["Remove mark" gnus-summary-unmark-as-processable t]
2408         ["Remove all marks" gnus-summary-unmark-all-processable t]
2409         ["Mark above" gnus-uu-mark-over t]
2410         ["Mark series" gnus-uu-mark-series t]
2411         ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2412         ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2413         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2414         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2415         ["Mark all" gnus-uu-mark-all t]
2416         ["Mark buffer" gnus-uu-mark-buffer t]
2417         ["Mark sparse" gnus-uu-mark-sparse t]
2418         ["Mark thread" gnus-uu-mark-thread t]
2419         ["Unmark thread" gnus-uu-unmark-thread t]
2420         ("Process Mark Sets"
2421          ["Kill" gnus-summary-kill-process-mark t]
2422          ["Yank" gnus-summary-yank-process-mark
2423           gnus-newsgroup-process-stack]
2424          ["Save" gnus-summary-save-process-mark t]
2425          ["Run command on marked..." gnus-summary-universal-argument t]))
2426        ("Scroll article"
2427         ["Page forward" gnus-summary-next-page
2428          ,@(if (featurep 'xemacs) '(t)
2429              '(:help "Show next page of article"))]
2430         ["Page backward" gnus-summary-prev-page
2431          ,@(if (featurep 'xemacs) '(t)
2432              '(:help "Show previous page of article"))]
2433         ["Line forward" gnus-summary-scroll-up t])
2434        ("Move"
2435         ["Next unread article" gnus-summary-next-unread-article t]
2436         ["Previous unread article" gnus-summary-prev-unread-article t]
2437         ["Next article" gnus-summary-next-article t]
2438         ["Previous article" gnus-summary-prev-article t]
2439         ["Next unread subject" gnus-summary-next-unread-subject t]
2440         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2441         ["Next article same subject" gnus-summary-next-same-subject t]
2442         ["Previous article same subject" gnus-summary-prev-same-subject t]
2443         ["First unread article" gnus-summary-first-unread-article t]
2444         ["Best unread article" gnus-summary-best-unread-article t]
2445         ["Go to subject number..." gnus-summary-goto-subject t]
2446         ["Go to article number..." gnus-summary-goto-article t]
2447         ["Go to the last article" gnus-summary-goto-last-article t]
2448         ["Pop article off history" gnus-summary-pop-article t])
2449        ("Sort"
2450         ["Sort by number" gnus-summary-sort-by-number t]
2451         ["Sort by author" gnus-summary-sort-by-author t]
2452         ["Sort by subject" gnus-summary-sort-by-subject t]
2453         ["Sort by date" gnus-summary-sort-by-date t]
2454         ["Sort by score" gnus-summary-sort-by-score t]
2455         ["Sort by lines" gnus-summary-sort-by-lines t]
2456         ["Sort by characters" gnus-summary-sort-by-chars t]
2457         ["Randomize" gnus-summary-sort-by-random t]
2458         ["Original sort" gnus-summary-sort-by-original t])
2459        ("Help"
2460         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2461         ["Describe group" gnus-summary-describe-group t]
2462         ["Fetch charter" gnus-group-fetch-charter
2463          ,@(if (featurep 'xemacs) nil
2464              '(:help "Display the charter of the current group"))]
2465         ["Fetch control message" gnus-group-fetch-control
2466          ,@(if (featurep 'xemacs) nil
2467              '(:help "Display the archived control message for the current group"))]
2468         ["Read manual" gnus-info-find-node t])
2469        ("Modes"
2470         ["Pick and read" gnus-pick-mode t]
2471         ["Binary" gnus-binary-mode t])
2472        ("Regeneration"
2473         ["Regenerate" gnus-summary-prepare t]
2474         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2475         ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2476         ["Toggle threading" gnus-summary-toggle-threads t])
2477        ["See old articles" gnus-summary-insert-old-articles t]
2478        ["See new articles" gnus-summary-insert-new-articles t]
2479        ["Filter articles..." gnus-summary-execute-command t]
2480        ["Run command on articles..." gnus-summary-universal-argument t]
2481        ["Search articles forward..." gnus-summary-search-article-forward t]
2482        ["Search articles backward..." gnus-summary-search-article-backward t]
2483        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2484        ["Expand window" gnus-summary-expand-window t]
2485        ["Expire expirable articles" gnus-summary-expire-articles
2486         (gnus-check-backend-function
2487          'request-expire-articles gnus-newsgroup-name)]
2488        ["Edit local kill file" gnus-summary-edit-local-kill t]
2489        ["Edit main kill file" gnus-summary-edit-global-kill t]
2490        ["Edit group parameters" gnus-summary-edit-parameters t]
2491        ["Customize group parameters" gnus-summary-customize-parameters t]
2492        ["Send a bug report" gnus-bug t]
2493        ("Exit"
2494         ["Catchup and exit" gnus-summary-catchup-and-exit
2495          ,@(if (featurep 'xemacs) '(t)
2496              '(:help "Mark unread articles in this group as read, then exit"))]
2497         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2498         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2499         ["Exit group" gnus-summary-exit
2500          ,@(if (featurep 'xemacs) '(t)
2501              '(:help "Exit current group, return to group selection mode"))]
2502         ["Exit group without updating" gnus-summary-exit-no-update t]
2503         ["Exit and goto next group" gnus-summary-next-group t]
2504         ["Exit and goto prev group" gnus-summary-prev-group t]
2505         ["Reselect group" gnus-summary-reselect-current-group t]
2506         ["Rescan group" gnus-summary-rescan-group t]
2507         ["Update dribble" gnus-summary-save-newsrc t])))
2508
2509     (gnus-run-hooks 'gnus-summary-menu-hook)))
2510
2511 (defvar gnus-summary-tool-bar-map nil)
2512
2513 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2514 (defun gnus-summary-make-tool-bar ()
2515   (if (and (fboundp 'tool-bar-add-item-from-menu)
2516            (default-value 'tool-bar-mode)
2517            (not gnus-summary-tool-bar-map))
2518       (setq gnus-summary-tool-bar-map
2519             (let ((tool-bar-map (make-sparse-keymap))
2520                   (load-path (mm-image-load-path)))
2521               (tool-bar-add-item-from-menu
2522                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2523               (tool-bar-add-item-from-menu
2524                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2525               (tool-bar-add-item-from-menu
2526                'gnus-summary-post-news "post" gnus-summary-mode-map)
2527               (tool-bar-add-item-from-menu
2528                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2529               (tool-bar-add-item-from-menu
2530                'gnus-summary-followup "followup" gnus-summary-mode-map)
2531               (tool-bar-add-item-from-menu
2532                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2533               (tool-bar-add-item-from-menu
2534                'gnus-summary-reply "reply" gnus-summary-mode-map)
2535               (tool-bar-add-item-from-menu
2536                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2537               (tool-bar-add-item-from-menu
2538                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2539               (tool-bar-add-item-from-menu
2540                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2541               (tool-bar-add-item-from-menu
2542                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2543               (tool-bar-add-item-from-menu
2544                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2545               (tool-bar-add-item-from-menu
2546                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2547               (tool-bar-add-item-from-menu
2548                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2549               (tool-bar-add-item-from-menu
2550                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2551               tool-bar-map)))
2552   (if gnus-summary-tool-bar-map
2553       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2554
2555 (defun gnus-score-set-default (var value)
2556   "A version of set that updates the GNU Emacs menu-bar."
2557   (set var value)
2558   ;; It is the message that forces the active status to be updated.
2559   (message ""))
2560
2561 (defun gnus-make-score-map (type)
2562   "Make a summary score map of type TYPE."
2563   (if t
2564       nil
2565     (let ((headers '(("author" "from" string)
2566                      ("subject" "subject" string)
2567                      ("article body" "body" string)
2568                      ("article head" "head" string)
2569                      ("xref" "xref" string)
2570                      ("extra header" "extra" string)
2571                      ("lines" "lines" number)
2572                      ("followups to author" "followup" string)))
2573           (types '((number ("less than" <)
2574                            ("greater than" >)
2575                            ("equal" =))
2576                    (string ("substring" s)
2577                            ("exact string" e)
2578                            ("fuzzy string" f)
2579                            ("regexp" r))))
2580           (perms '(("temporary" (current-time-string))
2581                    ("permanent" nil)
2582                    ("immediate" now)))
2583           header)
2584       (list
2585        (apply
2586         'nconc
2587         (list
2588          (if (eq type 'lower)
2589              "Lower score"
2590            "Increase score"))
2591         (let (outh)
2592           (while headers
2593             (setq header (car headers))
2594             (setq outh
2595                   (cons
2596                    (apply
2597                     'nconc
2598                     (list (car header))
2599                     (let ((ts (cdr (assoc (nth 2 header) types)))
2600                           outt)
2601                       (while ts
2602                         (setq outt
2603                               (cons
2604                                (apply
2605                                 'nconc
2606                                 (list (caar ts))
2607                                 (let ((ps perms)
2608                                       outp)
2609                                   (while ps
2610                                     (setq outp
2611                                           (cons
2612                                            (vector
2613                                             (caar ps)
2614                                             (list
2615                                              'gnus-summary-score-entry
2616                                              (nth 1 header)
2617                                              (if (or (string= (nth 1 header)
2618                                                               "head")
2619                                                      (string= (nth 1 header)
2620                                                               "body"))
2621                                                  ""
2622                                                (list 'gnus-summary-header
2623                                                      (nth 1 header)))
2624                                              (list 'quote (nth 1 (car ts)))
2625                                              (list 'gnus-score-delta-default
2626                                                    nil)
2627                                              (nth 1 (car ps))
2628                                              t)
2629                                             t)
2630                                            outp))
2631                                     (setq ps (cdr ps)))
2632                                   (list (nreverse outp))))
2633                                outt))
2634                         (setq ts (cdr ts)))
2635                       (list (nreverse outt))))
2636                    outh))
2637             (setq headers (cdr headers)))
2638           (list (nreverse outh))))))))
2639
2640 \f
2641
2642 (defun gnus-summary-mode (&optional group)
2643   "Major mode for reading articles.
2644
2645 All normal editing commands are switched off.
2646 \\<gnus-summary-mode-map>
2647 Each line in this buffer represents one article.  To read an
2648 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2649 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2650 respectively.
2651
2652 You can also post articles and send mail from this buffer.  To
2653 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2654 of an article, type `\\[gnus-summary-reply]'.
2655
2656 There are approx. one gazillion commands you can execute in this
2657 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2658
2659 The following commands are available:
2660
2661 \\{gnus-summary-mode-map}"
2662   (interactive)
2663   (kill-all-local-variables)
2664   (when (gnus-visual-p 'summary-menu 'menu)
2665     (gnus-summary-make-menu-bar)
2666     (gnus-summary-make-tool-bar))
2667   (gnus-summary-make-local-variables)
2668   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2669     (gnus-summary-make-local-variables))
2670   (gnus-make-thread-indent-array)
2671   (gnus-simplify-mode-line)
2672   (setq major-mode 'gnus-summary-mode)
2673   (setq mode-name "Summary")
2674   (make-local-variable 'minor-mode-alist)
2675   (use-local-map gnus-summary-mode-map)
2676   (buffer-disable-undo)
2677   (setq buffer-read-only t)             ;Disable modification
2678   (setq truncate-lines t)
2679   (setq selective-display t)
2680   (setq selective-display-ellipses t)   ;Display `...'
2681   (gnus-summary-set-display-table)
2682   (gnus-set-default-directory)
2683   (setq gnus-newsgroup-name group)
2684   (unless (gnus-news-group-p group)
2685     (setq gnus-newsgroup-incorporated
2686           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2687   (make-local-variable 'gnus-summary-line-format)
2688   (make-local-variable 'gnus-summary-line-format-spec)
2689   (make-local-variable 'gnus-summary-dummy-line-format)
2690   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2691   (make-local-variable 'gnus-summary-mark-positions)
2692   (gnus-make-local-hook 'pre-command-hook)
2693   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2694   (gnus-run-hooks 'gnus-summary-mode-hook)
2695   (turn-on-gnus-mailing-list-mode)
2696   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2697   (gnus-update-summary-mark-positions))
2698
2699 (defun gnus-summary-make-local-variables ()
2700   "Make all the local summary buffer variables."
2701   (let (global)
2702     (dolist (local gnus-summary-local-variables)
2703       (if (consp local)
2704           (progn
2705             (if (eq (cdr local) 'global)
2706                 ;; Copy the global value of the variable.
2707                 (setq global (symbol-value (car local)))
2708               ;; Use the value from the list.
2709               (setq global (eval (cdr local))))
2710             (set (make-local-variable (car local)) global))
2711         ;; Simple nil-valued local variable.
2712         (set (make-local-variable local) nil)))))
2713
2714 (defun gnus-summary-clear-local-variables ()
2715   (let ((locals gnus-summary-local-variables))
2716     (while locals
2717       (if (consp (car locals))
2718           (and (vectorp (caar locals))
2719                (set (caar locals) nil))
2720         (and (vectorp (car locals))
2721              (set (car locals) nil)))
2722       (setq locals (cdr locals)))))
2723
2724 ;; Summary data functions.
2725
2726 (defmacro gnus-data-number (data)
2727   `(car ,data))
2728
2729 (defmacro gnus-data-set-number (data number)
2730   `(setcar ,data ,number))
2731
2732 (defmacro gnus-data-mark (data)
2733   `(nth 1 ,data))
2734
2735 (defmacro gnus-data-set-mark (data mark)
2736   `(setcar (nthcdr 1 ,data) ,mark))
2737
2738 (defmacro gnus-data-pos (data)
2739   `(nth 2 ,data))
2740
2741 (defmacro gnus-data-set-pos (data pos)
2742   `(setcar (nthcdr 2 ,data) ,pos))
2743
2744 (defmacro gnus-data-header (data)
2745   `(nth 3 ,data))
2746
2747 (defmacro gnus-data-set-header (data header)
2748   `(setcar (nthcdr 3 ,data) ,header))
2749
2750 (defmacro gnus-data-level (data)
2751   `(nth 4 ,data))
2752
2753 (defmacro gnus-data-unread-p (data)
2754   `(= (nth 1 ,data) gnus-unread-mark))
2755
2756 (defmacro gnus-data-read-p (data)
2757   `(/= (nth 1 ,data) gnus-unread-mark))
2758
2759 (defmacro gnus-data-pseudo-p (data)
2760   `(consp (nth 3 ,data)))
2761
2762 (defmacro gnus-data-find (number)
2763   `(assq ,number gnus-newsgroup-data))
2764
2765 (defmacro gnus-data-find-list (number &optional data)
2766   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2767      (memq (assq ,number bdata)
2768            bdata)))
2769
2770 (defmacro gnus-data-make (number mark pos header level)
2771   `(list ,number ,mark ,pos ,header ,level))
2772
2773 (defun gnus-data-enter (after-article number mark pos header level offset)
2774   (let ((data (gnus-data-find-list after-article)))
2775     (unless data
2776       (error "No such article: %d" after-article))
2777     (setcdr data (cons (gnus-data-make number mark pos header level)
2778                        (cdr data)))
2779     (setq gnus-newsgroup-data-reverse nil)
2780     (gnus-data-update-list (cddr data) offset)))
2781
2782 (defun gnus-data-enter-list (after-article list &optional offset)
2783   (when list
2784     (let ((data (and after-article (gnus-data-find-list after-article)))
2785           (ilist list))
2786       (if (not (or data
2787                    after-article))
2788           (let ((odata gnus-newsgroup-data))
2789             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2790             (when offset
2791               (gnus-data-update-list odata offset)))
2792         ;; Find the last element in the list to be spliced into the main
2793         ;; list.
2794         (while (cdr list)
2795           (setq list (cdr list)))
2796         (if (not data)
2797             (progn
2798               (setcdr list gnus-newsgroup-data)
2799               (setq gnus-newsgroup-data ilist)
2800               (when offset
2801                 (gnus-data-update-list (cdr list) offset)))
2802           (setcdr list (cdr data))
2803           (setcdr data ilist)
2804           (when offset
2805             (gnus-data-update-list (cdr list) offset))))
2806       (setq gnus-newsgroup-data-reverse nil))))
2807
2808 (defun gnus-data-remove (article &optional offset)
2809   (let ((data gnus-newsgroup-data))
2810     (if (= (gnus-data-number (car data)) article)
2811         (progn
2812           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2813                 gnus-newsgroup-data-reverse nil)
2814           (when offset
2815             (gnus-data-update-list gnus-newsgroup-data offset)))
2816       (while (cdr data)
2817         (when (= (gnus-data-number (cadr data)) article)
2818           (setcdr data (cddr data))
2819           (when offset
2820             (gnus-data-update-list (cdr data) offset))
2821           (setq data nil
2822                 gnus-newsgroup-data-reverse nil))
2823         (setq data (cdr data))))))
2824
2825 (defmacro gnus-data-list (backward)
2826   `(if ,backward
2827        (or gnus-newsgroup-data-reverse
2828            (setq gnus-newsgroup-data-reverse
2829                  (reverse gnus-newsgroup-data)))
2830      gnus-newsgroup-data))
2831
2832 (defun gnus-data-update-list (data offset)
2833   "Add OFFSET to the POS of all data entries in DATA."
2834   (setq gnus-newsgroup-data-reverse nil)
2835   (while data
2836     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2837     (setq data (cdr data))))
2838
2839 (defun gnus-summary-article-pseudo-p (article)
2840   "Say whether this article is a pseudo article or not."
2841   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2842
2843 (defmacro gnus-summary-article-sparse-p (article)
2844   "Say whether this article is a sparse article or not."
2845   `(memq ,article gnus-newsgroup-sparse))
2846
2847 (defmacro gnus-summary-article-ancient-p (article)
2848   "Say whether this article is a sparse article or not."
2849   `(memq ,article gnus-newsgroup-ancient))
2850
2851 (defun gnus-article-parent-p (number)
2852   "Say whether this article is a parent or not."
2853   (let ((data (gnus-data-find-list number)))
2854     (and (cdr data)                     ; There has to be an article after...
2855          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2856             (gnus-data-level (nth 1 data))))))
2857
2858 (defun gnus-article-children (number)
2859   "Return a list of all children to NUMBER."
2860   (let* ((data (gnus-data-find-list number))
2861          (level (gnus-data-level (car data)))
2862          children)
2863     (setq data (cdr data))
2864     (while (and data
2865                 (= (gnus-data-level (car data)) (1+ level)))
2866       (push (gnus-data-number (car data)) children)
2867       (setq data (cdr data)))
2868     children))
2869
2870 (defmacro gnus-summary-skip-intangible ()
2871   "If the current article is intangible, then jump to a different article."
2872   '(let ((to (get-text-property (point) 'gnus-intangible)))
2873      (and to (gnus-summary-goto-subject to))))
2874
2875 (defmacro gnus-summary-article-intangible-p ()
2876   "Say whether this article is intangible or not."
2877   '(get-text-property (point) 'gnus-intangible))
2878
2879 (defun gnus-article-read-p (article)
2880   "Say whether ARTICLE is read or not."
2881   (not (or (memq article gnus-newsgroup-marked)
2882            (memq article gnus-newsgroup-spam-marked)
2883            (memq article gnus-newsgroup-unreads)
2884            (memq article gnus-newsgroup-unselected)
2885            (memq article gnus-newsgroup-dormant))))
2886
2887 ;; Some summary mode macros.
2888
2889 (defmacro gnus-summary-article-number ()
2890   "The article number of the article on the current line.
2891 If there isn't an article number here, then we return the current
2892 article number."
2893   '(progn
2894      (gnus-summary-skip-intangible)
2895      (or (get-text-property (point) 'gnus-number)
2896          (gnus-summary-last-subject))))
2897
2898 (defmacro gnus-summary-article-header (&optional number)
2899   "Return the header of article NUMBER."
2900   `(gnus-data-header (gnus-data-find
2901                       ,(or number '(gnus-summary-article-number)))))
2902
2903 (defmacro gnus-summary-thread-level (&optional number)
2904   "Return the level of thread that starts with article NUMBER."
2905   `(if (and (eq gnus-summary-make-false-root 'dummy)
2906             (get-text-property (point) 'gnus-intangible))
2907        0
2908      (gnus-data-level (gnus-data-find
2909                        ,(or number '(gnus-summary-article-number))))))
2910
2911 (defmacro gnus-summary-article-mark (&optional number)
2912   "Return the mark of article NUMBER."
2913   `(gnus-data-mark (gnus-data-find
2914                     ,(or number '(gnus-summary-article-number)))))
2915
2916 (defmacro gnus-summary-article-pos (&optional number)
2917   "Return the position of the line of article NUMBER."
2918   `(gnus-data-pos (gnus-data-find
2919                    ,(or number '(gnus-summary-article-number)))))
2920
2921 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2922 (defmacro gnus-summary-article-subject (&optional number)
2923   "Return current subject string or nil if nothing."
2924   `(let ((headers
2925           ,(if number
2926                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2927              '(gnus-data-header (assq (gnus-summary-article-number)
2928                                       gnus-newsgroup-data)))))
2929      (and headers
2930           (vectorp headers)
2931           (mail-header-subject headers))))
2932
2933 (defmacro gnus-summary-article-score (&optional number)
2934   "Return current article score."
2935   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2936                   gnus-newsgroup-scored))
2937        gnus-summary-default-score 0))
2938
2939 (defun gnus-summary-article-children (&optional number)
2940   "Return a list of article numbers that are children of article NUMBER."
2941   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2942          (level (gnus-data-level (car data)))
2943          l children)
2944     (while (and (setq data (cdr data))
2945                 (> (setq l (gnus-data-level (car data))) level))
2946       (and (= (1+ level) l)
2947            (push (gnus-data-number (car data))
2948                  children)))
2949     (nreverse children)))
2950
2951 (defun gnus-summary-article-parent (&optional number)
2952   "Return the article number of the parent of article NUMBER."
2953   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2954                                     (gnus-data-list t)))
2955          (level (gnus-data-level (car data))))
2956     (if (zerop level)
2957         ()                              ; This is a root.
2958       ;; We search until we find an article with a level less than
2959       ;; this one.  That function has to be the parent.
2960       (while (and (setq data (cdr data))
2961                   (not (< (gnus-data-level (car data)) level))))
2962       (and data (gnus-data-number (car data))))))
2963
2964 (defun gnus-unread-mark-p (mark)
2965   "Say whether MARK is the unread mark."
2966   (= mark gnus-unread-mark))
2967
2968 (defun gnus-read-mark-p (mark)
2969   "Say whether MARK is one of the marks that mark as read.
2970 This is all marks except unread, ticked, dormant, and expirable."
2971   (not (or (= mark gnus-unread-mark)
2972            (= mark gnus-ticked-mark)
2973            (= mark gnus-spam-mark)
2974            (= mark gnus-dormant-mark)
2975            (= mark gnus-expirable-mark))))
2976
2977 (defmacro gnus-article-mark (number)
2978   "Return the MARK of article NUMBER.
2979 This macro should only be used when computing the mark the \"first\"
2980 time; i.e., when generating the summary lines.  After that,
2981 `gnus-summary-article-mark' should be used to examine the
2982 marks of articles."
2983   `(cond
2984     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2985     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2986     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2987     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2988     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2989     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2990     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2991     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2992            gnus-ancient-mark))))
2993
2994 ;; Saving hidden threads.
2995
2996 (defmacro gnus-save-hidden-threads (&rest forms)
2997   "Save hidden threads, eval FORMS, and restore the hidden threads."
2998   (let ((config (make-symbol "config")))
2999     `(let ((,config (gnus-hidden-threads-configuration)))
3000        (unwind-protect
3001            (save-excursion
3002              ,@forms)
3003          (gnus-restore-hidden-threads-configuration ,config)))))
3004 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3005 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3006
3007 (defun gnus-data-compute-positions ()
3008   "Compute the positions of all articles."
3009   (setq gnus-newsgroup-data-reverse nil)
3010   (let ((data gnus-newsgroup-data))
3011     (save-excursion
3012       (gnus-save-hidden-threads
3013         (gnus-summary-show-all-threads)
3014         (goto-char (point-min))
3015         (while data
3016           (while (get-text-property (point) 'gnus-intangible)
3017             (forward-line 1))
3018           (gnus-data-set-pos (car data) (+ (point) 3))
3019           (setq data (cdr data))
3020           (forward-line 1))))))
3021
3022 (defun gnus-hidden-threads-configuration ()
3023   "Return the current hidden threads configuration."
3024   (save-excursion
3025     (let (config)
3026       (goto-char (point-min))
3027       (while (search-forward "\r" nil t)
3028         (push (1- (point)) config))
3029       config)))
3030
3031 (defun gnus-restore-hidden-threads-configuration (config)
3032   "Restore hidden threads configuration from CONFIG."
3033   (save-excursion
3034     (let (point buffer-read-only)
3035       (while (setq point (pop config))
3036         (when (and (< point (point-max))
3037                    (goto-char point)
3038                    (eq (char-after) ?\n))
3039           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3040
3041 ;; Various summary mode internalish functions.
3042
3043 (defun gnus-mouse-pick-article (e)
3044   (interactive "e")
3045   (mouse-set-point e)
3046   (gnus-summary-next-page nil t))
3047
3048 (defun gnus-summary-set-display-table ()
3049   "Change the display table.
3050 Odd characters have a tendency to mess
3051 up nicely formatted displays - we make all possible glyphs
3052 display only a single character."
3053
3054   ;; We start from the standard display table, if any.
3055   (let ((table (or (copy-sequence standard-display-table)
3056                    (make-display-table)))
3057         (i 32))
3058     ;; Nix out all the control chars...
3059     (while (>= (setq i (1- i)) 0)
3060       (aset table i [??]))
3061     ;; ... but not newline and cr, of course.  (cr is necessary for the
3062     ;; selective display).
3063     (aset table ?\n nil)
3064     (aset table ?\r nil)
3065     ;; We keep TAB as well.
3066     (aset table ?\t nil)
3067     ;; We nix out any glyphs over 126 that are not set already.
3068     (let ((i 256))
3069       (while (>= (setq i (1- i)) 127)
3070         ;; Only modify if the entry is nil.
3071         (unless (aref table i)
3072           (aset table i [??]))))
3073     (setq buffer-display-table table)))
3074
3075 (defun gnus-summary-set-article-display-arrow (pos)
3076   "Update the overlay arrow to point to line at position POS."
3077   (when (and gnus-summary-display-arrow
3078              (boundp 'overlay-arrow-position)
3079              (boundp 'overlay-arrow-string))
3080     (save-excursion
3081       (goto-char pos)
3082       (beginning-of-line)
3083       (unless overlay-arrow-position
3084         (setq overlay-arrow-position (make-marker)))
3085       (setq overlay-arrow-string "=>"
3086             overlay-arrow-position (set-marker overlay-arrow-position
3087                                                (point)
3088                                                (current-buffer))))))
3089
3090 (defun gnus-summary-setup-buffer (group)
3091   "Initialize summary buffer."
3092   (let ((buffer (gnus-summary-buffer-name group))
3093         (dead-name (concat "*Dead Summary "
3094                            (gnus-group-decoded-name group) "*")))
3095     ;; If a dead summary buffer exists, we kill it.
3096     (when (gnus-buffer-live-p dead-name)
3097       (gnus-kill-buffer dead-name))
3098     (if (get-buffer buffer)
3099         (progn
3100           (set-buffer buffer)
3101           (setq gnus-summary-buffer (current-buffer))
3102           (not gnus-newsgroup-prepared))
3103       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3104       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3105       (gnus-summary-mode group)
3106       (when gnus-carpal
3107         (gnus-carpal-setup-buffer 'summary))
3108       (unless gnus-single-article-buffer
3109         (make-local-variable 'gnus-article-buffer)
3110         (make-local-variable 'gnus-article-current)
3111         (make-local-variable 'gnus-original-article-buffer))
3112       (setq gnus-newsgroup-name group)
3113       ;; Set any local variables in the group parameters.
3114       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3115       t)))
3116
3117 (defun gnus-set-global-variables ()
3118   "Set the global equivalents of the buffer-local variables.
3119 They are set to the latest values they had.  These reflect the summary
3120 buffer that was in action when the last article was fetched."
3121   (when (eq major-mode 'gnus-summary-mode)
3122     (setq gnus-summary-buffer (current-buffer))
3123     (let ((name gnus-newsgroup-name)
3124           (marked gnus-newsgroup-marked)
3125           (spam gnus-newsgroup-spam-marked)
3126           (unread gnus-newsgroup-unreads)
3127           (headers gnus-current-headers)
3128           (data gnus-newsgroup-data)
3129           (summary gnus-summary-buffer)
3130           (article-buffer gnus-article-buffer)
3131           (original gnus-original-article-buffer)
3132           (gac gnus-article-current)
3133           (reffed gnus-reffed-article-number)
3134           (score-file gnus-current-score-file)
3135           (default-charset gnus-newsgroup-charset)
3136           vlist)
3137       (let ((locals gnus-newsgroup-variables))
3138         (while locals
3139           (if (consp (car locals))
3140               (push (eval (caar locals)) vlist)
3141             (push (eval (car locals)) vlist))
3142           (setq locals (cdr locals)))
3143         (setq vlist (nreverse vlist)))
3144       (save-excursion
3145         (set-buffer gnus-group-buffer)
3146         (setq gnus-newsgroup-name name
3147               gnus-newsgroup-marked marked
3148               gnus-newsgroup-spam-marked spam
3149               gnus-newsgroup-unreads unread
3150               gnus-current-headers headers
3151               gnus-newsgroup-data data
3152               gnus-article-current gac
3153               gnus-summary-buffer summary
3154               gnus-article-buffer article-buffer
3155               gnus-original-article-buffer original
3156               gnus-reffed-article-number reffed
3157               gnus-current-score-file score-file
3158               gnus-newsgroup-charset default-charset)
3159         (let ((locals gnus-newsgroup-variables))
3160           (while locals
3161             (if (consp (car locals))
3162                 (set (caar locals) (pop vlist))
3163               (set (car locals) (pop vlist)))
3164             (setq locals (cdr locals))))
3165         ;; The article buffer also has local variables.
3166         (when (gnus-buffer-live-p gnus-article-buffer)
3167           (set-buffer gnus-article-buffer)
3168           (setq gnus-summary-buffer summary))))))
3169
3170 (defun gnus-summary-article-unread-p (article)
3171   "Say whether ARTICLE is unread or not."
3172   (memq article gnus-newsgroup-unreads))
3173
3174 (defun gnus-summary-first-article-p (&optional article)
3175   "Return whether ARTICLE is the first article in the buffer."
3176   (if (not (setq article (or article (gnus-summary-article-number))))
3177       nil
3178     (eq article (caar gnus-newsgroup-data))))
3179
3180 (defun gnus-summary-last-article-p (&optional article)
3181   "Return whether ARTICLE is the last article in the buffer."
3182   (if (not (setq article (or article (gnus-summary-article-number))))
3183       ;; All non-existent numbers are the last article.  :-)
3184       t
3185     (not (cdr (gnus-data-find-list article)))))
3186
3187 (defun gnus-make-thread-indent-array ()
3188   (let ((n 200))
3189     (unless (and gnus-thread-indent-array
3190                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3191       (setq gnus-thread-indent-array (make-vector 201 "")
3192             gnus-thread-indent-array-level gnus-thread-indent-level)
3193       (while (>= n 0)
3194         (aset gnus-thread-indent-array n
3195               (make-string (* n gnus-thread-indent-level) ? ))
3196         (setq n (1- n))))))
3197
3198 (defun gnus-update-summary-mark-positions ()
3199   "Compute where the summary marks are to go."
3200   (save-excursion
3201     (when (gnus-buffer-exists-p gnus-summary-buffer)
3202       (set-buffer gnus-summary-buffer))
3203     (let ((gnus-replied-mark 129)
3204           (gnus-score-below-mark 130)
3205           (gnus-score-over-mark 130)
3206           (gnus-undownloaded-mark 131)
3207           (spec gnus-summary-line-format-spec)
3208           gnus-visual pos)
3209       (save-excursion
3210         (gnus-set-work-buffer)
3211         (let ((gnus-summary-line-format-spec spec)
3212               (gnus-newsgroup-downloadable '(0)))
3213           (gnus-summary-insert-line
3214            (make-full-mail-header 0 "" "nobody"
3215                                   "05 Apr 2001 23:33:09 +0400"
3216                                   "" "" 0 0 "" nil)
3217            0 nil t 128 t nil "" nil 1)
3218           (goto-char (point-min))
3219           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3220                                              (- (point) (point-min) 1)))))
3221           (goto-char (point-min))
3222           (push (cons 'replied (and (search-forward "\201" nil t)
3223                                     (- (point) (point-min) 1)))
3224                 pos)
3225           (goto-char (point-min))
3226           (push (cons 'score (and (search-forward "\202" nil t)
3227                                   (- (point) (point-min) 1)))
3228                 pos)
3229           (goto-char (point-min))
3230           (push (cons 'download
3231                       (and (search-forward "\203" nil t)
3232                            (- (point) (point-min) 1)))
3233                 pos)))
3234       (setq gnus-summary-mark-positions pos))))
3235
3236 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3237   "Insert a dummy root in the summary buffer."
3238   (beginning-of-line)
3239   (gnus-add-text-properties
3240    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3241    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3242
3243 (defun gnus-summary-extract-address-component (from)
3244   (or (car (funcall gnus-extract-address-components from))
3245       from))
3246
3247 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3248   (let ((default-mime-charset (with-current-buffer gnus-summary-buffer
3249                                 default-mime-charset)))
3250     ;; Is it really necessary to do this next part for each summary line?
3251     ;; Luckily, doesn't seem to slow things down much.
3252     (or
3253      (and gnus-ignored-from-addresses
3254           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3255           (let ((extra-headers (mail-header-extra header))
3256                 to
3257                 newsgroups)
3258             (cond
3259              ((setq to (cdr (assq 'To extra-headers)))
3260               (concat "-> "
3261                       (inline
3262                         (gnus-summary-extract-address-component
3263                          (funcall gnus-decode-encoded-word-function to)))))
3264              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3265               (concat "=> " newsgroups)))))
3266      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3267
3268 (defun gnus-summary-insert-line (gnus-tmp-header
3269                                  gnus-tmp-level gnus-tmp-current
3270                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3271                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3272                                  &optional gnus-tmp-dummy gnus-tmp-score
3273                                  gnus-tmp-process)
3274   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3275          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3276          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3277          (gnus-tmp-score-char
3278           (if (or (null gnus-summary-default-score)
3279                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3280                       gnus-summary-zcore-fuzz))
3281               ?\ ;;;Whitespace
3282             (if (< gnus-tmp-score gnus-summary-default-score)
3283                 gnus-score-below-mark gnus-score-over-mark)))
3284          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3285          (gnus-tmp-replied
3286           (cond (gnus-tmp-process gnus-process-mark)
3287                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3288                  gnus-cached-mark)
3289                 (gnus-tmp-replied gnus-replied-mark)
3290                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3291                  gnus-forwarded-mark)
3292                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3293                  gnus-saved-mark)
3294                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3295                  gnus-recent-mark)
3296                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3297                  gnus-unseen-mark)
3298                 (t gnus-no-mark)))
3299          (gnus-tmp-downloaded
3300           (cond (undownloaded
3301                  gnus-undownloaded-mark)
3302                 (gnus-newsgroup-agentized
3303                  gnus-downloaded-mark)
3304                 (t
3305                  gnus-no-mark)))
3306          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3307          (gnus-tmp-name
3308           (cond
3309            ((string-match "<[^>]+> *$" gnus-tmp-from)
3310             (let ((beg (match-beginning 0)))
3311               (or (and (string-match "^\".+\"" gnus-tmp-from)
3312                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3313                   (substring gnus-tmp-from 0 beg))))
3314            ((string-match "(.+)" gnus-tmp-from)
3315             (substring gnus-tmp-from
3316                        (1+ (match-beginning 0)) (1- (match-end 0))))
3317            (t gnus-tmp-from)))
3318          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3319          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3320          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3321          (buffer-read-only nil))
3322     (when (string= gnus-tmp-name "")
3323       (setq gnus-tmp-name gnus-tmp-from))
3324     (unless (numberp gnus-tmp-lines)
3325       (setq gnus-tmp-lines -1))
3326     (if (= gnus-tmp-lines -1)
3327         (setq gnus-tmp-lines "?")
3328       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3329     (gnus-put-text-property-excluding-characters-with-faces
3330      (point)
3331      (progn (eval gnus-summary-line-format-spec) (point))
3332      'gnus-number gnus-tmp-number)
3333     (when (gnus-visual-p 'summary-highlight 'highlight)
3334       (forward-line -1)
3335       (gnus-run-hooks 'gnus-summary-update-hook)
3336       (forward-line 1))))
3337
3338 (defun gnus-summary-update-line (&optional dont-update)
3339   "Update summary line after change."
3340   (when (and gnus-summary-default-score
3341              (not gnus-summary-inhibit-highlight))
3342     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3343            (article (gnus-summary-article-number))
3344            (score (gnus-summary-article-score article)))
3345       (unless dont-update
3346         (if (and gnus-summary-mark-below
3347                  (< (gnus-summary-article-score)
3348                     gnus-summary-mark-below))
3349             ;; This article has a low score, so we mark it as read.
3350             (when (memq article gnus-newsgroup-unreads)
3351               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3352           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3353             ;; This article was previously marked as read on account
3354             ;; of a low score, but now it has risen, so we mark it as
3355             ;; unread.
3356             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3357         (gnus-summary-update-mark
3358          (if (or (null gnus-summary-default-score)
3359                  (<= (abs (- score gnus-summary-default-score))
3360                      gnus-summary-zcore-fuzz))
3361              ?\ ;;;Whitespace
3362            (if (< score gnus-summary-default-score)
3363                gnus-score-below-mark gnus-score-over-mark))
3364          'score))
3365       ;; Do visual highlighting.
3366       (when (gnus-visual-p 'summary-highlight 'highlight)
3367         (gnus-run-hooks 'gnus-summary-update-hook)))))
3368
3369 (defvar gnus-tmp-new-adopts nil)
3370
3371 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3372   "Return the number of articles in THREAD.
3373 This may be 0 in some cases -- if none of the articles in
3374 the thread are to be displayed."
3375   (let* ((number
3376           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3377           (cond
3378            ((not (listp thread))
3379             1)
3380            ((and (consp thread) (cdr thread))
3381             (apply
3382              '+ 1 (mapcar
3383                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3384            ((null thread)
3385             1)
3386            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3387             1)
3388            (t 0))))
3389     (when (and level (zerop level) gnus-tmp-new-adopts)
3390       (incf number
3391             (apply '+ (mapcar
3392                        'gnus-summary-number-of-articles-in-thread
3393                        gnus-tmp-new-adopts))))
3394     (if char
3395         (if (> number 1) gnus-not-empty-thread-mark
3396           gnus-empty-thread-mark)
3397       number)))
3398
3399 (defsubst gnus-summary-line-message-size (head)
3400   "Return pretty-printed version of message size.
3401 This function is intended to be used in
3402 `gnus-summary-line-format-alist'."
3403   (let ((c (or (mail-header-chars head) -1)))
3404     (cond ((< c 0) "n/a")               ; chars not available
3405           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3406           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3407           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3408           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3409
3410
3411 (defun gnus-summary-set-local-parameters (group)
3412   "Go through the local params of GROUP and set all variable specs in that list."
3413   (let ((params (gnus-group-find-parameter group))
3414         (vars '(quit-config))           ; Ignore quit-config.
3415         elem)
3416     (while params
3417       (setq elem (car params)
3418             params (cdr params))
3419       (and (consp elem)                 ; Has to be a cons.
3420            (consp (cdr elem))           ; The cdr has to be a list.
3421            (symbolp (car elem))         ; Has to be a symbol in there.
3422            (not (memq (car elem) vars))
3423            (ignore-errors               ; So we set it.
3424              (push (car elem) vars)
3425              (make-local-variable (car elem))
3426              (set (car elem) (eval (nth 1 elem))))))))
3427
3428 (defun gnus-summary-read-group (group &optional show-all no-article
3429                                       kill-buffer no-display backward
3430                                       select-articles)
3431   "Start reading news in newsgroup GROUP.
3432 If SHOW-ALL is non-nil, already read articles are also listed.
3433 If NO-ARTICLE is non-nil, no article is selected initially.
3434 If NO-DISPLAY, don't generate a summary buffer."
3435   (let (result)
3436     (while (and group
3437                 (null (setq result
3438                             (let ((gnus-auto-select-next nil))
3439                               (or (gnus-summary-read-group-1
3440                                    group show-all no-article
3441                                    kill-buffer no-display
3442                                    select-articles)
3443                                   (setq show-all nil
3444                                         select-articles nil)))))
3445                 (eq gnus-auto-select-next 'quietly))
3446       (set-buffer gnus-group-buffer)
3447       ;; The entry function called above goes to the next
3448       ;; group automatically, so we go two groups back
3449       ;; if we are searching for the previous group.
3450       (when backward
3451         (gnus-group-prev-unread-group 2))
3452       (if (not (equal group (gnus-group-group-name)))
3453           (setq group (gnus-group-group-name))
3454         (setq group nil)))
3455     result))
3456
3457 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3458   "Directly jump to the other GROUP from summary buffer.
3459 If SHOW-ALL is non-nil, already read articles are also listed."
3460   (interactive
3461    (if (eq gnus-summary-buffer (current-buffer))
3462        (list (completing-read
3463               "Group: " gnus-active-hashtb nil t
3464               (when (and gnus-newsgroup-name
3465                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3466                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3467               'gnus-group-history)
3468              current-prefix-arg)
3469      (error "%s must be invoked from a gnus summary buffer." this-command)))
3470   (unless (or (zerop (length group))
3471               (and gnus-newsgroup-name
3472                    (string-equal gnus-newsgroup-name group)))
3473     (gnus-summary-exit)
3474     (gnus-summary-read-group group show-all
3475                              gnus-dont-select-after-jump-to-other-group)))
3476
3477 (defun gnus-summary-read-group-1 (group show-all no-article
3478                                         kill-buffer no-display
3479                                         &optional select-articles)
3480   ;; Killed foreign groups can't be entered.
3481   ;;  (when (and (not (gnus-group-native-p group))
3482   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3483   ;;    (error "Dead non-native groups can't be entered"))
3484   (gnus-message 5 "Retrieving newsgroup: %s..."
3485                 (gnus-group-decoded-name group))
3486   (let* ((new-group (gnus-summary-setup-buffer group))
3487          (quit-config (gnus-group-quit-config group))
3488          (did-select (and new-group (gnus-select-newsgroup
3489                                      group show-all select-articles))))
3490     (cond
3491      ;; This summary buffer exists already, so we just select it.
3492      ((not new-group)
3493       (gnus-set-global-variables)
3494       (when kill-buffer
3495         (gnus-kill-or-deaden-summary kill-buffer))
3496       (gnus-configure-windows 'summary 'force)
3497       (gnus-set-mode-line 'summary)
3498       (gnus-summary-position-point)
3499       (message "")
3500       t)
3501      ;; We couldn't select this group.
3502      ((null did-select)
3503       (when (and (eq major-mode 'gnus-summary-mode)
3504                  (not (equal (current-buffer) kill-buffer)))
3505         (kill-buffer (current-buffer))
3506         (if (not quit-config)
3507             (progn
3508               ;; Update the info -- marks might need to be removed,
3509               ;; for instance.
3510               (gnus-summary-update-info)
3511               (set-buffer gnus-group-buffer)
3512               (gnus-group-jump-to-group group)
3513               (gnus-group-next-unread-group 1))
3514           (gnus-handle-ephemeral-exit quit-config)))
3515       (let ((grpinfo (gnus-get-info group)))
3516         (if (null (gnus-info-read grpinfo))
3517             (gnus-message 3 "Group %s contains no messages"
3518                           (gnus-group-decoded-name group))
3519           (gnus-message 3 "Can't select group")))
3520       nil)
3521      ;; The user did a `C-g' while prompting for number of articles,
3522      ;; so we exit this group.
3523      ((eq did-select 'quit)
3524       (and (eq major-mode 'gnus-summary-mode)
3525            (not (equal (current-buffer) kill-buffer))
3526            (kill-buffer (current-buffer)))
3527       (when kill-buffer
3528         (gnus-kill-or-deaden-summary kill-buffer))
3529       (if (not quit-config)
3530           (progn
3531             (set-buffer gnus-group-buffer)
3532             (gnus-group-jump-to-group group)
3533             (gnus-group-next-unread-group 1)
3534             (gnus-configure-windows 'group 'force))
3535         (gnus-handle-ephemeral-exit quit-config))
3536       ;; Finally signal the quit.
3537       (signal 'quit nil))
3538      ;; The group was successfully selected.
3539      (t
3540       (gnus-set-global-variables)
3541       ;; Save the active value in effect when the group was entered.
3542       (setq gnus-newsgroup-active
3543             (gnus-copy-sequence
3544              (gnus-active gnus-newsgroup-name)))
3545       ;; You can change the summary buffer in some way with this hook.
3546       (gnus-run-hooks 'gnus-select-group-hook)
3547       (gnus-update-format-specifications
3548        nil 'summary 'summary-mode 'summary-dummy)
3549       (gnus-update-summary-mark-positions)
3550       ;; Do score processing.
3551       (when gnus-use-scoring
3552         (gnus-possibly-score-headers))
3553       ;; Check whether to fill in the gaps in the threads.
3554       (when gnus-build-sparse-threads
3555         (gnus-build-sparse-threads))
3556       ;; Find the initial limit.
3557       (if gnus-show-threads
3558           (if show-all
3559               (let ((gnus-newsgroup-dormant nil))
3560                 (gnus-summary-initial-limit show-all))
3561             (gnus-summary-initial-limit show-all))
3562         ;; When unthreaded, all articles are always shown.
3563         (setq gnus-newsgroup-limit
3564               (mapcar
3565                (lambda (header) (mail-header-number header))
3566                gnus-newsgroup-headers)))
3567       ;; Generate the summary buffer.
3568       (unless no-display
3569         (gnus-summary-prepare))
3570       (when gnus-use-trees
3571         (gnus-tree-open group)
3572         (setq gnus-summary-highlight-line-function
3573               'gnus-tree-highlight-article))
3574       ;; If the summary buffer is empty, but there are some low-scored
3575       ;; articles or some excluded dormants, we include these in the
3576       ;; buffer.
3577       (when (and (zerop (buffer-size))
3578                  (not no-display))
3579         (cond (gnus-newsgroup-dormant
3580                (gnus-summary-limit-include-dormant))
3581               ((and gnus-newsgroup-scored show-all)
3582                (gnus-summary-limit-include-expunged t))))
3583       ;; Function `gnus-apply-kill-file' must be called in this hook.
3584       (gnus-run-hooks 'gnus-apply-kill-hook)
3585       (if (and (zerop (buffer-size))
3586                (not no-display))
3587           (progn
3588             ;; This newsgroup is empty.
3589             (gnus-summary-catchup-and-exit nil t)
3590             (gnus-message 6 "No unread news")
3591             (when kill-buffer
3592               (gnus-kill-or-deaden-summary kill-buffer))
3593             ;; Return nil from this function.
3594             nil)
3595         ;; Hide conversation thread subtrees.  We cannot do this in
3596         ;; gnus-summary-prepare-hook since kill processing may not
3597         ;; work with hidden articles.
3598         (gnus-summary-maybe-hide-threads)
3599         (when kill-buffer
3600           (gnus-kill-or-deaden-summary kill-buffer))
3601         (gnus-summary-auto-select-subject)
3602         ;; Show first unread article if requested.
3603         (if (and (not no-article)
3604                  (not no-display)
3605                  gnus-newsgroup-unreads
3606                  gnus-auto-select-first)
3607             (progn
3608               (gnus-configure-windows 'summary)
3609               (let ((art (gnus-summary-article-number)))
3610                 (unless (and (not gnus-plugged)
3611                              (or (memq art gnus-newsgroup-undownloaded)
3612                                  (memq art gnus-newsgroup-downloadable)))
3613                   (gnus-summary-goto-article art))))
3614           ;; Don't select any articles.
3615           (gnus-summary-position-point)
3616           (gnus-configure-windows 'summary 'force)
3617           (gnus-set-mode-line 'summary))
3618         (when (and gnus-auto-center-group
3619                    (get-buffer-window gnus-group-buffer t))
3620           ;; Gotta use windows, because recenter does weird stuff if
3621           ;; the current buffer ain't the displayed window.
3622           (let ((owin (selected-window)))
3623             (select-window (get-buffer-window gnus-group-buffer t))
3624             (when (gnus-group-goto-group group)
3625               (recenter))
3626             (select-window owin)))
3627         ;; Mark this buffer as "prepared".
3628         (setq gnus-newsgroup-prepared t)
3629         (gnus-run-hooks 'gnus-summary-prepared-hook)
3630         (unless (gnus-ephemeral-group-p group)
3631           (gnus-group-update-group group))
3632         t)))))
3633
3634 (defun gnus-summary-auto-select-subject ()
3635   "Select the subject line on initial group entry."
3636   (goto-char (point-min))
3637   (cond
3638    ((eq gnus-auto-select-subject 'best)
3639     (gnus-summary-best-unread-subject))
3640    ((eq gnus-auto-select-subject 'unread)
3641     (gnus-summary-first-unread-subject))
3642    ((eq gnus-auto-select-subject 'unseen)
3643     (gnus-summary-first-unseen-subject))
3644    ((eq gnus-auto-select-subject 'unseen-or-unread)
3645     (gnus-summary-first-unseen-or-unread-subject))
3646    ((eq gnus-auto-select-subject 'first)
3647     ;; Do nothing.
3648     )
3649    ((functionp gnus-auto-select-subject)
3650     (funcall gnus-auto-select-subject))))
3651
3652 (defun gnus-summary-prepare ()
3653   "Generate the summary buffer."
3654   (interactive)
3655   (let ((buffer-read-only nil))
3656     (erase-buffer)
3657     (setq gnus-newsgroup-data nil
3658           gnus-newsgroup-data-reverse nil)
3659     (gnus-run-hooks 'gnus-summary-generate-hook)
3660     ;; Generate the buffer, either with threads or without.
3661     (when gnus-newsgroup-headers
3662       (gnus-summary-prepare-threads
3663        (if gnus-show-threads
3664            (gnus-sort-gathered-threads
3665             (funcall gnus-summary-thread-gathering-function
3666                      (gnus-sort-threads
3667                       (gnus-cut-threads (gnus-make-threads)))))
3668          ;; Unthreaded display.
3669          (gnus-sort-articles gnus-newsgroup-headers))))
3670     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3671     ;; Call hooks for modifying summary buffer.
3672     (goto-char (point-min))
3673     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3674
3675 (defsubst gnus-general-simplify-subject (subject)
3676   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3677   (setq subject
3678         (cond
3679          ;; Truncate the subject.
3680          (gnus-simplify-subject-functions
3681           (gnus-map-function gnus-simplify-subject-functions subject))
3682          ((numberp gnus-summary-gather-subject-limit)
3683           (setq subject (gnus-simplify-subject-re subject))
3684           (if (> (length subject) gnus-summary-gather-subject-limit)
3685               (substring subject 0 gnus-summary-gather-subject-limit)
3686             subject))
3687          ;; Fuzzily simplify it.
3688          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3689           (gnus-simplify-subject-fuzzy subject))
3690          ;; Just remove the leading "Re:".
3691          (t
3692           (gnus-simplify-subject-re subject))))
3693
3694   (if (and gnus-summary-gather-exclude-subject
3695            (string-match gnus-summary-gather-exclude-subject subject))
3696       nil                               ; This article shouldn't be gathered
3697     subject))
3698
3699 (defun gnus-summary-simplify-subject-query ()
3700   "Query where the respool algorithm would put this article."
3701   (interactive)
3702   (gnus-summary-select-article)
3703   (message "%s"
3704            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3705
3706 (defun gnus-gather-threads-by-subject (threads)
3707   "Gather threads by looking at Subject headers."
3708   (if (not gnus-summary-make-false-root)
3709       threads
3710     (let ((hashtb (gnus-make-hashtable 1024))
3711           (prev threads)
3712           (result threads)
3713           subject hthread whole-subject)
3714       (while threads
3715         (setq subject (gnus-general-simplify-subject
3716                        (setq whole-subject (mail-header-subject
3717                                             (caar threads)))))
3718         (when subject
3719           (if (setq hthread (gnus-gethash subject hashtb))
3720               (progn
3721                 ;; We enter a dummy root into the thread, if we
3722                 ;; haven't done that already.
3723                 (unless (stringp (caar hthread))
3724                   (setcar hthread (list whole-subject (car hthread))))
3725                 ;; We add this new gathered thread to this gathered
3726                 ;; thread.
3727                 (setcdr (car hthread)
3728                         (nconc (cdar hthread) (list (car threads))))
3729                 ;; Remove it from the list of threads.
3730                 (setcdr prev (cdr threads))
3731                 (setq threads prev))
3732             ;; Enter this thread into the hash table.
3733             (gnus-sethash subject
3734                           (if gnus-summary-make-false-root-always
3735                               (progn
3736                                 ;; If you want a dummy root above all
3737                                 ;; threads...
3738                                 (setcar threads (list whole-subject
3739                                                       (car threads)))
3740                                 threads)
3741                             threads)
3742                           hashtb)))
3743         (setq prev threads)
3744         (setq threads (cdr threads)))
3745       result)))
3746
3747 (defun gnus-gather-threads-by-references (threads)
3748   "Gather threads by looking at References headers."
3749   (let ((idhashtb (gnus-make-hashtable 1024))
3750         (thhashtb (gnus-make-hashtable 1024))
3751         (prev threads)
3752         (result threads)
3753         ids references id gthread gid entered ref)
3754     (while threads
3755       (when (setq references (mail-header-references (caar threads)))
3756         (setq id (mail-header-id (caar threads))
3757               ids (inline (gnus-split-references references))
3758               entered nil)
3759         (while (setq ref (pop ids))
3760           (setq ids (delete ref ids))
3761           (if (not (setq gid (gnus-gethash ref idhashtb)))
3762               (progn
3763                 (gnus-sethash ref id idhashtb)
3764                 (gnus-sethash id threads thhashtb))
3765             (setq gthread (gnus-gethash gid thhashtb))
3766             (unless entered
3767               ;; We enter a dummy root into the thread, if we
3768               ;; haven't done that already.
3769               (unless (stringp (caar gthread))
3770                 (setcar gthread (list (mail-header-subject (caar gthread))
3771                                       (car gthread))))
3772               ;; We add this new gathered thread to this gathered
3773               ;; thread.
3774               (setcdr (car gthread)
3775                       (nconc (cdar gthread) (list (car threads)))))
3776             ;; Add it into the thread hash table.
3777             (gnus-sethash id gthread thhashtb)
3778             (setq entered t)
3779             ;; Remove it from the list of threads.
3780             (setcdr prev (cdr threads))
3781             (setq threads prev))))
3782       (setq prev threads)
3783       (setq threads (cdr threads)))
3784     result))
3785
3786 (defun gnus-sort-gathered-threads (threads)
3787   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3788   (let ((result threads))
3789     (while threads
3790       (when (stringp (caar threads))
3791         (setcdr (car threads)
3792                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3793       (setq threads (cdr threads)))
3794     result))
3795
3796 (defun gnus-thread-loop-p (root thread)
3797   "Say whether ROOT is in THREAD."
3798   (let ((stack (list thread))
3799         (infloop 0)
3800         th)
3801     (while (setq thread (pop stack))
3802       (setq th (cdr thread))
3803       (while (and th
3804                   (not (eq (caar th) root)))
3805         (pop th))
3806       (if th
3807           ;; We have found a loop.
3808           (let (ref-dep)
3809             (setcdr thread (delq (car th) (cdr thread)))
3810             (if (boundp (setq ref-dep (intern "none"
3811                                               gnus-newsgroup-dependencies)))
3812                 (setcdr (symbol-value ref-dep)
3813                         (nconc (cdr (symbol-value ref-dep))
3814                                (list (car th))))
3815               (set ref-dep (list nil (car th))))
3816             (setq infloop 1
3817                   stack nil))
3818         ;; Push all the subthreads onto the stack.
3819         (push (cdr thread) stack)))
3820     infloop))
3821
3822 (defun gnus-make-threads ()
3823   "Go through the dependency hashtb and find the roots.  Return all threads."
3824   (let (threads)
3825     (while (catch 'infloop
3826              (mapatoms
3827               (lambda (refs)
3828                 ;; Deal with self-referencing References loops.
3829                 (when (and (car (symbol-value refs))
3830                            (not (zerop
3831                                  (apply
3832                                   '+
3833                                   (mapcar
3834                                    (lambda (thread)
3835                                      (gnus-thread-loop-p
3836                                       (car (symbol-value refs)) thread))
3837                                    (cdr (symbol-value refs)))))))
3838                   (setq threads nil)
3839                   (throw 'infloop t))
3840                 (unless (car (symbol-value refs))
3841                   ;; These threads do not refer back to any other articles,
3842                   ;; so they're roots.
3843                   (setq threads (append (cdr (symbol-value refs)) threads))))
3844               gnus-newsgroup-dependencies)))
3845     threads))
3846
3847 ;; Build the thread tree.
3848 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3849   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3850
3851 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3852 if it was already present.
3853
3854 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3855 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3856 Message-IDs will be renamed to a unique Message-ID before being
3857 entered.
3858
3859 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3860   (let* ((id (mail-header-id header))
3861          (id-dep (and id (intern id dependencies)))
3862          parent-id ref ref-dep ref-header replaced)
3863     ;; Enter this `header' in the `dependencies' table.
3864     (cond
3865      ((not id-dep)
3866       (setq header nil))
3867      ;; The first two cases do the normal part: enter a new `header'
3868      ;; in the `dependencies' table.
3869      ((not (boundp id-dep))
3870       (set id-dep (list header)))
3871      ((null (car (symbol-value id-dep)))
3872       (setcar (symbol-value id-dep) header))
3873
3874      ;; From here the `header' was already present in the
3875      ;; `dependencies' table.
3876      (force-new
3877       ;; Overrides an existing entry;
3878       ;; just set the header part of the entry.
3879       (setcar (symbol-value id-dep) header)
3880       (setq replaced t))
3881
3882      ;; Renames the existing `header' to a unique Message-ID.
3883      ((not gnus-summary-ignore-duplicates)
3884       ;; An article with this Message-ID has already been seen.
3885       ;; We rename the Message-ID.
3886       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3887            (list header))
3888       (mail-header-set-id header id))
3889
3890      ;; The last case ignores an existing entry, except it adds any
3891      ;; additional Xrefs (in case the two articles came from different
3892      ;; servers.
3893      ;; Also sets `header' to `nil' meaning that the `dependencies'
3894      ;; table was *not* modified.
3895      (t
3896       (mail-header-set-xref
3897        (car (symbol-value id-dep))
3898        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3899                    "")
3900                (or (mail-header-xref header) "")))
3901       (setq header nil)))
3902
3903     (when (and header (not replaced))
3904       ;; First check that we are not creating a References loop.
3905       (setq parent-id (gnus-parent-id (mail-header-references header)))
3906       (setq ref parent-id)
3907       (while (and ref
3908                   (setq ref-dep (intern-soft ref dependencies))
3909                   (boundp ref-dep)
3910                   (setq ref-header (car (symbol-value ref-dep))))
3911         (if (string= id ref)
3912             ;; Yuk!  This is a reference loop.  Make the article be a
3913             ;; root article.
3914             (progn
3915               (mail-header-set-references (car (symbol-value id-dep)) "none")
3916               (setq ref nil)
3917               (setq parent-id nil))
3918           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3919       (setq ref-dep (intern (or parent-id "none") dependencies))
3920       (if (boundp ref-dep)
3921           (setcdr (symbol-value ref-dep)
3922                   (nconc (cdr (symbol-value ref-dep))
3923                          (list (symbol-value id-dep))))
3924         (set ref-dep (list nil (symbol-value id-dep)))))
3925     header))
3926
3927 (defun gnus-extract-message-id-from-in-reply-to (string)
3928   (if (string-match "<[^>]+>" string)
3929       (substring string (match-beginning 0) (match-end 0))
3930     nil))
3931
3932 (defun gnus-build-sparse-threads ()
3933   (let ((headers gnus-newsgroup-headers)
3934         (mail-parse-charset gnus-newsgroup-charset)
3935         (gnus-summary-ignore-duplicates t)
3936         header references generation relations
3937         subject child end new-child date)
3938     ;; First we create an alist of generations/relations, where
3939     ;; generations is how much we trust the relation, and the relation
3940     ;; is parent/child.
3941     (gnus-message 7 "Making sparse threads...")
3942     (save-excursion
3943       (nnheader-set-temp-buffer " *gnus sparse threads*")
3944       (while (setq header (pop headers))
3945         (when (and (setq references (mail-header-references header))
3946                    (not (string= references "")))
3947           (insert references)
3948           (setq child (mail-header-id header)
3949                 subject (mail-header-subject header)
3950                 date (mail-header-date header)
3951                 generation 0)
3952           (while (search-backward ">" nil t)
3953             (setq end (1+ (point)))
3954             (when (search-backward "<" nil t)
3955               (setq new-child (buffer-substring (point) end))
3956               (push (list (incf generation)
3957                           child (setq child new-child)
3958                           subject date)
3959                     relations)))
3960           (when child
3961             (push (list (1+ generation) child nil subject) relations))
3962           (erase-buffer)))
3963       (kill-buffer (current-buffer)))
3964     ;; Sort over trustworthiness.
3965     (mapcar
3966      (lambda (relation)
3967        (when (gnus-dependencies-add-header
3968               (make-full-mail-header-from-decoded-header
3969                gnus-reffed-article-number
3970                (nth 3 relation) "" (or (nth 4 relation) "")
3971                (nth 1 relation)
3972                (or (nth 2 relation) "") 0 0 "")
3973               gnus-newsgroup-dependencies nil)
3974          (push gnus-reffed-article-number gnus-newsgroup-limit)
3975          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3976          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3977                gnus-newsgroup-reads)
3978          (decf gnus-reffed-article-number)))
3979      (sort relations 'car-less-than-car))
3980     (gnus-message 7 "Making sparse threads...done")))
3981
3982 (defun gnus-build-old-threads ()
3983   ;; Look at all the articles that refer back to old articles, and
3984   ;; fetch the headers for the articles that aren't there.  This will
3985   ;; build complete threads - if the roots haven't been expired by the
3986   ;; server, that is.
3987   (let ((mail-parse-charset gnus-newsgroup-charset)
3988         id heads)
3989     (mapatoms
3990      (lambda (refs)
3991        (when (not (car (symbol-value refs)))
3992          (setq heads (cdr (symbol-value refs)))
3993          (while heads
3994            (if (memq (mail-header-number (caar heads))
3995                      gnus-newsgroup-dormant)
3996                (setq heads (cdr heads))
3997              (setq id (symbol-name refs))
3998              (while (and (setq id (gnus-build-get-header id))
3999                          (not (car (gnus-id-to-thread id)))))
4000              (setq heads nil)))))
4001      gnus-newsgroup-dependencies)))
4002
4003 (defsubst gnus-remove-odd-characters (string)
4004   "Translate STRING into something that doesn't contain weird characters."
4005   (mm-subst-char-in-string
4006    ?\r ?\-
4007    (mm-subst-char-in-string ?\n ?\- string t) t))
4008
4009 ;; This function has to be called with point after the article number
4010 ;; on the beginning of the line.
4011 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4012   (let ((eol (point-at-eol))
4013         (buffer (current-buffer))
4014         header references in-reply-to)
4015
4016     ;; overview: [num subject from date id refs chars lines misc]
4017     (unwind-protect
4018         (progn
4019           (narrow-to-region (point) eol)
4020           (unless (eobp)
4021             (forward-char))
4022
4023           (setq header
4024                 (make-full-mail-header
4025                  number                         ; number
4026                  (nnheader-nov-field)           ; subject
4027                  (nnheader-nov-field)           ; from
4028                  (nnheader-nov-field)           ; date
4029                  (nnheader-nov-read-message-id) ; id
4030                  (nnheader-nov-field)           ; refs
4031                  (nnheader-nov-read-integer)    ; chars
4032                  (nnheader-nov-read-integer)    ; lines
4033                  (unless (eobp)
4034                    (if (looking-at "Xref: ")
4035                        (goto-char (match-end 0)))
4036                    (nnheader-nov-field))        ; Xref
4037                  (nnheader-nov-parse-extra))))  ; extra
4038
4039       (widen))
4040
4041     (when (and (string= references "")
4042                (setq in-reply-to (mail-header-extra header))
4043                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4044       (mail-header-set-references
4045        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4046
4047     (when gnus-alter-header-function
4048       (funcall gnus-alter-header-function header))
4049     (gnus-dependencies-add-header header dependencies force-new)))
4050
4051 (defun gnus-build-get-header (id)
4052   "Look through the buffer of NOV lines and find the header to ID.
4053 Enter this line into the dependencies hash table, and return
4054 the id of the parent article (if any)."
4055   (let ((deps gnus-newsgroup-dependencies)
4056         found header)
4057     (prog1
4058         (save-excursion
4059           (set-buffer nntp-server-buffer)
4060           (let ((case-fold-search nil))
4061             (goto-char (point-min))
4062             (while (and (not found)
4063                         (search-forward id nil t))
4064               (beginning-of-line)
4065               (setq found (looking-at
4066                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4067                                    (regexp-quote id))))
4068               (or found (beginning-of-line 2)))
4069             (when found
4070               (beginning-of-line)
4071               (and
4072                (setq header (gnus-nov-parse-line
4073                              (read (current-buffer)) deps))
4074                (gnus-parent-id (mail-header-references header))))))
4075       (when header
4076         (let ((number (mail-header-number header)))
4077           (push number gnus-newsgroup-limit)
4078           (push header gnus-newsgroup-headers)
4079           (if (memq number gnus-newsgroup-unselected)
4080               (progn
4081                 (setq gnus-newsgroup-unreads
4082                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4083                                                number))
4084                 (setq gnus-newsgroup-unselected
4085                       (delq number gnus-newsgroup-unselected)))
4086             (push number gnus-newsgroup-ancient)))))))
4087
4088 (defun gnus-build-all-threads ()
4089   "Read all the headers."
4090   (let ((gnus-summary-ignore-duplicates t)
4091         (mail-parse-charset gnus-newsgroup-charset)
4092         (dependencies gnus-newsgroup-dependencies)
4093         header article)
4094     (save-excursion
4095       (set-buffer nntp-server-buffer)
4096       (let ((case-fold-search nil))
4097         (goto-char (point-min))
4098         (while (not (eobp))
4099           (ignore-errors
4100             (setq article (read (current-buffer))
4101                   header (gnus-nov-parse-line article dependencies)))
4102           (when header
4103             (save-excursion
4104               (set-buffer gnus-summary-buffer)
4105               (push header gnus-newsgroup-headers)
4106               (if (memq (setq article (mail-header-number header))
4107                         gnus-newsgroup-unselected)
4108                   (progn
4109                     (setq gnus-newsgroup-unreads
4110                           (gnus-add-to-sorted-list
4111                            gnus-newsgroup-unreads article))
4112                     (setq gnus-newsgroup-unselected
4113                           (delq article gnus-newsgroup-unselected)))
4114                 (push article gnus-newsgroup-ancient)))
4115             (forward-line 1)))))))
4116
4117 (defun gnus-summary-update-article-line (article header)
4118   "Update the line for ARTICLE using HEADER."
4119   (let* ((id (mail-header-id header))
4120          (thread (gnus-id-to-thread id)))
4121     (unless thread
4122       (error "Article in no thread"))
4123     ;; Update the thread.
4124     (setcar thread header)
4125     (gnus-summary-goto-subject article)
4126     (let* ((datal (gnus-data-find-list article))
4127            (data (car datal))
4128            (buffer-read-only nil)
4129            (level (gnus-summary-thread-level)))
4130       (gnus-delete-line)
4131       (let ((inserted (- (point)
4132                          (progn
4133                            (gnus-summary-insert-line
4134                             header level nil
4135                             (memq article gnus-newsgroup-undownloaded)
4136                             (gnus-article-mark article)
4137                             (memq article gnus-newsgroup-replied)
4138                             (memq article gnus-newsgroup-expirable)
4139                             ;; Only insert the Subject string when it's different
4140                             ;; from the previous Subject string.
4141                             (if (and
4142                                  gnus-show-threads
4143                                  (gnus-subject-equal
4144                                   (condition-case ()
4145                                       (mail-header-subject
4146                                        (gnus-data-header
4147                                         (cadr
4148                                          (gnus-data-find-list
4149                                           article
4150                                           (gnus-data-list t)))))
4151                                     ;; Error on the side of excessive subjects.
4152                                     (error ""))
4153                                   (mail-header-subject header)))
4154                                 ""
4155                               (mail-header-subject header))
4156                             nil (cdr (assq article gnus-newsgroup-scored))
4157                             (memq article gnus-newsgroup-processable))
4158                            (point)))))
4159         (when (cdr datal)
4160           (gnus-data-update-list
4161            (cdr datal)
4162            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4163
4164 (defun gnus-summary-update-article (article &optional iheader)
4165   "Update ARTICLE in the summary buffer."
4166   (set-buffer gnus-summary-buffer)
4167   (let* ((header (gnus-summary-article-header article))
4168          (id (mail-header-id header))
4169          (data (gnus-data-find article))
4170          (thread (gnus-id-to-thread id))
4171          (references (mail-header-references header))
4172          (parent
4173           (gnus-id-to-thread
4174            (or (gnus-parent-id
4175                 (when (and references
4176                            (not (equal "" references)))
4177                   references))
4178                "none")))
4179          (buffer-read-only nil)
4180          (old (car thread)))
4181     (when thread
4182       (unless iheader
4183         (setcar thread nil)
4184         (when parent
4185           (delq thread parent)))
4186       (if (gnus-summary-insert-subject id header)
4187           ;; Set the (possibly) new article number in the data structure.
4188           (gnus-data-set-number data (gnus-id-to-article id))
4189         (setcar thread old)
4190         nil))))
4191
4192 (defun gnus-rebuild-thread (id &optional line)
4193   "Rebuild the thread containing ID.
4194 If LINE, insert the rebuilt thread starting on line LINE."
4195   (let ((buffer-read-only nil)
4196         old-pos current thread data)
4197     (if (not gnus-show-threads)
4198         (setq thread (list (car (gnus-id-to-thread id))))
4199       ;; Get the thread this article is part of.
4200       (setq thread (gnus-remove-thread id)))
4201     (setq old-pos (point-at-bol))
4202     (setq current (save-excursion
4203                     (and (re-search-backward "[\r\n]" nil t)
4204                          (gnus-summary-article-number))))
4205     ;; If this is a gathered thread, we have to go some re-gathering.
4206     (when (stringp (car thread))
4207       (let ((subject (car thread))
4208             roots thr)
4209         (setq thread (cdr thread))
4210         (while thread
4211           (unless (memq (setq thr (gnus-id-to-thread
4212                                    (gnus-root-id
4213                                     (mail-header-id (caar thread)))))
4214                         roots)
4215             (push thr roots))
4216           (setq thread (cdr thread)))
4217         ;; We now have all (unique) roots.
4218         (if (= (length roots) 1)
4219             ;; All the loose roots are now one solid root.
4220             (setq thread (car roots))
4221           (setq thread (cons subject (gnus-sort-threads roots))))))
4222     (let (threads)
4223       ;; We then insert this thread into the summary buffer.
4224       (when line
4225         (goto-char (point-min))
4226         (forward-line (1- line)))
4227       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4228         (if gnus-show-threads
4229             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4230           (gnus-summary-prepare-unthreaded thread))
4231         (setq data (nreverse gnus-newsgroup-data))
4232         (setq threads gnus-newsgroup-threads))
4233       ;; We splice the new data into the data structure.
4234       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4235       ;;!!! then we want to insert at the beginning of the buffer.
4236       ;;!!! That happens to be true with Gnus now, but that may
4237       ;;!!! change in the future.  Perhaps.
4238       (gnus-data-enter-list
4239        (if line nil current) data (- (point) old-pos))
4240       (setq gnus-newsgroup-threads
4241             (nconc threads gnus-newsgroup-threads))
4242       (gnus-data-compute-positions))))
4243
4244 (defun gnus-number-to-header (number)
4245   "Return the header for article NUMBER."
4246   (let ((headers gnus-newsgroup-headers))
4247     (while (and headers
4248                 (not (= number (mail-header-number (car headers)))))
4249       (pop headers))
4250     (when headers
4251       (car headers))))
4252
4253 (defun gnus-parent-headers (in-headers &optional generation)
4254   "Return the headers of the GENERATIONeth parent of HEADERS."
4255   (unless generation
4256     (setq generation 1))
4257   (let ((parent t)
4258         (headers in-headers)
4259         references)
4260     (while (and parent
4261                 (not (zerop generation))
4262                 (setq references (mail-header-references headers)))
4263       (setq headers (if (and references
4264                              (setq parent (gnus-parent-id references)))
4265                         (car (gnus-id-to-thread parent))
4266                       nil))
4267       (decf generation))
4268     (and (not (eq headers in-headers))
4269          headers)))
4270
4271 (defun gnus-id-to-thread (id)
4272   "Return the (sub-)thread where ID appears."
4273   (gnus-gethash id gnus-newsgroup-dependencies))
4274
4275 (defun gnus-id-to-article (id)
4276   "Return the article number of ID."
4277   (let ((thread (gnus-id-to-thread id)))
4278     (when (and thread
4279                (car thread))
4280       (mail-header-number (car thread)))))
4281
4282 (defun gnus-id-to-header (id)
4283   "Return the article headers of ID."
4284   (car (gnus-id-to-thread id)))
4285
4286 (defun gnus-article-displayed-root-p (article)
4287   "Say whether ARTICLE is a root(ish) article."
4288   (let ((level (gnus-summary-thread-level article))
4289         (refs (mail-header-references  (gnus-summary-article-header article)))
4290         particle)
4291     (cond
4292      ((null level) nil)
4293      ((zerop level) t)
4294      ((null refs) t)
4295      ((null (gnus-parent-id refs)) t)
4296      ((and (= 1 level)
4297            (null (setq particle (gnus-id-to-article
4298                                  (gnus-parent-id refs))))
4299            (null (gnus-summary-thread-level particle)))))))
4300
4301 (defun gnus-root-id (id)
4302   "Return the id of the root of the thread where ID appears."
4303   (let (last-id prev)
4304     (while (and id (setq prev (car (gnus-id-to-thread id))))
4305       (setq last-id id
4306             id (gnus-parent-id (mail-header-references prev))))
4307     last-id))
4308
4309 (defun gnus-articles-in-thread (thread)
4310   "Return the list of articles in THREAD."
4311   (cons (mail-header-number (car thread))
4312         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4313
4314 (defun gnus-remove-thread (id &optional dont-remove)
4315   "Remove the thread that has ID in it."
4316   (let (headers thread last-id)
4317     ;; First go up in this thread until we find the root.
4318     (setq last-id (gnus-root-id id)
4319           headers (message-flatten-list (gnus-id-to-thread last-id)))
4320     ;; We have now found the real root of this thread.  It might have
4321     ;; been gathered into some loose thread, so we have to search
4322     ;; through the threads to find the thread we wanted.
4323     (let ((threads gnus-newsgroup-threads)
4324           sub)
4325       (while threads
4326         (setq sub (car threads))
4327         (if (stringp (car sub))
4328             ;; This is a gathered thread, so we look at the roots
4329             ;; below it to find whether this article is in this
4330             ;; gathered root.
4331             (progn
4332               (setq sub (cdr sub))
4333               (while sub
4334                 (when (member (caar sub) headers)
4335                   (setq thread (car threads)
4336                         threads nil
4337                         sub nil))
4338                 (setq sub (cdr sub))))
4339           ;; It's an ordinary thread, so we check it.
4340           (when (eq (car sub) (car headers))
4341             (setq thread sub
4342                   threads nil)))
4343         (setq threads (cdr threads)))
4344       ;; If this article is in no thread, then it's a root.
4345       (if thread
4346           (unless dont-remove
4347             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4348         (setq thread (gnus-id-to-thread last-id)))
4349       (when thread
4350         (prog1
4351             thread                      ; We return this thread.
4352           (unless dont-remove
4353             (if (stringp (car thread))
4354                 (progn
4355                   ;; If we use dummy roots, then we have to remove the
4356                   ;; dummy root as well.
4357                   (when (eq gnus-summary-make-false-root 'dummy)
4358                     ;; We go to the dummy root by going to
4359                     ;; the first sub-"thread", and then one line up.
4360                     (gnus-summary-goto-article
4361                      (mail-header-number (caadr thread)))
4362                     (forward-line -1)
4363                     (gnus-delete-line)
4364                     (gnus-data-compute-positions))
4365                   (setq thread (cdr thread))
4366                   (while thread
4367                     (gnus-remove-thread-1 (car thread))
4368                     (setq thread (cdr thread))))
4369               (gnus-remove-thread-1 thread))))))))
4370
4371 (defun gnus-remove-thread-1 (thread)
4372   "Remove the thread THREAD recursively."
4373   (let ((number (mail-header-number (pop thread)))
4374         d)
4375     (setq thread (reverse thread))
4376     (while thread
4377       (gnus-remove-thread-1 (pop thread)))
4378     (when (setq d (gnus-data-find number))
4379       (goto-char (gnus-data-pos d))
4380       (gnus-summary-show-thread)
4381       (gnus-data-remove
4382        number
4383        (- (point-at-bol)
4384           (prog1
4385               (1+ (point-at-eol))
4386             (gnus-delete-line)))))))
4387
4388 (defun gnus-sort-threads-1 (threads func)
4389   (sort (mapcar (lambda (thread)
4390                   (cons (car thread)
4391                         (and (cdr thread)
4392                              (gnus-sort-threads-1 (cdr thread) func))))
4393                 threads) func))
4394
4395 (defun gnus-sort-threads (threads)
4396   "Sort THREADS."
4397   (if (not gnus-thread-sort-functions)
4398       threads
4399     (gnus-message 8 "Sorting threads...")
4400     (let ((max-lisp-eval-depth 5000))
4401       (prog1 (gnus-sort-threads-1
4402          threads
4403          (gnus-make-sort-function gnus-thread-sort-functions))
4404         (gnus-message 8 "Sorting threads...done")))))
4405
4406 (defun gnus-sort-articles (articles)
4407   "Sort ARTICLES."
4408   (when gnus-article-sort-functions
4409     (gnus-message 7 "Sorting articles...")
4410     (prog1
4411         (setq gnus-newsgroup-headers
4412               (sort articles (gnus-make-sort-function
4413                               gnus-article-sort-functions)))
4414       (gnus-message 7 "Sorting articles...done"))))
4415
4416 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4417 (defmacro gnus-thread-header (thread)
4418   "Return header of first article in THREAD.
4419 Note that THREAD must never, ever be anything else than a variable -
4420 using some other form will lead to serious barfage."
4421   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4422   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4423   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4424         (vector thread) 2))
4425
4426 (defsubst gnus-article-sort-by-number (h1 h2)
4427   "Sort articles by article number."
4428   (< (mail-header-number h1)
4429      (mail-header-number h2)))
4430
4431 (defun gnus-thread-sort-by-number (h1 h2)
4432   "Sort threads by root article number."
4433   (gnus-article-sort-by-number
4434    (gnus-thread-header h1) (gnus-thread-header h2)))
4435
4436 (defsubst gnus-article-sort-by-random (h1 h2)
4437   "Sort articles by article number."
4438   (zerop (random 2)))
4439
4440 (defun gnus-thread-sort-by-random (h1 h2)
4441   "Sort threads by root article number."
4442   (gnus-article-sort-by-random
4443    (gnus-thread-header h1) (gnus-thread-header h2)))
4444
4445 (defsubst gnus-article-sort-by-lines (h1 h2)
4446   "Sort articles by article Lines header."
4447   (< (mail-header-lines h1)
4448      (mail-header-lines h2)))
4449
4450 (defun gnus-thread-sort-by-lines (h1 h2)
4451   "Sort threads by root article Lines header."
4452   (gnus-article-sort-by-lines
4453    (gnus-thread-header h1) (gnus-thread-header h2)))
4454
4455 (defsubst gnus-article-sort-by-chars (h1 h2)
4456   "Sort articles by octet length."
4457   (< (mail-header-chars h1)
4458      (mail-header-chars h2)))
4459
4460 (defun gnus-thread-sort-by-chars (h1 h2)
4461   "Sort threads by root article octet length."
4462   (gnus-article-sort-by-chars
4463    (gnus-thread-header h1) (gnus-thread-header h2)))
4464
4465 (defsubst gnus-article-sort-by-author (h1 h2)
4466   "Sort articles by root author."
4467   (string-lessp
4468    (let ((addr (car (mime-entity-read-field h1 'From))))
4469      (or (std11-full-name-string addr)
4470          (std11-address-string addr)
4471          ""))
4472    (let ((addr (car (mime-entity-read-field h2 'From))))
4473      (or (std11-full-name-string addr)
4474          (std11-address-string addr)
4475          ""))
4476    ))
4477
4478 (defun gnus-thread-sort-by-author (h1 h2)
4479   "Sort threads by root author."
4480   (gnus-article-sort-by-author
4481    (gnus-thread-header h1)  (gnus-thread-header h2)))
4482
4483 (defsubst gnus-article-sort-by-subject (h1 h2)
4484   "Sort articles by root subject."
4485   (string-lessp
4486    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4487    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4488
4489 (defun gnus-thread-sort-by-subject (h1 h2)
4490   "Sort threads by root subject."
4491   (gnus-article-sort-by-subject
4492    (gnus-thread-header h1) (gnus-thread-header h2)))
4493
4494 (defsubst gnus-article-sort-by-date (h1 h2)
4495   "Sort articles by root article date."
4496   (time-less-p
4497    (gnus-date-get-time (mail-header-date h1))
4498    (gnus-date-get-time (mail-header-date h2))))
4499
4500 (defun gnus-thread-sort-by-date (h1 h2)
4501   "Sort threads by root article date."
4502   (gnus-article-sort-by-date
4503    (gnus-thread-header h1) (gnus-thread-header h2)))
4504
4505 (defsubst gnus-article-sort-by-score (h1 h2)
4506   "Sort articles by root article score.
4507 Unscored articles will be counted as having a score of zero."
4508   (> (or (cdr (assq (mail-header-number h1)
4509                     gnus-newsgroup-scored))
4510          gnus-summary-default-score 0)
4511      (or (cdr (assq (mail-header-number h2)
4512                     gnus-newsgroup-scored))
4513          gnus-summary-default-score 0)))
4514
4515 (defun gnus-thread-sort-by-score (h1 h2)
4516   "Sort threads by root article score."
4517   (gnus-article-sort-by-score
4518    (gnus-thread-header h1) (gnus-thread-header h2)))
4519
4520 (defun gnus-thread-sort-by-total-score (h1 h2)
4521   "Sort threads by the sum of all scores in the thread.
4522 Unscored articles will be counted as having a score of zero."
4523   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4524
4525 (defun gnus-thread-total-score (thread)
4526   ;; This function find the total score of THREAD.
4527   (cond
4528    ((null thread)
4529     0)
4530    ((consp thread)
4531     (if (stringp (car thread))
4532         (apply gnus-thread-score-function 0
4533                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4534       (gnus-thread-total-score-1 thread)))
4535    (t
4536     (gnus-thread-total-score-1 (list thread)))))
4537
4538 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4539   "Sort threads such that the thread with the most recently arrived article comes first."
4540   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4541
4542 (defun gnus-thread-highest-number (thread)
4543   "Return the highest article number in THREAD."
4544   (apply 'max (mapcar (lambda (header)
4545                         (mail-header-number header))
4546                       (message-flatten-list thread))))
4547
4548 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4549   "Sort threads such that the thread with the most recently dated article comes first."
4550   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4551
4552 (defun gnus-thread-latest-date (thread)
4553   "Return the highest article date in THREAD."
4554   (let ((previous-time 0))
4555     (apply 'max
4556            (mapcar
4557             (lambda (header)
4558               (setq previous-time
4559                     (condition-case ()
4560                         (time-to-seconds (mail-header-parse-date
4561                                           (mail-header-date header)))
4562                       (error previous-time))))
4563             (sort
4564              (message-flatten-list thread)
4565              (lambda (h1 h2)
4566                (< (mail-header-number h1)
4567                   (mail-header-number h2))))))))
4568
4569 (defun gnus-thread-total-score-1 (root)
4570   ;; This function find the total score of the thread below ROOT.
4571   (setq root (car root))
4572   (apply gnus-thread-score-function
4573          (or (append
4574               (mapcar 'gnus-thread-total-score
4575                       (cdr (gnus-id-to-thread (mail-header-id root))))
4576               (when (> (mail-header-number root) 0)
4577                 (list (or (cdr (assq (mail-header-number root)
4578                                      gnus-newsgroup-scored))
4579                           gnus-summary-default-score 0))))
4580              (list gnus-summary-default-score)
4581              '(0))))
4582
4583 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4584 (defvar gnus-tmp-prev-subject nil)
4585 (defvar gnus-tmp-false-parent nil)
4586 (defvar gnus-tmp-root-expunged nil)
4587 (defvar gnus-tmp-dummy-line nil)
4588
4589 (eval-when-compile (defvar gnus-tmp-header))
4590 (defun gnus-extra-header (type &optional header)
4591   "Return the extra header of TYPE."
4592   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4593       ""))
4594
4595 (defvar gnus-tmp-thread-tree-header-string "")
4596
4597 (defcustom gnus-sum-thread-tree-root "> "
4598   "With %B spec, used for the root of a thread.
4599 If nil, use subject instead."
4600   :type '(radio (const :format "%v  " nil) (string :size 0))
4601   :group 'gnus-thread)
4602 (defcustom gnus-sum-thread-tree-false-root "> "
4603   "With %B spec, used for a false root of a thread.
4604 If nil, use subject instead."
4605   :type '(radio (const :format "%v  " nil) (string :size 0))
4606   :group 'gnus-thread)
4607 (defcustom gnus-sum-thread-tree-single-indent ""
4608   "With %B spec, used for a thread with just one message.
4609 If nil, use subject instead."
4610   :type '(radio (const :format "%v  " nil) (string :size 0))
4611   :group 'gnus-thread)
4612 (defcustom gnus-sum-thread-tree-vertical "| "
4613   "With %B spec, used for drawing a vertical line."
4614   :type 'string
4615   :group 'gnus-thread)
4616 (defcustom gnus-sum-thread-tree-indent "  "
4617   "With %B spec, used for indenting."
4618   :type 'string
4619   :group 'gnus-thread)
4620 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4621   "With %B spec, used for a leaf with brothers."
4622   :type 'string
4623   :group 'gnus-thread)
4624 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4625   "With %B spec, used for a leaf without brothers."
4626   :type 'string
4627   :group 'gnus-thread)
4628
4629 (defun gnus-summary-prepare-threads (threads)
4630   "Prepare summary buffer from THREADS and indentation LEVEL.
4631 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4632 or a straight list of headers."
4633   (gnus-message 7 "Generating summary...")
4634
4635   (setq gnus-newsgroup-threads threads)
4636   (beginning-of-line)
4637
4638   (let ((gnus-tmp-level 0)
4639         (default-score (or gnus-summary-default-score 0))
4640         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4641         (building-line-count gnus-summary-display-while-building)
4642         (building-count (integerp gnus-summary-display-while-building))
4643         thread number subject stack state gnus-tmp-gathered beg-match
4644         new-roots gnus-tmp-new-adopts thread-end simp-subject
4645         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4646         gnus-tmp-replied gnus-tmp-subject-or-nil
4647         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4648         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4649         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4650         tree-stack)
4651
4652     (setq gnus-tmp-prev-subject nil
4653           gnus-tmp-thread-tree-header-string "")
4654
4655     (if (vectorp (car threads))
4656         ;; If this is a straight (sic) list of headers, then a
4657         ;; threaded summary display isn't required, so we just create
4658         ;; an unthreaded one.
4659         (gnus-summary-prepare-unthreaded threads)
4660
4661       ;; Do the threaded display.
4662
4663       (if gnus-summary-display-while-building
4664           (switch-to-buffer (buffer-name)))
4665       (while (or threads stack gnus-tmp-new-adopts new-roots)
4666
4667         (if (and (= gnus-tmp-level 0)
4668                  (or (not stack)
4669                      (= (caar stack) 0))
4670                  (not gnus-tmp-false-parent)
4671                  (or gnus-tmp-new-adopts new-roots))
4672             (if gnus-tmp-new-adopts
4673                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4674                       thread (list (car gnus-tmp-new-adopts))
4675                       gnus-tmp-header (caar thread)
4676                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4677               (when new-roots
4678                 (setq thread (list (car new-roots))
4679                       gnus-tmp-header (caar thread)
4680                       new-roots (cdr new-roots))))
4681
4682           (if threads
4683               ;; If there are some threads, we do them before the
4684               ;; threads on the stack.
4685               (setq thread threads
4686                     gnus-tmp-header (caar thread))
4687             ;; There were no current threads, so we pop something off
4688             ;; the stack.
4689             (setq state (car stack)
4690                   gnus-tmp-level (car state)
4691                   tree-stack (cadr state)
4692                   thread (caddr state)
4693                   stack (cdr stack)
4694                   gnus-tmp-header (caar thread))))
4695
4696         (setq gnus-tmp-false-parent nil)
4697         (setq gnus-tmp-root-expunged nil)
4698         (setq thread-end nil)
4699
4700         (if (stringp gnus-tmp-header)
4701             ;; The header is a dummy root.
4702             (cond
4703              ((eq gnus-summary-make-false-root 'adopt)
4704               ;; We let the first article adopt the rest.
4705               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4706                                                (cddar thread)))
4707               (setq gnus-tmp-gathered
4708                     (nconc (mapcar
4709                             (lambda (h) (mail-header-number (car h)))
4710                             (cddar thread))
4711                            gnus-tmp-gathered))
4712               (setq thread (cons (list (caar thread)
4713                                        (cadar thread))
4714                                  (cdr thread)))
4715               (setq gnus-tmp-level -1
4716                     gnus-tmp-false-parent t))
4717              ((eq gnus-summary-make-false-root 'empty)
4718               ;; We print adopted articles with empty subject fields.
4719               (setq gnus-tmp-gathered
4720                     (nconc (mapcar
4721                             (lambda (h) (mail-header-number (car h)))
4722                             (cddar thread))
4723                            gnus-tmp-gathered))
4724               (setq gnus-tmp-level -1))
4725              ((eq gnus-summary-make-false-root 'dummy)
4726               ;; We remember that we probably want to output a dummy
4727               ;; root.
4728               (setq gnus-tmp-dummy-line gnus-tmp-header)
4729               (setq gnus-tmp-prev-subject gnus-tmp-header))
4730              (t
4731               ;; We do not make a root for the gathered
4732               ;; sub-threads at all.
4733               (setq gnus-tmp-level -1)))
4734
4735           (setq number (mail-header-number gnus-tmp-header)
4736                 subject (mail-header-subject gnus-tmp-header)
4737                 simp-subject (gnus-simplify-subject-fully subject))
4738
4739           (cond
4740            ;; If the thread has changed subject, we might want to make
4741            ;; this subthread into a root.
4742            ((and (null gnus-thread-ignore-subject)
4743                  (not (zerop gnus-tmp-level))
4744                  gnus-tmp-prev-subject
4745                  (not (string= gnus-tmp-prev-subject simp-subject)))
4746             (setq new-roots (nconc new-roots (list (car thread)))
4747                   thread-end t
4748                   gnus-tmp-header nil))
4749            ;; If the article lies outside the current limit,
4750            ;; then we do not display it.
4751            ((not (memq number gnus-newsgroup-limit))
4752             (setq gnus-tmp-gathered
4753                   (nconc (mapcar
4754                           (lambda (h) (mail-header-number (car h)))
4755                           (cdar thread))
4756                          gnus-tmp-gathered))
4757             (setq gnus-tmp-new-adopts (if (cdar thread)
4758                                           (append gnus-tmp-new-adopts
4759                                                   (cdar thread))
4760                                         gnus-tmp-new-adopts)
4761                   thread-end t
4762                   gnus-tmp-header nil)
4763             (when (zerop gnus-tmp-level)
4764               (setq gnus-tmp-root-expunged t)))
4765            ;; Perhaps this article is to be marked as read?
4766            ((and gnus-summary-mark-below
4767                  (< (or (cdr (assq number gnus-newsgroup-scored))
4768                         default-score)
4769                     gnus-summary-mark-below)
4770                  ;; Don't touch sparse articles.
4771                  (not (gnus-summary-article-sparse-p number))
4772                  (not (gnus-summary-article-ancient-p number)))
4773             (setq gnus-newsgroup-unreads
4774                   (delq number gnus-newsgroup-unreads))
4775             (if gnus-newsgroup-auto-expire
4776                 (setq gnus-newsgroup-expirable
4777                       (gnus-add-to-sorted-list
4778                        gnus-newsgroup-expirable number))
4779               (push (cons number gnus-low-score-mark)
4780                     gnus-newsgroup-reads))))
4781
4782           (when gnus-tmp-header
4783             ;; We may have an old dummy line to output before this
4784             ;; article.
4785             (when (and gnus-tmp-dummy-line
4786                        (gnus-subject-equal
4787                         gnus-tmp-dummy-line
4788                         (mail-header-subject gnus-tmp-header)))
4789               (gnus-summary-insert-dummy-line
4790                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4791               (setq gnus-tmp-dummy-line nil))
4792
4793             ;; Compute the mark.
4794             (setq gnus-tmp-unread (gnus-article-mark number))
4795
4796             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4797                                   gnus-tmp-header gnus-tmp-level)
4798                   gnus-newsgroup-data)
4799
4800             ;; Actually insert the line.
4801             (setq
4802              gnus-tmp-subject-or-nil
4803              (cond
4804               ((and gnus-thread-ignore-subject
4805                     gnus-tmp-prev-subject
4806                     (not (string= gnus-tmp-prev-subject simp-subject)))
4807                subject)
4808               ((zerop gnus-tmp-level)
4809                (if (and (eq gnus-summary-make-false-root 'empty)
4810                         (memq number gnus-tmp-gathered)
4811                         gnus-tmp-prev-subject
4812                         (string= gnus-tmp-prev-subject simp-subject))
4813                    gnus-summary-same-subject
4814                  subject))
4815               (t gnus-summary-same-subject)))
4816             (if (and (eq gnus-summary-make-false-root 'adopt)
4817                      (= gnus-tmp-level 1)
4818                      (memq number gnus-tmp-gathered))
4819                 (setq gnus-tmp-opening-bracket ?\<
4820                       gnus-tmp-closing-bracket ?\>)
4821               (setq gnus-tmp-opening-bracket ?\[
4822                     gnus-tmp-closing-bracket ?\]))
4823             (setq
4824              gnus-tmp-indentation
4825              (aref gnus-thread-indent-array gnus-tmp-level)
4826              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4827              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4828                                 gnus-summary-default-score 0)
4829              gnus-tmp-score-char
4830              (if (or (null gnus-summary-default-score)
4831                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4832                          gnus-summary-zcore-fuzz))
4833                  ?\ ;;;Whitespace
4834                (if (< gnus-tmp-score gnus-summary-default-score)
4835                    gnus-score-below-mark gnus-score-over-mark))
4836              gnus-tmp-replied
4837              (cond ((memq number gnus-newsgroup-processable)
4838                     gnus-process-mark)
4839                    ((memq number gnus-newsgroup-cached)
4840                     gnus-cached-mark)
4841                    ((memq number gnus-newsgroup-replied)
4842                     gnus-replied-mark)
4843                    ((memq number gnus-newsgroup-forwarded)
4844                     gnus-forwarded-mark)
4845                    ((memq number gnus-newsgroup-saved)
4846                     gnus-saved-mark)
4847                    ((memq number gnus-newsgroup-recent)
4848                     gnus-recent-mark)
4849                    ((memq number gnus-newsgroup-unseen)
4850                     gnus-unseen-mark)
4851                    (t gnus-no-mark))
4852              gnus-tmp-downloaded
4853              (cond ((memq number gnus-newsgroup-undownloaded)
4854                     gnus-undownloaded-mark)
4855                    (gnus-newsgroup-agentized
4856                     gnus-downloaded-mark)
4857                    (t
4858                     gnus-no-mark))
4859              gnus-tmp-from (mail-header-from gnus-tmp-header)
4860              gnus-tmp-name
4861              (cond
4862               ((string-match "<[^>]+> *$" gnus-tmp-from)
4863                (setq beg-match (match-beginning 0))
4864                (or (and (string-match "^\".+\"" gnus-tmp-from)
4865                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4866                    (substring gnus-tmp-from 0 beg-match)))
4867               ((string-match "(.+)" gnus-tmp-from)
4868                (substring gnus-tmp-from
4869                           (1+ (match-beginning 0)) (1- (match-end 0))))
4870               (t gnus-tmp-from))
4871
4872              ;; Do the %B string
4873              gnus-tmp-thread-tree-header-string
4874              (cond
4875               ((not gnus-show-threads) "")
4876               ((zerop gnus-tmp-level)
4877                (cond ((cdar thread)
4878                       (or gnus-sum-thread-tree-root subject))
4879                      (gnus-tmp-new-adopts
4880                       (or gnus-sum-thread-tree-false-root subject))
4881                      (t
4882                       (or gnus-sum-thread-tree-single-indent subject))))
4883               (t
4884                (concat (apply 'concat
4885                               (mapcar (lambda (item)
4886                                         (if (= item 1)
4887                                             gnus-sum-thread-tree-vertical
4888                                           gnus-sum-thread-tree-indent))
4889                                       (cdr (reverse tree-stack))))
4890                        (if (nth 1 thread)
4891                            gnus-sum-thread-tree-leaf-with-other
4892                          gnus-sum-thread-tree-single-leaf)))))
4893             (when (string= gnus-tmp-name "")
4894               (setq gnus-tmp-name gnus-tmp-from))
4895             (unless (numberp gnus-tmp-lines)
4896               (setq gnus-tmp-lines -1))
4897             (if (= gnus-tmp-lines -1)
4898                 (setq gnus-tmp-lines "?")
4899               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4900               (gnus-put-text-property
4901              (point)
4902              (progn (eval gnus-summary-line-format-spec) (point))
4903                'gnus-number number)
4904             (when gnus-visual-p
4905               (forward-line -1)
4906               (gnus-run-hooks 'gnus-summary-update-hook)
4907               (forward-line 1))
4908
4909             (setq gnus-tmp-prev-subject simp-subject)))
4910
4911         (when (nth 1 thread)
4912           (push (list (max 0 gnus-tmp-level)
4913                       (copy-sequence tree-stack)
4914                       (nthcdr 1 thread))
4915                 stack))
4916         (push (if (nth 1 thread) 1 0) tree-stack)
4917         (incf gnus-tmp-level)
4918         (setq threads (if thread-end nil (cdar thread)))
4919         (if gnus-summary-display-while-building
4920             (if building-count
4921                 (progn
4922                   ;; use a set frequency
4923                   (setq building-line-count (1- building-line-count))
4924                   (when (= building-line-count 0)
4925                     (sit-for 0)
4926                     (setq building-line-count
4927                           gnus-summary-display-while-building)))
4928               ;; always
4929               (sit-for 0)))
4930         (unless threads
4931           (setq gnus-tmp-level 0)))))
4932   (gnus-message 7 "Generating summary...done"))
4933
4934 (defun gnus-summary-prepare-unthreaded (headers)
4935   "Generate an unthreaded summary buffer based on HEADERS."
4936   (let (header number mark)
4937
4938     (beginning-of-line)
4939
4940     (while headers
4941       ;; We may have to root out some bad articles...
4942       (when (memq (setq number (mail-header-number
4943                                 (setq header (pop headers))))
4944                   gnus-newsgroup-limit)
4945         ;; Mark article as read when it has a low score.
4946         (when (and gnus-summary-mark-below
4947                    (< (or (cdr (assq number gnus-newsgroup-scored))
4948                           gnus-summary-default-score 0)
4949                       gnus-summary-mark-below)
4950                    (not (gnus-summary-article-ancient-p number)))
4951           (setq gnus-newsgroup-unreads
4952                 (delq number gnus-newsgroup-unreads))
4953           (if gnus-newsgroup-auto-expire
4954               (push number gnus-newsgroup-expirable)
4955             (push (cons number gnus-low-score-mark)
4956                   gnus-newsgroup-reads)))
4957
4958         (setq mark (gnus-article-mark number))
4959         (push (gnus-data-make number mark (1+ (point)) header 0)
4960               gnus-newsgroup-data)
4961         (gnus-summary-insert-line
4962          header 0 number
4963          (memq number gnus-newsgroup-undownloaded)
4964          mark (memq number gnus-newsgroup-replied)
4965          (memq number gnus-newsgroup-expirable)
4966          (mail-header-subject header) nil
4967          (cdr (assq number gnus-newsgroup-scored))
4968          (memq number gnus-newsgroup-processable))))))
4969
4970 (defun gnus-summary-remove-list-identifiers ()
4971   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4972   (let ((regexp (if (consp gnus-list-identifiers)
4973                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4974                   gnus-list-identifiers))
4975         changed subject)
4976     (when regexp
4977       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
4978       (dolist (header gnus-newsgroup-headers)
4979         (setq subject (mail-header-subject header)
4980               changed nil)
4981         (while (string-match regexp subject)
4982           (setq subject
4983                 (concat (substring subject 0 (match-beginning 1))
4984                         (substring subject (match-end 0)))
4985                 changed t))
4986         (when changed
4987           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
4988             (setq subject
4989                   (concat (substring subject 0 (match-beginning 1))
4990                           (substring subject (match-end 1)))))
4991           (mail-header-set-subject header subject))))))
4992
4993 (defun gnus-fetch-headers (articles)
4994   "Fetch headers of ARTICLES."
4995   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4996     (gnus-message 5 "Fetching headers for %s..." name)
4997     (prog1
4998         (if (eq 'nov
4999                 (setq gnus-headers-retrieved-by
5000                       (gnus-retrieve-headers
5001                        articles gnus-newsgroup-name
5002                        ;; We might want to fetch old headers, but
5003                        ;; not if there is only 1 article.
5004                        (and (or (and
5005                                  (not (eq gnus-fetch-old-headers 'some))
5006                                  (not (numberp gnus-fetch-old-headers)))
5007                                 (> (length articles) 1))
5008                             gnus-fetch-old-headers))))
5009             (gnus-get-newsgroup-headers-xover
5010              articles nil nil gnus-newsgroup-name t)
5011           (gnus-get-newsgroup-headers))
5012       (gnus-message 5 "Fetching headers for %s...done" name))))
5013
5014 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5015   "Select newsgroup GROUP.
5016 If READ-ALL is non-nil, all articles in the group are selected.
5017 If SELECT-ARTICLES, only select those articles from GROUP."
5018   (let* ((entry (gnus-group-entry group))
5019          ;;!!! Dirty hack; should be removed.
5020          (gnus-summary-ignore-duplicates
5021           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5022               t
5023             gnus-summary-ignore-duplicates))
5024          (info (nth 2 entry))
5025          articles fetched-articles cached)
5026
5027     (unless (gnus-check-server
5028              (set (make-local-variable 'gnus-current-select-method)
5029                   (gnus-find-method-for-group group)))
5030       (error "Couldn't open server"))
5031
5032     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5033         (gnus-activate-group group)     ; Or we can activate it...
5034         (progn                          ; Or we bug out.
5035           (when (equal major-mode 'gnus-summary-mode)
5036             (gnus-kill-buffer (current-buffer)))
5037           (error "Couldn't activate group %s: %s"
5038                  group (gnus-status-message group))))
5039
5040     (unless (gnus-request-group group t)
5041       (when (equal major-mode 'gnus-summary-mode)
5042         (gnus-kill-buffer (current-buffer)))
5043       (error "Couldn't request group %s: %s"
5044              group (gnus-status-message group)))
5045
5046     (when gnus-agent
5047       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5048       
5049       (setq gnus-summary-use-undownloaded-faces
5050             (gnus-agent-find-parameter
5051              group
5052              'agent-enable-undownloaded-faces)))
5053
5054     (setq gnus-newsgroup-name group
5055           gnus-newsgroup-unselected nil
5056           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5057
5058     (let ((display (gnus-group-find-parameter group 'display)))
5059       (setq gnus-newsgroup-display
5060             (cond
5061              ((not (zerop (or (car-safe read-all) 0)))
5062               ;; The user entered the group with C-u SPC/RET, let's show
5063               ;; all articles.
5064               'gnus-not-ignore)
5065              ((eq display 'all)
5066               'gnus-not-ignore)
5067              ((arrayp display)
5068               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5069              ((numberp display)
5070               ;; The following is probably the "correct" solution, but
5071               ;; it makes Gnus fetch all headers and then limit the
5072               ;; articles (which is slow), so instead we hack the
5073               ;; select-articles parameter instead. -- Simon Josefsson
5074               ;; <jas@kth.se>
5075               ;;
5076               ;; (gnus-byte-compile
5077               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5078               ;;                         display)))))
5079               (setq select-articles
5080                     (gnus-uncompress-range
5081                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5082                              (if (> tmp 0)
5083                                  tmp
5084                                1))
5085                            (cdr (gnus-active group)))))
5086               nil)
5087              (t
5088               nil))))
5089
5090     (gnus-summary-setup-default-charset)
5091
5092     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5093     (when (gnus-virtual-group-p group)
5094       (setq cached gnus-newsgroup-cached))
5095
5096     (setq gnus-newsgroup-unreads
5097           (gnus-sorted-ndifference
5098            (gnus-sorted-ndifference gnus-newsgroup-unreads
5099                                     gnus-newsgroup-marked)
5100            gnus-newsgroup-dormant))
5101
5102     (setq gnus-newsgroup-processable nil)
5103
5104     (gnus-update-read-articles group gnus-newsgroup-unreads)
5105
5106     ;; Adjust and set lists of article marks.
5107     (when info
5108       (gnus-adjust-marked-articles info))
5109     (if (setq articles select-articles)
5110         (setq gnus-newsgroup-unselected
5111               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5112       (setq articles (gnus-articles-to-read group read-all)))
5113
5114     (cond
5115      ((null articles)
5116       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5117       'quit)
5118      ((eq articles 0) nil)
5119      (t
5120       ;; Init the dependencies hash table.
5121       (setq gnus-newsgroup-dependencies
5122             (gnus-make-hashtable (length articles)))
5123       (gnus-set-global-variables)
5124       ;; Retrieve the headers and read them in.
5125
5126       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5127
5128       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5129       (when cached
5130         (setq gnus-newsgroup-cached cached))
5131
5132       ;; Suppress duplicates?
5133       (when gnus-suppress-duplicates
5134         (gnus-dup-suppress-articles))
5135
5136       ;; Set the initial limit.
5137       (setq gnus-newsgroup-limit (copy-sequence articles))
5138       ;; Remove canceled articles from the list of unread articles.
5139       (setq fetched-articles
5140             (mapcar (lambda (headers) (mail-header-number headers))
5141                     gnus-newsgroup-headers))
5142       (setq gnus-newsgroup-articles fetched-articles)
5143       (setq gnus-newsgroup-unreads
5144             (gnus-sorted-nintersection
5145              gnus-newsgroup-unreads fetched-articles))
5146       (gnus-compute-unseen-list)
5147
5148       ;; Removed marked articles that do not exist.
5149       (gnus-update-missing-marks
5150        (gnus-sorted-difference articles fetched-articles))
5151       ;; We might want to build some more threads first.
5152       (when (and gnus-fetch-old-headers
5153                  (eq gnus-headers-retrieved-by 'nov))
5154         (if (eq gnus-fetch-old-headers 'invisible)
5155             (gnus-build-all-threads)
5156           (gnus-build-old-threads)))
5157       ;; Let the Gnus agent mark articles as read.
5158       (when gnus-agent
5159         (gnus-agent-get-undownloaded-list))
5160       ;; Remove list identifiers from subject
5161       (when gnus-list-identifiers
5162         (gnus-summary-remove-list-identifiers))
5163       ;; Check whether auto-expire is to be done in this group.
5164       (setq gnus-newsgroup-auto-expire
5165             (gnus-group-auto-expirable-p group))
5166       ;; Set up the article buffer now, if necessary.
5167       (unless gnus-single-article-buffer
5168         (gnus-article-setup-buffer))
5169       ;; First and last article in this newsgroup.
5170       (when gnus-newsgroup-headers
5171         (setq gnus-newsgroup-begin
5172               (mail-header-number (car gnus-newsgroup-headers))
5173               gnus-newsgroup-end
5174               (mail-header-number
5175                (gnus-last-element gnus-newsgroup-headers))))
5176       ;; GROUP is successfully selected.
5177       (or gnus-newsgroup-headers t)))))
5178
5179 (defun gnus-compute-unseen-list ()
5180   ;; The `seen' marks are treated specially.
5181   (if (not gnus-newsgroup-seen)
5182       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5183     (setq gnus-newsgroup-unseen
5184           (gnus-inverse-list-range-intersection
5185            gnus-newsgroup-articles gnus-newsgroup-seen))))
5186
5187 (defun gnus-summary-display-make-predicate (display)
5188   (require 'gnus-agent)
5189   (when (= (length display) 1)
5190     (setq display (car display)))
5191   (unless gnus-summary-display-cache
5192     (dolist (elem (append '((unread . unread)
5193                             (read . read)
5194                             (unseen . unseen))
5195                           gnus-article-mark-lists))
5196       (push (cons (cdr elem)
5197                   (gnus-byte-compile
5198                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5199             gnus-summary-display-cache)))
5200   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5201         (gnus-category-predicate-cache gnus-summary-display-cache))
5202     (gnus-get-predicate display)))
5203
5204 ;; Uses the dynamically bound `number' variable.
5205 (eval-when-compile
5206   (defvar number))
5207 (defun gnus-article-marked-p (type &optional article)
5208   (let ((article (or article number)))
5209     (cond
5210      ((eq type 'tick)
5211       (memq article gnus-newsgroup-marked))
5212      ((eq type 'spam)
5213       (memq article gnus-newsgroup-spam-marked))
5214      ((eq type 'unsend)
5215       (memq article gnus-newsgroup-unsendable))
5216      ((eq type 'undownload)
5217       (memq article gnus-newsgroup-undownloaded))
5218      ((eq type 'download)
5219       (memq article gnus-newsgroup-downloadable))
5220      ((eq type 'unread)
5221       (memq article gnus-newsgroup-unreads))
5222      ((eq type 'read)
5223       (memq article gnus-newsgroup-reads))
5224      ((eq type 'dormant)
5225       (memq article gnus-newsgroup-dormant) )
5226      ((eq type 'expire)
5227       (memq article gnus-newsgroup-expirable))
5228      ((eq type 'reply)
5229       (memq article gnus-newsgroup-replied))
5230      ((eq type 'killed)
5231       (memq article gnus-newsgroup-killed))
5232      ((eq type 'bookmark)
5233       (assq article gnus-newsgroup-bookmarks))
5234      ((eq type 'score)
5235       (assq article gnus-newsgroup-scored))
5236      ((eq type 'save)
5237       (memq article gnus-newsgroup-saved))
5238      ((eq type 'cache)
5239       (memq article gnus-newsgroup-cached))
5240      ((eq type 'forward)
5241       (memq article gnus-newsgroup-forwarded))
5242      ((eq type 'seen)
5243       (not (memq article gnus-newsgroup-unseen)))
5244      ((eq type 'recent)
5245       (memq article gnus-newsgroup-recent))
5246      (t t))))
5247
5248 (defun gnus-articles-to-read (group &optional read-all)
5249   "Find out what articles the user wants to read."
5250   (let* ((display (gnus-group-find-parameter group 'display))
5251          (articles
5252           ;; Select all articles if `read-all' is non-nil, or if there
5253           ;; are no unread articles.
5254           (if (or read-all
5255                   (and (zerop (length gnus-newsgroup-marked))
5256                        (zerop (length gnus-newsgroup-unreads)))
5257                   ;; Fetch all if the predicate is non-nil.
5258                   gnus-newsgroup-display)
5259               ;; We want to select the headers for all the articles in
5260               ;; the group, so we select either all the active
5261               ;; articles in the group, or (if that's nil), the
5262               ;; articles in the cache.
5263               (or
5264                (gnus-uncompress-range (gnus-active group))
5265                (gnus-cache-articles-in-group group))
5266             ;; Select only the "normal" subset of articles.
5267             (gnus-sorted-nunion
5268              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5269              gnus-newsgroup-unreads)))
5270          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5271          (scored (length scored-list))
5272          (number (length articles))
5273          (marked (+ (length gnus-newsgroup-marked)
5274                     (length gnus-newsgroup-dormant)))
5275          (select
5276           (cond
5277            ((numberp read-all)
5278             read-all)
5279            ((numberp gnus-newsgroup-display)
5280             gnus-newsgroup-display)
5281            (t
5282             (condition-case ()
5283                 (cond
5284                  ((and (or (<= scored marked) (= scored number))
5285                        (numberp gnus-large-newsgroup)
5286                        (> number gnus-large-newsgroup))
5287                   (let* ((cursor-in-echo-area nil)
5288                          (initial (gnus-parameter-large-newsgroup-initial
5289                                    gnus-newsgroup-name))
5290                          (input
5291                           (read-string
5292                            (format
5293                             "How many articles from %s (%s %d): "
5294                             (gnus-limit-string
5295                              (gnus-group-decoded-name gnus-newsgroup-name)
5296                              35)
5297                             (if initial "max" "default")
5298                             number)
5299                            (if initial
5300                                (cons (number-to-string initial)
5301                                      0)))))
5302                     (if (string-match "^[ \t]*$" input) number input)))
5303                  ((and (> scored marked) (< scored number)
5304                        (> (- scored number) 20))
5305                   (let ((input
5306                          (read-string
5307                           (format "%s %s (%d scored, %d total): "
5308                                   "How many articles from"
5309                                   (gnus-group-decoded-name group)
5310                                   scored number))))
5311                     (if (string-match "^[ \t]*$" input)
5312                         number input)))
5313                  (t number))
5314               (quit
5315                (message "Quit getting the articles to read")
5316                nil))))))
5317     (setq select (if (stringp select) (string-to-number select) select))
5318     (if (or (null select) (zerop select))
5319         select
5320       (if (and (not (zerop scored)) (<= (abs select) scored))
5321           (progn
5322             (setq articles (sort scored-list '<))
5323             (setq number (length articles)))
5324         (setq articles (copy-sequence articles)))
5325
5326       (when (< (abs select) number)
5327         (if (< select 0)
5328             ;; Select the N oldest articles.
5329             (setcdr (nthcdr (1- (abs select)) articles) nil)
5330           ;; Select the N most recent articles.
5331           (setq articles (nthcdr (- number select) articles))))
5332       (setq gnus-newsgroup-unselected
5333             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5334       (when gnus-alter-articles-to-read-function
5335         (setq articles
5336               (sort
5337                (funcall gnus-alter-articles-to-read-function
5338                         gnus-newsgroup-name articles)
5339                '<)))
5340       articles)))
5341
5342 (defun gnus-killed-articles (killed articles)
5343   (let (out)
5344     (while articles
5345       (when (inline (gnus-member-of-range (car articles) killed))
5346         (push (car articles) out))
5347       (setq articles (cdr articles)))
5348     out))
5349
5350 (defun gnus-uncompress-marks (marks)
5351   "Uncompress the mark ranges in MARKS."
5352   (let ((uncompressed '(score bookmark))
5353         out)
5354     (while marks
5355       (if (memq (caar marks) uncompressed)
5356           (push (car marks) out)
5357         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5358       (setq marks (cdr marks)))
5359     out))
5360
5361 (defun gnus-article-mark-to-type (mark)
5362   "Return the type of MARK."
5363   (or (cadr (assq mark gnus-article-special-mark-lists))
5364       'list))
5365
5366 (defun gnus-article-unpropagatable-p (mark)
5367   "Return whether MARK should be propagated to back end."
5368   (memq mark gnus-article-unpropagated-mark-lists))
5369
5370 (defun gnus-adjust-marked-articles (info)
5371   "Set all article lists and remove all marks that are no longer valid."
5372   (let* ((marked-lists (gnus-info-marks info))
5373          (active (gnus-active (gnus-info-group info)))
5374          (min (car active))
5375          (max (cdr active))
5376          (types gnus-article-mark-lists)
5377          marks var articles article mark mark-type
5378          bgn end)
5379
5380     (dolist (marks marked-lists)
5381       (setq mark (car marks)
5382             mark-type (gnus-article-mark-to-type mark)
5383             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5384
5385       ;; We set the variable according to the type of the marks list,
5386       ;; and then adjust the marks to a subset of the active articles.
5387       (cond
5388        ;; Adjust "simple" lists - compressed yet unsorted
5389        ((eq mark-type 'list)
5390         ;; Simultaneously uncompress and clip to active range
5391         ;; See gnus-uncompress-range for a description of possible marks
5392         (let (l lh)
5393           (if (not (cadr marks))
5394               (set var nil)
5395             (setq articles (if (numberp (cddr marks))
5396                                (list (cdr marks))
5397                              (cdr marks))
5398                   lh (cons nil nil)
5399                   l lh)
5400
5401             (while (setq article (pop articles))
5402               (cond ((consp article)
5403                      (setq bgn (max (car article) min)
5404                            end (min (cdr article) max))
5405                      (while (<= bgn end)
5406                        (setq l (setcdr l (cons bgn nil))
5407                              bgn (1+ bgn))))
5408                     ((and (<= min article)
5409                           (>= max article))
5410                      (setq l (setcdr l (cons article nil))))))
5411             (set var (cdr lh)))))
5412        ;; Adjust assocs.
5413        ((eq mark-type 'tuple)
5414         (set var (setq articles (cdr marks)))
5415         (when (not (listp (cdr (symbol-value var))))
5416           (set var (list (symbol-value var))))
5417         (when (not (listp (cdr articles)))
5418           (setq articles (list articles)))
5419         (while articles
5420           (when (or (not (consp (setq article (pop articles))))
5421                     (< (car article) min)
5422                     (> (car article) max))
5423             (set var (delq article (symbol-value var))))))
5424        ;; Adjust ranges (sloppily).
5425        ((eq mark-type 'range)
5426         (cond
5427          ((eq mark 'seen)
5428           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5429           ;; It should be (seen (NUM1 . NUM2)).
5430           (when (numberp (cddr marks))
5431             (setcdr marks (list (cdr marks))))
5432           (setq articles (cdr marks))
5433           (while (and articles
5434                       (or (and (consp (car articles))
5435                                (> min (cdar articles)))
5436                           (and (numberp (car articles))
5437                                (> min (car articles)))))
5438             (pop articles))
5439           (set var articles))))))))
5440
5441 (defun gnus-update-missing-marks (missing)
5442   "Go through the list of MISSING articles and remove them from the mark lists."
5443   (when missing
5444     (let (var m)
5445       ;; Go through all types.
5446       (dolist (elem gnus-article-mark-lists)
5447         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5448           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5449           (when (symbol-value var)
5450             ;; This list has articles.  So we delete all missing
5451             ;; articles from it.
5452             (setq m missing)
5453             (while m
5454               (set var (delq (pop m) (symbol-value var))))))))))
5455
5456 (defun gnus-update-marks ()
5457   "Enter the various lists of marked articles into the newsgroup info list."
5458   (let ((types gnus-article-mark-lists)
5459         (info (gnus-get-info gnus-newsgroup-name))
5460         type list newmarked symbol delta-marks)
5461     (when info
5462       ;; Add all marks lists to the list of marks lists.
5463       (while (setq type (pop types))
5464         (setq list (symbol-value
5465                     (setq symbol
5466                           (intern (format "gnus-newsgroup-%s" (car type))))))
5467
5468         (when list
5469           ;; Get rid of the entries of the articles that have the
5470           ;; default score.
5471           (when (and (eq (cdr type) 'score)
5472                      gnus-save-score
5473                      list)
5474             (let* ((arts list)
5475                    (prev (cons nil list))
5476                    (all prev))
5477               (while arts
5478                 (if (or (not (consp (car arts)))
5479                         (= (cdar arts) gnus-summary-default-score))
5480                     (setcdr prev (cdr arts))
5481                   (setq prev arts))
5482                 (setq arts (cdr arts)))
5483               (setq list (cdr all)))))
5484
5485         (when (eq (cdr type) 'seen)
5486           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5487
5488         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5489           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5490
5491         (when (and (gnus-check-backend-function
5492                     'request-set-mark gnus-newsgroup-name)
5493                    (not (gnus-article-unpropagatable-p (cdr type))))
5494           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5495                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5496                  (add (gnus-remove-from-range
5497                        (gnus-copy-sequence list) old)))
5498             (when add
5499               (push (list add 'add (list (cdr type))) delta-marks))
5500             (when del
5501               (push (list del 'del (list (cdr type))) delta-marks))))
5502
5503         (when list
5504           (push (cons (cdr type) list) newmarked)))
5505
5506       (when delta-marks
5507         (unless (gnus-check-group gnus-newsgroup-name)
5508           (error "Can't open server for %s" gnus-newsgroup-name))
5509         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5510
5511       ;; Enter these new marks into the info of the group.
5512       (if (nthcdr 3 info)
5513           (setcar (nthcdr 3 info) newmarked)
5514         ;; Add the marks lists to the end of the info.
5515         (when newmarked
5516           (setcdr (nthcdr 2 info) (list newmarked))))
5517
5518       ;; Cut off the end of the info if there's nothing else there.
5519       (let ((i 5))
5520         (while (and (> i 2)
5521                     (not (nth i info)))
5522           (when (nthcdr (decf i) info)
5523             (setcdr (nthcdr i info) nil)))))))
5524
5525 (defun gnus-set-mode-line (where)
5526   "Set the mode line of the article or summary buffers.
5527 If WHERE is `summary', the summary mode line format will be used."
5528   ;; Is this mode line one we keep updated?
5529   (when (and (memq where gnus-updated-mode-lines)
5530              (symbol-value
5531               (intern (format "gnus-%s-mode-line-format-spec" where))))
5532     (let (mode-string)
5533       (save-excursion
5534         ;; We evaluate this in the summary buffer since these
5535         ;; variables are buffer-local to that buffer.
5536         (set-buffer gnus-summary-buffer)
5537         ;; We bind all these variables that are used in the `eval' form
5538         ;; below.
5539         (let* ((mformat (symbol-value
5540                          (intern
5541                           (format "gnus-%s-mode-line-format-spec" where))))
5542                (gnus-tmp-group-name (gnus-group-decoded-name
5543                                      gnus-newsgroup-name))
5544                (gnus-tmp-article-number (or gnus-current-article 0))
5545                (gnus-tmp-unread gnus-newsgroup-unreads)
5546                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5547                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5548                (gnus-tmp-unread-and-unselected
5549                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5550                             (zerop gnus-tmp-unselected))
5551                        "")
5552                       ((zerop gnus-tmp-unselected)
5553                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5554                       (t (format "{%d(+%d) more}"
5555                                  gnus-tmp-unread-and-unticked
5556                                  gnus-tmp-unselected))))
5557                (gnus-tmp-subject
5558                 (if (and gnus-current-headers
5559                          (vectorp gnus-current-headers))
5560                     (gnus-mode-string-quote
5561                      (mail-header-subject gnus-current-headers))
5562                   ""))
5563                bufname-length max-len
5564                gnus-tmp-header);; passed as argument to any user-format-funcs
5565           (setq mode-string (eval mformat))
5566           (setq bufname-length (if (string-match "%b" mode-string)
5567                                    (- (length
5568                                        (buffer-name
5569                                         (if (eq where 'summary)
5570                                             nil
5571                                           (get-buffer gnus-article-buffer))))
5572                                       2)
5573                                  0))
5574           (setq max-len (max 4 (if gnus-mode-non-string-length
5575                                    (- (window-width)
5576                                       gnus-mode-non-string-length
5577                                       bufname-length)
5578                                  (length mode-string))))
5579           ;; We might have to chop a bit of the string off...
5580           (when (> (length mode-string) max-len)
5581             (setq mode-string
5582                   (concat (gnus-truncate-string mode-string (- max-len 3))
5583                           "...")))
5584           ;; Pad the mode string a bit.
5585           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5586       ;; Update the mode line.
5587       (setq mode-line-buffer-identification
5588             (gnus-mode-line-buffer-identification (list mode-string)))
5589       (set-buffer-modified-p t))))
5590
5591 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5592   "Go through the HEADERS list and add all Xrefs to a hash table.
5593 The resulting hash table is returned, or nil if no Xrefs were found."
5594   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5595          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5596          (xref-hashtb (gnus-make-hashtable))
5597          start group entry number xrefs header)
5598     (while headers
5599       (setq header (pop headers))
5600       (when (and (setq xrefs (mail-header-xref header))
5601                  (not (memq (setq number (mail-header-number header))
5602                             unreads)))
5603         (setq start 0)
5604         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5605           (setq start (match-end 0))
5606           (setq group (if prefix
5607                           (concat prefix (substring xrefs (match-beginning 1)
5608                                                     (match-end 1)))
5609                         (substring xrefs (match-beginning 1) (match-end 1))))
5610           (setq number
5611                 (string-to-int (substring xrefs (match-beginning 2)
5612                                           (match-end 2))))
5613           (if (setq entry (gnus-gethash group xref-hashtb))
5614               (setcdr entry (cons number (cdr entry)))
5615             (gnus-sethash group (cons number nil) xref-hashtb)))))
5616     (and start xref-hashtb)))
5617
5618 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5619   "Look through all the headers and mark the Xrefs as read."
5620   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5621         name info xref-hashtb idlist method nth4)
5622     (save-excursion
5623       (set-buffer gnus-group-buffer)
5624       (when (setq xref-hashtb
5625                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5626         (mapatoms
5627          (lambda (group)
5628            (unless (string= from-newsgroup (setq name (symbol-name group)))
5629              (setq idlist (symbol-value group))
5630              ;; Dead groups are not updated.
5631              (and (prog1
5632                       (setq info (gnus-get-info name))
5633                     (when (stringp (setq nth4 (gnus-info-method info)))
5634                       (setq nth4 (gnus-server-to-method nth4))))
5635                   ;; Only do the xrefs if the group has the same
5636                   ;; select method as the group we have just read.
5637                   (or (gnus-methods-equal-p
5638                        nth4 (gnus-find-method-for-group from-newsgroup))
5639                       virtual
5640                       (equal nth4 (setq method (gnus-find-method-for-group
5641                                                 from-newsgroup)))
5642                       (and (equal (car nth4) (car method))
5643                            (equal (nth 1 nth4) (nth 1 method))))
5644                   gnus-use-cross-reference
5645                   (or (not (eq gnus-use-cross-reference t))
5646                       virtual
5647                       ;; Only do cross-references on subscribed
5648                       ;; groups, if that is what is wanted.
5649                       (<= (gnus-info-level info) gnus-level-subscribed))
5650                   (gnus-group-make-articles-read name idlist))))
5651          xref-hashtb)))))
5652
5653 (defun gnus-compute-read-articles (group articles)
5654   (let* ((entry (gnus-group-entry group))
5655          (info (nth 2 entry))
5656          (active (gnus-active group))
5657          ninfo)
5658     (when entry
5659       ;; First peel off all invalid article numbers.
5660       (when active
5661         (let ((ids articles)
5662               id first)
5663           (while (setq id (pop ids))
5664             (when (and first (> id (cdr active)))
5665               ;; We'll end up in this situation in one particular
5666               ;; obscure situation.  If you re-scan a group and get
5667               ;; a new article that is cross-posted to a different
5668               ;; group that has not been re-scanned, you might get
5669               ;; crossposted article that has a higher number than
5670               ;; Gnus believes possible.  So we re-activate this
5671               ;; group as well.  This might mean doing the
5672               ;; crossposting thingy will *increase* the number
5673               ;; of articles in some groups.  Tsk, tsk.
5674               (setq active (or (gnus-activate-group group) active)))
5675             (when (or (> id (cdr active))
5676                       (< id (car active)))
5677               (setq articles (delq id articles))))))
5678       ;; If the read list is nil, we init it.
5679       (if (and active
5680                (null (gnus-info-read info))
5681                (> (car active) 1))
5682           (setq ninfo (cons 1 (1- (car active))))
5683         (setq ninfo (gnus-info-read info)))
5684       ;; Then we add the read articles to the range.
5685       (gnus-add-to-range
5686        ninfo (setq articles (sort articles '<))))))
5687
5688 (defun gnus-group-make-articles-read (group articles)
5689   "Update the info of GROUP to say that ARTICLES are read."
5690   (let* ((num 0)
5691          (entry (gnus-group-entry group))
5692          (info (nth 2 entry))
5693          (active (gnus-active group))
5694          range)
5695     (when entry
5696       (setq range (gnus-compute-read-articles group articles))
5697       (with-current-buffer gnus-group-buffer
5698         (gnus-undo-register
5699           `(progn
5700              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5701              (gnus-info-set-read ',info ',(gnus-info-read info))
5702              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5703              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5704              (gnus-group-update-group ,group t))))
5705       ;; Add the read articles to the range.
5706       (gnus-info-set-read info range)
5707       (gnus-request-set-mark group (list (list range 'add '(read))))
5708       ;; Then we have to re-compute how many unread
5709       ;; articles there are in this group.
5710       (when active
5711         (cond
5712          ((not range)
5713           (setq num (- (1+ (cdr active)) (car active))))
5714          ((not (listp (cdr range)))
5715           (setq num (- (cdr active) (- (1+ (cdr range))
5716                                        (car range)))))
5717          (t
5718           (while range
5719             (if (numberp (car range))
5720                 (setq num (1+ num))
5721               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5722             (setq range (cdr range)))
5723           (setq num (- (cdr active) num))))
5724         ;; Update the number of unread articles.
5725         (setcar entry num)
5726         ;; Update the group buffer.
5727         (unless (gnus-ephemeral-group-p group)
5728           (gnus-group-update-group group t))))))
5729
5730 (defvar gnus-newsgroup-none-id 0)
5731
5732 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5733   (let ((cur nntp-server-buffer)
5734         (dependencies
5735          (or dependencies
5736              (save-excursion (set-buffer gnus-summary-buffer)
5737                              gnus-newsgroup-dependencies)))
5738         headers id end ref
5739         (mail-parse-charset gnus-newsgroup-charset)
5740         (mail-parse-ignored-charsets
5741          (save-excursion (condition-case nil
5742                              (set-buffer gnus-summary-buffer)
5743                            (error))
5744                          gnus-newsgroup-ignored-charsets)))
5745     (save-excursion
5746       (set-buffer nntp-server-buffer)
5747       ;; Translate all TAB characters into SPACE characters.
5748       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5749       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5750       (ietf-drums-unfold-fws)
5751       (gnus-run-hooks 'gnus-parse-headers-hook)
5752       (let ((case-fold-search t)
5753             in-reply-to header p lines chars ctype)
5754         (goto-char (point-min))
5755         ;; Search to the beginning of the next header.  Error messages
5756         ;; do not begin with 2 or 3.
5757         (while (re-search-forward "^[23][0-9]+ " nil t)
5758           (setq id nil
5759                 ref nil)
5760           ;; This implementation of this function, with nine
5761           ;; search-forwards instead of the one re-search-forward and
5762           ;; a case (which basically was the old function) is actually
5763           ;; about twice as fast, even though it looks messier.  You
5764           ;; can't have everything, I guess.  Speed and elegance
5765           ;; doesn't always go hand in hand.
5766           (setq
5767            header
5768            (make-full-mail-header
5769             ;; Number.
5770             (prog1
5771                 (read cur)
5772               (end-of-line)
5773               (setq p (point))
5774               (narrow-to-region (point)
5775                                 (or (and (search-forward "\n.\n" nil t)
5776                                          (- (point) 2))
5777                                     (point))))
5778             ;; Subject.
5779             (progn
5780               (goto-char p)
5781               (if (search-forward "\nsubject:" nil t)
5782                   (nnheader-header-value)
5783                 "(none)"))
5784             ;; From.
5785             (progn
5786               (goto-char p)
5787               (if (search-forward "\nfrom:" nil t)
5788                   (nnheader-header-value)
5789                 "(nobody)"))
5790             ;; Date.
5791             (progn
5792               (goto-char p)
5793               (if (search-forward "\ndate:" nil t)
5794                   (nnheader-header-value) ""))
5795             ;; Message-ID.
5796             (progn
5797               (goto-char p)
5798               (setq id (if (re-search-forward
5799                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5800                            ;; We do it this way to make sure the Message-ID
5801                            ;; is (somewhat) syntactically valid.
5802                            (buffer-substring (match-beginning 1)
5803                                              (match-end 1))
5804                          ;; If there was no message-id, we just fake one
5805                          ;; to make subsequent routines simpler.
5806                          (nnheader-generate-fake-message-id))))
5807             ;; References.
5808             (progn
5809               (goto-char p)
5810               (if (search-forward "\nreferences:" nil t)
5811                   (progn
5812                     (setq end (point))
5813                     (prog1
5814                         (nnheader-header-value)
5815                       (setq ref
5816                             (buffer-substring
5817                              (progn
5818                                ;; (end-of-line)
5819                                (search-backward ">" end t)
5820                                (1+ (point)))
5821                              (progn
5822                                (search-backward "<" end t)
5823                                (point))))))
5824                 ;; Get the references from the in-reply-to header if there
5825                 ;; were no references and the in-reply-to header looks
5826                 ;; promising.
5827                 (if (and (search-forward "\nin-reply-to:" nil t)
5828                          (setq in-reply-to (nnheader-header-value))
5829                          (string-match "<[^>]+>" in-reply-to))
5830                     (let (ref2)
5831                       (setq ref (substring in-reply-to (match-beginning 0)
5832                                            (match-end 0)))
5833                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5834                         (setq ref2 (substring in-reply-to (match-beginning 0)
5835                                               (match-end 0)))
5836                         (when (> (length ref2) (length ref))
5837                           (setq ref ref2)))
5838                       ref)
5839                   (setq ref nil))))
5840             ;; Chars.
5841             (progn
5842               (goto-char p)
5843               (if (search-forward "\nchars: " nil t)
5844                   (if (numberp (setq chars (ignore-errors (read cur))))
5845                       chars -1)
5846                 -1))
5847             ;; Lines.
5848             (progn
5849               (goto-char p)
5850               (if (search-forward "\nlines: " nil t)
5851                   (if (numberp (setq lines (ignore-errors (read cur))))
5852                       lines -1)
5853                 -1))
5854             ;; Xref.
5855             (progn
5856               (goto-char p)
5857               (and (search-forward "\nxref:" nil t)
5858                    (nnheader-header-value)))
5859             ;; Extra.
5860             (when gnus-extra-headers
5861               (let ((extra gnus-extra-headers)
5862                     out)
5863                 (while extra
5864                   (goto-char p)
5865                   (when (search-forward
5866                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5867                     (push (cons (car extra) (nnheader-header-value)) out))
5868                   (pop extra))
5869                 out))))
5870           (goto-char p)
5871           (if (and (search-forward "\ncontent-type: " nil t)
5872                    (setq ctype (nnheader-header-value)))
5873               (mime-entity-set-content-type-internal
5874                header (mime-parse-Content-Type ctype)))
5875           (when (equal id ref)
5876             (setq ref nil))
5877
5878           (when gnus-alter-header-function
5879             (funcall gnus-alter-header-function header)
5880             (setq id (mail-header-id header)
5881                   ref (gnus-parent-id (mail-header-references header))))
5882
5883           (when (setq header
5884                       (gnus-dependencies-add-header
5885                        header dependencies force-new))
5886             (push header headers))
5887           (goto-char (point-max))
5888           (widen))
5889         (nreverse headers)))))
5890
5891 ;; Goes through the xover lines and returns a list of vectors
5892 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5893                                                   force-new dependencies
5894                                                   group also-fetch-heads)
5895   "Parse the news overview data in the server buffer.
5896 Return a list of headers that match SEQUENCE (see
5897 `nntp-retrieve-headers')."
5898   ;; Get the Xref when the users reads the articles since most/some
5899   ;; NNTP servers do not include Xrefs when using XOVER.
5900   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5901   (let ((mail-parse-charset gnus-newsgroup-charset)
5902         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5903         (cur nntp-server-buffer)
5904         (dependencies (or dependencies gnus-newsgroup-dependencies))
5905         (allp (cond
5906                ((eq gnus-read-all-available-headers t)
5907                 t)
5908                ((stringp gnus-read-all-available-headers)
5909                 (string-match gnus-read-all-available-headers group))
5910                (t
5911                 nil)))
5912         number headers header)
5913     (save-excursion
5914       (set-buffer nntp-server-buffer)
5915       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5916       ;; Allow the user to mangle the headers before parsing them.
5917       (gnus-run-hooks 'gnus-parse-headers-hook)
5918       (goto-char (point-min))
5919       (gnus-parse-without-error
5920         (while (and (or sequence allp)
5921                     (not (eobp)))
5922           (setq number (read cur))
5923           (when (not allp)
5924             (while (and sequence
5925                         (< (car sequence) number))
5926               (setq sequence (cdr sequence))))
5927           (when (and (or allp
5928                          (and sequence
5929                               (eq number (car sequence))))
5930                      (progn
5931                        (setq sequence (cdr sequence))
5932                        (setq header (inline
5933                                       (gnus-nov-parse-line
5934                                        number dependencies force-new)))))
5935             (push header headers))
5936           (forward-line 1)))
5937       ;; A common bug in inn is that if you have posted an article and
5938       ;; then retrieves the active file, it will answer correctly --
5939       ;; the new article is included.  However, a NOV entry for the
5940       ;; article may not have been generated yet, so this may fail.
5941       ;; We work around this problem by retrieving the last few
5942       ;; headers using HEAD.
5943       (if (or (not also-fetch-heads)
5944               (not sequence))
5945           ;; We (probably) got all the headers.
5946           (nreverse headers)
5947         (let ((gnus-nov-is-evil t))
5948           (nconc
5949            (nreverse headers)
5950            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5951              (gnus-get-newsgroup-headers))))))))
5952
5953 (defun gnus-article-get-xrefs ()
5954   "Fill in the Xref value in `gnus-current-headers', if necessary.
5955 This is meant to be called in `gnus-article-internal-prepare-hook'."
5956   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5957                                  gnus-current-headers)))
5958     (or (not gnus-use-cross-reference)
5959         (not headers)
5960         (and (mail-header-xref headers)
5961              (not (string= (mail-header-xref headers) "")))
5962         (let ((case-fold-search t)
5963               xref)
5964           (save-restriction
5965             (nnheader-narrow-to-headers)
5966             (goto-char (point-min))
5967             (when (or (and (not (eobp))
5968                            (eq (downcase (char-after)) ?x)
5969                            (looking-at "Xref:"))
5970                       (search-forward "\nXref:" nil t))
5971               (goto-char (1+ (match-end 0)))
5972               (setq xref (buffer-substring (point) (point-at-eol)))
5973               (mail-header-set-xref headers xref)))))))
5974
5975 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5976   "Find article ID and insert the summary line for that article.
5977 OLD-HEADER can either be a header or a line number to insert
5978 the subject line on."
5979   (let* ((line (and (numberp old-header) old-header))
5980          (old-header (and (vectorp old-header) old-header))
5981          (header (cond ((and old-header use-old-header)
5982                         old-header)
5983                        ((and (numberp id)
5984                              (gnus-number-to-header id))
5985                         (gnus-number-to-header id))
5986                        (t
5987                         (gnus-read-header id))))
5988          (number (and (numberp id) id))
5989          d)
5990     (when header
5991       ;; Rebuild the thread that this article is part of and go to the
5992       ;; article we have fetched.
5993       (when (and (not gnus-show-threads)
5994                  old-header)
5995         (when (and number
5996                    (setq d (gnus-data-find (mail-header-number old-header))))
5997           (goto-char (gnus-data-pos d))
5998           (gnus-data-remove
5999            number
6000            (- (point-at-bol)
6001               (prog1
6002                   (1+ (point-at-eol))
6003                 (gnus-delete-line))))))
6004       (when old-header
6005         (mail-header-set-number header (mail-header-number old-header)))
6006       (setq gnus-newsgroup-sparse
6007             (delq (setq number (mail-header-number header))
6008                   gnus-newsgroup-sparse))
6009       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6010       (push number gnus-newsgroup-limit)
6011       (gnus-rebuild-thread (mail-header-id header) line)
6012       (gnus-summary-goto-subject number nil t))
6013     (when (and (numberp number)
6014                (> number 0))
6015       ;; We have to update the boundaries even if we can't fetch the
6016       ;; article if ID is a number -- so that the next `P' or `N'
6017       ;; command will fetch the previous (or next) article even
6018       ;; if the one we tried to fetch this time has been canceled.
6019       (when (> number gnus-newsgroup-end)
6020         (setq gnus-newsgroup-end number))
6021       (when (< number gnus-newsgroup-begin)
6022         (setq gnus-newsgroup-begin number))
6023       (setq gnus-newsgroup-unselected
6024             (delq number gnus-newsgroup-unselected)))
6025     ;; Report back a success?
6026     (and header (mail-header-number header))))
6027
6028 ;;; Process/prefix in the summary buffer
6029
6030 (defun gnus-summary-work-articles (n)
6031   "Return a list of articles to be worked upon.
6032 The prefix argument, the list of process marked articles, and the
6033 current article will be taken into consideration."
6034   (save-excursion
6035     (set-buffer gnus-summary-buffer)
6036     (cond
6037      (n
6038       ;; A numerical prefix has been given.
6039       (setq n (prefix-numeric-value n))
6040       (let ((backward (< n 0))
6041             (n (abs (prefix-numeric-value n)))
6042             articles article)
6043         (save-excursion
6044           (while
6045               (and (> n 0)
6046                    (push (setq article (gnus-summary-article-number))
6047                          articles)
6048                    (if backward
6049                        (gnus-summary-find-prev nil article)
6050                      (gnus-summary-find-next nil article)))
6051             (decf n)))
6052         (nreverse articles)))
6053      ((and (gnus-region-active-p) (mark))
6054       (message "region active")
6055       ;; Work on the region between point and mark.
6056       (let ((max (max (point) (mark)))
6057             articles article)
6058         (save-excursion
6059           (goto-char (min (point) (mark)))
6060           (while
6061               (and
6062                (push (setq article (gnus-summary-article-number)) articles)
6063                (gnus-summary-find-next nil article)
6064                (< (point) max)))
6065           (nreverse articles))))
6066      (gnus-newsgroup-processable
6067       ;; There are process-marked articles present.
6068       ;; Save current state.
6069       (gnus-summary-save-process-mark)
6070       ;; Return the list.
6071       (reverse gnus-newsgroup-processable))
6072      (t
6073       ;; Just return the current article.
6074       (list (gnus-summary-article-number))))))
6075
6076 (defmacro gnus-summary-iterate (arg &rest forms)
6077   "Iterate over the process/prefixed articles and do FORMS.
6078 ARG is the interactive prefix given to the command.  FORMS will be
6079 executed with point over the summary line of the articles."
6080   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6081     `(let ((,articles (gnus-summary-work-articles ,arg)))
6082        (while ,articles
6083          (gnus-summary-goto-subject (car ,articles))
6084          ,@forms
6085          (pop ,articles)))))
6086
6087 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6088 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6089
6090 (defun gnus-summary-save-process-mark ()
6091   "Push the current set of process marked articles on the stack."
6092   (interactive)
6093   (push (copy-sequence gnus-newsgroup-processable)
6094         gnus-newsgroup-process-stack))
6095
6096 (defun gnus-summary-kill-process-mark ()
6097   "Push the current set of process marked articles on the stack and unmark."
6098   (interactive)
6099   (gnus-summary-save-process-mark)
6100   (gnus-summary-unmark-all-processable))
6101
6102 (defun gnus-summary-yank-process-mark ()
6103   "Pop the last process mark state off the stack and restore it."
6104   (interactive)
6105   (unless gnus-newsgroup-process-stack
6106     (error "Empty mark stack"))
6107   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6108
6109 (defun gnus-summary-process-mark-set (set)
6110   "Make SET into the current process marked articles."
6111   (gnus-summary-unmark-all-processable)
6112   (while set
6113     (gnus-summary-set-process-mark (pop set))))
6114
6115 ;;; Searching and stuff
6116
6117 (defun gnus-summary-search-group (&optional backward use-level)
6118   "Search for next unread newsgroup.
6119 If optional argument BACKWARD is non-nil, search backward instead."
6120   (save-excursion
6121     (set-buffer gnus-group-buffer)
6122     (when (gnus-group-search-forward
6123            backward nil (if use-level (gnus-group-group-level) nil))
6124       (gnus-group-group-name))))
6125
6126 (defun gnus-summary-best-group (&optional exclude-group)
6127   "Find the name of the best unread group.
6128 If EXCLUDE-GROUP, do not go to this group."
6129   (save-excursion
6130     (set-buffer gnus-group-buffer)
6131     (save-excursion
6132       (gnus-group-best-unread-group exclude-group))))
6133
6134 (defun gnus-summary-find-next (&optional unread article backward)
6135   (if backward
6136       (gnus-summary-find-prev unread article)
6137     (let* ((dummy (gnus-summary-article-intangible-p))
6138            (article (or article (gnus-summary-article-number)))
6139            (data (gnus-data-find-list article))
6140            result)
6141       (when (and (not dummy)
6142                  (or (not gnus-summary-check-current)
6143                      (not unread)
6144                      (not (gnus-data-unread-p (car data)))))
6145         (setq data (cdr data)))
6146       (when (setq result
6147                   (if unread
6148                       (progn
6149                         (while data
6150                           (unless (memq (gnus-data-number (car data))
6151                                         (cond
6152                                          ((eq gnus-auto-goto-ignores
6153                                               'always-undownloaded)
6154                                           gnus-newsgroup-undownloaded)
6155                                          (gnus-plugged
6156                                           nil)
6157                                          ((eq gnus-auto-goto-ignores
6158                                               'unfetched)
6159                                           gnus-newsgroup-unfetched)
6160                                          ((eq gnus-auto-goto-ignores
6161                                               'undownloaded)
6162                                           gnus-newsgroup-undownloaded)))
6163                             (when (gnus-data-unread-p (car data))
6164                               (setq result (car data)
6165                                     data nil)))
6166                           (setq data (cdr data)))
6167                         result)
6168                     (car data)))
6169         (goto-char (gnus-data-pos result))
6170         (gnus-data-number result)))))
6171
6172 (defun gnus-summary-find-prev (&optional unread article)
6173   (let* ((eobp (eobp))
6174          (article (or article (gnus-summary-article-number)))
6175          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6176          result)
6177     (when (and (not eobp)
6178                (or (not gnus-summary-check-current)
6179                    (not unread)
6180                    (not (gnus-data-unread-p (car data)))))
6181       (setq data (cdr data)))
6182     (when (setq result
6183                 (if unread
6184                     (progn
6185                       (while data
6186                         (unless (memq (gnus-data-number (car data))
6187                                       (cond
6188                                        ((eq gnus-auto-goto-ignores
6189                                             'always-undownloaded)
6190                                         gnus-newsgroup-undownloaded)
6191                                        (gnus-plugged
6192                                         nil)
6193                                        ((eq gnus-auto-goto-ignores
6194                                             'unfetched)
6195                                         gnus-newsgroup-unfetched)
6196                                        ((eq gnus-auto-goto-ignores
6197                                             'undownloaded)
6198                                         gnus-newsgroup-undownloaded)))
6199                           (when (gnus-data-unread-p (car data))
6200                             (setq result (car data)
6201                                   data nil)))
6202                         (setq data (cdr data)))
6203                       result)
6204                   (car data)))
6205       (goto-char (gnus-data-pos result))
6206       (gnus-data-number result))))
6207
6208 (defun gnus-summary-find-subject (subject &optional unread backward article)
6209   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6210          (article (or article (gnus-summary-article-number)))
6211          (articles (gnus-data-list backward))
6212          (arts (gnus-data-find-list article articles))
6213          result)
6214     (when (or (not gnus-summary-check-current)
6215               (not unread)
6216               (not (gnus-data-unread-p (car arts))))
6217       (setq arts (cdr arts)))
6218     (while arts
6219       (and (or (not unread)
6220                (gnus-data-unread-p (car arts)))
6221            (vectorp (gnus-data-header (car arts)))
6222            (gnus-subject-equal
6223             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6224            (setq result (car arts)
6225                  arts nil))
6226       (setq arts (cdr arts)))
6227     (and result
6228          (goto-char (gnus-data-pos result))
6229          (gnus-data-number result))))
6230
6231 (defun gnus-summary-search-forward (&optional unread subject backward)
6232   "Search forward for an article.
6233 If UNREAD, look for unread articles.  If SUBJECT, look for
6234 articles with that subject.  If BACKWARD, search backward instead."
6235   (cond (subject (gnus-summary-find-subject subject unread backward))
6236         (backward (gnus-summary-find-prev unread))
6237         (t (gnus-summary-find-next unread))))
6238
6239 (defun gnus-recenter (&optional n)
6240   "Center point in window and redisplay frame.
6241 Also do horizontal recentering."
6242   (interactive "P")
6243   (when (and gnus-auto-center-summary
6244              (not (eq gnus-auto-center-summary 'vertical)))
6245     (gnus-horizontal-recenter))
6246   (recenter n))
6247
6248 (defun gnus-summary-recenter ()
6249   "Center point in the summary window.
6250 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6251 displayed, no centering will be performed."
6252   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6253   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6254   (interactive)
6255   ;; The user has to want it.
6256   (when gnus-auto-center-summary
6257     (let* ((top (cond ((< (window-height) 4) 0)
6258                       ((< (window-height) 7) 1)
6259                       (t (if (numberp gnus-auto-center-summary)
6260                              gnus-auto-center-summary
6261                            2))))
6262            (height (1- (window-height)))
6263            (bottom (save-excursion (goto-char (point-max))
6264                                    (forward-line (- height))
6265                                    (point)))
6266            (window (get-buffer-window (current-buffer))))
6267       (when (get-buffer-window gnus-article-buffer)
6268         ;; Only do recentering when the article buffer is displayed,
6269         ;; Set the window start to either `bottom', which is the biggest
6270         ;; possible valid number, or the second line from the top,
6271         ;; whichever is the least.
6272         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6273           (if (> bottom top-pos)
6274               ;; Keep the second line from the top visible
6275               (set-window-start window top-pos t)
6276             ;; Try to keep the bottom line visible; if it's partially
6277             ;; obscured, either scroll one more line to make it fully
6278             ;; visible, or revert to using TOP-POS.
6279             (save-excursion
6280               (goto-char (point-max))
6281               (forward-line -1)
6282               (let ((last-line-start (point)))
6283                 (goto-char bottom)
6284                 (set-window-start window (point) t)
6285                 (when (not (pos-visible-in-window-p last-line-start window))
6286                   (forward-line 1)
6287                   (set-window-start window (min (point) top-pos) t)))))))
6288       ;; Do horizontal recentering while we're at it.
6289       (when (and (get-buffer-window (current-buffer) t)
6290                  (not (eq gnus-auto-center-summary 'vertical)))
6291         (let ((selected (selected-window)))
6292           (select-window (get-buffer-window (current-buffer) t))
6293           (gnus-summary-position-point)
6294           (gnus-horizontal-recenter)
6295           (select-window selected))))))
6296
6297 (defun gnus-summary-jump-to-group (newsgroup)
6298   "Move point to NEWSGROUP in group mode buffer."
6299   ;; Keep update point of group mode buffer if visible.
6300   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6301       (save-window-excursion
6302         ;; Take care of tree window mode.
6303         (when (get-buffer-window gnus-group-buffer)
6304           (pop-to-buffer gnus-group-buffer))
6305         (gnus-group-jump-to-group newsgroup))
6306     (save-excursion
6307       ;; Take care of tree window mode.
6308       (if (get-buffer-window gnus-group-buffer)
6309           (pop-to-buffer gnus-group-buffer)
6310         (set-buffer gnus-group-buffer))
6311       (gnus-group-jump-to-group newsgroup))))
6312
6313 ;; This function returns a list of article numbers based on the
6314 ;; difference between the ranges of read articles in this group and
6315 ;; the range of active articles.
6316 (defun gnus-list-of-unread-articles (group)
6317   (let* ((read (gnus-info-read (gnus-get-info group)))
6318          (active (or (gnus-active group) (gnus-activate-group group)))
6319          (last (cdr active))
6320          first nlast unread)
6321     ;; If none are read, then all are unread.
6322     (if (not read)
6323         (setq first (car active))
6324       ;; If the range of read articles is a single range, then the
6325       ;; first unread article is the article after the last read
6326       ;; article.  Sounds logical, doesn't it?
6327       (if (and (not (listp (cdr read)))
6328                (or (< (car read) (car active))
6329                    (progn (setq read (list read))
6330                           nil)))
6331           (setq first (max (car active) (1+ (cdr read))))
6332         ;; `read' is a list of ranges.
6333         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6334                                   (caar read)))
6335                   1)
6336           (setq first (car active)))
6337         (while read
6338           (when first
6339             (while (< first nlast)
6340               (setq unread (cons first unread)
6341                     first (1+ first))))
6342           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6343           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6344           (setq read (cdr read)))))
6345     ;; And add the last unread articles.
6346     (while (<= first last)
6347       (setq unread (cons first unread)
6348             first (1+ first)))
6349     ;; Return the list of unread articles.
6350     (delq 0 (nreverse unread))))
6351
6352 (defun gnus-list-of-read-articles (group)
6353   "Return a list of unread, unticked and non-dormant articles."
6354   (let* ((info (gnus-get-info group))
6355          (marked (gnus-info-marks info))
6356          (active (gnus-active group)))
6357     (and info active
6358          (gnus-list-range-difference
6359           (gnus-list-range-difference
6360            (gnus-sorted-complement
6361             (gnus-uncompress-range active)
6362             (gnus-list-of-unread-articles group))
6363            (cdr (assq 'dormant marked)))
6364           (cdr (assq 'tick marked))))))
6365
6366 ;; This function returns a sequence of article numbers based on the
6367 ;; difference between the ranges of read articles in this group and
6368 ;; the range of active articles.
6369 (defun gnus-sequence-of-unread-articles (group)
6370   (let* ((read (gnus-info-read (gnus-get-info group)))
6371          (active (or (gnus-active group) (gnus-activate-group group)))
6372          (last (cdr active))
6373          first nlast unread)
6374     ;; If none are read, then all are unread.
6375     (if (not read)
6376         (setq first (car active))
6377       ;; If the range of read articles is a single range, then the
6378       ;; first unread article is the article after the last read
6379       ;; article.  Sounds logical, doesn't it?
6380       (if (and (not (listp (cdr read)))
6381                (or (< (car read) (car active))
6382                    (progn (setq read (list read))
6383                           nil)))
6384           (setq first (max (car active) (1+ (cdr read))))
6385         ;; `read' is a list of ranges.
6386         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6387                                   (caar read)))
6388                   1)
6389           (setq first (car active)))
6390         (while read
6391           (when first
6392             (push (cons first nlast) unread))
6393           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6394           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6395           (setq read (cdr read)))))
6396     ;; And add the last unread articles.
6397     (cond ((< first last)
6398            (push (cons first last) unread))
6399           ((= first last)
6400            (push first unread)))
6401     ;; Return the sequence of unread articles.
6402     (delq 0 (nreverse unread))))
6403
6404 ;; Various summary commands
6405
6406 (defun gnus-summary-select-article-buffer ()
6407   "Reconfigure windows to show article buffer."
6408   (interactive)
6409   (if (not (gnus-buffer-live-p gnus-article-buffer))
6410       (error "There is no article buffer for this summary buffer")
6411     (gnus-configure-windows 'article)
6412     (select-window (get-buffer-window gnus-article-buffer))))
6413
6414 (defun gnus-summary-universal-argument (arg)
6415   "Perform any operation on all articles that are process/prefixed."
6416   (interactive "P")
6417   (let ((articles (gnus-summary-work-articles arg))
6418         func article)
6419     (if (eq
6420          (setq
6421           func
6422           (key-binding
6423            (read-key-sequence
6424             (substitute-command-keys
6425              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6426          'undefined)
6427         (gnus-error 1 "Undefined key")
6428       (save-excursion
6429         (while articles
6430           (gnus-summary-goto-subject (setq article (pop articles)))
6431           (let (gnus-newsgroup-processable)
6432             (command-execute func))
6433           (gnus-summary-remove-process-mark article)))))
6434   (gnus-summary-position-point))
6435
6436 (defun gnus-summary-toggle-truncation (&optional arg)
6437   "Toggle truncation of summary lines.
6438 With ARG, turn line truncation on if ARG is positive."
6439   (interactive "P")
6440   (setq truncate-lines
6441         (if (null arg) (not truncate-lines)
6442           (> (prefix-numeric-value arg) 0)))
6443   (redraw-display))
6444
6445 (defun gnus-summary-find-for-reselect ()
6446   "Return the number of an article to stay on across a reselect.
6447 The current article is considered, then following articles, then previous
6448 articles.  An article is sought which is not cancelled and isn't a temporary
6449 insertion from another group.  If there's no such then return a dummy 0."
6450   (let (found)
6451     (dolist (rev '(nil t))
6452       (unless found      ; don't demand the reverse list if we don't need it
6453         (let ((data (gnus-data-find-list
6454                      (gnus-summary-article-number) (gnus-data-list rev))))
6455           (while (and data (not found))
6456             (if (and (< 0 (gnus-data-number (car data)))
6457                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6458                 (setq found (gnus-data-number (car data))))
6459             (setq data (cdr data))))))
6460     (or found 0)))
6461
6462 (defun gnus-summary-reselect-current-group (&optional all rescan)
6463   "Exit and then reselect the current newsgroup.
6464 The prefix argument ALL means to select all articles."
6465   (interactive "P")
6466   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6467     (error "Ephemeral groups can't be reselected"))
6468   (let ((current-subject (gnus-summary-find-for-reselect))
6469         (group gnus-newsgroup-name))
6470     (setq gnus-newsgroup-begin nil)
6471     (gnus-summary-exit nil 'leave-hidden)
6472     ;; We have to adjust the point of group mode buffer because
6473     ;; point was moved to the next unread newsgroup by exiting.
6474     (gnus-summary-jump-to-group group)
6475     (when rescan
6476       (save-excursion
6477         (save-window-excursion
6478           ;; Don't show group contents.
6479           (set-window-start (selected-window) (point-max))
6480           (gnus-group-get-new-news-this-group 1))))
6481     (gnus-group-read-group all t)
6482     (gnus-summary-goto-subject current-subject nil t)))
6483
6484 (defun gnus-summary-rescan-group (&optional all)
6485   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6486   (interactive "P")
6487   (gnus-summary-reselect-current-group all t))
6488
6489 (defun gnus-summary-update-info (&optional non-destructive)
6490   (save-excursion
6491     (let ((group gnus-newsgroup-name))
6492       (when group
6493         (when gnus-newsgroup-kill-headers
6494           (setq gnus-newsgroup-killed
6495                 (gnus-compress-sequence
6496                  (gnus-sorted-union
6497                   (gnus-list-range-intersection
6498                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6499                   gnus-newsgroup-unreads)
6500                  t)))
6501         (unless (listp (cdr gnus-newsgroup-killed))
6502           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6503         (let ((headers gnus-newsgroup-headers))
6504           ;; Set the new ranges of read articles.
6505           (save-excursion
6506             (set-buffer gnus-group-buffer)
6507             (gnus-undo-force-boundary))
6508           (gnus-update-read-articles
6509            group (gnus-sorted-union
6510                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6511           ;; Set the current article marks.
6512           (let ((gnus-newsgroup-scored
6513                  (if (and (not gnus-save-score)
6514                           (not non-destructive))
6515                      nil
6516                    gnus-newsgroup-scored)))
6517             (save-excursion
6518               (gnus-update-marks)))
6519           ;; Do the cross-ref thing.
6520           (when gnus-use-cross-reference
6521             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6522           ;; Do not switch windows but change the buffer to work.
6523           (set-buffer gnus-group-buffer)
6524           (unless (gnus-ephemeral-group-p group)
6525             (gnus-group-update-group group)))))))
6526
6527 (defun gnus-summary-save-newsrc (&optional force)
6528   "Save the current number of read/marked articles in the dribble buffer.
6529 The dribble buffer will then be saved.
6530 If FORCE (the prefix), also save the .newsrc file(s)."
6531   (interactive "P")
6532   (gnus-summary-update-info t)
6533   (if force
6534       (gnus-save-newsrc-file)
6535     (gnus-dribble-save)))
6536
6537 (defun gnus-summary-exit (&optional temporary leave-hidden)
6538   "Exit reading current newsgroup, and then return to group selection mode.
6539 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6540   (interactive)
6541   (gnus-set-global-variables)
6542   (gnus-kill-save-kill-buffer)
6543   (gnus-async-halt-prefetch)
6544   (let* ((group gnus-newsgroup-name)
6545          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6546          (gnus-group-is-exiting-p t)
6547          (mode major-mode)
6548          (group-point nil)
6549          (buf (current-buffer)))
6550     (unless quit-config
6551       ;; Do adaptive scoring, and possibly save score files.
6552       (when gnus-newsgroup-adaptive
6553         (gnus-score-adaptive))
6554       (when gnus-use-scoring
6555         (gnus-score-save)))
6556     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6557     ;; If we have several article buffers, we kill them at exit.
6558     (unless gnus-single-article-buffer
6559       (gnus-kill-buffer gnus-original-article-buffer)
6560       (setq gnus-article-current nil))
6561     (when gnus-use-cache
6562       (gnus-cache-possibly-remove-articles)
6563       (gnus-cache-save-buffers))
6564     (gnus-async-prefetch-remove-group group)
6565     (when gnus-suppress-duplicates
6566       (gnus-dup-enter-articles))
6567     (when gnus-use-trees
6568       (gnus-tree-close group))
6569     (when gnus-use-cache
6570       (gnus-cache-write-active))
6571     ;; Remove entries for this group.
6572     (nnmail-purge-split-history (gnus-group-real-name group))
6573     ;; Make all changes in this group permanent.
6574     (unless quit-config
6575       (gnus-run-hooks 'gnus-exit-group-hook)
6576       (gnus-summary-update-info))
6577     (gnus-close-group group)
6578     ;; Make sure where we were, and go to next newsgroup.
6579     (set-buffer gnus-group-buffer)
6580     (unless quit-config
6581       (gnus-group-jump-to-group group))
6582     (gnus-run-hooks 'gnus-summary-exit-hook)
6583     (unless (or quit-config
6584                 ;; If this group has disappeared from the summary
6585                 ;; buffer, don't skip forwards.
6586                 (not (string= group (gnus-group-group-name))))
6587       (gnus-group-next-unread-group 1))
6588     (setq group-point (point))
6589     (if temporary
6590         nil                             ;Nothing to do.
6591       ;; If we have several article buffers, we kill them at exit.
6592       (unless gnus-single-article-buffer
6593         (gnus-kill-buffer gnus-article-buffer)
6594         (gnus-kill-buffer gnus-original-article-buffer)
6595         (setq gnus-article-current nil))
6596       (set-buffer buf)
6597       (if (not gnus-kill-summary-on-exit)
6598           (progn
6599             (gnus-deaden-summary)
6600             (setq mode nil))
6601         ;; We set all buffer-local variables to nil.  It is unclear why
6602         ;; this is needed, but if we don't, buffer-local variables are
6603         ;; not garbage-collected, it seems.  This would the lead to en
6604         ;; ever-growing Emacs.
6605         (gnus-summary-clear-local-variables)
6606         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6607           (gnus-summary-clear-local-variables))
6608         (when (get-buffer gnus-article-buffer)
6609           (bury-buffer gnus-article-buffer))
6610         ;; We clear the global counterparts of the buffer-local
6611         ;; variables as well, just to be on the safe side.
6612         (set-buffer gnus-group-buffer)
6613         (gnus-summary-clear-local-variables)
6614         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6615           (gnus-summary-clear-local-variables))
6616         ;; Return to group mode buffer.
6617         (when (eq mode 'gnus-summary-mode)
6618           (gnus-kill-buffer buf)))
6619       (setq gnus-current-select-method gnus-select-method)
6620       (if leave-hidden
6621           (set-buffer gnus-group-buffer)
6622         (pop-to-buffer gnus-group-buffer))
6623       (if (not quit-config)
6624           (progn
6625             (goto-char group-point)
6626             (unless leave-hidden
6627               (gnus-configure-windows 'group 'force))
6628             (unless (pos-visible-in-window-p)
6629               (forward-line (/ (static-if (featurep 'xemacs)
6630                                    (window-displayed-height)
6631                                  (1- (window-height)))
6632                                -2))
6633               (set-window-start (selected-window) (point))
6634               (goto-char group-point)))
6635         (gnus-handle-ephemeral-exit quit-config))
6636       ;; Clear the current group name.
6637       (unless quit-config
6638         (setq gnus-newsgroup-name nil)))))
6639
6640 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6641 (defun gnus-summary-exit-no-update (&optional no-questions)
6642   "Quit reading current newsgroup without updating read article info."
6643   (interactive)
6644   (let* ((group gnus-newsgroup-name)
6645          (gnus-group-is-exiting-p t)
6646          (gnus-group-is-exiting-without-update-p t)
6647          (quit-config (gnus-group-quit-config group)))
6648     (when (or no-questions
6649               gnus-expert-user
6650               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6651       (gnus-async-halt-prefetch)
6652       (run-hooks 'gnus-summary-prepare-exit-hook)
6653       ;; If we have several article buffers, we kill them at exit.
6654       (unless gnus-single-article-buffer
6655         (gnus-kill-buffer gnus-article-buffer)
6656         (gnus-kill-buffer gnus-original-article-buffer)
6657         (setq gnus-article-current nil))
6658       (if (not gnus-kill-summary-on-exit)
6659           (gnus-deaden-summary)
6660         (gnus-close-group group)
6661         (gnus-summary-clear-local-variables)
6662         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6663           (gnus-summary-clear-local-variables))
6664         (set-buffer gnus-group-buffer)
6665         (gnus-summary-clear-local-variables)
6666         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6667           (gnus-summary-clear-local-variables))
6668         (gnus-kill-buffer gnus-summary-buffer))
6669       (unless gnus-single-article-buffer
6670         (setq gnus-article-current nil))
6671       (when gnus-use-trees
6672         (gnus-tree-close group))
6673       (gnus-async-prefetch-remove-group group)
6674       (when (get-buffer gnus-article-buffer)
6675         (bury-buffer gnus-article-buffer))
6676       ;; Return to the group buffer.
6677       (gnus-configure-windows 'group 'force)
6678       ;; Clear the current group name.
6679       (setq gnus-newsgroup-name nil)
6680       (unless (gnus-ephemeral-group-p group)
6681         (gnus-group-update-group group))
6682       (when (equal (gnus-group-group-name) group)
6683         (gnus-group-next-unread-group 1))
6684       (when quit-config
6685         (gnus-handle-ephemeral-exit quit-config)))))
6686
6687 (defun gnus-handle-ephemeral-exit (quit-config)
6688   "Handle movement when leaving an ephemeral group.
6689 The state which existed when entering the ephemeral is reset."
6690   (if (not (buffer-name (car quit-config)))
6691       (gnus-configure-windows 'group 'force)
6692     (set-buffer (car quit-config))
6693     (cond ((eq major-mode 'gnus-summary-mode)
6694            (gnus-set-global-variables))
6695           ((eq major-mode 'gnus-article-mode)
6696            (save-excursion
6697              ;; The `gnus-summary-buffer' variable may point
6698              ;; to the old summary buffer when using a single
6699              ;; article buffer.
6700              (unless (gnus-buffer-live-p gnus-summary-buffer)
6701                (set-buffer gnus-group-buffer))
6702              (set-buffer gnus-summary-buffer)
6703              (gnus-set-global-variables))))
6704     (if (or (eq (cdr quit-config) 'article)
6705             (eq (cdr quit-config) 'pick))
6706         (progn
6707           ;; The current article may be from the ephemeral group
6708           ;; thus it is best that we reload this article
6709           ;;
6710           ;; If we're exiting from a large digest, this can be
6711           ;; extremely slow.  So, it's better not to reload it. -- jh.
6712           ;;(gnus-summary-show-article)
6713           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6714               (gnus-configure-windows 'pick 'force)
6715             (gnus-configure-windows (cdr quit-config) 'force)))
6716       (gnus-configure-windows (cdr quit-config) 'force))
6717     (when (eq major-mode 'gnus-summary-mode)
6718       (gnus-summary-next-subject 1 nil t)
6719       (gnus-summary-recenter)
6720       (gnus-summary-position-point))))
6721
6722 (defun gnus-summary-preview-mime-message ()
6723   "MIME decode and play this message."
6724   (interactive)
6725   (let ((gnus-break-pages nil)
6726         (gnus-show-mime t))
6727     (gnus-summary-select-article gnus-show-all-headers t))
6728   (let ((w (get-buffer-window gnus-article-buffer)))
6729     (when w
6730       (select-window (get-buffer-window gnus-article-buffer)))))
6731
6732 ;;; Dead summaries.
6733
6734 (defvar gnus-dead-summary-mode-map nil)
6735
6736 (unless gnus-dead-summary-mode-map
6737   (setq gnus-dead-summary-mode-map (make-keymap))
6738   (suppress-keymap gnus-dead-summary-mode-map)
6739   (substitute-key-definition
6740    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6741   (dolist (key '("\C-d" "\r" "\177" [delete]))
6742     (define-key gnus-dead-summary-mode-map
6743       key 'gnus-summary-wake-up-the-dead))
6744   (dolist (key '("q" "Q"))
6745     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6746
6747 (defvar gnus-dead-summary-mode nil
6748   "Minor mode for Gnus summary buffers.")
6749
6750 (defun gnus-dead-summary-mode (&optional arg)
6751   "Minor mode for Gnus summary buffers."
6752   (interactive "P")
6753   (when (eq major-mode 'gnus-summary-mode)
6754     (make-local-variable 'gnus-dead-summary-mode)
6755     (setq gnus-dead-summary-mode
6756           (if (null arg) (not gnus-dead-summary-mode)
6757             (> (prefix-numeric-value arg) 0)))
6758     (when gnus-dead-summary-mode
6759       (add-minor-mode
6760        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6761
6762 (defun gnus-deaden-summary ()
6763   "Make the current summary buffer into a dead summary buffer."
6764   ;; Kill any previous dead summary buffer.
6765   (when (and gnus-dead-summary
6766              (buffer-name gnus-dead-summary))
6767     (save-excursion
6768       (set-buffer gnus-dead-summary)
6769       (when gnus-dead-summary-mode
6770         (kill-buffer (current-buffer)))))
6771   ;; Make this the current dead summary.
6772   (setq gnus-dead-summary (current-buffer))
6773   (gnus-dead-summary-mode 1)
6774   (let ((name (buffer-name)))
6775     (when (string-match "Summary" name)
6776       (rename-buffer
6777        (concat (substring name 0 (match-beginning 0)) "Dead "
6778                (substring name (match-beginning 0)))
6779        t)
6780       (bury-buffer))))
6781
6782 (defun gnus-kill-or-deaden-summary (buffer)
6783   "Kill or deaden the summary BUFFER."
6784   (save-excursion
6785     (when (and (buffer-name buffer)
6786                (not gnus-single-article-buffer))
6787       (save-excursion
6788         (set-buffer buffer)
6789         (gnus-kill-buffer gnus-article-buffer)
6790         (gnus-kill-buffer gnus-original-article-buffer)))
6791     (cond
6792      ;; Kill the buffer.
6793      (gnus-kill-summary-on-exit
6794       (when (and gnus-use-trees
6795                  (gnus-buffer-exists-p buffer))
6796         (save-excursion
6797           (set-buffer buffer)
6798           (gnus-tree-close gnus-newsgroup-name)))
6799       (gnus-kill-buffer buffer))
6800      ;; Deaden the buffer.
6801      ((gnus-buffer-exists-p buffer)
6802       (save-excursion
6803         (set-buffer buffer)
6804         (gnus-deaden-summary))))))
6805
6806 (defun gnus-summary-wake-up-the-dead (&rest args)
6807   "Wake up the dead summary buffer."
6808   (interactive)
6809   (gnus-dead-summary-mode -1)
6810   (let ((name (buffer-name)))
6811     (when (string-match "Dead " name)
6812       (rename-buffer
6813        (concat (substring name 0 (match-beginning 0))
6814                (substring name (match-end 0)))
6815        t)))
6816   (gnus-message 3 "This dead summary is now alive again"))
6817
6818 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6819 (defun gnus-summary-fetch-faq (&optional faq-dir)
6820   "Fetch the FAQ for the current group.
6821 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6822 in."
6823   (interactive
6824    (list
6825     (when current-prefix-arg
6826       (completing-read
6827        "FAQ dir: " (and (listp gnus-group-faq-directory)
6828                         (mapcar (lambda (file) (list file))
6829                                 gnus-group-faq-directory))))))
6830   (let (gnus-faq-buffer)
6831     (when (setq gnus-faq-buffer
6832                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6833       (gnus-configure-windows 'summary-faq))))
6834
6835 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6836 (defun gnus-summary-describe-group (&optional force)
6837   "Describe the current newsgroup."
6838   (interactive "P")
6839   (gnus-group-describe-group force gnus-newsgroup-name))
6840
6841 (defun gnus-summary-describe-briefly ()
6842   "Describe summary mode commands briefly."
6843   (interactive)
6844   (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")))
6845
6846 ;; Walking around group mode buffer from summary mode.
6847
6848 (defun gnus-summary-next-group (&optional no-article target-group backward)
6849   "Exit current newsgroup and then select next unread newsgroup.
6850 If prefix argument NO-ARTICLE is non-nil, no article is selected
6851 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6852 previous group instead."
6853   (interactive "P")
6854   ;; Stop pre-fetching.
6855   (gnus-async-halt-prefetch)
6856   (let ((current-group gnus-newsgroup-name)
6857         (current-buffer (current-buffer))
6858         entered)
6859     ;; First we semi-exit this group to update Xrefs and all variables.
6860     ;; We can't do a real exit, because the window conf must remain
6861     ;; the same in case the user is prompted for info, and we don't
6862     ;; want the window conf to change before that...
6863     (gnus-summary-exit t)
6864     (while (not entered)
6865       ;; Then we find what group we are supposed to enter.
6866       (set-buffer gnus-group-buffer)
6867       (gnus-group-jump-to-group current-group)
6868       (setq target-group
6869             (or target-group
6870                 (if (eq gnus-keep-same-level 'best)
6871                     (gnus-summary-best-group gnus-newsgroup-name)
6872                   (gnus-summary-search-group backward gnus-keep-same-level))))
6873       (if (not target-group)
6874           ;; There are no further groups, so we return to the group
6875           ;; buffer.
6876           (progn
6877             (gnus-message 5 "Returning to the group buffer")
6878             (setq entered t)
6879             (when (gnus-buffer-live-p current-buffer)
6880               (set-buffer current-buffer)
6881               (gnus-summary-exit))
6882             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6883         ;; We try to enter the target group.
6884         (gnus-group-jump-to-group target-group)
6885         (let ((unreads (gnus-group-group-unread)))
6886           (if (and (or (eq t unreads)
6887                        (and unreads (not (zerop unreads))))
6888                    (gnus-summary-read-group
6889                     target-group nil no-article
6890                     (and (buffer-name current-buffer) current-buffer)
6891                     nil backward))
6892               (setq entered t)
6893             (setq current-group target-group
6894                   target-group nil)))))))
6895
6896 (defun gnus-summary-prev-group (&optional no-article)
6897   "Exit current newsgroup and then select previous unread newsgroup.
6898 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6899   (interactive "P")
6900   (gnus-summary-next-group no-article nil t))
6901
6902 ;; Walking around summary lines.
6903
6904 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6905   "Go to the first subject satisfying any non-nil constraint.
6906 If UNREAD is non-nil, the article should be unread.
6907 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6908 If UNSEEN is non-nil, the article should be unseen.
6909 Returns the article selected or nil if there are no matching articles."
6910   (interactive "P")
6911   (cond
6912    ;; Empty summary.
6913    ((null gnus-newsgroup-data)
6914     (gnus-message 3 "No articles in the group")
6915     nil)
6916    ;; Pick the first article.
6917    ((not (or unread undownloaded unseen))
6918     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6919     (gnus-data-number (car gnus-newsgroup-data)))
6920    ;; Find the first unread article.
6921    (t
6922     (let ((data gnus-newsgroup-data))
6923       (while (and data
6924                   (let ((num (gnus-data-number (car data))))
6925                     (or (memq num gnus-newsgroup-unfetched)
6926                         (not (or (and unread
6927                                       (memq num gnus-newsgroup-unreads))
6928                                  (and undownloaded
6929                                       (memq num gnus-newsgroup-undownloaded))
6930                                  (and unseen
6931                                       (memq num gnus-newsgroup-unseen)))))))
6932         (setq data (cdr data)))
6933       (prog1
6934           (if data
6935               (progn
6936                 (goto-char (gnus-data-pos (car data)))
6937                 (gnus-data-number (car data)))
6938             (gnus-message 3 "No more%s articles"
6939                           (let* ((r (when unread " unread"))
6940                                  (d (when undownloaded " undownloaded"))
6941                                  (s (when unseen " unseen"))
6942                                  (l (delq nil (list r d s))))
6943                             (cond ((= 3 (length l))
6944                                    (concat r "," d ", or" s))
6945                                   ((= 2 (length l))
6946                                    (concat (car l) ", or" (cadr l)))
6947                                   ((= 1 (length l))
6948                                    (car l))
6949                                   (t
6950                                    ""))))
6951             nil
6952             )
6953         (gnus-summary-position-point))))))
6954
6955 (defun gnus-summary-next-subject (n &optional unread dont-display)
6956   "Go to next N'th summary line.
6957 If N is negative, go to the previous N'th subject line.
6958 If UNREAD is non-nil, only unread articles are selected.
6959 The difference between N and the actual number of steps taken is
6960 returned."
6961   (interactive "p")
6962   (let ((backward (< n 0))
6963         (n (abs n)))
6964     (while (and (> n 0)
6965                 (if backward
6966                     (gnus-summary-find-prev unread)
6967                   (gnus-summary-find-next unread)))
6968       (unless (zerop (setq n (1- n)))
6969         (gnus-summary-show-thread)))
6970     (when (/= 0 n)
6971       (gnus-message 7 "No more%s articles"
6972                     (if unread " unread" "")))
6973     (unless dont-display
6974       (gnus-summary-recenter)
6975       (gnus-summary-position-point))
6976     n))
6977
6978 (defun gnus-summary-next-unread-subject (n)
6979   "Go to next N'th unread summary line."
6980   (interactive "p")
6981   (gnus-summary-next-subject n t))
6982
6983 (defun gnus-summary-prev-subject (n &optional unread)
6984   "Go to previous N'th summary line.
6985 If optional argument UNREAD is non-nil, only unread article is selected."
6986   (interactive "p")
6987   (gnus-summary-next-subject (- n) unread))
6988
6989 (defun gnus-summary-prev-unread-subject (n)
6990   "Go to previous N'th unread summary line."
6991   (interactive "p")
6992   (gnus-summary-next-subject (- n) t))
6993
6994 (defun gnus-summary-goto-subjects (articles)
6995   "Insert the subject header for ARTICLES in the current buffer."
6996   (save-excursion
6997     (dolist (article articles)
6998       (gnus-summary-goto-subject article t)))
6999   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7000   (gnus-summary-position-point))
7001  
7002 (defun gnus-summary-goto-subject (article &optional force silent)
7003   "Go the subject line of ARTICLE.
7004 If FORCE, also allow jumping to articles not currently shown."
7005   (interactive "nArticle number: ")
7006   (unless (numberp article)
7007     (error "Article %s is not a number" article))
7008   (let ((b (point))
7009         (data (gnus-data-find article)))
7010     ;; We read in the article if we have to.
7011     (and (not data)
7012          force
7013          (gnus-summary-insert-subject
7014           article
7015           (if (or (numberp force) (vectorp force)) force)
7016           t)
7017          (setq data (gnus-data-find article)))
7018     (goto-char b)
7019     (if (not data)
7020         (progn
7021           (unless silent
7022             (gnus-message 3 "Can't find article %d" article))
7023           nil)
7024       (let ((pt (gnus-data-pos data)))
7025         (goto-char pt)
7026         (gnus-summary-set-article-display-arrow pt))
7027       (gnus-summary-position-point)
7028       article)))
7029
7030 ;; Walking around summary lines with displaying articles.
7031
7032 (defun gnus-summary-expand-window (&optional arg)
7033   "Make the summary buffer take up the entire Emacs frame.
7034 Given a prefix, will force an `article' buffer configuration."
7035   (interactive "P")
7036   (if arg
7037       (gnus-configure-windows 'article 'force)
7038     (gnus-configure-windows 'summary 'force)))
7039
7040 (defun gnus-summary-display-article (article &optional all-header)
7041   "Display ARTICLE in article buffer."
7042   (when (gnus-buffer-live-p gnus-article-buffer)
7043     (with-current-buffer gnus-article-buffer
7044       (set-buffer-multibyte t)))
7045   (gnus-set-global-variables)
7046   (when (gnus-buffer-live-p gnus-article-buffer)
7047     (with-current-buffer gnus-article-buffer
7048       (setq gnus-article-charset gnus-newsgroup-charset)
7049       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7050       (set-buffer-multibyte t)))
7051   (if (null article)
7052       nil
7053     (prog1
7054         (if gnus-summary-display-article-function
7055             (funcall gnus-summary-display-article-function article all-header)
7056           (gnus-article-prepare article all-header))
7057       (with-current-buffer gnus-article-buffer
7058         (set (make-local-variable 'gnus-summary-search-article-matched-data)
7059              nil))
7060       (gnus-run-hooks 'gnus-select-article-hook)
7061       (when (and gnus-current-article
7062                  (not (zerop gnus-current-article)))
7063         (gnus-summary-goto-subject gnus-current-article))
7064       (gnus-summary-recenter)
7065       (when (and gnus-use-trees gnus-show-threads)
7066         (gnus-possibly-generate-tree article)
7067         (gnus-highlight-selected-tree article))
7068       ;; Successfully display article.
7069       (gnus-article-set-window-start
7070        (cdr (assq article gnus-newsgroup-bookmarks))))))
7071
7072 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7073   "Select the current article.
7074 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7075 non-nil, the article will be re-fetched even if it already present in
7076 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7077 be displayed."
7078   ;; Make sure we are in the summary buffer to work around bbdb bug.
7079   (unless (eq major-mode 'gnus-summary-mode)
7080     (set-buffer gnus-summary-buffer))
7081   (let ((article (or article (gnus-summary-article-number)))
7082         (all-headers (not (not all-headers))) ;Must be T or NIL.
7083         gnus-summary-display-article-function)
7084     (and (not pseudo)
7085          (gnus-summary-article-pseudo-p article)
7086          (error "This is a pseudo-article"))
7087     (save-excursion
7088       (set-buffer gnus-summary-buffer)
7089       (if (or (and gnus-single-article-buffer
7090                    (or (null gnus-current-article)
7091                        (null gnus-article-current)
7092                        (null (get-buffer gnus-article-buffer))
7093                        (not (eq article (cdr gnus-article-current)))
7094                        (not (equal (car gnus-article-current)
7095                                    gnus-newsgroup-name))))
7096               (and (not gnus-single-article-buffer)
7097                    (or (null gnus-current-article)
7098                        (not (eq gnus-current-article article))))
7099               force)
7100           ;; The requested article is different from the current article.
7101           (progn
7102             (gnus-summary-display-article article all-headers)
7103             (gnus-article-set-window-start
7104              (cdr (assq article gnus-newsgroup-bookmarks)))
7105             article)
7106         'old))))
7107
7108 (defun gnus-summary-force-verify-and-decrypt ()
7109   "Display buttons for signed/encrypted parts and verify/decrypt them."
7110   (interactive)
7111   (let ((mm-verify-option 'known)
7112         (mm-decrypt-option 'known)
7113         (gnus-article-emulate-mime t)
7114         (gnus-buttonized-mime-types (append (list "multipart/signed"
7115                                                   "multipart/encrypted")
7116                                             gnus-buttonized-mime-types)))
7117     (gnus-summary-select-article nil 'force)))
7118
7119 (defun gnus-summary-set-current-mark (&optional current-mark)
7120   "Obsolete function."
7121   nil)
7122
7123 (defun gnus-summary-next-article (&optional unread subject backward push)
7124   "Select the next article.
7125 If UNREAD, only unread articles are selected.
7126 If SUBJECT, only articles with SUBJECT are selected.
7127 If BACKWARD, the previous article is selected instead of the next."
7128   (interactive "P")
7129   (cond
7130    ;; Is there such an article?
7131    ((and (gnus-summary-search-forward unread subject backward)
7132          (or (gnus-summary-display-article (gnus-summary-article-number))
7133              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7134     (gnus-summary-position-point))
7135    ;; If not, we try the first unread, if that is wanted.
7136    ((and subject
7137          gnus-auto-select-same
7138          (gnus-summary-first-unread-article))
7139     (gnus-summary-position-point)
7140     (gnus-message 6 "Wrapped"))
7141    ;; Try to get next/previous article not displayed in this group.
7142    ((and gnus-auto-extend-newsgroup
7143          (not unread) (not subject))
7144     (gnus-summary-goto-article
7145      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7146      nil (count-lines (point-min) (point))))
7147    ;; Go to next/previous group.
7148    (t
7149     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7150       (gnus-summary-jump-to-group gnus-newsgroup-name))
7151     (let ((cmd last-command-char)
7152           (point
7153            (save-excursion
7154              (set-buffer gnus-group-buffer)
7155              (point)))
7156           (group
7157            (if (eq gnus-keep-same-level 'best)
7158                (gnus-summary-best-group gnus-newsgroup-name)
7159              (gnus-summary-search-group backward gnus-keep-same-level))))
7160       ;; For some reason, the group window gets selected.  We change
7161       ;; it back.
7162       (select-window (get-buffer-window (current-buffer)))
7163       ;; Select next unread newsgroup automagically.
7164       (cond
7165        ((or (not gnus-auto-select-next)
7166             (not cmd))
7167         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7168        ((or (eq gnus-auto-select-next 'quietly)
7169             (and (eq gnus-auto-select-next 'slightly-quietly)
7170                  push)
7171             (and (eq gnus-auto-select-next 'almost-quietly)
7172                  (gnus-summary-last-article-p)))
7173         ;; Select quietly.
7174         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7175             (gnus-summary-exit)
7176           (gnus-message 7 "No more%s articles (%s)..."
7177                         (if unread " unread" "")
7178                         (if group (concat "selecting " group)
7179                           "exiting"))
7180           (gnus-summary-next-group nil group backward)))
7181        (t
7182         (when (gnus-key-press-event-p last-input-event)
7183           (gnus-summary-walk-group-buffer
7184            gnus-newsgroup-name cmd unread backward point))))))))
7185
7186 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7187   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7188                       (?\C-p (gnus-group-prev-unread-group 1))))
7189         (cursor-in-echo-area t)
7190         keve key group ended prompt)
7191     (save-excursion
7192       (set-buffer gnus-group-buffer)
7193       (goto-char start)
7194       (setq group
7195             (if (eq gnus-keep-same-level 'best)
7196                 (gnus-summary-best-group gnus-newsgroup-name)
7197               (gnus-summary-search-group backward gnus-keep-same-level))))
7198     (while (not ended)
7199       (setq prompt
7200             (format
7201              "No more%s articles%s " (if unread " unread" "")
7202              (if (and group
7203                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7204                  (format " (Type %s for %s [%s])"
7205                          (single-key-description cmd) group
7206                          (gnus-group-unread group))
7207                (format " (Type %s to exit %s)"
7208                        (single-key-description cmd)
7209                        gnus-newsgroup-name))))
7210       ;; Confirm auto selection.
7211       (setq key (car (setq keve (gnus-read-event-char prompt)))
7212             ended t)
7213       (cond
7214        ((assq key keystrokes)
7215         (let ((obuf (current-buffer)))
7216           (switch-to-buffer gnus-group-buffer)
7217           (when group
7218             (gnus-group-jump-to-group group))
7219           (eval (cadr (assq key keystrokes)))
7220           (setq group (gnus-group-group-name))
7221           (switch-to-buffer obuf))
7222         (setq ended nil))
7223        ((equal key cmd)
7224         (if (or (not group)
7225                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7226             (gnus-summary-exit)
7227           (gnus-summary-next-group nil group backward)))
7228        (t
7229         (push (cdr keve) unread-command-events))))))
7230
7231 (defun gnus-summary-next-unread-article ()
7232   "Select unread article after current one."
7233   (interactive)
7234   (gnus-summary-next-article
7235    (or (not (eq gnus-summary-goto-unread 'never))
7236        (gnus-summary-last-article-p (gnus-summary-article-number)))
7237    (and gnus-auto-select-same
7238         (gnus-summary-article-subject))))
7239
7240 (defun gnus-summary-prev-article (&optional unread subject)
7241   "Select the article after the current one.
7242 If UNREAD is non-nil, only unread articles are selected."
7243   (interactive "P")
7244   (gnus-summary-next-article unread subject t))
7245
7246 (defun gnus-summary-prev-unread-article ()
7247   "Select unread article before current one."
7248   (interactive)
7249   (gnus-summary-prev-article
7250    (or (not (eq gnus-summary-goto-unread 'never))
7251        (gnus-summary-first-article-p (gnus-summary-article-number)))
7252    (and gnus-auto-select-same
7253         (gnus-summary-article-subject))))
7254
7255 (defun gnus-summary-next-page (&optional lines circular stop)
7256   "Show next page of the selected article.
7257 If at the end of the current article, select the next article.
7258 LINES says how many lines should be scrolled up.
7259
7260 If CIRCULAR is non-nil, go to the start of the article instead of
7261 selecting the next article when reaching the end of the current
7262 article.
7263
7264 If STOP is non-nil, just stop when reaching the end of the message.
7265
7266 Also see the variable `gnus-article-skip-boring'."
7267   (interactive "P")
7268   (setq gnus-summary-buffer (current-buffer))
7269   (gnus-set-global-variables)
7270   (let ((article (gnus-summary-article-number))
7271         (article-window (get-buffer-window gnus-article-buffer t))
7272         endp)
7273     ;; If the buffer is empty, we have no article.
7274     (unless article
7275       (error "No article to select"))
7276     (gnus-configure-windows 'article)
7277     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7278         (if (and (eq gnus-summary-goto-unread 'never)
7279                  (not (gnus-summary-last-article-p article)))
7280             (gnus-summary-next-article)
7281           (gnus-summary-next-unread-article))
7282       (if (or (null gnus-current-article)
7283               (null gnus-article-current)
7284               (/= article (cdr gnus-article-current))
7285               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7286           ;; Selected subject is different from current article's.
7287           (gnus-summary-display-article article)
7288         (when article-window
7289           (gnus-eval-in-buffer-window gnus-article-buffer
7290             (setq endp (or (gnus-article-next-page lines)
7291                            (gnus-article-only-boring-p))))
7292           (when endp
7293             (cond (stop
7294                    (gnus-message 3 "End of message"))
7295                   (circular
7296                    (gnus-summary-beginning-of-article))
7297                   (lines
7298                    (gnus-message 3 "End of message"))
7299                   ((null lines)
7300                    (if (and (eq gnus-summary-goto-unread 'never)
7301                             (not (gnus-summary-last-article-p article)))
7302                        (gnus-summary-next-article)
7303                      (gnus-summary-next-unread-article))))))))
7304     (gnus-summary-recenter)
7305     (gnus-summary-position-point)))
7306
7307 (defun gnus-summary-prev-page (&optional lines move)
7308   "Show previous page of selected article.
7309 Argument LINES specifies lines to be scrolled down.
7310 If MOVE, move to the previous unread article if point is at
7311 the beginning of the buffer."
7312   (interactive "P")
7313   (let ((article (gnus-summary-article-number))
7314         (article-window (get-buffer-window gnus-article-buffer t))
7315         endp)
7316     (gnus-configure-windows 'article)
7317     (if (or (null gnus-current-article)
7318             (null gnus-article-current)
7319             (/= article (cdr gnus-article-current))
7320             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7321         ;; Selected subject is different from current article's.
7322         (gnus-summary-display-article article)
7323       (gnus-summary-recenter)
7324       (when article-window
7325         (gnus-eval-in-buffer-window gnus-article-buffer
7326           (setq endp (gnus-article-prev-page lines)))
7327         (when (and move endp)
7328           (cond (lines
7329                  (gnus-message 3 "Beginning of message"))
7330                 ((null lines)
7331                  (if (and (eq gnus-summary-goto-unread 'never)
7332                           (not (gnus-summary-first-article-p article)))
7333                      (gnus-summary-prev-article)
7334                    (gnus-summary-prev-unread-article))))))))
7335   (gnus-summary-position-point))
7336
7337 (defun gnus-summary-prev-page-or-article (&optional lines)
7338   "Show previous page of selected article.
7339 Argument LINES specifies lines to be scrolled down.
7340 If at the beginning of the article, go to the next article."
7341   (interactive "P")
7342   (gnus-summary-prev-page lines t))
7343
7344 (defun gnus-summary-scroll-up (lines)
7345   "Scroll up (or down) one line current article.
7346 Argument LINES specifies lines to be scrolled up (or down if negative)."
7347   (interactive "p")
7348   (gnus-configure-windows 'article)
7349   (gnus-summary-show-thread)
7350   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7351     (gnus-eval-in-buffer-window gnus-article-buffer
7352       (cond ((> lines 0)
7353              (when (gnus-article-next-page lines)
7354                (gnus-message 3 "End of message")))
7355             ((< lines 0)
7356              (gnus-article-prev-page (- lines))))))
7357   (gnus-summary-recenter)
7358   (gnus-summary-position-point))
7359
7360 (defun gnus-summary-scroll-down (lines)
7361   "Scroll down (or up) one line current article.
7362 Argument LINES specifies lines to be scrolled down (or up if negative)."
7363   (interactive "p")
7364   (gnus-summary-scroll-up (- lines)))
7365
7366 (defun gnus-summary-next-same-subject ()
7367   "Select next article which has the same subject as current one."
7368   (interactive)
7369   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7370
7371 (defun gnus-summary-prev-same-subject ()
7372   "Select previous article which has the same subject as current one."
7373   (interactive)
7374   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7375
7376 (defun gnus-summary-next-unread-same-subject ()
7377   "Select next unread article which has the same subject as current one."
7378   (interactive)
7379   (gnus-summary-next-article t (gnus-summary-article-subject)))
7380
7381 (defun gnus-summary-prev-unread-same-subject ()
7382   "Select previous unread article which has the same subject as current one."
7383   (interactive)
7384   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7385
7386 (defun gnus-summary-first-unread-article ()
7387   "Select the first unread article.
7388 Return nil if there are no unread articles."
7389   (interactive)
7390   (prog1
7391       (when (gnus-summary-first-subject t)
7392         (gnus-summary-show-thread)
7393         (gnus-summary-first-subject t)
7394         (gnus-summary-display-article (gnus-summary-article-number)))
7395     (gnus-summary-position-point)))
7396
7397 (defun gnus-summary-first-unread-subject ()
7398   "Place the point on the subject line of the first unread article.
7399 Return nil if there are no unread articles."
7400   (interactive)
7401   (prog1
7402       (when (gnus-summary-first-subject t)
7403         (gnus-summary-show-thread)
7404         (gnus-summary-first-subject t))
7405     (gnus-summary-position-point)))
7406
7407 (defun gnus-summary-first-unseen-subject ()
7408   "Place the point on the subject line of the first unseen article.
7409 Return nil if there are no unseen articles."
7410   (interactive)
7411   (prog1
7412       (when (gnus-summary-first-subject nil nil t)
7413         (gnus-summary-show-thread)
7414         (gnus-summary-first-subject nil nil t))
7415     (gnus-summary-position-point)))
7416
7417 (defun gnus-summary-first-unseen-or-unread-subject ()
7418   "Place the point on the subject line of the first unseen article or,
7419 if all article have been seen, on the subject line of the first unread
7420 article."
7421   (interactive)
7422   (prog1
7423       (unless (when (gnus-summary-first-subject nil nil t)
7424                 (gnus-summary-show-thread)
7425                 (gnus-summary-first-subject nil nil t))
7426         (when (gnus-summary-first-subject t)
7427           (gnus-summary-show-thread)
7428           (gnus-summary-first-subject t)))
7429     (gnus-summary-position-point)))
7430
7431 (defun gnus-summary-first-article ()
7432   "Select the first article.
7433 Return nil if there are no articles."
7434   (interactive)
7435   (prog1
7436       (when (gnus-summary-first-subject)
7437         (gnus-summary-show-thread)
7438         (gnus-summary-first-subject)
7439         (gnus-summary-display-article (gnus-summary-article-number)))
7440     (gnus-summary-position-point)))
7441
7442 (defun gnus-summary-best-unread-article (&optional arg)
7443   "Select the unread article with the highest score.
7444 If given a prefix argument, select the next unread article that has a
7445 score higher than the default score."
7446   (interactive "P")
7447   (let ((article (if arg
7448                      (gnus-summary-better-unread-subject)
7449                    (gnus-summary-best-unread-subject))))
7450     (if article
7451         (gnus-summary-goto-article article)
7452       (error "No unread articles"))))
7453
7454 (defun gnus-summary-best-unread-subject ()
7455   "Select the unread subject with the highest score."
7456   (interactive)
7457   (let ((best -1000000)
7458         (data gnus-newsgroup-data)
7459         article score)
7460     (while data
7461       (and (gnus-data-unread-p (car data))
7462            (> (setq score
7463                     (gnus-summary-article-score (gnus-data-number (car data))))
7464               best)
7465            (setq best score
7466                  article (gnus-data-number (car data))))
7467       (setq data (cdr data)))
7468     (when article
7469       (gnus-summary-goto-subject article))
7470     (gnus-summary-position-point)
7471     article))
7472
7473 (defun gnus-summary-better-unread-subject ()
7474   "Select the first unread subject that has a score over the default score."
7475   (interactive)
7476   (let ((data gnus-newsgroup-data)
7477         article score)
7478     (while (and (setq article (gnus-data-number (car data)))
7479                 (or (gnus-data-read-p (car data))
7480                     (not (> (gnus-summary-article-score article)
7481                             gnus-summary-default-score))))
7482       (setq data (cdr data)))
7483     (when article
7484       (gnus-summary-goto-subject article))
7485     (gnus-summary-position-point)
7486     article))
7487
7488 (defun gnus-summary-last-subject ()
7489   "Go to the last displayed subject line in the group."
7490   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7491     (when article
7492       (gnus-summary-goto-subject article))))
7493
7494 (defun gnus-summary-goto-article (article &optional all-headers force)
7495   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7496 If ALL-HEADERS is non-nil, no header lines are hidden.
7497 If FORCE, go to the article even if it isn't displayed.  If FORCE
7498 is a number, it is the line the article is to be displayed on."
7499   (interactive
7500    (list
7501     (completing-read
7502      "Article number or Message-ID: "
7503      (mapcar (lambda (number) (list (int-to-string number)))
7504              gnus-newsgroup-limit))
7505     current-prefix-arg
7506     t))
7507   (prog1
7508       (if (and (stringp article)
7509                (string-match "@\\|%40" article))
7510           (gnus-summary-refer-article article)
7511         (when (stringp article)
7512           (setq article (string-to-number article)))
7513         (if (gnus-summary-goto-subject article force)
7514             (gnus-summary-display-article article all-headers)
7515           (gnus-message 4 "Couldn't go to article %s" article) nil))
7516     (gnus-summary-position-point)))
7517
7518 (defun gnus-summary-goto-last-article ()
7519   "Go to the previously read article."
7520   (interactive)
7521   (prog1
7522       (when gnus-last-article
7523         (gnus-summary-goto-article gnus-last-article nil t))
7524     (gnus-summary-position-point)))
7525
7526 (defun gnus-summary-pop-article (number)
7527   "Pop one article off the history and go to the previous.
7528 NUMBER articles will be popped off."
7529   (interactive "p")
7530   (let (to)
7531     (setq gnus-newsgroup-history
7532           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7533     (if to
7534         (gnus-summary-goto-article (car to) nil t)
7535       (error "Article history empty")))
7536   (gnus-summary-position-point))
7537
7538 ;; Summary commands and functions for limiting the summary buffer.
7539
7540 (defun gnus-summary-limit-to-articles (n)
7541   "Limit the summary buffer to the next N articles.
7542 If not given a prefix, use the process marked articles instead."
7543   (interactive "P")
7544   (prog1
7545       (let ((articles (gnus-summary-work-articles n)))
7546         (setq gnus-newsgroup-processable nil)
7547         (gnus-summary-limit articles))
7548     (gnus-summary-position-point)))
7549
7550 (defun gnus-summary-pop-limit (&optional total)
7551   "Restore the previous limit.
7552 If given a prefix, remove all limits."
7553   (interactive "P")
7554   (when total
7555     (setq gnus-newsgroup-limits
7556           (list (mapcar (lambda (h) (mail-header-number h))
7557                         gnus-newsgroup-headers))))
7558   (unless gnus-newsgroup-limits
7559     (error "No limit to pop"))
7560   (prog1
7561       (gnus-summary-limit nil 'pop)
7562     (gnus-summary-position-point)))
7563
7564 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7565   "Limit the summary buffer to articles that have subjects that match a regexp.
7566 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7567   (interactive
7568    (list (read-string (if current-prefix-arg
7569                           "Exclude subject (regexp): "
7570                         "Limit to subject (regexp): "))
7571          nil current-prefix-arg))
7572   (unless header
7573     (setq header "subject"))
7574   (when (not (equal "" subject))
7575     (prog1
7576         (let ((articles (gnus-summary-find-matching
7577                          (or header "subject") subject 'all nil nil
7578                          not-matching)))
7579           (unless articles
7580             (error "Found no matches for \"%s\"" subject))
7581           (gnus-summary-limit articles))
7582       (gnus-summary-position-point))))
7583
7584 (defun gnus-summary-limit-to-author (from &optional not-matching)
7585   "Limit the summary buffer to articles that have authors that match a regexp.
7586 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7587   (interactive
7588    (list (read-string (if current-prefix-arg
7589                           "Exclude author (regexp): "
7590                         "Limit to author (regexp): "))
7591          current-prefix-arg))
7592   (gnus-summary-limit-to-subject from "from" not-matching))
7593
7594 (defun gnus-summary-limit-to-age (age &optional younger-p)
7595   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7596 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7597 articles that are younger than AGE days."
7598   (interactive
7599    (let ((younger current-prefix-arg)
7600          (days-got nil)
7601          days)
7602      (while (not days-got)
7603        (setq days (if younger
7604                       (read-string "Limit to articles younger than (in days, older when negative): ")
7605                     (read-string
7606                      "Limit to articles older than (in days, younger when negative): ")))
7607        (when (> (length days) 0)
7608          (setq days (read days)))
7609        (if (numberp days)
7610            (progn
7611              (setq days-got t)
7612              (when (< days 0)
7613                (setq younger (not younger))
7614                (setq days (* days -1))))
7615          (message "Please enter a number.")
7616          (sleep-for 1)))
7617      (list days younger)))
7618   (prog1
7619       (let ((data gnus-newsgroup-data)
7620             (cutoff (days-to-time age))
7621             articles d date is-younger)
7622         (while (setq d (pop data))
7623           (when (and (vectorp (gnus-data-header d))
7624                      (setq date (mail-header-date (gnus-data-header d))))
7625             (setq is-younger (time-less-p
7626                               (time-since (condition-case ()
7627                                               (date-to-time date)
7628                                             (error '(0 0))))
7629                               cutoff))
7630             (when (if younger-p
7631                       is-younger
7632                     (not is-younger))
7633               (push (gnus-data-number d) articles))))
7634         (gnus-summary-limit (nreverse articles)))
7635     (gnus-summary-position-point)))
7636
7637 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7638   "Limit the summary buffer to articles that match an 'extra' header."
7639   (interactive
7640    (let ((header
7641           (intern
7642            (gnus-completing-read-with-default
7643             (symbol-name (car gnus-extra-headers))
7644             (if current-prefix-arg
7645                 "Exclude extra header:"
7646               "Limit extra header:")
7647             (mapcar (lambda (x)
7648                       (cons (symbol-name x) x))
7649                     gnus-extra-headers)
7650             nil
7651             t))))
7652      (list header
7653            (read-string (format "%s header %s (regexp): "
7654                                 (if current-prefix-arg "Exclude" "Limit to")
7655                                 header))
7656            current-prefix-arg)))
7657   (when (not (equal "" regexp))
7658     (prog1
7659         (let ((articles (gnus-summary-find-matching
7660                          (cons 'extra header) regexp 'all nil nil
7661                          not-matching)))
7662           (unless articles
7663             (error "Found no matches for \"%s\"" regexp))
7664           (gnus-summary-limit articles))
7665       (gnus-summary-position-point))))
7666
7667 (defun gnus-summary-limit-to-display-predicate ()
7668   "Limit the summary buffer to the predicated in the `display' group parameter."
7669   (interactive)
7670   (unless gnus-newsgroup-display
7671     (error "There is no `display' group parameter"))
7672   (let (articles)
7673     (dolist (number gnus-newsgroup-articles)
7674       (when (funcall gnus-newsgroup-display)
7675         (push number articles)))
7676     (gnus-summary-limit articles))
7677   (gnus-summary-position-point))
7678
7679 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7680 (make-obsolete
7681  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7682
7683 (defun gnus-summary-limit-to-unread (&optional all)
7684   "Limit the summary buffer to articles that are not marked as read.
7685 If ALL is non-nil, limit strictly to unread articles."
7686   (interactive "P")
7687   (if all
7688       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7689     (gnus-summary-limit-to-marks
7690      ;; Concat all the marks that say that an article is read and have
7691      ;; those removed.
7692      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7693            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7694            gnus-low-score-mark gnus-expirable-mark
7695            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7696            gnus-duplicate-mark gnus-souped-mark)
7697      'reverse)))
7698
7699 (defun gnus-summary-limit-to-replied (&optional unreplied)
7700   "Limit the summary buffer to replied articles.
7701 If UNREPLIED (the prefix), limit to unreplied articles."
7702   (interactive "P")
7703   (if unreplied
7704       (gnus-summary-limit
7705        (gnus-set-difference gnus-newsgroup-articles
7706         gnus-newsgroup-replied))
7707     (gnus-summary-limit gnus-newsgroup-replied))
7708   (gnus-summary-position-point))
7709
7710 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7711 (make-obsolete 'gnus-summary-delete-marked-with
7712                'gnus-summary-limit-exclude-marks)
7713
7714 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7715   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7716 If REVERSE, limit the summary buffer to articles that are marked
7717 with MARKS.  MARKS can either be a string of marks or a list of marks.
7718 Returns how many articles were removed."
7719   (interactive "sMarks: ")
7720   (gnus-summary-limit-to-marks marks t))
7721
7722 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7723   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7724 If REVERSE (the prefix), limit the summary buffer to articles that are
7725 not marked with MARKS.  MARKS can either be a string of marks or a
7726 list of marks.
7727 Returns how many articles were removed."
7728   (interactive "sMarks: \nP")
7729   (prog1
7730       (let ((data gnus-newsgroup-data)
7731             (marks (if (listp marks) marks
7732                      (append marks nil))) ; Transform to list.
7733             articles)
7734         (while data
7735           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7736                   (memq (gnus-data-mark (car data)) marks))
7737             (push (gnus-data-number (car data)) articles))
7738           (setq data (cdr data)))
7739         (gnus-summary-limit articles))
7740     (gnus-summary-position-point)))
7741
7742 (defun gnus-summary-limit-to-score (score)
7743   "Limit to articles with score at or above SCORE."
7744   (interactive "NLimit to articles with score of at least: ")
7745   (let ((data gnus-newsgroup-data)
7746         articles)
7747     (while data
7748       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7749                 score)
7750         (push (gnus-data-number (car data)) articles))
7751       (setq data (cdr data)))
7752     (prog1
7753         (gnus-summary-limit articles)
7754       (gnus-summary-position-point))))
7755
7756 (defun gnus-summary-limit-to-unseen ()
7757   "Limit to unseen articles."
7758   (interactive)
7759   (prog1
7760       (gnus-summary-limit gnus-newsgroup-unseen)
7761     (gnus-summary-position-point)))
7762
7763 (defun gnus-summary-limit-include-thread (id)
7764   "Display all the hidden articles that is in the thread with ID in it.
7765 When called interactively, ID is the Message-ID of the current
7766 article."
7767   (interactive (list (mail-header-id (gnus-summary-article-header))))
7768   (let ((articles (gnus-articles-in-thread
7769                    (gnus-id-to-thread (gnus-root-id id)))))
7770     (prog1
7771         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7772       (gnus-summary-limit-include-matching-articles
7773        "subject"
7774        (regexp-quote (gnus-simplify-subject-re
7775                       (mail-header-subject (gnus-id-to-header id)))))
7776       (gnus-summary-position-point))))
7777
7778 (defun gnus-summary-limit-include-matching-articles (header regexp)
7779   "Display all the hidden articles that have HEADERs that match REGEXP."
7780   (interactive (list (read-string "Match on header: ")
7781                      (read-string "Regexp: ")))
7782   (let ((articles (gnus-find-matching-articles header regexp)))
7783     (prog1
7784         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7785       (gnus-summary-position-point))))
7786
7787 (defun gnus-summary-insert-dormant-articles ()
7788   "Insert all the dormant articles for this group into the current buffer."
7789   (interactive)
7790   (let ((gnus-verbose (max 6 gnus-verbose)))
7791     (if (not gnus-newsgroup-dormant)
7792         (gnus-message 3 "No cached articles for this group")
7793       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7794
7795 (defun gnus-summary-limit-include-dormant ()
7796   "Display all the hidden articles that are marked as dormant.
7797 Note that this command only works on a subset of the articles currently
7798 fetched for this group."
7799   (interactive)
7800   (unless gnus-newsgroup-dormant
7801     (error "There are no dormant articles in this group"))
7802   (prog1
7803       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7804     (gnus-summary-position-point)))
7805
7806 (defun gnus-summary-limit-exclude-dormant ()
7807   "Hide all dormant articles."
7808   (interactive)
7809   (prog1
7810       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7811     (gnus-summary-position-point)))
7812
7813 (defun gnus-summary-limit-exclude-childless-dormant ()
7814   "Hide all dormant articles that have no children."
7815   (interactive)
7816   (let ((data (gnus-data-list t))
7817         articles d children)
7818     ;; Find all articles that are either not dormant or have
7819     ;; children.
7820     (while (setq d (pop data))
7821       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7822                 (and (setq children
7823                            (gnus-article-children (gnus-data-number d)))
7824                      (let (found)
7825                        (while children
7826                          (when (memq (car children) articles)
7827                            (setq children nil
7828                                  found t))
7829                          (pop children))
7830                        found)))
7831         (push (gnus-data-number d) articles)))
7832     ;; Do the limiting.
7833     (prog1
7834         (gnus-summary-limit articles)
7835       (gnus-summary-position-point))))
7836
7837 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7838   "Mark all unread excluded articles as read.
7839 If ALL, mark even excluded ticked and dormants as read."
7840   (interactive "P")
7841   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7842   (let ((articles (gnus-sorted-ndifference
7843                    (sort
7844                     (mapcar (lambda (h) (mail-header-number h))
7845                             gnus-newsgroup-headers)
7846                     '<)
7847                    gnus-newsgroup-limit))
7848         article)
7849     (setq gnus-newsgroup-unreads
7850           (gnus-sorted-intersection gnus-newsgroup-unreads
7851                                     gnus-newsgroup-limit))
7852     (if all
7853         (setq gnus-newsgroup-dormant nil
7854               gnus-newsgroup-marked nil
7855               gnus-newsgroup-reads
7856               (nconc
7857                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7858                gnus-newsgroup-reads))
7859       (while (setq article (pop articles))
7860         (unless (or (memq article gnus-newsgroup-dormant)
7861                     (memq article gnus-newsgroup-marked))
7862           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7863
7864 (defun gnus-summary-limit (articles &optional pop)
7865   (if pop
7866       ;; We pop the previous limit off the stack and use that.
7867       (setq articles (car gnus-newsgroup-limits)
7868             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7869     ;; We use the new limit, so we push the old limit on the stack.
7870     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7871   ;; Set the limit.
7872   (setq gnus-newsgroup-limit articles)
7873   (let ((total (length gnus-newsgroup-data))
7874         (data (gnus-data-find-list (gnus-summary-article-number)))
7875         (gnus-summary-mark-below nil)   ; Inhibit this.
7876         found)
7877     ;; This will do all the work of generating the new summary buffer
7878     ;; according to the new limit.
7879     (gnus-summary-prepare)
7880     ;; Hide any threads, possibly.
7881     (gnus-summary-maybe-hide-threads)
7882     ;; Try to return to the article you were at, or one in the
7883     ;; neighborhood.
7884     (when data
7885       ;; We try to find some article after the current one.
7886       (while data
7887         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7888           (setq data nil
7889                 found t))
7890         (setq data (cdr data))))
7891     (unless found
7892       ;; If there is no data, that means that we were after the last
7893       ;; article.  The same goes when we can't find any articles
7894       ;; after the current one.
7895       (goto-char (point-max))
7896       (gnus-summary-find-prev))
7897     (gnus-set-mode-line 'summary)
7898     ;; We return how many articles were removed from the summary
7899     ;; buffer as a result of the new limit.
7900     (- total (length gnus-newsgroup-data))))
7901
7902 (defsubst gnus-invisible-cut-children (threads)
7903   (let ((num 0))
7904     (while threads
7905       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7906         (incf num))
7907       (pop threads))
7908     (< num 2)))
7909
7910 (defsubst gnus-cut-thread (thread)
7911   "Go forwards in the thread until we find an article that we want to display."
7912   (when (or (eq gnus-fetch-old-headers 'some)
7913             (eq gnus-fetch-old-headers 'invisible)
7914             (numberp gnus-fetch-old-headers)
7915             (eq gnus-build-sparse-threads 'some)
7916             (eq gnus-build-sparse-threads 'more))
7917     ;; Deal with old-fetched headers and sparse threads.
7918     (while (and
7919             thread
7920             (or
7921              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7922              (gnus-summary-article-ancient-p
7923               (mail-header-number (car thread))))
7924             (if (or (<= (length (cdr thread)) 1)
7925                     (eq gnus-fetch-old-headers 'invisible))
7926                 (setq gnus-newsgroup-limit
7927                       (delq (mail-header-number (car thread))
7928                             gnus-newsgroup-limit)
7929                       thread (cadr thread))
7930               (when (gnus-invisible-cut-children (cdr thread))
7931                 (let ((th (cdr thread)))
7932                   (while th
7933                     (if (memq (mail-header-number (caar th))
7934                               gnus-newsgroup-limit)
7935                         (setq thread (car th)
7936                               th nil)
7937                       (setq th (cdr th))))))))))
7938   thread)
7939
7940 (defun gnus-cut-threads (threads)
7941   "Cut off all uninteresting articles from the beginning of THREADS."
7942   (when (or (eq gnus-fetch-old-headers 'some)
7943             (eq gnus-fetch-old-headers 'invisible)
7944             (numberp gnus-fetch-old-headers)
7945             (eq gnus-build-sparse-threads 'some)
7946             (eq gnus-build-sparse-threads 'more))
7947     (let ((th threads))
7948       (while th
7949         (setcar th (gnus-cut-thread (car th)))
7950         (setq th (cdr th)))))
7951   ;; Remove nixed out threads.
7952   (delq nil threads))
7953
7954 (defun gnus-summary-initial-limit (&optional show-if-empty)
7955   "Figure out what the initial limit is supposed to be on group entry.
7956 This entails weeding out unwanted dormants, low-scored articles,
7957 fetch-old-headers verbiage, and so on."
7958   ;; Most groups have nothing to remove.
7959   (if (or gnus-inhibit-limiting
7960           (and (null gnus-newsgroup-dormant)
7961                (eq gnus-newsgroup-display 'gnus-not-ignore)
7962                (not (eq gnus-fetch-old-headers 'some))
7963                (not (numberp gnus-fetch-old-headers))
7964                (not (eq gnus-fetch-old-headers 'invisible))
7965                (null gnus-summary-expunge-below)
7966                (not (eq gnus-build-sparse-threads 'some))
7967                (not (eq gnus-build-sparse-threads 'more))
7968                (null gnus-thread-expunge-below)
7969                (not gnus-use-nocem)))
7970       ()                                ; Do nothing.
7971     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7972     (setq gnus-newsgroup-limit nil)
7973     (mapatoms
7974      (lambda (node)
7975        (unless (car (symbol-value node))
7976          ;; These threads have no parents -- they are roots.
7977          (let ((nodes (cdr (symbol-value node)))
7978                thread)
7979            (while nodes
7980              (if (and gnus-thread-expunge-below
7981                       (< (gnus-thread-total-score (car nodes))
7982                          gnus-thread-expunge-below))
7983                  (gnus-expunge-thread (pop nodes))
7984                (setq thread (pop nodes))
7985                (gnus-summary-limit-children thread))))))
7986      gnus-newsgroup-dependencies)
7987     ;; If this limitation resulted in an empty group, we might
7988     ;; pop the previous limit and use it instead.
7989     (when (and (not gnus-newsgroup-limit)
7990                show-if-empty)
7991       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7992     gnus-newsgroup-limit))
7993
7994 (defun gnus-summary-limit-children (thread)
7995   "Return 1 if this subthread is visible and 0 if it is not."
7996   ;; First we get the number of visible children to this thread.  This
7997   ;; is done by recursing down the thread using this function, so this
7998   ;; will really go down to a leaf article first, before slowly
7999   ;; working its way up towards the root.
8000   (when thread
8001     (let* ((max-lisp-eval-depth 5000)
8002            (children
8003            (if (cdr thread)
8004                (apply '+ (mapcar 'gnus-summary-limit-children
8005                                  (cdr thread)))
8006              0))
8007           (number (mail-header-number (car thread)))
8008           score)
8009       (if (and
8010            (not (memq number gnus-newsgroup-marked))
8011            (or
8012             ;; If this article is dormant and has absolutely no visible
8013             ;; children, then this article isn't visible.
8014             (and (memq number gnus-newsgroup-dormant)
8015                  (zerop children))
8016             ;; If this is "fetch-old-headered" and there is no
8017             ;; visible children, then we don't want this article.
8018             (and (or (eq gnus-fetch-old-headers 'some)
8019                      (numberp gnus-fetch-old-headers))
8020                  (gnus-summary-article-ancient-p number)
8021                  (zerop children))
8022             ;; If this is "fetch-old-headered" and `invisible', then
8023             ;; we don't want this article.
8024             (and (eq gnus-fetch-old-headers 'invisible)
8025                  (gnus-summary-article-ancient-p number))
8026             ;; If this is a sparsely inserted article with no children,
8027             ;; we don't want it.
8028             (and (eq gnus-build-sparse-threads 'some)
8029                  (gnus-summary-article-sparse-p number)
8030                  (zerop children))
8031             ;; If we use expunging, and this article is really
8032             ;; low-scored, then we don't want this article.
8033             (when (and gnus-summary-expunge-below
8034                        (< (setq score
8035                                 (or (cdr (assq number gnus-newsgroup-scored))
8036                                     gnus-summary-default-score))
8037                           gnus-summary-expunge-below))
8038               ;; We increase the expunge-tally here, but that has
8039               ;; nothing to do with the limits, really.
8040               (incf gnus-newsgroup-expunged-tally)
8041               ;; We also mark as read here, if that's wanted.
8042               (when (and gnus-summary-mark-below
8043                          (< score gnus-summary-mark-below))
8044                 (setq gnus-newsgroup-unreads
8045                       (delq number gnus-newsgroup-unreads))
8046                 (if gnus-newsgroup-auto-expire
8047                     (push number gnus-newsgroup-expirable)
8048                   (push (cons number gnus-low-score-mark)
8049                         gnus-newsgroup-reads)))
8050               t)
8051             ;; Do the `display' group parameter.
8052             (and gnus-newsgroup-display
8053                  (not (funcall gnus-newsgroup-display)))
8054             ;; Check NoCeM things.
8055             (when (and gnus-use-nocem
8056                        (gnus-nocem-unwanted-article-p
8057                         (mail-header-id (car thread))))
8058               (setq gnus-newsgroup-unreads
8059                     (delq number gnus-newsgroup-unreads))
8060               t)))
8061           ;; Nope, invisible article.
8062           0
8063         ;; Ok, this article is to be visible, so we add it to the limit
8064         ;; and return 1.
8065         (push number gnus-newsgroup-limit)
8066         1))))
8067
8068 (defun gnus-expunge-thread (thread)
8069   "Mark all articles in THREAD as read."
8070   (let* ((number (mail-header-number (car thread))))
8071     (incf gnus-newsgroup-expunged-tally)
8072     ;; We also mark as read here, if that's wanted.
8073     (setq gnus-newsgroup-unreads
8074           (delq number gnus-newsgroup-unreads))
8075     (if gnus-newsgroup-auto-expire
8076         (push number gnus-newsgroup-expirable)
8077       (push (cons number gnus-low-score-mark)
8078             gnus-newsgroup-reads)))
8079   ;; Go recursively through all subthreads.
8080   (mapcar 'gnus-expunge-thread (cdr thread)))
8081
8082 ;; Summary article oriented commands
8083
8084 (defun gnus-summary-refer-parent-article (n)
8085   "Refer parent article N times.
8086 If N is negative, go to ancestor -N instead.
8087 The difference between N and the number of articles fetched is returned."
8088   (interactive "p")
8089   (let ((skip 1)
8090         error header ref)
8091     (when (not (natnump n))
8092       (setq skip (abs n)
8093             n 1))
8094     (while (and (> n 0)
8095                 (not error))
8096       (setq header (gnus-summary-article-header))
8097       (if (and (eq (mail-header-number header)
8098                    (cdr gnus-article-current))
8099                (equal gnus-newsgroup-name
8100                       (car gnus-article-current)))
8101           ;; If we try to find the parent of the currently
8102           ;; displayed article, then we take a look at the actual
8103           ;; References header, since this is slightly more
8104           ;; reliable than the References field we got from the
8105           ;; server.
8106           (save-excursion
8107             (set-buffer gnus-original-article-buffer)
8108             (nnheader-narrow-to-headers)
8109             (unless (setq ref (message-fetch-field "references"))
8110               (when (setq ref (message-fetch-field "in-reply-to"))
8111                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8112             (widen))
8113         (setq ref
8114               ;; It's not the current article, so we take a bet on
8115               ;; the value we got from the server.
8116               (mail-header-references header)))
8117       (if (and ref
8118                (not (equal ref "")))
8119           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8120             (gnus-message 1 "Couldn't find parent"))
8121         (gnus-message 1 "No references in article %d"
8122                       (gnus-summary-article-number))
8123         (setq error t))
8124       (decf n))
8125     (gnus-summary-position-point)
8126     n))
8127
8128 (defun gnus-summary-refer-references ()
8129   "Fetch all articles mentioned in the References header.
8130 Return the number of articles fetched."
8131   (interactive)
8132   (let ((ref (mail-header-references (gnus-summary-article-header)))
8133         (current (gnus-summary-article-number))
8134         (n 0))
8135     (if (or (not ref)
8136             (equal ref ""))
8137         (error "No References in the current article")
8138       ;; For each Message-ID in the References header...
8139       (while (string-match "<[^>]*>" ref)
8140         (incf n)
8141         ;; ... fetch that article.
8142         (gnus-summary-refer-article
8143          (prog1 (match-string 0 ref)
8144            (setq ref (substring ref (match-end 0))))))
8145       (gnus-summary-goto-subject current)
8146       (gnus-summary-position-point)
8147       n)))
8148
8149 (defun gnus-summary-refer-thread (&optional limit)
8150   "Fetch all articles in the current thread.
8151 If LIMIT (the numerical prefix), fetch that many old headers instead
8152 of what's specified by the `gnus-refer-thread-limit' variable."
8153   (interactive "P")
8154   (let ((id (mail-header-id (gnus-summary-article-header)))
8155         (limit (if limit (prefix-numeric-value limit)
8156                  gnus-refer-thread-limit)))
8157     (unless (eq gnus-fetch-old-headers 'invisible)
8158       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8159       ;; Retrieve the headers and read them in.
8160       (if (eq (if (numberp limit)
8161                   (gnus-retrieve-headers
8162                    (list (min
8163                           (+ (mail-header-number
8164                               (gnus-summary-article-header))
8165                              limit)
8166                           gnus-newsgroup-end))
8167                    gnus-newsgroup-name (* limit 2))
8168                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8169                 ;; headers.
8170                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8171                                        gnus-newsgroup-name limit))
8172               'nov)
8173           (gnus-build-all-threads)
8174         (error "Can't fetch thread from back ends that don't support NOV"))
8175       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8176     (gnus-summary-limit-include-thread id)))
8177
8178 (defun gnus-summary-refer-article (message-id)
8179   "Fetch an article specified by MESSAGE-ID."
8180   (interactive "sMessage-ID: ")
8181   (when (and (stringp message-id)
8182              (not (zerop (length message-id))))
8183     (setq message-id (gnus-replace-in-string message-id " " ""))
8184     ;; Construct the correct Message-ID if necessary.
8185     ;; Suggested by tale@pawl.rpi.edu.
8186     (unless (string-match "^<" message-id)
8187       (setq message-id (concat "<" message-id)))
8188     (unless (string-match ">$" message-id)
8189       (setq message-id (concat message-id ">")))
8190     ;; People often post MIDs from URLs, so unhex it:
8191     (unless (string-match "@" message-id)
8192       (setq message-id (gnus-url-unhex-string message-id)))
8193     (let* ((header (gnus-id-to-header message-id))
8194            (sparse (and header
8195                         (gnus-summary-article-sparse-p
8196                          (mail-header-number header))
8197                         (memq (mail-header-number header)
8198                               gnus-newsgroup-limit)))
8199            number)
8200       (cond
8201        ;; If the article is present in the buffer we just go to it.
8202        ((and header
8203              (or (not (gnus-summary-article-sparse-p
8204                        (mail-header-number header)))
8205                  sparse))
8206         (prog1
8207             (gnus-summary-goto-article
8208              (mail-header-number header) nil t)
8209           (when sparse
8210             (gnus-summary-update-article (mail-header-number header)))))
8211        (t
8212         ;; We fetch the article.
8213         (catch 'found
8214           (dolist (gnus-override-method (gnus-refer-article-methods))
8215             (when (and (gnus-check-server gnus-override-method)
8216                        ;; Fetch the header,
8217                        (setq number (gnus-summary-insert-subject message-id)))
8218               ;; and display the article.
8219               (gnus-summary-select-article nil nil nil number)
8220               (throw 'found t)))
8221           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8222
8223 (defun gnus-refer-article-methods ()
8224   "Return a list of referable methods."
8225   (cond
8226    ;; No method, so we default to current and native.
8227    ((null gnus-refer-article-method)
8228     (list gnus-current-select-method gnus-select-method))
8229    ;; Current.
8230    ((eq 'current gnus-refer-article-method)
8231     (list gnus-current-select-method))
8232    ;; List of select methods.
8233    ((not (and (symbolp (car gnus-refer-article-method))
8234               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8235     (let (out)
8236       (dolist (method gnus-refer-article-method)
8237         (push (if (eq 'current method)
8238                   gnus-current-select-method
8239                 method)
8240               out))
8241       (nreverse out)))
8242    ;; One single select method.
8243    (t
8244     (list gnus-refer-article-method))))
8245
8246 (defun gnus-summary-edit-parameters ()
8247   "Edit the group parameters of the current group."
8248   (interactive)
8249   (gnus-group-edit-group gnus-newsgroup-name 'params))
8250
8251 (defun gnus-summary-customize-parameters ()
8252   "Customize the group parameters of the current group."
8253   (interactive)
8254   (gnus-group-customize gnus-newsgroup-name))
8255
8256 (defun gnus-summary-enter-digest-group (&optional force)
8257   "Enter an nndoc group based on the current article.
8258 If FORCE, force a digest interpretation.  If not, try
8259 to guess what the document format is."
8260   (interactive "P")
8261   (let ((conf gnus-current-window-configuration))
8262     (save-window-excursion
8263       (save-excursion
8264         (let (gnus-article-prepare-hook
8265               gnus-display-mime-function
8266               gnus-break-pages)
8267           (gnus-summary-select-article))))
8268     (setq gnus-current-window-configuration conf)
8269     (let* ((name (format "%s-%d"
8270                          (gnus-group-prefixed-name
8271                           gnus-newsgroup-name (list 'nndoc ""))
8272                          (save-excursion
8273                            (set-buffer gnus-summary-buffer)
8274                            gnus-current-article)))
8275            (ogroup gnus-newsgroup-name)
8276            (params (append (gnus-info-params (gnus-get-info ogroup))
8277                            (list (cons 'to-group ogroup))
8278                            (list (cons 'parent-group ogroup))
8279                            (list (cons 'save-article-group ogroup))))
8280            (case-fold-search t)
8281            (buf (current-buffer))
8282            dig to-address)
8283       (save-excursion
8284         (set-buffer gnus-original-article-buffer)
8285         ;; Have the digest group inherit the main mail address of
8286         ;; the parent article.
8287         (when (setq to-address (or (gnus-fetch-field "reply-to")
8288                                    (gnus-fetch-field "from")))
8289           (setq params (append
8290                         (list (cons 'to-address
8291                                     (funcall gnus-decode-encoded-word-function
8292                                              to-address))))))
8293         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8294         (insert-buffer-substring gnus-original-article-buffer)
8295         ;; Remove lines that may lead nndoc to misinterpret the
8296         ;; document type.
8297         (narrow-to-region
8298          (goto-char (point-min))
8299          (or (search-forward "\n\n" nil t) (point)))
8300         (goto-char (point-min))
8301         (delete-matching-lines "^Path:\\|^From ")
8302         (widen))
8303       (unwind-protect
8304           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8305                     (gnus-newsgroup-ephemeral-ignored-charsets
8306                      gnus-newsgroup-ignored-charsets))
8307                 (gnus-group-read-ephemeral-group
8308                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8309                               (nndoc-article-type
8310                                ,(if force 'mbox 'guess)))
8311                  t nil nil nil
8312                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8313                                                         "ADAPT")))))
8314               ;; Make all postings to this group go to the parent group.
8315               (nconc (gnus-info-params (gnus-get-info name))
8316                      params)
8317             ;; Couldn't select this doc group.
8318             (switch-to-buffer buf)
8319             (gnus-set-global-variables)
8320             (gnus-configure-windows 'summary)
8321             (gnus-message 3 "Article couldn't be entered?"))
8322         (kill-buffer dig)))))
8323
8324 (defun gnus-summary-read-document (n)
8325   "Open a new group based on the current article(s).
8326 This will allow you to read digests and other similar
8327 documents as newsgroups.
8328 Obeys the standard process/prefix convention."
8329   (interactive "P")
8330   (let* ((articles (gnus-summary-work-articles n))
8331          (ogroup gnus-newsgroup-name)
8332          (params (append (gnus-info-params (gnus-get-info ogroup))
8333                          (list (cons 'to-group ogroup))))
8334          article group egroup groups vgroup)
8335     (while (setq article (pop articles))
8336       (setq group (format "%s-%d" gnus-newsgroup-name article))
8337       (gnus-summary-remove-process-mark article)
8338       (when (gnus-summary-display-article article)
8339         (save-excursion
8340           (with-temp-buffer
8341             (insert-buffer-substring gnus-original-article-buffer)
8342             ;; Remove some headers that may lead nndoc to make
8343             ;; the wrong guess.
8344             (message-narrow-to-head)
8345             (goto-char (point-min))
8346             (delete-matching-lines "^Path:\\|^From ")
8347             (widen)
8348             (if (setq egroup
8349                       (gnus-group-read-ephemeral-group
8350                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8351                                      (nndoc-article-type guess))
8352                        t nil t))
8353                 (progn
8354                   ;; Make all postings to this group go to the parent group.
8355                   (nconc (gnus-info-params (gnus-get-info egroup))
8356                          params)
8357                   (push egroup groups))
8358               ;; Couldn't select this doc group.
8359               (gnus-error 3 "Article couldn't be entered"))))))
8360     ;; Now we have selected all the documents.
8361     (cond
8362      ((not groups)
8363       (error "None of the articles could be interpreted as documents"))
8364      ((gnus-group-read-ephemeral-group
8365        (setq vgroup (format
8366                      "nnvirtual:%s-%s" gnus-newsgroup-name
8367                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8368        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8369        t
8370        (cons (current-buffer) 'summary)))
8371      (t
8372       (error "Couldn't select virtual nndoc group")))))
8373
8374 (defun gnus-summary-isearch-article (&optional regexp-p)
8375   "Do incremental search forward on the current article.
8376 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8377   (interactive "P")
8378   (let* ((gnus-inhibit-treatment t)
8379          (old (gnus-summary-select-article)))
8380     (gnus-configure-windows 'article)
8381     (gnus-eval-in-buffer-window gnus-article-buffer
8382       (save-restriction
8383         (widen)
8384         (when (eq 'old old)
8385           (gnus-article-show-all-headers))
8386         (goto-char (point-min))
8387         (isearch-forward regexp-p)))))
8388
8389 (defun gnus-summary-search-article-forward (regexp &optional backward)
8390   "Search for an article containing REGEXP forward.
8391 If BACKWARD, search backward instead."
8392   (interactive
8393    (list (read-string
8394           (format "Search article %s (regexp%s): "
8395                   (if current-prefix-arg "backward" "forward")
8396                   (if gnus-last-search-regexp
8397                       (concat ", default " gnus-last-search-regexp)
8398                     "")))
8399          current-prefix-arg))
8400   (if (string-equal regexp "")
8401       (setq regexp (or gnus-last-search-regexp ""))
8402     (setq gnus-last-search-regexp regexp)
8403     (setq gnus-article-before-search gnus-current-article))
8404   ;; Intentionally set gnus-last-article.
8405   (setq gnus-last-article gnus-article-before-search)
8406   (let ((gnus-last-article gnus-last-article))
8407     (if (gnus-summary-search-article regexp backward)
8408         (gnus-summary-show-thread)
8409       (error "Search failed: \"%s\"" regexp))))
8410
8411 (defun gnus-summary-search-article-backward (regexp)
8412   "Search for an article containing REGEXP backward."
8413   (interactive
8414    (list (read-string
8415           (format "Search article backward (regexp%s): "
8416                   (if gnus-last-search-regexp
8417                       (concat ", default " gnus-last-search-regexp)
8418                     "")))))
8419   (gnus-summary-search-article-forward regexp 'backward))
8420
8421 (eval-when-compile
8422   (defmacro gnus-summary-search-article-position-point (regexp backward)
8423     "Dehighlight the last matched text and goto the beginning position."
8424     (` (if (and gnus-summary-search-article-matched-data
8425                 (let ((text (caddr gnus-summary-search-article-matched-data))
8426                       (inhibit-read-only t)
8427                       buffer-read-only)
8428                   (delete-region
8429                    (goto-char (car gnus-summary-search-article-matched-data))
8430                    (cadr gnus-summary-search-article-matched-data))
8431                   (insert text)
8432                   (string-match (, regexp) text)))
8433            (if (, backward) (beginning-of-line) (end-of-line))
8434          (goto-char (if (, backward) (point-max) (point-min))))))
8435
8436   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
8437     "Place point where X-Face image is displayed."
8438     (if (featurep 'xemacs)
8439         (` (let ((end (if (search-forward "\n\n" nil t)
8440                           (goto-char (1- (point)))
8441                         (point-min)))
8442                  extent)
8443              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
8444              (unless (and (re-search-forward "^From:" end t)
8445                           (setq extent (extent-at (point)))
8446                           (extent-begin-glyph extent))
8447                (goto-char (, opoint)))))
8448       (` (let ((end (if (search-forward "\n\n" nil t)
8449                         (goto-char (1- (point)))
8450                       (point-min)))
8451                (start (or (search-backward "\n\n" nil t) (point-min))))
8452            (goto-char
8453             (or (text-property-any start end 'x-face-image t);; x-face-e21
8454                 (text-property-any start end 'x-face-mule-bitmap-image t)
8455                 (, opoint)))))))
8456
8457   (defmacro gnus-summary-search-article-highlight-matched-text
8458     (backward treated x-face)
8459     "Highlight matched text in the function `gnus-summary-search-article'."
8460     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
8461              (end (set-marker (make-marker) (match-end 0)))
8462              (inhibit-read-only t)
8463              buffer-read-only)
8464          (unless treated
8465            (let ((,@
8466                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
8467                     (mapcar
8468                      (lambda (item) (setq items (delq item items)))
8469                      '(gnus-treat-buttonize
8470                        gnus-treat-fill-article
8471                        gnus-treat-fill-long-lines
8472                        gnus-treat-emphasize
8473                        gnus-treat-highlight-headers
8474                        gnus-treat-highlight-citation
8475                        gnus-treat-highlight-signature
8476                        gnus-treat-overstrike
8477                        gnus-treat-display-x-face
8478                        gnus-treat-buttonize-head
8479                        gnus-treat-decode-article-as-default-mime-charset))
8480                     (static-if (featurep 'xemacs)
8481                         items
8482                       (cons '(x-face-mule-delete-x-face-field
8483                               (quote never))
8484                             items))))
8485                  (gnus-treat-display-x-face
8486                   (when (, x-face) gnus-treat-display-x-face)))
8487              (gnus-article-prepare-mime-display)))
8488          (goto-char (if (, backward) start end))
8489          (when (, x-face)
8490            (gnus-summary-search-article-highlight-goto-x-face (point)))
8491          (setq gnus-summary-search-article-matched-data
8492                (list start end (buffer-substring start end)))
8493          (unless (eq start end);; matched text has been deleted. :-<
8494            (put-text-property start end 'face
8495                               (or (find-face 'isearch)
8496                                   'secondary-selection))))))
8497   )
8498
8499 (defun gnus-summary-search-article (regexp &optional backward)
8500   "Search for an article containing REGEXP.
8501 Optional argument BACKWARD means do search for backward.
8502 `gnus-select-article-hook' is not called during the search."
8503   ;; We have to require this here to make sure that the following
8504   ;; dynamic binding isn't shadowed by autoloading.
8505   (require 'gnus-async)
8506   (require 'gnus-art)
8507   (let ((gnus-select-article-hook nil)  ;Disable hook.
8508         (gnus-article-prepare-hook nil)
8509         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8510         (gnus-use-article-prefetch nil)
8511         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8512         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8513         (gnus-visual nil)
8514         (gnus-keep-backlog nil)
8515         (gnus-break-pages nil)
8516         (gnus-summary-display-arrow nil)
8517         (gnus-updated-mode-lines nil)
8518         (gnus-auto-center-summary nil)
8519         (sum (current-buffer))
8520         (found nil)
8521         point treated)
8522     (gnus-save-hidden-threads
8523       (static-if (featurep 'xemacs)
8524           (let ((gnus-inhibit-treatment t))
8525             (setq treated (eq 'old (gnus-summary-select-article)))
8526             (when (and treated
8527                        (not (and (gnus-buffer-live-p gnus-article-buffer)
8528                                  (window-live-p (get-buffer-window
8529                                                  gnus-article-buffer t)))))
8530               (gnus-summary-select-article nil t)
8531               (setq treated nil)))
8532         (let ((gnus-inhibit-treatment t)
8533               (x-face-mule-delete-x-face-field 'never))
8534           (setq treated (eq 'old (gnus-summary-select-article)))
8535           (when (and treated
8536                      (not
8537                       (and (gnus-buffer-live-p gnus-article-buffer)
8538                            (window-live-p (get-buffer-window
8539                                            gnus-article-buffer t))
8540                            (or (not (string-match "^\\^X-Face:" regexp))
8541                                (with-current-buffer gnus-article-buffer
8542                                  gnus-summary-search-article-matched-data)))))
8543             (gnus-summary-select-article nil t)
8544             (setq treated nil))))
8545       (set-buffer gnus-article-buffer)
8546       (widen)
8547       (if treated
8548           (progn
8549             (gnus-article-show-all-headers)
8550             (gnus-summary-search-article-position-point regexp backward))
8551         (goto-char (if backward (point-max) (point-min))))
8552       (while (not found)
8553         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8554         (if (if backward
8555                 (re-search-backward regexp nil t)
8556               (re-search-forward regexp nil t))
8557             ;; We found the regexp.
8558             (progn
8559               (gnus-summary-search-article-highlight-matched-text
8560                backward treated (string-match "^\\^X-Face:" regexp))
8561               (setq found 'found)
8562               (forward-line
8563                (/ (- 2 (window-height
8564                         (get-buffer-window gnus-article-buffer t)))
8565                   2))
8566               (set-window-start
8567                (get-buffer-window (current-buffer))
8568                (point))
8569               (set-buffer sum)
8570               (setq point (point)))
8571           ;; We didn't find it, so we go to the next article.
8572           (set-buffer sum)
8573           (setq found 'not)
8574           (while (eq found 'not)
8575             (if (not (if backward (gnus-summary-find-prev)
8576                        (gnus-summary-find-next)))
8577                 ;; No more articles.
8578                 (setq found t)
8579               ;; Select the next article and adjust point.
8580               (unless (gnus-summary-article-sparse-p
8581                        (gnus-summary-article-number))
8582                 (setq found nil)
8583                 (let ((gnus-inhibit-treatment t))
8584                   (gnus-summary-select-article))
8585                 (setq treated nil)
8586                 (set-buffer gnus-article-buffer)
8587                 (widen)
8588                 (goto-char (if backward (point-max) (point-min))))))))
8589       (gnus-message 7 ""))
8590     ;; Return whether we found the regexp.
8591     (when (eq found 'found)
8592       (goto-char point)
8593       (gnus-summary-show-thread)
8594       (gnus-summary-goto-subject gnus-current-article)
8595       (gnus-summary-position-point)
8596       t)))
8597
8598 (defun gnus-find-matching-articles (header regexp)
8599   "Return a list of all articles that match REGEXP on HEADER.
8600 This search includes all articles in the current group that Gnus has
8601 fetched headers for, whether they are displayed or not."
8602   (let ((articles nil)
8603         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8604         (case-fold-search t))
8605     (dolist (header gnus-newsgroup-headers)
8606       (when (string-match regexp (funcall func header))
8607         (push (mail-header-number header) articles)))
8608     (nreverse articles)))
8609
8610 (defun gnus-summary-find-matching (header regexp &optional backward unread
8611                                           not-case-fold not-matching)
8612   "Return a list of all articles that match REGEXP on HEADER.
8613 The search stars on the current article and goes forwards unless
8614 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8615 If UNREAD is non-nil, only unread articles will
8616 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8617 in the comparisons. If NOT-MATCHING, return a list of all articles that
8618 not match REGEXP on HEADER."
8619   (let ((case-fold-search (not not-case-fold))
8620         articles d func)
8621     (if (consp header)
8622         (if (eq (car header) 'extra)
8623             (setq func
8624                   `(lambda (h)
8625                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8626                          "")))
8627           (error "%s is an invalid header" header))
8628       (unless (fboundp (intern (concat "mail-header-" header)))
8629         (error "%s is not a valid header" header))
8630       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8631     (dolist (d (if (eq backward 'all)
8632                    gnus-newsgroup-data
8633                  (gnus-data-find-list
8634                   (gnus-summary-article-number)
8635                   (gnus-data-list backward))))
8636       (when (and (or (not unread)       ; We want all articles...
8637                      (gnus-data-unread-p d)) ; Or just unreads.
8638                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8639                  (if not-matching
8640                      (not (string-match
8641                            regexp
8642                            (funcall func (gnus-data-header d))))
8643                    (string-match regexp
8644                                  (funcall func (gnus-data-header d)))))
8645         (push (gnus-data-number d) articles))) ; Success!
8646     (nreverse articles)))
8647
8648 (defun gnus-summary-execute-command (header regexp command &optional backward)
8649   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8650 If HEADER is an empty string (or nil), the match is done on the entire
8651 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8652   (interactive
8653    (list (let ((completion-ignore-case t))
8654            (completing-read
8655             "Header name: "
8656             (mapcar (lambda (header) (list (format "%s" header)))
8657                     (append
8658                      '("Number" "Subject" "From" "Lines" "Date"
8659                        "Message-ID" "Xref" "References" "Body")
8660                      gnus-extra-headers))
8661             nil 'require-match))
8662          (read-string "Regexp: ")
8663          (read-key-sequence "Command: ")
8664          current-prefix-arg))
8665   (when (equal header "Body")
8666     (setq header ""))
8667   ;; Hidden thread subtrees must be searched as well.
8668   (gnus-summary-show-all-threads)
8669   ;; We don't want to change current point nor window configuration.
8670   (save-excursion
8671     (save-window-excursion
8672       (let (gnus-visual
8673             gnus-treat-strip-trailing-blank-lines
8674             gnus-treat-strip-leading-blank-lines
8675             gnus-treat-strip-multiple-blank-lines
8676             gnus-treat-hide-boring-headers
8677             gnus-treat-fold-newsgroups
8678             gnus-article-prepare-hook)
8679         (gnus-message 6 "Executing %s..." (key-description command))
8680         ;; We'd like to execute COMMAND interactively so as to give arguments.
8681         (gnus-execute header regexp
8682                       `(call-interactively ',(key-binding command))
8683                       backward)
8684         (gnus-message 6 "Executing %s...done" (key-description command))))))
8685
8686 (defun gnus-summary-beginning-of-article ()
8687   "Scroll the article back to the beginning."
8688   (interactive)
8689   (gnus-summary-select-article)
8690   (gnus-configure-windows 'article)
8691   (gnus-eval-in-buffer-window gnus-article-buffer
8692     (widen)
8693     (goto-char (point-min))
8694     (when gnus-break-pages
8695       (gnus-narrow-to-page))))
8696
8697 (defun gnus-summary-end-of-article ()
8698   "Scroll to the end of the article."
8699   (interactive)
8700   (gnus-summary-select-article)
8701   (gnus-configure-windows 'article)
8702   (gnus-eval-in-buffer-window gnus-article-buffer
8703     (widen)
8704     (goto-char (point-max))
8705     (recenter -3)
8706     (when gnus-break-pages
8707       (when (re-search-backward page-delimiter nil t)
8708         (narrow-to-region (match-end 0) (point-max)))
8709       (gnus-narrow-to-page))))
8710
8711 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8712   "Truncate to LEN and quote all \"(\"'s in STRING."
8713   (gnus-replace-in-string (if (and len (> (length string) len))
8714                               (substring string 0 len)
8715                             string)
8716                           "[()]" "\\\\\\&"))
8717
8718 (defun gnus-summary-print-article (&optional filename n)
8719   "Generate and print a PostScript image of the process-marked (mail) articles.
8720
8721 If used interactively, print the current article if none are
8722 process-marked.  With prefix arg, prompt the user for the name of the
8723 file to save in.
8724
8725 When used from Lisp, accept two optional args FILENAME and N.  N means
8726 to print the next N articles.  If N is negative, print the N previous
8727 articles.  If N is nil and articles have been marked with the process
8728 mark, print these instead.
8729
8730 If the optional first argument FILENAME is nil, send the image to the
8731 printer.  If FILENAME is a string, save the PostScript image in a file with
8732 that name.  If FILENAME is a number, prompt the user for the name of the file
8733 to save in."
8734   (interactive (list (ps-print-preprint current-prefix-arg)))
8735   (dolist (article (gnus-summary-work-articles n))
8736     (gnus-summary-select-article nil nil 'pseudo article)
8737     (gnus-eval-in-buffer-window gnus-article-buffer
8738       (gnus-print-buffer))
8739     (gnus-summary-remove-process-mark article))
8740   (ps-despool filename))
8741
8742 (defun gnus-print-buffer ()
8743   (let ((buffer (generate-new-buffer " *print*")))
8744     (unwind-protect
8745         (progn
8746           (copy-to-buffer buffer (point-min) (point-max))
8747           (set-buffer buffer)
8748           (gnus-remove-text-with-property 'gnus-decoration)
8749           (when (gnus-visual-p 'article-highlight 'highlight)
8750             ;; Copy-to-buffer doesn't copy overlay.  So redo
8751             ;; highlight.
8752             (let ((gnus-article-buffer buffer))
8753               (gnus-article-highlight-citation t)
8754               (gnus-article-highlight-signature)
8755               (gnus-article-emphasize)
8756               (gnus-article-delete-invisible-text)))
8757           (let ((ps-left-header
8758                  (list
8759                   (concat "("
8760                           (gnus-summary-print-truncate-and-quote
8761                            (mail-header-subject gnus-current-headers)
8762                            66) ")")
8763                   (concat "("
8764                           (gnus-summary-print-truncate-and-quote
8765                            (mail-header-from gnus-current-headers)
8766                            45) ")")))
8767                 (ps-right-header
8768                  (list
8769                   "/pagenumberstring load"
8770                   (concat "("
8771                           (mail-header-date gnus-current-headers) ")"))))
8772             (gnus-run-hooks 'gnus-ps-print-hook)
8773             (save-excursion
8774               (if window-system
8775                   (ps-spool-buffer-with-faces)
8776                 (ps-spool-buffer)))))
8777       (kill-buffer buffer))))
8778
8779 (defun gnus-summary-show-article (&optional arg)
8780   "Force redisplaying of the current article.
8781 If ARG (the prefix) is a number, show the article with the charset
8782 defined in `gnus-summary-show-article-charset-alist', or the charset
8783 input.
8784 If ARG (the prefix) is non-nil and not a number, show the raw article
8785 without any article massaging functions being run.  Normally, the key
8786 strokes are `C-u g'."
8787   (interactive "P")
8788   (cond
8789    ((numberp arg)
8790     (gnus-summary-show-article t)
8791     (let* ((gnus-newsgroup-charset
8792             (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8793                 (mm-read-coding-system
8794                  "View as charset: " ;; actually it is coding system.
8795                  (save-excursion
8796                    (set-buffer gnus-article-buffer)
8797                    (mm-detect-coding-region (point) (point-max))))))
8798            (default-mime-charset gnus-newsgroup-charset)
8799            (gnus-newsgroup-ignored-charsets 'gnus-all))
8800       (gnus-summary-select-article nil 'force)
8801       (let ((deps gnus-newsgroup-dependencies)
8802             head header lines)
8803         (save-excursion
8804           (set-buffer gnus-original-article-buffer)
8805           (save-restriction
8806             (message-narrow-to-head)
8807             (setq head (buffer-string))
8808             (goto-char (point-min))
8809             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8810               (goto-char (point-max))
8811               (widen)
8812               (setq lines (1- (count-lines (point) (point-max))))))
8813           (with-temp-buffer
8814             (insert (format "211 %d Article retrieved.\n"
8815                             (cdr gnus-article-current)))
8816             (insert head)
8817             (if lines (insert (format "Lines: %d\n" lines)))
8818             (insert ".\n")
8819             (let ((nntp-server-buffer (current-buffer)))
8820               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8821         (gnus-data-set-header
8822          (gnus-data-find (cdr gnus-article-current))
8823          header)
8824         (gnus-summary-update-article-line
8825          (cdr gnus-article-current) header)
8826         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8827           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8828    ((not arg)
8829     ;; Select the article the normal way.
8830     (gnus-summary-select-article nil 'force))
8831    (t
8832     ;; We have to require this here to make sure that the following
8833     ;; dynamic binding isn't shadowed by autoloading.
8834     (require 'gnus-async)
8835     (require 'gnus-art)
8836     ;; Bind the article treatment functions to nil.
8837     (let ((gnus-have-all-headers t)
8838           gnus-article-prepare-hook
8839           gnus-article-decode-hook
8840           gnus-break-pages
8841           gnus-show-mime
8842           (gnus-inhibit-treatment t))
8843       (gnus-summary-select-article nil 'force))))
8844   (gnus-summary-goto-subject gnus-current-article)
8845   (gnus-summary-position-point))
8846
8847 (defun gnus-summary-show-raw-article ()
8848   "Show the raw article without any article massaging functions being run."
8849   (interactive)
8850   (gnus-summary-show-article t))
8851
8852 (defun gnus-summary-verbose-headers (&optional arg)
8853   "Toggle permanent full header display.
8854 If ARG is a positive number, turn header display on.
8855 If ARG is a negative number, turn header display off."
8856   (interactive "P")
8857   (setq gnus-show-all-headers
8858         (cond ((or (not (numberp arg))
8859                    (zerop arg))
8860                (not gnus-show-all-headers))
8861               ((natnump arg)
8862                t)))
8863   (gnus-summary-show-article))
8864
8865 (defun gnus-summary-toggle-header (&optional arg)
8866   "Show the headers if they are hidden, or hide them if they are shown.
8867 If ARG is a positive number, show the entire header.
8868 If ARG is a negative number, hide the unwanted header lines."
8869   (interactive "P")
8870   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8871                      (get-buffer-window gnus-article-buffer t))))
8872     (with-current-buffer gnus-article-buffer
8873       (widen)
8874       (article-narrow-to-head)
8875       (let* ((buffer-read-only nil)
8876              (inhibit-point-motion-hooks t)
8877              (hidden (if (numberp arg)
8878                          (>= arg 0)
8879                        (gnus-article-hidden-text-p 'headers)))
8880              s e)
8881         (delete-region (point-min) (point-max))
8882         (with-current-buffer gnus-original-article-buffer
8883           (goto-char (setq s (point-min)))
8884           (setq e (if (search-forward "\n\n" nil t)
8885                       (1- (point))
8886                     (point-max))))
8887         (insert-buffer-substring gnus-original-article-buffer s e)
8888         ;; In T-gnus, gnus-article-decode-hook doesn't contain
8889         ;; article-decode-encoded-words by default.
8890         (article-decode-encoded-words)
8891         (run-hooks 'gnus-article-decode-hook)
8892         (if hidden
8893             (let ((gnus-treat-hide-headers nil)
8894                   (gnus-treat-hide-boring-headers nil))
8895               (gnus-delete-wash-type 'headers)
8896               (gnus-treat-article 'head))
8897           (gnus-treat-article 'head))
8898         (widen)
8899         (if window
8900             (set-window-start window (goto-char (point-min))))
8901         (if gnus-break-pages
8902             (gnus-narrow-to-page)
8903           (when (gnus-visual-p 'page-marker)
8904             (let ((buffer-read-only nil))
8905               (gnus-remove-text-with-property 'gnus-prev)
8906               (gnus-remove-text-with-property 'gnus-next))))
8907         (gnus-set-mode-line 'article)))))
8908
8909 (defun gnus-summary-show-all-headers ()
8910   "Make all header lines visible."
8911   (interactive)
8912   (gnus-summary-toggle-header 1))
8913
8914 (defun gnus-summary-toggle-mime (&optional arg)
8915   "Toggle MIME processing.
8916 If ARG is a positive number, turn MIME processing on."
8917   (interactive "P")
8918   (setq gnus-show-mime
8919         (if (null arg)
8920             (not gnus-show-mime)
8921           (> (prefix-numeric-value arg) 0)))
8922   (gnus-summary-select-article t 'force))
8923
8924 (defun gnus-summary-caesar-message (&optional arg)
8925   "Caesar rotate the current article by 13.
8926 The numerical prefix specifies how many places to rotate each letter
8927 forward."
8928   (interactive "P")
8929   (gnus-summary-select-article)
8930   (let ((mail-header-separator ""))
8931     (gnus-eval-in-buffer-window gnus-article-buffer
8932       (save-restriction
8933         (widen)
8934         (let ((start (window-start))
8935               buffer-read-only)
8936           (message-caesar-buffer-body arg)
8937           (set-window-start (get-buffer-window (current-buffer)) start))))))
8938
8939 (autoload 'unmorse-region "morse"
8940   "Convert morse coded text in region to ordinary ASCII text."
8941   t)
8942
8943 (defun gnus-summary-morse-message (&optional arg)
8944   "Morse decode the current article."
8945   (interactive "P")
8946   (gnus-summary-select-article)
8947   (let ((mail-header-separator ""))
8948     (gnus-eval-in-buffer-window gnus-article-buffer
8949       (save-excursion
8950         (save-restriction
8951           (widen)
8952           (let ((pos (window-start))
8953                 buffer-read-only)
8954             (goto-char (point-min))
8955             (when (message-goto-body)
8956               (gnus-narrow-to-body))
8957             (goto-char (point-min))
8958             (while (re-search-forward "·" (point-max) t)
8959               (replace-match "."))
8960             (unmorse-region (point-min) (point-max))
8961             (widen)
8962             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8963
8964 (defun gnus-summary-stop-page-breaking ()
8965   "Stop page breaking in the current article."
8966   (interactive)
8967   (gnus-summary-select-article)
8968   (gnus-eval-in-buffer-window gnus-article-buffer
8969     (widen)
8970     (when (gnus-visual-p 'page-marker)
8971       (let ((buffer-read-only nil))
8972         (gnus-remove-text-with-property 'gnus-prev)
8973         (gnus-remove-text-with-property 'gnus-next))
8974       (setq gnus-page-broken nil))))
8975
8976 (defun gnus-summary-move-article (&optional n to-newsgroup
8977                                             select-method action)
8978   "Move the current article to a different newsgroup.
8979 If N is a positive number, move the N next articles.
8980 If N is a negative number, move the N previous articles.
8981 If N is nil and any articles have been marked with the process mark,
8982 move those articles instead.
8983 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8984 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8985 re-spool using this method.
8986
8987 When called interactively with TO-NEWSGROUP being nil, the value of
8988 the variable `gnus-move-split-methods' is used for finding a default
8989 for the target newsgroup.
8990
8991 For this function to work, both the current newsgroup and the
8992 newsgroup that you want to move to have to support the `request-move'
8993 and `request-accept' functions.
8994
8995 ACTION can be either `move' (the default), `crosspost' or `copy'."
8996   (interactive "P")
8997   (unless action
8998     (setq action 'move))
8999   ;; Check whether the source group supports the required functions.
9000   (cond ((and (eq action 'move)
9001               (not (gnus-check-backend-function
9002                     'request-move-article gnus-newsgroup-name)))
9003          (error "The current group does not support article moving"))
9004         ((and (eq action 'crosspost)
9005               (not (gnus-check-backend-function
9006                     'request-replace-article gnus-newsgroup-name)))
9007          (error "The current group does not support article editing")))
9008   (let ((articles (gnus-summary-work-articles n))
9009         (prefix (if (gnus-check-backend-function
9010                      'request-move-article gnus-newsgroup-name)
9011                     (funcall gnus-move-group-prefix-function
9012                              gnus-newsgroup-name)
9013                   ""))
9014         (names '((move "Move" "Moving")
9015                  (copy "Copy" "Copying")
9016                  (crosspost "Crosspost" "Crossposting")))
9017         (copy-buf (save-excursion
9018                     (nnheader-set-temp-buffer " *copy article*")))
9019         art-group to-method new-xref article to-groups)
9020     (unless (assq action names)
9021       (error "Unknown action %s" action))
9022     ;; Read the newsgroup name.
9023     (when (and (not to-newsgroup)
9024                (not select-method))
9025       (if (and gnus-move-split-methods
9026                (not
9027                 (and (memq gnus-current-article articles)
9028                      (gnus-buffer-live-p gnus-original-article-buffer))))
9029           ;; When `gnus-move-split-methods' is non-nil, we have to
9030           ;; select an article to give `gnus-read-move-group-name' an
9031           ;; opportunity to suggest an appropriate default.  However,
9032           ;; we needn't render or mark the article.
9033           (let ((gnus-display-mime-function nil)
9034                 (gnus-article-prepare-hook nil)
9035                 (gnus-mark-article-hook nil))
9036             (gnus-summary-select-article nil nil nil (car articles))))
9037       (setq to-newsgroup
9038             (gnus-read-move-group-name
9039              (cadr (assq action names))
9040              (symbol-value (intern (format "gnus-current-%s-group" action)))
9041              articles prefix))
9042       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
9043     (setq to-method (or select-method
9044                         (gnus-server-to-method
9045                          (gnus-group-method to-newsgroup))))
9046     ;; Check the method we are to move this article to...
9047     (unless (gnus-check-backend-function
9048              'request-accept-article (car to-method))
9049       (error "%s does not support article copying" (car to-method)))
9050     (unless (gnus-check-server to-method)
9051       (error "Can't open server %s" (car to-method)))
9052     (gnus-message 6 "%s to %s: %s..."
9053                   (caddr (assq action names))
9054                   (or (car select-method) to-newsgroup) articles)
9055     (while articles
9056       (setq article (pop articles))
9057       (setq
9058        art-group
9059        (cond
9060         ;; Move the article.
9061         ((eq action 'move)
9062          ;; Remove this article from future suppression.
9063          (gnus-dup-unsuppress-article article)
9064          (gnus-request-move-article
9065           article                       ; Article to move
9066           gnus-newsgroup-name           ; From newsgroup
9067           (nth 1 (gnus-find-method-for-group
9068                   gnus-newsgroup-name)) ; Server
9069           (list 'gnus-request-accept-article
9070                 to-newsgroup (list 'quote select-method)
9071                 (not articles) t)       ; Accept form
9072           (not articles)))              ; Only save nov last time
9073         ;; Copy the article.
9074         ((eq action 'copy)
9075          (save-excursion
9076            (set-buffer copy-buf)
9077            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9078              (gnus-request-accept-article
9079               to-newsgroup select-method (not articles) t))))
9080         ;; Crosspost the article.
9081         ((eq action 'crosspost)
9082          (let ((xref (message-tokenize-header
9083                       (mail-header-xref (gnus-summary-article-header article))
9084                       " ")))
9085            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9086                                   ":" (number-to-string article)))
9087            (unless xref
9088              (setq xref (list (system-name))))
9089            (setq new-xref
9090                  (concat
9091                   (mapconcat 'identity
9092                              (delete "Xref:" (delete new-xref xref))
9093                              " ")
9094                   " " new-xref))
9095            (save-excursion
9096              (set-buffer copy-buf)
9097              ;; First put the article in the destination group.
9098              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9099              (when (consp (setq art-group
9100                                 (gnus-request-accept-article
9101                                  to-newsgroup select-method (not articles))))
9102                (setq new-xref (concat new-xref " " (car art-group)
9103                                       ":"
9104                                       (number-to-string (cdr art-group))))
9105                ;; Now we have the new Xrefs header, so we insert
9106                ;; it and replace the new article.
9107                (nnheader-replace-header "Xref" new-xref)
9108                (gnus-request-replace-article
9109                 (cdr art-group) to-newsgroup (current-buffer))
9110                art-group))))))
9111       (cond
9112        ((not art-group)
9113         (gnus-message 1 "Couldn't %s article %s: %s"
9114                       (cadr (assq action names)) article
9115                       (nnheader-get-report (car to-method))))
9116        ((eq art-group 'junk)
9117         (when (eq action 'move)
9118           (gnus-summary-mark-article article gnus-canceled-mark)
9119           (gnus-message 4 "Deleted article %s" article)
9120           ;; run the delete hook
9121           (run-hook-with-args 'gnus-summary-article-delete-hook
9122                               action
9123                               (gnus-data-header
9124                                (assoc article (gnus-data-list nil)))
9125                               gnus-newsgroup-name nil
9126                               select-method)))
9127        (t
9128         (let* ((pto-group (gnus-group-prefixed-name
9129                            (car art-group) to-method))
9130                (info (gnus-get-info pto-group))
9131                (to-group (gnus-info-group info))
9132                to-marks)
9133           ;; Update the group that has been moved to.
9134           (when (and info
9135                      (memq action '(move copy)))
9136             (unless (member to-group to-groups)
9137               (push to-group to-groups))
9138
9139             (unless (memq article gnus-newsgroup-unreads)
9140               (push 'read to-marks)
9141               (gnus-info-set-read
9142                info (gnus-add-to-range (gnus-info-read info)
9143                                        (list (cdr art-group)))))
9144
9145             ;; See whether the article is to be put in the cache.
9146             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9147                              gnus-article-mark-lists
9148                            (delete '(expirable . expire)
9149                                    (copy-sequence gnus-article-mark-lists))))
9150                   (to-article (cdr art-group)))
9151
9152               ;; Enter the article into the cache in the new group,
9153               ;; if that is required.
9154               (when gnus-use-cache
9155                 (gnus-cache-possibly-enter-article
9156                  to-group to-article
9157                  (let ((header (copy-sequence
9158                                 (gnus-summary-article-header article))))
9159                    (mail-header-set-number header to-article)
9160                    header)
9161                  (memq article gnus-newsgroup-marked)
9162                  (memq article gnus-newsgroup-dormant)
9163                  (memq article gnus-newsgroup-unreads)))
9164
9165               (when gnus-preserve-marks
9166                 ;; Copy any marks over to the new group.
9167                 (when (and (equal to-group gnus-newsgroup-name)
9168                            (not (memq article gnus-newsgroup-unreads)))
9169                   ;; Mark this article as read in this group.
9170                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9171                   (setcdr (gnus-active to-group) to-article)
9172                   (setcdr gnus-newsgroup-active to-article))
9173
9174                 (while marks
9175                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9176                     (when (memq article (symbol-value
9177                                          (intern (format "gnus-newsgroup-%s"
9178                                                          (caar marks)))))
9179                       (push (cdar marks) to-marks)
9180                       ;; If the other group is the same as this group,
9181                       ;; then we have to add the mark to the list.
9182                       (when (equal to-group gnus-newsgroup-name)
9183                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9184                              (cons to-article
9185                                    (symbol-value
9186                                     (intern (format "gnus-newsgroup-%s"
9187                                                     (caar marks)))))))
9188                       ;; Copy the marks to other group.
9189                       (gnus-add-marked-articles
9190                        to-group (cdar marks) (list to-article) info)))
9191                   (setq marks (cdr marks)))
9192
9193                 (gnus-request-set-mark
9194                  to-group (list (list (list to-article) 'add to-marks))))
9195
9196               (gnus-dribble-enter
9197                (concat "(gnus-group-set-info '"
9198                        (gnus-prin1-to-string (gnus-get-info to-group))
9199                        ")"))))
9200
9201           ;; Update the Xref header in this article to point to
9202           ;; the new crossposted article we have just created.
9203           (when (eq action 'crosspost)
9204             (save-excursion
9205               (set-buffer copy-buf)
9206               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9207               (nnheader-replace-header "Xref" new-xref)
9208               (gnus-request-replace-article
9209                article gnus-newsgroup-name (current-buffer))))
9210
9211           ;; run the move/copy/crosspost/respool hook
9212           (run-hook-with-args 'gnus-summary-article-move-hook
9213                               action
9214                               (gnus-data-header
9215                                (assoc article (gnus-data-list nil)))
9216                               gnus-newsgroup-name
9217                               to-newsgroup
9218                               select-method))
9219
9220         ;;;!!!Why is this necessary?
9221         (set-buffer gnus-summary-buffer)
9222         
9223         (gnus-summary-goto-subject article)
9224         (when (eq action 'move)
9225           (gnus-summary-mark-article article gnus-canceled-mark))))
9226       (gnus-summary-remove-process-mark article))
9227     ;; Re-activate all groups that have been moved to.
9228     (save-excursion
9229       (set-buffer gnus-group-buffer)
9230       (let ((gnus-group-marked to-groups))
9231         (gnus-group-get-new-news-this-group nil t)))
9232
9233     (gnus-kill-buffer copy-buf)
9234     (gnus-summary-position-point)
9235     (gnus-set-mode-line 'summary)))
9236
9237 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9238   "Move the current article to a different newsgroup.
9239 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9240 When called interactively, if TO-NEWSGROUP is nil, use the value of
9241 the variable `gnus-move-split-methods' for finding a default target
9242 newsgroup.
9243 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9244 re-spool using this method."
9245   (interactive "P")
9246   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9247
9248 (defun gnus-summary-crosspost-article (&optional n)
9249   "Crosspost the current article to some other group."
9250   (interactive "P")
9251   (gnus-summary-move-article n nil nil 'crosspost))
9252
9253 (defcustom gnus-summary-respool-default-method nil
9254   "Default method type for respooling an article.
9255 If nil, use to the current newsgroup method."
9256   :type 'symbol
9257   :group 'gnus-summary-mail)
9258
9259 (defcustom gnus-summary-display-while-building nil
9260   "If non-nil, show and update the summary buffer as it's being built.
9261 If the value is t, update the buffer after every line is inserted.  If
9262 the value is an integer (N), update the display every N lines."
9263   :group 'gnus-thread
9264   :type '(choice (const :tag "off" nil)
9265                  number
9266                  (const :tag "frequently" t)))
9267
9268 (defun gnus-summary-respool-article (&optional n method)
9269   "Respool the current article.
9270 The article will be squeezed through the mail spooling process again,
9271 which means that it will be put in some mail newsgroup or other
9272 depending on `nnmail-split-methods'.
9273 If N is a positive number, respool the N next articles.
9274 If N is a negative number, respool the N previous articles.
9275 If N is nil and any articles have been marked with the process mark,
9276 respool those articles instead.
9277
9278 Respooling can be done both from mail groups and \"real\" newsgroups.
9279 In the former case, the articles in question will be moved from the
9280 current group into whatever groups they are destined to.  In the
9281 latter case, they will be copied into the relevant groups."
9282   (interactive
9283    (list current-prefix-arg
9284          (let* ((methods (gnus-methods-using 'respool))
9285                 (methname
9286                  (symbol-name (or gnus-summary-respool-default-method
9287                                   (car (gnus-find-method-for-group
9288                                         gnus-newsgroup-name)))))
9289                 (method
9290                  (gnus-completing-read-with-default
9291                   methname "What backend do you want to use when respooling?"
9292                   methods nil t nil 'gnus-mail-method-history))
9293                 ms)
9294            (cond
9295             ((zerop (length (setq ms (gnus-servers-using-backend
9296                                       (intern method)))))
9297              (list (intern method) ""))
9298             ((= 1 (length ms))
9299              (car ms))
9300             (t
9301              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9302                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9303                            ms-alist))))))))
9304   (unless method
9305     (error "No method given for respooling"))
9306   (if (assoc (symbol-name
9307               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9308              (gnus-methods-using 'respool))
9309       (gnus-summary-move-article n nil method)
9310     (gnus-summary-copy-article n nil method)))
9311
9312 (defun gnus-summary-import-article (file &optional edit)
9313   "Import an arbitrary file into a mail newsgroup."
9314   (interactive "fImport file: \nP")
9315   (let ((group gnus-newsgroup-name)
9316         (now (current-time))
9317         atts lines group-art)
9318     (unless (gnus-check-backend-function 'request-accept-article group)
9319       (error "%s does not support article importing" group))
9320     (or (file-readable-p file)
9321         (not (file-regular-p file))
9322         (error "Can't read %s" file))
9323     (save-excursion
9324       (set-buffer (gnus-get-buffer-create " *import file*"))
9325       (erase-buffer)
9326       (nnheader-insert-file-contents file)
9327       (goto-char (point-min))
9328       (if (nnheader-article-p)
9329           (save-restriction
9330             (goto-char (point-min))
9331             (search-forward "\n\n" nil t)
9332             (narrow-to-region (point-min) (1- (point)))
9333             (goto-char (point-min))
9334             (unless (re-search-forward "^date:" nil t)
9335               (goto-char (point-max))
9336               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9337         ;; This doesn't look like an article, so we fudge some headers.
9338         (setq atts (file-attributes file)
9339               lines (count-lines (point-min) (point-max)))
9340         (insert "From: " (read-string "From: ") "\n"
9341                 "Subject: " (read-string "Subject: ") "\n"
9342                 "Date: " (message-make-date (nth 5 atts)) "\n"
9343                 "Message-ID: " (message-make-message-id) "\n"
9344                 "Lines: " (int-to-string lines) "\n"
9345                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9346       (setq group-art (gnus-request-accept-article group nil t))
9347       (kill-buffer (current-buffer)))
9348     (setq gnus-newsgroup-active (gnus-activate-group group))
9349     (forward-line 1)
9350     (gnus-summary-goto-article (cdr group-art) nil t)
9351     (when edit
9352       (gnus-summary-edit-article))))
9353
9354 (defun gnus-summary-create-article ()
9355   "Create an article in a mail newsgroup."
9356   (interactive)
9357   (let ((group gnus-newsgroup-name)
9358         (now (current-time))
9359         group-art)
9360     (unless (gnus-check-backend-function 'request-accept-article group)
9361       (error "%s does not support article importing" group))
9362     (save-excursion
9363       (set-buffer (gnus-get-buffer-create " *import file*"))
9364       (erase-buffer)
9365       (goto-char (point-min))
9366       ;; This doesn't look like an article, so we fudge some headers.
9367       (insert "From: " (read-string "From: ") "\n"
9368               "Subject: " (read-string "Subject: ") "\n"
9369               "Date: " (message-make-date now) "\n"
9370               "Message-ID: " (message-make-message-id) "\n")
9371       (setq group-art (gnus-request-accept-article group nil t))
9372       (kill-buffer (current-buffer)))
9373     (setq gnus-newsgroup-active (gnus-activate-group group))
9374     (forward-line 1)
9375     (gnus-summary-goto-article (cdr group-art) nil t)
9376     (gnus-summary-edit-article)))
9377
9378 (defun gnus-summary-article-posted-p ()
9379   "Say whether the current (mail) article is available from news as well.
9380 This will be the case if the article has both been mailed and posted."
9381   (interactive)
9382   (let ((id (mail-header-references (gnus-summary-article-header)))
9383         (gnus-override-method (car (gnus-refer-article-methods))))
9384     (if (gnus-request-head id "")
9385         (gnus-message 2 "The current message was found on %s"
9386                       gnus-override-method)
9387       (gnus-message 2 "The current message couldn't be found on %s"
9388                     gnus-override-method)
9389       nil)))
9390
9391 (defun gnus-summary-expire-articles (&optional now)
9392   "Expire all articles that are marked as expirable in the current group."
9393   (interactive)
9394   (when (and (not gnus-group-is-exiting-without-update-p)
9395              (gnus-check-backend-function
9396               'request-expire-articles gnus-newsgroup-name))
9397     ;; This backend supports expiry.
9398     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9399            (expirable (if total
9400                           (progn
9401                             ;; We need to update the info for
9402                             ;; this group for `gnus-list-of-read-articles'
9403                             ;; to give us the right answer.
9404                             (gnus-run-hooks 'gnus-exit-group-hook)
9405                             (gnus-summary-update-info)
9406                             (gnus-list-of-read-articles gnus-newsgroup-name))
9407                         (setq gnus-newsgroup-expirable
9408                               (sort gnus-newsgroup-expirable '<))))
9409            (expiry-wait (if now 'immediate
9410                           (gnus-group-find-parameter
9411                            gnus-newsgroup-name 'expiry-wait)))
9412            (nnmail-expiry-target
9413             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9414                 nnmail-expiry-target))
9415            es)
9416       (when expirable
9417         ;; There are expirable articles in this group, so we run them
9418         ;; through the expiry process.
9419         (gnus-message 6 "Expiring articles...")
9420         (unless (gnus-check-group gnus-newsgroup-name)
9421           (error "Can't open server for %s" gnus-newsgroup-name))
9422         ;; The list of articles that weren't expired is returned.
9423         (save-excursion
9424           (if expiry-wait
9425               (let ((nnmail-expiry-wait-function nil)
9426                     (nnmail-expiry-wait expiry-wait))
9427                 (setq es (gnus-request-expire-articles
9428                           expirable gnus-newsgroup-name)))
9429             (setq es (gnus-request-expire-articles
9430                       expirable gnus-newsgroup-name)))
9431           (unless total
9432             (setq gnus-newsgroup-expirable es))
9433           ;; We go through the old list of expirable, and mark all
9434           ;; really expired articles as nonexistent.
9435           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
9436             (let ((gnus-use-cache nil))
9437               (dolist (article expirable)
9438                 (when (and (not (memq article es))
9439                            (gnus-data-find article))
9440                   (gnus-summary-mark-article article gnus-canceled-mark)
9441                   (run-hook-with-args 'gnus-summary-article-expire-hook
9442                                       'delete
9443                                       (gnus-data-header
9444                                        (assoc article (gnus-data-list nil)))
9445                                       gnus-newsgroup-name
9446                                       nil
9447                                       nil))))))
9448         (gnus-message 6 "Expiring articles...done")))))
9449
9450 (defun gnus-summary-expire-articles-now ()
9451   "Expunge all expirable articles in the current group.
9452 This means that *all* articles that are marked as expirable will be
9453 deleted forever, right now."
9454   (interactive)
9455   (or gnus-expert-user
9456       (gnus-yes-or-no-p
9457        "Are you really, really, really sure you want to delete all these messages? ")
9458       (error "Phew!"))
9459   (gnus-summary-expire-articles t))
9460
9461 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9462 (defun gnus-summary-delete-article (&optional n)
9463   "Delete the N next (mail) articles.
9464 This command actually deletes articles.  This is not a marking
9465 command.  The article will disappear forever from your life, never to
9466 return.
9467
9468 If N is negative, delete backwards.
9469 If N is nil and articles have been marked with the process mark,
9470 delete these instead.
9471
9472 If `gnus-novice-user' is non-nil you will be asked for
9473 confirmation before the articles are deleted."
9474   (interactive "P")
9475   (unless (gnus-check-backend-function 'request-expire-articles
9476                                        gnus-newsgroup-name)
9477     (error "The current newsgroup does not support article deletion"))
9478   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9479     (error "Couldn't open server"))
9480   ;; Compute the list of articles to delete.
9481   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9482         (nnmail-expiry-target 'delete)
9483         not-deleted)
9484     (if (and gnus-novice-user
9485              (not (gnus-yes-or-no-p
9486                    (format "Do you really want to delete %s forever? "
9487                            (if (> (length articles) 1)
9488                                (format "these %s articles" (length articles))
9489                              "this article")))))
9490         ()
9491       ;; Delete the articles.
9492       (setq not-deleted (gnus-request-expire-articles
9493                          articles gnus-newsgroup-name 'force))
9494       (while articles
9495         (gnus-summary-remove-process-mark (car articles))
9496         ;; The backend might not have been able to delete the article
9497         ;; after all.
9498         (unless (memq (car articles) not-deleted)
9499           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9500         (let* ((article (car articles))
9501                (id (mail-header-id (gnus-data-header
9502                                     (assoc article (gnus-data-list nil))))))
9503           (run-hook-with-args 'gnus-summary-article-delete-hook
9504                               'delete id gnus-newsgroup-name nil
9505                               nil))
9506         (setq articles (cdr articles)))
9507       (when not-deleted
9508         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9509     (gnus-summary-position-point)
9510     (gnus-set-mode-line 'summary)
9511     not-deleted))
9512
9513 (defun gnus-summary-edit-article (&optional force)
9514   "Edit the current article.
9515 This will have permanent effect only in mail groups.
9516 If FORCE is non-nil, allow editing of articles even in read-only
9517 groups."
9518   (interactive "P")
9519   (save-excursion
9520     (set-buffer gnus-summary-buffer)
9521     (let ((mail-parse-charset gnus-newsgroup-charset)
9522           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9523       (gnus-set-global-variables)
9524       (when (and (not force)
9525                  (gnus-group-read-only-p))
9526         (error "The current newsgroup does not support article editing"))
9527       (gnus-summary-show-article t)
9528       (gnus-article-edit-article
9529        'ignore
9530        `(lambda (no-highlight)
9531           (let ((mail-parse-charset ',gnus-newsgroup-charset)
9532                 (message-options message-options)
9533                 (message-options-set-recipient)
9534                 (mail-parse-ignored-charsets
9535                  ',gnus-newsgroup-ignored-charsets))
9536             (gnus-summary-edit-article-done
9537              ,(or (mail-header-references gnus-current-headers) "")
9538              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
9539
9540 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9541
9542 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9543                                                  no-highlight)
9544   "Make edits to the current article permanent."
9545   (interactive)
9546   (save-excursion
9547     ;; The buffer restriction contains the entire article if it exists.
9548     (when (article-goto-body)
9549       (let ((lines (count-lines (point) (point-max)))
9550             (length (- (point-max) (point)))
9551             (case-fold-search t)
9552             (body (copy-marker (point))))
9553         (goto-char (point-min))
9554         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9555           (delete-region (match-beginning 1) (match-end 1))
9556           (insert (number-to-string length)))
9557         (goto-char (point-min))
9558         (when (re-search-forward
9559                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9560           (delete-region (match-beginning 1) (match-end 1))
9561           (insert (number-to-string length)))
9562         (goto-char (point-min))
9563         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9564           (delete-region (match-beginning 1) (match-end 1))
9565           (insert (number-to-string lines))))))
9566   ;; Replace the article.
9567   (let ((buf (current-buffer)))
9568     (with-temp-buffer
9569       (insert-buffer-substring buf)
9570
9571       (if (and (not read-only)
9572                (not (gnus-request-replace-article
9573                      (cdr gnus-article-current) (car gnus-article-current)
9574                      (current-buffer) t)))
9575           (error "Couldn't replace article")
9576         ;; Update the summary buffer.
9577         (if (and references
9578                  (equal (message-tokenize-header references " ")
9579                         (message-tokenize-header
9580                          (or (message-fetch-field "references") "") " ")))
9581             ;; We only have to update this line.
9582             (save-excursion
9583               (save-restriction
9584                 (message-narrow-to-head)
9585                 (let ((head (buffer-string))
9586                       header)
9587                   (with-temp-buffer
9588                     (insert (format "211 %d Article retrieved.\n"
9589                                     (cdr gnus-article-current)))
9590                     (insert head)
9591                     (insert ".\n")
9592                     (let ((nntp-server-buffer (current-buffer)))
9593                       (setq header (car (gnus-get-newsgroup-headers
9594                                          nil t))))
9595                     (save-excursion
9596                       (set-buffer gnus-summary-buffer)
9597                       (gnus-data-set-header
9598                        (gnus-data-find (cdr gnus-article-current))
9599                        header)
9600                       (gnus-summary-update-article-line
9601                        (cdr gnus-article-current) header)
9602                       (if (gnus-summary-goto-subject
9603                            (cdr gnus-article-current) nil t)
9604                           (gnus-summary-update-secondary-mark
9605                            (cdr gnus-article-current))))))))
9606           ;; Update threads.
9607           (set-buffer (or buffer gnus-summary-buffer))
9608           (gnus-summary-update-article (cdr gnus-article-current))
9609           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9610               (gnus-summary-update-secondary-mark
9611                (cdr gnus-article-current))))
9612         ;; Prettify the article buffer again.
9613         (unless no-highlight
9614           (save-excursion
9615             (set-buffer gnus-article-buffer)
9616             ;;;!!! Fix this -- article should be rehighlighted.
9617             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9618             (set-buffer gnus-original-article-buffer)
9619             (gnus-request-article
9620              (cdr gnus-article-current)
9621              (car gnus-article-current) (current-buffer))))
9622         ;; Prettify the summary buffer line.
9623         (when (gnus-visual-p 'summary-highlight 'highlight)
9624           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9625
9626 (defun gnus-summary-edit-wash (key)
9627   "Perform editing command KEY in the article buffer."
9628   (interactive
9629    (list
9630     (progn
9631       (message "%s" (concat (this-command-keys) "- "))
9632       (read-char))))
9633   (message "")
9634   (gnus-summary-edit-article)
9635   (execute-kbd-macro (concat (this-command-keys) key))
9636   (gnus-article-edit-done))
9637
9638 ;;; Respooling
9639
9640 (defun gnus-summary-respool-query (&optional silent trace)
9641   "Query where the respool algorithm would put this article."
9642   (interactive)
9643   (let (gnus-mark-article-hook)
9644     (gnus-summary-select-article)
9645     (save-excursion
9646       (set-buffer gnus-original-article-buffer)
9647       (let ((groups (nnmail-article-group 'identity trace)))
9648         (unless silent
9649           (if groups
9650               (message "This message would go to %s"
9651                        (mapconcat 'car groups ", "))
9652             (message "This message would go to no groups"))
9653           groups)))))
9654
9655 (defun gnus-summary-respool-trace ()
9656   "Trace where the respool algorithm would put this article.
9657 Display a buffer showing all fancy splitting patterns which matched."
9658   (interactive)
9659   (gnus-summary-respool-query nil t))
9660
9661 ;; Summary marking commands.
9662
9663 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9664   "Mark articles which has the same subject as read, and then select the next.
9665 If UNMARK is positive, remove any kind of mark.
9666 If UNMARK is negative, tick articles."
9667   (interactive "P")
9668   (when unmark
9669     (setq unmark (prefix-numeric-value unmark)))
9670   (let ((count
9671          (gnus-summary-mark-same-subject
9672           (gnus-summary-article-subject) unmark)))
9673     ;; Select next unread article.  If auto-select-same mode, should
9674     ;; select the first unread article.
9675     (gnus-summary-next-article t (and gnus-auto-select-same
9676                                       (gnus-summary-article-subject)))
9677     (gnus-message 7 "%d article%s marked as %s"
9678                   count (if (= count 1) " is" "s are")
9679                   (if unmark "unread" "read"))))
9680
9681 (defun gnus-summary-kill-same-subject (&optional unmark)
9682   "Mark articles which has the same subject as read.
9683 If UNMARK is positive, remove any kind of mark.
9684 If UNMARK is negative, tick articles."
9685   (interactive "P")
9686   (when unmark
9687     (setq unmark (prefix-numeric-value unmark)))
9688   (let ((count
9689          (gnus-summary-mark-same-subject
9690           (gnus-summary-article-subject) unmark)))
9691     ;; If marked as read, go to next unread subject.
9692     (when (null unmark)
9693       ;; Go to next unread subject.
9694       (gnus-summary-next-subject 1 t))
9695     (gnus-message 7 "%d articles are marked as %s"
9696                   count (if unmark "unread" "read"))))
9697
9698 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9699   "Mark articles with same SUBJECT as read, and return marked number.
9700 If optional argument UNMARK is positive, remove any kinds of marks.
9701 If optional argument UNMARK is negative, mark articles as unread instead."
9702   (let ((count 1))
9703     (save-excursion
9704       (cond
9705        ((null unmark)                   ; Mark as read.
9706         (while (and
9707                 (progn
9708                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9709                   (gnus-summary-show-thread) t)
9710                 (gnus-summary-find-subject subject))
9711           (setq count (1+ count))))
9712        ((> unmark 0)                    ; Tick.
9713         (while (and
9714                 (progn
9715                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9716                   (gnus-summary-show-thread) t)
9717                 (gnus-summary-find-subject subject))
9718           (setq count (1+ count))))
9719        (t                               ; Mark as unread.
9720         (while (and
9721                 (progn
9722                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9723                   (gnus-summary-show-thread) t)
9724                 (gnus-summary-find-subject subject))
9725           (setq count (1+ count)))))
9726       (gnus-set-mode-line 'summary)
9727       ;; Return the number of marked articles.
9728       count)))
9729
9730 (defun gnus-summary-mark-as-processable (n &optional unmark)
9731   "Set the process mark on the next N articles.
9732 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9733 the process mark instead.  The difference between N and the actual
9734 number of articles marked is returned."
9735   (interactive "P")
9736   (if (and (null n) (gnus-region-active-p))
9737       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9738     (setq n (prefix-numeric-value n))
9739     (let ((backward (< n 0))
9740           (n (abs n)))
9741       (while (and
9742               (> n 0)
9743               (if unmark
9744                   (gnus-summary-remove-process-mark
9745                    (gnus-summary-article-number))
9746                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9747               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9748         (setq n (1- n)))
9749       (when (/= 0 n)
9750         (gnus-message 7 "No more articles"))
9751       (gnus-summary-recenter)
9752       (gnus-summary-position-point)
9753       n)))
9754
9755 (defun gnus-summary-unmark-as-processable (n)
9756   "Remove the process mark from the next N articles.
9757 If N is negative, unmark backward instead.  The difference between N and
9758 the actual number of articles unmarked is returned."
9759   (interactive "P")
9760   (gnus-summary-mark-as-processable n t))
9761
9762 (defun gnus-summary-unmark-all-processable ()
9763   "Remove the process mark from all articles."
9764   (interactive)
9765   (save-excursion
9766     (while gnus-newsgroup-processable
9767       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9768   (gnus-summary-position-point))
9769
9770 (defun gnus-summary-add-mark (article type)
9771   "Mark ARTICLE with a mark of TYPE."
9772   (let ((vtype (car (assq type gnus-article-mark-lists)))
9773         var)
9774     (if (not vtype)
9775         (error "No such mark type: %s" type)
9776       (setq var (intern (format "gnus-newsgroup-%s" type)))
9777       (set var (cons article (symbol-value var)))
9778       (if (memq type '(processable cached replied forwarded recent saved))
9779           (gnus-summary-update-secondary-mark article)
9780         ;;; !!! This is bogus.  We should find out what primary
9781         ;;; !!! mark we want to set.
9782         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9783
9784 (defun gnus-summary-mark-as-expirable (n)
9785   "Mark N articles forward as expirable.
9786 If N is negative, mark backward instead.  The difference between N and
9787 the actual number of articles marked is returned."
9788   (interactive "p")
9789   (gnus-summary-mark-forward n gnus-expirable-mark))
9790
9791 (defun gnus-summary-mark-as-spam (n)
9792   "Mark N articles forward as spam.
9793 If N is negative, mark backward instead.  The difference between N and
9794 the actual number of articles marked is returned."
9795   (interactive "p")
9796   (gnus-summary-mark-forward n gnus-spam-mark))
9797
9798 (defun gnus-summary-mark-article-as-replied (article)
9799   "Mark ARTICLE as replied to and update the summary line.
9800 ARTICLE can also be a list of articles."
9801   (interactive (list (gnus-summary-article-number)))
9802   (let ((articles (if (listp article) article (list article))))
9803     (dolist (article articles)
9804       (unless (numberp article)
9805         (error "%s is not a number" article))
9806       (push article gnus-newsgroup-replied)
9807       (let ((buffer-read-only nil))
9808         (when (gnus-summary-goto-subject article nil t)
9809           (gnus-summary-update-secondary-mark article))))))
9810
9811 (defun gnus-summary-mark-article-as-forwarded (article)
9812   "Mark ARTICLE as forwarded and update the summary line.
9813 ARTICLE can also be a list of articles."
9814   (let ((articles (if (listp article) article (list article))))
9815     (dolist (article articles)
9816       (push article gnus-newsgroup-forwarded)
9817       (let ((buffer-read-only nil))
9818         (when (gnus-summary-goto-subject article nil t)
9819           (gnus-summary-update-secondary-mark article))))))
9820
9821 (defun gnus-summary-set-bookmark (article)
9822   "Set a bookmark in current article."
9823   (interactive (list (gnus-summary-article-number)))
9824   (when (or (not (get-buffer gnus-article-buffer))
9825             (not gnus-current-article)
9826             (not gnus-article-current)
9827             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9828     (error "No current article selected"))
9829   ;; Remove old bookmark, if one exists.
9830   (gnus-pull article gnus-newsgroup-bookmarks)
9831   ;; Set the new bookmark, which is on the form
9832   ;; (article-number . line-number-in-body).
9833   (push
9834    (cons article
9835          (save-excursion
9836            (set-buffer gnus-article-buffer)
9837            (count-lines
9838             (min (point)
9839                  (save-excursion
9840                    (article-goto-body)
9841                    (point)))
9842             (point))))
9843    gnus-newsgroup-bookmarks)
9844   (gnus-message 6 "A bookmark has been added to the current article."))
9845
9846 (defun gnus-summary-remove-bookmark (article)
9847   "Remove the bookmark from the current article."
9848   (interactive (list (gnus-summary-article-number)))
9849   ;; Remove old bookmark, if one exists.
9850   (if (not (assq article gnus-newsgroup-bookmarks))
9851       (gnus-message 6 "No bookmark in current article.")
9852     (gnus-pull article gnus-newsgroup-bookmarks)
9853     (gnus-message 6 "Removed bookmark.")))
9854
9855 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9856 (defun gnus-summary-mark-as-dormant (n)
9857   "Mark N articles forward as dormant.
9858 If N is negative, mark backward instead.  The difference between N and
9859 the actual number of articles marked is returned."
9860   (interactive "p")
9861   (gnus-summary-mark-forward n gnus-dormant-mark))
9862
9863 (defun gnus-summary-set-process-mark (article)
9864   "Set the process mark on ARTICLE and update the summary line."
9865   (setq gnus-newsgroup-processable
9866         (cons article
9867               (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-remove-process-mark (article)
9874   "Remove the process mark from ARTICLE and update the summary line."
9875   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9876   (when (gnus-summary-goto-subject article)
9877     (gnus-summary-show-thread)
9878     (gnus-summary-goto-subject article)
9879     (gnus-summary-update-secondary-mark article)))
9880
9881 (defun gnus-summary-set-saved-mark (article)
9882   "Set the process mark on ARTICLE and update the summary line."
9883   (push article gnus-newsgroup-saved)
9884   (when (gnus-summary-goto-subject article)
9885     (gnus-summary-update-secondary-mark article)))
9886
9887 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9888   "Mark N articles as read forwards.
9889 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9890 The difference between N and the actual number of articles marked is
9891 returned.
9892 If NO-EXPIRE, auto-expiry will be inhibited."
9893   (interactive "p")
9894   (gnus-summary-show-thread)
9895   (let ((backward (< n 0))
9896         (gnus-summary-goto-unread
9897          (and gnus-summary-goto-unread
9898               (not (eq gnus-summary-goto-unread 'never))
9899               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9900                                     gnus-ticked-mark gnus-dormant-mark)))))
9901         (n (abs n))
9902         (mark (or mark gnus-del-mark)))
9903     (while (and (> n 0)
9904                 (gnus-summary-mark-article nil mark no-expire)
9905                 (zerop (gnus-summary-next-subject
9906                         (if backward -1 1)
9907                         (and gnus-summary-goto-unread
9908                              (not (eq gnus-summary-goto-unread 'never)))
9909                         t)))
9910       (setq n (1- n)))
9911     (when (/= 0 n)
9912       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9913     (gnus-summary-recenter)
9914     (gnus-summary-position-point)
9915     (gnus-set-mode-line 'summary)
9916     n))
9917
9918 (defun gnus-summary-mark-article-as-read (mark)
9919   "Mark the current article quickly as read with MARK."
9920   (let ((article (gnus-summary-article-number)))
9921     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9922     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9923     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9924     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9925     (push (cons article mark) gnus-newsgroup-reads)
9926     ;; Possibly remove from cache, if that is used.
9927     (when gnus-use-cache
9928       (gnus-cache-enter-remove-article article))
9929     ;; Allow the backend to change the mark.
9930     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9931     ;; Check for auto-expiry.
9932     (when (and gnus-newsgroup-auto-expire
9933                (memq mark gnus-auto-expirable-marks))
9934       (setq mark gnus-expirable-mark)
9935       ;; Let the backend know about the mark change.
9936       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9937       (push article gnus-newsgroup-expirable))
9938     ;; Set the mark in the buffer.
9939     (gnus-summary-update-mark mark 'unread)
9940     t))
9941
9942 (defun gnus-summary-mark-article-as-unread (mark)
9943   "Mark the current article quickly as unread with MARK."
9944   (let* ((article (gnus-summary-article-number))
9945          (old-mark (gnus-summary-article-mark article)))
9946     ;; Allow the backend to change the mark.
9947     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9948     (if (eq mark old-mark)
9949         t
9950       (if (<= article 0)
9951           (progn
9952             (gnus-error 1 "Can't mark negative article numbers")
9953             nil)
9954         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9955         (setq gnus-newsgroup-spam-marked
9956               (delq article gnus-newsgroup-spam-marked))
9957         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9958         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9959         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9960         (cond ((= mark gnus-ticked-mark)
9961                (setq gnus-newsgroup-marked
9962                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9963                                               article)))
9964               ((= mark gnus-spam-mark)
9965                (setq gnus-newsgroup-spam-marked
9966                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9967                                               article)))
9968               ((= mark gnus-dormant-mark)
9969                (setq gnus-newsgroup-dormant
9970                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9971                                               article)))
9972               (t
9973                (setq gnus-newsgroup-unreads
9974                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9975                                               article))))
9976         (gnus-pull article gnus-newsgroup-reads)
9977
9978         ;; See whether the article is to be put in the cache.
9979         (and gnus-use-cache
9980              (vectorp (gnus-summary-article-header article))
9981              (save-excursion
9982                (gnus-cache-possibly-enter-article
9983                 gnus-newsgroup-name article
9984                 (gnus-summary-article-header article)
9985                 (= mark gnus-ticked-mark)
9986                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9987
9988         ;; Fix the mark.
9989         (gnus-summary-update-mark mark 'unread)
9990         t))))
9991
9992 (defun gnus-summary-mark-article (&optional article mark no-expire)
9993   "Mark ARTICLE with MARK.  MARK can be any character.
9994 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9995 `??' (dormant) and `?E' (expirable).
9996 If MARK is nil, then the default character `?r' is used.
9997 If ARTICLE is nil, then the article on the current line will be
9998 marked.
9999 If NO-EXPIRE, auto-expiry will be inhibited."
10000   ;; The mark might be a string.
10001   (when (stringp mark)
10002     (setq mark (aref mark 0)))
10003   ;; If no mark is given, then we check auto-expiring.
10004   (when (null mark)
10005     (setq mark gnus-del-mark))
10006   (when (and (not no-expire)
10007              gnus-newsgroup-auto-expire
10008              (memq mark gnus-auto-expirable-marks))
10009     (setq mark gnus-expirable-mark))
10010   (let ((article (or article (gnus-summary-article-number)))
10011         (old-mark (gnus-summary-article-mark article)))
10012     ;; Allow the backend to change the mark.
10013     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10014     (if (eq mark old-mark)
10015         t
10016       (unless article
10017         (error "No article on current line"))
10018       (if (not (if (or (= mark gnus-unread-mark)
10019                        (= mark gnus-ticked-mark)
10020                        (= mark gnus-spam-mark)
10021                        (= mark gnus-dormant-mark))
10022                    (gnus-mark-article-as-unread article mark)
10023                  (gnus-mark-article-as-read article mark)))
10024           t
10025         ;; See whether the article is to be put in the cache.
10026         (and gnus-use-cache
10027              (not (= mark gnus-canceled-mark))
10028              (vectorp (gnus-summary-article-header article))
10029              (save-excursion
10030                (gnus-cache-possibly-enter-article
10031                 gnus-newsgroup-name article
10032                 (gnus-summary-article-header article)
10033                 (= mark gnus-ticked-mark)
10034                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10035
10036         (when (gnus-summary-goto-subject article nil t)
10037           (let ((buffer-read-only nil))
10038             (gnus-summary-show-thread)
10039             ;; Fix the mark.
10040             (gnus-summary-update-mark mark 'unread)
10041             t))))))
10042
10043 (defun gnus-summary-update-secondary-mark (article)
10044   "Update the secondary (read, process, cache) mark."
10045   (gnus-summary-update-mark
10046    (cond ((memq article gnus-newsgroup-processable)
10047           gnus-process-mark)
10048          ((memq article gnus-newsgroup-cached)
10049           gnus-cached-mark)
10050          ((memq article gnus-newsgroup-replied)
10051           gnus-replied-mark)
10052          ((memq article gnus-newsgroup-forwarded)
10053           gnus-forwarded-mark)
10054          ((memq article gnus-newsgroup-saved)
10055           gnus-saved-mark)
10056          ((memq article gnus-newsgroup-recent)
10057           gnus-recent-mark)
10058          ((memq article gnus-newsgroup-unseen)
10059           gnus-unseen-mark)
10060          (t gnus-no-mark))
10061    'replied)
10062   (when (gnus-visual-p 'summary-highlight 'highlight)
10063     (gnus-run-hooks 'gnus-summary-update-hook))
10064   t)
10065
10066 (defun gnus-summary-update-download-mark (article)
10067   "Update the download mark."
10068   (gnus-summary-update-mark
10069    (cond ((memq article gnus-newsgroup-undownloaded)
10070           gnus-undownloaded-mark)
10071          (gnus-newsgroup-agentized
10072           gnus-downloaded-mark)
10073          (t
10074           gnus-no-mark))
10075    'download)
10076   (gnus-summary-update-line t)
10077   t)
10078
10079 (defun gnus-summary-update-mark (mark type)
10080   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10081         (buffer-read-only nil))
10082     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10083     (when forward
10084       (when (looking-at "\r")
10085         (incf forward))
10086       (when (<= (+ forward (point)) (point-max))
10087         ;; Go to the right position on the line.
10088         (goto-char (+ forward (point)))
10089         ;; Replace the old mark with the new mark.
10090         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10091         ;; Optionally update the marks by some user rule.
10092         (when (eq type 'unread)
10093           (gnus-data-set-mark
10094            (gnus-data-find (gnus-summary-article-number)) mark)
10095           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10096
10097 (defun gnus-mark-article-as-read (article &optional mark)
10098   "Enter ARTICLE in the pertinent lists and remove it from others."
10099   ;; Make the article expirable.
10100   (let ((mark (or mark gnus-del-mark)))
10101     (setq gnus-newsgroup-expirable
10102           (if (= mark gnus-expirable-mark)
10103               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10104             (delq article gnus-newsgroup-expirable)))
10105     ;; Remove from unread and marked lists.
10106     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10107     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10108     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10109     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10110     (push (cons article mark) gnus-newsgroup-reads)
10111     ;; Possibly remove from cache, if that is used.
10112     (when gnus-use-cache
10113       (gnus-cache-enter-remove-article article))
10114     t))
10115
10116 (defun gnus-mark-article-as-unread (article &optional mark)
10117   "Enter ARTICLE in the pertinent lists and remove it from others."
10118   (let ((mark (or mark gnus-ticked-mark)))
10119     (if (<= article 0)
10120         (progn
10121           (gnus-error 1 "Can't mark negative article numbers")
10122           nil)
10123       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10124             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10125             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10126             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10127             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10128
10129       ;; Unsuppress duplicates?
10130       (when gnus-suppress-duplicates
10131         (gnus-dup-unsuppress-article article))
10132
10133       (cond ((= mark gnus-ticked-mark)
10134              (setq gnus-newsgroup-marked
10135                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10136             ((= mark gnus-spam-mark)
10137              (setq gnus-newsgroup-spam-marked
10138                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10139                                             article)))
10140             ((= mark gnus-dormant-mark)
10141              (setq gnus-newsgroup-dormant
10142                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10143             (t
10144              (setq gnus-newsgroup-unreads
10145                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10146       (gnus-pull article gnus-newsgroup-reads)
10147       t)))
10148
10149 (defalias 'gnus-summary-mark-as-unread-forward
10150   'gnus-summary-tick-article-forward)
10151 (make-obsolete 'gnus-summary-mark-as-unread-forward
10152                'gnus-summary-tick-article-forward)
10153 (defun gnus-summary-tick-article-forward (n)
10154   "Tick N articles forwards.
10155 If N is negative, tick backwards instead.
10156 The difference between N and the number of articles ticked is returned."
10157   (interactive "p")
10158   (gnus-summary-mark-forward n gnus-ticked-mark))
10159
10160 (defalias 'gnus-summary-mark-as-unread-backward
10161   'gnus-summary-tick-article-backward)
10162 (make-obsolete 'gnus-summary-mark-as-unread-backward
10163                'gnus-summary-tick-article-backward)
10164 (defun gnus-summary-tick-article-backward (n)
10165   "Tick N articles backwards.
10166 The difference between N and the number of articles ticked is returned."
10167   (interactive "p")
10168   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10169
10170 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10171 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10172 (defun gnus-summary-tick-article (&optional article clear-mark)
10173   "Mark current article as unread.
10174 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10175 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10176   (interactive)
10177   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10178                                        gnus-ticked-mark)))
10179
10180 (defun gnus-summary-mark-as-read-forward (n)
10181   "Mark N articles as read forwards.
10182 If N is negative, mark backwards instead.
10183 The difference between N and the actual number of articles marked is
10184 returned."
10185   (interactive "p")
10186   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10187
10188 (defun gnus-summary-mark-as-read-backward (n)
10189   "Mark the N articles as read backwards.
10190 The difference between N and the actual number of articles marked is
10191 returned."
10192   (interactive "p")
10193   (gnus-summary-mark-forward
10194    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10195
10196 (defun gnus-summary-mark-as-read (&optional article mark)
10197   "Mark current article as read.
10198 ARTICLE specifies the article to be marked as read.
10199 MARK specifies a string to be inserted at the beginning of the line."
10200   (gnus-summary-mark-article article mark))
10201
10202 (defun gnus-summary-clear-mark-forward (n)
10203   "Clear marks from N articles forward.
10204 If N is negative, clear backward instead.
10205 The difference between N and the number of marks cleared is returned."
10206   (interactive "p")
10207   (gnus-summary-mark-forward n gnus-unread-mark))
10208
10209 (defun gnus-summary-clear-mark-backward (n)
10210   "Clear marks from N articles backward.
10211 The difference between N and the number of marks cleared is returned."
10212   (interactive "p")
10213   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10214
10215 (defun gnus-summary-mark-unread-as-read ()
10216   "Intended to be used by `gnus-summary-mark-article-hook'."
10217   (when (memq gnus-current-article gnus-newsgroup-unreads)
10218     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10219
10220 (defun gnus-summary-mark-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-current-article
10226                                  (or new-mark gnus-read-mark)))))
10227
10228 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10229   "Intended to be used by `gnus-summary-mark-article-hook'."
10230   (let ((mark (gnus-summary-article-mark)))
10231     (when (or (gnus-unread-mark-p mark)
10232               (gnus-read-mark-p mark))
10233       (gnus-summary-mark-article (gnus-summary-article-number)
10234                                  (or new-mark gnus-read-mark)))))
10235
10236 (defun gnus-summary-mark-unread-as-ticked ()
10237   "Intended to be used by `gnus-summary-mark-article-hook'."
10238   (when (memq gnus-current-article gnus-newsgroup-unreads)
10239     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10240
10241 (defun gnus-summary-mark-region-as-read (point mark all)
10242   "Mark all unread articles between point and mark as read.
10243 If given a prefix, mark all articles between point and mark as read,
10244 even ticked and dormant ones."
10245   (interactive "r\nP")
10246   (save-excursion
10247     (let (article)
10248       (goto-char point)
10249       (beginning-of-line)
10250       (while (and
10251               (< (point) mark)
10252               (progn
10253                 (when (or all
10254                           (memq (setq article (gnus-summary-article-number))
10255                                 gnus-newsgroup-unreads))
10256                   (gnus-summary-mark-article article gnus-del-mark))
10257                 t)
10258               (gnus-summary-find-next))))))
10259
10260 (defun gnus-summary-mark-below (score mark)
10261   "Mark articles with score less than SCORE with MARK."
10262   (interactive "P\ncMark: ")
10263   (setq score (if score
10264                   (prefix-numeric-value score)
10265                 (or gnus-summary-default-score 0)))
10266   (save-excursion
10267     (set-buffer gnus-summary-buffer)
10268     (goto-char (point-min))
10269     (while
10270         (progn
10271           (and (< (gnus-summary-article-score) score)
10272                (gnus-summary-mark-article nil mark))
10273           (gnus-summary-find-next)))))
10274
10275 (defun gnus-summary-kill-below (&optional score)
10276   "Mark articles with score below SCORE as read."
10277   (interactive "P")
10278   (gnus-summary-mark-below score gnus-killed-mark))
10279
10280 (defun gnus-summary-clear-above (&optional score)
10281   "Clear all marks from articles with score above SCORE."
10282   (interactive "P")
10283   (gnus-summary-mark-above score gnus-unread-mark))
10284
10285 (defun gnus-summary-tick-above (&optional score)
10286   "Tick all articles with score above SCORE."
10287   (interactive "P")
10288   (gnus-summary-mark-above score gnus-ticked-mark))
10289
10290 (defun gnus-summary-mark-above (score mark)
10291   "Mark articles with score over SCORE with MARK."
10292   (interactive "P\ncMark: ")
10293   (setq score (if score
10294                   (prefix-numeric-value score)
10295                 (or gnus-summary-default-score 0)))
10296   (save-excursion
10297     (set-buffer gnus-summary-buffer)
10298     (goto-char (point-min))
10299     (while (and (progn
10300                   (when (> (gnus-summary-article-score) score)
10301                     (gnus-summary-mark-article nil mark))
10302                   t)
10303                 (gnus-summary-find-next)))))
10304
10305 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10306 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10307 (defun gnus-summary-limit-include-expunged (&optional no-error)
10308   "Display all the hidden articles that were expunged for low scores."
10309   (interactive)
10310   (let ((buffer-read-only nil))
10311     (let ((scored gnus-newsgroup-scored)
10312           headers h)
10313       (while scored
10314         (unless (gnus-summary-article-header (caar scored))
10315           (and (setq h (gnus-number-to-header (caar scored)))
10316                (< (cdar scored) gnus-summary-expunge-below)
10317                (push h headers)))
10318         (setq scored (cdr scored)))
10319       (if (not headers)
10320           (when (not no-error)
10321             (error "No expunged articles hidden"))
10322         (goto-char (point-min))
10323         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10324         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10325         (mapcar (lambda (x) (push (mail-header-number x)
10326                                   gnus-newsgroup-limit))
10327                 headers)
10328         (gnus-summary-prepare-unthreaded (nreverse headers))
10329         (goto-char (point-min))
10330         (gnus-summary-position-point)
10331         t))))
10332
10333 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10334   "Mark all unread articles in this newsgroup as read.
10335 If prefix argument ALL is non-nil, ticked and dormant articles will
10336 also be marked as read.
10337 If QUIETLY is non-nil, no questions will be asked.
10338
10339 If TO-HERE is non-nil, it should be a point in the buffer.  All
10340 articles before (after, if REVERSE is set) this point will be marked
10341 as read.
10342
10343 Note that this function will only catch up the unread article
10344 in the current summary buffer limitation.
10345
10346 The number of articles marked as read is returned."
10347   (interactive "P")
10348   (prog1
10349       (save-excursion
10350         (when (or quietly
10351                   (not gnus-interactive-catchup) ;Without confirmation?
10352                   gnus-expert-user
10353                   (gnus-y-or-n-p
10354                    (if all
10355                        "Mark absolutely all articles as read? "
10356                      "Mark all unread articles as read? ")))
10357           (if (and not-mark
10358                    (not gnus-newsgroup-adaptive)
10359                    (not gnus-newsgroup-auto-expire)
10360                    (not gnus-suppress-duplicates)
10361                    (or (not gnus-use-cache)
10362                        (eq gnus-use-cache 'passive)))
10363               (progn
10364                 (when all
10365                   (setq gnus-newsgroup-marked nil
10366                         gnus-newsgroup-spam-marked nil
10367                         gnus-newsgroup-dormant nil))
10368                 (setq gnus-newsgroup-unreads
10369                       (gnus-sorted-nunion
10370                        (gnus-intersection gnus-newsgroup-unreads
10371                                           gnus-newsgroup-downloadable)
10372                        gnus-newsgroup-unfetched)))
10373             ;; We actually mark all articles as canceled, which we
10374             ;; have to do when using auto-expiry or adaptive scoring.
10375             (gnus-summary-show-all-threads)
10376             (if (and to-here reverse)
10377                 (progn
10378                   (goto-char to-here)
10379                   (gnus-summary-mark-current-read-and-unread-as-read
10380                    gnus-catchup-mark)
10381                   (while (gnus-summary-find-next (not all))
10382                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10383               (when (gnus-summary-first-subject (not all))
10384                 (while (and
10385                         (if to-here (< (point) to-here) t)
10386                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10387                         (gnus-summary-find-next (not all))))))
10388             (gnus-set-mode-line 'summary))
10389           t))
10390     (gnus-summary-position-point)))
10391
10392 (defun gnus-summary-catchup-to-here (&optional all)
10393   "Mark all unticked articles before the current one as read.
10394 If ALL is non-nil, also mark ticked and dormant articles as read."
10395   (interactive "P")
10396   (save-excursion
10397     (gnus-save-hidden-threads
10398       (let ((beg (point)))
10399         ;; We check that there are unread articles.
10400         (when (or all (gnus-summary-find-prev))
10401           (gnus-summary-catchup all t beg)))))
10402   (gnus-summary-position-point))
10403
10404 (defun gnus-summary-catchup-from-here (&optional all)
10405   "Mark all unticked articles after (and including) the current one as read.
10406 If ALL is non-nil, also mark ticked and dormant articles as read."
10407   (interactive "P")
10408   (save-excursion
10409     (gnus-save-hidden-threads
10410       (let ((beg (point)))
10411         ;; We check that there are unread articles.
10412         (when (or all (gnus-summary-find-next))
10413           (gnus-summary-catchup all t beg nil t)))))
10414   (gnus-summary-position-point))
10415
10416 (defun gnus-summary-catchup-all (&optional quietly)
10417   "Mark all articles in this newsgroup as read.
10418 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10419 instead, which marks only unread articles as read."
10420   (interactive "P")
10421   (gnus-summary-catchup t quietly))
10422
10423 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10424   "Mark all unread articles in this group as read, then exit.
10425 If prefix argument ALL is non-nil, all articles are marked as read.
10426 If QUIETLY is non-nil, no questions will be asked."
10427   (interactive "P")
10428   (when (gnus-summary-catchup all quietly nil 'fast)
10429     ;; Select next newsgroup or exit.
10430     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10431              (eq gnus-auto-select-next 'quietly))
10432         (gnus-summary-next-group nil)
10433       (gnus-summary-exit))))
10434
10435 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10436   "Mark all articles in this newsgroup as read, and then exit.
10437 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10438 instead, which marks only unread articles as read."
10439   (interactive "P")
10440   (gnus-summary-catchup-and-exit t quietly))
10441
10442 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10443   "Mark all articles in this group as read and select the next group.
10444 If given a prefix, mark all articles, unread as well as ticked, as
10445 read."
10446   (interactive "P")
10447   (save-excursion
10448     (gnus-summary-catchup all))
10449   (gnus-summary-next-group))
10450
10451 ;;;
10452 ;;; with article
10453 ;;;
10454
10455 (defmacro gnus-with-article (article &rest forms)
10456   "Select ARTICLE and perform FORMS in the original article buffer.
10457 Then replace the article with the result."
10458   `(progn
10459      ;; We don't want the article to be marked as read.
10460      (let (gnus-mark-article-hook)
10461        (gnus-summary-select-article t t nil ,article))
10462      (set-buffer gnus-original-article-buffer)
10463      ,@forms
10464      (if (not (gnus-check-backend-function
10465                'request-replace-article (car gnus-article-current)))
10466          (gnus-message 5 "Read-only group; not replacing")
10467        (unless (gnus-request-replace-article
10468                 ,article (car gnus-article-current)
10469                 (current-buffer) t)
10470          (error "Couldn't replace article")))
10471      ;; The cache and backlog have to be flushed somewhat.
10472      (when gnus-keep-backlog
10473        (gnus-backlog-remove-article
10474         (car gnus-article-current) (cdr gnus-article-current)))
10475      (when gnus-use-cache
10476        (gnus-cache-update-article
10477         (car gnus-article-current) (cdr gnus-article-current)))))
10478
10479 (put 'gnus-with-article 'lisp-indent-function 1)
10480 (put 'gnus-with-article 'edebug-form-spec '(form body))
10481
10482 ;; Thread-based commands.
10483
10484 (defun gnus-summary-articles-in-thread (&optional article)
10485   "Return a list of all articles in the current thread.
10486 If ARTICLE is non-nil, return all articles in the thread that starts
10487 with that article."
10488   (let* ((article (or article (gnus-summary-article-number)))
10489          (data (gnus-data-find-list article))
10490          (top-level (gnus-data-level (car data)))
10491          (top-subject
10492           (cond ((null gnus-thread-operation-ignore-subject)
10493                  (gnus-simplify-subject-re
10494                   (mail-header-subject (gnus-data-header (car data)))))
10495                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10496                  (gnus-simplify-subject-fuzzy
10497                   (mail-header-subject (gnus-data-header (car data)))))
10498                 (t nil)))
10499          (end-point (save-excursion
10500                       (if (gnus-summary-go-to-next-thread)
10501                           (point) (point-max))))
10502          articles)
10503     (while (and data
10504                 (< (gnus-data-pos (car data)) end-point))
10505       (when (or (not top-subject)
10506                 (string= top-subject
10507                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10508                              (gnus-simplify-subject-fuzzy
10509                               (mail-header-subject
10510                                (gnus-data-header (car data))))
10511                            (gnus-simplify-subject-re
10512                             (mail-header-subject
10513                              (gnus-data-header (car data)))))))
10514         (push (gnus-data-number (car data)) articles))
10515       (unless (and (setq data (cdr data))
10516                    (> (gnus-data-level (car data)) top-level))
10517         (setq data nil)))
10518     ;; Return the list of articles.
10519     (nreverse articles)))
10520
10521 (defun gnus-summary-rethread-current ()
10522   "Rethread the thread the current article is part of."
10523   (interactive)
10524   (let* ((gnus-show-threads t)
10525          (article (gnus-summary-article-number))
10526          (id (mail-header-id (gnus-summary-article-header)))
10527          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10528     (unless id
10529       (error "No article on the current line"))
10530     (gnus-rebuild-thread id)
10531     (gnus-summary-goto-subject article)))
10532
10533 (defun gnus-summary-reparent-thread ()
10534   "Make the current article child of the marked (or previous) article.
10535
10536 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10537 is non-nil or the Subject: of both articles are the same."
10538   (interactive)
10539   (unless (not (gnus-group-read-only-p))
10540     (error "The current newsgroup does not support article editing"))
10541   (unless (<= (length gnus-newsgroup-processable) 1)
10542     (error "No more than one article may be marked"))
10543   (save-window-excursion
10544     (let ((gnus-article-buffer " *reparent*")
10545           (current-article (gnus-summary-article-number))
10546           ;; First grab the marked article, otherwise one line up.
10547           (parent-article (if (not (null gnus-newsgroup-processable))
10548                               (car gnus-newsgroup-processable)
10549                             (save-excursion
10550                               (if (eq (forward-line -1) 0)
10551                                   (gnus-summary-article-number)
10552                                 (error "Beginning of summary buffer"))))))
10553       (unless (not (eq current-article parent-article))
10554         (error "An article may not be self-referential"))
10555       (let ((message-id (mail-header-id
10556                          (gnus-summary-article-header parent-article))))
10557         (unless (and message-id (not (equal message-id "")))
10558           (error "No message-id in desired parent"))
10559         (gnus-with-article current-article
10560           (save-restriction
10561             (goto-char (point-min))
10562             (message-narrow-to-head)
10563             (if (re-search-forward "^References: " nil t)
10564                 (progn
10565                   (re-search-forward "^[^ \t]" nil t)
10566                   (forward-line -1)
10567                   (end-of-line)
10568                   (insert " " message-id))
10569               (insert "References: " message-id "\n"))))
10570         (set-buffer gnus-summary-buffer)
10571         (gnus-summary-unmark-all-processable)
10572         (gnus-summary-update-article current-article)
10573         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10574             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10575         (gnus-summary-rethread-current)
10576         (gnus-message 3 "Article %d is now the child of article %d"
10577                       current-article parent-article)))))
10578
10579 (defun gnus-summary-toggle-threads (&optional arg)
10580   "Toggle showing conversation threads.
10581 If ARG is positive number, turn showing conversation threads on."
10582   (interactive "P")
10583   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10584     (setq gnus-show-threads
10585           (if (null arg) (not gnus-show-threads)
10586             (> (prefix-numeric-value arg) 0)))
10587     (gnus-summary-prepare)
10588     (gnus-summary-goto-subject current)
10589     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10590     (gnus-summary-position-point)))
10591
10592 (defun gnus-summary-show-all-threads ()
10593   "Show all threads."
10594   (interactive)
10595   (save-excursion
10596     (let ((buffer-read-only nil))
10597       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10598   (gnus-summary-position-point))
10599
10600 (defun gnus-summary-show-thread ()
10601   "Show thread subtrees.
10602 Returns nil if no thread was there to be shown."
10603   (interactive)
10604   (let ((buffer-read-only nil)
10605         (orig (point))
10606         (end (point-at-eol))
10607         ;; Leave point at bol
10608         (beg (progn (beginning-of-line) (point))))
10609     (prog1
10610         ;; Any hidden lines here?
10611         (search-forward "\r" end t)
10612       (subst-char-in-region beg end ?\^M ?\n t)
10613       (goto-char orig)
10614       (gnus-summary-position-point))))
10615
10616 (defun gnus-summary-maybe-hide-threads ()
10617   "If requested, hide the threads that should be hidden."
10618   (when (and gnus-show-threads
10619              gnus-thread-hide-subtree)
10620     (gnus-summary-hide-all-threads
10621      (if (or (consp gnus-thread-hide-subtree)
10622              (functionp gnus-thread-hide-subtree))
10623          (gnus-make-predicate gnus-thread-hide-subtree)
10624        nil))))
10625
10626 ;;; Hiding predicates.
10627
10628 (defun gnus-article-unread-p (header)
10629   (memq (mail-header-number header) gnus-newsgroup-unreads))
10630
10631 (defun gnus-article-unseen-p (header)
10632   (memq (mail-header-number header) gnus-newsgroup-unseen))
10633
10634 (defun gnus-map-articles (predicate articles)
10635   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10636   (apply 'gnus-or (mapcar predicate
10637                           (mapcar 'gnus-summary-article-header articles))))
10638
10639 (defun gnus-summary-hide-all-threads (&optional predicate)
10640   "Hide all thread subtrees.
10641 If PREDICATE is supplied, threads that satisfy this predicate
10642 will not be hidden."
10643   (interactive)
10644   (save-excursion
10645     (goto-char (point-min))
10646     (let ((end nil))
10647       (while (not end)
10648         (when (or (not predicate)
10649                   (gnus-map-articles
10650                    predicate (gnus-summary-article-children)))
10651             (gnus-summary-hide-thread))
10652         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10653   (gnus-summary-position-point))
10654
10655 (defun gnus-summary-hide-thread ()
10656   "Hide thread subtrees.
10657 If PREDICATE is supplied, threads that satisfy this predicate
10658 will not be hidden.
10659 Returns nil if no threads were there to be hidden."
10660   (interactive)
10661   (let ((buffer-read-only nil)
10662         (start (point))
10663         (article (gnus-summary-article-number)))
10664     (goto-char start)
10665     ;; Go forward until either the buffer ends or the subthread
10666     ;; ends.
10667     (when (and (not (eobp))
10668                (or (zerop (gnus-summary-next-thread 1 t))
10669                    (goto-char (point-max))))
10670       (prog1
10671           (if (and (> (point) start)
10672                    (search-backward "\n" start t))
10673               (progn
10674                 (subst-char-in-region start (point) ?\n ?\^M)
10675                 (gnus-summary-goto-subject article))
10676             (goto-char start)
10677             nil)))))
10678
10679 (defun gnus-summary-go-to-next-thread (&optional previous)
10680   "Go to the same level (or less) next thread.
10681 If PREVIOUS is non-nil, go to previous thread instead.
10682 Return the article number moved to, or nil if moving was impossible."
10683   (let ((level (gnus-summary-thread-level))
10684         (way (if previous -1 1))
10685         (beg (point)))
10686     (forward-line way)
10687     (while (and (not (eobp))
10688                 (< level (gnus-summary-thread-level)))
10689       (forward-line way))
10690     (if (eobp)
10691         (progn
10692           (goto-char beg)
10693           nil)
10694       (setq beg (point))
10695       (prog1
10696           (gnus-summary-article-number)
10697         (goto-char beg)))))
10698
10699 (defun gnus-summary-next-thread (n &optional silent)
10700   "Go to the same level next N'th thread.
10701 If N is negative, search backward instead.
10702 Returns the difference between N and the number of skips actually
10703 done.
10704
10705 If SILENT, don't output messages."
10706   (interactive "p")
10707   (let ((backward (< n 0))
10708         (n (abs n)))
10709     (while (and (> n 0)
10710                 (gnus-summary-go-to-next-thread backward))
10711       (decf n))
10712     (unless silent
10713       (gnus-summary-position-point))
10714     (when (and (not silent) (/= 0 n))
10715       (gnus-message 7 "No more threads"))
10716     n))
10717
10718 (defun gnus-summary-prev-thread (n)
10719   "Go to the same level previous N'th thread.
10720 Returns the difference between N and the number of skips actually
10721 done."
10722   (interactive "p")
10723   (gnus-summary-next-thread (- n)))
10724
10725 (defun gnus-summary-go-down-thread ()
10726   "Go down one level in the current thread."
10727   (let ((children (gnus-summary-article-children)))
10728     (when children
10729       (gnus-summary-goto-subject (car children)))))
10730
10731 (defun gnus-summary-go-up-thread ()
10732   "Go up one level in the current thread."
10733   (let ((parent (gnus-summary-article-parent)))
10734     (when parent
10735       (gnus-summary-goto-subject parent))))
10736
10737 (defun gnus-summary-down-thread (n)
10738   "Go down thread N steps.
10739 If N is negative, go up instead.
10740 Returns the difference between N and how many steps down that were
10741 taken."
10742   (interactive "p")
10743   (let ((up (< n 0))
10744         (n (abs n)))
10745     (while (and (> n 0)
10746                 (if up (gnus-summary-go-up-thread)
10747                   (gnus-summary-go-down-thread)))
10748       (setq n (1- n)))
10749     (gnus-summary-position-point)
10750     (when (/= 0 n)
10751       (gnus-message 7 "Can't go further"))
10752     n))
10753
10754 (defun gnus-summary-up-thread (n)
10755   "Go up thread N steps.
10756 If N is negative, go down instead.
10757 Returns the difference between N and how many steps down that were
10758 taken."
10759   (interactive "p")
10760   (gnus-summary-down-thread (- n)))
10761
10762 (defun gnus-summary-top-thread ()
10763   "Go to the top of the thread."
10764   (interactive)
10765   (while (gnus-summary-go-up-thread))
10766   (gnus-summary-article-number))
10767
10768 (defun gnus-summary-kill-thread (&optional unmark)
10769   "Mark articles under current thread as read.
10770 If the prefix argument is positive, remove any kinds of marks.
10771 If the prefix argument is negative, tick articles instead."
10772   (interactive "P")
10773   (when unmark
10774     (setq unmark (prefix-numeric-value unmark)))
10775   (let ((articles (gnus-summary-articles-in-thread)))
10776     (save-excursion
10777       ;; Expand the thread.
10778       (gnus-summary-show-thread)
10779       ;; Mark all the articles.
10780       (while articles
10781         (gnus-summary-goto-subject (car articles))
10782         (cond ((null unmark)
10783                (gnus-summary-mark-article-as-read gnus-killed-mark))
10784               ((> unmark 0)
10785                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10786               (t
10787                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10788         (setq articles (cdr articles))))
10789     ;; Hide killed subtrees.
10790     (and (null unmark)
10791          gnus-thread-hide-killed
10792          (gnus-summary-hide-thread))
10793     ;; If marked as read, go to next unread subject.
10794     (when (null unmark)
10795       ;; Go to next unread subject.
10796       (gnus-summary-next-subject 1 t)))
10797   (gnus-set-mode-line 'summary))
10798
10799 ;; Summary sorting commands
10800
10801 (defun gnus-summary-sort-by-number (&optional reverse)
10802   "Sort the summary buffer by article number.
10803 Argument REVERSE means reverse order."
10804   (interactive "P")
10805   (gnus-summary-sort 'number reverse))
10806
10807 (defun gnus-summary-sort-by-random (&optional reverse)
10808   "Randomize the order in the summary buffer.
10809 Argument REVERSE means to randomize in reverse order."
10810   (interactive "P")
10811   (gnus-summary-sort 'random reverse))
10812
10813 (defun gnus-summary-sort-by-author (&optional reverse)
10814   "Sort the summary buffer by author name alphabetically.
10815 If `case-fold-search' is non-nil, case of letters is ignored.
10816 Argument REVERSE means reverse order."
10817   (interactive "P")
10818   (gnus-summary-sort 'author reverse))
10819
10820 (defun gnus-summary-sort-by-subject (&optional reverse)
10821   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10822 If `case-fold-search' is non-nil, case of letters is ignored.
10823 Argument REVERSE means reverse order."
10824   (interactive "P")
10825   (gnus-summary-sort 'subject reverse))
10826
10827 (defun gnus-summary-sort-by-date (&optional reverse)
10828   "Sort the summary buffer by date.
10829 Argument REVERSE means reverse order."
10830   (interactive "P")
10831   (gnus-summary-sort 'date reverse))
10832
10833 (defun gnus-summary-sort-by-score (&optional reverse)
10834   "Sort the summary buffer by score.
10835 Argument REVERSE means reverse order."
10836   (interactive "P")
10837   (gnus-summary-sort 'score reverse))
10838
10839 (defun gnus-summary-sort-by-lines (&optional reverse)
10840   "Sort the summary buffer by the number of lines.
10841 Argument REVERSE means reverse order."
10842   (interactive "P")
10843   (gnus-summary-sort 'lines reverse))
10844
10845 (defun gnus-summary-sort-by-chars (&optional reverse)
10846   "Sort the summary buffer by article length.
10847 Argument REVERSE means reverse order."
10848   (interactive "P")
10849   (gnus-summary-sort 'chars reverse))
10850
10851 (defun gnus-summary-sort-by-original (&optional reverse)
10852   "Sort the summary buffer using the default sorting method.
10853 Argument REVERSE means reverse order."
10854   (interactive "P")
10855   (let* ((buffer-read-only)
10856          (gnus-summary-prepare-hook nil))
10857     ;; We do the sorting by regenerating the threads.
10858     (gnus-summary-prepare)
10859     ;; Hide subthreads if needed.
10860     (gnus-summary-maybe-hide-threads)))
10861
10862 (defun gnus-summary-sort (predicate reverse)
10863   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10864   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10865          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10866          (gnus-thread-sort-functions
10867           (if (not reverse)
10868               thread
10869             `(lambda (t1 t2)
10870                (,thread t2 t1))))
10871          (gnus-sort-gathered-threads-function
10872           gnus-thread-sort-functions)
10873          (gnus-article-sort-functions
10874           (if (not reverse)
10875               article
10876             `(lambda (t1 t2)
10877                (,article t2 t1))))
10878          (buffer-read-only)
10879          (gnus-summary-prepare-hook nil))
10880     ;; We do the sorting by regenerating the threads.
10881     (gnus-summary-prepare)
10882     ;; Hide subthreads if needed.
10883     (gnus-summary-maybe-hide-threads)))
10884
10885 ;; Summary saving commands.
10886
10887 (defun gnus-summary-save-article (&optional n not-saved)
10888   "Save the current article using the default saver function.
10889 If N is a positive number, save the N next articles.
10890 If N is a negative number, save the N previous articles.
10891 If N is nil and any articles have been marked with the process mark,
10892 save those articles instead.
10893 The variable `gnus-default-article-saver' specifies the saver function."
10894   (interactive "P")
10895   (let* ((articles (gnus-summary-work-articles n))
10896          (save-buffer (save-excursion
10897                         (nnheader-set-temp-buffer " *Gnus Save*")))
10898          (num (length articles))
10899          header file)
10900     (dolist (article articles)
10901       (setq header (gnus-summary-article-header article))
10902       (if (not (vectorp header))
10903           ;; This is a pseudo-article.
10904           (if (assq 'name header)
10905               (gnus-copy-file (cdr (assq 'name header)))
10906             (gnus-message 1 "Article %d is unsaveable" article))
10907         ;; This is a real article.
10908         (save-window-excursion
10909           (let ((gnus-display-mime-function nil)
10910                 (gnus-article-prepare-hook nil))
10911             (gnus-summary-select-article t nil nil article)))
10912         (save-excursion
10913           (set-buffer save-buffer)
10914           (erase-buffer)
10915           (insert-buffer-substring gnus-original-article-buffer))
10916         (setq file (gnus-article-save save-buffer file num))
10917         (gnus-summary-remove-process-mark article)
10918         (unless not-saved
10919           (gnus-summary-set-saved-mark article))))
10920     (gnus-kill-buffer save-buffer)
10921     (gnus-summary-position-point)
10922     (gnus-set-mode-line 'summary)
10923     n))
10924
10925 (defun gnus-summary-pipe-output (&optional arg headers)
10926   "Pipe the current article to a subprocess.
10927 If N is a positive number, pipe the N next articles.
10928 If N is a negative number, pipe the N previous articles.
10929 If N is nil and any articles have been marked with the process mark,
10930 pipe those articles instead.
10931 If HEADERS (the symbolic prefix), include the headers, too."
10932   (interactive (gnus-interactive "P\ny"))
10933   (require 'gnus-art)
10934   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10935         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10936     (gnus-summary-save-article arg t))
10937   (let ((buffer (get-buffer "*Shell Command Output*")))
10938     (when (and buffer
10939                (not (zerop (buffer-size buffer))))
10940       (gnus-configure-windows 'pipe))))
10941
10942 (defun gnus-summary-save-article-mail (&optional arg)
10943   "Append the current article to an mail file.
10944 If N is a positive number, save the N next articles.
10945 If N is a negative number, save the N previous articles.
10946 If N is nil and any articles have been marked with the process mark,
10947 save those articles instead."
10948   (interactive "P")
10949   (require 'gnus-art)
10950   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10951     (gnus-summary-save-article arg)))
10952
10953 (defun gnus-summary-save-article-rmail (&optional arg)
10954   "Append the current article to an rmail file.
10955 If N is a positive number, save the N next articles.
10956 If N is a negative number, save the N previous articles.
10957 If N is nil and any articles have been marked with the process mark,
10958 save those articles instead."
10959   (interactive "P")
10960   (require 'gnus-art)
10961   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10962     (gnus-summary-save-article arg)))
10963
10964 (defun gnus-summary-save-article-file (&optional arg)
10965   "Append the current article to a file.
10966 If N is a positive number, save the N next articles.
10967 If N is a negative number, save the N previous articles.
10968 If N is nil and any articles have been marked with the process mark,
10969 save those articles instead."
10970   (interactive "P")
10971   (require 'gnus-art)
10972   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10973     (gnus-summary-save-article arg)))
10974
10975 (defun gnus-summary-write-article-file (&optional arg)
10976   "Write the current article to a file, deleting the previous file.
10977 If N is a positive number, save the N next articles.
10978 If N is a negative number, save the N previous articles.
10979 If N is nil and any articles have been marked with the process mark,
10980 save those articles instead."
10981   (interactive "P")
10982   (require 'gnus-art)
10983   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10984     (gnus-summary-save-article arg)))
10985
10986 (defun gnus-summary-save-article-body-file (&optional arg)
10987   "Append the current article body to a file.
10988 If N is a positive number, save the N next articles.
10989 If N is a negative number, save the N previous articles.
10990 If N is nil and any articles have been marked with the process mark,
10991 save those articles instead."
10992   (interactive "P")
10993   (require 'gnus-art)
10994   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10995     (gnus-summary-save-article arg)))
10996
10997 (defun gnus-summary-muttprint (&optional arg)
10998   "Print the current article using Muttprint.
10999 If N is a positive number, save the N next articles.
11000 If N is a negative number, save the N previous articles.
11001 If N is nil and any articles have been marked with the process mark,
11002 save those articles instead."
11003   (interactive "P")
11004   (require 'gnus-art)
11005   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11006     (gnus-summary-save-article arg t)))
11007
11008 (defun gnus-summary-pipe-message (program)
11009   "Pipe the current article through PROGRAM."
11010   (interactive "sProgram: ")
11011   (gnus-summary-select-article)
11012   (let ((mail-header-separator ""))
11013     (gnus-eval-in-buffer-window gnus-article-buffer
11014       (save-restriction
11015         (widen)
11016         (let ((start (window-start))
11017               buffer-read-only)
11018           (message-pipe-buffer-body program)
11019           (set-window-start (get-buffer-window (current-buffer)) start))))))
11020
11021 (defun gnus-get-split-value (methods)
11022   "Return a value based on the split METHODS."
11023   (let (split-name method result match)
11024     (when methods
11025       (save-excursion
11026         (set-buffer gnus-original-article-buffer)
11027         (save-restriction
11028           (nnheader-narrow-to-headers)
11029           (while (and methods (not split-name))
11030             (goto-char (point-min))
11031             (setq method (pop methods))
11032             (setq match (car method))
11033             (when (cond
11034                    ((stringp match)
11035                     ;; Regular expression.
11036                     (ignore-errors
11037                       (re-search-forward match nil t)))
11038                    ((functionp match)
11039                     ;; Function.
11040                     (save-restriction
11041                       (widen)
11042                       (setq result (funcall match gnus-newsgroup-name))))
11043                    ((consp match)
11044                     ;; Form.
11045                     (save-restriction
11046                       (widen)
11047                       (setq result (eval match)))))
11048               (setq split-name (cdr method))
11049               (cond ((stringp result)
11050                      (push (expand-file-name
11051                             result gnus-article-save-directory)
11052                            split-name))
11053                     ((consp result)
11054                      (setq split-name (append result split-name)))))))))
11055     (nreverse split-name)))
11056
11057 (defun gnus-valid-move-group-p (group)
11058   (and (boundp group)
11059        (symbol-name group)
11060        (symbol-value group)
11061        (gnus-get-function (gnus-find-method-for-group
11062                            (symbol-name group)) 'request-accept-article t)))
11063
11064 (defun gnus-read-move-group-name (prompt default articles prefix)
11065   "Read a group name."
11066   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11067          (minibuffer-confirm-incomplete nil) ; XEmacs
11068          (prom
11069           (format "%s %s to:"
11070                   prompt
11071                   (if (> (length articles) 1)
11072                       (format "these %d articles" (length articles))
11073                     "this article")))
11074          (to-newsgroup
11075           (cond
11076            ((null split-name)
11077             (gnus-completing-read-with-default
11078              default prom
11079              gnus-active-hashtb
11080              'gnus-valid-move-group-p
11081              nil prefix
11082              'gnus-group-history))
11083            ((= 1 (length split-name))
11084             (gnus-completing-read-with-default
11085              (car split-name) prom
11086              gnus-active-hashtb
11087              'gnus-valid-move-group-p
11088              nil nil
11089              'gnus-group-history))
11090            (t
11091             (gnus-completing-read-with-default
11092              nil prom
11093              (mapcar (lambda (el) (list el))
11094                      (nreverse split-name))
11095              nil nil nil
11096              'gnus-group-history))))
11097          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11098     (when to-newsgroup
11099       (if (or (string= to-newsgroup "")
11100               (string= to-newsgroup prefix))
11101           (setq to-newsgroup default))
11102       (unless to-newsgroup
11103         (error "No group name entered"))
11104       (or (gnus-active to-newsgroup)
11105           (gnus-activate-group to-newsgroup nil nil to-method)
11106           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11107                                      to-newsgroup))
11108               (or (and (gnus-request-create-group to-newsgroup to-method)
11109                        (gnus-activate-group
11110                         to-newsgroup nil nil to-method)
11111                        (gnus-subscribe-group to-newsgroup))
11112                   (error "Couldn't create group %s" to-newsgroup)))
11113           (error "No such group: %s" to-newsgroup)))
11114     to-newsgroup))
11115
11116 (defun gnus-summary-save-parts (type dir n &optional reverse)
11117   "Save parts matching TYPE to DIR.
11118 If REVERSE, save parts that do not match TYPE."
11119   (interactive
11120    (list (read-string "Save parts of type: "
11121                       (or (car gnus-summary-save-parts-type-history)
11122                           gnus-summary-save-parts-default-mime)
11123                       'gnus-summary-save-parts-type-history)
11124          (setq gnus-summary-save-parts-last-directory
11125                (read-file-name "Save to directory: "
11126                                gnus-summary-save-parts-last-directory
11127                                nil t))
11128          current-prefix-arg))
11129   (gnus-summary-iterate n
11130     (let ((gnus-display-mime-function nil)
11131           (gnus-inhibit-treatment t))
11132       (gnus-summary-select-article))
11133     (save-excursion
11134       (set-buffer gnus-article-buffer)
11135       (let ((handles (or gnus-article-mime-handles
11136                          (mm-dissect-buffer nil gnus-article-loose-mime)
11137                          (and gnus-article-emulate-mime
11138                               (mm-uu-dissect)))))
11139         (when handles
11140           (gnus-summary-save-parts-1 type dir handles reverse)
11141           (unless gnus-article-mime-handles ;; Don't destroy this case.
11142             (mm-destroy-parts handles)))))))
11143
11144 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11145   (if (stringp (car handle))
11146       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11147               (cdr handle))
11148     (when (if reverse
11149               (not (string-match type (mm-handle-media-type handle)))
11150             (string-match type (mm-handle-media-type handle)))
11151       (let ((file (expand-file-name
11152                    (gnus-map-function
11153                     mm-file-name-rewrite-functions
11154                     (file-name-nondirectory
11155                      (or
11156                       (mail-content-type-get
11157                        (mm-handle-disposition handle) 'filename)
11158                       (mail-content-type-get
11159                        (mm-handle-type handle) 'name)
11160                       (concat gnus-newsgroup-name
11161                               "." (number-to-string
11162                                    (cdr gnus-article-current))))))
11163                    dir)))
11164         (unless (file-exists-p file)
11165           (mm-save-part-to-file handle file))))))
11166
11167 ;; Summary extract commands
11168
11169 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11170   (let ((buffer-read-only nil)
11171         (article (gnus-summary-article-number))
11172         after-article b e)
11173     (unless (gnus-summary-goto-subject article)
11174       (error "No such article: %d" article))
11175     (gnus-summary-position-point)
11176     ;; If all commands are to be bunched up on one line, we collect
11177     ;; them here.
11178     (unless gnus-view-pseudos-separately
11179       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11180             files action)
11181         (while ps
11182           (setq action (cdr (assq 'action (car ps))))
11183           (setq files (list (cdr (assq 'name (car ps)))))
11184           (while (and ps (cdr ps)
11185                       (string= (or action "1")
11186                                (or (cdr (assq 'action (cadr ps))) "2")))
11187             (push (cdr (assq 'name (cadr ps))) files)
11188             (setcdr ps (cddr ps)))
11189           (when files
11190             (when (not (string-match "%s" action))
11191               (push " " files))
11192             (push " " files)
11193             (when (assq 'execute (car ps))
11194               (setcdr (assq 'execute (car ps))
11195                       (funcall (if (string-match "%s" action)
11196                                    'format 'concat)
11197                                action
11198                                (mapconcat
11199                                 (lambda (f)
11200                                   (if (equal f " ")
11201                                       f
11202                                     (shell-quote-argument f)))
11203                                 files " ")))))
11204           (setq ps (cdr ps)))))
11205     (if (and gnus-view-pseudos (not not-view))
11206         (while pslist
11207           (when (assq 'execute (car pslist))
11208             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11209                                   (eq gnus-view-pseudos 'not-confirm)))
11210           (setq pslist (cdr pslist)))
11211       (save-excursion
11212         (while pslist
11213           (setq after-article (or (cdr (assq 'article (car pslist)))
11214                                   (gnus-summary-article-number)))
11215           (gnus-summary-goto-subject after-article)
11216           (forward-line 1)
11217           (setq b (point))
11218           (insert "    " (file-name-nondirectory
11219                           (cdr (assq 'name (car pslist))))
11220                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11221           (setq e (point))
11222           (forward-line -1)             ; back to `b'
11223           (gnus-add-text-properties
11224            b (1- e) (list 'gnus-number gnus-reffed-article-number
11225                           gnus-mouse-face-prop gnus-mouse-face))
11226           (gnus-data-enter
11227            after-article gnus-reffed-article-number
11228            gnus-unread-mark b (car pslist) 0 (- e b))
11229           (setq gnus-newsgroup-unreads
11230                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11231                                          gnus-reffed-article-number))
11232           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11233           (setq pslist (cdr pslist)))))))
11234
11235 (defun gnus-pseudos< (p1 p2)
11236   (let ((c1 (cdr (assq 'action p1)))
11237         (c2 (cdr (assq 'action p2))))
11238     (and c1 c2 (string< c1 c2))))
11239
11240 (defun gnus-request-pseudo-article (props)
11241   (cond ((assq 'execute props)
11242          (gnus-execute-command (cdr (assq 'execute props)))))
11243   (let ((gnus-current-article (gnus-summary-article-number)))
11244     (gnus-run-hooks 'gnus-mark-article-hook)))
11245
11246 (defun gnus-execute-command (command &optional automatic)
11247   (save-excursion
11248     (gnus-article-setup-buffer)
11249     (set-buffer gnus-article-buffer)
11250     (setq buffer-read-only nil)
11251     (let ((command (if automatic command
11252                      (read-string "Command: " (cons command 0)))))
11253       (erase-buffer)
11254       (insert "$ " command "\n\n")
11255       (if gnus-view-pseudo-asynchronously
11256           (start-process "gnus-execute" (current-buffer) shell-file-name
11257                          shell-command-switch command)
11258         (call-process shell-file-name nil t nil
11259                       shell-command-switch command)))))
11260
11261 ;; Summary kill commands.
11262
11263 (defun gnus-summary-edit-global-kill (article)
11264   "Edit the \"global\" kill file."
11265   (interactive (list (gnus-summary-article-number)))
11266   (gnus-group-edit-global-kill article))
11267
11268 (defun gnus-summary-edit-local-kill ()
11269   "Edit a local kill file applied to the current newsgroup."
11270   (interactive)
11271   (setq gnus-current-headers (gnus-summary-article-header))
11272   (gnus-group-edit-local-kill
11273    (gnus-summary-article-number) gnus-newsgroup-name))
11274
11275 ;;; Header reading.
11276
11277 (defun gnus-read-header (id &optional header)
11278   "Read the headers of article ID and enter them into the Gnus system."
11279   (let ((group gnus-newsgroup-name)
11280         (gnus-override-method
11281          (or
11282           gnus-override-method
11283           (and (gnus-news-group-p gnus-newsgroup-name)
11284                (car (gnus-refer-article-methods)))))
11285         where)
11286     ;; First we check to see whether the header in question is already
11287     ;; fetched.
11288     (if (stringp id)
11289         ;; This is a Message-ID.
11290         (setq header (or header (gnus-id-to-header id)))
11291       ;; This is an article number.
11292       (setq header (or header (gnus-summary-article-header id))))
11293     (if (and header
11294              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11295         ;; We have found the header.
11296         header
11297       ;; If this is a sparse article, we have to nix out its
11298       ;; previous entry in the thread hashtb.
11299       (when (and header
11300                  (gnus-summary-article-sparse-p (mail-header-number header)))
11301         (let* ((parent (gnus-parent-id (mail-header-references header)))
11302                (thread (and parent (gnus-id-to-thread parent))))
11303           (when thread
11304             (delq (assq header thread) thread))))
11305       ;; We have to really fetch the header to this article.
11306       (save-excursion
11307         (set-buffer nntp-server-buffer)
11308         (when (setq where (gnus-request-head id group))
11309           (nnheader-fold-continuation-lines)
11310           (goto-char (point-max))
11311           (insert ".\n")
11312           (goto-char (point-min))
11313           (insert "211 ")
11314           (princ (cond
11315                   ((numberp id) id)
11316                   ((cdr where) (cdr where))
11317                   (header (mail-header-number header))
11318                   (t gnus-reffed-article-number))
11319                  (current-buffer))
11320           (insert " Article retrieved.\n"))
11321         (if (or (not where)
11322                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11323             ()                          ; Malformed head.
11324           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11325             (when (and (stringp id)
11326                        (not (string= (gnus-group-real-name group)
11327                                      (car where))))
11328               ;; If we fetched by Message-ID and the article came
11329               ;; from a different group, we fudge some bogus article
11330               ;; numbers for this article.
11331               (mail-header-set-number header gnus-reffed-article-number))
11332             (save-excursion
11333               (set-buffer gnus-summary-buffer)
11334               (decf gnus-reffed-article-number)
11335               (gnus-remove-header (mail-header-number header))
11336               (push header gnus-newsgroup-headers)
11337               (setq gnus-current-headers header)
11338               (push (mail-header-number header) gnus-newsgroup-limit)))
11339           header)))))
11340
11341 (defun gnus-remove-header (number)
11342   "Remove header NUMBER from `gnus-newsgroup-headers'."
11343   (if (and gnus-newsgroup-headers
11344            (= number (mail-header-number (car gnus-newsgroup-headers))))
11345       (pop gnus-newsgroup-headers)
11346     (let ((headers gnus-newsgroup-headers))
11347       (while (and (cdr headers)
11348                   (not (= number (mail-header-number (cadr headers)))))
11349         (pop headers))
11350       (when (cdr headers)
11351         (setcdr headers (cddr headers))))))
11352
11353 ;;;
11354 ;;; summary highlights
11355 ;;;
11356
11357 (defun gnus-highlight-selected-summary ()
11358   "Highlight selected article in summary buffer."
11359   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11360   (when gnus-summary-selected-face
11361     (save-excursion
11362       (let* ((beg (point-at-bol))
11363              (end (point-at-eol))
11364              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11365              (from (if (get-text-property beg gnus-mouse-face-prop)
11366                        beg
11367                      (or (next-single-property-change
11368                           beg gnus-mouse-face-prop nil end)
11369                          beg)))
11370              (to
11371               (if (= from end)
11372                   (- from 2)
11373                 (or (next-single-property-change
11374                      from gnus-mouse-face-prop nil end)
11375                     end))))
11376         ;; If no mouse-face prop on line we will have to = from = end,
11377         ;; so we highlight the entire line instead.
11378         (when (= (+ to 2) from)
11379           (setq from beg)
11380           (setq to end))
11381         (if gnus-newsgroup-selected-overlay
11382             ;; Move old overlay.
11383             (gnus-move-overlay
11384              gnus-newsgroup-selected-overlay from to (current-buffer))
11385           ;; Create new overlay.
11386           (gnus-overlay-put
11387            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11388            'face gnus-summary-selected-face))))))
11389
11390 (defvar gnus-summary-highlight-line-cached nil)
11391 (defvar gnus-summary-highlight-line-trigger nil)
11392
11393 (defun gnus-summary-highlight-line-0 ()
11394   (if (and (eq gnus-summary-highlight-line-trigger
11395                gnus-summary-highlight)
11396            gnus-summary-highlight-line-cached)
11397       gnus-summary-highlight-line-cached
11398     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11399           gnus-summary-highlight-line-cached
11400           (let* ((cond (list 'cond))
11401                  (c cond)
11402                  (list gnus-summary-highlight))
11403             (while list
11404               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11405                               nil))
11406               (setq c (cdr c)
11407                     list (cdr list)))
11408             (gnus-byte-compile (list 'lambda nil cond))))))
11409
11410 (defun gnus-summary-highlight-line ()
11411   "Highlight current line according to `gnus-summary-highlight'."
11412   (let* ((beg (point-at-bol))
11413          (article (or (gnus-summary-article-number) gnus-current-article))
11414          (score (or (cdr (assq article
11415                                gnus-newsgroup-scored))
11416                     gnus-summary-default-score 0))
11417          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11418          (inhibit-read-only t)
11419          (default gnus-summary-default-score)
11420          (default-high gnus-summary-default-high-score)
11421          (default-low gnus-summary-default-low-score)
11422          (uncached (and gnus-summary-use-undownloaded-faces
11423                         (memq article gnus-newsgroup-undownloaded)
11424                         (not (memq article gnus-newsgroup-cached)))))
11425     (let ((face (funcall (gnus-summary-highlight-line-0))))
11426       (unless (eq face (get-text-property beg 'face))
11427         (gnus-put-text-property-excluding-characters-with-faces
11428          beg (point-at-eol) 'face
11429          (setq face (if (boundp face) (symbol-value face) face)))
11430         (when gnus-summary-highlight-line-function
11431           (funcall gnus-summary-highlight-line-function article face))))))
11432
11433 (defun gnus-update-read-articles (group unread &optional compute)
11434   "Update the list of read articles in GROUP.
11435 UNREAD is a sorted list."
11436   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11437         (info (gnus-get-info group))
11438         (prev 1)
11439         read)
11440     (if (or (not info) (not active))
11441         ;; There is no info on this group if it was, in fact,
11442         ;; killed.  Gnus stores no information on killed groups, so
11443         ;; there's nothing to be done.
11444         ;; One could store the information somewhere temporarily,
11445         ;; perhaps...  Hmmm...
11446         ()
11447       ;; Remove any negative articles numbers.
11448       (while (and unread (< (car unread) 0))
11449         (setq unread (cdr unread)))
11450       ;; Remove any expired article numbers
11451       (while (and unread (< (car unread) (car active)))
11452         (setq unread (cdr unread)))
11453       ;; Compute the ranges of read articles by looking at the list of
11454       ;; unread articles.
11455       (while unread
11456         (when (/= (car unread) prev)
11457           (push (if (= prev (1- (car unread))) prev
11458                   (cons prev (1- (car unread))))
11459                 read))
11460         (setq prev (1+ (car unread)))
11461         (setq unread (cdr unread)))
11462       (when (<= prev (cdr active))
11463         (push (cons prev (cdr active)) read))
11464       (setq read (if (> (length read) 1) (nreverse read) read))
11465       (if compute
11466           read
11467         (save-excursion
11468           (let (setmarkundo)
11469             ;; Propagate the read marks to the backend.
11470             (when (gnus-check-backend-function 'request-set-mark group)
11471               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11472                     (add (gnus-remove-from-range read (gnus-info-read info))))
11473                 (when (or add del)
11474                   (unless (gnus-check-group group)
11475                     (error "Can't open server for %s" group))
11476                   (gnus-request-set-mark
11477                    group (delq nil (list (if add (list add 'add '(read)))
11478                                          (if del (list del 'del '(read))))))
11479                   (setq setmarkundo
11480                         `(gnus-request-set-mark
11481                           ,group
11482                           ',(delq nil (list
11483                                        (if del (list del 'add '(read)))
11484                                        (if add (list add 'del '(read))))))))))
11485             (set-buffer gnus-group-buffer)
11486             (gnus-undo-register
11487               `(progn
11488                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11489                  (gnus-info-set-read ',info ',(gnus-info-read info))
11490                  (gnus-get-unread-articles-in-group ',info
11491                                                     (gnus-active ,group))
11492                  (gnus-group-update-group ,group t)
11493                  ,setmarkundo))))
11494         ;; Enter this list into the group info.
11495         (gnus-info-set-read info read)
11496         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11497         (gnus-get-unread-articles-in-group info (gnus-active group))
11498         t))))
11499
11500 (defun gnus-offer-save-summaries ()
11501   "Offer to save all active summary buffers."
11502   (let (buffers)
11503     ;; Go through all buffers and find all summaries.
11504     (dolist (buffer (buffer-list))
11505       (when (and (setq buffer (buffer-name buffer))
11506                  (string-match "Summary" buffer)
11507                  (save-excursion
11508                    (set-buffer buffer)
11509                    ;; We check that this is, indeed, a summary buffer.
11510                    (and (eq major-mode 'gnus-summary-mode)
11511                         ;; Also make sure this isn't bogus.
11512                         gnus-newsgroup-prepared
11513                         ;; Also make sure that this isn't a
11514                         ;; dead summary buffer.
11515                         (not gnus-dead-summary-mode))))
11516         (push buffer buffers)))
11517     ;; Go through all these summary buffers and offer to save them.
11518     (when buffers
11519       (save-excursion
11520         (map-y-or-n-p
11521          "Update summary buffer %s? "
11522          (lambda (buf)
11523            (switch-to-buffer buf)
11524            (gnus-summary-exit))
11525          buffers)))))
11526
11527
11528 ;;; @ for mime-partial
11529 ;;;
11530
11531 (defun gnus-request-partial-message ()
11532   (save-excursion
11533     (let ((number (gnus-summary-article-number))
11534           (group gnus-newsgroup-name)
11535           (mother gnus-article-buffer))
11536       (set-buffer (get-buffer-create " *Partial Article*"))
11537       (erase-buffer)
11538       (setq mime-preview-buffer mother)
11539       (gnus-request-article-this-buffer number group)
11540       (mime-parse-buffer)
11541       )))
11542
11543 (autoload 'mime-combine-message/partial-pieces-automatically
11544   "mime-partial"
11545   "Internal method to combine message/partial messages automatically.")
11546
11547 (mime-add-condition
11548  'action '((type . message)(subtype . partial)
11549            (major-mode . gnus-original-article-mode)
11550            (method . mime-combine-message/partial-pieces-automatically)
11551            (summary-buffer-exp . gnus-summary-buffer)
11552            (request-partial-message-method . gnus-request-partial-message)
11553            ))
11554
11555
11556 ;;; @ for message/rfc822
11557 ;;;
11558
11559 (defun gnus-mime-extract-message/rfc822 (entity situation)
11560   "Burst a forwarded article."
11561   (save-excursion
11562     (set-buffer gnus-summary-buffer)
11563     (let* ((group (completing-read "Group: " gnus-active-hashtb nil t
11564                                    gnus-newsgroup-name 'gnus-group-history))
11565            (gnus-group-marked (list group))
11566            article info)
11567       (with-temp-buffer
11568         (mime-insert-entity-content entity)
11569         (setq article (gnus-request-accept-article group)))
11570       (when (and (consp article)
11571                  (numberp (setq article (cdr article))))
11572         (setq info (gnus-get-info group))
11573         (gnus-info-set-read info
11574                             (gnus-remove-from-range (gnus-info-read info)
11575                                                     (list article)))
11576         (when (string-equal group gnus-newsgroup-name)
11577           (forward-line 1)
11578           (let (gnus-show-threads)
11579             (gnus-summary-goto-subject article t))
11580           (gnus-summary-clear-mark-forward 1))
11581         (set-buffer gnus-group-buffer)
11582         (gnus-group-get-new-news-this-group nil t)))))
11583
11584 (mime-add-condition
11585  'action '((type . message)(subtype . rfc822)
11586            (major-mode . gnus-original-article-mode)
11587            (method . gnus-mime-extract-message/rfc822)
11588            (mode . "extract")
11589            ))
11590
11591 (mime-add-condition
11592  'action '((type . message)(subtype . news)
11593            (major-mode . gnus-original-article-mode)
11594            (method . gnus-mime-extract-message/rfc822)
11595            (mode . "extract")
11596            ))
11597
11598 (defun gnus-mime-extract-multipart (entity situation)
11599   (let ((children (mime-entity-children entity))
11600         mime-acting-situation-to-override
11601         f)
11602     (while children
11603       (mime-play-entity (car children)
11604                         (cons (assq 'mode situation)
11605                               mime-acting-situation-to-override))
11606       (setq children (cdr children)))
11607     (if (setq f (cdr (assq 'after-method
11608                            mime-acting-situation-to-override)))
11609         (eval f)
11610       )))
11611
11612 (mime-add-condition
11613  'action '((type . multipart)
11614            (method . gnus-mime-extract-multipart)
11615            (mode . "extract")
11616            )
11617  'with-default)
11618
11619
11620 ;;; @ end
11621 ;;;
11622
11623 (defun gnus-summary-inherit-default-charset ()
11624   "Import `default-mime-charset' from summary buffer.
11625 Also take care of `default-mime-charset-unlimited' if the LIMIT version
11626 of FLIM is used."
11627   (if (buffer-live-p gnus-summary-buffer)
11628       (let (d-m-c d-m-c-u)
11629         (with-current-buffer gnus-summary-buffer
11630           (setq d-m-c (if (local-variable-p 'default-mime-charset
11631                                             gnus-summary-buffer)
11632                           default-mime-charset
11633                         t)
11634                 ;; LIMIT
11635                 d-m-c-u (if (local-variable-p 'default-mime-charset-unlimited
11636                                               gnus-summary-buffer)
11637                             (symbol-value 'default-mime-charset-unlimited)
11638                           t)))
11639         (if (eq t d-m-c)
11640             (kill-local-variable 'default-mime-charset)
11641           (set (make-local-variable 'default-mime-charset) d-m-c))
11642         (if (eq t d-m-c-u)
11643             (kill-local-variable 'default-mime-charset-unlimited)
11644           (set (make-local-variable 'default-mime-charset-unlimited)
11645                d-m-c-u)))))
11646
11647 (defun gnus-summary-setup-default-charset ()
11648   "Setup newsgroup default charset."
11649   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11650       (progn
11651         (setq gnus-newsgroup-charset nil)
11652         (set (make-local-variable 'default-mime-charset) nil)
11653         (when (boundp 'default-mime-charset-unlimited);; LIMIT
11654           (set (make-local-variable 'default-mime-charset-unlimited) nil)))
11655     (let ((ignored-charsets
11656            (or gnus-newsgroup-ephemeral-ignored-charsets
11657                (append
11658                 (and gnus-newsgroup-name
11659                      (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11660                 gnus-newsgroup-ignored-charsets)))
11661           charset)
11662       (setq gnus-newsgroup-charset
11663             (or gnus-newsgroup-ephemeral-charset
11664                 (when (and gnus-newsgroup-name
11665                            (setq charset (gnus-parameter-charset
11666                                           gnus-newsgroup-name)))
11667                   (make-local-variable 'default-mime-charset)
11668                   (setq default-mime-charset charset))
11669                 gnus-default-charset))
11670       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11671            ignored-charsets))))
11672
11673 ;;;
11674 ;;; Mime Commands
11675 ;;;
11676
11677 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11678   "Display the current article buffer fully MIME-buttonized.
11679 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11680 treated as multipart/mixed."
11681   (interactive "P")
11682   (require 'gnus-art)
11683   (let ((gnus-unbuttonized-mime-types nil)
11684         (gnus-mime-display-multipart-as-mixed show-all-parts))
11685     (gnus-summary-show-article)))
11686
11687 (defun gnus-summary-repair-multipart (article)
11688   "Add a Content-Type header to a multipart article without one."
11689   (interactive (list (gnus-summary-article-number)))
11690   (gnus-with-article article
11691     (message-narrow-to-head)
11692     (message-remove-header "Mime-Version")
11693     (goto-char (point-max))
11694     (insert "Mime-Version: 1.0\n")
11695     (widen)
11696     (when (search-forward "\n--" nil t)
11697       (let ((separator (buffer-substring (point) (point-at-eol))))
11698         (message-narrow-to-head)
11699         (message-remove-header "Content-Type")
11700         (goto-char (point-max))
11701         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11702                         separator))
11703         (widen))))
11704   (let (gnus-mark-article-hook)
11705     (gnus-summary-select-article t t nil article)))
11706
11707 (defun gnus-summary-toggle-display-buttonized ()
11708   "Toggle the buttonizing of the article buffer."
11709   (interactive)
11710   (require 'gnus-art)
11711   (if (setq gnus-inhibit-mime-unbuttonizing
11712             (not gnus-inhibit-mime-unbuttonizing))
11713       (let ((gnus-unbuttonized-mime-types nil))
11714         (gnus-summary-show-article))
11715     (gnus-summary-show-article)))
11716
11717 ;;;
11718 ;;; Intelli-mouse commmands
11719 ;;;
11720
11721 (defun gnus-wheel-summary-scroll (event)
11722   (interactive "e")
11723   (let ((amount (if (memq 'shift (event-modifiers event))
11724                     (car gnus-wheel-scroll-amount)
11725                   (cdr gnus-wheel-scroll-amount)))
11726         (direction (- (* (static-if (featurep 'xemacs)
11727                              (event-button event)
11728                            (cond ((eq 'mouse-4 (event-basic-type event))
11729                                   4)
11730                                  ((eq 'mouse-5 (event-basic-type event))
11731                                   5)))
11732                          2) 9))
11733         edge)
11734     (gnus-summary-scroll-up (* amount direction))
11735     (when (gnus-eval-in-buffer-window gnus-article-buffer
11736             (save-restriction
11737               (widen)
11738               (and (if (< 0 direction)
11739                        (gnus-article-next-page 0)
11740                      (gnus-article-prev-page 0)
11741                      (bobp))
11742                    (if (setq edge (get-text-property
11743                                    (point-min) 'gnus-wheel-edge))
11744                        (setq edge (* edge direction))
11745                      (setq edge -1))
11746                    (or (plusp edge)
11747                        (let ((buffer-read-only nil)
11748                              (inhibit-read-only t))
11749                          (put-text-property (point-min) (point-max)
11750                                             'gnus-wheel-edge direction)
11751                          nil))
11752                    (or (> edge gnus-wheel-edge-resistance)
11753                        (let ((buffer-read-only nil)
11754                              (inhibit-read-only t))
11755                          (put-text-property (point-min) (point-max)
11756                                             'gnus-wheel-edge
11757                                             (* (1+ edge) direction))
11758                          nil))
11759                    (eq last-command 'gnus-wheel-summary-scroll))))
11760       (gnus-summary-next-article nil nil (minusp direction)))))
11761
11762 (defun gnus-wheel-install ()
11763   "Enable mouse wheel support on summary window."
11764   (when gnus-use-wheel
11765     (let ((keys
11766            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
11767       (dolist (key keys)
11768         (define-key gnus-summary-mode-map key
11769           'gnus-wheel-summary-scroll)))))
11770
11771 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
11772
11773 ;;;
11774 ;;; Traditional PGP commmands
11775 ;;;
11776
11777 (defun gnus-summary-decrypt-article (&optional force)
11778   "Decrypt the current article in traditional PGP way.
11779 This will have permanent effect only in mail groups.
11780 If FORCE is non-nil, allow editing of articles even in read-only
11781 groups."
11782   (interactive "P")
11783   (gnus-summary-select-article t)
11784   (gnus-eval-in-buffer-window gnus-article-buffer
11785     (save-excursion
11786       (save-restriction
11787         (widen)
11788         (goto-char (point-min))
11789         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
11790           (error "Not a traditional PGP message!"))
11791         (let ((armor-start (match-beginning 0)))
11792           (if (and (pgg-decrypt-region armor-start (point-max))
11793                    (or force (not (gnus-group-read-only-p))))
11794               (let ((inhibit-read-only t)
11795                     buffer-read-only)
11796                 (delete-region armor-start
11797                                (progn
11798                                  (re-search-forward "^-+END PGP" nil t)
11799                                  (beginning-of-line 2)
11800                                  (point)))
11801                 (insert-buffer-substring pgg-output-buffer))))))))
11802
11803 (defun gnus-summary-verify-article ()
11804   "Verify the current article in traditional PGP way."
11805   (interactive)
11806   (save-excursion
11807     (set-buffer gnus-original-article-buffer)
11808     (goto-char (point-min))
11809     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
11810       (error "Not a traditional PGP message!"))
11811     (re-search-forward "^-+END PGP" nil t)
11812     (beginning-of-line 2)
11813     (call-interactively (function pgg-verify-region))))
11814
11815 ;;;
11816 ;;; Generic summary marking commands
11817 ;;;
11818
11819 (defvar gnus-summary-marking-alist
11820   '((read gnus-del-mark "d")
11821     (unread gnus-unread-mark "u")
11822     (ticked gnus-ticked-mark "!")
11823     (dormant gnus-dormant-mark "?")
11824     (expirable gnus-expirable-mark "e"))
11825   "An alist of names/marks/keystrokes.")
11826
11827 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11828 (defvar gnus-summary-mark-map)
11829
11830 (defun gnus-summary-make-all-marking-commands ()
11831   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11832   (dolist (elem gnus-summary-marking-alist)
11833     (apply 'gnus-summary-make-marking-command elem)))
11834
11835 (defun gnus-summary-make-marking-command (name mark keystroke)
11836   (let ((map (make-sparse-keymap)))
11837     (define-key gnus-summary-generic-mark-map keystroke map)
11838     (dolist (lway `((next "next" next nil "n")
11839                     (next-unread "next unread" next t "N")
11840                     (prev "previous" prev nil "p")
11841                     (prev-unread "previous unread" prev t "P")
11842                     (nomove "" nil nil ,keystroke)))
11843       (let ((func (gnus-summary-make-marking-command-1
11844                    mark (car lway) lway name)))
11845         (setq func (eval func))
11846         (define-key map (nth 4 lway) func)))))
11847
11848 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11849   `(defun ,(intern
11850             (format "gnus-summary-put-mark-as-%s%s"
11851                     name (if (eq way 'nomove)
11852                              ""
11853                            (concat "-" (symbol-name way)))))
11854      (n)
11855      ,(format
11856        "Mark the current article as %s%s.
11857 If N, the prefix, then repeat N times.
11858 If N is negative, move in reverse order.
11859 The difference between N and the actual number of articles marked is
11860 returned."
11861        name (car (cdr lway)))
11862      (interactive "p")
11863      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11864
11865 (defun gnus-summary-generic-mark (n mark move unread)
11866   "Mark N articles with MARK."
11867   (unless (eq major-mode 'gnus-summary-mode)
11868     (error "This command can only be used in the summary buffer"))
11869   (gnus-summary-show-thread)
11870   (let ((nummove
11871          (cond
11872           ((eq move 'next) 1)
11873           ((eq move 'prev) -1)
11874           (t 0))))
11875     (if (zerop nummove)
11876         (setq n 1)
11877       (when (< n 0)
11878         (setq n (abs n)
11879               nummove (* -1 nummove))))
11880     (while (and (> n 0)
11881                 (gnus-summary-mark-article nil mark)
11882                 (zerop (gnus-summary-next-subject nummove unread t)))
11883       (setq n (1- n)))
11884     (when (/= 0 n)
11885       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11886     (gnus-summary-recenter)
11887     (gnus-summary-position-point)
11888     (gnus-set-mode-line 'summary)
11889     n))
11890
11891 (defun gnus-summary-insert-articles (articles)
11892   (when (setq articles
11893               (gnus-sorted-difference articles
11894                                       (mapcar (lambda (h)
11895                                                 (mail-header-number h))
11896                                               gnus-newsgroup-headers)))
11897     (setq gnus-newsgroup-headers
11898           (gnus-merge 'list
11899                       gnus-newsgroup-headers
11900                       (gnus-fetch-headers articles)
11901                       'gnus-article-sort-by-number))
11902     ;; Suppress duplicates?
11903     (when gnus-suppress-duplicates
11904       (gnus-dup-suppress-articles))
11905
11906     ;; We might want to build some more threads first.
11907     (when (and gnus-fetch-old-headers
11908                (eq gnus-headers-retrieved-by 'nov))
11909       (if (eq gnus-fetch-old-headers 'invisible)
11910           (gnus-build-all-threads)
11911         (gnus-build-old-threads)))
11912     ;; Let the Gnus agent mark articles as read.
11913     (when gnus-agent
11914       (gnus-agent-get-undownloaded-list))
11915     ;; Remove list identifiers from subject
11916     (when gnus-list-identifiers
11917       (gnus-summary-remove-list-identifiers))
11918     ;; First and last article in this newsgroup.
11919     (when gnus-newsgroup-headers
11920       (setq gnus-newsgroup-begin
11921             (mail-header-number (car gnus-newsgroup-headers))
11922             gnus-newsgroup-end
11923             (mail-header-number
11924              (gnus-last-element gnus-newsgroup-headers))))
11925     (when gnus-use-scoring
11926       (gnus-possibly-score-headers))))
11927
11928 (defun gnus-summary-insert-old-articles (&optional all)
11929   "Insert all old articles in this group.
11930 If ALL is non-nil, already read articles become readable.
11931 If ALL is a number, fetch this number of articles."
11932   (interactive "P")
11933   (prog1
11934       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11935             older len)
11936         (setq older
11937               ;; Some nntp servers lie about their active range.  When
11938               ;; this happens, the active range can be in the millions.
11939               ;; Use a compressed range to avoid creating a huge list.
11940               (gnus-range-difference (list gnus-newsgroup-active) old))
11941         (setq len (gnus-range-length older))
11942         (cond
11943          ((null older) nil)
11944          ((numberp all)
11945           (if (< all len)
11946               (let ((older-range (nreverse older)))
11947                 (setq older nil)
11948
11949                 (while (> all 0)
11950                   (let* ((r (pop older-range))
11951                          (min (if (numberp r) r (car r)))
11952                          (max (if (numberp r) r (cdr r))))
11953                     (while (and (<= min max)
11954                                 (> all 0))
11955                       (push max older)
11956                       (setq all (1- all)
11957                             max (1- max))))))
11958             (setq older (gnus-uncompress-range older))))
11959          (all
11960           (setq older (gnus-uncompress-range older)))
11961          (t
11962           (when (and (numberp gnus-large-newsgroup)
11963                    (> len gnus-large-newsgroup))
11964               (let* ((cursor-in-echo-area nil)
11965                      (initial (gnus-parameter-large-newsgroup-initial
11966                                gnus-newsgroup-name))
11967                      (input
11968                       (read-string
11969                        (format
11970                         "How many articles from %s (%s %d): "
11971                         (gnus-limit-string
11972                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11973                         (if initial "max" "default")
11974                         len)
11975                        (if initial
11976                            (cons (number-to-string initial)
11977                                  0)))))
11978                 (unless (string-match "^[ \t]*$" input)
11979                   (setq all (string-to-number input))
11980                   (if (< all len)
11981                       (let ((older-range (nreverse older)))
11982                         (setq older nil)
11983
11984                         (while (> all 0)
11985                           (let* ((r (pop older-range))
11986                                  (min (if (numberp r) r (car r)))
11987                                  (max (if (numberp r) r (cdr r))))
11988                             (while (and (<= min max)
11989                                         (> all 0))
11990                               (push max older)
11991                               (setq all (1- all)
11992                                     max (1- max))))))))))
11993           (setq older (gnus-uncompress-range older))))
11994         (if (not older)
11995             (message "No old news.")
11996           (gnus-summary-insert-articles older)
11997           (gnus-summary-limit (gnus-sorted-nunion old older))))
11998     (gnus-summary-position-point)))
11999
12000 (defun gnus-summary-insert-new-articles ()
12001   "Insert all new articles in this group."
12002   (interactive)
12003   (prog1
12004       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12005             (old-active gnus-newsgroup-active)
12006             (nnmail-fetched-sources (list t))
12007             i new)
12008         (setq gnus-newsgroup-active
12009               (gnus-activate-group gnus-newsgroup-name 'scan))
12010         (setq i (cdr gnus-newsgroup-active))
12011         (while (> i (cdr old-active))
12012           (push i new)
12013           (decf i))
12014         (if (not new)
12015             (message "No gnus is bad news")
12016           (gnus-summary-insert-articles new)
12017           (setq gnus-newsgroup-unreads
12018                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12019           (gnus-summary-limit (gnus-sorted-nunion old new))))
12020     (gnus-summary-position-point)))
12021
12022 (gnus-summary-make-all-marking-commands)
12023
12024 (gnus-ems-redefine)
12025
12026 (provide 'gnus-sum)
12027
12028 (run-hooks 'gnus-sum-load-hook)
12029
12030 ;; Local Variables:
12031 ;; coding: iso-8859-1
12032 ;; End:
12033
12034 ;;; gnus-sum.el ends here