5c6b0970113bf040bffbdbfa5756c349b5d1c28b
[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 (defun gnus-widget-reversible-match (widget value)
707   "Ignoring WIDGET, convert VALUE to internal form.
708 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
709   ;; (debug value)
710   (or (symbolp value)
711       (and (listp value)
712            (eq (length value) 2)
713            (eq (nth 0 value) 'not)
714            (symbolp (nth 1 value)))))
715
716 (defun gnus-widget-reversible-to-internal (widget value)
717   "Ignoring WIDGET, convert VALUE to internal form.
718 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
719 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
720   ;; (debug value)
721   (if (atom value)
722       (list value nil)
723     (list (nth 1 value) t)))
724
725 (defun gnus-widget-reversible-to-external (widget value)
726   "Ignoring WIDGET, convert VALUE to external form.
727 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
728 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
729   ;; (debug value)
730   (if (nth 1 value)
731       (list 'not (nth 0 value))
732     (nth 0 value)))
733
734 (define-widget 'gnus-widget-reversible 'group
735   "A `group' that convert values."
736   :match 'gnus-widget-reversible-match
737   :value-to-internal 'gnus-widget-reversible-to-internal
738   :value-to-external 'gnus-widget-reversible-to-external)
739                         
740
741 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
742   "*List of functions used for sorting articles in the summary buffer.
743
744 Each function takes two articles and returns non-nil if the first
745 article should be sorted before the other.  If you use more than one
746 function, the primary sort function should be the last.  You should
747 probably always include `gnus-article-sort-by-number' in the list of
748 sorting functions -- preferably first.  Also note that sorting by date
749 is often much slower than sorting by number, and the sorting order is
750 very similar.  (Sorting by date means sorting by the time the message
751 was sent, sorting by number means sorting by arrival time.)
752
753 Each item can also be a list `(not F)' where F is a function;
754 this reverses the sort order.
755
756 Ready-made functions include `gnus-article-sort-by-number',
757 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
758 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
759 and `gnus-article-sort-by-score'.
760
761 When threading is turned on, the variable `gnus-thread-sort-functions'
762 controls how articles are sorted."
763   :group 'gnus-summary-sort
764   :type '(repeat (gnus-widget-reversible
765                   (choice (function-item gnus-article-sort-by-number)
766                           (function-item gnus-article-sort-by-author)
767                           (function-item gnus-article-sort-by-subject)
768                           (function-item gnus-article-sort-by-date)
769                           (function-item gnus-article-sort-by-score)
770                           (function-item gnus-article-sort-by-random)
771                           (function :tag "other"))
772                   (boolean :tag "Reverse order"))))
773
774
775 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
776   "*List of functions used for sorting threads in the summary buffer.
777 By default, threads are sorted by article number.
778
779 Each function takes two threads and returns non-nil if the first
780 thread should be sorted before the other.  If you use more than one
781 function, the primary sort function should be the last.  You should
782 probably always include `gnus-thread-sort-by-number' in the list of
783 sorting functions -- preferably first.  Also note that sorting by date
784 is often much slower than sorting by number, and the sorting order is
785 very similar.  (Sorting by date means sorting by the time the message
786 was sent, sorting by number means sorting by arrival time.)
787
788 Each list item can also be a list `(not F)' where F is a
789 function; this specifies reversed sort order.
790
791 Ready-made functions include `gnus-thread-sort-by-number',
792 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
793 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
794 `gnus-thread-sort-by-most-recent-number',
795 `gnus-thread-sort-by-most-recent-date',
796 `gnus-thread-sort-by-random', and
797 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
798
799 When threading is turned off, the variable
800 `gnus-article-sort-functions' controls how articles are sorted."
801   :group 'gnus-summary-sort
802   :type '(repeat 
803           (gnus-widget-reversible
804            (choice (function-item gnus-thread-sort-by-number)
805                    (function-item gnus-thread-sort-by-author)
806                    (function-item gnus-thread-sort-by-subject)
807                    (function-item gnus-thread-sort-by-date)
808                    (function-item gnus-thread-sort-by-score)
809                    (function-item gnus-thread-sort-by-most-recent-number)
810                    (function-item gnus-thread-sort-by-most-recent-date)
811                    (function-item gnus-thread-sort-by-random)
812                    (function-item gnus-thread-sort-by-total-score)
813                    (function :tag "other"))
814            (boolean :tag "Reverse order"))))
815
816 (defcustom gnus-thread-score-function '+
817   "*Function used for calculating the total score of a thread.
818
819 The function is called with the scores of the article and each
820 subthread and should then return the score of the thread.
821
822 Some functions you can use are `+', `max', or `min'."
823   :group 'gnus-summary-sort
824   :type 'function)
825
826 (defcustom gnus-summary-expunge-below nil
827   "All articles that have a score less than this variable will be expunged.
828 This variable is local to the summary buffers."
829   :group 'gnus-score-default
830   :type '(choice (const :tag "off" nil)
831                  integer))
832
833 (defcustom gnus-thread-expunge-below nil
834   "All threads that have a total score less than this variable will be expunged.
835 See `gnus-thread-score-function' for en explanation of what a
836 \"thread score\" is.
837
838 This variable is local to the summary buffers."
839   :group 'gnus-threading
840   :group 'gnus-score-default
841   :type '(choice (const :tag "off" nil)
842                  integer))
843
844 (defcustom gnus-summary-mode-hook nil
845   "*A hook for Gnus summary mode.
846 This hook is run before any variables are set in the summary buffer."
847   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
848   :group 'gnus-summary-various
849   :type 'hook)
850
851 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
852 (when (featurep 'xemacs)
853   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
854   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
855   (add-hook 'gnus-summary-mode-hook
856             'gnus-xmas-switch-horizontal-scrollbar-off))
857
858 (defcustom gnus-summary-menu-hook nil
859   "*Hook run after the creation of the summary mode menu."
860   :group 'gnus-summary-visual
861   :type 'hook)
862
863 (defcustom gnus-summary-exit-hook nil
864   "*A hook called on exit from the summary buffer.
865 It will be called with point in the group buffer."
866   :group 'gnus-summary-exit
867   :type 'hook)
868
869 (defcustom gnus-summary-prepare-hook nil
870   "*A hook called after the summary buffer has been generated.
871 If you want to modify the summary buffer, you can use this hook."
872   :group 'gnus-summary-various
873   :type 'hook)
874
875 (defcustom gnus-summary-prepared-hook nil
876   "*A hook called as the last thing after the summary buffer has been generated."
877   :group 'gnus-summary-various
878   :type 'hook)
879
880 (defcustom gnus-summary-generate-hook nil
881   "*A hook run just before generating the summary buffer.
882 This hook is commonly used to customize threading variables and the
883 like."
884   :group 'gnus-summary-various
885   :type 'hook)
886
887 (defcustom gnus-select-group-hook nil
888   "*A hook called when a newsgroup is selected.
889
890 If you'd like to simplify subjects like the
891 `gnus-summary-next-same-subject' command does, you can use the
892 following hook:
893
894  (add-hook gnus-select-group-hook
895            (lambda ()
896              (mapcar (lambda (header)
897                        (mail-header-set-subject
898                         header
899                         (gnus-simplify-subject
900                          (mail-header-subject header) 're-only)))
901                      gnus-newsgroup-headers)))"
902   :group 'gnus-group-select
903   :type 'hook)
904
905 (defcustom gnus-select-article-hook nil
906   "*A hook called when an article is selected."
907   :group 'gnus-summary-choose
908   :options '(gnus-agent-fetch-selected-article)
909   :type 'hook)
910
911 (defcustom gnus-visual-mark-article-hook
912   (list 'gnus-highlight-selected-summary)
913   "*Hook run after selecting an article in the summary buffer.
914 It is meant to be used for highlighting the article in some way.  It
915 is not run if `gnus-visual' is nil."
916   :group 'gnus-summary-visual
917   :type 'hook)
918
919 (defcustom gnus-parse-headers-hook '(gnus-summary-inherit-default-charset)
920   "*A hook called before parsing the headers."
921   :group 'gnus-various
922   :type 'hook)
923
924 (defcustom gnus-exit-group-hook nil
925   "*A hook called when exiting summary mode.
926 This hook is not called from the non-updating exit commands like `Q'."
927   :group 'gnus-various
928   :type 'hook)
929
930 (defcustom gnus-summary-update-hook
931   (list 'gnus-summary-highlight-line)
932   "*A hook called when a summary line is changed.
933 The hook will not be called if `gnus-visual' is nil.
934
935 The default function `gnus-summary-highlight-line' will
936 highlight the line according to the `gnus-summary-highlight'
937 variable."
938   :group 'gnus-summary-visual
939   :type 'hook)
940
941 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
942   "*A hook called when an article is selected for the first time.
943 The hook is intended to mark an article as read (or unread)
944 automatically when it is selected."
945   :group 'gnus-summary-choose
946   :type 'hook)
947
948 (defcustom gnus-group-no-more-groups-hook nil
949   "*A hook run when returning to group mode having no more (unread) groups."
950   :group 'gnus-group-select
951   :type 'hook)
952
953 (defcustom gnus-ps-print-hook nil
954   "*A hook run before ps-printing something from Gnus."
955   :group 'gnus-summary
956   :type 'hook)
957
958 (defcustom gnus-summary-article-move-hook nil
959   "*A hook called after an article is moved, copied, respooled, or crossposted."
960   :group 'gnus-summary
961   :type 'hook)
962
963 (defcustom gnus-summary-article-delete-hook nil
964   "*A hook called after an article is deleted."
965   :group 'gnus-summary
966   :type 'hook)
967
968 (defcustom gnus-summary-article-expire-hook nil
969   "*A hook called after an article is expired."
970   :group 'gnus-summary
971   :type 'hook)
972
973 (defcustom gnus-summary-display-arrow
974   (and (fboundp 'display-graphic-p)
975        (display-graphic-p))
976   "*If non-nil, display an arrow highlighting the current article."
977   :version "21.1"
978   :group 'gnus-summary
979   :type 'boolean)
980
981 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
982   "Face used for highlighting the current article in the summary buffer."
983   :group 'gnus-summary-visual
984   :type 'face)
985
986 (defvar gnus-tmp-downloaded nil)
987
988 (defcustom gnus-summary-highlight
989   '(((eq mark gnus-canceled-mark)
990      . gnus-summary-cancelled-face)
991     ((and uncached (> score default-high))
992      . gnus-summary-high-undownloaded-face)
993     ((and uncached (< score default-low))
994      . gnus-summary-low-undownloaded-face)
995     (uncached
996      . gnus-summary-normal-undownloaded-face)
997     ((and (> score default-high)
998           (or (eq mark gnus-dormant-mark)
999               (eq mark gnus-ticked-mark)))
1000      . gnus-summary-high-ticked-face)
1001     ((and (< score default-low)
1002           (or (eq mark gnus-dormant-mark)
1003               (eq mark gnus-ticked-mark)))
1004      . gnus-summary-low-ticked-face)
1005     ((or (eq mark gnus-dormant-mark)
1006          (eq mark gnus-ticked-mark))
1007      . gnus-summary-normal-ticked-face)
1008     ((and (> score default-high) (eq mark gnus-ancient-mark))
1009      . gnus-summary-high-ancient-face)
1010     ((and (< score default-low) (eq mark gnus-ancient-mark))
1011      . gnus-summary-low-ancient-face)
1012     ((eq mark gnus-ancient-mark)
1013      . gnus-summary-normal-ancient-face)
1014     ((and (> score default-high) (eq mark gnus-unread-mark))
1015      . gnus-summary-high-unread-face)
1016     ((and (< score default-low) (eq mark gnus-unread-mark))
1017      . gnus-summary-low-unread-face)
1018     ((eq mark gnus-unread-mark)
1019      . gnus-summary-normal-unread-face)
1020     ((> score default-high)
1021      . gnus-summary-high-read-face)
1022     ((< score default-low)
1023      . gnus-summary-low-read-face)
1024     (t
1025      . gnus-summary-normal-read-face))
1026   "*Controls the highlighting of summary buffer lines.
1027
1028 A list of (FORM . FACE) pairs.  When deciding how a a particular
1029 summary line should be displayed, each form is evaluated.  The content
1030 of the face field after the first true form is used.  You can change
1031 how those summary lines are displayed, by editing the face field.
1032
1033 You can use the following variables in the FORM field.
1034
1035 score:        The article's score
1036 default:      The default article score.
1037 default-high: The default score for high scored articles.
1038 default-low:  The default score for low scored articles.
1039 below:        The score below which articles are automatically marked as read.
1040 mark:         The article's mark.
1041 uncached:     Non-nil if the article is uncached."
1042   :group 'gnus-summary-visual
1043   :type '(repeat (cons (sexp :tag "Form" nil)
1044                        face)))
1045
1046 (defcustom gnus-alter-header-function nil
1047   "Function called to allow alteration of article header structures.
1048 The function is called with one parameter, the article header vector,
1049 which it may alter in any way."
1050   :type '(choice (const :tag "None" nil)
1051                  function)
1052   :group 'gnus-summary)
1053
1054 (defvar gnus-decode-encoded-word-function
1055   (mime-find-field-decoder 'From 'nov)
1056   "Variable that says which function should be used to decode a string with encoded words.")
1057
1058 (defcustom gnus-extra-headers '(To Newsgroups)
1059   "*Extra headers to parse."
1060   :version "21.1"
1061   :group 'gnus-summary
1062   :type '(repeat symbol))
1063
1064 (defcustom gnus-ignored-from-addresses
1065   (and user-mail-address (regexp-quote user-mail-address))
1066   "*Regexp of From headers that may be suppressed in favor of To headers."
1067   :version "21.1"
1068   :group 'gnus-summary
1069   :type 'regexp)
1070
1071 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1072   "List of charsets that should be ignored.
1073 When these charsets are used in the \"charset\" parameter, the
1074 default charset will be used instead."
1075   :version "21.1"
1076   :type '(repeat symbol)
1077   :group 'gnus-charset)
1078
1079 (gnus-define-group-parameter
1080  ignored-charsets
1081  :type list
1082  :function-document
1083  "Return the ignored charsets of GROUP."
1084  :variable gnus-group-ignored-charsets-alist
1085  :variable-default
1086  '(("alt\\.chinese\\.text" iso-8859-1))
1087  :variable-document
1088  "Alist of regexps (to match group names) and charsets that should be ignored.
1089 When these charsets are used in the \"charset\" parameter, the
1090 default charset will be used instead."
1091  :variable-group gnus-charset
1092  :variable-type '(repeat (cons (regexp :tag "Group")
1093                                (repeat symbol)))
1094  :parameter-type '(choice :tag "Ignored charsets"
1095                           :value nil
1096                           (repeat (symbol)))
1097  :parameter-document       "\
1098 List of charsets that should be ignored.
1099
1100 When these charsets are used in the \"charset\" parameter, the
1101 default charset will be used instead.")
1102
1103 (defcustom gnus-group-highlight-words-alist nil
1104   "Alist of group regexps and highlight regexps.
1105 This variable uses the same syntax as `gnus-emphasis-alist'."
1106   :version "21.1"
1107   :type '(repeat (cons (regexp :tag "Group")
1108                        (repeat (list (regexp :tag "Highlight regexp")
1109                                      (number :tag "Group for entire word" 0)
1110                                      (number :tag "Group for displayed part" 0)
1111                                      (symbol :tag "Face"
1112                                              gnus-emphasis-highlight-words)))))
1113   :group 'gnus-summary-visual)
1114
1115 (defcustom gnus-use-wheel nil
1116   "Use Intelli-mouse on summary movement"
1117   :type 'boolean
1118   :group 'gnus-summary-maneuvering)
1119
1120 (defcustom gnus-wheel-scroll-amount '(5 . 1)
1121   "Amount to scroll messages by spinning the mouse wheel.
1122 This is actually a cons cell, where the first item is the amount to scroll
1123 on a normal wheel event, and the second is the amount to scroll when the
1124 wheel is moved with the shift key depressed."
1125   :type '(cons (integer :tag "Shift") integer)
1126   :group 'gnus-summary-maneuvering)
1127
1128 (defcustom gnus-wheel-edge-resistance 2
1129   "How hard it should be to change the current article
1130 by moving the mouse over the edge of the article window."
1131   :type 'integer
1132   :group 'gnus-summary-maneuvering)
1133
1134 (defcustom gnus-summary-show-article-charset-alist
1135   nil
1136   "Alist of number and charset.
1137 The article will be shown with the charset corresponding to the
1138 numbered argument.
1139 For example: ((1 . cn-gb-2312) (2 . big5))."
1140   :version "21.1"
1141   :type '(repeat (cons (number :tag "Argument" 1)
1142                        (symbol :tag "Charset")))
1143   :group 'gnus-charset)
1144
1145 (defcustom gnus-preserve-marks t
1146   "Whether marks are preserved when moving, copying and respooling messages."
1147   :version "21.1"
1148   :type 'boolean
1149   :group 'gnus-summary-marks)
1150
1151 (defcustom gnus-alter-articles-to-read-function nil
1152   "Function to be called to alter the list of articles to be selected."
1153   :type '(choice (const nil) function)
1154   :group 'gnus-summary)
1155
1156 (defcustom gnus-orphan-score nil
1157   "*All orphans get this score added.  Set in the score file."
1158   :group 'gnus-score-default
1159   :type '(choice (const nil)
1160                  integer))
1161
1162 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1163   "*A regexp to match MIME parts when saving multiple parts of a
1164 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1165 This regexp will be used by default when prompting the user for which
1166 type of files to save."
1167   :group 'gnus-summary
1168   :type 'regexp)
1169
1170 (defcustom gnus-read-all-available-headers nil
1171   "Whether Gnus should parse all headers made available to it.
1172 This is mostly relevant for slow back ends where the user may
1173 wish to widen the summary buffer to include all headers
1174 that were fetched.  Say, for nnultimate groups."
1175   :group 'gnus-summary
1176   :type '(choice boolean regexp))
1177
1178 (defcustom gnus-summary-muttprint-program "muttprint"
1179   "Command (and optional arguments) used to run Muttprint."
1180   :version "21.3"
1181   :group 'gnus-summary
1182   :type 'string)
1183
1184 (defcustom gnus-article-loose-mime nil
1185   "If non-nil, don't require MIME-Version header.
1186 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1187 supply the MIME-Version header or deliberately strip it From the mail.
1188 Set it to non-nil, Gnus will treat some articles as MIME even if
1189 the MIME-Version header is missed."
1190   :version "21.3"
1191   :type 'boolean
1192   :group 'gnus-article-mime)
1193
1194 (defcustom gnus-article-emulate-mime t
1195   "If non-nil, use MIME emulation for uuencode and the like.
1196 This means that Gnus will search message bodies for text that look
1197 like uuencoded bits, yEncoded bits, and so on, and present that using
1198 the normal Gnus MIME machinery."
1199   :type 'boolean
1200   :group 'gnus-article-mime)
1201
1202 ;;; Internal variables
1203
1204 (defvar gnus-summary-display-cache nil)
1205 (defvar gnus-article-mime-handles nil)
1206 (defvar gnus-article-decoded-p nil)
1207 (defvar gnus-article-charset nil)
1208 (defvar gnus-article-ignored-charsets nil)
1209 (defvar gnus-scores-exclude-files nil)
1210 (defvar gnus-page-broken nil)
1211
1212 (defvar gnus-original-article nil)
1213 (defvar gnus-article-internal-prepare-hook nil)
1214 (defvar gnus-newsgroup-process-stack nil)
1215
1216 (defvar gnus-thread-indent-array nil)
1217 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1218 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1219   "Function called to sort the articles within a thread after it has been gathered together.")
1220
1221 (defvar gnus-summary-save-parts-type-history nil)
1222 (defvar gnus-summary-save-parts-last-directory nil)
1223
1224 ;; Avoid highlighting in kill files.
1225 (defvar gnus-summary-inhibit-highlight nil)
1226 (defvar gnus-newsgroup-selected-overlay nil)
1227 (defvar gnus-inhibit-limiting nil)
1228 (defvar gnus-newsgroup-adaptive-score-file nil)
1229 (defvar gnus-current-score-file nil)
1230 (defvar gnus-current-move-group nil)
1231 (defvar gnus-current-copy-group nil)
1232 (defvar gnus-current-crosspost-group nil)
1233 (defvar gnus-newsgroup-display nil)
1234
1235 (defvar gnus-newsgroup-dependencies nil)
1236 (defvar gnus-newsgroup-adaptive nil)
1237 (defvar gnus-summary-display-article-function nil)
1238 (defvar gnus-summary-highlight-line-function nil
1239   "Function called after highlighting a summary line.")
1240
1241 (defvar gnus-summary-line-format-alist
1242   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1243     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1244     (?s gnus-tmp-subject-or-nil ?s)
1245     (?n gnus-tmp-name ?s)
1246     (?A (std11-address-string
1247          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1248     (?a (or (std11-full-name-string
1249              (car (mime-entity-read-field gnus-tmp-header 'From)))
1250             gnus-tmp-from) ?s)
1251     (?F gnus-tmp-from ?s)
1252     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1253     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1254     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1255     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1256     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1257     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1258     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1259     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1260     (?L gnus-tmp-lines ?s)
1261     (?O gnus-tmp-downloaded ?c)
1262     (?I gnus-tmp-indentation ?s)
1263     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1264     (?R gnus-tmp-replied ?c)
1265     (?\[ gnus-tmp-opening-bracket ?c)
1266     (?\] gnus-tmp-closing-bracket ?c)
1267     (?\> (make-string gnus-tmp-level ? ) ?s)
1268     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1269     (?i gnus-tmp-score ?d)
1270     (?z gnus-tmp-score-char ?c)
1271     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1272     (?U gnus-tmp-unread ?c)
1273     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1274         ?s)
1275     (?t (gnus-summary-number-of-articles-in-thread
1276          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1277         ?d)
1278     (?e (gnus-summary-number-of-articles-in-thread
1279          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1280         ?c)
1281     (?u gnus-tmp-user-defined ?s)
1282     (?P (gnus-pick-line-number) ?d)
1283     (?B gnus-tmp-thread-tree-header-string ?s)
1284     (user-date (gnus-user-date
1285                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1286   "An alist of format specifications that can appear in summary lines.
1287 These are paired with what variables they correspond with, along with
1288 the type of the variable (string, integer, character, etc).")
1289
1290 (defvar gnus-summary-dummy-line-format-alist
1291   `((?S gnus-tmp-subject ?s)
1292     (?N gnus-tmp-number ?d)
1293     (?u gnus-tmp-user-defined ?s)))
1294
1295 (defvar gnus-summary-mode-line-format-alist
1296   `((?G gnus-tmp-group-name ?s)
1297     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1298     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1299     (?A gnus-tmp-article-number ?d)
1300     (?Z gnus-tmp-unread-and-unselected ?s)
1301     (?V gnus-version ?s)
1302     (?U gnus-tmp-unread-and-unticked ?d)
1303     (?S gnus-tmp-subject ?s)
1304     (?e gnus-tmp-unselected ?d)
1305     (?u gnus-tmp-user-defined ?s)
1306     (?d (length gnus-newsgroup-dormant) ?d)
1307     (?t (length gnus-newsgroup-marked) ?d)
1308     (?h (length gnus-newsgroup-spam-marked) ?d)
1309     (?r (length gnus-newsgroup-reads) ?d)
1310     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1311     (?E gnus-newsgroup-expunged-tally ?d)
1312     (?s (gnus-current-score-file-nondirectory) ?s)))
1313
1314 (defvar gnus-last-search-regexp nil
1315   "Default regexp for article search command.")
1316
1317 (defvar gnus-summary-search-article-matched-data nil
1318   "Last matched data of article search command.  It is the local variable
1319 in `gnus-article-buffer' which consists of the list of start position,
1320 end position and text.")
1321
1322 (defvar gnus-last-shell-command nil
1323   "Default shell command on article.")
1324
1325 (defvar gnus-newsgroup-agentized nil
1326   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1327 (defvar gnus-newsgroup-begin nil)
1328 (defvar gnus-newsgroup-end nil)
1329 (defvar gnus-newsgroup-last-rmail nil)
1330 (defvar gnus-newsgroup-last-mail nil)
1331 (defvar gnus-newsgroup-last-folder nil)
1332 (defvar gnus-newsgroup-last-file nil)
1333 (defvar gnus-newsgroup-auto-expire nil)
1334 (defvar gnus-newsgroup-active nil)
1335
1336 (defvar gnus-newsgroup-data nil)
1337 (defvar gnus-newsgroup-data-reverse nil)
1338 (defvar gnus-newsgroup-limit nil)
1339 (defvar gnus-newsgroup-limits nil)
1340 (defvar gnus-summary-use-undownloaded-faces nil)
1341
1342 (defvar gnus-newsgroup-unreads nil
1343   "Sorted list of unread articles in the current newsgroup.")
1344
1345 (defvar gnus-newsgroup-unselected nil
1346   "Sorted list of unselected unread articles in the current newsgroup.")
1347
1348 (defvar gnus-newsgroup-reads nil
1349   "Alist of read articles and article marks in the current newsgroup.")
1350
1351 (defvar gnus-newsgroup-expunged-tally nil)
1352
1353 (defvar gnus-newsgroup-marked nil
1354   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1355
1356 (defvar gnus-newsgroup-spam-marked nil
1357   "List of ranges of articles that have been marked as spam.")
1358
1359 (defvar gnus-newsgroup-killed nil
1360   "List of ranges of articles that have been through the scoring process.")
1361
1362 (defvar gnus-newsgroup-cached nil
1363   "Sorted list of articles that come from the article cache.")
1364
1365 (defvar gnus-newsgroup-saved nil
1366   "List of articles that have been saved.")
1367
1368 (defvar gnus-newsgroup-kill-headers nil)
1369
1370 (defvar gnus-newsgroup-replied nil
1371   "List of articles that have been replied to in the current newsgroup.")
1372
1373 (defvar gnus-newsgroup-forwarded nil
1374   "List of articles that have been forwarded in the current newsgroup.")
1375
1376 (defvar gnus-newsgroup-recent nil
1377   "List of articles that have are recent in the current newsgroup.")
1378
1379 (defvar gnus-newsgroup-expirable nil
1380   "Sorted list of articles in the current newsgroup that can be expired.")
1381
1382 (defvar gnus-newsgroup-processable nil
1383   "List of articles in the current newsgroup that can be processed.")
1384
1385 (defvar gnus-newsgroup-downloadable nil
1386   "Sorted list of articles in the current newsgroup that can be processed.")
1387
1388 (defvar gnus-newsgroup-unfetched nil
1389   "Sorted list of articles in the current newsgroup whose headers have
1390 not been fetched into the agent.
1391
1392 This list will always be a subset of gnus-newsgroup-undownloaded.")
1393
1394 (defvar gnus-newsgroup-undownloaded nil
1395   "List of articles in the current newsgroup that haven't been downloaded.")
1396
1397 (defvar gnus-newsgroup-unsendable nil
1398   "List of articles in the current newsgroup that won't be sent.")
1399
1400 (defvar gnus-newsgroup-bookmarks nil
1401   "List of articles in the current newsgroup that have bookmarks.")
1402
1403 (defvar gnus-newsgroup-dormant nil
1404   "Sorted list of dormant articles in the current newsgroup.")
1405
1406 (defvar gnus-newsgroup-unseen nil
1407   "List of unseen articles in the current newsgroup.")
1408
1409 (defvar gnus-newsgroup-seen nil
1410   "Range of seen articles in the current newsgroup.")
1411
1412 (defvar gnus-newsgroup-articles nil
1413   "List of articles in the current newsgroup.")
1414
1415 (defvar gnus-newsgroup-scored nil
1416   "List of scored articles in the current newsgroup.")
1417
1418 (defvar gnus-newsgroup-incorporated nil
1419   "List of incorporated articles in the current newsgroup.")
1420
1421 (defvar gnus-newsgroup-headers nil
1422   "List of article headers in the current newsgroup.")
1423
1424 (defvar gnus-newsgroup-threads nil)
1425
1426 (defvar gnus-newsgroup-prepared nil
1427   "Whether the current group has been prepared properly.")
1428
1429 (defvar gnus-newsgroup-ancient nil
1430   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1431
1432 (defvar gnus-newsgroup-sparse nil)
1433
1434 (defvar gnus-current-article nil)
1435 (defvar gnus-article-current nil)
1436 (defvar gnus-current-headers nil)
1437 (defvar gnus-have-all-headers nil)
1438 (defvar gnus-last-article nil)
1439 (defvar gnus-newsgroup-history nil)
1440 (defvar gnus-newsgroup-charset nil)
1441 (defvar gnus-newsgroup-ephemeral-charset nil)
1442 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1443
1444 (defvar gnus-article-before-search nil)
1445
1446 (defvar gnus-summary-local-variables
1447   '(gnus-newsgroup-name
1448     gnus-newsgroup-begin gnus-newsgroup-end
1449     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1450     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1451     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1452     gnus-newsgroup-unselected gnus-newsgroup-marked
1453     gnus-newsgroup-spam-marked
1454     gnus-newsgroup-reads gnus-newsgroup-saved
1455     gnus-newsgroup-replied gnus-newsgroup-forwarded
1456     gnus-newsgroup-recent
1457     gnus-newsgroup-expirable
1458     gnus-newsgroup-processable gnus-newsgroup-killed
1459     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1460     gnus-newsgroup-unfetched
1461     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1462     gnus-newsgroup-seen gnus-newsgroup-articles
1463     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1464     gnus-newsgroup-headers gnus-newsgroup-threads
1465     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1466     gnus-current-article gnus-current-headers gnus-have-all-headers
1467     gnus-last-article gnus-article-internal-prepare-hook
1468     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1469     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1470     gnus-thread-expunge-below
1471     gnus-score-alist gnus-current-score-file
1472     (gnus-summary-expunge-below . global)
1473     (gnus-summary-mark-below . global)
1474     (gnus-orphan-score . global)
1475     gnus-newsgroup-active gnus-scores-exclude-files
1476     gnus-newsgroup-history gnus-newsgroup-ancient
1477     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1478     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1479     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1480     (gnus-newsgroup-expunged-tally . 0)
1481     gnus-cache-removable-articles gnus-newsgroup-cached
1482     gnus-newsgroup-data gnus-newsgroup-data-reverse
1483     gnus-newsgroup-limit gnus-newsgroup-limits
1484     gnus-newsgroup-charset gnus-newsgroup-display
1485     gnus-summary-use-undownloaded-faces
1486     gnus-newsgroup-incorporated)
1487   "Variables that are buffer-local to the summary buffers.")
1488
1489 (defvar gnus-newsgroup-variables nil
1490   "A list of variables that have separate values in different newsgroups.
1491 A list of newsgroup (summary buffer) local variables, or cons of
1492 variables and their default expressions to be evalled (when the default
1493 values are not nil), that should be made global while the summary buffer
1494 is active.
1495
1496 Note: The default expressions will be evaluated (using function `eval')
1497 before assignment to the local variable rather than just assigned to it.
1498 If the default expression is the symbol `global', that symbol will not
1499 be evaluated but the global value of the local variable will be used
1500 instead.
1501
1502 These variables can be used to set variables in the group parameters
1503 while still allowing them to affect operations done in other buffers.
1504 For example:
1505
1506 \(setq gnus-newsgroup-variables
1507      '(message-use-followup-to
1508        (gnus-visible-headers .
1509          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1510 ")
1511
1512 ;; Byte-compiler warning.
1513 (eval-when-compile
1514   ;; Bind features so that require will believe that gnus-sum has
1515   ;; already been loaded (avoids infinite recursion)
1516   (let ((features (cons 'gnus-sum features)))
1517     ;; Several of the declarations in gnus-sum are needed to load the
1518     ;; following files. Right now, these definitions have been
1519     ;; compiled but not defined (evaluated).  We could either do a
1520     ;; eval-and-compile about all of the declarations or evaluate the
1521     ;; source file.
1522     (if (boundp 'gnus-newsgroup-variables)
1523         nil
1524       (load "gnus-sum.el" t t t))
1525     (require 'gnus)
1526     (require 'gnus-agent)
1527     (require 'gnus-art)))
1528
1529 ;; Subject simplification.
1530
1531 (defun gnus-simplify-whitespace (str)
1532   "Remove excessive whitespace from STR."
1533   ;; Multiple spaces.
1534   (while (string-match "[ \t][ \t]+" str)
1535     (setq str (concat (substring str 0 (match-beginning 0))
1536                         " "
1537                         (substring str (match-end 0)))))
1538   ;; Leading spaces.
1539   (when (string-match "^[ \t]+" str)
1540     (setq str (substring str (match-end 0))))
1541   ;; Trailing spaces.
1542   (when (string-match "[ \t]+$" str)
1543     (setq str (substring str 0 (match-beginning 0))))
1544   str)
1545
1546 (defun gnus-simplify-all-whitespace (str)
1547   "Remove all whitespace from STR."
1548   (while (string-match "[ \t\n]+" str)
1549     (setq str (replace-match "" nil nil str)))
1550   str)
1551
1552 (defsubst gnus-simplify-subject-re (subject)
1553   "Remove \"Re:\" from subject lines."
1554   (if (string-match message-subject-re-regexp subject)
1555       (substring subject (match-end 0))
1556     subject))
1557
1558 (defun gnus-simplify-subject (subject &optional re-only)
1559   "Remove `Re:' and words in parentheses.
1560 If RE-ONLY is non-nil, strip leading `Re:'s only."
1561   (let ((case-fold-search t))           ;Ignore case.
1562     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1563     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1564       (setq subject (substring subject (match-end 0))))
1565     ;; Remove uninteresting prefixes.
1566     (when (and (not re-only)
1567                gnus-simplify-ignored-prefixes
1568                (string-match gnus-simplify-ignored-prefixes subject))
1569       (setq subject (substring subject (match-end 0))))
1570     ;; Remove words in parentheses from end.
1571     (unless re-only
1572       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1573         (setq subject (substring subject 0 (match-beginning 0)))))
1574     ;; Return subject string.
1575     subject))
1576
1577 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1578 ;; all whitespace.
1579 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1580   (goto-char (point-min))
1581   (while (re-search-forward regexp nil t)
1582     (replace-match (or newtext ""))))
1583
1584 (defun gnus-simplify-buffer-fuzzy ()
1585   "Simplify string in the buffer fuzzily.
1586 The string in the accessible portion of the current buffer is simplified.
1587 It is assumed to be a single-line subject.
1588 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1589 matter is removed.  Additional things can be deleted by setting
1590 `gnus-simplify-subject-fuzzy-regexp'."
1591   (let ((case-fold-search t)
1592         (modified-tick))
1593     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1594
1595     (while (not (eq modified-tick (buffer-modified-tick)))
1596       (setq modified-tick (buffer-modified-tick))
1597       (cond
1598        ((listp gnus-simplify-subject-fuzzy-regexp)
1599         (mapcar 'gnus-simplify-buffer-fuzzy-step
1600                 gnus-simplify-subject-fuzzy-regexp))
1601        (gnus-simplify-subject-fuzzy-regexp
1602         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1603       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1604       (gnus-simplify-buffer-fuzzy-step
1605        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1606       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1607
1608     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1609     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1610     (gnus-simplify-buffer-fuzzy-step " $")
1611     (gnus-simplify-buffer-fuzzy-step "^ +")))
1612
1613 (defun gnus-simplify-subject-fuzzy (subject)
1614   "Simplify a subject string fuzzily.
1615 See `gnus-simplify-buffer-fuzzy' for details."
1616   (save-excursion
1617     (gnus-set-work-buffer)
1618     (let ((case-fold-search t))
1619       ;; Remove uninteresting prefixes.
1620       (when (and gnus-simplify-ignored-prefixes
1621                  (string-match gnus-simplify-ignored-prefixes subject))
1622         (setq subject (substring subject (match-end 0))))
1623       (insert subject)
1624       (inline (gnus-simplify-buffer-fuzzy))
1625       (buffer-string))))
1626
1627 (defsubst gnus-simplify-subject-fully (subject)
1628   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1629   (cond
1630    (gnus-simplify-subject-functions
1631     (gnus-map-function gnus-simplify-subject-functions subject))
1632    ((null gnus-summary-gather-subject-limit)
1633     (gnus-simplify-subject-re subject))
1634    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1635     (gnus-simplify-subject-fuzzy subject))
1636    ((numberp gnus-summary-gather-subject-limit)
1637     (gnus-limit-string (gnus-simplify-subject-re subject)
1638                        gnus-summary-gather-subject-limit))
1639    (t
1640     subject)))
1641
1642 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1643   "Check whether two subjects are equal.
1644 If optional argument SIMPLE-FIRST is t, first argument is already
1645 simplified."
1646   (cond
1647    ((null simple-first)
1648     (equal (gnus-simplify-subject-fully s1)
1649            (gnus-simplify-subject-fully s2)))
1650    (t
1651     (equal s1
1652            (gnus-simplify-subject-fully s2)))))
1653
1654 (defun gnus-summary-bubble-group ()
1655   "Increase the score of the current group.
1656 This is a handy function to add to `gnus-summary-exit-hook' to
1657 increase the score of each group you read."
1658   (gnus-group-add-score gnus-newsgroup-name))
1659
1660 \f
1661 ;;;
1662 ;;; Gnus summary mode
1663 ;;;
1664
1665 (put 'gnus-summary-mode 'mode-class 'special)
1666
1667 (defvar gnus-article-commands-menu)
1668
1669 ;; Non-orthogonal keys
1670
1671 (gnus-define-keys gnus-summary-mode-map
1672   " " gnus-summary-next-page
1673   "\177" gnus-summary-prev-page
1674   [delete] gnus-summary-prev-page
1675   [backspace] gnus-summary-prev-page
1676   "\r" gnus-summary-scroll-up
1677   "\M-\r" gnus-summary-scroll-down
1678   "n" gnus-summary-next-unread-article
1679   "p" gnus-summary-prev-unread-article
1680   "N" gnus-summary-next-article
1681   "P" gnus-summary-prev-article
1682   "\M-\C-n" gnus-summary-next-same-subject
1683   "\M-\C-p" gnus-summary-prev-same-subject
1684   "\M-n" gnus-summary-next-unread-subject
1685   "\M-p" gnus-summary-prev-unread-subject
1686   "." gnus-summary-first-unread-article
1687   "," gnus-summary-best-unread-article
1688   "\M-s" gnus-summary-search-article-forward
1689   "\M-r" gnus-summary-search-article-backward
1690   "<" gnus-summary-beginning-of-article
1691   ">" gnus-summary-end-of-article
1692   "j" gnus-summary-goto-article
1693   "^" gnus-summary-refer-parent-article
1694   "\M-^" gnus-summary-refer-article
1695   "u" gnus-summary-tick-article-forward
1696   "!" gnus-summary-tick-article-forward
1697   "U" gnus-summary-tick-article-backward
1698   "d" gnus-summary-mark-as-read-forward
1699   "D" gnus-summary-mark-as-read-backward
1700   "E" gnus-summary-mark-as-expirable
1701   "\M-u" gnus-summary-clear-mark-forward
1702   "\M-U" gnus-summary-clear-mark-backward
1703   "k" gnus-summary-kill-same-subject-and-select
1704   "\C-k" gnus-summary-kill-same-subject
1705   "\M-\C-k" gnus-summary-kill-thread
1706   "\M-\C-l" gnus-summary-lower-thread
1707   "e" gnus-summary-edit-article
1708   "#" gnus-summary-mark-as-processable
1709   "\M-#" gnus-summary-unmark-as-processable
1710   "\M-\C-t" gnus-summary-toggle-threads
1711   "\M-\C-s" gnus-summary-show-thread
1712   "\M-\C-h" gnus-summary-hide-thread
1713   "\M-\C-f" gnus-summary-next-thread
1714   "\M-\C-b" gnus-summary-prev-thread
1715   [(meta down)] gnus-summary-next-thread
1716   [(meta up)] gnus-summary-prev-thread
1717   "\M-\C-u" gnus-summary-up-thread
1718   "\M-\C-d" gnus-summary-down-thread
1719   "&" gnus-summary-execute-command
1720   "c" gnus-summary-catchup-and-exit
1721   "\C-w" gnus-summary-mark-region-as-read
1722   "\C-t" gnus-summary-toggle-truncation
1723   "?" gnus-summary-mark-as-dormant
1724   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1725   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1726   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1727   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1728   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1729   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1730   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1731   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1732   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1733   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1734   "=" gnus-summary-expand-window
1735   "\C-x\C-s" gnus-summary-reselect-current-group
1736   "\M-g" gnus-summary-rescan-group
1737   "w" gnus-summary-stop-page-breaking
1738   "\C-c\C-r" gnus-summary-caesar-message
1739   "\M-t" gnus-summary-toggle-mime
1740   "f" gnus-summary-followup
1741   "F" gnus-summary-followup-with-original
1742   "C" gnus-summary-cancel-article
1743   "r" gnus-summary-reply
1744   "R" gnus-summary-reply-with-original
1745   "\C-c\C-f" gnus-summary-mail-forward
1746   "o" gnus-summary-save-article
1747   "\C-o" gnus-summary-save-article-mail
1748   "|" gnus-summary-pipe-output
1749   "\M-k" gnus-summary-edit-local-kill
1750   "\M-K" gnus-summary-edit-global-kill
1751   ;; "V" gnus-version
1752   "\C-c\C-d" gnus-summary-describe-group
1753   "q" gnus-summary-exit
1754   "Q" gnus-summary-exit-no-update
1755   "\C-c\C-i" gnus-info-find-node
1756   gnus-mouse-2 gnus-mouse-pick-article
1757   "m" gnus-summary-mail-other-window
1758   "a" gnus-summary-post-news
1759   "i" gnus-summary-news-other-window
1760   "x" gnus-summary-limit-to-unread
1761   "s" gnus-summary-isearch-article
1762   "t" gnus-summary-toggle-header
1763   "g" gnus-summary-show-article
1764   "l" gnus-summary-goto-last-article
1765   "v" gnus-summary-preview-mime-message
1766   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1767   "\C-d" gnus-summary-enter-digest-group
1768   "\M-\C-d" gnus-summary-read-document
1769   "\M-\C-e" gnus-summary-edit-parameters
1770   "\M-\C-a" gnus-summary-customize-parameters
1771   "\C-c\C-b" gnus-bug
1772   "\C-c\C-n" gnus-namazu-search
1773   "*" gnus-cache-enter-article
1774   "\M-*" gnus-cache-remove-article
1775   "\M-&" gnus-summary-universal-argument
1776   "\C-l" gnus-recenter
1777   "I" gnus-summary-increase-score
1778   "L" gnus-summary-lower-score
1779   "\M-i" gnus-symbolic-argument
1780   "h" gnus-summary-select-article-buffer
1781
1782   "V" gnus-summary-score-map
1783   "X" gnus-uu-extract-map
1784   "S" gnus-summary-send-map)
1785
1786   ;; Sort of orthogonal keymap
1787 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1788   "t" gnus-summary-tick-article-forward
1789   "!" gnus-summary-tick-article-forward
1790   "d" gnus-summary-mark-as-read-forward
1791   "r" gnus-summary-mark-as-read-forward
1792   "c" gnus-summary-clear-mark-forward
1793   " " gnus-summary-clear-mark-forward
1794   "e" gnus-summary-mark-as-expirable
1795   "x" gnus-summary-mark-as-expirable
1796   "?" gnus-summary-mark-as-dormant
1797   "b" gnus-summary-set-bookmark
1798   "B" gnus-summary-remove-bookmark
1799   "#" gnus-summary-mark-as-processable
1800   "\M-#" gnus-summary-unmark-as-processable
1801   "S" gnus-summary-limit-include-expunged
1802   "C" gnus-summary-catchup
1803   "H" gnus-summary-catchup-to-here
1804   "h" gnus-summary-catchup-from-here
1805   "\C-c" gnus-summary-catchup-all
1806   "k" gnus-summary-kill-same-subject-and-select
1807   "K" gnus-summary-kill-same-subject
1808   "P" gnus-uu-mark-map)
1809
1810 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1811   "c" gnus-summary-clear-above
1812   "u" gnus-summary-tick-above
1813   "m" gnus-summary-mark-above
1814   "k" gnus-summary-kill-below)
1815
1816 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1817   "/" gnus-summary-limit-to-subject
1818   "n" gnus-summary-limit-to-articles
1819   "w" gnus-summary-pop-limit
1820   "s" gnus-summary-limit-to-subject
1821   "a" gnus-summary-limit-to-author
1822   "u" gnus-summary-limit-to-unread
1823   "m" gnus-summary-limit-to-marks
1824   "M" gnus-summary-limit-exclude-marks
1825   "v" gnus-summary-limit-to-score
1826   "*" gnus-summary-limit-include-cached
1827   "D" gnus-summary-limit-include-dormant
1828   "T" gnus-summary-limit-include-thread
1829   "d" gnus-summary-limit-exclude-dormant
1830   "t" gnus-summary-limit-to-age
1831   "." gnus-summary-limit-to-unseen
1832   "x" gnus-summary-limit-to-extra
1833   "p" gnus-summary-limit-to-display-predicate
1834   "E" gnus-summary-limit-include-expunged
1835   "c" gnus-summary-limit-exclude-childless-dormant
1836   "C" gnus-summary-limit-mark-excluded-as-read
1837   "o" gnus-summary-insert-old-articles
1838   "N" gnus-summary-insert-new-articles
1839   "r" gnus-summary-limit-to-replied)
1840
1841 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1842   "n" gnus-summary-next-unread-article
1843   "p" gnus-summary-prev-unread-article
1844   "N" gnus-summary-next-article
1845   "P" gnus-summary-prev-article
1846   "\C-n" gnus-summary-next-same-subject
1847   "\C-p" gnus-summary-prev-same-subject
1848   "\M-n" gnus-summary-next-unread-subject
1849   "\M-p" gnus-summary-prev-unread-subject
1850   "f" gnus-summary-first-unread-article
1851   "b" gnus-summary-best-unread-article
1852   "j" gnus-summary-goto-article
1853   "g" gnus-summary-goto-subject
1854   "l" gnus-summary-goto-last-article
1855   "o" gnus-summary-pop-article)
1856
1857 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1858   "k" gnus-summary-kill-thread
1859   "l" gnus-summary-lower-thread
1860   "i" gnus-summary-raise-thread
1861   "T" gnus-summary-toggle-threads
1862   "t" gnus-summary-rethread-current
1863   "^" gnus-summary-reparent-thread
1864   "s" gnus-summary-show-thread
1865   "S" gnus-summary-show-all-threads
1866   "h" gnus-summary-hide-thread
1867   "H" gnus-summary-hide-all-threads
1868   "n" gnus-summary-next-thread
1869   "p" gnus-summary-prev-thread
1870   "u" gnus-summary-up-thread
1871   "o" gnus-summary-top-thread
1872   "d" gnus-summary-down-thread
1873   "#" gnus-uu-mark-thread
1874   "\M-#" gnus-uu-unmark-thread)
1875
1876 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1877   "g" gnus-summary-prepare
1878   "c" gnus-summary-insert-cached-articles
1879   "d" gnus-summary-insert-dormant-articles)
1880
1881 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1882   "c" gnus-summary-catchup-and-exit
1883   "C" gnus-summary-catchup-all-and-exit
1884   "E" gnus-summary-exit-no-update
1885   "J" gnus-summary-jump-to-other-group
1886   "Q" gnus-summary-exit
1887   "Z" gnus-summary-exit
1888   "n" gnus-summary-catchup-and-goto-next-group
1889   "R" gnus-summary-reselect-current-group
1890   "G" gnus-summary-rescan-group
1891   "N" gnus-summary-next-group
1892   "s" gnus-summary-save-newsrc
1893   "P" gnus-summary-prev-group)
1894
1895 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1896   " " gnus-summary-next-page
1897   "n" gnus-summary-next-page
1898   "\177" gnus-summary-prev-page
1899   [delete] gnus-summary-prev-page
1900   "p" gnus-summary-prev-page
1901   "\r" gnus-summary-scroll-up
1902   "\M-\r" gnus-summary-scroll-down
1903   "<" gnus-summary-beginning-of-article
1904   ">" gnus-summary-end-of-article
1905   "b" gnus-summary-beginning-of-article
1906   "e" gnus-summary-end-of-article
1907   "^" gnus-summary-refer-parent-article
1908   "r" gnus-summary-refer-parent-article
1909   "D" gnus-summary-enter-digest-group
1910   "R" gnus-summary-refer-references
1911   "T" gnus-summary-refer-thread
1912   "g" gnus-summary-show-article
1913   "s" gnus-summary-isearch-article
1914   "P" gnus-summary-print-article
1915   "M" gnus-mailing-list-insinuate
1916   "t" gnus-article-babel)
1917
1918 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1919   "b" gnus-article-add-buttons
1920   "B" gnus-article-add-buttons-to-head
1921   "o" gnus-article-treat-overstrike
1922   "e" gnus-article-emphasize
1923   "w" gnus-article-fill-cited-article
1924   "Q" gnus-article-fill-long-lines
1925   "C" gnus-article-capitalize-sentences
1926   "c" gnus-article-remove-cr
1927   "Z" gnus-article-decode-HZ
1928   "A" gnus-article-treat-ansi-sequences
1929   "h" gnus-article-wash-html
1930   "u" gnus-article-unsplit-urls
1931   "f" gnus-article-display-x-face
1932   "l" gnus-summary-stop-page-breaking
1933   "r" gnus-summary-caesar-message
1934   "m" gnus-summary-morse-message
1935   "t" gnus-summary-toggle-header
1936   "g" gnus-treat-smiley
1937   "v" gnus-summary-verbose-headers
1938   "m" gnus-summary-toggle-mime
1939   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1940   "p" gnus-article-verify-x-pgp-sig
1941   "d" gnus-article-treat-dumbquotes)
1942
1943 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1944   ;; mnemonic: deuglif*Y*
1945   "u" gnus-article-outlook-unwrap-lines
1946   "a" gnus-article-outlook-repair-attribution
1947   "c" gnus-article-outlook-rearrange-citation
1948   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1949
1950 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1951   "a" gnus-article-hide
1952   "h" gnus-article-hide-headers
1953   "b" gnus-article-hide-boring-headers
1954   "s" gnus-article-hide-signature
1955   "c" gnus-article-hide-citation
1956   "C" gnus-article-hide-citation-in-followups
1957   "l" gnus-article-hide-list-identifiers
1958   "B" gnus-article-strip-banner
1959   "P" gnus-article-hide-pem
1960   "\C-c" gnus-article-hide-citation-maybe)
1961
1962 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1963   "a" gnus-article-highlight
1964   "h" gnus-article-highlight-headers
1965   "c" gnus-article-highlight-citation
1966   "s" gnus-article-highlight-signature)
1967
1968 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1969   "f" gnus-article-treat-fold-headers
1970   "u" gnus-article-treat-unfold-headers
1971   "n" gnus-article-treat-fold-newsgroups)
1972
1973 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1974   "x" gnus-article-display-x-face
1975   "d" gnus-article-display-face
1976   "s" gnus-treat-smiley
1977   "D" gnus-article-remove-images
1978   "f" gnus-treat-from-picon
1979   "m" gnus-treat-mail-picon
1980   "n" gnus-treat-newsgroups-picon)
1981
1982 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1983   "z" gnus-article-date-ut
1984   "u" gnus-article-date-ut
1985   "l" gnus-article-date-local
1986   "p" gnus-article-date-english
1987   "e" gnus-article-date-lapsed
1988   "o" gnus-article-date-original
1989   "i" gnus-article-date-iso8601
1990   "s" gnus-article-date-user)
1991
1992 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1993   "t" gnus-article-remove-trailing-blank-lines
1994   "l" gnus-article-strip-leading-blank-lines
1995   "m" gnus-article-strip-multiple-blank-lines
1996   "a" gnus-article-strip-blank-lines
1997   "A" gnus-article-strip-all-blank-lines
1998   "s" gnus-article-strip-leading-space
1999   "e" gnus-article-strip-trailing-space
2000   "w" gnus-article-remove-leading-whitespace)
2001
2002 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2003   "v" gnus-version
2004   "f" gnus-summary-fetch-faq
2005   "d" gnus-summary-describe-group
2006   "h" gnus-summary-describe-briefly
2007   "i" gnus-info-find-node
2008   "c" gnus-group-fetch-charter
2009   "C" gnus-group-fetch-control)
2010
2011 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2012   "e" gnus-summary-expire-articles
2013   "\M-\C-e" gnus-summary-expire-articles-now
2014   "\177" gnus-summary-delete-article
2015   [delete] gnus-summary-delete-article
2016   [backspace] gnus-summary-delete-article
2017   "m" gnus-summary-move-article
2018   "r" gnus-summary-respool-article
2019   "w" gnus-summary-edit-article
2020   "c" gnus-summary-copy-article
2021   "B" gnus-summary-crosspost-article
2022   "q" gnus-summary-respool-query
2023   "t" gnus-summary-respool-trace
2024   "i" gnus-summary-import-article
2025   "I" gnus-summary-create-article
2026   "p" gnus-summary-article-posted-p)
2027
2028 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2029   "o" gnus-summary-save-article
2030   "m" gnus-summary-save-article-mail
2031   "F" gnus-summary-write-article-file
2032   "r" gnus-summary-save-article-rmail
2033   "f" gnus-summary-save-article-file
2034   "b" gnus-summary-save-article-body-file
2035   "h" gnus-summary-save-article-folder
2036   "v" gnus-summary-save-article-vm
2037   "p" gnus-summary-pipe-output
2038   "P" gnus-summary-muttprint
2039   "s" gnus-soup-add-article)
2040
2041 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2042   "b" gnus-summary-display-buttonized
2043   "m" gnus-summary-repair-multipart
2044   "v" gnus-article-view-part
2045   "o" gnus-article-save-part
2046   "c" gnus-article-copy-part
2047   "C" gnus-article-view-part-as-charset
2048   "e" gnus-article-view-part-externally
2049   "E" gnus-article-encrypt-body
2050   "i" gnus-article-inline-part
2051   "|" gnus-article-pipe-part)
2052
2053 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2054   "p" gnus-summary-mark-as-processable
2055   "u" gnus-summary-unmark-as-processable
2056   "U" gnus-summary-unmark-all-processable
2057   "v" gnus-uu-mark-over
2058   "s" gnus-uu-mark-series
2059   "r" gnus-uu-mark-region
2060   "g" gnus-uu-unmark-region
2061   "R" gnus-uu-mark-by-regexp
2062   "G" gnus-uu-unmark-by-regexp
2063   "t" gnus-uu-mark-thread
2064   "T" gnus-uu-unmark-thread
2065   "a" gnus-uu-mark-all
2066   "b" gnus-uu-mark-buffer
2067   "S" gnus-uu-mark-sparse
2068   "k" gnus-summary-kill-process-mark
2069   "y" gnus-summary-yank-process-mark
2070   "w" gnus-summary-save-process-mark
2071   "i" gnus-uu-invert-processable)
2072
2073 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2074   ;;"x" gnus-uu-extract-any
2075   "m" gnus-summary-save-parts
2076   "u" gnus-uu-decode-uu
2077   "U" gnus-uu-decode-uu-and-save
2078   "s" gnus-uu-decode-unshar
2079   "S" gnus-uu-decode-unshar-and-save
2080   "o" gnus-uu-decode-save
2081   "O" gnus-uu-decode-save
2082   "b" gnus-uu-decode-binhex
2083   "B" gnus-uu-decode-binhex
2084   "p" gnus-uu-decode-postscript
2085   "P" gnus-uu-decode-postscript-and-save)
2086
2087 (gnus-define-keys
2088     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2089   "u" gnus-uu-decode-uu-view
2090   "U" gnus-uu-decode-uu-and-save-view
2091   "s" gnus-uu-decode-unshar-view
2092   "S" gnus-uu-decode-unshar-and-save-view
2093   "o" gnus-uu-decode-save-view
2094   "O" gnus-uu-decode-save-view
2095   "b" gnus-uu-decode-binhex-view
2096   "B" gnus-uu-decode-binhex-view
2097   "p" gnus-uu-decode-postscript-view
2098   "P" gnus-uu-decode-postscript-and-save-view)
2099
2100 (defvar gnus-article-post-menu nil)
2101
2102 (defconst gnus-summary-menu-maxlen 20)
2103
2104 (defun gnus-summary-menu-split (menu)
2105   ;; If we have lots of elements, divide them into groups of 20
2106   ;; and make a pane (or submenu) for each one.
2107   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2108       (let ((menu menu) sublists next
2109             (i 1))
2110         (while menu
2111           ;; Pull off the next gnus-summary-menu-maxlen elements
2112           ;; and make them the next element of sublist.
2113           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2114           (if next
2115               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2116                       nil))
2117           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2118                                              (aref (car (last menu)) 0)) menu)
2119                                sublists))
2120           (setq i (1+ i))
2121           (setq menu next))
2122         (nreverse sublists))
2123     ;; Few elements--put them all in one pane.
2124     menu))
2125
2126 (defun gnus-summary-make-menu-bar ()
2127   (gnus-turn-off-edit-menu 'summary)
2128
2129   (unless (boundp 'gnus-summary-misc-menu)
2130
2131     (easy-menu-define
2132      gnus-summary-kill-menu gnus-summary-mode-map ""
2133      (cons
2134       "Score"
2135       (nconc
2136        (list
2137         ["Customize" gnus-score-customize t])
2138        (gnus-make-score-map 'increase)
2139        (gnus-make-score-map 'lower)
2140        '(("Mark"
2141           ["Kill below" gnus-summary-kill-below t]
2142           ["Mark above" gnus-summary-mark-above t]
2143           ["Tick above" gnus-summary-tick-above t]
2144           ["Clear above" gnus-summary-clear-above t])
2145          ["Current score" gnus-summary-current-score t]
2146          ["Set score" gnus-summary-set-score t]
2147          ["Switch current score file..." gnus-score-change-score-file t]
2148          ["Set mark below..." gnus-score-set-mark-below t]
2149          ["Set expunge below..." gnus-score-set-expunge-below t]
2150          ["Edit current score file" gnus-score-edit-current-scores t]
2151          ["Edit score file" gnus-score-edit-file t]
2152          ["Trace score" gnus-score-find-trace t]
2153          ["Find words" gnus-score-find-favourite-words t]
2154          ["Rescore buffer" gnus-summary-rescore t]
2155          ["Increase score..." gnus-summary-increase-score t]
2156          ["Lower score..." gnus-summary-lower-score t]))))
2157
2158     ;; Define both the Article menu in the summary buffer and the
2159     ;; equivalent Commands menu in the article buffer here for
2160     ;; consistency.
2161     (let ((innards
2162            `(("Hide"
2163               ["All" gnus-article-hide t]
2164               ["Headers" gnus-article-hide-headers t]
2165               ["Signature" gnus-article-hide-signature t]
2166               ["Citation" gnus-article-hide-citation t]
2167               ["List identifiers" gnus-article-hide-list-identifiers t]
2168               ["Banner" gnus-article-strip-banner t]
2169               ["Boring headers" gnus-article-hide-boring-headers t])
2170              ("Highlight"
2171               ["All" gnus-article-highlight t]
2172               ["Headers" gnus-article-highlight-headers t]
2173               ["Signature" gnus-article-highlight-signature t]
2174               ["Citation" gnus-article-highlight-citation t])
2175              ("Date"
2176               ["Local" gnus-article-date-local t]
2177               ["ISO8601" gnus-article-date-iso8601 t]
2178               ["UT" gnus-article-date-ut t]
2179               ["Original" gnus-article-date-original t]
2180               ["Lapsed" gnus-article-date-lapsed t]
2181               ["User-defined" gnus-article-date-user t])
2182              ("Display"
2183               ["Remove images" gnus-article-remove-images t]
2184               ["Toggle smiley" gnus-treat-smiley t]
2185               ["Show X-Face" gnus-article-display-x-face t]
2186               ["Show picons in From" gnus-treat-from-picon t]
2187               ["Show picons in mail headers" gnus-treat-mail-picon t]
2188               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2189               ("View as different encoding"
2190                ,@(gnus-summary-menu-split
2191                   (mapcar
2192                    (lambda (cs)
2193                      ;; Since easymenu under Emacs doesn't allow
2194                      ;; lambda forms for menu commands, we should
2195                      ;; provide intern'ed function symbols.
2196                      (let ((command (intern (format "\
2197 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2198                        (fset command
2199                              `(lambda ()
2200                                 (interactive)
2201                                 (let ((gnus-summary-show-article-charset-alist
2202                                        '((1 . ,cs))))
2203                                   (gnus-summary-show-article 1))))
2204                        `[,(symbol-name cs) ,command t]))
2205                    (sort (if (fboundp 'coding-system-list)
2206                              (coding-system-list)
2207                            ;;(mapcar 'car mm-mime-mule-charset-alist)
2208                            )
2209                          'string<)))))
2210              ("Washing"
2211               ("Remove Blanks"
2212                ["Leading" gnus-article-strip-leading-blank-lines t]
2213                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2214                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2215                ["All of the above" gnus-article-strip-blank-lines t]
2216                ["All" gnus-article-strip-all-blank-lines t]
2217                ["Leading space" gnus-article-strip-leading-space t]
2218                ["Trailing space" gnus-article-strip-trailing-space t]
2219                ["Leading space in headers"
2220                 gnus-article-remove-leading-whitespace t])
2221               ["Overstrike" gnus-article-treat-overstrike t]
2222               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2223               ["Emphasis" gnus-article-emphasize t]
2224               ["Word wrap" gnus-article-fill-cited-article t]
2225               ["Fill long lines" gnus-article-fill-long-lines t]
2226               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2227               ["Remove CR" gnus-article-remove-cr t]
2228               ["Rot 13" gnus-summary-caesar-message
2229                ,@(if (featurep 'xemacs) '(t)
2230                    '(:help "\"Caesar rotate\" article by 13"))]
2231               ["Morse decode" gnus-summary-morse-message t]
2232               ["Unix pipe..." gnus-summary-pipe-message t]
2233               ["Add buttons" gnus-article-add-buttons t]
2234               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2235               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2236               ["Toggle MIME" gnus-summary-toggle-mime t]
2237               ["Verbose header" gnus-summary-verbose-headers t]
2238               ["Toggle header" gnus-summary-toggle-header t]
2239               ["Unfold headers" gnus-article-treat-unfold-headers t]
2240               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2241               ["Html" gnus-article-wash-html t]
2242               ["Unsplit URLs" gnus-article-unsplit-urls t]
2243               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2244               ["Decode HZ" gnus-article-decode-HZ t]
2245               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2246               ("(Outlook) Deuglify"
2247                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2248                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2249                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2250                ["Full (Outlook) deuglify"
2251                 gnus-article-outlook-deuglify-article t])
2252               )
2253              ("Output"
2254               ["Save in default format..." gnus-summary-save-article
2255                ,@(if (featurep 'xemacs) '(t)
2256                    '(:help "Save article using default method"))]
2257               ["Save in file..." gnus-summary-save-article-file
2258                ,@(if (featurep 'xemacs) '(t)
2259                    '(:help "Save article in file"))]
2260               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2261               ["Save in MH folder..." gnus-summary-save-article-folder t]
2262               ["Save in VM folder..." gnus-summary-save-article-vm t]
2263               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2264               ["Save body in file..." gnus-summary-save-article-body-file t]
2265               ["Pipe through a filter..." gnus-summary-pipe-output t]
2266               ["Add to SOUP packet" gnus-soup-add-article t]
2267               ["Print with Muttprint..." gnus-summary-muttprint t]
2268               ["Print" gnus-summary-print-article t])
2269              ("Backend"
2270               ["Respool article..." gnus-summary-respool-article t]
2271               ["Move article..." gnus-summary-move-article
2272                (gnus-check-backend-function
2273                 'request-move-article gnus-newsgroup-name)]
2274               ["Copy article..." gnus-summary-copy-article t]
2275               ["Crosspost article..." gnus-summary-crosspost-article
2276                (gnus-check-backend-function
2277                 'request-replace-article gnus-newsgroup-name)]
2278               ["Import file..." gnus-summary-import-article
2279                (gnus-check-backend-function
2280                 'request-accept-article gnus-newsgroup-name)]
2281               ["Create article..." gnus-summary-create-article
2282                (gnus-check-backend-function
2283                 'request-accept-article gnus-newsgroup-name)]
2284               ["Check if posted" gnus-summary-article-posted-p t]
2285               ["Edit article" gnus-summary-edit-article
2286                (not (gnus-group-read-only-p))]
2287               ["Delete article" gnus-summary-delete-article
2288                (gnus-check-backend-function
2289                 'request-expire-articles gnus-newsgroup-name)]
2290               ["Query respool" gnus-summary-respool-query t]
2291               ["Trace respool" gnus-summary-respool-trace t]
2292               ["Delete expirable articles" gnus-summary-expire-articles-now
2293                (gnus-check-backend-function
2294                 'request-expire-articles gnus-newsgroup-name)])
2295              ("Extract"
2296               ["Uudecode" gnus-uu-decode-uu
2297                ,@(if (featurep 'xemacs) '(t)
2298                    '(:help "Decode uuencoded article(s)"))]
2299               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2300               ["Unshar" gnus-uu-decode-unshar t]
2301               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2302               ["Save" gnus-uu-decode-save t]
2303               ["Binhex" gnus-uu-decode-binhex t]
2304               ["Postscript" gnus-uu-decode-postscript t])
2305              ("Cache"
2306               ["Enter article" gnus-cache-enter-article t]
2307               ["Remove article" gnus-cache-remove-article t])
2308              ["Translate" gnus-article-babel t]
2309              ["Select article buffer" gnus-summary-select-article-buffer t]
2310              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2311              ["Isearch article..." gnus-summary-isearch-article t]
2312              ["Beginning of the article" gnus-summary-beginning-of-article t]
2313              ["End of the article" gnus-summary-end-of-article t]
2314              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2315              ["Fetch referenced articles" gnus-summary-refer-references t]
2316              ["Fetch current thread" gnus-summary-refer-thread t]
2317              ["Fetch article with id..." gnus-summary-refer-article t]
2318              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2319              ["Redisplay" gnus-summary-show-article t]
2320              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2321       (easy-menu-define
2322        gnus-summary-article-menu gnus-summary-mode-map ""
2323        (cons "Article" innards))
2324
2325       (if (not (keymapp gnus-summary-article-menu))
2326           (easy-menu-define
2327            gnus-article-commands-menu gnus-article-mode-map ""
2328            (cons "Commands" innards))
2329         ;; in Emacs, don't share menu.
2330         (setq gnus-article-commands-menu
2331               (copy-keymap gnus-summary-article-menu))
2332         (define-key gnus-article-mode-map [menu-bar commands]
2333           (cons "Commands" gnus-article-commands-menu))))
2334
2335     (easy-menu-define
2336      gnus-summary-thread-menu gnus-summary-mode-map ""
2337      '("Threads"
2338        ["Find all messages in thread" gnus-summary-refer-thread t]
2339        ["Toggle threading" gnus-summary-toggle-threads t]
2340        ["Hide threads" gnus-summary-hide-all-threads t]
2341        ["Show threads" gnus-summary-show-all-threads t]
2342        ["Hide thread" gnus-summary-hide-thread t]
2343        ["Show thread" gnus-summary-show-thread t]
2344        ["Go to next thread" gnus-summary-next-thread t]
2345        ["Go to previous thread" gnus-summary-prev-thread t]
2346        ["Go down thread" gnus-summary-down-thread t]
2347        ["Go up thread" gnus-summary-up-thread t]
2348        ["Top of thread" gnus-summary-top-thread t]
2349        ["Mark thread as read" gnus-summary-kill-thread t]
2350        ["Lower thread score" gnus-summary-lower-thread t]
2351        ["Raise thread score" gnus-summary-raise-thread t]
2352        ["Rethread current" gnus-summary-rethread-current t]))
2353
2354     (easy-menu-define
2355      gnus-summary-post-menu gnus-summary-mode-map ""
2356      `("Post"
2357        ["Send a message (mail or news)" gnus-summary-post-news
2358         ,@(if (featurep 'xemacs) '(t)
2359             '(:help "Post an article"))]
2360        ["Followup" gnus-summary-followup
2361         ,@(if (featurep 'xemacs) '(t)
2362             '(:help "Post followup to this article"))]
2363        ["Followup and yank" gnus-summary-followup-with-original
2364         ,@(if (featurep 'xemacs) '(t)
2365             '(:help "Post followup to this article, quoting its contents"))]
2366        ["Supersede article" gnus-summary-supersede-article t]
2367        ["Cancel article" gnus-summary-cancel-article
2368         ,@(if (featurep 'xemacs) '(t)
2369             '(:help "Cancel an article you posted"))]
2370        ["Reply" gnus-summary-reply t]
2371        ["Reply and yank" gnus-summary-reply-with-original t]
2372        ["Wide reply" gnus-summary-wide-reply t]
2373        ["Wide reply and yank" gnus-summary-wide-reply-with-original
2374         ,@(if (featurep 'xemacs) '(t)
2375             '(:help "Mail a reply, quoting this article"))]
2376        ["Very wide reply" gnus-summary-very-wide-reply t]
2377        ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2378         ,@(if (featurep 'xemacs) '(t)
2379             '(:help "Mail a very wide reply, quoting this article"))]
2380        ["Mail forward" gnus-summary-mail-forward t]
2381        ["Post forward" gnus-summary-post-forward t]
2382        ["Digest and mail" gnus-summary-digest-mail-forward t]
2383        ["Digest and post" gnus-summary-digest-post-forward t]
2384        ["Resend message" gnus-summary-resend-message t]
2385        ["Resend message edit" gnus-summary-resend-message-edit t]
2386        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2387        ["Send a mail" gnus-summary-mail-other-window t]
2388        ["Create a local message" gnus-summary-news-other-window t]
2389        ["Uuencode and post" gnus-uu-post-news
2390         ,@(if (featurep 'xemacs) '(t)
2391             '(:help "Post a uuencoded article"))]
2392        ["Followup via news" gnus-summary-followup-to-mail t]
2393        ["Followup via news and yank"
2394         gnus-summary-followup-to-mail-with-original t]
2395        ;;("Draft"
2396        ;;["Send" gnus-summary-send-draft t]
2397        ;;["Send bounced" gnus-resend-bounced-mail t])
2398        ))
2399
2400     (cond
2401      ((not (keymapp gnus-summary-post-menu))
2402       (setq gnus-article-post-menu gnus-summary-post-menu))
2403      ((not gnus-article-post-menu)
2404       ;; Don't share post menu.
2405       (setq gnus-article-post-menu
2406             (copy-keymap gnus-summary-post-menu))))
2407     (define-key gnus-article-mode-map [menu-bar post]
2408       (cons "Post" gnus-article-post-menu))
2409
2410     (easy-menu-define
2411      gnus-summary-misc-menu gnus-summary-mode-map ""
2412      `("Gnus"
2413        ("Mark Read"
2414         ["Mark as read" gnus-summary-mark-as-read-forward t]
2415         ["Mark same subject and select"
2416          gnus-summary-kill-same-subject-and-select t]
2417         ["Mark same subject" gnus-summary-kill-same-subject t]
2418         ["Catchup" gnus-summary-catchup
2419          ,@(if (featurep 'xemacs) '(t)
2420              '(:help "Mark unread articles in this group as read"))]
2421         ["Catchup all" gnus-summary-catchup-all t]
2422         ["Catchup to here" gnus-summary-catchup-to-here t]
2423         ["Catchup from here" gnus-summary-catchup-from-here t]
2424         ["Catchup region" gnus-summary-mark-region-as-read
2425          (gnus-mark-active-p)]
2426         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2427        ("Mark Various"
2428         ["Tick" gnus-summary-tick-article-forward t]
2429         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2430         ["Remove marks" gnus-summary-clear-mark-forward t]
2431         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2432         ["Set bookmark" gnus-summary-set-bookmark t]
2433         ["Remove bookmark" gnus-summary-remove-bookmark t])
2434        ("Limit to"
2435         ["Marks..." gnus-summary-limit-to-marks t]
2436         ["Subject..." gnus-summary-limit-to-subject t]
2437         ["Author..." gnus-summary-limit-to-author t]
2438         ["Age..." gnus-summary-limit-to-age t]
2439         ["Extra..." gnus-summary-limit-to-extra t]
2440         ["Score..." gnus-summary-limit-to-score t]
2441         ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2442         ["Unread" gnus-summary-limit-to-unread t]
2443         ["Unseen" gnus-summary-limit-to-unseen t]
2444         ["Replied" gnus-summary-limit-to-replied t]
2445         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2446         ["Next articles" gnus-summary-limit-to-articles t]
2447         ["Pop limit" gnus-summary-pop-limit t]
2448         ["Show dormant" gnus-summary-limit-include-dormant t]
2449         ["Hide childless dormant"
2450          gnus-summary-limit-exclude-childless-dormant t]
2451         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2452         ["Hide marked" gnus-summary-limit-exclude-marks t]
2453         ["Show expunged" gnus-summary-limit-include-expunged t])
2454        ("Process Mark"
2455         ["Set mark" gnus-summary-mark-as-processable t]
2456         ["Remove mark" gnus-summary-unmark-as-processable t]
2457         ["Remove all marks" gnus-summary-unmark-all-processable t]
2458         ["Mark above" gnus-uu-mark-over t]
2459         ["Mark series" gnus-uu-mark-series t]
2460         ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2461         ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2462         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2463         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2464         ["Mark all" gnus-uu-mark-all t]
2465         ["Mark buffer" gnus-uu-mark-buffer t]
2466         ["Mark sparse" gnus-uu-mark-sparse t]
2467         ["Mark thread" gnus-uu-mark-thread t]
2468         ["Unmark thread" gnus-uu-unmark-thread t]
2469         ("Process Mark Sets"
2470          ["Kill" gnus-summary-kill-process-mark t]
2471          ["Yank" gnus-summary-yank-process-mark
2472           gnus-newsgroup-process-stack]
2473          ["Save" gnus-summary-save-process-mark t]
2474          ["Run command on marked..." gnus-summary-universal-argument t]))
2475        ("Scroll article"
2476         ["Page forward" gnus-summary-next-page
2477          ,@(if (featurep 'xemacs) '(t)
2478              '(:help "Show next page of article"))]
2479         ["Page backward" gnus-summary-prev-page
2480          ,@(if (featurep 'xemacs) '(t)
2481              '(:help "Show previous page of article"))]
2482         ["Line forward" gnus-summary-scroll-up t])
2483        ("Move"
2484         ["Next unread article" gnus-summary-next-unread-article t]
2485         ["Previous unread article" gnus-summary-prev-unread-article t]
2486         ["Next article" gnus-summary-next-article t]
2487         ["Previous article" gnus-summary-prev-article t]
2488         ["Next unread subject" gnus-summary-next-unread-subject t]
2489         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2490         ["Next article same subject" gnus-summary-next-same-subject t]
2491         ["Previous article same subject" gnus-summary-prev-same-subject t]
2492         ["First unread article" gnus-summary-first-unread-article t]
2493         ["Best unread article" gnus-summary-best-unread-article t]
2494         ["Go to subject number..." gnus-summary-goto-subject t]
2495         ["Go to article number..." gnus-summary-goto-article t]
2496         ["Go to the last article" gnus-summary-goto-last-article t]
2497         ["Pop article off history" gnus-summary-pop-article t])
2498        ("Sort"
2499         ["Sort by number" gnus-summary-sort-by-number t]
2500         ["Sort by author" gnus-summary-sort-by-author t]
2501         ["Sort by subject" gnus-summary-sort-by-subject t]
2502         ["Sort by date" gnus-summary-sort-by-date t]
2503         ["Sort by score" gnus-summary-sort-by-score t]
2504         ["Sort by lines" gnus-summary-sort-by-lines t]
2505         ["Sort by characters" gnus-summary-sort-by-chars t]
2506         ["Randomize" gnus-summary-sort-by-random t]
2507         ["Original sort" gnus-summary-sort-by-original t])
2508        ("Help"
2509         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2510         ["Describe group" gnus-summary-describe-group t]
2511         ["Fetch charter" gnus-group-fetch-charter
2512          ,@(if (featurep 'xemacs) nil
2513              '(:help "Display the charter of the current group"))]
2514         ["Fetch control message" gnus-group-fetch-control
2515          ,@(if (featurep 'xemacs) nil
2516              '(:help "Display the archived control message for the current group"))]
2517         ["Read manual" gnus-info-find-node t])
2518        ("Modes"
2519         ["Pick and read" gnus-pick-mode t]
2520         ["Binary" gnus-binary-mode t])
2521        ("Regeneration"
2522         ["Regenerate" gnus-summary-prepare t]
2523         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2524         ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2525         ["Toggle threading" gnus-summary-toggle-threads t])
2526        ["See old articles" gnus-summary-insert-old-articles t]
2527        ["See new articles" gnus-summary-insert-new-articles t]
2528        ["Filter articles..." gnus-summary-execute-command t]
2529        ["Run command on articles..." gnus-summary-universal-argument t]
2530        ["Search articles forward..." gnus-summary-search-article-forward t]
2531        ["Search articles backward..." gnus-summary-search-article-backward t]
2532        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2533        ["Expand window" gnus-summary-expand-window t]
2534        ["Expire expirable articles" gnus-summary-expire-articles
2535         (gnus-check-backend-function
2536          'request-expire-articles gnus-newsgroup-name)]
2537        ["Edit local kill file" gnus-summary-edit-local-kill t]
2538        ["Edit main kill file" gnus-summary-edit-global-kill t]
2539        ["Edit group parameters" gnus-summary-edit-parameters t]
2540        ["Customize group parameters" gnus-summary-customize-parameters t]
2541        ["Send a bug report" gnus-bug t]
2542        ("Exit"
2543         ["Catchup and exit" gnus-summary-catchup-and-exit
2544          ,@(if (featurep 'xemacs) '(t)
2545              '(:help "Mark unread articles in this group as read, then exit"))]
2546         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2547         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2548         ["Exit group" gnus-summary-exit
2549          ,@(if (featurep 'xemacs) '(t)
2550              '(:help "Exit current group, return to group selection mode"))]
2551         ["Exit group without updating" gnus-summary-exit-no-update t]
2552         ["Exit and goto next group" gnus-summary-next-group t]
2553         ["Exit and goto prev group" gnus-summary-prev-group t]
2554         ["Reselect group" gnus-summary-reselect-current-group t]
2555         ["Rescan group" gnus-summary-rescan-group t]
2556         ["Update dribble" gnus-summary-save-newsrc t])))
2557
2558     (gnus-run-hooks 'gnus-summary-menu-hook)))
2559
2560 (defvar gnus-summary-tool-bar-map nil)
2561
2562 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2563 (defun gnus-summary-make-tool-bar ()
2564   (if (and (fboundp 'tool-bar-add-item-from-menu)
2565            (default-value 'tool-bar-mode)
2566            (not gnus-summary-tool-bar-map))
2567       (setq gnus-summary-tool-bar-map
2568             (let ((tool-bar-map (make-sparse-keymap))
2569                   (load-path (mm-image-load-path)))
2570               (tool-bar-add-item-from-menu
2571                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2572               (tool-bar-add-item-from-menu
2573                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2574               (tool-bar-add-item-from-menu
2575                'gnus-summary-post-news "post" gnus-summary-mode-map)
2576               (tool-bar-add-item-from-menu
2577                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2578               (tool-bar-add-item-from-menu
2579                'gnus-summary-followup "followup" gnus-summary-mode-map)
2580               (tool-bar-add-item-from-menu
2581                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2582               (tool-bar-add-item-from-menu
2583                'gnus-summary-reply "reply" gnus-summary-mode-map)
2584               (tool-bar-add-item-from-menu
2585                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2586               (tool-bar-add-item-from-menu
2587                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2588               (tool-bar-add-item-from-menu
2589                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2590               (tool-bar-add-item-from-menu
2591                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2592               (tool-bar-add-item-from-menu
2593                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2594               (tool-bar-add-item-from-menu
2595                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2596               (tool-bar-add-item-from-menu
2597                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2598               (tool-bar-add-item-from-menu
2599                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2600               tool-bar-map)))
2601   (if gnus-summary-tool-bar-map
2602       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2603
2604 (defun gnus-score-set-default (var value)
2605   "A version of set that updates the GNU Emacs menu-bar."
2606   (set var value)
2607   ;; It is the message that forces the active status to be updated.
2608   (message ""))
2609
2610 (defun gnus-make-score-map (type)
2611   "Make a summary score map of type TYPE."
2612   (if t
2613       nil
2614     (let ((headers '(("author" "from" string)
2615                      ("subject" "subject" string)
2616                      ("article body" "body" string)
2617                      ("article head" "head" string)
2618                      ("xref" "xref" string)
2619                      ("extra header" "extra" string)
2620                      ("lines" "lines" number)
2621                      ("followups to author" "followup" string)))
2622           (types '((number ("less than" <)
2623                            ("greater than" >)
2624                            ("equal" =))
2625                    (string ("substring" s)
2626                            ("exact string" e)
2627                            ("fuzzy string" f)
2628                            ("regexp" r))))
2629           (perms '(("temporary" (current-time-string))
2630                    ("permanent" nil)
2631                    ("immediate" now)))
2632           header)
2633       (list
2634        (apply
2635         'nconc
2636         (list
2637          (if (eq type 'lower)
2638              "Lower score"
2639            "Increase score"))
2640         (let (outh)
2641           (while headers
2642             (setq header (car headers))
2643             (setq outh
2644                   (cons
2645                    (apply
2646                     'nconc
2647                     (list (car header))
2648                     (let ((ts (cdr (assoc (nth 2 header) types)))
2649                           outt)
2650                       (while ts
2651                         (setq outt
2652                               (cons
2653                                (apply
2654                                 'nconc
2655                                 (list (caar ts))
2656                                 (let ((ps perms)
2657                                       outp)
2658                                   (while ps
2659                                     (setq outp
2660                                           (cons
2661                                            (vector
2662                                             (caar ps)
2663                                             (list
2664                                              'gnus-summary-score-entry
2665                                              (nth 1 header)
2666                                              (if (or (string= (nth 1 header)
2667                                                               "head")
2668                                                      (string= (nth 1 header)
2669                                                               "body"))
2670                                                  ""
2671                                                (list 'gnus-summary-header
2672                                                      (nth 1 header)))
2673                                              (list 'quote (nth 1 (car ts)))
2674                                              (list 'gnus-score-delta-default
2675                                                    nil)
2676                                              (nth 1 (car ps))
2677                                              t)
2678                                             t)
2679                                            outp))
2680                                     (setq ps (cdr ps)))
2681                                   (list (nreverse outp))))
2682                                outt))
2683                         (setq ts (cdr ts)))
2684                       (list (nreverse outt))))
2685                    outh))
2686             (setq headers (cdr headers)))
2687           (list (nreverse outh))))))))
2688
2689 \f
2690
2691 (defun gnus-summary-mode (&optional group)
2692   "Major mode for reading articles.
2693
2694 All normal editing commands are switched off.
2695 \\<gnus-summary-mode-map>
2696 Each line in this buffer represents one article.  To read an
2697 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2698 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2699 respectively.
2700
2701 You can also post articles and send mail from this buffer.  To
2702 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2703 of an article, type `\\[gnus-summary-reply]'.
2704
2705 There are approx. one gazillion commands you can execute in this
2706 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2707
2708 The following commands are available:
2709
2710 \\{gnus-summary-mode-map}"
2711   (interactive)
2712   (kill-all-local-variables)
2713   (when (gnus-visual-p 'summary-menu 'menu)
2714     (gnus-summary-make-menu-bar)
2715     (gnus-summary-make-tool-bar))
2716   (gnus-summary-make-local-variables)
2717   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2718     (gnus-summary-make-local-variables))
2719   (gnus-make-thread-indent-array)
2720   (gnus-simplify-mode-line)
2721   (setq major-mode 'gnus-summary-mode)
2722   (setq mode-name "Summary")
2723   (make-local-variable 'minor-mode-alist)
2724   (use-local-map gnus-summary-mode-map)
2725   (buffer-disable-undo)
2726   (setq buffer-read-only t)             ;Disable modification
2727   (setq truncate-lines t)
2728   (setq selective-display t)
2729   (setq selective-display-ellipses t)   ;Display `...'
2730   (gnus-summary-set-display-table)
2731   (gnus-set-default-directory)
2732   (setq gnus-newsgroup-name group)
2733   (unless (gnus-news-group-p group)
2734     (setq gnus-newsgroup-incorporated
2735           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2736   (make-local-variable 'gnus-summary-line-format)
2737   (make-local-variable 'gnus-summary-line-format-spec)
2738   (make-local-variable 'gnus-summary-dummy-line-format)
2739   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2740   (make-local-variable 'gnus-summary-mark-positions)
2741   (gnus-make-local-hook 'pre-command-hook)
2742   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2743   (gnus-run-hooks 'gnus-summary-mode-hook)
2744   (turn-on-gnus-mailing-list-mode)
2745   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2746   (gnus-update-summary-mark-positions))
2747
2748 (defun gnus-summary-make-local-variables ()
2749   "Make all the local summary buffer variables."
2750   (let (global)
2751     (dolist (local gnus-summary-local-variables)
2752       (if (consp local)
2753           (progn
2754             (if (eq (cdr local) 'global)
2755                 ;; Copy the global value of the variable.
2756                 (setq global (symbol-value (car local)))
2757               ;; Use the value from the list.
2758               (setq global (eval (cdr local))))
2759             (set (make-local-variable (car local)) global))
2760         ;; Simple nil-valued local variable.
2761         (set (make-local-variable local) nil)))))
2762
2763 (defun gnus-summary-clear-local-variables ()
2764   (let ((locals gnus-summary-local-variables))
2765     (while locals
2766       (if (consp (car locals))
2767           (and (vectorp (caar locals))
2768                (set (caar locals) nil))
2769         (and (vectorp (car locals))
2770              (set (car locals) nil)))
2771       (setq locals (cdr locals)))))
2772
2773 ;; Summary data functions.
2774
2775 (defmacro gnus-data-number (data)
2776   `(car ,data))
2777
2778 (defmacro gnus-data-set-number (data number)
2779   `(setcar ,data ,number))
2780
2781 (defmacro gnus-data-mark (data)
2782   `(nth 1 ,data))
2783
2784 (defmacro gnus-data-set-mark (data mark)
2785   `(setcar (nthcdr 1 ,data) ,mark))
2786
2787 (defmacro gnus-data-pos (data)
2788   `(nth 2 ,data))
2789
2790 (defmacro gnus-data-set-pos (data pos)
2791   `(setcar (nthcdr 2 ,data) ,pos))
2792
2793 (defmacro gnus-data-header (data)
2794   `(nth 3 ,data))
2795
2796 (defmacro gnus-data-set-header (data header)
2797   `(setcar (nthcdr 3 ,data) ,header))
2798
2799 (defmacro gnus-data-level (data)
2800   `(nth 4 ,data))
2801
2802 (defmacro gnus-data-unread-p (data)
2803   `(= (nth 1 ,data) gnus-unread-mark))
2804
2805 (defmacro gnus-data-read-p (data)
2806   `(/= (nth 1 ,data) gnus-unread-mark))
2807
2808 (defmacro gnus-data-pseudo-p (data)
2809   `(consp (nth 3 ,data)))
2810
2811 (defmacro gnus-data-find (number)
2812   `(assq ,number gnus-newsgroup-data))
2813
2814 (defmacro gnus-data-find-list (number &optional data)
2815   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2816      (memq (assq ,number bdata)
2817            bdata)))
2818
2819 (defmacro gnus-data-make (number mark pos header level)
2820   `(list ,number ,mark ,pos ,header ,level))
2821
2822 (defun gnus-data-enter (after-article number mark pos header level offset)
2823   (let ((data (gnus-data-find-list after-article)))
2824     (unless data
2825       (error "No such article: %d" after-article))
2826     (setcdr data (cons (gnus-data-make number mark pos header level)
2827                        (cdr data)))
2828     (setq gnus-newsgroup-data-reverse nil)
2829     (gnus-data-update-list (cddr data) offset)))
2830
2831 (defun gnus-data-enter-list (after-article list &optional offset)
2832   (when list
2833     (let ((data (and after-article (gnus-data-find-list after-article)))
2834           (ilist list))
2835       (if (not (or data
2836                    after-article))
2837           (let ((odata gnus-newsgroup-data))
2838             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2839             (when offset
2840               (gnus-data-update-list odata offset)))
2841         ;; Find the last element in the list to be spliced into the main
2842         ;; list.
2843         (while (cdr list)
2844           (setq list (cdr list)))
2845         (if (not data)
2846             (progn
2847               (setcdr list gnus-newsgroup-data)
2848               (setq gnus-newsgroup-data ilist)
2849               (when offset
2850                 (gnus-data-update-list (cdr list) offset)))
2851           (setcdr list (cdr data))
2852           (setcdr data ilist)
2853           (when offset
2854             (gnus-data-update-list (cdr list) offset))))
2855       (setq gnus-newsgroup-data-reverse nil))))
2856
2857 (defun gnus-data-remove (article &optional offset)
2858   (let ((data gnus-newsgroup-data))
2859     (if (= (gnus-data-number (car data)) article)
2860         (progn
2861           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2862                 gnus-newsgroup-data-reverse nil)
2863           (when offset
2864             (gnus-data-update-list gnus-newsgroup-data offset)))
2865       (while (cdr data)
2866         (when (= (gnus-data-number (cadr data)) article)
2867           (setcdr data (cddr data))
2868           (when offset
2869             (gnus-data-update-list (cdr data) offset))
2870           (setq data nil
2871                 gnus-newsgroup-data-reverse nil))
2872         (setq data (cdr data))))))
2873
2874 (defmacro gnus-data-list (backward)
2875   `(if ,backward
2876        (or gnus-newsgroup-data-reverse
2877            (setq gnus-newsgroup-data-reverse
2878                  (reverse gnus-newsgroup-data)))
2879      gnus-newsgroup-data))
2880
2881 (defun gnus-data-update-list (data offset)
2882   "Add OFFSET to the POS of all data entries in DATA."
2883   (setq gnus-newsgroup-data-reverse nil)
2884   (while data
2885     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2886     (setq data (cdr data))))
2887
2888 (defun gnus-summary-article-pseudo-p (article)
2889   "Say whether this article is a pseudo article or not."
2890   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2891
2892 (defmacro gnus-summary-article-sparse-p (article)
2893   "Say whether this article is a sparse article or not."
2894   `(memq ,article gnus-newsgroup-sparse))
2895
2896 (defmacro gnus-summary-article-ancient-p (article)
2897   "Say whether this article is a sparse article or not."
2898   `(memq ,article gnus-newsgroup-ancient))
2899
2900 (defun gnus-article-parent-p (number)
2901   "Say whether this article is a parent or not."
2902   (let ((data (gnus-data-find-list number)))
2903     (and (cdr data)                     ; There has to be an article after...
2904          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2905             (gnus-data-level (nth 1 data))))))
2906
2907 (defun gnus-article-children (number)
2908   "Return a list of all children to NUMBER."
2909   (let* ((data (gnus-data-find-list number))
2910          (level (gnus-data-level (car data)))
2911          children)
2912     (setq data (cdr data))
2913     (while (and data
2914                 (= (gnus-data-level (car data)) (1+ level)))
2915       (push (gnus-data-number (car data)) children)
2916       (setq data (cdr data)))
2917     children))
2918
2919 (defmacro gnus-summary-skip-intangible ()
2920   "If the current article is intangible, then jump to a different article."
2921   '(let ((to (get-text-property (point) 'gnus-intangible)))
2922      (and to (gnus-summary-goto-subject to))))
2923
2924 (defmacro gnus-summary-article-intangible-p ()
2925   "Say whether this article is intangible or not."
2926   '(get-text-property (point) 'gnus-intangible))
2927
2928 (defun gnus-article-read-p (article)
2929   "Say whether ARTICLE is read or not."
2930   (not (or (memq article gnus-newsgroup-marked)
2931            (memq article gnus-newsgroup-spam-marked)
2932            (memq article gnus-newsgroup-unreads)
2933            (memq article gnus-newsgroup-unselected)
2934            (memq article gnus-newsgroup-dormant))))
2935
2936 ;; Some summary mode macros.
2937
2938 (defmacro gnus-summary-article-number ()
2939   "The article number of the article on the current line.
2940 If there isn't an article number here, then we return the current
2941 article number."
2942   '(progn
2943      (gnus-summary-skip-intangible)
2944      (or (get-text-property (point) 'gnus-number)
2945          (gnus-summary-last-subject))))
2946
2947 (defmacro gnus-summary-article-header (&optional number)
2948   "Return the header of article NUMBER."
2949   `(gnus-data-header (gnus-data-find
2950                       ,(or number '(gnus-summary-article-number)))))
2951
2952 (defmacro gnus-summary-thread-level (&optional number)
2953   "Return the level of thread that starts with article NUMBER."
2954   `(if (and (eq gnus-summary-make-false-root 'dummy)
2955             (get-text-property (point) 'gnus-intangible))
2956        0
2957      (gnus-data-level (gnus-data-find
2958                        ,(or number '(gnus-summary-article-number))))))
2959
2960 (defmacro gnus-summary-article-mark (&optional number)
2961   "Return the mark of article NUMBER."
2962   `(gnus-data-mark (gnus-data-find
2963                     ,(or number '(gnus-summary-article-number)))))
2964
2965 (defmacro gnus-summary-article-pos (&optional number)
2966   "Return the position of the line of article NUMBER."
2967   `(gnus-data-pos (gnus-data-find
2968                    ,(or number '(gnus-summary-article-number)))))
2969
2970 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2971 (defmacro gnus-summary-article-subject (&optional number)
2972   "Return current subject string or nil if nothing."
2973   `(let ((headers
2974           ,(if number
2975                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2976              '(gnus-data-header (assq (gnus-summary-article-number)
2977                                       gnus-newsgroup-data)))))
2978      (and headers
2979           (vectorp headers)
2980           (mail-header-subject headers))))
2981
2982 (defmacro gnus-summary-article-score (&optional number)
2983   "Return current article score."
2984   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2985                   gnus-newsgroup-scored))
2986        gnus-summary-default-score 0))
2987
2988 (defun gnus-summary-article-children (&optional number)
2989   "Return a list of article numbers that are children of article NUMBER."
2990   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2991          (level (gnus-data-level (car data)))
2992          l children)
2993     (while (and (setq data (cdr data))
2994                 (> (setq l (gnus-data-level (car data))) level))
2995       (and (= (1+ level) l)
2996            (push (gnus-data-number (car data))
2997                  children)))
2998     (nreverse children)))
2999
3000 (defun gnus-summary-article-parent (&optional number)
3001   "Return the article number of the parent of article NUMBER."
3002   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3003                                     (gnus-data-list t)))
3004          (level (gnus-data-level (car data))))
3005     (if (zerop level)
3006         ()                              ; This is a root.
3007       ;; We search until we find an article with a level less than
3008       ;; this one.  That function has to be the parent.
3009       (while (and (setq data (cdr data))
3010                   (not (< (gnus-data-level (car data)) level))))
3011       (and data (gnus-data-number (car data))))))
3012
3013 (defun gnus-unread-mark-p (mark)
3014   "Say whether MARK is the unread mark."
3015   (= mark gnus-unread-mark))
3016
3017 (defun gnus-read-mark-p (mark)
3018   "Say whether MARK is one of the marks that mark as read.
3019 This is all marks except unread, ticked, dormant, and expirable."
3020   (not (or (= mark gnus-unread-mark)
3021            (= mark gnus-ticked-mark)
3022            (= mark gnus-spam-mark)
3023            (= mark gnus-dormant-mark)
3024            (= mark gnus-expirable-mark))))
3025
3026 (defmacro gnus-article-mark (number)
3027   "Return the MARK of article NUMBER.
3028 This macro should only be used when computing the mark the \"first\"
3029 time; i.e., when generating the summary lines.  After that,
3030 `gnus-summary-article-mark' should be used to examine the
3031 marks of articles."
3032   `(cond
3033     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3034     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3035     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3036     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3037     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3038     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3039     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3040     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3041            gnus-ancient-mark))))
3042
3043 ;; Saving hidden threads.
3044
3045 (defmacro gnus-save-hidden-threads (&rest forms)
3046   "Save hidden threads, eval FORMS, and restore the hidden threads."
3047   (let ((config (make-symbol "config")))
3048     `(let ((,config (gnus-hidden-threads-configuration)))
3049        (unwind-protect
3050            (save-excursion
3051              ,@forms)
3052          (gnus-restore-hidden-threads-configuration ,config)))))
3053 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3054 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3055
3056 (defun gnus-data-compute-positions ()
3057   "Compute the positions of all articles."
3058   (setq gnus-newsgroup-data-reverse nil)
3059   (let ((data gnus-newsgroup-data))
3060     (save-excursion
3061       (gnus-save-hidden-threads
3062         (gnus-summary-show-all-threads)
3063         (goto-char (point-min))
3064         (while data
3065           (while (get-text-property (point) 'gnus-intangible)
3066             (forward-line 1))
3067           (gnus-data-set-pos (car data) (+ (point) 3))
3068           (setq data (cdr data))
3069           (forward-line 1))))))
3070
3071 (defun gnus-hidden-threads-configuration ()
3072   "Return the current hidden threads configuration."
3073   (save-excursion
3074     (let (config)
3075       (goto-char (point-min))
3076       (while (search-forward "\r" nil t)
3077         (push (1- (point)) config))
3078       config)))
3079
3080 (defun gnus-restore-hidden-threads-configuration (config)
3081   "Restore hidden threads configuration from CONFIG."
3082   (save-excursion
3083     (let (point buffer-read-only)
3084       (while (setq point (pop config))
3085         (when (and (< point (point-max))
3086                    (goto-char point)
3087                    (eq (char-after) ?\n))
3088           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3089
3090 ;; Various summary mode internalish functions.
3091
3092 (defun gnus-mouse-pick-article (e)
3093   (interactive "e")
3094   (mouse-set-point e)
3095   (gnus-summary-next-page nil t))
3096
3097 (defun gnus-summary-set-display-table ()
3098   "Change the display table.
3099 Odd characters have a tendency to mess
3100 up nicely formatted displays - we make all possible glyphs
3101 display only a single character."
3102
3103   ;; We start from the standard display table, if any.
3104   (let ((table (or (copy-sequence standard-display-table)
3105                    (make-display-table)))
3106         (i 32))
3107     ;; Nix out all the control chars...
3108     (while (>= (setq i (1- i)) 0)
3109       (aset table i [??]))
3110     ;; ... but not newline and cr, of course.  (cr is necessary for the
3111     ;; selective display).
3112     (aset table ?\n nil)
3113     (aset table ?\r nil)
3114     ;; We keep TAB as well.
3115     (aset table ?\t nil)
3116     ;; We nix out any glyphs over 126 that are not set already.
3117     (let ((i 256))
3118       (while (>= (setq i (1- i)) 127)
3119         ;; Only modify if the entry is nil.
3120         (unless (aref table i)
3121           (aset table i [??]))))
3122     (setq buffer-display-table table)))
3123
3124 (defun gnus-summary-set-article-display-arrow (pos)
3125   "Update the overlay arrow to point to line at position POS."
3126   (when (and gnus-summary-display-arrow
3127              (boundp 'overlay-arrow-position)
3128              (boundp 'overlay-arrow-string))
3129     (save-excursion
3130       (goto-char pos)
3131       (beginning-of-line)
3132       (unless overlay-arrow-position
3133         (setq overlay-arrow-position (make-marker)))
3134       (setq overlay-arrow-string "=>"
3135             overlay-arrow-position (set-marker overlay-arrow-position
3136                                                (point)
3137                                                (current-buffer))))))
3138
3139 (defun gnus-summary-setup-buffer (group)
3140   "Initialize summary buffer."
3141   (let ((buffer (gnus-summary-buffer-name group))
3142         (dead-name (concat "*Dead Summary "
3143                            (gnus-group-decoded-name group) "*")))
3144     ;; If a dead summary buffer exists, we kill it.
3145     (when (gnus-buffer-live-p dead-name)
3146       (gnus-kill-buffer dead-name))
3147     (if (get-buffer buffer)
3148         (progn
3149           (set-buffer buffer)
3150           (setq gnus-summary-buffer (current-buffer))
3151           (not gnus-newsgroup-prepared))
3152       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3153       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3154       (gnus-summary-mode group)
3155       (when gnus-carpal
3156         (gnus-carpal-setup-buffer 'summary))
3157       (unless gnus-single-article-buffer
3158         (make-local-variable 'gnus-article-buffer)
3159         (make-local-variable 'gnus-article-current)
3160         (make-local-variable 'gnus-original-article-buffer))
3161       (setq gnus-newsgroup-name group)
3162       ;; Set any local variables in the group parameters.
3163       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3164       t)))
3165
3166 (defun gnus-set-global-variables ()
3167   "Set the global equivalents of the buffer-local variables.
3168 They are set to the latest values they had.  These reflect the summary
3169 buffer that was in action when the last article was fetched."
3170   (when (eq major-mode 'gnus-summary-mode)
3171     (setq gnus-summary-buffer (current-buffer))
3172     (let ((name gnus-newsgroup-name)
3173           (marked gnus-newsgroup-marked)
3174           (spam gnus-newsgroup-spam-marked)
3175           (unread gnus-newsgroup-unreads)
3176           (headers gnus-current-headers)
3177           (data gnus-newsgroup-data)
3178           (summary gnus-summary-buffer)
3179           (article-buffer gnus-article-buffer)
3180           (original gnus-original-article-buffer)
3181           (gac gnus-article-current)
3182           (reffed gnus-reffed-article-number)
3183           (score-file gnus-current-score-file)
3184           (default-charset gnus-newsgroup-charset)
3185           vlist)
3186       (let ((locals gnus-newsgroup-variables))
3187         (while locals
3188           (if (consp (car locals))
3189               (push (eval (caar locals)) vlist)
3190             (push (eval (car locals)) vlist))
3191           (setq locals (cdr locals)))
3192         (setq vlist (nreverse vlist)))
3193       (save-excursion
3194         (set-buffer gnus-group-buffer)
3195         (setq gnus-newsgroup-name name
3196               gnus-newsgroup-marked marked
3197               gnus-newsgroup-spam-marked spam
3198               gnus-newsgroup-unreads unread
3199               gnus-current-headers headers
3200               gnus-newsgroup-data data
3201               gnus-article-current gac
3202               gnus-summary-buffer summary
3203               gnus-article-buffer article-buffer
3204               gnus-original-article-buffer original
3205               gnus-reffed-article-number reffed
3206               gnus-current-score-file score-file
3207               gnus-newsgroup-charset default-charset)
3208         (let ((locals gnus-newsgroup-variables))
3209           (while locals
3210             (if (consp (car locals))
3211                 (set (caar locals) (pop vlist))
3212               (set (car locals) (pop vlist)))
3213             (setq locals (cdr locals))))
3214         ;; The article buffer also has local variables.
3215         (when (gnus-buffer-live-p gnus-article-buffer)
3216           (set-buffer gnus-article-buffer)
3217           (setq gnus-summary-buffer summary))))))
3218
3219 (defun gnus-summary-article-unread-p (article)
3220   "Say whether ARTICLE is unread or not."
3221   (memq article gnus-newsgroup-unreads))
3222
3223 (defun gnus-summary-first-article-p (&optional article)
3224   "Return whether ARTICLE is the first article in the buffer."
3225   (if (not (setq article (or article (gnus-summary-article-number))))
3226       nil
3227     (eq article (caar gnus-newsgroup-data))))
3228
3229 (defun gnus-summary-last-article-p (&optional article)
3230   "Return whether ARTICLE is the last article in the buffer."
3231   (if (not (setq article (or article (gnus-summary-article-number))))
3232       ;; All non-existent numbers are the last article.  :-)
3233       t
3234     (not (cdr (gnus-data-find-list article)))))
3235
3236 (defun gnus-make-thread-indent-array ()
3237   (let ((n 200))
3238     (unless (and gnus-thread-indent-array
3239                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3240       (setq gnus-thread-indent-array (make-vector 201 "")
3241             gnus-thread-indent-array-level gnus-thread-indent-level)
3242       (while (>= n 0)
3243         (aset gnus-thread-indent-array n
3244               (make-string (* n gnus-thread-indent-level) ? ))
3245         (setq n (1- n))))))
3246
3247 (defun gnus-update-summary-mark-positions ()
3248   "Compute where the summary marks are to go."
3249   (save-excursion
3250     (when (gnus-buffer-exists-p gnus-summary-buffer)
3251       (set-buffer gnus-summary-buffer))
3252     (let ((gnus-replied-mark 129)
3253           (gnus-score-below-mark 130)
3254           (gnus-score-over-mark 130)
3255           (gnus-undownloaded-mark 131)
3256           (spec gnus-summary-line-format-spec)
3257           gnus-visual pos)
3258       (save-excursion
3259         (gnus-set-work-buffer)
3260         (let ((gnus-summary-line-format-spec spec)
3261               (gnus-newsgroup-downloadable '(0)))
3262           (gnus-summary-insert-line
3263            (make-full-mail-header 0 "" "nobody"
3264                                   "05 Apr 2001 23:33:09 +0400"
3265                                   "" "" 0 0 "" nil)
3266            0 nil t 128 t nil "" nil 1)
3267           (goto-char (point-min))
3268           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3269                                              (- (point) (point-min) 1)))))
3270           (goto-char (point-min))
3271           (push (cons 'replied (and (search-forward "\201" nil t)
3272                                     (- (point) (point-min) 1)))
3273                 pos)
3274           (goto-char (point-min))
3275           (push (cons 'score (and (search-forward "\202" nil t)
3276                                   (- (point) (point-min) 1)))
3277                 pos)
3278           (goto-char (point-min))
3279           (push (cons 'download
3280                       (and (search-forward "\203" nil t)
3281                            (- (point) (point-min) 1)))
3282                 pos)))
3283       (setq gnus-summary-mark-positions pos))))
3284
3285 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3286   "Insert a dummy root in the summary buffer."
3287   (beginning-of-line)
3288   (gnus-add-text-properties
3289    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3290    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3291
3292 (defun gnus-summary-extract-address-component (from)
3293   (or (car (funcall gnus-extract-address-components from))
3294       from))
3295
3296 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3297   (let ((default-mime-charset (with-current-buffer gnus-summary-buffer
3298                                 default-mime-charset)))
3299     ;; Is it really necessary to do this next part for each summary line?
3300     ;; Luckily, doesn't seem to slow things down much.
3301     (or
3302      (and gnus-ignored-from-addresses
3303           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3304           (let ((extra-headers (mail-header-extra header))
3305                 to
3306                 newsgroups)
3307             (cond
3308              ((setq to (cdr (assq 'To extra-headers)))
3309               (concat "-> "
3310                       (inline
3311                         (gnus-summary-extract-address-component
3312                          (funcall gnus-decode-encoded-word-function to)))))
3313              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3314               (concat "=> " newsgroups)))))
3315      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3316
3317 (defun gnus-summary-insert-line (gnus-tmp-header
3318                                  gnus-tmp-level gnus-tmp-current
3319                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3320                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3321                                  &optional gnus-tmp-dummy gnus-tmp-score
3322                                  gnus-tmp-process)
3323   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3324          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3325          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3326          (gnus-tmp-score-char
3327           (if (or (null gnus-summary-default-score)
3328                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3329                       gnus-summary-zcore-fuzz))
3330               ?\ ;;;Whitespace
3331             (if (< gnus-tmp-score gnus-summary-default-score)
3332                 gnus-score-below-mark gnus-score-over-mark)))
3333          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3334          (gnus-tmp-replied
3335           (cond (gnus-tmp-process gnus-process-mark)
3336                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3337                  gnus-cached-mark)
3338                 (gnus-tmp-replied gnus-replied-mark)
3339                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3340                  gnus-forwarded-mark)
3341                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3342                  gnus-saved-mark)
3343                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3344                  gnus-recent-mark)
3345                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3346                  gnus-unseen-mark)
3347                 (t gnus-no-mark)))
3348          (gnus-tmp-downloaded
3349           (cond (undownloaded
3350                  gnus-undownloaded-mark)
3351                 (gnus-newsgroup-agentized
3352                  gnus-downloaded-mark)
3353                 (t
3354                  gnus-no-mark)))
3355          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3356          (gnus-tmp-name
3357           (cond
3358            ((string-match "<[^>]+> *$" gnus-tmp-from)
3359             (let ((beg (match-beginning 0)))
3360               (or (and (string-match "^\".+\"" gnus-tmp-from)
3361                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3362                   (substring gnus-tmp-from 0 beg))))
3363            ((string-match "(.+)" gnus-tmp-from)
3364             (substring gnus-tmp-from
3365                        (1+ (match-beginning 0)) (1- (match-end 0))))
3366            (t gnus-tmp-from)))
3367          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3368          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3369          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3370          (buffer-read-only nil))
3371     (when (string= gnus-tmp-name "")
3372       (setq gnus-tmp-name gnus-tmp-from))
3373     (unless (numberp gnus-tmp-lines)
3374       (setq gnus-tmp-lines -1))
3375     (if (= gnus-tmp-lines -1)
3376         (setq gnus-tmp-lines "?")
3377       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3378     (gnus-put-text-property-excluding-characters-with-faces
3379      (point)
3380      (progn (eval gnus-summary-line-format-spec) (point))
3381      'gnus-number gnus-tmp-number)
3382     (when (gnus-visual-p 'summary-highlight 'highlight)
3383       (forward-line -1)
3384       (gnus-run-hooks 'gnus-summary-update-hook)
3385       (forward-line 1))))
3386
3387 (defun gnus-summary-update-line (&optional dont-update)
3388   "Update summary line after change."
3389   (when (and gnus-summary-default-score
3390              (not gnus-summary-inhibit-highlight))
3391     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3392            (article (gnus-summary-article-number))
3393            (score (gnus-summary-article-score article)))
3394       (unless dont-update
3395         (if (and gnus-summary-mark-below
3396                  (< (gnus-summary-article-score)
3397                     gnus-summary-mark-below))
3398             ;; This article has a low score, so we mark it as read.
3399             (when (memq article gnus-newsgroup-unreads)
3400               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3401           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3402             ;; This article was previously marked as read on account
3403             ;; of a low score, but now it has risen, so we mark it as
3404             ;; unread.
3405             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3406         (gnus-summary-update-mark
3407          (if (or (null gnus-summary-default-score)
3408                  (<= (abs (- score gnus-summary-default-score))
3409                      gnus-summary-zcore-fuzz))
3410              ?\ ;;;Whitespace
3411            (if (< score gnus-summary-default-score)
3412                gnus-score-below-mark gnus-score-over-mark))
3413          'score))
3414       ;; Do visual highlighting.
3415       (when (gnus-visual-p 'summary-highlight 'highlight)
3416         (gnus-run-hooks 'gnus-summary-update-hook)))))
3417
3418 (defvar gnus-tmp-new-adopts nil)
3419
3420 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3421   "Return the number of articles in THREAD.
3422 This may be 0 in some cases -- if none of the articles in
3423 the thread are to be displayed."
3424   (let* ((number
3425           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3426           (cond
3427            ((not (listp thread))
3428             1)
3429            ((and (consp thread) (cdr thread))
3430             (apply
3431              '+ 1 (mapcar
3432                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3433            ((null thread)
3434             1)
3435            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3436             1)
3437            (t 0))))
3438     (when (and level (zerop level) gnus-tmp-new-adopts)
3439       (incf number
3440             (apply '+ (mapcar
3441                        'gnus-summary-number-of-articles-in-thread
3442                        gnus-tmp-new-adopts))))
3443     (if char
3444         (if (> number 1) gnus-not-empty-thread-mark
3445           gnus-empty-thread-mark)
3446       number)))
3447
3448 (defsubst gnus-summary-line-message-size (head)
3449   "Return pretty-printed version of message size.
3450 This function is intended to be used in
3451 `gnus-summary-line-format-alist'."
3452   (let ((c (or (mail-header-chars head) -1)))
3453     (cond ((< c 0) "n/a")               ; chars not available
3454           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3455           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3456           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3457           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3458
3459
3460 (defun gnus-summary-set-local-parameters (group)
3461   "Go through the local params of GROUP and set all variable specs in that list."
3462   (let ((params (gnus-group-find-parameter group))
3463         (vars '(quit-config))           ; Ignore quit-config.
3464         elem)
3465     (while params
3466       (setq elem (car params)
3467             params (cdr params))
3468       (and (consp elem)                 ; Has to be a cons.
3469            (consp (cdr elem))           ; The cdr has to be a list.
3470            (symbolp (car elem))         ; Has to be a symbol in there.
3471            (not (memq (car elem) vars))
3472            (ignore-errors               ; So we set it.
3473              (push (car elem) vars)
3474              (make-local-variable (car elem))
3475              (set (car elem) (eval (nth 1 elem))))))))
3476
3477 (defun gnus-summary-read-group (group &optional show-all no-article
3478                                       kill-buffer no-display backward
3479                                       select-articles)
3480   "Start reading news in newsgroup GROUP.
3481 If SHOW-ALL is non-nil, already read articles are also listed.
3482 If NO-ARTICLE is non-nil, no article is selected initially.
3483 If NO-DISPLAY, don't generate a summary buffer."
3484   (let (result)
3485     (while (and group
3486                 (null (setq result
3487                             (let ((gnus-auto-select-next nil))
3488                               (or (gnus-summary-read-group-1
3489                                    group show-all no-article
3490                                    kill-buffer no-display
3491                                    select-articles)
3492                                   (setq show-all nil
3493                                         select-articles nil)))))
3494                 (eq gnus-auto-select-next 'quietly))
3495       (set-buffer gnus-group-buffer)
3496       ;; The entry function called above goes to the next
3497       ;; group automatically, so we go two groups back
3498       ;; if we are searching for the previous group.
3499       (when backward
3500         (gnus-group-prev-unread-group 2))
3501       (if (not (equal group (gnus-group-group-name)))
3502           (setq group (gnus-group-group-name))
3503         (setq group nil)))
3504     result))
3505
3506 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3507   "Directly jump to the other GROUP from summary buffer.
3508 If SHOW-ALL is non-nil, already read articles are also listed."
3509   (interactive
3510    (if (eq gnus-summary-buffer (current-buffer))
3511        (list (completing-read
3512               "Group: " gnus-active-hashtb nil t
3513               (when (and gnus-newsgroup-name
3514                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3515                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3516               'gnus-group-history)
3517              current-prefix-arg)
3518      (error "%s must be invoked from a gnus summary buffer." this-command)))
3519   (unless (or (zerop (length group))
3520               (and gnus-newsgroup-name
3521                    (string-equal gnus-newsgroup-name group)))
3522     (gnus-summary-exit)
3523     (gnus-summary-read-group group show-all
3524                              gnus-dont-select-after-jump-to-other-group)))
3525
3526 (defun gnus-summary-read-group-1 (group show-all no-article
3527                                         kill-buffer no-display
3528                                         &optional select-articles)
3529   ;; Killed foreign groups can't be entered.
3530   ;;  (when (and (not (gnus-group-native-p group))
3531   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3532   ;;    (error "Dead non-native groups can't be entered"))
3533   (gnus-message 5 "Retrieving newsgroup: %s..."
3534                 (gnus-group-decoded-name group))
3535   (let* ((new-group (gnus-summary-setup-buffer group))
3536          (quit-config (gnus-group-quit-config group))
3537          (did-select (and new-group (gnus-select-newsgroup
3538                                      group show-all select-articles))))
3539     (cond
3540      ;; This summary buffer exists already, so we just select it.
3541      ((not new-group)
3542       (gnus-set-global-variables)
3543       (when kill-buffer
3544         (gnus-kill-or-deaden-summary kill-buffer))
3545       (gnus-configure-windows 'summary 'force)
3546       (gnus-set-mode-line 'summary)
3547       (gnus-summary-position-point)
3548       (message "")
3549       t)
3550      ;; We couldn't select this group.
3551      ((null did-select)
3552       (when (and (eq major-mode 'gnus-summary-mode)
3553                  (not (equal (current-buffer) kill-buffer)))
3554         (kill-buffer (current-buffer))
3555         (if (not quit-config)
3556             (progn
3557               ;; Update the info -- marks might need to be removed,
3558               ;; for instance.
3559               (gnus-summary-update-info)
3560               (set-buffer gnus-group-buffer)
3561               (gnus-group-jump-to-group group)
3562               (gnus-group-next-unread-group 1))
3563           (gnus-handle-ephemeral-exit quit-config)))
3564       (let ((grpinfo (gnus-get-info group)))
3565         (if (null (gnus-info-read grpinfo))
3566             (gnus-message 3 "Group %s contains no messages"
3567                           (gnus-group-decoded-name group))
3568           (gnus-message 3 "Can't select group")))
3569       nil)
3570      ;; The user did a `C-g' while prompting for number of articles,
3571      ;; so we exit this group.
3572      ((eq did-select 'quit)
3573       (and (eq major-mode 'gnus-summary-mode)
3574            (not (equal (current-buffer) kill-buffer))
3575            (kill-buffer (current-buffer)))
3576       (when kill-buffer
3577         (gnus-kill-or-deaden-summary kill-buffer))
3578       (if (not quit-config)
3579           (progn
3580             (set-buffer gnus-group-buffer)
3581             (gnus-group-jump-to-group group)
3582             (gnus-group-next-unread-group 1)
3583             (gnus-configure-windows 'group 'force))
3584         (gnus-handle-ephemeral-exit quit-config))
3585       ;; Finally signal the quit.
3586       (signal 'quit nil))
3587      ;; The group was successfully selected.
3588      (t
3589       (gnus-set-global-variables)
3590       ;; Save the active value in effect when the group was entered.
3591       (setq gnus-newsgroup-active
3592             (gnus-copy-sequence
3593              (gnus-active gnus-newsgroup-name)))
3594       ;; You can change the summary buffer in some way with this hook.
3595       (gnus-run-hooks 'gnus-select-group-hook)
3596       (gnus-update-format-specifications
3597        nil 'summary 'summary-mode 'summary-dummy)
3598       (gnus-update-summary-mark-positions)
3599       ;; Do score processing.
3600       (when gnus-use-scoring
3601         (gnus-possibly-score-headers))
3602       ;; Check whether to fill in the gaps in the threads.
3603       (when gnus-build-sparse-threads
3604         (gnus-build-sparse-threads))
3605       ;; Find the initial limit.
3606       (if gnus-show-threads
3607           (if show-all
3608               (let ((gnus-newsgroup-dormant nil))
3609                 (gnus-summary-initial-limit show-all))
3610             (gnus-summary-initial-limit show-all))
3611         ;; When unthreaded, all articles are always shown.
3612         (setq gnus-newsgroup-limit
3613               (mapcar
3614                (lambda (header) (mail-header-number header))
3615                gnus-newsgroup-headers)))
3616       ;; Generate the summary buffer.
3617       (unless no-display
3618         (gnus-summary-prepare))
3619       (when gnus-use-trees
3620         (gnus-tree-open group)
3621         (setq gnus-summary-highlight-line-function
3622               'gnus-tree-highlight-article))
3623       ;; If the summary buffer is empty, but there are some low-scored
3624       ;; articles or some excluded dormants, we include these in the
3625       ;; buffer.
3626       (when (and (zerop (buffer-size))
3627                  (not no-display))
3628         (cond (gnus-newsgroup-dormant
3629                (gnus-summary-limit-include-dormant))
3630               ((and gnus-newsgroup-scored show-all)
3631                (gnus-summary-limit-include-expunged t))))
3632       ;; Function `gnus-apply-kill-file' must be called in this hook.
3633       (gnus-run-hooks 'gnus-apply-kill-hook)
3634       (if (and (zerop (buffer-size))
3635                (not no-display))
3636           (progn
3637             ;; This newsgroup is empty.
3638             (gnus-summary-catchup-and-exit nil t)
3639             (gnus-message 6 "No unread news")
3640             (when kill-buffer
3641               (gnus-kill-or-deaden-summary kill-buffer))
3642             ;; Return nil from this function.
3643             nil)
3644         ;; Hide conversation thread subtrees.  We cannot do this in
3645         ;; gnus-summary-prepare-hook since kill processing may not
3646         ;; work with hidden articles.
3647         (gnus-summary-maybe-hide-threads)
3648         (when kill-buffer
3649           (gnus-kill-or-deaden-summary kill-buffer))
3650         (gnus-summary-auto-select-subject)
3651         ;; Show first unread article if requested.
3652         (if (and (not no-article)
3653                  (not no-display)
3654                  gnus-newsgroup-unreads
3655                  gnus-auto-select-first)
3656             (progn
3657               (gnus-configure-windows 'summary)
3658               (let ((art (gnus-summary-article-number)))
3659                 (unless (and (not gnus-plugged)
3660                              (or (memq art gnus-newsgroup-undownloaded)
3661                                  (memq art gnus-newsgroup-downloadable)))
3662                   (gnus-summary-goto-article art))))
3663           ;; Don't select any articles.
3664           (gnus-summary-position-point)
3665           (gnus-configure-windows 'summary 'force)
3666           (gnus-set-mode-line 'summary))
3667         (when (and gnus-auto-center-group
3668                    (get-buffer-window gnus-group-buffer t))
3669           ;; Gotta use windows, because recenter does weird stuff if
3670           ;; the current buffer ain't the displayed window.
3671           (let ((owin (selected-window)))
3672             (select-window (get-buffer-window gnus-group-buffer t))
3673             (when (gnus-group-goto-group group)
3674               (recenter))
3675             (select-window owin)))
3676         ;; Mark this buffer as "prepared".
3677         (setq gnus-newsgroup-prepared t)
3678         (gnus-run-hooks 'gnus-summary-prepared-hook)
3679         (unless (gnus-ephemeral-group-p group)
3680           (gnus-group-update-group group))
3681         t)))))
3682
3683 (defun gnus-summary-auto-select-subject ()
3684   "Select the subject line on initial group entry."
3685   (goto-char (point-min))
3686   (cond
3687    ((eq gnus-auto-select-subject 'best)
3688     (gnus-summary-best-unread-subject))
3689    ((eq gnus-auto-select-subject 'unread)
3690     (gnus-summary-first-unread-subject))
3691    ((eq gnus-auto-select-subject 'unseen)
3692     (gnus-summary-first-unseen-subject))
3693    ((eq gnus-auto-select-subject 'unseen-or-unread)
3694     (gnus-summary-first-unseen-or-unread-subject))
3695    ((eq gnus-auto-select-subject 'first)
3696     ;; Do nothing.
3697     )
3698    ((functionp gnus-auto-select-subject)
3699     (funcall gnus-auto-select-subject))))
3700
3701 (defun gnus-summary-prepare ()
3702   "Generate the summary buffer."
3703   (interactive)
3704   (let ((buffer-read-only nil))
3705     (erase-buffer)
3706     (setq gnus-newsgroup-data nil
3707           gnus-newsgroup-data-reverse nil)
3708     (gnus-run-hooks 'gnus-summary-generate-hook)
3709     ;; Generate the buffer, either with threads or without.
3710     (when gnus-newsgroup-headers
3711       (gnus-summary-prepare-threads
3712        (if gnus-show-threads
3713            (gnus-sort-gathered-threads
3714             (funcall gnus-summary-thread-gathering-function
3715                      (gnus-sort-threads
3716                       (gnus-cut-threads (gnus-make-threads)))))
3717          ;; Unthreaded display.
3718          (gnus-sort-articles gnus-newsgroup-headers))))
3719     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3720     ;; Call hooks for modifying summary buffer.
3721     (goto-char (point-min))
3722     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3723
3724 (defsubst gnus-general-simplify-subject (subject)
3725   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3726   (setq subject
3727         (cond
3728          ;; Truncate the subject.
3729          (gnus-simplify-subject-functions
3730           (gnus-map-function gnus-simplify-subject-functions subject))
3731          ((numberp gnus-summary-gather-subject-limit)
3732           (setq subject (gnus-simplify-subject-re subject))
3733           (if (> (length subject) gnus-summary-gather-subject-limit)
3734               (substring subject 0 gnus-summary-gather-subject-limit)
3735             subject))
3736          ;; Fuzzily simplify it.
3737          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3738           (gnus-simplify-subject-fuzzy subject))
3739          ;; Just remove the leading "Re:".
3740          (t
3741           (gnus-simplify-subject-re subject))))
3742
3743   (if (and gnus-summary-gather-exclude-subject
3744            (string-match gnus-summary-gather-exclude-subject subject))
3745       nil                               ; This article shouldn't be gathered
3746     subject))
3747
3748 (defun gnus-summary-simplify-subject-query ()
3749   "Query where the respool algorithm would put this article."
3750   (interactive)
3751   (gnus-summary-select-article)
3752   (message "%s"
3753            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3754
3755 (defun gnus-gather-threads-by-subject (threads)
3756   "Gather threads by looking at Subject headers."
3757   (if (not gnus-summary-make-false-root)
3758       threads
3759     (let ((hashtb (gnus-make-hashtable 1024))
3760           (prev threads)
3761           (result threads)
3762           subject hthread whole-subject)
3763       (while threads
3764         (setq subject (gnus-general-simplify-subject
3765                        (setq whole-subject (mail-header-subject
3766                                             (caar threads)))))
3767         (when subject
3768           (if (setq hthread (gnus-gethash subject hashtb))
3769               (progn
3770                 ;; We enter a dummy root into the thread, if we
3771                 ;; haven't done that already.
3772                 (unless (stringp (caar hthread))
3773                   (setcar hthread (list whole-subject (car hthread))))
3774                 ;; We add this new gathered thread to this gathered
3775                 ;; thread.
3776                 (setcdr (car hthread)
3777                         (nconc (cdar hthread) (list (car threads))))
3778                 ;; Remove it from the list of threads.
3779                 (setcdr prev (cdr threads))
3780                 (setq threads prev))
3781             ;; Enter this thread into the hash table.
3782             (gnus-sethash subject
3783                           (if gnus-summary-make-false-root-always
3784                               (progn
3785                                 ;; If you want a dummy root above all
3786                                 ;; threads...
3787                                 (setcar threads (list whole-subject
3788                                                       (car threads)))
3789                                 threads)
3790                             threads)
3791                           hashtb)))
3792         (setq prev threads)
3793         (setq threads (cdr threads)))
3794       result)))
3795
3796 (defun gnus-gather-threads-by-references (threads)
3797   "Gather threads by looking at References headers."
3798   (let ((idhashtb (gnus-make-hashtable 1024))
3799         (thhashtb (gnus-make-hashtable 1024))
3800         (prev threads)
3801         (result threads)
3802         ids references id gthread gid entered ref)
3803     (while threads
3804       (when (setq references (mail-header-references (caar threads)))
3805         (setq id (mail-header-id (caar threads))
3806               ids (inline (gnus-split-references references))
3807               entered nil)
3808         (while (setq ref (pop ids))
3809           (setq ids (delete ref ids))
3810           (if (not (setq gid (gnus-gethash ref idhashtb)))
3811               (progn
3812                 (gnus-sethash ref id idhashtb)
3813                 (gnus-sethash id threads thhashtb))
3814             (setq gthread (gnus-gethash gid thhashtb))
3815             (unless entered
3816               ;; We enter a dummy root into the thread, if we
3817               ;; haven't done that already.
3818               (unless (stringp (caar gthread))
3819                 (setcar gthread (list (mail-header-subject (caar gthread))
3820                                       (car gthread))))
3821               ;; We add this new gathered thread to this gathered
3822               ;; thread.
3823               (setcdr (car gthread)
3824                       (nconc (cdar gthread) (list (car threads)))))
3825             ;; Add it into the thread hash table.
3826             (gnus-sethash id gthread thhashtb)
3827             (setq entered t)
3828             ;; Remove it from the list of threads.
3829             (setcdr prev (cdr threads))
3830             (setq threads prev))))
3831       (setq prev threads)
3832       (setq threads (cdr threads)))
3833     result))
3834
3835 (defun gnus-sort-gathered-threads (threads)
3836   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3837   (let ((result threads))
3838     (while threads
3839       (when (stringp (caar threads))
3840         (setcdr (car threads)
3841                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3842       (setq threads (cdr threads)))
3843     result))
3844
3845 (defun gnus-thread-loop-p (root thread)
3846   "Say whether ROOT is in THREAD."
3847   (let ((stack (list thread))
3848         (infloop 0)
3849         th)
3850     (while (setq thread (pop stack))
3851       (setq th (cdr thread))
3852       (while (and th
3853                   (not (eq (caar th) root)))
3854         (pop th))
3855       (if th
3856           ;; We have found a loop.
3857           (let (ref-dep)
3858             (setcdr thread (delq (car th) (cdr thread)))
3859             (if (boundp (setq ref-dep (intern "none"
3860                                               gnus-newsgroup-dependencies)))
3861                 (setcdr (symbol-value ref-dep)
3862                         (nconc (cdr (symbol-value ref-dep))
3863                                (list (car th))))
3864               (set ref-dep (list nil (car th))))
3865             (setq infloop 1
3866                   stack nil))
3867         ;; Push all the subthreads onto the stack.
3868         (push (cdr thread) stack)))
3869     infloop))
3870
3871 (defun gnus-make-threads ()
3872   "Go through the dependency hashtb and find the roots.  Return all threads."
3873   (let (threads)
3874     (while (catch 'infloop
3875              (mapatoms
3876               (lambda (refs)
3877                 ;; Deal with self-referencing References loops.
3878                 (when (and (car (symbol-value refs))
3879                            (not (zerop
3880                                  (apply
3881                                   '+
3882                                   (mapcar
3883                                    (lambda (thread)
3884                                      (gnus-thread-loop-p
3885                                       (car (symbol-value refs)) thread))
3886                                    (cdr (symbol-value refs)))))))
3887                   (setq threads nil)
3888                   (throw 'infloop t))
3889                 (unless (car (symbol-value refs))
3890                   ;; These threads do not refer back to any other articles,
3891                   ;; so they're roots.
3892                   (setq threads (append (cdr (symbol-value refs)) threads))))
3893               gnus-newsgroup-dependencies)))
3894     threads))
3895
3896 ;; Build the thread tree.
3897 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3898   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3899
3900 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3901 if it was already present.
3902
3903 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3904 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3905 Message-IDs will be renamed to a unique Message-ID before being
3906 entered.
3907
3908 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3909   (let* ((id (mail-header-id header))
3910          (id-dep (and id (intern id dependencies)))
3911          parent-id ref ref-dep ref-header replaced)
3912     ;; Enter this `header' in the `dependencies' table.
3913     (cond
3914      ((not id-dep)
3915       (setq header nil))
3916      ;; The first two cases do the normal part: enter a new `header'
3917      ;; in the `dependencies' table.
3918      ((not (boundp id-dep))
3919       (set id-dep (list header)))
3920      ((null (car (symbol-value id-dep)))
3921       (setcar (symbol-value id-dep) header))
3922
3923      ;; From here the `header' was already present in the
3924      ;; `dependencies' table.
3925      (force-new
3926       ;; Overrides an existing entry;
3927       ;; just set the header part of the entry.
3928       (setcar (symbol-value id-dep) header)
3929       (setq replaced t))
3930
3931      ;; Renames the existing `header' to a unique Message-ID.
3932      ((not gnus-summary-ignore-duplicates)
3933       ;; An article with this Message-ID has already been seen.
3934       ;; We rename the Message-ID.
3935       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3936            (list header))
3937       (mail-header-set-id header id))
3938
3939      ;; The last case ignores an existing entry, except it adds any
3940      ;; additional Xrefs (in case the two articles came from different
3941      ;; servers.
3942      ;; Also sets `header' to `nil' meaning that the `dependencies'
3943      ;; table was *not* modified.
3944      (t
3945       (mail-header-set-xref
3946        (car (symbol-value id-dep))
3947        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3948                    "")
3949                (or (mail-header-xref header) "")))
3950       (setq header nil)))
3951
3952     (when (and header (not replaced))
3953       ;; First check that we are not creating a References loop.
3954       (setq parent-id (gnus-parent-id (mail-header-references header)))
3955       (setq ref parent-id)
3956       (while (and ref
3957                   (setq ref-dep (intern-soft ref dependencies))
3958                   (boundp ref-dep)
3959                   (setq ref-header (car (symbol-value ref-dep))))
3960         (if (string= id ref)
3961             ;; Yuk!  This is a reference loop.  Make the article be a
3962             ;; root article.
3963             (progn
3964               (mail-header-set-references (car (symbol-value id-dep)) "none")
3965               (setq ref nil)
3966               (setq parent-id nil))
3967           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3968       (setq ref-dep (intern (or parent-id "none") dependencies))
3969       (if (boundp ref-dep)
3970           (setcdr (symbol-value ref-dep)
3971                   (nconc (cdr (symbol-value ref-dep))
3972                          (list (symbol-value id-dep))))
3973         (set ref-dep (list nil (symbol-value id-dep)))))
3974     header))
3975
3976 (defun gnus-extract-message-id-from-in-reply-to (string)
3977   (if (string-match "<[^>]+>" string)
3978       (substring string (match-beginning 0) (match-end 0))
3979     nil))
3980
3981 (defun gnus-build-sparse-threads ()
3982   (let ((headers gnus-newsgroup-headers)
3983         (mail-parse-charset gnus-newsgroup-charset)
3984         (gnus-summary-ignore-duplicates t)
3985         header references generation relations
3986         subject child end new-child date)
3987     ;; First we create an alist of generations/relations, where
3988     ;; generations is how much we trust the relation, and the relation
3989     ;; is parent/child.
3990     (gnus-message 7 "Making sparse threads...")
3991     (save-excursion
3992       (nnheader-set-temp-buffer " *gnus sparse threads*")
3993       (while (setq header (pop headers))
3994         (when (and (setq references (mail-header-references header))
3995                    (not (string= references "")))
3996           (insert references)
3997           (setq child (mail-header-id header)
3998                 subject (mail-header-subject header)
3999                 date (mail-header-date header)
4000                 generation 0)
4001           (while (search-backward ">" nil t)
4002             (setq end (1+ (point)))
4003             (when (search-backward "<" nil t)
4004               (setq new-child (buffer-substring (point) end))
4005               (push (list (incf generation)
4006                           child (setq child new-child)
4007                           subject date)
4008                     relations)))
4009           (when child
4010             (push (list (1+ generation) child nil subject) relations))
4011           (erase-buffer)))
4012       (kill-buffer (current-buffer)))
4013     ;; Sort over trustworthiness.
4014     (mapcar
4015      (lambda (relation)
4016        (when (gnus-dependencies-add-header
4017               (make-full-mail-header-from-decoded-header
4018                gnus-reffed-article-number
4019                (nth 3 relation) "" (or (nth 4 relation) "")
4020                (nth 1 relation)
4021                (or (nth 2 relation) "") 0 0 "")
4022               gnus-newsgroup-dependencies nil)
4023          (push gnus-reffed-article-number gnus-newsgroup-limit)
4024          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4025          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4026                gnus-newsgroup-reads)
4027          (decf gnus-reffed-article-number)))
4028      (sort relations 'car-less-than-car))
4029     (gnus-message 7 "Making sparse threads...done")))
4030
4031 (defun gnus-build-old-threads ()
4032   ;; Look at all the articles that refer back to old articles, and
4033   ;; fetch the headers for the articles that aren't there.  This will
4034   ;; build complete threads - if the roots haven't been expired by the
4035   ;; server, that is.
4036   (let ((mail-parse-charset gnus-newsgroup-charset)
4037         id heads)
4038     (mapatoms
4039      (lambda (refs)
4040        (when (not (car (symbol-value refs)))
4041          (setq heads (cdr (symbol-value refs)))
4042          (while heads
4043            (if (memq (mail-header-number (caar heads))
4044                      gnus-newsgroup-dormant)
4045                (setq heads (cdr heads))
4046              (setq id (symbol-name refs))
4047              (while (and (setq id (gnus-build-get-header id))
4048                          (not (car (gnus-id-to-thread id)))))
4049              (setq heads nil)))))
4050      gnus-newsgroup-dependencies)))
4051
4052 (defsubst gnus-remove-odd-characters (string)
4053   "Translate STRING into something that doesn't contain weird characters."
4054   (mm-subst-char-in-string
4055    ?\r ?\-
4056    (mm-subst-char-in-string ?\n ?\- string t) t))
4057
4058 ;; This function has to be called with point after the article number
4059 ;; on the beginning of the line.
4060 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4061   (let ((eol (point-at-eol))
4062         (buffer (current-buffer))
4063         header references in-reply-to)
4064
4065     ;; overview: [num subject from date id refs chars lines misc]
4066     (unwind-protect
4067         (progn
4068           (narrow-to-region (point) eol)
4069           (unless (eobp)
4070             (forward-char))
4071
4072           (setq header
4073                 (make-full-mail-header
4074                  number                         ; number
4075                  (nnheader-nov-field)           ; subject
4076                  (nnheader-nov-field)           ; from
4077                  (nnheader-nov-field)           ; date
4078                  (nnheader-nov-read-message-id) ; id
4079                  (nnheader-nov-field)           ; refs
4080                  (nnheader-nov-read-integer)    ; chars
4081                  (nnheader-nov-read-integer)    ; lines
4082                  (unless (eobp)
4083                    (if (looking-at "Xref: ")
4084                        (goto-char (match-end 0)))
4085                    (nnheader-nov-field))        ; Xref
4086                  (nnheader-nov-parse-extra))))  ; extra
4087
4088       (widen))
4089
4090     (when (and (string= references "")
4091                (setq in-reply-to (mail-header-extra header))
4092                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4093       (mail-header-set-references
4094        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4095
4096     (when gnus-alter-header-function
4097       (funcall gnus-alter-header-function header))
4098     (gnus-dependencies-add-header header dependencies force-new)))
4099
4100 (defun gnus-build-get-header (id)
4101   "Look through the buffer of NOV lines and find the header to ID.
4102 Enter this line into the dependencies hash table, and return
4103 the id of the parent article (if any)."
4104   (let ((deps gnus-newsgroup-dependencies)
4105         found header)
4106     (prog1
4107         (save-excursion
4108           (set-buffer nntp-server-buffer)
4109           (let ((case-fold-search nil))
4110             (goto-char (point-min))
4111             (while (and (not found)
4112                         (search-forward id nil t))
4113               (beginning-of-line)
4114               (setq found (looking-at
4115                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4116                                    (regexp-quote id))))
4117               (or found (beginning-of-line 2)))
4118             (when found
4119               (beginning-of-line)
4120               (and
4121                (setq header (gnus-nov-parse-line
4122                              (read (current-buffer)) deps))
4123                (gnus-parent-id (mail-header-references header))))))
4124       (when header
4125         (let ((number (mail-header-number header)))
4126           (push number gnus-newsgroup-limit)
4127           (push header gnus-newsgroup-headers)
4128           (if (memq number gnus-newsgroup-unselected)
4129               (progn
4130                 (setq gnus-newsgroup-unreads
4131                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4132                                                number))
4133                 (setq gnus-newsgroup-unselected
4134                       (delq number gnus-newsgroup-unselected)))
4135             (push number gnus-newsgroup-ancient)))))))
4136
4137 (defun gnus-build-all-threads ()
4138   "Read all the headers."
4139   (let ((gnus-summary-ignore-duplicates t)
4140         (mail-parse-charset gnus-newsgroup-charset)
4141         (dependencies gnus-newsgroup-dependencies)
4142         header article)
4143     (save-excursion
4144       (set-buffer nntp-server-buffer)
4145       (let ((case-fold-search nil))
4146         (goto-char (point-min))
4147         (while (not (eobp))
4148           (ignore-errors
4149             (setq article (read (current-buffer))
4150                   header (gnus-nov-parse-line article dependencies)))
4151           (when header
4152             (save-excursion
4153               (set-buffer gnus-summary-buffer)
4154               (push header gnus-newsgroup-headers)
4155               (if (memq (setq article (mail-header-number header))
4156                         gnus-newsgroup-unselected)
4157                   (progn
4158                     (setq gnus-newsgroup-unreads
4159                           (gnus-add-to-sorted-list
4160                            gnus-newsgroup-unreads article))
4161                     (setq gnus-newsgroup-unselected
4162                           (delq article gnus-newsgroup-unselected)))
4163                 (push article gnus-newsgroup-ancient)))
4164             (forward-line 1)))))))
4165
4166 (defun gnus-summary-update-article-line (article header)
4167   "Update the line for ARTICLE using HEADER."
4168   (let* ((id (mail-header-id header))
4169          (thread (gnus-id-to-thread id)))
4170     (unless thread
4171       (error "Article in no thread"))
4172     ;; Update the thread.
4173     (setcar thread header)
4174     (gnus-summary-goto-subject article)
4175     (let* ((datal (gnus-data-find-list article))
4176            (data (car datal))
4177            (buffer-read-only nil)
4178            (level (gnus-summary-thread-level)))
4179       (gnus-delete-line)
4180       (let ((inserted (- (point)
4181                          (progn
4182                            (gnus-summary-insert-line
4183                             header level nil
4184                             (memq article gnus-newsgroup-undownloaded)
4185                             (gnus-article-mark article)
4186                             (memq article gnus-newsgroup-replied)
4187                             (memq article gnus-newsgroup-expirable)
4188                             ;; Only insert the Subject string when it's different
4189                             ;; from the previous Subject string.
4190                             (if (and
4191                                  gnus-show-threads
4192                                  (gnus-subject-equal
4193                                   (condition-case ()
4194                                       (mail-header-subject
4195                                        (gnus-data-header
4196                                         (cadr
4197                                          (gnus-data-find-list
4198                                           article
4199                                           (gnus-data-list t)))))
4200                                     ;; Error on the side of excessive subjects.
4201                                     (error ""))
4202                                   (mail-header-subject header)))
4203                                 ""
4204                               (mail-header-subject header))
4205                             nil (cdr (assq article gnus-newsgroup-scored))
4206                             (memq article gnus-newsgroup-processable))
4207                            (point)))))
4208         (when (cdr datal)
4209           (gnus-data-update-list
4210            (cdr datal)
4211            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4212
4213 (defun gnus-summary-update-article (article &optional iheader)
4214   "Update ARTICLE in the summary buffer."
4215   (set-buffer gnus-summary-buffer)
4216   (let* ((header (gnus-summary-article-header article))
4217          (id (mail-header-id header))
4218          (data (gnus-data-find article))
4219          (thread (gnus-id-to-thread id))
4220          (references (mail-header-references header))
4221          (parent
4222           (gnus-id-to-thread
4223            (or (gnus-parent-id
4224                 (when (and references
4225                            (not (equal "" references)))
4226                   references))
4227                "none")))
4228          (buffer-read-only nil)
4229          (old (car thread)))
4230     (when thread
4231       (unless iheader
4232         (setcar thread nil)
4233         (when parent
4234           (delq thread parent)))
4235       (if (gnus-summary-insert-subject id header)
4236           ;; Set the (possibly) new article number in the data structure.
4237           (gnus-data-set-number data (gnus-id-to-article id))
4238         (setcar thread old)
4239         nil))))
4240
4241 (defun gnus-rebuild-thread (id &optional line)
4242   "Rebuild the thread containing ID.
4243 If LINE, insert the rebuilt thread starting on line LINE."
4244   (let ((buffer-read-only nil)
4245         old-pos current thread data)
4246     (if (not gnus-show-threads)
4247         (setq thread (list (car (gnus-id-to-thread id))))
4248       ;; Get the thread this article is part of.
4249       (setq thread (gnus-remove-thread id)))
4250     (setq old-pos (point-at-bol))
4251     (setq current (save-excursion
4252                     (and (re-search-backward "[\r\n]" nil t)
4253                          (gnus-summary-article-number))))
4254     ;; If this is a gathered thread, we have to go some re-gathering.
4255     (when (stringp (car thread))
4256       (let ((subject (car thread))
4257             roots thr)
4258         (setq thread (cdr thread))
4259         (while thread
4260           (unless (memq (setq thr (gnus-id-to-thread
4261                                    (gnus-root-id
4262                                     (mail-header-id (caar thread)))))
4263                         roots)
4264             (push thr roots))
4265           (setq thread (cdr thread)))
4266         ;; We now have all (unique) roots.
4267         (if (= (length roots) 1)
4268             ;; All the loose roots are now one solid root.
4269             (setq thread (car roots))
4270           (setq thread (cons subject (gnus-sort-threads roots))))))
4271     (let (threads)
4272       ;; We then insert this thread into the summary buffer.
4273       (when line
4274         (goto-char (point-min))
4275         (forward-line (1- line)))
4276       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4277         (if gnus-show-threads
4278             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4279           (gnus-summary-prepare-unthreaded thread))
4280         (setq data (nreverse gnus-newsgroup-data))
4281         (setq threads gnus-newsgroup-threads))
4282       ;; We splice the new data into the data structure.
4283       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4284       ;;!!! then we want to insert at the beginning of the buffer.
4285       ;;!!! That happens to be true with Gnus now, but that may
4286       ;;!!! change in the future.  Perhaps.
4287       (gnus-data-enter-list
4288        (if line nil current) data (- (point) old-pos))
4289       (setq gnus-newsgroup-threads
4290             (nconc threads gnus-newsgroup-threads))
4291       (gnus-data-compute-positions))))
4292
4293 (defun gnus-number-to-header (number)
4294   "Return the header for article NUMBER."
4295   (let ((headers gnus-newsgroup-headers))
4296     (while (and headers
4297                 (not (= number (mail-header-number (car headers)))))
4298       (pop headers))
4299     (when headers
4300       (car headers))))
4301
4302 (defun gnus-parent-headers (in-headers &optional generation)
4303   "Return the headers of the GENERATIONeth parent of HEADERS."
4304   (unless generation
4305     (setq generation 1))
4306   (let ((parent t)
4307         (headers in-headers)
4308         references)
4309     (while (and parent
4310                 (not (zerop generation))
4311                 (setq references (mail-header-references headers)))
4312       (setq headers (if (and references
4313                              (setq parent (gnus-parent-id references)))
4314                         (car (gnus-id-to-thread parent))
4315                       nil))
4316       (decf generation))
4317     (and (not (eq headers in-headers))
4318          headers)))
4319
4320 (defun gnus-id-to-thread (id)
4321   "Return the (sub-)thread where ID appears."
4322   (gnus-gethash id gnus-newsgroup-dependencies))
4323
4324 (defun gnus-id-to-article (id)
4325   "Return the article number of ID."
4326   (let ((thread (gnus-id-to-thread id)))
4327     (when (and thread
4328                (car thread))
4329       (mail-header-number (car thread)))))
4330
4331 (defun gnus-id-to-header (id)
4332   "Return the article headers of ID."
4333   (car (gnus-id-to-thread id)))
4334
4335 (defun gnus-article-displayed-root-p (article)
4336   "Say whether ARTICLE is a root(ish) article."
4337   (let ((level (gnus-summary-thread-level article))
4338         (refs (mail-header-references  (gnus-summary-article-header article)))
4339         particle)
4340     (cond
4341      ((null level) nil)
4342      ((zerop level) t)
4343      ((null refs) t)
4344      ((null (gnus-parent-id refs)) t)
4345      ((and (= 1 level)
4346            (null (setq particle (gnus-id-to-article
4347                                  (gnus-parent-id refs))))
4348            (null (gnus-summary-thread-level particle)))))))
4349
4350 (defun gnus-root-id (id)
4351   "Return the id of the root of the thread where ID appears."
4352   (let (last-id prev)
4353     (while (and id (setq prev (car (gnus-id-to-thread id))))
4354       (setq last-id id
4355             id (gnus-parent-id (mail-header-references prev))))
4356     last-id))
4357
4358 (defun gnus-articles-in-thread (thread)
4359   "Return the list of articles in THREAD."
4360   (cons (mail-header-number (car thread))
4361         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4362
4363 (defun gnus-remove-thread (id &optional dont-remove)
4364   "Remove the thread that has ID in it."
4365   (let (headers thread last-id)
4366     ;; First go up in this thread until we find the root.
4367     (setq last-id (gnus-root-id id)
4368           headers (message-flatten-list (gnus-id-to-thread last-id)))
4369     ;; We have now found the real root of this thread.  It might have
4370     ;; been gathered into some loose thread, so we have to search
4371     ;; through the threads to find the thread we wanted.
4372     (let ((threads gnus-newsgroup-threads)
4373           sub)
4374       (while threads
4375         (setq sub (car threads))
4376         (if (stringp (car sub))
4377             ;; This is a gathered thread, so we look at the roots
4378             ;; below it to find whether this article is in this
4379             ;; gathered root.
4380             (progn
4381               (setq sub (cdr sub))
4382               (while sub
4383                 (when (member (caar sub) headers)
4384                   (setq thread (car threads)
4385                         threads nil
4386                         sub nil))
4387                 (setq sub (cdr sub))))
4388           ;; It's an ordinary thread, so we check it.
4389           (when (eq (car sub) (car headers))
4390             (setq thread sub
4391                   threads nil)))
4392         (setq threads (cdr threads)))
4393       ;; If this article is in no thread, then it's a root.
4394       (if thread
4395           (unless dont-remove
4396             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4397         (setq thread (gnus-id-to-thread last-id)))
4398       (when thread
4399         (prog1
4400             thread                      ; We return this thread.
4401           (unless dont-remove
4402             (if (stringp (car thread))
4403                 (progn
4404                   ;; If we use dummy roots, then we have to remove the
4405                   ;; dummy root as well.
4406                   (when (eq gnus-summary-make-false-root 'dummy)
4407                     ;; We go to the dummy root by going to
4408                     ;; the first sub-"thread", and then one line up.
4409                     (gnus-summary-goto-article
4410                      (mail-header-number (caadr thread)))
4411                     (forward-line -1)
4412                     (gnus-delete-line)
4413                     (gnus-data-compute-positions))
4414                   (setq thread (cdr thread))
4415                   (while thread
4416                     (gnus-remove-thread-1 (car thread))
4417                     (setq thread (cdr thread))))
4418               (gnus-remove-thread-1 thread))))))))
4419
4420 (defun gnus-remove-thread-1 (thread)
4421   "Remove the thread THREAD recursively."
4422   (let ((number (mail-header-number (pop thread)))
4423         d)
4424     (setq thread (reverse thread))
4425     (while thread
4426       (gnus-remove-thread-1 (pop thread)))
4427     (when (setq d (gnus-data-find number))
4428       (goto-char (gnus-data-pos d))
4429       (gnus-summary-show-thread)
4430       (gnus-data-remove
4431        number
4432        (- (point-at-bol)
4433           (prog1
4434               (1+ (point-at-eol))
4435             (gnus-delete-line)))))))
4436
4437 (defun gnus-sort-threads-1 (threads func)
4438   (sort (mapcar (lambda (thread)
4439                   (cons (car thread)
4440                         (and (cdr thread)
4441                              (gnus-sort-threads-1 (cdr thread) func))))
4442                 threads) func))
4443
4444 (defun gnus-sort-threads (threads)
4445   "Sort THREADS."
4446   (if (not gnus-thread-sort-functions)
4447       threads
4448     (gnus-message 8 "Sorting threads...")
4449     (let ((max-lisp-eval-depth 5000))
4450       (prog1 (gnus-sort-threads-1
4451          threads
4452          (gnus-make-sort-function gnus-thread-sort-functions))
4453         (gnus-message 8 "Sorting threads...done")))))
4454
4455 (defun gnus-sort-articles (articles)
4456   "Sort ARTICLES."
4457   (when gnus-article-sort-functions
4458     (gnus-message 7 "Sorting articles...")
4459     (prog1
4460         (setq gnus-newsgroup-headers
4461               (sort articles (gnus-make-sort-function
4462                               gnus-article-sort-functions)))
4463       (gnus-message 7 "Sorting articles...done"))))
4464
4465 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4466 (defmacro gnus-thread-header (thread)
4467   "Return header of first article in THREAD.
4468 Note that THREAD must never, ever be anything else than a variable -
4469 using some other form will lead to serious barfage."
4470   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4471   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4472   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4473         (vector thread) 2))
4474
4475 (defsubst gnus-article-sort-by-number (h1 h2)
4476   "Sort articles by article number."
4477   (< (mail-header-number h1)
4478      (mail-header-number h2)))
4479
4480 (defun gnus-thread-sort-by-number (h1 h2)
4481   "Sort threads by root article number."
4482   (gnus-article-sort-by-number
4483    (gnus-thread-header h1) (gnus-thread-header h2)))
4484
4485 (defsubst gnus-article-sort-by-random (h1 h2)
4486   "Sort articles by article number."
4487   (zerop (random 2)))
4488
4489 (defun gnus-thread-sort-by-random (h1 h2)
4490   "Sort threads by root article number."
4491   (gnus-article-sort-by-random
4492    (gnus-thread-header h1) (gnus-thread-header h2)))
4493
4494 (defsubst gnus-article-sort-by-lines (h1 h2)
4495   "Sort articles by article Lines header."
4496   (< (mail-header-lines h1)
4497      (mail-header-lines h2)))
4498
4499 (defun gnus-thread-sort-by-lines (h1 h2)
4500   "Sort threads by root article Lines header."
4501   (gnus-article-sort-by-lines
4502    (gnus-thread-header h1) (gnus-thread-header h2)))
4503
4504 (defsubst gnus-article-sort-by-chars (h1 h2)
4505   "Sort articles by octet length."
4506   (< (mail-header-chars h1)
4507      (mail-header-chars h2)))
4508
4509 (defun gnus-thread-sort-by-chars (h1 h2)
4510   "Sort threads by root article octet length."
4511   (gnus-article-sort-by-chars
4512    (gnus-thread-header h1) (gnus-thread-header h2)))
4513
4514 (defsubst gnus-article-sort-by-author (h1 h2)
4515   "Sort articles by root author."
4516   (string-lessp
4517    (let ((addr (car (mime-entity-read-field h1 'From))))
4518      (or (std11-full-name-string addr)
4519          (std11-address-string addr)
4520          ""))
4521    (let ((addr (car (mime-entity-read-field h2 'From))))
4522      (or (std11-full-name-string addr)
4523          (std11-address-string addr)
4524          ""))
4525    ))
4526
4527 (defun gnus-thread-sort-by-author (h1 h2)
4528   "Sort threads by root author."
4529   (gnus-article-sort-by-author
4530    (gnus-thread-header h1)  (gnus-thread-header h2)))
4531
4532 (defsubst gnus-article-sort-by-subject (h1 h2)
4533   "Sort articles by root subject."
4534   (string-lessp
4535    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4536    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4537
4538 (defun gnus-thread-sort-by-subject (h1 h2)
4539   "Sort threads by root subject."
4540   (gnus-article-sort-by-subject
4541    (gnus-thread-header h1) (gnus-thread-header h2)))
4542
4543 (defsubst gnus-article-sort-by-date (h1 h2)
4544   "Sort articles by root article date."
4545   (time-less-p
4546    (gnus-date-get-time (mail-header-date h1))
4547    (gnus-date-get-time (mail-header-date h2))))
4548
4549 (defun gnus-thread-sort-by-date (h1 h2)
4550   "Sort threads by root article date."
4551   (gnus-article-sort-by-date
4552    (gnus-thread-header h1) (gnus-thread-header h2)))
4553
4554 (defsubst gnus-article-sort-by-score (h1 h2)
4555   "Sort articles by root article score.
4556 Unscored articles will be counted as having a score of zero."
4557   (> (or (cdr (assq (mail-header-number h1)
4558                     gnus-newsgroup-scored))
4559          gnus-summary-default-score 0)
4560      (or (cdr (assq (mail-header-number h2)
4561                     gnus-newsgroup-scored))
4562          gnus-summary-default-score 0)))
4563
4564 (defun gnus-thread-sort-by-score (h1 h2)
4565   "Sort threads by root article score."
4566   (gnus-article-sort-by-score
4567    (gnus-thread-header h1) (gnus-thread-header h2)))
4568
4569 (defun gnus-thread-sort-by-total-score (h1 h2)
4570   "Sort threads by the sum of all scores in the thread.
4571 Unscored articles will be counted as having a score of zero."
4572   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4573
4574 (defun gnus-thread-total-score (thread)
4575   ;; This function find the total score of THREAD.
4576   (cond
4577    ((null thread)
4578     0)
4579    ((consp thread)
4580     (if (stringp (car thread))
4581         (apply gnus-thread-score-function 0
4582                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4583       (gnus-thread-total-score-1 thread)))
4584    (t
4585     (gnus-thread-total-score-1 (list thread)))))
4586
4587 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4588   "Sort threads such that the thread with the most recently arrived article comes first."
4589   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4590
4591 (defun gnus-thread-highest-number (thread)
4592   "Return the highest article number in THREAD."
4593   (apply 'max (mapcar (lambda (header)
4594                         (mail-header-number header))
4595                       (message-flatten-list thread))))
4596
4597 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4598   "Sort threads such that the thread with the most recently dated article comes first."
4599   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4600
4601 (defun gnus-thread-latest-date (thread)
4602   "Return the highest article date in THREAD."
4603   (let ((previous-time 0))
4604     (apply 'max
4605            (mapcar
4606             (lambda (header)
4607               (setq previous-time
4608                     (condition-case ()
4609                         (time-to-seconds (mail-header-parse-date
4610                                           (mail-header-date header)))
4611                       (error previous-time))))
4612             (sort
4613              (message-flatten-list thread)
4614              (lambda (h1 h2)
4615                (< (mail-header-number h1)
4616                   (mail-header-number h2))))))))
4617
4618 (defun gnus-thread-total-score-1 (root)
4619   ;; This function find the total score of the thread below ROOT.
4620   (setq root (car root))
4621   (apply gnus-thread-score-function
4622          (or (append
4623               (mapcar 'gnus-thread-total-score
4624                       (cdr (gnus-id-to-thread (mail-header-id root))))
4625               (when (> (mail-header-number root) 0)
4626                 (list (or (cdr (assq (mail-header-number root)
4627                                      gnus-newsgroup-scored))
4628                           gnus-summary-default-score 0))))
4629              (list gnus-summary-default-score)
4630              '(0))))
4631
4632 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4633 (defvar gnus-tmp-prev-subject nil)
4634 (defvar gnus-tmp-false-parent nil)
4635 (defvar gnus-tmp-root-expunged nil)
4636 (defvar gnus-tmp-dummy-line nil)
4637
4638 (eval-when-compile (defvar gnus-tmp-header))
4639 (defun gnus-extra-header (type &optional header)
4640   "Return the extra header of TYPE."
4641   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4642       ""))
4643
4644 (defvar gnus-tmp-thread-tree-header-string "")
4645
4646 (defcustom gnus-sum-thread-tree-root "> "
4647   "With %B spec, used for the root of a thread.
4648 If nil, use subject instead."
4649   :type '(radio (const :format "%v  " nil) (string :size 0))
4650   :group 'gnus-thread)
4651 (defcustom gnus-sum-thread-tree-false-root "> "
4652   "With %B spec, used for a false root of a thread.
4653 If nil, use subject instead."
4654   :type '(radio (const :format "%v  " nil) (string :size 0))
4655   :group 'gnus-thread)
4656 (defcustom gnus-sum-thread-tree-single-indent ""
4657   "With %B spec, used for a thread with just one message.
4658 If nil, use subject instead."
4659   :type '(radio (const :format "%v  " nil) (string :size 0))
4660   :group 'gnus-thread)
4661 (defcustom gnus-sum-thread-tree-vertical "| "
4662   "With %B spec, used for drawing a vertical line."
4663   :type 'string
4664   :group 'gnus-thread)
4665 (defcustom gnus-sum-thread-tree-indent "  "
4666   "With %B spec, used for indenting."
4667   :type 'string
4668   :group 'gnus-thread)
4669 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4670   "With %B spec, used for a leaf with brothers."
4671   :type 'string
4672   :group 'gnus-thread)
4673 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4674   "With %B spec, used for a leaf without brothers."
4675   :type 'string
4676   :group 'gnus-thread)
4677
4678 (defun gnus-summary-prepare-threads (threads)
4679   "Prepare summary buffer from THREADS and indentation LEVEL.
4680 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4681 or a straight list of headers."
4682   (gnus-message 7 "Generating summary...")
4683
4684   (setq gnus-newsgroup-threads threads)
4685   (beginning-of-line)
4686
4687   (let ((gnus-tmp-level 0)
4688         (default-score (or gnus-summary-default-score 0))
4689         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4690         (building-line-count gnus-summary-display-while-building)
4691         (building-count (integerp gnus-summary-display-while-building))
4692         thread number subject stack state gnus-tmp-gathered beg-match
4693         new-roots gnus-tmp-new-adopts thread-end simp-subject
4694         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4695         gnus-tmp-replied gnus-tmp-subject-or-nil
4696         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4697         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4698         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4699         tree-stack)
4700
4701     (setq gnus-tmp-prev-subject nil
4702           gnus-tmp-thread-tree-header-string "")
4703
4704     (if (vectorp (car threads))
4705         ;; If this is a straight (sic) list of headers, then a
4706         ;; threaded summary display isn't required, so we just create
4707         ;; an unthreaded one.
4708         (gnus-summary-prepare-unthreaded threads)
4709
4710       ;; Do the threaded display.
4711
4712       (if gnus-summary-display-while-building
4713           (switch-to-buffer (buffer-name)))
4714       (while (or threads stack gnus-tmp-new-adopts new-roots)
4715
4716         (if (and (= gnus-tmp-level 0)
4717                  (or (not stack)
4718                      (= (caar stack) 0))
4719                  (not gnus-tmp-false-parent)
4720                  (or gnus-tmp-new-adopts new-roots))
4721             (if gnus-tmp-new-adopts
4722                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4723                       thread (list (car gnus-tmp-new-adopts))
4724                       gnus-tmp-header (caar thread)
4725                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4726               (when new-roots
4727                 (setq thread (list (car new-roots))
4728                       gnus-tmp-header (caar thread)
4729                       new-roots (cdr new-roots))))
4730
4731           (if threads
4732               ;; If there are some threads, we do them before the
4733               ;; threads on the stack.
4734               (setq thread threads
4735                     gnus-tmp-header (caar thread))
4736             ;; There were no current threads, so we pop something off
4737             ;; the stack.
4738             (setq state (car stack)
4739                   gnus-tmp-level (car state)
4740                   tree-stack (cadr state)
4741                   thread (caddr state)
4742                   stack (cdr stack)
4743                   gnus-tmp-header (caar thread))))
4744
4745         (setq gnus-tmp-false-parent nil)
4746         (setq gnus-tmp-root-expunged nil)
4747         (setq thread-end nil)
4748
4749         (if (stringp gnus-tmp-header)
4750             ;; The header is a dummy root.
4751             (cond
4752              ((eq gnus-summary-make-false-root 'adopt)
4753               ;; We let the first article adopt the rest.
4754               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4755                                                (cddar thread)))
4756               (setq gnus-tmp-gathered
4757                     (nconc (mapcar
4758                             (lambda (h) (mail-header-number (car h)))
4759                             (cddar thread))
4760                            gnus-tmp-gathered))
4761               (setq thread (cons (list (caar thread)
4762                                        (cadar thread))
4763                                  (cdr thread)))
4764               (setq gnus-tmp-level -1
4765                     gnus-tmp-false-parent t))
4766              ((eq gnus-summary-make-false-root 'empty)
4767               ;; We print adopted articles with empty subject fields.
4768               (setq gnus-tmp-gathered
4769                     (nconc (mapcar
4770                             (lambda (h) (mail-header-number (car h)))
4771                             (cddar thread))
4772                            gnus-tmp-gathered))
4773               (setq gnus-tmp-level -1))
4774              ((eq gnus-summary-make-false-root 'dummy)
4775               ;; We remember that we probably want to output a dummy
4776               ;; root.
4777               (setq gnus-tmp-dummy-line gnus-tmp-header)
4778               (setq gnus-tmp-prev-subject gnus-tmp-header))
4779              (t
4780               ;; We do not make a root for the gathered
4781               ;; sub-threads at all.
4782               (setq gnus-tmp-level -1)))
4783
4784           (setq number (mail-header-number gnus-tmp-header)
4785                 subject (mail-header-subject gnus-tmp-header)
4786                 simp-subject (gnus-simplify-subject-fully subject))
4787
4788           (cond
4789            ;; If the thread has changed subject, we might want to make
4790            ;; this subthread into a root.
4791            ((and (null gnus-thread-ignore-subject)
4792                  (not (zerop gnus-tmp-level))
4793                  gnus-tmp-prev-subject
4794                  (not (string= gnus-tmp-prev-subject simp-subject)))
4795             (setq new-roots (nconc new-roots (list (car thread)))
4796                   thread-end t
4797                   gnus-tmp-header nil))
4798            ;; If the article lies outside the current limit,
4799            ;; then we do not display it.
4800            ((not (memq number gnus-newsgroup-limit))
4801             (setq gnus-tmp-gathered
4802                   (nconc (mapcar
4803                           (lambda (h) (mail-header-number (car h)))
4804                           (cdar thread))
4805                          gnus-tmp-gathered))
4806             (setq gnus-tmp-new-adopts (if (cdar thread)
4807                                           (append gnus-tmp-new-adopts
4808                                                   (cdar thread))
4809                                         gnus-tmp-new-adopts)
4810                   thread-end t
4811                   gnus-tmp-header nil)
4812             (when (zerop gnus-tmp-level)
4813               (setq gnus-tmp-root-expunged t)))
4814            ;; Perhaps this article is to be marked as read?
4815            ((and gnus-summary-mark-below
4816                  (< (or (cdr (assq number gnus-newsgroup-scored))
4817                         default-score)
4818                     gnus-summary-mark-below)
4819                  ;; Don't touch sparse articles.
4820                  (not (gnus-summary-article-sparse-p number))
4821                  (not (gnus-summary-article-ancient-p number)))
4822             (setq gnus-newsgroup-unreads
4823                   (delq number gnus-newsgroup-unreads))
4824             (if gnus-newsgroup-auto-expire
4825                 (setq gnus-newsgroup-expirable
4826                       (gnus-add-to-sorted-list
4827                        gnus-newsgroup-expirable number))
4828               (push (cons number gnus-low-score-mark)
4829                     gnus-newsgroup-reads))))
4830
4831           (when gnus-tmp-header
4832             ;; We may have an old dummy line to output before this
4833             ;; article.
4834             (when (and gnus-tmp-dummy-line
4835                        (gnus-subject-equal
4836                         gnus-tmp-dummy-line
4837                         (mail-header-subject gnus-tmp-header)))
4838               (gnus-summary-insert-dummy-line
4839                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4840               (setq gnus-tmp-dummy-line nil))
4841
4842             ;; Compute the mark.
4843             (setq gnus-tmp-unread (gnus-article-mark number))
4844
4845             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4846                                   gnus-tmp-header gnus-tmp-level)
4847                   gnus-newsgroup-data)
4848
4849             ;; Actually insert the line.
4850             (setq
4851              gnus-tmp-subject-or-nil
4852              (cond
4853               ((and gnus-thread-ignore-subject
4854                     gnus-tmp-prev-subject
4855                     (not (string= gnus-tmp-prev-subject simp-subject)))
4856                subject)
4857               ((zerop gnus-tmp-level)
4858                (if (and (eq gnus-summary-make-false-root 'empty)
4859                         (memq number gnus-tmp-gathered)
4860                         gnus-tmp-prev-subject
4861                         (string= gnus-tmp-prev-subject simp-subject))
4862                    gnus-summary-same-subject
4863                  subject))
4864               (t gnus-summary-same-subject)))
4865             (if (and (eq gnus-summary-make-false-root 'adopt)
4866                      (= gnus-tmp-level 1)
4867                      (memq number gnus-tmp-gathered))
4868                 (setq gnus-tmp-opening-bracket ?\<
4869                       gnus-tmp-closing-bracket ?\>)
4870               (setq gnus-tmp-opening-bracket ?\[
4871                     gnus-tmp-closing-bracket ?\]))
4872             (setq
4873              gnus-tmp-indentation
4874              (aref gnus-thread-indent-array gnus-tmp-level)
4875              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4876              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4877                                 gnus-summary-default-score 0)
4878              gnus-tmp-score-char
4879              (if (or (null gnus-summary-default-score)
4880                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4881                          gnus-summary-zcore-fuzz))
4882                  ?\ ;;;Whitespace
4883                (if (< gnus-tmp-score gnus-summary-default-score)
4884                    gnus-score-below-mark gnus-score-over-mark))
4885              gnus-tmp-replied
4886              (cond ((memq number gnus-newsgroup-processable)
4887                     gnus-process-mark)
4888                    ((memq number gnus-newsgroup-cached)
4889                     gnus-cached-mark)
4890                    ((memq number gnus-newsgroup-replied)
4891                     gnus-replied-mark)
4892                    ((memq number gnus-newsgroup-forwarded)
4893                     gnus-forwarded-mark)
4894                    ((memq number gnus-newsgroup-saved)
4895                     gnus-saved-mark)
4896                    ((memq number gnus-newsgroup-recent)
4897                     gnus-recent-mark)
4898                    ((memq number gnus-newsgroup-unseen)
4899                     gnus-unseen-mark)
4900                    (t gnus-no-mark))
4901              gnus-tmp-downloaded
4902              (cond ((memq number gnus-newsgroup-undownloaded)
4903                     gnus-undownloaded-mark)
4904                    (gnus-newsgroup-agentized
4905                     gnus-downloaded-mark)
4906                    (t
4907                     gnus-no-mark))
4908              gnus-tmp-from (mail-header-from gnus-tmp-header)
4909              gnus-tmp-name
4910              (cond
4911               ((string-match "<[^>]+> *$" gnus-tmp-from)
4912                (setq beg-match (match-beginning 0))
4913                (or (and (string-match "^\".+\"" gnus-tmp-from)
4914                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4915                    (substring gnus-tmp-from 0 beg-match)))
4916               ((string-match "(.+)" gnus-tmp-from)
4917                (substring gnus-tmp-from
4918                           (1+ (match-beginning 0)) (1- (match-end 0))))
4919               (t gnus-tmp-from))
4920
4921              ;; Do the %B string
4922              gnus-tmp-thread-tree-header-string
4923              (cond
4924               ((not gnus-show-threads) "")
4925               ((zerop gnus-tmp-level)
4926                (cond ((cdar thread)
4927                       (or gnus-sum-thread-tree-root subject))
4928                      (gnus-tmp-new-adopts
4929                       (or gnus-sum-thread-tree-false-root subject))
4930                      (t
4931                       (or gnus-sum-thread-tree-single-indent subject))))
4932               (t
4933                (concat (apply 'concat
4934                               (mapcar (lambda (item)
4935                                         (if (= item 1)
4936                                             gnus-sum-thread-tree-vertical
4937                                           gnus-sum-thread-tree-indent))
4938                                       (cdr (reverse tree-stack))))
4939                        (if (nth 1 thread)
4940                            gnus-sum-thread-tree-leaf-with-other
4941                          gnus-sum-thread-tree-single-leaf)))))
4942             (when (string= gnus-tmp-name "")
4943               (setq gnus-tmp-name gnus-tmp-from))
4944             (unless (numberp gnus-tmp-lines)
4945               (setq gnus-tmp-lines -1))
4946             (if (= gnus-tmp-lines -1)
4947                 (setq gnus-tmp-lines "?")
4948               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4949               (gnus-put-text-property
4950              (point)
4951              (progn (eval gnus-summary-line-format-spec) (point))
4952                'gnus-number number)
4953             (when gnus-visual-p
4954               (forward-line -1)
4955               (gnus-run-hooks 'gnus-summary-update-hook)
4956               (forward-line 1))
4957
4958             (setq gnus-tmp-prev-subject simp-subject)))
4959
4960         (when (nth 1 thread)
4961           (push (list (max 0 gnus-tmp-level)
4962                       (copy-sequence tree-stack)
4963                       (nthcdr 1 thread))
4964                 stack))
4965         (push (if (nth 1 thread) 1 0) tree-stack)
4966         (incf gnus-tmp-level)
4967         (setq threads (if thread-end nil (cdar thread)))
4968         (if gnus-summary-display-while-building
4969             (if building-count
4970                 (progn
4971                   ;; use a set frequency
4972                   (setq building-line-count (1- building-line-count))
4973                   (when (= building-line-count 0)
4974                     (sit-for 0)
4975                     (setq building-line-count
4976                           gnus-summary-display-while-building)))
4977               ;; always
4978               (sit-for 0)))
4979         (unless threads
4980           (setq gnus-tmp-level 0)))))
4981   (gnus-message 7 "Generating summary...done"))
4982
4983 (defun gnus-summary-prepare-unthreaded (headers)
4984   "Generate an unthreaded summary buffer based on HEADERS."
4985   (let (header number mark)
4986
4987     (beginning-of-line)
4988
4989     (while headers
4990       ;; We may have to root out some bad articles...
4991       (when (memq (setq number (mail-header-number
4992                                 (setq header (pop headers))))
4993                   gnus-newsgroup-limit)
4994         ;; Mark article as read when it has a low score.
4995         (when (and gnus-summary-mark-below
4996                    (< (or (cdr (assq number gnus-newsgroup-scored))
4997                           gnus-summary-default-score 0)
4998                       gnus-summary-mark-below)
4999                    (not (gnus-summary-article-ancient-p number)))
5000           (setq gnus-newsgroup-unreads
5001                 (delq number gnus-newsgroup-unreads))
5002           (if gnus-newsgroup-auto-expire
5003               (push number gnus-newsgroup-expirable)
5004             (push (cons number gnus-low-score-mark)
5005                   gnus-newsgroup-reads)))
5006
5007         (setq mark (gnus-article-mark number))
5008         (push (gnus-data-make number mark (1+ (point)) header 0)
5009               gnus-newsgroup-data)
5010         (gnus-summary-insert-line
5011          header 0 number
5012          (memq number gnus-newsgroup-undownloaded)
5013          mark (memq number gnus-newsgroup-replied)
5014          (memq number gnus-newsgroup-expirable)
5015          (mail-header-subject header) nil
5016          (cdr (assq number gnus-newsgroup-scored))
5017          (memq number gnus-newsgroup-processable))))))
5018
5019 (defun gnus-summary-remove-list-identifiers ()
5020   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5021   (let ((regexp (if (consp gnus-list-identifiers)
5022                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5023                   gnus-list-identifiers))
5024         changed subject)
5025     (when regexp
5026       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5027       (dolist (header gnus-newsgroup-headers)
5028         (setq subject (mail-header-subject header)
5029               changed nil)
5030         (while (string-match regexp subject)
5031           (setq subject
5032                 (concat (substring subject 0 (match-beginning 1))
5033                         (substring subject (match-end 0)))
5034                 changed t))
5035         (when changed
5036           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5037             (setq subject
5038                   (concat (substring subject 0 (match-beginning 1))
5039                           (substring subject (match-end 1)))))
5040           (mail-header-set-subject header subject))))))
5041
5042 (defun gnus-fetch-headers (articles)
5043   "Fetch headers of ARTICLES."
5044   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5045     (gnus-message 5 "Fetching headers for %s..." name)
5046     (prog1
5047         (if (eq 'nov
5048                 (setq gnus-headers-retrieved-by
5049                       (gnus-retrieve-headers
5050                        articles gnus-newsgroup-name
5051                        ;; We might want to fetch old headers, but
5052                        ;; not if there is only 1 article.
5053                        (and (or (and
5054                                  (not (eq gnus-fetch-old-headers 'some))
5055                                  (not (numberp gnus-fetch-old-headers)))
5056                                 (> (length articles) 1))
5057                             gnus-fetch-old-headers))))
5058             (gnus-get-newsgroup-headers-xover
5059              articles nil nil gnus-newsgroup-name t)
5060           (gnus-get-newsgroup-headers))
5061       (gnus-message 5 "Fetching headers for %s...done" name))))
5062
5063 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5064   "Select newsgroup GROUP.
5065 If READ-ALL is non-nil, all articles in the group are selected.
5066 If SELECT-ARTICLES, only select those articles from GROUP."
5067   (let* ((entry (gnus-group-entry group))
5068          ;;!!! Dirty hack; should be removed.
5069          (gnus-summary-ignore-duplicates
5070           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5071               t
5072             gnus-summary-ignore-duplicates))
5073          (info (nth 2 entry))
5074          articles fetched-articles cached)
5075
5076     (unless (gnus-check-server
5077              (set (make-local-variable 'gnus-current-select-method)
5078                   (gnus-find-method-for-group group)))
5079       (error "Couldn't open server"))
5080
5081     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5082         (gnus-activate-group group)     ; Or we can activate it...
5083         (progn                          ; Or we bug out.
5084           (when (equal major-mode 'gnus-summary-mode)
5085             (gnus-kill-buffer (current-buffer)))
5086           (error "Couldn't activate group %s: %s"
5087                  group (gnus-status-message group))))
5088
5089     (unless (gnus-request-group group t)
5090       (when (equal major-mode 'gnus-summary-mode)
5091         (gnus-kill-buffer (current-buffer)))
5092       (error "Couldn't request group %s: %s"
5093              group (gnus-status-message group)))
5094
5095     (when gnus-agent
5096       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5097       
5098       (setq gnus-summary-use-undownloaded-faces
5099             (gnus-agent-find-parameter
5100              group
5101              'agent-enable-undownloaded-faces)))
5102
5103     (setq gnus-newsgroup-name group
5104           gnus-newsgroup-unselected nil
5105           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5106
5107     (let ((display (gnus-group-find-parameter group 'display)))
5108       (setq gnus-newsgroup-display
5109             (cond
5110              ((not (zerop (or (car-safe read-all) 0)))
5111               ;; The user entered the group with C-u SPC/RET, let's show
5112               ;; all articles.
5113               'gnus-not-ignore)
5114              ((eq display 'all)
5115               'gnus-not-ignore)
5116              ((arrayp display)
5117               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5118              ((numberp display)
5119               ;; The following is probably the "correct" solution, but
5120               ;; it makes Gnus fetch all headers and then limit the
5121               ;; articles (which is slow), so instead we hack the
5122               ;; select-articles parameter instead. -- Simon Josefsson
5123               ;; <jas@kth.se>
5124               ;;
5125               ;; (gnus-byte-compile
5126               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5127               ;;                         display)))))
5128               (setq select-articles
5129                     (gnus-uncompress-range
5130                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5131                              (if (> tmp 0)
5132                                  tmp
5133                                1))
5134                            (cdr (gnus-active group)))))
5135               nil)
5136              (t
5137               nil))))
5138
5139     (gnus-summary-setup-default-charset)
5140
5141     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5142     (when (gnus-virtual-group-p group)
5143       (setq cached gnus-newsgroup-cached))
5144
5145     (setq gnus-newsgroup-unreads
5146           (gnus-sorted-ndifference
5147            (gnus-sorted-ndifference gnus-newsgroup-unreads
5148                                     gnus-newsgroup-marked)
5149            gnus-newsgroup-dormant))
5150
5151     (setq gnus-newsgroup-processable nil)
5152
5153     (gnus-update-read-articles group gnus-newsgroup-unreads)
5154
5155     ;; Adjust and set lists of article marks.
5156     (when info
5157       (gnus-adjust-marked-articles info))
5158     (if (setq articles select-articles)
5159         (setq gnus-newsgroup-unselected
5160               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5161       (setq articles (gnus-articles-to-read group read-all)))
5162
5163     (cond
5164      ((null articles)
5165       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5166       'quit)
5167      ((eq articles 0) nil)
5168      (t
5169       ;; Init the dependencies hash table.
5170       (setq gnus-newsgroup-dependencies
5171             (gnus-make-hashtable (length articles)))
5172       (gnus-set-global-variables)
5173       ;; Retrieve the headers and read them in.
5174
5175       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5176
5177       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5178       (when cached
5179         (setq gnus-newsgroup-cached cached))
5180
5181       ;; Suppress duplicates?
5182       (when gnus-suppress-duplicates
5183         (gnus-dup-suppress-articles))
5184
5185       ;; Set the initial limit.
5186       (setq gnus-newsgroup-limit (copy-sequence articles))
5187       ;; Remove canceled articles from the list of unread articles.
5188       (setq fetched-articles
5189             (mapcar (lambda (headers) (mail-header-number headers))
5190                     gnus-newsgroup-headers))
5191       (setq gnus-newsgroup-articles fetched-articles)
5192       (setq gnus-newsgroup-unreads
5193             (gnus-sorted-nintersection
5194              gnus-newsgroup-unreads fetched-articles))
5195       (gnus-compute-unseen-list)
5196
5197       ;; Removed marked articles that do not exist.
5198       (gnus-update-missing-marks
5199        (gnus-sorted-difference articles fetched-articles))
5200       ;; We might want to build some more threads first.
5201       (when (and gnus-fetch-old-headers
5202                  (eq gnus-headers-retrieved-by 'nov))
5203         (if (eq gnus-fetch-old-headers 'invisible)
5204             (gnus-build-all-threads)
5205           (gnus-build-old-threads)))
5206       ;; Let the Gnus agent mark articles as read.
5207       (when gnus-agent
5208         (gnus-agent-get-undownloaded-list))
5209       ;; Remove list identifiers from subject
5210       (when gnus-list-identifiers
5211         (gnus-summary-remove-list-identifiers))
5212       ;; Check whether auto-expire is to be done in this group.
5213       (setq gnus-newsgroup-auto-expire
5214             (gnus-group-auto-expirable-p group))
5215       ;; Set up the article buffer now, if necessary.
5216       (unless gnus-single-article-buffer
5217         (gnus-article-setup-buffer))
5218       ;; First and last article in this newsgroup.
5219       (when gnus-newsgroup-headers
5220         (setq gnus-newsgroup-begin
5221               (mail-header-number (car gnus-newsgroup-headers))
5222               gnus-newsgroup-end
5223               (mail-header-number
5224                (gnus-last-element gnus-newsgroup-headers))))
5225       ;; GROUP is successfully selected.
5226       (or gnus-newsgroup-headers t)))))
5227
5228 (defun gnus-compute-unseen-list ()
5229   ;; The `seen' marks are treated specially.
5230   (if (not gnus-newsgroup-seen)
5231       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5232     (setq gnus-newsgroup-unseen
5233           (gnus-inverse-list-range-intersection
5234            gnus-newsgroup-articles gnus-newsgroup-seen))))
5235
5236 (defun gnus-summary-display-make-predicate (display)
5237   (require 'gnus-agent)
5238   (when (= (length display) 1)
5239     (setq display (car display)))
5240   (unless gnus-summary-display-cache
5241     (dolist (elem (append '((unread . unread)
5242                             (read . read)
5243                             (unseen . unseen))
5244                           gnus-article-mark-lists))
5245       (push (cons (cdr elem)
5246                   (gnus-byte-compile
5247                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5248             gnus-summary-display-cache)))
5249   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5250         (gnus-category-predicate-cache gnus-summary-display-cache))
5251     (gnus-get-predicate display)))
5252
5253 ;; Uses the dynamically bound `number' variable.
5254 (eval-when-compile
5255   (defvar number))
5256 (defun gnus-article-marked-p (type &optional article)
5257   (let ((article (or article number)))
5258     (cond
5259      ((eq type 'tick)
5260       (memq article gnus-newsgroup-marked))
5261      ((eq type 'spam)
5262       (memq article gnus-newsgroup-spam-marked))
5263      ((eq type 'unsend)
5264       (memq article gnus-newsgroup-unsendable))
5265      ((eq type 'undownload)
5266       (memq article gnus-newsgroup-undownloaded))
5267      ((eq type 'download)
5268       (memq article gnus-newsgroup-downloadable))
5269      ((eq type 'unread)
5270       (memq article gnus-newsgroup-unreads))
5271      ((eq type 'read)
5272       (memq article gnus-newsgroup-reads))
5273      ((eq type 'dormant)
5274       (memq article gnus-newsgroup-dormant) )
5275      ((eq type 'expire)
5276       (memq article gnus-newsgroup-expirable))
5277      ((eq type 'reply)
5278       (memq article gnus-newsgroup-replied))
5279      ((eq type 'killed)
5280       (memq article gnus-newsgroup-killed))
5281      ((eq type 'bookmark)
5282       (assq article gnus-newsgroup-bookmarks))
5283      ((eq type 'score)
5284       (assq article gnus-newsgroup-scored))
5285      ((eq type 'save)
5286       (memq article gnus-newsgroup-saved))
5287      ((eq type 'cache)
5288       (memq article gnus-newsgroup-cached))
5289      ((eq type 'forward)
5290       (memq article gnus-newsgroup-forwarded))
5291      ((eq type 'seen)
5292       (not (memq article gnus-newsgroup-unseen)))
5293      ((eq type 'recent)
5294       (memq article gnus-newsgroup-recent))
5295      (t t))))
5296
5297 (defun gnus-articles-to-read (group &optional read-all)
5298   "Find out what articles the user wants to read."
5299   (let* ((display (gnus-group-find-parameter group 'display))
5300          (articles
5301           ;; Select all articles if `read-all' is non-nil, or if there
5302           ;; are no unread articles.
5303           (if (or read-all
5304                   (and (zerop (length gnus-newsgroup-marked))
5305                        (zerop (length gnus-newsgroup-unreads)))
5306                   ;; Fetch all if the predicate is non-nil.
5307                   gnus-newsgroup-display)
5308               ;; We want to select the headers for all the articles in
5309               ;; the group, so we select either all the active
5310               ;; articles in the group, or (if that's nil), the
5311               ;; articles in the cache.
5312               (or
5313                (gnus-uncompress-range (gnus-active group))
5314                (gnus-cache-articles-in-group group))
5315             ;; Select only the "normal" subset of articles.
5316             (gnus-sorted-nunion
5317              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5318              gnus-newsgroup-unreads)))
5319          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5320          (scored (length scored-list))
5321          (number (length articles))
5322          (marked (+ (length gnus-newsgroup-marked)
5323                     (length gnus-newsgroup-dormant)))
5324          (select
5325           (cond
5326            ((numberp read-all)
5327             read-all)
5328            ((numberp gnus-newsgroup-display)
5329             gnus-newsgroup-display)
5330            (t
5331             (condition-case ()
5332                 (cond
5333                  ((and (or (<= scored marked) (= scored number))
5334                        (numberp gnus-large-newsgroup)
5335                        (> number gnus-large-newsgroup))
5336                   (let* ((cursor-in-echo-area nil)
5337                          (initial (gnus-parameter-large-newsgroup-initial
5338                                    gnus-newsgroup-name))
5339                          (input
5340                           (read-string
5341                            (format
5342                             "How many articles from %s (%s %d): "
5343                             (gnus-limit-string
5344                              (gnus-group-decoded-name gnus-newsgroup-name)
5345                              35)
5346                             (if initial "max" "default")
5347                             number)
5348                            (if initial
5349                                (cons (number-to-string initial)
5350                                      0)))))
5351                     (if (string-match "^[ \t]*$" input) number input)))
5352                  ((and (> scored marked) (< scored number)
5353                        (> (- scored number) 20))
5354                   (let ((input
5355                          (read-string
5356                           (format "%s %s (%d scored, %d total): "
5357                                   "How many articles from"
5358                                   (gnus-group-decoded-name group)
5359                                   scored number))))
5360                     (if (string-match "^[ \t]*$" input)
5361                         number input)))
5362                  (t number))
5363               (quit
5364                (message "Quit getting the articles to read")
5365                nil))))))
5366     (setq select (if (stringp select) (string-to-number select) select))
5367     (if (or (null select) (zerop select))
5368         select
5369       (if (and (not (zerop scored)) (<= (abs select) scored))
5370           (progn
5371             (setq articles (sort scored-list '<))
5372             (setq number (length articles)))
5373         (setq articles (copy-sequence articles)))
5374
5375       (when (< (abs select) number)
5376         (if (< select 0)
5377             ;; Select the N oldest articles.
5378             (setcdr (nthcdr (1- (abs select)) articles) nil)
5379           ;; Select the N most recent articles.
5380           (setq articles (nthcdr (- number select) articles))))
5381       (setq gnus-newsgroup-unselected
5382             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5383       (when gnus-alter-articles-to-read-function
5384         (setq articles
5385               (sort
5386                (funcall gnus-alter-articles-to-read-function
5387                         gnus-newsgroup-name articles)
5388                '<)))
5389       articles)))
5390
5391 (defun gnus-killed-articles (killed articles)
5392   (let (out)
5393     (while articles
5394       (when (inline (gnus-member-of-range (car articles) killed))
5395         (push (car articles) out))
5396       (setq articles (cdr articles)))
5397     out))
5398
5399 (defun gnus-uncompress-marks (marks)
5400   "Uncompress the mark ranges in MARKS."
5401   (let ((uncompressed '(score bookmark))
5402         out)
5403     (while marks
5404       (if (memq (caar marks) uncompressed)
5405           (push (car marks) out)
5406         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5407       (setq marks (cdr marks)))
5408     out))
5409
5410 (defun gnus-article-mark-to-type (mark)
5411   "Return the type of MARK."
5412   (or (cadr (assq mark gnus-article-special-mark-lists))
5413       'list))
5414
5415 (defun gnus-article-unpropagatable-p (mark)
5416   "Return whether MARK should be propagated to back end."
5417   (memq mark gnus-article-unpropagated-mark-lists))
5418
5419 (defun gnus-adjust-marked-articles (info)
5420   "Set all article lists and remove all marks that are no longer valid."
5421   (let* ((marked-lists (gnus-info-marks info))
5422          (active (gnus-active (gnus-info-group info)))
5423          (min (car active))
5424          (max (cdr active))
5425          (types gnus-article-mark-lists)
5426          marks var articles article mark mark-type
5427          bgn end)
5428
5429     (dolist (marks marked-lists)
5430       (setq mark (car marks)
5431             mark-type (gnus-article-mark-to-type mark)
5432             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5433
5434       ;; We set the variable according to the type of the marks list,
5435       ;; and then adjust the marks to a subset of the active articles.
5436       (cond
5437        ;; Adjust "simple" lists - compressed yet unsorted
5438        ((eq mark-type 'list)
5439         ;; Simultaneously uncompress and clip to active range
5440         ;; See gnus-uncompress-range for a description of possible marks
5441         (let (l lh)
5442           (if (not (cadr marks))
5443               (set var nil)
5444             (setq articles (if (numberp (cddr marks))
5445                                (list (cdr marks))
5446                              (cdr marks))
5447                   lh (cons nil nil)
5448                   l lh)
5449
5450             (while (setq article (pop articles))
5451               (cond ((consp article)
5452                      (setq bgn (max (car article) min)
5453                            end (min (cdr article) max))
5454                      (while (<= bgn end)
5455                        (setq l (setcdr l (cons bgn nil))
5456                              bgn (1+ bgn))))
5457                     ((and (<= min article)
5458                           (>= max article))
5459                      (setq l (setcdr l (cons article nil))))))
5460             (set var (cdr lh)))))
5461        ;; Adjust assocs.
5462        ((eq mark-type 'tuple)
5463         (set var (setq articles (cdr marks)))
5464         (when (not (listp (cdr (symbol-value var))))
5465           (set var (list (symbol-value var))))
5466         (when (not (listp (cdr articles)))
5467           (setq articles (list articles)))
5468         (while articles
5469           (when (or (not (consp (setq article (pop articles))))
5470                     (< (car article) min)
5471                     (> (car article) max))
5472             (set var (delq article (symbol-value var))))))
5473        ;; Adjust ranges (sloppily).
5474        ((eq mark-type 'range)
5475         (cond
5476          ((eq mark 'seen)
5477           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5478           ;; It should be (seen (NUM1 . NUM2)).
5479           (when (numberp (cddr marks))
5480             (setcdr marks (list (cdr marks))))
5481           (setq articles (cdr marks))
5482           (while (and articles
5483                       (or (and (consp (car articles))
5484                                (> min (cdar articles)))
5485                           (and (numberp (car articles))
5486                                (> min (car articles)))))
5487             (pop articles))
5488           (set var articles))))))))
5489
5490 (defun gnus-update-missing-marks (missing)
5491   "Go through the list of MISSING articles and remove them from the mark lists."
5492   (when missing
5493     (let (var m)
5494       ;; Go through all types.
5495       (dolist (elem gnus-article-mark-lists)
5496         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5497           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5498           (when (symbol-value var)
5499             ;; This list has articles.  So we delete all missing
5500             ;; articles from it.
5501             (setq m missing)
5502             (while m
5503               (set var (delq (pop m) (symbol-value var))))))))))
5504
5505 (defun gnus-update-marks ()
5506   "Enter the various lists of marked articles into the newsgroup info list."
5507   (let ((types gnus-article-mark-lists)
5508         (info (gnus-get-info gnus-newsgroup-name))
5509         type list newmarked symbol delta-marks)
5510     (when info
5511       ;; Add all marks lists to the list of marks lists.
5512       (while (setq type (pop types))
5513         (setq list (symbol-value
5514                     (setq symbol
5515                           (intern (format "gnus-newsgroup-%s" (car type))))))
5516
5517         (when list
5518           ;; Get rid of the entries of the articles that have the
5519           ;; default score.
5520           (when (and (eq (cdr type) 'score)
5521                      gnus-save-score
5522                      list)
5523             (let* ((arts list)
5524                    (prev (cons nil list))
5525                    (all prev))
5526               (while arts
5527                 (if (or (not (consp (car arts)))
5528                         (= (cdar arts) gnus-summary-default-score))
5529                     (setcdr prev (cdr arts))
5530                   (setq prev arts))
5531                 (setq arts (cdr arts)))
5532               (setq list (cdr all)))))
5533
5534         (when (eq (cdr type) 'seen)
5535           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5536
5537         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5538           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5539
5540         (when (and (gnus-check-backend-function
5541                     'request-set-mark gnus-newsgroup-name)
5542                    (not (gnus-article-unpropagatable-p (cdr type))))
5543           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5544                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5545                  (add (gnus-remove-from-range
5546                        (gnus-copy-sequence list) old)))
5547             (when add
5548               (push (list add 'add (list (cdr type))) delta-marks))
5549             (when del
5550               (push (list del 'del (list (cdr type))) delta-marks))))
5551
5552         (when list
5553           (push (cons (cdr type) list) newmarked)))
5554
5555       (when delta-marks
5556         (unless (gnus-check-group gnus-newsgroup-name)
5557           (error "Can't open server for %s" gnus-newsgroup-name))
5558         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5559
5560       ;; Enter these new marks into the info of the group.
5561       (if (nthcdr 3 info)
5562           (setcar (nthcdr 3 info) newmarked)
5563         ;; Add the marks lists to the end of the info.
5564         (when newmarked
5565           (setcdr (nthcdr 2 info) (list newmarked))))
5566
5567       ;; Cut off the end of the info if there's nothing else there.
5568       (let ((i 5))
5569         (while (and (> i 2)
5570                     (not (nth i info)))
5571           (when (nthcdr (decf i) info)
5572             (setcdr (nthcdr i info) nil)))))))
5573
5574 (defun gnus-set-mode-line (where)
5575   "Set the mode line of the article or summary buffers.
5576 If WHERE is `summary', the summary mode line format will be used."
5577   ;; Is this mode line one we keep updated?
5578   (when (and (memq where gnus-updated-mode-lines)
5579              (symbol-value
5580               (intern (format "gnus-%s-mode-line-format-spec" where))))
5581     (let (mode-string)
5582       (save-excursion
5583         ;; We evaluate this in the summary buffer since these
5584         ;; variables are buffer-local to that buffer.
5585         (set-buffer gnus-summary-buffer)
5586         ;; We bind all these variables that are used in the `eval' form
5587         ;; below.
5588         (let* ((mformat (symbol-value
5589                          (intern
5590                           (format "gnus-%s-mode-line-format-spec" where))))
5591                (gnus-tmp-group-name (gnus-group-decoded-name
5592                                      gnus-newsgroup-name))
5593                (gnus-tmp-article-number (or gnus-current-article 0))
5594                (gnus-tmp-unread gnus-newsgroup-unreads)
5595                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5596                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5597                (gnus-tmp-unread-and-unselected
5598                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5599                             (zerop gnus-tmp-unselected))
5600                        "")
5601                       ((zerop gnus-tmp-unselected)
5602                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5603                       (t (format "{%d(+%d) more}"
5604                                  gnus-tmp-unread-and-unticked
5605                                  gnus-tmp-unselected))))
5606                (gnus-tmp-subject
5607                 (if (and gnus-current-headers
5608                          (vectorp gnus-current-headers))
5609                     (gnus-mode-string-quote
5610                      (mail-header-subject gnus-current-headers))
5611                   ""))
5612                bufname-length max-len
5613                gnus-tmp-header);; passed as argument to any user-format-funcs
5614           (setq mode-string (eval mformat))
5615           (setq bufname-length (if (string-match "%b" mode-string)
5616                                    (- (length
5617                                        (buffer-name
5618                                         (if (eq where 'summary)
5619                                             nil
5620                                           (get-buffer gnus-article-buffer))))
5621                                       2)
5622                                  0))
5623           (setq max-len (max 4 (if gnus-mode-non-string-length
5624                                    (- (window-width)
5625                                       gnus-mode-non-string-length
5626                                       bufname-length)
5627                                  (length mode-string))))
5628           ;; We might have to chop a bit of the string off...
5629           (when (> (length mode-string) max-len)
5630             (setq mode-string
5631                   (concat (gnus-truncate-string mode-string (- max-len 3))
5632                           "...")))
5633           ;; Pad the mode string a bit.
5634           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5635       ;; Update the mode line.
5636       (setq mode-line-buffer-identification
5637             (gnus-mode-line-buffer-identification (list mode-string)))
5638       (set-buffer-modified-p t))))
5639
5640 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5641   "Go through the HEADERS list and add all Xrefs to a hash table.
5642 The resulting hash table is returned, or nil if no Xrefs were found."
5643   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5644          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5645          (xref-hashtb (gnus-make-hashtable))
5646          start group entry number xrefs header)
5647     (while headers
5648       (setq header (pop headers))
5649       (when (and (setq xrefs (mail-header-xref header))
5650                  (not (memq (setq number (mail-header-number header))
5651                             unreads)))
5652         (setq start 0)
5653         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5654           (setq start (match-end 0))
5655           (setq group (if prefix
5656                           (concat prefix (substring xrefs (match-beginning 1)
5657                                                     (match-end 1)))
5658                         (substring xrefs (match-beginning 1) (match-end 1))))
5659           (setq number
5660                 (string-to-int (substring xrefs (match-beginning 2)
5661                                           (match-end 2))))
5662           (if (setq entry (gnus-gethash group xref-hashtb))
5663               (setcdr entry (cons number (cdr entry)))
5664             (gnus-sethash group (cons number nil) xref-hashtb)))))
5665     (and start xref-hashtb)))
5666
5667 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5668   "Look through all the headers and mark the Xrefs as read."
5669   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5670         name info xref-hashtb idlist method nth4)
5671     (save-excursion
5672       (set-buffer gnus-group-buffer)
5673       (when (setq xref-hashtb
5674                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5675         (mapatoms
5676          (lambda (group)
5677            (unless (string= from-newsgroup (setq name (symbol-name group)))
5678              (setq idlist (symbol-value group))
5679              ;; Dead groups are not updated.
5680              (and (prog1
5681                       (setq info (gnus-get-info name))
5682                     (when (stringp (setq nth4 (gnus-info-method info)))
5683                       (setq nth4 (gnus-server-to-method nth4))))
5684                   ;; Only do the xrefs if the group has the same
5685                   ;; select method as the group we have just read.
5686                   (or (gnus-methods-equal-p
5687                        nth4 (gnus-find-method-for-group from-newsgroup))
5688                       virtual
5689                       (equal nth4 (setq method (gnus-find-method-for-group
5690                                                 from-newsgroup)))
5691                       (and (equal (car nth4) (car method))
5692                            (equal (nth 1 nth4) (nth 1 method))))
5693                   gnus-use-cross-reference
5694                   (or (not (eq gnus-use-cross-reference t))
5695                       virtual
5696                       ;; Only do cross-references on subscribed
5697                       ;; groups, if that is what is wanted.
5698                       (<= (gnus-info-level info) gnus-level-subscribed))
5699                   (gnus-group-make-articles-read name idlist))))
5700          xref-hashtb)))))
5701
5702 (defun gnus-compute-read-articles (group articles)
5703   (let* ((entry (gnus-group-entry group))
5704          (info (nth 2 entry))
5705          (active (gnus-active group))
5706          ninfo)
5707     (when entry
5708       ;; First peel off all invalid article numbers.
5709       (when active
5710         (let ((ids articles)
5711               id first)
5712           (while (setq id (pop ids))
5713             (when (and first (> id (cdr active)))
5714               ;; We'll end up in this situation in one particular
5715               ;; obscure situation.  If you re-scan a group and get
5716               ;; a new article that is cross-posted to a different
5717               ;; group that has not been re-scanned, you might get
5718               ;; crossposted article that has a higher number than
5719               ;; Gnus believes possible.  So we re-activate this
5720               ;; group as well.  This might mean doing the
5721               ;; crossposting thingy will *increase* the number
5722               ;; of articles in some groups.  Tsk, tsk.
5723               (setq active (or (gnus-activate-group group) active)))
5724             (when (or (> id (cdr active))
5725                       (< id (car active)))
5726               (setq articles (delq id articles))))))
5727       ;; If the read list is nil, we init it.
5728       (if (and active
5729                (null (gnus-info-read info))
5730                (> (car active) 1))
5731           (setq ninfo (cons 1 (1- (car active))))
5732         (setq ninfo (gnus-info-read info)))
5733       ;; Then we add the read articles to the range.
5734       (gnus-add-to-range
5735        ninfo (setq articles (sort articles '<))))))
5736
5737 (defun gnus-group-make-articles-read (group articles)
5738   "Update the info of GROUP to say that ARTICLES are read."
5739   (let* ((num 0)
5740          (entry (gnus-group-entry group))
5741          (info (nth 2 entry))
5742          (active (gnus-active group))
5743          range)
5744     (when entry
5745       (setq range (gnus-compute-read-articles group articles))
5746       (with-current-buffer gnus-group-buffer
5747         (gnus-undo-register
5748           `(progn
5749              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5750              (gnus-info-set-read ',info ',(gnus-info-read info))
5751              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5752              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5753              (gnus-group-update-group ,group t))))
5754       ;; Add the read articles to the range.
5755       (gnus-info-set-read info range)
5756       (gnus-request-set-mark group (list (list range 'add '(read))))
5757       ;; Then we have to re-compute how many unread
5758       ;; articles there are in this group.
5759       (when active
5760         (cond
5761          ((not range)
5762           (setq num (- (1+ (cdr active)) (car active))))
5763          ((not (listp (cdr range)))
5764           (setq num (- (cdr active) (- (1+ (cdr range))
5765                                        (car range)))))
5766          (t
5767           (while range
5768             (if (numberp (car range))
5769                 (setq num (1+ num))
5770               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5771             (setq range (cdr range)))
5772           (setq num (- (cdr active) num))))
5773         ;; Update the number of unread articles.
5774         (setcar entry num)
5775         ;; Update the group buffer.
5776         (unless (gnus-ephemeral-group-p group)
5777           (gnus-group-update-group group t))))))
5778
5779 (defvar gnus-newsgroup-none-id 0)
5780
5781 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5782   (let ((cur nntp-server-buffer)
5783         (dependencies
5784          (or dependencies
5785              (save-excursion (set-buffer gnus-summary-buffer)
5786                              gnus-newsgroup-dependencies)))
5787         headers id end ref
5788         (mail-parse-charset gnus-newsgroup-charset)
5789         (mail-parse-ignored-charsets
5790          (save-excursion (condition-case nil
5791                              (set-buffer gnus-summary-buffer)
5792                            (error))
5793                          gnus-newsgroup-ignored-charsets)))
5794     (save-excursion
5795       (set-buffer nntp-server-buffer)
5796       ;; Translate all TAB characters into SPACE characters.
5797       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5798       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5799       (ietf-drums-unfold-fws)
5800       (gnus-run-hooks 'gnus-parse-headers-hook)
5801       (let ((case-fold-search t)
5802             in-reply-to header p lines chars ctype)
5803         (goto-char (point-min))
5804         ;; Search to the beginning of the next header.  Error messages
5805         ;; do not begin with 2 or 3.
5806         (while (re-search-forward "^[23][0-9]+ " nil t)
5807           (setq id nil
5808                 ref nil)
5809           ;; This implementation of this function, with nine
5810           ;; search-forwards instead of the one re-search-forward and
5811           ;; a case (which basically was the old function) is actually
5812           ;; about twice as fast, even though it looks messier.  You
5813           ;; can't have everything, I guess.  Speed and elegance
5814           ;; doesn't always go hand in hand.
5815           (setq
5816            header
5817            (make-full-mail-header
5818             ;; Number.
5819             (prog1
5820                 (read cur)
5821               (end-of-line)
5822               (setq p (point))
5823               (narrow-to-region (point)
5824                                 (or (and (search-forward "\n.\n" nil t)
5825                                          (- (point) 2))
5826                                     (point))))
5827             ;; Subject.
5828             (progn
5829               (goto-char p)
5830               (if (search-forward "\nsubject:" nil t)
5831                   (nnheader-header-value)
5832                 "(none)"))
5833             ;; From.
5834             (progn
5835               (goto-char p)
5836               (if (search-forward "\nfrom:" nil t)
5837                   (nnheader-header-value)
5838                 "(nobody)"))
5839             ;; Date.
5840             (progn
5841               (goto-char p)
5842               (if (search-forward "\ndate:" nil t)
5843                   (nnheader-header-value) ""))
5844             ;; Message-ID.
5845             (progn
5846               (goto-char p)
5847               (setq id (if (re-search-forward
5848                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5849                            ;; We do it this way to make sure the Message-ID
5850                            ;; is (somewhat) syntactically valid.
5851                            (buffer-substring (match-beginning 1)
5852                                              (match-end 1))
5853                          ;; If there was no message-id, we just fake one
5854                          ;; to make subsequent routines simpler.
5855                          (nnheader-generate-fake-message-id))))
5856             ;; References.
5857             (progn
5858               (goto-char p)
5859               (if (search-forward "\nreferences:" nil t)
5860                   (progn
5861                     (setq end (point))
5862                     (prog1
5863                         (nnheader-header-value)
5864                       (setq ref
5865                             (buffer-substring
5866                              (progn
5867                                ;; (end-of-line)
5868                                (search-backward ">" end t)
5869                                (1+ (point)))
5870                              (progn
5871                                (search-backward "<" end t)
5872                                (point))))))
5873                 ;; Get the references from the in-reply-to header if there
5874                 ;; were no references and the in-reply-to header looks
5875                 ;; promising.
5876                 (if (and (search-forward "\nin-reply-to:" nil t)
5877                          (setq in-reply-to (nnheader-header-value))
5878                          (string-match "<[^>]+>" in-reply-to))
5879                     (let (ref2)
5880                       (setq ref (substring in-reply-to (match-beginning 0)
5881                                            (match-end 0)))
5882                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5883                         (setq ref2 (substring in-reply-to (match-beginning 0)
5884                                               (match-end 0)))
5885                         (when (> (length ref2) (length ref))
5886                           (setq ref ref2)))
5887                       ref)
5888                   (setq ref nil))))
5889             ;; Chars.
5890             (progn
5891               (goto-char p)
5892               (if (search-forward "\nchars: " nil t)
5893                   (if (numberp (setq chars (ignore-errors (read cur))))
5894                       chars -1)
5895                 -1))
5896             ;; Lines.
5897             (progn
5898               (goto-char p)
5899               (if (search-forward "\nlines: " nil t)
5900                   (if (numberp (setq lines (ignore-errors (read cur))))
5901                       lines -1)
5902                 -1))
5903             ;; Xref.
5904             (progn
5905               (goto-char p)
5906               (and (search-forward "\nxref:" nil t)
5907                    (nnheader-header-value)))
5908             ;; Extra.
5909             (when gnus-extra-headers
5910               (let ((extra gnus-extra-headers)
5911                     out)
5912                 (while extra
5913                   (goto-char p)
5914                   (when (search-forward
5915                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5916                     (push (cons (car extra) (nnheader-header-value)) out))
5917                   (pop extra))
5918                 out))))
5919           (goto-char p)
5920           (if (and (search-forward "\ncontent-type: " nil t)
5921                    (setq ctype (nnheader-header-value)))
5922               (mime-entity-set-content-type-internal
5923                header (mime-parse-Content-Type ctype)))
5924           (when (equal id ref)
5925             (setq ref nil))
5926
5927           (when gnus-alter-header-function
5928             (funcall gnus-alter-header-function header)
5929             (setq id (mail-header-id header)
5930                   ref (gnus-parent-id (mail-header-references header))))
5931
5932           (when (setq header
5933                       (gnus-dependencies-add-header
5934                        header dependencies force-new))
5935             (push header headers))
5936           (goto-char (point-max))
5937           (widen))
5938         (nreverse headers)))))
5939
5940 ;; Goes through the xover lines and returns a list of vectors
5941 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5942                                                   force-new dependencies
5943                                                   group also-fetch-heads)
5944   "Parse the news overview data in the server buffer.
5945 Return a list of headers that match SEQUENCE (see
5946 `nntp-retrieve-headers')."
5947   ;; Get the Xref when the users reads the articles since most/some
5948   ;; NNTP servers do not include Xrefs when using XOVER.
5949   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5950   (let ((mail-parse-charset gnus-newsgroup-charset)
5951         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5952         (cur nntp-server-buffer)
5953         (dependencies (or dependencies gnus-newsgroup-dependencies))
5954         (allp (cond
5955                ((eq gnus-read-all-available-headers t)
5956                 t)
5957                ((stringp gnus-read-all-available-headers)
5958                 (string-match gnus-read-all-available-headers group))
5959                (t
5960                 nil)))
5961         number headers header)
5962     (save-excursion
5963       (set-buffer nntp-server-buffer)
5964       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5965       ;; Allow the user to mangle the headers before parsing them.
5966       (gnus-run-hooks 'gnus-parse-headers-hook)
5967       (goto-char (point-min))
5968       (gnus-parse-without-error
5969         (while (and (or sequence allp)
5970                     (not (eobp)))
5971           (setq number (read cur))
5972           (when (not allp)
5973             (while (and sequence
5974                         (< (car sequence) number))
5975               (setq sequence (cdr sequence))))
5976           (when (and (or allp
5977                          (and sequence
5978                               (eq number (car sequence))))
5979                      (progn
5980                        (setq sequence (cdr sequence))
5981                        (setq header (inline
5982                                       (gnus-nov-parse-line
5983                                        number dependencies force-new)))))
5984             (push header headers))
5985           (forward-line 1)))
5986       ;; A common bug in inn is that if you have posted an article and
5987       ;; then retrieves the active file, it will answer correctly --
5988       ;; the new article is included.  However, a NOV entry for the
5989       ;; article may not have been generated yet, so this may fail.
5990       ;; We work around this problem by retrieving the last few
5991       ;; headers using HEAD.
5992       (if (or (not also-fetch-heads)
5993               (not sequence))
5994           ;; We (probably) got all the headers.
5995           (nreverse headers)
5996         (let ((gnus-nov-is-evil t))
5997           (nconc
5998            (nreverse headers)
5999            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6000              (gnus-get-newsgroup-headers))))))))
6001
6002 (defun gnus-article-get-xrefs ()
6003   "Fill in the Xref value in `gnus-current-headers', if necessary.
6004 This is meant to be called in `gnus-article-internal-prepare-hook'."
6005   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
6006                                  gnus-current-headers)))
6007     (or (not gnus-use-cross-reference)
6008         (not headers)
6009         (and (mail-header-xref headers)
6010              (not (string= (mail-header-xref headers) "")))
6011         (let ((case-fold-search t)
6012               xref)
6013           (save-restriction
6014             (nnheader-narrow-to-headers)
6015             (goto-char (point-min))
6016             (when (or (and (not (eobp))
6017                            (eq (downcase (char-after)) ?x)
6018                            (looking-at "Xref:"))
6019                       (search-forward "\nXref:" nil t))
6020               (goto-char (1+ (match-end 0)))
6021               (setq xref (buffer-substring (point) (point-at-eol)))
6022               (mail-header-set-xref headers xref)))))))
6023
6024 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6025   "Find article ID and insert the summary line for that article.
6026 OLD-HEADER can either be a header or a line number to insert
6027 the subject line on."
6028   (let* ((line (and (numberp old-header) old-header))
6029          (old-header (and (vectorp old-header) old-header))
6030          (header (cond ((and old-header use-old-header)
6031                         old-header)
6032                        ((and (numberp id)
6033                              (gnus-number-to-header id))
6034                         (gnus-number-to-header id))
6035                        (t
6036                         (gnus-read-header id))))
6037          (number (and (numberp id) id))
6038          d)
6039     (when header
6040       ;; Rebuild the thread that this article is part of and go to the
6041       ;; article we have fetched.
6042       (when (and (not gnus-show-threads)
6043                  old-header)
6044         (when (and number
6045                    (setq d (gnus-data-find (mail-header-number old-header))))
6046           (goto-char (gnus-data-pos d))
6047           (gnus-data-remove
6048            number
6049            (- (point-at-bol)
6050               (prog1
6051                   (1+ (point-at-eol))
6052                 (gnus-delete-line))))))
6053       (when old-header
6054         (mail-header-set-number header (mail-header-number old-header)))
6055       (setq gnus-newsgroup-sparse
6056             (delq (setq number (mail-header-number header))
6057                   gnus-newsgroup-sparse))
6058       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6059       (push number gnus-newsgroup-limit)
6060       (gnus-rebuild-thread (mail-header-id header) line)
6061       (gnus-summary-goto-subject number nil t))
6062     (when (and (numberp number)
6063                (> number 0))
6064       ;; We have to update the boundaries even if we can't fetch the
6065       ;; article if ID is a number -- so that the next `P' or `N'
6066       ;; command will fetch the previous (or next) article even
6067       ;; if the one we tried to fetch this time has been canceled.
6068       (when (> number gnus-newsgroup-end)
6069         (setq gnus-newsgroup-end number))
6070       (when (< number gnus-newsgroup-begin)
6071         (setq gnus-newsgroup-begin number))
6072       (setq gnus-newsgroup-unselected
6073             (delq number gnus-newsgroup-unselected)))
6074     ;; Report back a success?
6075     (and header (mail-header-number header))))
6076
6077 ;;; Process/prefix in the summary buffer
6078
6079 (defun gnus-summary-work-articles (n)
6080   "Return a list of articles to be worked upon.
6081 The prefix argument, the list of process marked articles, and the
6082 current article will be taken into consideration."
6083   (save-excursion
6084     (set-buffer gnus-summary-buffer)
6085     (cond
6086      (n
6087       ;; A numerical prefix has been given.
6088       (setq n (prefix-numeric-value n))
6089       (let ((backward (< n 0))
6090             (n (abs (prefix-numeric-value n)))
6091             articles article)
6092         (save-excursion
6093           (while
6094               (and (> n 0)
6095                    (push (setq article (gnus-summary-article-number))
6096                          articles)
6097                    (if backward
6098                        (gnus-summary-find-prev nil article)
6099                      (gnus-summary-find-next nil article)))
6100             (decf n)))
6101         (nreverse articles)))
6102      ((and (gnus-region-active-p) (mark))
6103       (message "region active")
6104       ;; Work on the region between point and mark.
6105       (let ((max (max (point) (mark)))
6106             articles article)
6107         (save-excursion
6108           (goto-char (min (point) (mark)))
6109           (while
6110               (and
6111                (push (setq article (gnus-summary-article-number)) articles)
6112                (gnus-summary-find-next nil article)
6113                (< (point) max)))
6114           (nreverse articles))))
6115      (gnus-newsgroup-processable
6116       ;; There are process-marked articles present.
6117       ;; Save current state.
6118       (gnus-summary-save-process-mark)
6119       ;; Return the list.
6120       (reverse gnus-newsgroup-processable))
6121      (t
6122       ;; Just return the current article.
6123       (list (gnus-summary-article-number))))))
6124
6125 (defmacro gnus-summary-iterate (arg &rest forms)
6126   "Iterate over the process/prefixed articles and do FORMS.
6127 ARG is the interactive prefix given to the command.  FORMS will be
6128 executed with point over the summary line of the articles."
6129   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6130     `(let ((,articles (gnus-summary-work-articles ,arg)))
6131        (while ,articles
6132          (gnus-summary-goto-subject (car ,articles))
6133          ,@forms
6134          (pop ,articles)))))
6135
6136 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6137 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6138
6139 (defun gnus-summary-save-process-mark ()
6140   "Push the current set of process marked articles on the stack."
6141   (interactive)
6142   (push (copy-sequence gnus-newsgroup-processable)
6143         gnus-newsgroup-process-stack))
6144
6145 (defun gnus-summary-kill-process-mark ()
6146   "Push the current set of process marked articles on the stack and unmark."
6147   (interactive)
6148   (gnus-summary-save-process-mark)
6149   (gnus-summary-unmark-all-processable))
6150
6151 (defun gnus-summary-yank-process-mark ()
6152   "Pop the last process mark state off the stack and restore it."
6153   (interactive)
6154   (unless gnus-newsgroup-process-stack
6155     (error "Empty mark stack"))
6156   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6157
6158 (defun gnus-summary-process-mark-set (set)
6159   "Make SET into the current process marked articles."
6160   (gnus-summary-unmark-all-processable)
6161   (while set
6162     (gnus-summary-set-process-mark (pop set))))
6163
6164 ;;; Searching and stuff
6165
6166 (defun gnus-summary-search-group (&optional backward use-level)
6167   "Search for next unread newsgroup.
6168 If optional argument BACKWARD is non-nil, search backward instead."
6169   (save-excursion
6170     (set-buffer gnus-group-buffer)
6171     (when (gnus-group-search-forward
6172            backward nil (if use-level (gnus-group-group-level) nil))
6173       (gnus-group-group-name))))
6174
6175 (defun gnus-summary-best-group (&optional exclude-group)
6176   "Find the name of the best unread group.
6177 If EXCLUDE-GROUP, do not go to this group."
6178   (save-excursion
6179     (set-buffer gnus-group-buffer)
6180     (save-excursion
6181       (gnus-group-best-unread-group exclude-group))))
6182
6183 (defun gnus-summary-find-next (&optional unread article backward)
6184   (if backward
6185       (gnus-summary-find-prev unread article)
6186     (let* ((dummy (gnus-summary-article-intangible-p))
6187            (article (or article (gnus-summary-article-number)))
6188            (data (gnus-data-find-list article))
6189            result)
6190       (when (and (not dummy)
6191                  (or (not gnus-summary-check-current)
6192                      (not unread)
6193                      (not (gnus-data-unread-p (car data)))))
6194         (setq data (cdr data)))
6195       (when (setq result
6196                   (if unread
6197                       (progn
6198                         (while data
6199                           (unless (memq (gnus-data-number (car data))
6200                                         (cond
6201                                          ((eq gnus-auto-goto-ignores
6202                                               'always-undownloaded)
6203                                           gnus-newsgroup-undownloaded)
6204                                          (gnus-plugged
6205                                           nil)
6206                                          ((eq gnus-auto-goto-ignores
6207                                               'unfetched)
6208                                           gnus-newsgroup-unfetched)
6209                                          ((eq gnus-auto-goto-ignores
6210                                               'undownloaded)
6211                                           gnus-newsgroup-undownloaded)))
6212                             (when (gnus-data-unread-p (car data))
6213                               (setq result (car data)
6214                                     data nil)))
6215                           (setq data (cdr data)))
6216                         result)
6217                     (car data)))
6218         (goto-char (gnus-data-pos result))
6219         (gnus-data-number result)))))
6220
6221 (defun gnus-summary-find-prev (&optional unread article)
6222   (let* ((eobp (eobp))
6223          (article (or article (gnus-summary-article-number)))
6224          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6225          result)
6226     (when (and (not eobp)
6227                (or (not gnus-summary-check-current)
6228                    (not unread)
6229                    (not (gnus-data-unread-p (car data)))))
6230       (setq data (cdr data)))
6231     (when (setq result
6232                 (if unread
6233                     (progn
6234                       (while data
6235                         (unless (memq (gnus-data-number (car data))
6236                                       (cond
6237                                        ((eq gnus-auto-goto-ignores
6238                                             'always-undownloaded)
6239                                         gnus-newsgroup-undownloaded)
6240                                        (gnus-plugged
6241                                         nil)
6242                                        ((eq gnus-auto-goto-ignores
6243                                             'unfetched)
6244                                         gnus-newsgroup-unfetched)
6245                                        ((eq gnus-auto-goto-ignores
6246                                             'undownloaded)
6247                                         gnus-newsgroup-undownloaded)))
6248                           (when (gnus-data-unread-p (car data))
6249                             (setq result (car data)
6250                                   data nil)))
6251                         (setq data (cdr data)))
6252                       result)
6253                   (car data)))
6254       (goto-char (gnus-data-pos result))
6255       (gnus-data-number result))))
6256
6257 (defun gnus-summary-find-subject (subject &optional unread backward article)
6258   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6259          (article (or article (gnus-summary-article-number)))
6260          (articles (gnus-data-list backward))
6261          (arts (gnus-data-find-list article articles))
6262          result)
6263     (when (or (not gnus-summary-check-current)
6264               (not unread)
6265               (not (gnus-data-unread-p (car arts))))
6266       (setq arts (cdr arts)))
6267     (while arts
6268       (and (or (not unread)
6269                (gnus-data-unread-p (car arts)))
6270            (vectorp (gnus-data-header (car arts)))
6271            (gnus-subject-equal
6272             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6273            (setq result (car arts)
6274                  arts nil))
6275       (setq arts (cdr arts)))
6276     (and result
6277          (goto-char (gnus-data-pos result))
6278          (gnus-data-number result))))
6279
6280 (defun gnus-summary-search-forward (&optional unread subject backward)
6281   "Search forward for an article.
6282 If UNREAD, look for unread articles.  If SUBJECT, look for
6283 articles with that subject.  If BACKWARD, search backward instead."
6284   (cond (subject (gnus-summary-find-subject subject unread backward))
6285         (backward (gnus-summary-find-prev unread))
6286         (t (gnus-summary-find-next unread))))
6287
6288 (defun gnus-recenter (&optional n)
6289   "Center point in window and redisplay frame.
6290 Also do horizontal recentering."
6291   (interactive "P")
6292   (when (and gnus-auto-center-summary
6293              (not (eq gnus-auto-center-summary 'vertical)))
6294     (gnus-horizontal-recenter))
6295   (recenter n))
6296
6297 (defun gnus-summary-recenter ()
6298   "Center point in the summary window.
6299 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6300 displayed, no centering will be performed."
6301   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6302   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6303   (interactive)
6304   ;; The user has to want it.
6305   (when gnus-auto-center-summary
6306     (let* ((top (cond ((< (window-height) 4) 0)
6307                       ((< (window-height) 7) 1)
6308                       (t (if (numberp gnus-auto-center-summary)
6309                              gnus-auto-center-summary
6310                            2))))
6311            (height (1- (window-height)))
6312            (bottom (save-excursion (goto-char (point-max))
6313                                    (forward-line (- height))
6314                                    (point)))
6315            (window (get-buffer-window (current-buffer))))
6316       (when (get-buffer-window gnus-article-buffer)
6317         ;; Only do recentering when the article buffer is displayed,
6318         ;; Set the window start to either `bottom', which is the biggest
6319         ;; possible valid number, or the second line from the top,
6320         ;; whichever is the least.
6321         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6322           (if (> bottom top-pos)
6323               ;; Keep the second line from the top visible
6324               (set-window-start window top-pos t)
6325             ;; Try to keep the bottom line visible; if it's partially
6326             ;; obscured, either scroll one more line to make it fully
6327             ;; visible, or revert to using TOP-POS.
6328             (save-excursion
6329               (goto-char (point-max))
6330               (forward-line -1)
6331               (let ((last-line-start (point)))
6332                 (goto-char bottom)
6333                 (set-window-start window (point) t)
6334                 (when (not (pos-visible-in-window-p last-line-start window))
6335                   (forward-line 1)
6336                   (set-window-start window (min (point) top-pos) t)))))))
6337       ;; Do horizontal recentering while we're at it.
6338       (when (and (get-buffer-window (current-buffer) t)
6339                  (not (eq gnus-auto-center-summary 'vertical)))
6340         (let ((selected (selected-window)))
6341           (select-window (get-buffer-window (current-buffer) t))
6342           (gnus-summary-position-point)
6343           (gnus-horizontal-recenter)
6344           (select-window selected))))))
6345
6346 (defun gnus-summary-jump-to-group (newsgroup)
6347   "Move point to NEWSGROUP in group mode buffer."
6348   ;; Keep update point of group mode buffer if visible.
6349   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6350       (save-window-excursion
6351         ;; Take care of tree window mode.
6352         (when (get-buffer-window gnus-group-buffer)
6353           (pop-to-buffer gnus-group-buffer))
6354         (gnus-group-jump-to-group newsgroup))
6355     (save-excursion
6356       ;; Take care of tree window mode.
6357       (if (get-buffer-window gnus-group-buffer)
6358           (pop-to-buffer gnus-group-buffer)
6359         (set-buffer gnus-group-buffer))
6360       (gnus-group-jump-to-group newsgroup))))
6361
6362 ;; This function returns a list of article numbers based on the
6363 ;; difference between the ranges of read articles in this group and
6364 ;; the range of active articles.
6365 (defun gnus-list-of-unread-articles (group)
6366   (let* ((read (gnus-info-read (gnus-get-info group)))
6367          (active (or (gnus-active group) (gnus-activate-group group)))
6368          (last (cdr active))
6369          first nlast unread)
6370     ;; If none are read, then all are unread.
6371     (if (not read)
6372         (setq first (car active))
6373       ;; If the range of read articles is a single range, then the
6374       ;; first unread article is the article after the last read
6375       ;; article.  Sounds logical, doesn't it?
6376       (if (and (not (listp (cdr read)))
6377                (or (< (car read) (car active))
6378                    (progn (setq read (list read))
6379                           nil)))
6380           (setq first (max (car active) (1+ (cdr read))))
6381         ;; `read' is a list of ranges.
6382         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6383                                   (caar read)))
6384                   1)
6385           (setq first (car active)))
6386         (while read
6387           (when first
6388             (while (< first nlast)
6389               (setq unread (cons first unread)
6390                     first (1+ first))))
6391           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6392           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6393           (setq read (cdr read)))))
6394     ;; And add the last unread articles.
6395     (while (<= first last)
6396       (setq unread (cons first unread)
6397             first (1+ first)))
6398     ;; Return the list of unread articles.
6399     (delq 0 (nreverse unread))))
6400
6401 (defun gnus-list-of-read-articles (group)
6402   "Return a list of unread, unticked and non-dormant articles."
6403   (let* ((info (gnus-get-info group))
6404          (marked (gnus-info-marks info))
6405          (active (gnus-active group)))
6406     (and info active
6407          (gnus-list-range-difference
6408           (gnus-list-range-difference
6409            (gnus-sorted-complement
6410             (gnus-uncompress-range active)
6411             (gnus-list-of-unread-articles group))
6412            (cdr (assq 'dormant marked)))
6413           (cdr (assq 'tick marked))))))
6414
6415 ;; This function returns a sequence of article numbers based on the
6416 ;; difference between the ranges of read articles in this group and
6417 ;; the range of active articles.
6418 (defun gnus-sequence-of-unread-articles (group)
6419   (let* ((read (gnus-info-read (gnus-get-info group)))
6420          (active (or (gnus-active group) (gnus-activate-group group)))
6421          (last (cdr active))
6422          first nlast unread)
6423     ;; If none are read, then all are unread.
6424     (if (not read)
6425         (setq first (car active))
6426       ;; If the range of read articles is a single range, then the
6427       ;; first unread article is the article after the last read
6428       ;; article.  Sounds logical, doesn't it?
6429       (if (and (not (listp (cdr read)))
6430                (or (< (car read) (car active))
6431                    (progn (setq read (list read))
6432                           nil)))
6433           (setq first (max (car active) (1+ (cdr read))))
6434         ;; `read' is a list of ranges.
6435         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6436                                   (caar read)))
6437                   1)
6438           (setq first (car active)))
6439         (while read
6440           (when first
6441             (push (cons first nlast) unread))
6442           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6443           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6444           (setq read (cdr read)))))
6445     ;; And add the last unread articles.
6446     (cond ((< first last)
6447            (push (cons first last) unread))
6448           ((= first last)
6449            (push first unread)))
6450     ;; Return the sequence of unread articles.
6451     (delq 0 (nreverse unread))))
6452
6453 ;; Various summary commands
6454
6455 (defun gnus-summary-select-article-buffer ()
6456   "Reconfigure windows to show article buffer."
6457   (interactive)
6458   (if (not (gnus-buffer-live-p gnus-article-buffer))
6459       (error "There is no article buffer for this summary buffer")
6460     (gnus-configure-windows 'article)
6461     (select-window (get-buffer-window gnus-article-buffer))))
6462
6463 (defun gnus-summary-universal-argument (arg)
6464   "Perform any operation on all articles that are process/prefixed."
6465   (interactive "P")
6466   (let ((articles (gnus-summary-work-articles arg))
6467         func article)
6468     (if (eq
6469          (setq
6470           func
6471           (key-binding
6472            (read-key-sequence
6473             (substitute-command-keys
6474              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6475          'undefined)
6476         (gnus-error 1 "Undefined key")
6477       (save-excursion
6478         (while articles
6479           (gnus-summary-goto-subject (setq article (pop articles)))
6480           (let (gnus-newsgroup-processable)
6481             (command-execute func))
6482           (gnus-summary-remove-process-mark article)))))
6483   (gnus-summary-position-point))
6484
6485 (defun gnus-summary-toggle-truncation (&optional arg)
6486   "Toggle truncation of summary lines.
6487 With ARG, turn line truncation on if ARG is positive."
6488   (interactive "P")
6489   (setq truncate-lines
6490         (if (null arg) (not truncate-lines)
6491           (> (prefix-numeric-value arg) 0)))
6492   (redraw-display))
6493
6494 (defun gnus-summary-find-for-reselect ()
6495   "Return the number of an article to stay on across a reselect.
6496 The current article is considered, then following articles, then previous
6497 articles.  An article is sought which is not cancelled and isn't a temporary
6498 insertion from another group.  If there's no such then return a dummy 0."
6499   (let (found)
6500     (dolist (rev '(nil t))
6501       (unless found      ; don't demand the reverse list if we don't need it
6502         (let ((data (gnus-data-find-list
6503                      (gnus-summary-article-number) (gnus-data-list rev))))
6504           (while (and data (not found))
6505             (if (and (< 0 (gnus-data-number (car data)))
6506                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6507                 (setq found (gnus-data-number (car data))))
6508             (setq data (cdr data))))))
6509     (or found 0)))
6510
6511 (defun gnus-summary-reselect-current-group (&optional all rescan)
6512   "Exit and then reselect the current newsgroup.
6513 The prefix argument ALL means to select all articles."
6514   (interactive "P")
6515   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6516     (error "Ephemeral groups can't be reselected"))
6517   (let ((current-subject (gnus-summary-find-for-reselect))
6518         (group gnus-newsgroup-name))
6519     (setq gnus-newsgroup-begin nil)
6520     (gnus-summary-exit nil 'leave-hidden)
6521     ;; We have to adjust the point of group mode buffer because
6522     ;; point was moved to the next unread newsgroup by exiting.
6523     (gnus-summary-jump-to-group group)
6524     (when rescan
6525       (save-excursion
6526         (save-window-excursion
6527           ;; Don't show group contents.
6528           (set-window-start (selected-window) (point-max))
6529           (gnus-group-get-new-news-this-group 1))))
6530     (gnus-group-read-group all t)
6531     (gnus-summary-goto-subject current-subject nil t)))
6532
6533 (defun gnus-summary-rescan-group (&optional all)
6534   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6535   (interactive "P")
6536   (gnus-summary-reselect-current-group all t))
6537
6538 (defun gnus-summary-update-info (&optional non-destructive)
6539   (save-excursion
6540     (let ((group gnus-newsgroup-name))
6541       (when group
6542         (when gnus-newsgroup-kill-headers
6543           (setq gnus-newsgroup-killed
6544                 (gnus-compress-sequence
6545                  (gnus-sorted-union
6546                   (gnus-list-range-intersection
6547                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6548                   gnus-newsgroup-unreads)
6549                  t)))
6550         (unless (listp (cdr gnus-newsgroup-killed))
6551           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6552         (let ((headers gnus-newsgroup-headers))
6553           ;; Set the new ranges of read articles.
6554           (save-excursion
6555             (set-buffer gnus-group-buffer)
6556             (gnus-undo-force-boundary))
6557           (gnus-update-read-articles
6558            group (gnus-sorted-union
6559                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6560           ;; Set the current article marks.
6561           (let ((gnus-newsgroup-scored
6562                  (if (and (not gnus-save-score)
6563                           (not non-destructive))
6564                      nil
6565                    gnus-newsgroup-scored)))
6566             (save-excursion
6567               (gnus-update-marks)))
6568           ;; Do the cross-ref thing.
6569           (when gnus-use-cross-reference
6570             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6571           ;; Do not switch windows but change the buffer to work.
6572           (set-buffer gnus-group-buffer)
6573           (unless (gnus-ephemeral-group-p group)
6574             (gnus-group-update-group group)))))))
6575
6576 (defun gnus-summary-save-newsrc (&optional force)
6577   "Save the current number of read/marked articles in the dribble buffer.
6578 The dribble buffer will then be saved.
6579 If FORCE (the prefix), also save the .newsrc file(s)."
6580   (interactive "P")
6581   (gnus-summary-update-info t)
6582   (if force
6583       (gnus-save-newsrc-file)
6584     (gnus-dribble-save)))
6585
6586 (defun gnus-summary-exit (&optional temporary leave-hidden)
6587   "Exit reading current newsgroup, and then return to group selection mode.
6588 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6589   (interactive)
6590   (gnus-set-global-variables)
6591   (gnus-kill-save-kill-buffer)
6592   (gnus-async-halt-prefetch)
6593   (let* ((group gnus-newsgroup-name)
6594          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6595          (gnus-group-is-exiting-p t)
6596          (mode major-mode)
6597          (group-point nil)
6598          (buf (current-buffer)))
6599     (unless quit-config
6600       ;; Do adaptive scoring, and possibly save score files.
6601       (when gnus-newsgroup-adaptive
6602         (gnus-score-adaptive))
6603       (when gnus-use-scoring
6604         (gnus-score-save)))
6605     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6606     ;; If we have several article buffers, we kill them at exit.
6607     (unless gnus-single-article-buffer
6608       (gnus-kill-buffer gnus-original-article-buffer)
6609       (setq gnus-article-current nil))
6610     (when gnus-use-cache
6611       (gnus-cache-possibly-remove-articles)
6612       (gnus-cache-save-buffers))
6613     (gnus-async-prefetch-remove-group group)
6614     (when gnus-suppress-duplicates
6615       (gnus-dup-enter-articles))
6616     (when gnus-use-trees
6617       (gnus-tree-close group))
6618     (when gnus-use-cache
6619       (gnus-cache-write-active))
6620     ;; Remove entries for this group.
6621     (nnmail-purge-split-history (gnus-group-real-name group))
6622     ;; Make all changes in this group permanent.
6623     (unless quit-config
6624       (gnus-run-hooks 'gnus-exit-group-hook)
6625       (gnus-summary-update-info))
6626     (gnus-close-group group)
6627     ;; Make sure where we were, and go to next newsgroup.
6628     (set-buffer gnus-group-buffer)
6629     (unless quit-config
6630       (gnus-group-jump-to-group group))
6631     (gnus-run-hooks 'gnus-summary-exit-hook)
6632     (unless (or quit-config
6633                 ;; If this group has disappeared from the summary
6634                 ;; buffer, don't skip forwards.
6635                 (not (string= group (gnus-group-group-name))))
6636       (gnus-group-next-unread-group 1))
6637     (setq group-point (point))
6638     (if temporary
6639         nil                             ;Nothing to do.
6640       ;; If we have several article buffers, we kill them at exit.
6641       (unless gnus-single-article-buffer
6642         (gnus-kill-buffer gnus-article-buffer)
6643         (gnus-kill-buffer gnus-original-article-buffer)
6644         (setq gnus-article-current nil))
6645       (set-buffer buf)
6646       (if (not gnus-kill-summary-on-exit)
6647           (progn
6648             (gnus-deaden-summary)
6649             (setq mode nil))
6650         ;; We set all buffer-local variables to nil.  It is unclear why
6651         ;; this is needed, but if we don't, buffer-local variables are
6652         ;; not garbage-collected, it seems.  This would the lead to en
6653         ;; ever-growing Emacs.
6654         (gnus-summary-clear-local-variables)
6655         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6656           (gnus-summary-clear-local-variables))
6657         (when (get-buffer gnus-article-buffer)
6658           (bury-buffer gnus-article-buffer))
6659         ;; We clear the global counterparts of the buffer-local
6660         ;; variables as well, just to be on the safe side.
6661         (set-buffer gnus-group-buffer)
6662         (gnus-summary-clear-local-variables)
6663         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6664           (gnus-summary-clear-local-variables))
6665         ;; Return to group mode buffer.
6666         (when (eq mode 'gnus-summary-mode)
6667           (gnus-kill-buffer buf)))
6668       (setq gnus-current-select-method gnus-select-method)
6669       (if leave-hidden
6670           (set-buffer gnus-group-buffer)
6671         (pop-to-buffer gnus-group-buffer))
6672       (if (not quit-config)
6673           (progn
6674             (goto-char group-point)
6675             (unless leave-hidden
6676               (gnus-configure-windows 'group 'force))
6677             (unless (pos-visible-in-window-p)
6678               (forward-line (/ (static-if (featurep 'xemacs)
6679                                    (window-displayed-height)
6680                                  (1- (window-height)))
6681                                -2))
6682               (set-window-start (selected-window) (point))
6683               (goto-char group-point)))
6684         (gnus-handle-ephemeral-exit quit-config))
6685       ;; Clear the current group name.
6686       (unless quit-config
6687         (setq gnus-newsgroup-name nil)))))
6688
6689 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6690 (defun gnus-summary-exit-no-update (&optional no-questions)
6691   "Quit reading current newsgroup without updating read article info."
6692   (interactive)
6693   (let* ((group gnus-newsgroup-name)
6694          (gnus-group-is-exiting-p t)
6695          (gnus-group-is-exiting-without-update-p t)
6696          (quit-config (gnus-group-quit-config group)))
6697     (when (or no-questions
6698               gnus-expert-user
6699               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6700       (gnus-async-halt-prefetch)
6701       (run-hooks 'gnus-summary-prepare-exit-hook)
6702       ;; If we have several article buffers, we kill them at exit.
6703       (unless gnus-single-article-buffer
6704         (gnus-kill-buffer gnus-article-buffer)
6705         (gnus-kill-buffer gnus-original-article-buffer)
6706         (setq gnus-article-current nil))
6707       (if (not gnus-kill-summary-on-exit)
6708           (gnus-deaden-summary)
6709         (gnus-close-group group)
6710         (gnus-summary-clear-local-variables)
6711         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6712           (gnus-summary-clear-local-variables))
6713         (set-buffer gnus-group-buffer)
6714         (gnus-summary-clear-local-variables)
6715         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6716           (gnus-summary-clear-local-variables))
6717         (gnus-kill-buffer gnus-summary-buffer))
6718       (unless gnus-single-article-buffer
6719         (setq gnus-article-current nil))
6720       (when gnus-use-trees
6721         (gnus-tree-close group))
6722       (gnus-async-prefetch-remove-group group)
6723       (when (get-buffer gnus-article-buffer)
6724         (bury-buffer gnus-article-buffer))
6725       ;; Return to the group buffer.
6726       (gnus-configure-windows 'group 'force)
6727       ;; Clear the current group name.
6728       (setq gnus-newsgroup-name nil)
6729       (unless (gnus-ephemeral-group-p group)
6730         (gnus-group-update-group group))
6731       (when (equal (gnus-group-group-name) group)
6732         (gnus-group-next-unread-group 1))
6733       (when quit-config
6734         (gnus-handle-ephemeral-exit quit-config)))))
6735
6736 (defun gnus-handle-ephemeral-exit (quit-config)
6737   "Handle movement when leaving an ephemeral group.
6738 The state which existed when entering the ephemeral is reset."
6739   (if (not (buffer-name (car quit-config)))
6740       (gnus-configure-windows 'group 'force)
6741     (set-buffer (car quit-config))
6742     (cond ((eq major-mode 'gnus-summary-mode)
6743            (gnus-set-global-variables))
6744           ((eq major-mode 'gnus-article-mode)
6745            (save-excursion
6746              ;; The `gnus-summary-buffer' variable may point
6747              ;; to the old summary buffer when using a single
6748              ;; article buffer.
6749              (unless (gnus-buffer-live-p gnus-summary-buffer)
6750                (set-buffer gnus-group-buffer))
6751              (set-buffer gnus-summary-buffer)
6752              (gnus-set-global-variables))))
6753     (if (or (eq (cdr quit-config) 'article)
6754             (eq (cdr quit-config) 'pick))
6755         (progn
6756           ;; The current article may be from the ephemeral group
6757           ;; thus it is best that we reload this article
6758           ;;
6759           ;; If we're exiting from a large digest, this can be
6760           ;; extremely slow.  So, it's better not to reload it. -- jh.
6761           ;;(gnus-summary-show-article)
6762           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6763               (gnus-configure-windows 'pick 'force)
6764             (gnus-configure-windows (cdr quit-config) 'force)))
6765       (gnus-configure-windows (cdr quit-config) 'force))
6766     (when (eq major-mode 'gnus-summary-mode)
6767       (gnus-summary-next-subject 1 nil t)
6768       (gnus-summary-recenter)
6769       (gnus-summary-position-point))))
6770
6771 (defun gnus-summary-preview-mime-message ()
6772   "MIME decode and play this message."
6773   (interactive)
6774   (let ((gnus-break-pages nil)
6775         (gnus-show-mime t))
6776     (gnus-summary-select-article gnus-show-all-headers t))
6777   (let ((w (get-buffer-window gnus-article-buffer)))
6778     (when w
6779       (select-window (get-buffer-window gnus-article-buffer)))))
6780
6781 ;;; Dead summaries.
6782
6783 (defvar gnus-dead-summary-mode-map nil)
6784
6785 (unless gnus-dead-summary-mode-map
6786   (setq gnus-dead-summary-mode-map (make-keymap))
6787   (suppress-keymap gnus-dead-summary-mode-map)
6788   (substitute-key-definition
6789    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6790   (dolist (key '("\C-d" "\r" "\177" [delete]))
6791     (define-key gnus-dead-summary-mode-map
6792       key 'gnus-summary-wake-up-the-dead))
6793   (dolist (key '("q" "Q"))
6794     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6795
6796 (defvar gnus-dead-summary-mode nil
6797   "Minor mode for Gnus summary buffers.")
6798
6799 (defun gnus-dead-summary-mode (&optional arg)
6800   "Minor mode for Gnus summary buffers."
6801   (interactive "P")
6802   (when (eq major-mode 'gnus-summary-mode)
6803     (make-local-variable 'gnus-dead-summary-mode)
6804     (setq gnus-dead-summary-mode
6805           (if (null arg) (not gnus-dead-summary-mode)
6806             (> (prefix-numeric-value arg) 0)))
6807     (when gnus-dead-summary-mode
6808       (add-minor-mode
6809        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6810
6811 (defun gnus-deaden-summary ()
6812   "Make the current summary buffer into a dead summary buffer."
6813   ;; Kill any previous dead summary buffer.
6814   (when (and gnus-dead-summary
6815              (buffer-name gnus-dead-summary))
6816     (save-excursion
6817       (set-buffer gnus-dead-summary)
6818       (when gnus-dead-summary-mode
6819         (kill-buffer (current-buffer)))))
6820   ;; Make this the current dead summary.
6821   (setq gnus-dead-summary (current-buffer))
6822   (gnus-dead-summary-mode 1)
6823   (let ((name (buffer-name)))
6824     (when (string-match "Summary" name)
6825       (rename-buffer
6826        (concat (substring name 0 (match-beginning 0)) "Dead "
6827                (substring name (match-beginning 0)))
6828        t)
6829       (bury-buffer))))
6830
6831 (defun gnus-kill-or-deaden-summary (buffer)
6832   "Kill or deaden the summary BUFFER."
6833   (save-excursion
6834     (when (and (buffer-name buffer)
6835                (not gnus-single-article-buffer))
6836       (save-excursion
6837         (set-buffer buffer)
6838         (gnus-kill-buffer gnus-article-buffer)
6839         (gnus-kill-buffer gnus-original-article-buffer)))
6840     (cond
6841      ;; Kill the buffer.
6842      (gnus-kill-summary-on-exit
6843       (when (and gnus-use-trees
6844                  (gnus-buffer-exists-p buffer))
6845         (save-excursion
6846           (set-buffer buffer)
6847           (gnus-tree-close gnus-newsgroup-name)))
6848       (gnus-kill-buffer buffer))
6849      ;; Deaden the buffer.
6850      ((gnus-buffer-exists-p buffer)
6851       (save-excursion
6852         (set-buffer buffer)
6853         (gnus-deaden-summary))))))
6854
6855 (defun gnus-summary-wake-up-the-dead (&rest args)
6856   "Wake up the dead summary buffer."
6857   (interactive)
6858   (gnus-dead-summary-mode -1)
6859   (let ((name (buffer-name)))
6860     (when (string-match "Dead " name)
6861       (rename-buffer
6862        (concat (substring name 0 (match-beginning 0))
6863                (substring name (match-end 0)))
6864        t)))
6865   (gnus-message 3 "This dead summary is now alive again"))
6866
6867 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6868 (defun gnus-summary-fetch-faq (&optional faq-dir)
6869   "Fetch the FAQ for the current group.
6870 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6871 in."
6872   (interactive
6873    (list
6874     (when current-prefix-arg
6875       (completing-read
6876        "FAQ dir: " (and (listp gnus-group-faq-directory)
6877                         (mapcar (lambda (file) (list file))
6878                                 gnus-group-faq-directory))))))
6879   (let (gnus-faq-buffer)
6880     (when (setq gnus-faq-buffer
6881                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6882       (gnus-configure-windows 'summary-faq))))
6883
6884 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6885 (defun gnus-summary-describe-group (&optional force)
6886   "Describe the current newsgroup."
6887   (interactive "P")
6888   (gnus-group-describe-group force gnus-newsgroup-name))
6889
6890 (defun gnus-summary-describe-briefly ()
6891   "Describe summary mode commands briefly."
6892   (interactive)
6893   (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")))
6894
6895 ;; Walking around group mode buffer from summary mode.
6896
6897 (defun gnus-summary-next-group (&optional no-article target-group backward)
6898   "Exit current newsgroup and then select next unread newsgroup.
6899 If prefix argument NO-ARTICLE is non-nil, no article is selected
6900 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6901 previous group instead."
6902   (interactive "P")
6903   ;; Stop pre-fetching.
6904   (gnus-async-halt-prefetch)
6905   (let ((current-group gnus-newsgroup-name)
6906         (current-buffer (current-buffer))
6907         entered)
6908     ;; First we semi-exit this group to update Xrefs and all variables.
6909     ;; We can't do a real exit, because the window conf must remain
6910     ;; the same in case the user is prompted for info, and we don't
6911     ;; want the window conf to change before that...
6912     (gnus-summary-exit t)
6913     (while (not entered)
6914       ;; Then we find what group we are supposed to enter.
6915       (set-buffer gnus-group-buffer)
6916       (gnus-group-jump-to-group current-group)
6917       (setq target-group
6918             (or target-group
6919                 (if (eq gnus-keep-same-level 'best)
6920                     (gnus-summary-best-group gnus-newsgroup-name)
6921                   (gnus-summary-search-group backward gnus-keep-same-level))))
6922       (if (not target-group)
6923           ;; There are no further groups, so we return to the group
6924           ;; buffer.
6925           (progn
6926             (gnus-message 5 "Returning to the group buffer")
6927             (setq entered t)
6928             (when (gnus-buffer-live-p current-buffer)
6929               (set-buffer current-buffer)
6930               (gnus-summary-exit))
6931             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6932         ;; We try to enter the target group.
6933         (gnus-group-jump-to-group target-group)
6934         (let ((unreads (gnus-group-group-unread)))
6935           (if (and (or (eq t unreads)
6936                        (and unreads (not (zerop unreads))))
6937                    (gnus-summary-read-group
6938                     target-group nil no-article
6939                     (and (buffer-name current-buffer) current-buffer)
6940                     nil backward))
6941               (setq entered t)
6942             (setq current-group target-group
6943                   target-group nil)))))))
6944
6945 (defun gnus-summary-prev-group (&optional no-article)
6946   "Exit current newsgroup and then select previous unread newsgroup.
6947 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6948   (interactive "P")
6949   (gnus-summary-next-group no-article nil t))
6950
6951 ;; Walking around summary lines.
6952
6953 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6954   "Go to the first subject satisfying any non-nil constraint.
6955 If UNREAD is non-nil, the article should be unread.
6956 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6957 If UNSEEN is non-nil, the article should be unseen.
6958 Returns the article selected or nil if there are no matching articles."
6959   (interactive "P")
6960   (cond
6961    ;; Empty summary.
6962    ((null gnus-newsgroup-data)
6963     (gnus-message 3 "No articles in the group")
6964     nil)
6965    ;; Pick the first article.
6966    ((not (or unread undownloaded unseen))
6967     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6968     (gnus-data-number (car gnus-newsgroup-data)))
6969    ;; Find the first unread article.
6970    (t
6971     (let ((data gnus-newsgroup-data))
6972       (while (and data
6973                   (let ((num (gnus-data-number (car data))))
6974                     (or (memq num gnus-newsgroup-unfetched)
6975                         (not (or (and unread
6976                                       (memq num gnus-newsgroup-unreads))
6977                                  (and undownloaded
6978                                       (memq num gnus-newsgroup-undownloaded))
6979                                  (and unseen
6980                                       (memq num gnus-newsgroup-unseen)))))))
6981         (setq data (cdr data)))
6982       (prog1
6983           (if data
6984               (progn
6985                 (goto-char (gnus-data-pos (car data)))
6986                 (gnus-data-number (car data)))
6987             (gnus-message 3 "No more%s articles"
6988                           (let* ((r (when unread " unread"))
6989                                  (d (when undownloaded " undownloaded"))
6990                                  (s (when unseen " unseen"))
6991                                  (l (delq nil (list r d s))))
6992                             (cond ((= 3 (length l))
6993                                    (concat r "," d ", or" s))
6994                                   ((= 2 (length l))
6995                                    (concat (car l) ", or" (cadr l)))
6996                                   ((= 1 (length l))
6997                                    (car l))
6998                                   (t
6999                                    ""))))
7000             nil
7001             )
7002         (gnus-summary-position-point))))))
7003
7004 (defun gnus-summary-next-subject (n &optional unread dont-display)
7005   "Go to next N'th summary line.
7006 If N is negative, go to the previous N'th subject line.
7007 If UNREAD is non-nil, only unread articles are selected.
7008 The difference between N and the actual number of steps taken is
7009 returned."
7010   (interactive "p")
7011   (let ((backward (< n 0))
7012         (n (abs n)))
7013     (while (and (> n 0)
7014                 (if backward
7015                     (gnus-summary-find-prev unread)
7016                   (gnus-summary-find-next unread)))
7017       (unless (zerop (setq n (1- n)))
7018         (gnus-summary-show-thread)))
7019     (when (/= 0 n)
7020       (gnus-message 7 "No more%s articles"
7021                     (if unread " unread" "")))
7022     (unless dont-display
7023       (gnus-summary-recenter)
7024       (gnus-summary-position-point))
7025     n))
7026
7027 (defun gnus-summary-next-unread-subject (n)
7028   "Go to next N'th unread summary line."
7029   (interactive "p")
7030   (gnus-summary-next-subject n t))
7031
7032 (defun gnus-summary-prev-subject (n &optional unread)
7033   "Go to previous N'th summary line.
7034 If optional argument UNREAD is non-nil, only unread article is selected."
7035   (interactive "p")
7036   (gnus-summary-next-subject (- n) unread))
7037
7038 (defun gnus-summary-prev-unread-subject (n)
7039   "Go to previous N'th unread summary line."
7040   (interactive "p")
7041   (gnus-summary-next-subject (- n) t))
7042
7043 (defun gnus-summary-goto-subjects (articles)
7044   "Insert the subject header for ARTICLES in the current buffer."
7045   (save-excursion
7046     (dolist (article articles)
7047       (gnus-summary-goto-subject article t)))
7048   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7049   (gnus-summary-position-point))
7050  
7051 (defun gnus-summary-goto-subject (article &optional force silent)
7052   "Go the subject line of ARTICLE.
7053 If FORCE, also allow jumping to articles not currently shown."
7054   (interactive "nArticle number: ")
7055   (unless (numberp article)
7056     (error "Article %s is not a number" article))
7057   (let ((b (point))
7058         (data (gnus-data-find article)))
7059     ;; We read in the article if we have to.
7060     (and (not data)
7061          force
7062          (gnus-summary-insert-subject
7063           article
7064           (if (or (numberp force) (vectorp force)) force)
7065           t)
7066          (setq data (gnus-data-find article)))
7067     (goto-char b)
7068     (if (not data)
7069         (progn
7070           (unless silent
7071             (gnus-message 3 "Can't find article %d" article))
7072           nil)
7073       (let ((pt (gnus-data-pos data)))
7074         (goto-char pt)
7075         (gnus-summary-set-article-display-arrow pt))
7076       (gnus-summary-position-point)
7077       article)))
7078
7079 ;; Walking around summary lines with displaying articles.
7080
7081 (defun gnus-summary-expand-window (&optional arg)
7082   "Make the summary buffer take up the entire Emacs frame.
7083 Given a prefix, will force an `article' buffer configuration."
7084   (interactive "P")
7085   (if arg
7086       (gnus-configure-windows 'article 'force)
7087     (gnus-configure-windows 'summary 'force)))
7088
7089 (defun gnus-summary-display-article (article &optional all-header)
7090   "Display ARTICLE in article buffer."
7091   (when (gnus-buffer-live-p gnus-article-buffer)
7092     (with-current-buffer gnus-article-buffer
7093       (set-buffer-multibyte t)))
7094   (gnus-set-global-variables)
7095   (when (gnus-buffer-live-p gnus-article-buffer)
7096     (with-current-buffer gnus-article-buffer
7097       (setq gnus-article-charset gnus-newsgroup-charset)
7098       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7099       (set-buffer-multibyte t)))
7100   (if (null article)
7101       nil
7102     (prog1
7103         (if gnus-summary-display-article-function
7104             (funcall gnus-summary-display-article-function article all-header)
7105           (gnus-article-prepare article all-header))
7106       (with-current-buffer gnus-article-buffer
7107         (set (make-local-variable 'gnus-summary-search-article-matched-data)
7108              nil))
7109       (gnus-run-hooks 'gnus-select-article-hook)
7110       (when (and gnus-current-article
7111                  (not (zerop gnus-current-article)))
7112         (gnus-summary-goto-subject gnus-current-article))
7113       (gnus-summary-recenter)
7114       (when (and gnus-use-trees gnus-show-threads)
7115         (gnus-possibly-generate-tree article)
7116         (gnus-highlight-selected-tree article))
7117       ;; Successfully display article.
7118       (gnus-article-set-window-start
7119        (cdr (assq article gnus-newsgroup-bookmarks))))))
7120
7121 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7122   "Select the current article.
7123 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7124 non-nil, the article will be re-fetched even if it already present in
7125 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7126 be displayed."
7127   ;; Make sure we are in the summary buffer to work around bbdb bug.
7128   (unless (eq major-mode 'gnus-summary-mode)
7129     (set-buffer gnus-summary-buffer))
7130   (let ((article (or article (gnus-summary-article-number)))
7131         (all-headers (not (not all-headers))) ;Must be T or NIL.
7132         gnus-summary-display-article-function)
7133     (and (not pseudo)
7134          (gnus-summary-article-pseudo-p article)
7135          (error "This is a pseudo-article"))
7136     (save-excursion
7137       (set-buffer gnus-summary-buffer)
7138       (if (or (and gnus-single-article-buffer
7139                    (or (null gnus-current-article)
7140                        (null gnus-article-current)
7141                        (null (get-buffer gnus-article-buffer))
7142                        (not (eq article (cdr gnus-article-current)))
7143                        (not (equal (car gnus-article-current)
7144                                    gnus-newsgroup-name))))
7145               (and (not gnus-single-article-buffer)
7146                    (or (null gnus-current-article)
7147                        (not (eq gnus-current-article article))))
7148               force)
7149           ;; The requested article is different from the current article.
7150           (progn
7151             (gnus-summary-display-article article all-headers)
7152             (gnus-article-set-window-start
7153              (cdr (assq article gnus-newsgroup-bookmarks)))
7154             article)
7155         'old))))
7156
7157 (defun gnus-summary-force-verify-and-decrypt ()
7158   "Display buttons for signed/encrypted parts and verify/decrypt them."
7159   (interactive)
7160   (let ((mm-verify-option 'known)
7161         (mm-decrypt-option 'known)
7162         (gnus-article-emulate-mime t)
7163         (gnus-buttonized-mime-types (append (list "multipart/signed"
7164                                                   "multipart/encrypted")
7165                                             gnus-buttonized-mime-types)))
7166     (gnus-summary-select-article nil 'force)))
7167
7168 (defun gnus-summary-set-current-mark (&optional current-mark)
7169   "Obsolete function."
7170   nil)
7171
7172 (defun gnus-summary-next-article (&optional unread subject backward push)
7173   "Select the next article.
7174 If UNREAD, only unread articles are selected.
7175 If SUBJECT, only articles with SUBJECT are selected.
7176 If BACKWARD, the previous article is selected instead of the next."
7177   (interactive "P")
7178   (cond
7179    ;; Is there such an article?
7180    ((and (gnus-summary-search-forward unread subject backward)
7181          (or (gnus-summary-display-article (gnus-summary-article-number))
7182              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7183     (gnus-summary-position-point))
7184    ;; If not, we try the first unread, if that is wanted.
7185    ((and subject
7186          gnus-auto-select-same
7187          (gnus-summary-first-unread-article))
7188     (gnus-summary-position-point)
7189     (gnus-message 6 "Wrapped"))
7190    ;; Try to get next/previous article not displayed in this group.
7191    ((and gnus-auto-extend-newsgroup
7192          (not unread) (not subject))
7193     (gnus-summary-goto-article
7194      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7195      nil (count-lines (point-min) (point))))
7196    ;; Go to next/previous group.
7197    (t
7198     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7199       (gnus-summary-jump-to-group gnus-newsgroup-name))
7200     (let ((cmd last-command-char)
7201           (point
7202            (save-excursion
7203              (set-buffer gnus-group-buffer)
7204              (point)))
7205           (group
7206            (if (eq gnus-keep-same-level 'best)
7207                (gnus-summary-best-group gnus-newsgroup-name)
7208              (gnus-summary-search-group backward gnus-keep-same-level))))
7209       ;; For some reason, the group window gets selected.  We change
7210       ;; it back.
7211       (select-window (get-buffer-window (current-buffer)))
7212       ;; Select next unread newsgroup automagically.
7213       (cond
7214        ((or (not gnus-auto-select-next)
7215             (not cmd))
7216         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7217        ((or (eq gnus-auto-select-next 'quietly)
7218             (and (eq gnus-auto-select-next 'slightly-quietly)
7219                  push)
7220             (and (eq gnus-auto-select-next 'almost-quietly)
7221                  (gnus-summary-last-article-p)))
7222         ;; Select quietly.
7223         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7224             (gnus-summary-exit)
7225           (gnus-message 7 "No more%s articles (%s)..."
7226                         (if unread " unread" "")
7227                         (if group (concat "selecting " group)
7228                           "exiting"))
7229           (gnus-summary-next-group nil group backward)))
7230        (t
7231         (when (gnus-key-press-event-p last-input-event)
7232           (gnus-summary-walk-group-buffer
7233            gnus-newsgroup-name cmd unread backward point))))))))
7234
7235 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7236   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7237                       (?\C-p (gnus-group-prev-unread-group 1))))
7238         (cursor-in-echo-area t)
7239         keve key group ended prompt)
7240     (save-excursion
7241       (set-buffer gnus-group-buffer)
7242       (goto-char start)
7243       (setq group
7244             (if (eq gnus-keep-same-level 'best)
7245                 (gnus-summary-best-group gnus-newsgroup-name)
7246               (gnus-summary-search-group backward gnus-keep-same-level))))
7247     (while (not ended)
7248       (setq prompt
7249             (format
7250              "No more%s articles%s " (if unread " unread" "")
7251              (if (and group
7252                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7253                  (format " (Type %s for %s [%s])"
7254                          (single-key-description cmd) group
7255                          (gnus-group-unread group))
7256                (format " (Type %s to exit %s)"
7257                        (single-key-description cmd)
7258                        gnus-newsgroup-name))))
7259       ;; Confirm auto selection.
7260       (setq key (car (setq keve (gnus-read-event-char prompt)))
7261             ended t)
7262       (cond
7263        ((assq key keystrokes)
7264         (let ((obuf (current-buffer)))
7265           (switch-to-buffer gnus-group-buffer)
7266           (when group
7267             (gnus-group-jump-to-group group))
7268           (eval (cadr (assq key keystrokes)))
7269           (setq group (gnus-group-group-name))
7270           (switch-to-buffer obuf))
7271         (setq ended nil))
7272        ((equal key cmd)
7273         (if (or (not group)
7274                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7275             (gnus-summary-exit)
7276           (gnus-summary-next-group nil group backward)))
7277        (t
7278         (push (cdr keve) unread-command-events))))))
7279
7280 (defun gnus-summary-next-unread-article ()
7281   "Select unread article after current one."
7282   (interactive)
7283   (gnus-summary-next-article
7284    (or (not (eq gnus-summary-goto-unread 'never))
7285        (gnus-summary-last-article-p (gnus-summary-article-number)))
7286    (and gnus-auto-select-same
7287         (gnus-summary-article-subject))))
7288
7289 (defun gnus-summary-prev-article (&optional unread subject)
7290   "Select the article after the current one.
7291 If UNREAD is non-nil, only unread articles are selected."
7292   (interactive "P")
7293   (gnus-summary-next-article unread subject t))
7294
7295 (defun gnus-summary-prev-unread-article ()
7296   "Select unread article before current one."
7297   (interactive)
7298   (gnus-summary-prev-article
7299    (or (not (eq gnus-summary-goto-unread 'never))
7300        (gnus-summary-first-article-p (gnus-summary-article-number)))
7301    (and gnus-auto-select-same
7302         (gnus-summary-article-subject))))
7303
7304 (defun gnus-summary-next-page (&optional lines circular stop)
7305   "Show next page of the selected article.
7306 If at the end of the current article, select the next article.
7307 LINES says how many lines should be scrolled up.
7308
7309 If CIRCULAR is non-nil, go to the start of the article instead of
7310 selecting the next article when reaching the end of the current
7311 article.
7312
7313 If STOP is non-nil, just stop when reaching the end of the message.
7314
7315 Also see the variable `gnus-article-skip-boring'."
7316   (interactive "P")
7317   (setq gnus-summary-buffer (current-buffer))
7318   (gnus-set-global-variables)
7319   (let ((article (gnus-summary-article-number))
7320         (article-window (get-buffer-window gnus-article-buffer t))
7321         endp)
7322     ;; If the buffer is empty, we have no article.
7323     (unless article
7324       (error "No article to select"))
7325     (gnus-configure-windows 'article)
7326     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7327         (if (and (eq gnus-summary-goto-unread 'never)
7328                  (not (gnus-summary-last-article-p article)))
7329             (gnus-summary-next-article)
7330           (gnus-summary-next-unread-article))
7331       (if (or (null gnus-current-article)
7332               (null gnus-article-current)
7333               (/= article (cdr gnus-article-current))
7334               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7335           ;; Selected subject is different from current article's.
7336           (gnus-summary-display-article article)
7337         (when article-window
7338           (gnus-eval-in-buffer-window gnus-article-buffer
7339             (setq endp (or (gnus-article-next-page lines)
7340                            (gnus-article-only-boring-p))))
7341           (when endp
7342             (cond (stop
7343                    (gnus-message 3 "End of message"))
7344                   (circular
7345                    (gnus-summary-beginning-of-article))
7346                   (lines
7347                    (gnus-message 3 "End of message"))
7348                   ((null lines)
7349                    (if (and (eq gnus-summary-goto-unread 'never)
7350                             (not (gnus-summary-last-article-p article)))
7351                        (gnus-summary-next-article)
7352                      (gnus-summary-next-unread-article))))))))
7353     (gnus-summary-recenter)
7354     (gnus-summary-position-point)))
7355
7356 (defun gnus-summary-prev-page (&optional lines move)
7357   "Show previous page of selected article.
7358 Argument LINES specifies lines to be scrolled down.
7359 If MOVE, move to the previous unread article if point is at
7360 the beginning of the buffer."
7361   (interactive "P")
7362   (let ((article (gnus-summary-article-number))
7363         (article-window (get-buffer-window gnus-article-buffer t))
7364         endp)
7365     (gnus-configure-windows 'article)
7366     (if (or (null gnus-current-article)
7367             (null gnus-article-current)
7368             (/= article (cdr gnus-article-current))
7369             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7370         ;; Selected subject is different from current article's.
7371         (gnus-summary-display-article article)
7372       (gnus-summary-recenter)
7373       (when article-window
7374         (gnus-eval-in-buffer-window gnus-article-buffer
7375           (setq endp (gnus-article-prev-page lines)))
7376         (when (and move endp)
7377           (cond (lines
7378                  (gnus-message 3 "Beginning of message"))
7379                 ((null lines)
7380                  (if (and (eq gnus-summary-goto-unread 'never)
7381                           (not (gnus-summary-first-article-p article)))
7382                      (gnus-summary-prev-article)
7383                    (gnus-summary-prev-unread-article))))))))
7384   (gnus-summary-position-point))
7385
7386 (defun gnus-summary-prev-page-or-article (&optional lines)
7387   "Show previous page of selected article.
7388 Argument LINES specifies lines to be scrolled down.
7389 If at the beginning of the article, go to the next article."
7390   (interactive "P")
7391   (gnus-summary-prev-page lines t))
7392
7393 (defun gnus-summary-scroll-up (lines)
7394   "Scroll up (or down) one line current article.
7395 Argument LINES specifies lines to be scrolled up (or down if negative)."
7396   (interactive "p")
7397   (gnus-configure-windows 'article)
7398   (gnus-summary-show-thread)
7399   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7400     (gnus-eval-in-buffer-window gnus-article-buffer
7401       (cond ((> lines 0)
7402              (when (gnus-article-next-page lines)
7403                (gnus-message 3 "End of message")))
7404             ((< lines 0)
7405              (gnus-article-prev-page (- lines))))))
7406   (gnus-summary-recenter)
7407   (gnus-summary-position-point))
7408
7409 (defun gnus-summary-scroll-down (lines)
7410   "Scroll down (or up) one line current article.
7411 Argument LINES specifies lines to be scrolled down (or up if negative)."
7412   (interactive "p")
7413   (gnus-summary-scroll-up (- lines)))
7414
7415 (defun gnus-summary-next-same-subject ()
7416   "Select next article which has the same subject as current one."
7417   (interactive)
7418   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7419
7420 (defun gnus-summary-prev-same-subject ()
7421   "Select previous article which has the same subject as current one."
7422   (interactive)
7423   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7424
7425 (defun gnus-summary-next-unread-same-subject ()
7426   "Select next unread article which has the same subject as current one."
7427   (interactive)
7428   (gnus-summary-next-article t (gnus-summary-article-subject)))
7429
7430 (defun gnus-summary-prev-unread-same-subject ()
7431   "Select previous unread article which has the same subject as current one."
7432   (interactive)
7433   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7434
7435 (defun gnus-summary-first-unread-article ()
7436   "Select the first unread article.
7437 Return nil if there are no unread articles."
7438   (interactive)
7439   (prog1
7440       (when (gnus-summary-first-subject t)
7441         (gnus-summary-show-thread)
7442         (gnus-summary-first-subject t)
7443         (gnus-summary-display-article (gnus-summary-article-number)))
7444     (gnus-summary-position-point)))
7445
7446 (defun gnus-summary-first-unread-subject ()
7447   "Place the point on the subject line of the first unread article.
7448 Return nil if there are no unread articles."
7449   (interactive)
7450   (prog1
7451       (when (gnus-summary-first-subject t)
7452         (gnus-summary-show-thread)
7453         (gnus-summary-first-subject t))
7454     (gnus-summary-position-point)))
7455
7456 (defun gnus-summary-first-unseen-subject ()
7457   "Place the point on the subject line of the first unseen article.
7458 Return nil if there are no unseen articles."
7459   (interactive)
7460   (prog1
7461       (when (gnus-summary-first-subject nil nil t)
7462         (gnus-summary-show-thread)
7463         (gnus-summary-first-subject nil nil t))
7464     (gnus-summary-position-point)))
7465
7466 (defun gnus-summary-first-unseen-or-unread-subject ()
7467   "Place the point on the subject line of the first unseen article or,
7468 if all article have been seen, on the subject line of the first unread
7469 article."
7470   (interactive)
7471   (prog1
7472       (unless (when (gnus-summary-first-subject nil nil t)
7473                 (gnus-summary-show-thread)
7474                 (gnus-summary-first-subject nil nil t))
7475         (when (gnus-summary-first-subject t)
7476           (gnus-summary-show-thread)
7477           (gnus-summary-first-subject t)))
7478     (gnus-summary-position-point)))
7479
7480 (defun gnus-summary-first-article ()
7481   "Select the first article.
7482 Return nil if there are no articles."
7483   (interactive)
7484   (prog1
7485       (when (gnus-summary-first-subject)
7486         (gnus-summary-show-thread)
7487         (gnus-summary-first-subject)
7488         (gnus-summary-display-article (gnus-summary-article-number)))
7489     (gnus-summary-position-point)))
7490
7491 (defun gnus-summary-best-unread-article (&optional arg)
7492   "Select the unread article with the highest score.
7493 If given a prefix argument, select the next unread article that has a
7494 score higher than the default score."
7495   (interactive "P")
7496   (let ((article (if arg
7497                      (gnus-summary-better-unread-subject)
7498                    (gnus-summary-best-unread-subject))))
7499     (if article
7500         (gnus-summary-goto-article article)
7501       (error "No unread articles"))))
7502
7503 (defun gnus-summary-best-unread-subject ()
7504   "Select the unread subject with the highest score."
7505   (interactive)
7506   (let ((best -1000000)
7507         (data gnus-newsgroup-data)
7508         article score)
7509     (while data
7510       (and (gnus-data-unread-p (car data))
7511            (> (setq score
7512                     (gnus-summary-article-score (gnus-data-number (car data))))
7513               best)
7514            (setq best score
7515                  article (gnus-data-number (car data))))
7516       (setq data (cdr data)))
7517     (when article
7518       (gnus-summary-goto-subject article))
7519     (gnus-summary-position-point)
7520     article))
7521
7522 (defun gnus-summary-better-unread-subject ()
7523   "Select the first unread subject that has a score over the default score."
7524   (interactive)
7525   (let ((data gnus-newsgroup-data)
7526         article score)
7527     (while (and (setq article (gnus-data-number (car data)))
7528                 (or (gnus-data-read-p (car data))
7529                     (not (> (gnus-summary-article-score article)
7530                             gnus-summary-default-score))))
7531       (setq data (cdr data)))
7532     (when article
7533       (gnus-summary-goto-subject article))
7534     (gnus-summary-position-point)
7535     article))
7536
7537 (defun gnus-summary-last-subject ()
7538   "Go to the last displayed subject line in the group."
7539   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7540     (when article
7541       (gnus-summary-goto-subject article))))
7542
7543 (defun gnus-summary-goto-article (article &optional all-headers force)
7544   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7545 If ALL-HEADERS is non-nil, no header lines are hidden.
7546 If FORCE, go to the article even if it isn't displayed.  If FORCE
7547 is a number, it is the line the article is to be displayed on."
7548   (interactive
7549    (list
7550     (completing-read
7551      "Article number or Message-ID: "
7552      (mapcar (lambda (number) (list (int-to-string number)))
7553              gnus-newsgroup-limit))
7554     current-prefix-arg
7555     t))
7556   (prog1
7557       (if (and (stringp article)
7558                (string-match "@\\|%40" article))
7559           (gnus-summary-refer-article article)
7560         (when (stringp article)
7561           (setq article (string-to-number article)))
7562         (if (gnus-summary-goto-subject article force)
7563             (gnus-summary-display-article article all-headers)
7564           (gnus-message 4 "Couldn't go to article %s" article) nil))
7565     (gnus-summary-position-point)))
7566
7567 (defun gnus-summary-goto-last-article ()
7568   "Go to the previously read article."
7569   (interactive)
7570   (prog1
7571       (when gnus-last-article
7572         (gnus-summary-goto-article gnus-last-article nil t))
7573     (gnus-summary-position-point)))
7574
7575 (defun gnus-summary-pop-article (number)
7576   "Pop one article off the history and go to the previous.
7577 NUMBER articles will be popped off."
7578   (interactive "p")
7579   (let (to)
7580     (setq gnus-newsgroup-history
7581           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7582     (if to
7583         (gnus-summary-goto-article (car to) nil t)
7584       (error "Article history empty")))
7585   (gnus-summary-position-point))
7586
7587 ;; Summary commands and functions for limiting the summary buffer.
7588
7589 (defun gnus-summary-limit-to-articles (n)
7590   "Limit the summary buffer to the next N articles.
7591 If not given a prefix, use the process marked articles instead."
7592   (interactive "P")
7593   (prog1
7594       (let ((articles (gnus-summary-work-articles n)))
7595         (setq gnus-newsgroup-processable nil)
7596         (gnus-summary-limit articles))
7597     (gnus-summary-position-point)))
7598
7599 (defun gnus-summary-pop-limit (&optional total)
7600   "Restore the previous limit.
7601 If given a prefix, remove all limits."
7602   (interactive "P")
7603   (when total
7604     (setq gnus-newsgroup-limits
7605           (list (mapcar (lambda (h) (mail-header-number h))
7606                         gnus-newsgroup-headers))))
7607   (unless gnus-newsgroup-limits
7608     (error "No limit to pop"))
7609   (prog1
7610       (gnus-summary-limit nil 'pop)
7611     (gnus-summary-position-point)))
7612
7613 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7614   "Limit the summary buffer to articles that have subjects that match a regexp.
7615 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7616   (interactive
7617    (list (read-string (if current-prefix-arg
7618                           "Exclude subject (regexp): "
7619                         "Limit to subject (regexp): "))
7620          nil current-prefix-arg))
7621   (unless header
7622     (setq header "subject"))
7623   (when (not (equal "" subject))
7624     (prog1
7625         (let ((articles (gnus-summary-find-matching
7626                          (or header "subject") subject 'all nil nil
7627                          not-matching)))
7628           (unless articles
7629             (error "Found no matches for \"%s\"" subject))
7630           (gnus-summary-limit articles))
7631       (gnus-summary-position-point))))
7632
7633 (defun gnus-summary-limit-to-author (from &optional not-matching)
7634   "Limit the summary buffer to articles that have authors that match a regexp.
7635 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7636   (interactive
7637    (list (read-string (if current-prefix-arg
7638                           "Exclude author (regexp): "
7639                         "Limit to author (regexp): "))
7640          current-prefix-arg))
7641   (gnus-summary-limit-to-subject from "from" not-matching))
7642
7643 (defun gnus-summary-limit-to-age (age &optional younger-p)
7644   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7645 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7646 articles that are younger than AGE days."
7647   (interactive
7648    (let ((younger current-prefix-arg)
7649          (days-got nil)
7650          days)
7651      (while (not days-got)
7652        (setq days (if younger
7653                       (read-string "Limit to articles younger than (in days, older when negative): ")
7654                     (read-string
7655                      "Limit to articles older than (in days, younger when negative): ")))
7656        (when (> (length days) 0)
7657          (setq days (read days)))
7658        (if (numberp days)
7659            (progn
7660              (setq days-got t)
7661              (when (< days 0)
7662                (setq younger (not younger))
7663                (setq days (* days -1))))
7664          (message "Please enter a number.")
7665          (sleep-for 1)))
7666      (list days younger)))
7667   (prog1
7668       (let ((data gnus-newsgroup-data)
7669             (cutoff (days-to-time age))
7670             articles d date is-younger)
7671         (while (setq d (pop data))
7672           (when (and (vectorp (gnus-data-header d))
7673                      (setq date (mail-header-date (gnus-data-header d))))
7674             (setq is-younger (time-less-p
7675                               (time-since (condition-case ()
7676                                               (date-to-time date)
7677                                             (error '(0 0))))
7678                               cutoff))
7679             (when (if younger-p
7680                       is-younger
7681                     (not is-younger))
7682               (push (gnus-data-number d) articles))))
7683         (gnus-summary-limit (nreverse articles)))
7684     (gnus-summary-position-point)))
7685
7686 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7687   "Limit the summary buffer to articles that match an 'extra' header."
7688   (interactive
7689    (let ((header
7690           (intern
7691            (gnus-completing-read-with-default
7692             (symbol-name (car gnus-extra-headers))
7693             (if current-prefix-arg
7694                 "Exclude extra header:"
7695               "Limit extra header:")
7696             (mapcar (lambda (x)
7697                       (cons (symbol-name x) x))
7698                     gnus-extra-headers)
7699             nil
7700             t))))
7701      (list header
7702            (read-string (format "%s header %s (regexp): "
7703                                 (if current-prefix-arg "Exclude" "Limit to")
7704                                 header))
7705            current-prefix-arg)))
7706   (when (not (equal "" regexp))
7707     (prog1
7708         (let ((articles (gnus-summary-find-matching
7709                          (cons 'extra header) regexp 'all nil nil
7710                          not-matching)))
7711           (unless articles
7712             (error "Found no matches for \"%s\"" regexp))
7713           (gnus-summary-limit articles))
7714       (gnus-summary-position-point))))
7715
7716 (defun gnus-summary-limit-to-display-predicate ()
7717   "Limit the summary buffer to the predicated in the `display' group parameter."
7718   (interactive)
7719   (unless gnus-newsgroup-display
7720     (error "There is no `display' group parameter"))
7721   (let (articles)
7722     (dolist (number gnus-newsgroup-articles)
7723       (when (funcall gnus-newsgroup-display)
7724         (push number articles)))
7725     (gnus-summary-limit articles))
7726   (gnus-summary-position-point))
7727
7728 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7729 (make-obsolete
7730  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7731
7732 (defun gnus-summary-limit-to-unread (&optional all)
7733   "Limit the summary buffer to articles that are not marked as read.
7734 If ALL is non-nil, limit strictly to unread articles."
7735   (interactive "P")
7736   (if all
7737       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7738     (gnus-summary-limit-to-marks
7739      ;; Concat all the marks that say that an article is read and have
7740      ;; those removed.
7741      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7742            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7743            gnus-low-score-mark gnus-expirable-mark
7744            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7745            gnus-duplicate-mark gnus-souped-mark)
7746      'reverse)))
7747
7748 (defun gnus-summary-limit-to-replied (&optional unreplied)
7749   "Limit the summary buffer to replied articles.
7750 If UNREPLIED (the prefix), limit to unreplied articles."
7751   (interactive "P")
7752   (if unreplied
7753       (gnus-summary-limit
7754        (gnus-set-difference gnus-newsgroup-articles
7755         gnus-newsgroup-replied))
7756     (gnus-summary-limit gnus-newsgroup-replied))
7757   (gnus-summary-position-point))
7758
7759 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7760 (make-obsolete 'gnus-summary-delete-marked-with
7761                'gnus-summary-limit-exclude-marks)
7762
7763 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7764   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7765 If REVERSE, limit the summary buffer to articles that are marked
7766 with MARKS.  MARKS can either be a string of marks or a list of marks.
7767 Returns how many articles were removed."
7768   (interactive "sMarks: ")
7769   (gnus-summary-limit-to-marks marks t))
7770
7771 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7772   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7773 If REVERSE (the prefix), limit the summary buffer to articles that are
7774 not marked with MARKS.  MARKS can either be a string of marks or a
7775 list of marks.
7776 Returns how many articles were removed."
7777   (interactive "sMarks: \nP")
7778   (prog1
7779       (let ((data gnus-newsgroup-data)
7780             (marks (if (listp marks) marks
7781                      (append marks nil))) ; Transform to list.
7782             articles)
7783         (while data
7784           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7785                   (memq (gnus-data-mark (car data)) marks))
7786             (push (gnus-data-number (car data)) articles))
7787           (setq data (cdr data)))
7788         (gnus-summary-limit articles))
7789     (gnus-summary-position-point)))
7790
7791 (defun gnus-summary-limit-to-score (score)
7792   "Limit to articles with score at or above SCORE."
7793   (interactive "NLimit to articles with score of at least: ")
7794   (let ((data gnus-newsgroup-data)
7795         articles)
7796     (while data
7797       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7798                 score)
7799         (push (gnus-data-number (car data)) articles))
7800       (setq data (cdr data)))
7801     (prog1
7802         (gnus-summary-limit articles)
7803       (gnus-summary-position-point))))
7804
7805 (defun gnus-summary-limit-to-unseen ()
7806   "Limit to unseen articles."
7807   (interactive)
7808   (prog1
7809       (gnus-summary-limit gnus-newsgroup-unseen)
7810     (gnus-summary-position-point)))
7811
7812 (defun gnus-summary-limit-include-thread (id)
7813   "Display all the hidden articles that is in the thread with ID in it.
7814 When called interactively, ID is the Message-ID of the current
7815 article."
7816   (interactive (list (mail-header-id (gnus-summary-article-header))))
7817   (let ((articles (gnus-articles-in-thread
7818                    (gnus-id-to-thread (gnus-root-id id)))))
7819     (prog1
7820         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7821       (gnus-summary-limit-include-matching-articles
7822        "subject"
7823        (regexp-quote (gnus-simplify-subject-re
7824                       (mail-header-subject (gnus-id-to-header id)))))
7825       (gnus-summary-position-point))))
7826
7827 (defun gnus-summary-limit-include-matching-articles (header regexp)
7828   "Display all the hidden articles that have HEADERs that match REGEXP."
7829   (interactive (list (read-string "Match on header: ")
7830                      (read-string "Regexp: ")))
7831   (let ((articles (gnus-find-matching-articles header regexp)))
7832     (prog1
7833         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7834       (gnus-summary-position-point))))
7835
7836 (defun gnus-summary-insert-dormant-articles ()
7837   "Insert all the dormant articles for this group into the current buffer."
7838   (interactive)
7839   (let ((gnus-verbose (max 6 gnus-verbose)))
7840     (if (not gnus-newsgroup-dormant)
7841         (gnus-message 3 "No cached articles for this group")
7842       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7843
7844 (defun gnus-summary-limit-include-dormant ()
7845   "Display all the hidden articles that are marked as dormant.
7846 Note that this command only works on a subset of the articles currently
7847 fetched for this group."
7848   (interactive)
7849   (unless gnus-newsgroup-dormant
7850     (error "There are no dormant articles in this group"))
7851   (prog1
7852       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7853     (gnus-summary-position-point)))
7854
7855 (defun gnus-summary-limit-exclude-dormant ()
7856   "Hide all dormant articles."
7857   (interactive)
7858   (prog1
7859       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7860     (gnus-summary-position-point)))
7861
7862 (defun gnus-summary-limit-exclude-childless-dormant ()
7863   "Hide all dormant articles that have no children."
7864   (interactive)
7865   (let ((data (gnus-data-list t))
7866         articles d children)
7867     ;; Find all articles that are either not dormant or have
7868     ;; children.
7869     (while (setq d (pop data))
7870       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7871                 (and (setq children
7872                            (gnus-article-children (gnus-data-number d)))
7873                      (let (found)
7874                        (while children
7875                          (when (memq (car children) articles)
7876                            (setq children nil
7877                                  found t))
7878                          (pop children))
7879                        found)))
7880         (push (gnus-data-number d) articles)))
7881     ;; Do the limiting.
7882     (prog1
7883         (gnus-summary-limit articles)
7884       (gnus-summary-position-point))))
7885
7886 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7887   "Mark all unread excluded articles as read.
7888 If ALL, mark even excluded ticked and dormants as read."
7889   (interactive "P")
7890   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7891   (let ((articles (gnus-sorted-ndifference
7892                    (sort
7893                     (mapcar (lambda (h) (mail-header-number h))
7894                             gnus-newsgroup-headers)
7895                     '<)
7896                    gnus-newsgroup-limit))
7897         article)
7898     (setq gnus-newsgroup-unreads
7899           (gnus-sorted-intersection gnus-newsgroup-unreads
7900                                     gnus-newsgroup-limit))
7901     (if all
7902         (setq gnus-newsgroup-dormant nil
7903               gnus-newsgroup-marked nil
7904               gnus-newsgroup-reads
7905               (nconc
7906                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7907                gnus-newsgroup-reads))
7908       (while (setq article (pop articles))
7909         (unless (or (memq article gnus-newsgroup-dormant)
7910                     (memq article gnus-newsgroup-marked))
7911           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7912
7913 (defun gnus-summary-limit (articles &optional pop)
7914   (if pop
7915       ;; We pop the previous limit off the stack and use that.
7916       (setq articles (car gnus-newsgroup-limits)
7917             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7918     ;; We use the new limit, so we push the old limit on the stack.
7919     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7920   ;; Set the limit.
7921   (setq gnus-newsgroup-limit articles)
7922   (let ((total (length gnus-newsgroup-data))
7923         (data (gnus-data-find-list (gnus-summary-article-number)))
7924         (gnus-summary-mark-below nil)   ; Inhibit this.
7925         found)
7926     ;; This will do all the work of generating the new summary buffer
7927     ;; according to the new limit.
7928     (gnus-summary-prepare)
7929     ;; Hide any threads, possibly.
7930     (gnus-summary-maybe-hide-threads)
7931     ;; Try to return to the article you were at, or one in the
7932     ;; neighborhood.
7933     (when data
7934       ;; We try to find some article after the current one.
7935       (while data
7936         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7937           (setq data nil
7938                 found t))
7939         (setq data (cdr data))))
7940     (unless found
7941       ;; If there is no data, that means that we were after the last
7942       ;; article.  The same goes when we can't find any articles
7943       ;; after the current one.
7944       (goto-char (point-max))
7945       (gnus-summary-find-prev))
7946     (gnus-set-mode-line 'summary)
7947     ;; We return how many articles were removed from the summary
7948     ;; buffer as a result of the new limit.
7949     (- total (length gnus-newsgroup-data))))
7950
7951 (defsubst gnus-invisible-cut-children (threads)
7952   (let ((num 0))
7953     (while threads
7954       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7955         (incf num))
7956       (pop threads))
7957     (< num 2)))
7958
7959 (defsubst gnus-cut-thread (thread)
7960   "Go forwards in the thread until we find an article that we want to display."
7961   (when (or (eq gnus-fetch-old-headers 'some)
7962             (eq gnus-fetch-old-headers 'invisible)
7963             (numberp gnus-fetch-old-headers)
7964             (eq gnus-build-sparse-threads 'some)
7965             (eq gnus-build-sparse-threads 'more))
7966     ;; Deal with old-fetched headers and sparse threads.
7967     (while (and
7968             thread
7969             (or
7970              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7971              (gnus-summary-article-ancient-p
7972               (mail-header-number (car thread))))
7973             (if (or (<= (length (cdr thread)) 1)
7974                     (eq gnus-fetch-old-headers 'invisible))
7975                 (setq gnus-newsgroup-limit
7976                       (delq (mail-header-number (car thread))
7977                             gnus-newsgroup-limit)
7978                       thread (cadr thread))
7979               (when (gnus-invisible-cut-children (cdr thread))
7980                 (let ((th (cdr thread)))
7981                   (while th
7982                     (if (memq (mail-header-number (caar th))
7983                               gnus-newsgroup-limit)
7984                         (setq thread (car th)
7985                               th nil)
7986                       (setq th (cdr th))))))))))
7987   thread)
7988
7989 (defun gnus-cut-threads (threads)
7990   "Cut off all uninteresting articles from the beginning of THREADS."
7991   (when (or (eq gnus-fetch-old-headers 'some)
7992             (eq gnus-fetch-old-headers 'invisible)
7993             (numberp gnus-fetch-old-headers)
7994             (eq gnus-build-sparse-threads 'some)
7995             (eq gnus-build-sparse-threads 'more))
7996     (let ((th threads))
7997       (while th
7998         (setcar th (gnus-cut-thread (car th)))
7999         (setq th (cdr th)))))
8000   ;; Remove nixed out threads.
8001   (delq nil threads))
8002
8003 (defun gnus-summary-initial-limit (&optional show-if-empty)
8004   "Figure out what the initial limit is supposed to be on group entry.
8005 This entails weeding out unwanted dormants, low-scored articles,
8006 fetch-old-headers verbiage, and so on."
8007   ;; Most groups have nothing to remove.
8008   (if (or gnus-inhibit-limiting
8009           (and (null gnus-newsgroup-dormant)
8010                (eq gnus-newsgroup-display 'gnus-not-ignore)
8011                (not (eq gnus-fetch-old-headers 'some))
8012                (not (numberp gnus-fetch-old-headers))
8013                (not (eq gnus-fetch-old-headers 'invisible))
8014                (null gnus-summary-expunge-below)
8015                (not (eq gnus-build-sparse-threads 'some))
8016                (not (eq gnus-build-sparse-threads 'more))
8017                (null gnus-thread-expunge-below)
8018                (not gnus-use-nocem)))
8019       ()                                ; Do nothing.
8020     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8021     (setq gnus-newsgroup-limit nil)
8022     (mapatoms
8023      (lambda (node)
8024        (unless (car (symbol-value node))
8025          ;; These threads have no parents -- they are roots.
8026          (let ((nodes (cdr (symbol-value node)))
8027                thread)
8028            (while nodes
8029              (if (and gnus-thread-expunge-below
8030                       (< (gnus-thread-total-score (car nodes))
8031                          gnus-thread-expunge-below))
8032                  (gnus-expunge-thread (pop nodes))
8033                (setq thread (pop nodes))
8034                (gnus-summary-limit-children thread))))))
8035      gnus-newsgroup-dependencies)
8036     ;; If this limitation resulted in an empty group, we might
8037     ;; pop the previous limit and use it instead.
8038     (when (and (not gnus-newsgroup-limit)
8039                show-if-empty)
8040       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8041     gnus-newsgroup-limit))
8042
8043 (defun gnus-summary-limit-children (thread)
8044   "Return 1 if this subthread is visible and 0 if it is not."
8045   ;; First we get the number of visible children to this thread.  This
8046   ;; is done by recursing down the thread using this function, so this
8047   ;; will really go down to a leaf article first, before slowly
8048   ;; working its way up towards the root.
8049   (when thread
8050     (let* ((max-lisp-eval-depth 5000)
8051            (children
8052            (if (cdr thread)
8053                (apply '+ (mapcar 'gnus-summary-limit-children
8054                                  (cdr thread)))
8055              0))
8056           (number (mail-header-number (car thread)))
8057           score)
8058       (if (and
8059            (not (memq number gnus-newsgroup-marked))
8060            (or
8061             ;; If this article is dormant and has absolutely no visible
8062             ;; children, then this article isn't visible.
8063             (and (memq number gnus-newsgroup-dormant)
8064                  (zerop children))
8065             ;; If this is "fetch-old-headered" and there is no
8066             ;; visible children, then we don't want this article.
8067             (and (or (eq gnus-fetch-old-headers 'some)
8068                      (numberp gnus-fetch-old-headers))
8069                  (gnus-summary-article-ancient-p number)
8070                  (zerop children))
8071             ;; If this is "fetch-old-headered" and `invisible', then
8072             ;; we don't want this article.
8073             (and (eq gnus-fetch-old-headers 'invisible)
8074                  (gnus-summary-article-ancient-p number))
8075             ;; If this is a sparsely inserted article with no children,
8076             ;; we don't want it.
8077             (and (eq gnus-build-sparse-threads 'some)
8078                  (gnus-summary-article-sparse-p number)
8079                  (zerop children))
8080             ;; If we use expunging, and this article is really
8081             ;; low-scored, then we don't want this article.
8082             (when (and gnus-summary-expunge-below
8083                        (< (setq score
8084                                 (or (cdr (assq number gnus-newsgroup-scored))
8085                                     gnus-summary-default-score))
8086                           gnus-summary-expunge-below))
8087               ;; We increase the expunge-tally here, but that has
8088               ;; nothing to do with the limits, really.
8089               (incf gnus-newsgroup-expunged-tally)
8090               ;; We also mark as read here, if that's wanted.
8091               (when (and gnus-summary-mark-below
8092                          (< score gnus-summary-mark-below))
8093                 (setq gnus-newsgroup-unreads
8094                       (delq number gnus-newsgroup-unreads))
8095                 (if gnus-newsgroup-auto-expire
8096                     (push number gnus-newsgroup-expirable)
8097                   (push (cons number gnus-low-score-mark)
8098                         gnus-newsgroup-reads)))
8099               t)
8100             ;; Do the `display' group parameter.
8101             (and gnus-newsgroup-display
8102                  (not (funcall gnus-newsgroup-display)))
8103             ;; Check NoCeM things.
8104             (when (and gnus-use-nocem
8105                        (gnus-nocem-unwanted-article-p
8106                         (mail-header-id (car thread))))
8107               (setq gnus-newsgroup-unreads
8108                     (delq number gnus-newsgroup-unreads))
8109               t)))
8110           ;; Nope, invisible article.
8111           0
8112         ;; Ok, this article is to be visible, so we add it to the limit
8113         ;; and return 1.
8114         (push number gnus-newsgroup-limit)
8115         1))))
8116
8117 (defun gnus-expunge-thread (thread)
8118   "Mark all articles in THREAD as read."
8119   (let* ((number (mail-header-number (car thread))))
8120     (incf gnus-newsgroup-expunged-tally)
8121     ;; We also mark as read here, if that's wanted.
8122     (setq gnus-newsgroup-unreads
8123           (delq number gnus-newsgroup-unreads))
8124     (if gnus-newsgroup-auto-expire
8125         (push number gnus-newsgroup-expirable)
8126       (push (cons number gnus-low-score-mark)
8127             gnus-newsgroup-reads)))
8128   ;; Go recursively through all subthreads.
8129   (mapcar 'gnus-expunge-thread (cdr thread)))
8130
8131 ;; Summary article oriented commands
8132
8133 (defun gnus-summary-refer-parent-article (n)
8134   "Refer parent article N times.
8135 If N is negative, go to ancestor -N instead.
8136 The difference between N and the number of articles fetched is returned."
8137   (interactive "p")
8138   (let ((skip 1)
8139         error header ref)
8140     (when (not (natnump n))
8141       (setq skip (abs n)
8142             n 1))
8143     (while (and (> n 0)
8144                 (not error))
8145       (setq header (gnus-summary-article-header))
8146       (if (and (eq (mail-header-number header)
8147                    (cdr gnus-article-current))
8148                (equal gnus-newsgroup-name
8149                       (car gnus-article-current)))
8150           ;; If we try to find the parent of the currently
8151           ;; displayed article, then we take a look at the actual
8152           ;; References header, since this is slightly more
8153           ;; reliable than the References field we got from the
8154           ;; server.
8155           (save-excursion
8156             (set-buffer gnus-original-article-buffer)
8157             (nnheader-narrow-to-headers)
8158             (unless (setq ref (message-fetch-field "references"))
8159               (when (setq ref (message-fetch-field "in-reply-to"))
8160                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8161             (widen))
8162         (setq ref
8163               ;; It's not the current article, so we take a bet on
8164               ;; the value we got from the server.
8165               (mail-header-references header)))
8166       (if (and ref
8167                (not (equal ref "")))
8168           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8169             (gnus-message 1 "Couldn't find parent"))
8170         (gnus-message 1 "No references in article %d"
8171                       (gnus-summary-article-number))
8172         (setq error t))
8173       (decf n))
8174     (gnus-summary-position-point)
8175     n))
8176
8177 (defun gnus-summary-refer-references ()
8178   "Fetch all articles mentioned in the References header.
8179 Return the number of articles fetched."
8180   (interactive)
8181   (let ((ref (mail-header-references (gnus-summary-article-header)))
8182         (current (gnus-summary-article-number))
8183         (n 0))
8184     (if (or (not ref)
8185             (equal ref ""))
8186         (error "No References in the current article")
8187       ;; For each Message-ID in the References header...
8188       (while (string-match "<[^>]*>" ref)
8189         (incf n)
8190         ;; ... fetch that article.
8191         (gnus-summary-refer-article
8192          (prog1 (match-string 0 ref)
8193            (setq ref (substring ref (match-end 0))))))
8194       (gnus-summary-goto-subject current)
8195       (gnus-summary-position-point)
8196       n)))
8197
8198 (defun gnus-summary-refer-thread (&optional limit)
8199   "Fetch all articles in the current thread.
8200 If LIMIT (the numerical prefix), fetch that many old headers instead
8201 of what's specified by the `gnus-refer-thread-limit' variable."
8202   (interactive "P")
8203   (let ((id (mail-header-id (gnus-summary-article-header)))
8204         (limit (if limit (prefix-numeric-value limit)
8205                  gnus-refer-thread-limit)))
8206     (unless (eq gnus-fetch-old-headers 'invisible)
8207       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8208       ;; Retrieve the headers and read them in.
8209       (if (eq (if (numberp limit)
8210                   (gnus-retrieve-headers
8211                    (list (min
8212                           (+ (mail-header-number
8213                               (gnus-summary-article-header))
8214                              limit)
8215                           gnus-newsgroup-end))
8216                    gnus-newsgroup-name (* limit 2))
8217                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8218                 ;; headers.
8219                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8220                                        gnus-newsgroup-name limit))
8221               'nov)
8222           (gnus-build-all-threads)
8223         (error "Can't fetch thread from back ends that don't support NOV"))
8224       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8225     (gnus-summary-limit-include-thread id)))
8226
8227 (defun gnus-summary-refer-article (message-id)
8228   "Fetch an article specified by MESSAGE-ID."
8229   (interactive "sMessage-ID: ")
8230   (when (and (stringp message-id)
8231              (not (zerop (length message-id))))
8232     (setq message-id (gnus-replace-in-string message-id " " ""))
8233     ;; Construct the correct Message-ID if necessary.
8234     ;; Suggested by tale@pawl.rpi.edu.
8235     (unless (string-match "^<" message-id)
8236       (setq message-id (concat "<" message-id)))
8237     (unless (string-match ">$" message-id)
8238       (setq message-id (concat message-id ">")))
8239     ;; People often post MIDs from URLs, so unhex it:
8240     (unless (string-match "@" message-id)
8241       (setq message-id (gnus-url-unhex-string message-id)))
8242     (let* ((header (gnus-id-to-header message-id))
8243            (sparse (and header
8244                         (gnus-summary-article-sparse-p
8245                          (mail-header-number header))
8246                         (memq (mail-header-number header)
8247                               gnus-newsgroup-limit)))
8248            number)
8249       (cond
8250        ;; If the article is present in the buffer we just go to it.
8251        ((and header
8252              (or (not (gnus-summary-article-sparse-p
8253                        (mail-header-number header)))
8254                  sparse))
8255         (prog1
8256             (gnus-summary-goto-article
8257              (mail-header-number header) nil t)
8258           (when sparse
8259             (gnus-summary-update-article (mail-header-number header)))))
8260        (t
8261         ;; We fetch the article.
8262         (catch 'found
8263           (dolist (gnus-override-method (gnus-refer-article-methods))
8264             (when (and (gnus-check-server gnus-override-method)
8265                        ;; Fetch the header,
8266                        (setq number (gnus-summary-insert-subject message-id)))
8267               ;; and display the article.
8268               (gnus-summary-select-article nil nil nil number)
8269               (throw 'found t)))
8270           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8271
8272 (defun gnus-refer-article-methods ()
8273   "Return a list of referable methods."
8274   (cond
8275    ;; No method, so we default to current and native.
8276    ((null gnus-refer-article-method)
8277     (list gnus-current-select-method gnus-select-method))
8278    ;; Current.
8279    ((eq 'current gnus-refer-article-method)
8280     (list gnus-current-select-method))
8281    ;; List of select methods.
8282    ((not (and (symbolp (car gnus-refer-article-method))
8283               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8284     (let (out)
8285       (dolist (method gnus-refer-article-method)
8286         (push (if (eq 'current method)
8287                   gnus-current-select-method
8288                 method)
8289               out))
8290       (nreverse out)))
8291    ;; One single select method.
8292    (t
8293     (list gnus-refer-article-method))))
8294
8295 (defun gnus-summary-edit-parameters ()
8296   "Edit the group parameters of the current group."
8297   (interactive)
8298   (gnus-group-edit-group gnus-newsgroup-name 'params))
8299
8300 (defun gnus-summary-customize-parameters ()
8301   "Customize the group parameters of the current group."
8302   (interactive)
8303   (gnus-group-customize gnus-newsgroup-name))
8304
8305 (defun gnus-summary-enter-digest-group (&optional force)
8306   "Enter an nndoc group based on the current article.
8307 If FORCE, force a digest interpretation.  If not, try
8308 to guess what the document format is."
8309   (interactive "P")
8310   (let ((conf gnus-current-window-configuration))
8311     (save-window-excursion
8312       (save-excursion
8313         (let (gnus-article-prepare-hook
8314               gnus-display-mime-function
8315               gnus-break-pages)
8316           (gnus-summary-select-article))))
8317     (setq gnus-current-window-configuration conf)
8318     (let* ((name (format "%s-%d"
8319                          (gnus-group-prefixed-name
8320                           gnus-newsgroup-name (list 'nndoc ""))
8321                          (save-excursion
8322                            (set-buffer gnus-summary-buffer)
8323                            gnus-current-article)))
8324            (ogroup gnus-newsgroup-name)
8325            (params (append (gnus-info-params (gnus-get-info ogroup))
8326                            (list (cons 'to-group ogroup))
8327                            (list (cons 'parent-group ogroup))
8328                            (list (cons 'save-article-group ogroup))))
8329            (case-fold-search t)
8330            (buf (current-buffer))
8331            dig to-address)
8332       (save-excursion
8333         (set-buffer gnus-original-article-buffer)
8334         ;; Have the digest group inherit the main mail address of
8335         ;; the parent article.
8336         (when (setq to-address (or (gnus-fetch-field "reply-to")
8337                                    (gnus-fetch-field "from")))
8338           (setq params (append
8339                         (list (cons 'to-address
8340                                     (funcall gnus-decode-encoded-word-function
8341                                              to-address))))))
8342         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8343         (insert-buffer-substring gnus-original-article-buffer)
8344         ;; Remove lines that may lead nndoc to misinterpret the
8345         ;; document type.
8346         (narrow-to-region
8347          (goto-char (point-min))
8348          (or (search-forward "\n\n" nil t) (point)))
8349         (goto-char (point-min))
8350         (delete-matching-lines "^Path:\\|^From ")
8351         (widen))
8352       (unwind-protect
8353           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8354                     (gnus-newsgroup-ephemeral-ignored-charsets
8355                      gnus-newsgroup-ignored-charsets))
8356                 (gnus-group-read-ephemeral-group
8357                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8358                               (nndoc-article-type
8359                                ,(if force 'mbox 'guess)))
8360                  t nil nil nil
8361                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8362                                                         "ADAPT")))))
8363               ;; Make all postings to this group go to the parent group.
8364               (nconc (gnus-info-params (gnus-get-info name))
8365                      params)
8366             ;; Couldn't select this doc group.
8367             (switch-to-buffer buf)
8368             (gnus-set-global-variables)
8369             (gnus-configure-windows 'summary)
8370             (gnus-message 3 "Article couldn't be entered?"))
8371         (kill-buffer dig)))))
8372
8373 (defun gnus-summary-read-document (n)
8374   "Open a new group based on the current article(s).
8375 This will allow you to read digests and other similar
8376 documents as newsgroups.
8377 Obeys the standard process/prefix convention."
8378   (interactive "P")
8379   (let* ((articles (gnus-summary-work-articles n))
8380          (ogroup gnus-newsgroup-name)
8381          (params (append (gnus-info-params (gnus-get-info ogroup))
8382                          (list (cons 'to-group ogroup))))
8383          article group egroup groups vgroup)
8384     (while (setq article (pop articles))
8385       (setq group (format "%s-%d" gnus-newsgroup-name article))
8386       (gnus-summary-remove-process-mark article)
8387       (when (gnus-summary-display-article article)
8388         (save-excursion
8389           (with-temp-buffer
8390             (insert-buffer-substring gnus-original-article-buffer)
8391             ;; Remove some headers that may lead nndoc to make
8392             ;; the wrong guess.
8393             (message-narrow-to-head)
8394             (goto-char (point-min))
8395             (delete-matching-lines "^Path:\\|^From ")
8396             (widen)
8397             (if (setq egroup
8398                       (gnus-group-read-ephemeral-group
8399                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8400                                      (nndoc-article-type guess))
8401                        t nil t))
8402                 (progn
8403                   ;; Make all postings to this group go to the parent group.
8404                   (nconc (gnus-info-params (gnus-get-info egroup))
8405                          params)
8406                   (push egroup groups))
8407               ;; Couldn't select this doc group.
8408               (gnus-error 3 "Article couldn't be entered"))))))
8409     ;; Now we have selected all the documents.
8410     (cond
8411      ((not groups)
8412       (error "None of the articles could be interpreted as documents"))
8413      ((gnus-group-read-ephemeral-group
8414        (setq vgroup (format
8415                      "nnvirtual:%s-%s" gnus-newsgroup-name
8416                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8417        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8418        t
8419        (cons (current-buffer) 'summary)))
8420      (t
8421       (error "Couldn't select virtual nndoc group")))))
8422
8423 (defun gnus-summary-isearch-article (&optional regexp-p)
8424   "Do incremental search forward on the current article.
8425 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8426   (interactive "P")
8427   (let* ((gnus-inhibit-treatment t)
8428          (old (gnus-summary-select-article)))
8429     (gnus-configure-windows 'article)
8430     (gnus-eval-in-buffer-window gnus-article-buffer
8431       (save-restriction
8432         (widen)
8433         (when (eq 'old old)
8434           (gnus-article-show-all-headers))
8435         (goto-char (point-min))
8436         (isearch-forward regexp-p)))))
8437
8438 (defun gnus-summary-search-article-forward (regexp &optional backward)
8439   "Search for an article containing REGEXP forward.
8440 If BACKWARD, search backward instead."
8441   (interactive
8442    (list (read-string
8443           (format "Search article %s (regexp%s): "
8444                   (if current-prefix-arg "backward" "forward")
8445                   (if gnus-last-search-regexp
8446                       (concat ", default " gnus-last-search-regexp)
8447                     "")))
8448          current-prefix-arg))
8449   (if (string-equal regexp "")
8450       (setq regexp (or gnus-last-search-regexp ""))
8451     (setq gnus-last-search-regexp regexp)
8452     (setq gnus-article-before-search gnus-current-article))
8453   ;; Intentionally set gnus-last-article.
8454   (setq gnus-last-article gnus-article-before-search)
8455   (let ((gnus-last-article gnus-last-article))
8456     (if (gnus-summary-search-article regexp backward)
8457         (gnus-summary-show-thread)
8458       (error "Search failed: \"%s\"" regexp))))
8459
8460 (defun gnus-summary-search-article-backward (regexp)
8461   "Search for an article containing REGEXP backward."
8462   (interactive
8463    (list (read-string
8464           (format "Search article backward (regexp%s): "
8465                   (if gnus-last-search-regexp
8466                       (concat ", default " gnus-last-search-regexp)
8467                     "")))))
8468   (gnus-summary-search-article-forward regexp 'backward))
8469
8470 (eval-when-compile
8471   (defmacro gnus-summary-search-article-position-point (regexp backward)
8472     "Dehighlight the last matched text and goto the beginning position."
8473     (` (if (and gnus-summary-search-article-matched-data
8474                 (let ((text (caddr gnus-summary-search-article-matched-data))
8475                       (inhibit-read-only t)
8476                       buffer-read-only)
8477                   (delete-region
8478                    (goto-char (car gnus-summary-search-article-matched-data))
8479                    (cadr gnus-summary-search-article-matched-data))
8480                   (insert text)
8481                   (string-match (, regexp) text)))
8482            (if (, backward) (beginning-of-line) (end-of-line))
8483          (goto-char (if (, backward) (point-max) (point-min))))))
8484
8485   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
8486     "Place point where X-Face image is displayed."
8487     (if (featurep 'xemacs)
8488         (` (let ((end (if (search-forward "\n\n" nil t)
8489                           (goto-char (1- (point)))
8490                         (point-min)))
8491                  extent)
8492              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
8493              (unless (and (re-search-forward "^From:" end t)
8494                           (setq extent (extent-at (point)))
8495                           (extent-begin-glyph extent))
8496                (goto-char (, opoint)))))
8497       (` (let ((end (if (search-forward "\n\n" nil t)
8498                         (goto-char (1- (point)))
8499                       (point-min)))
8500                (start (or (search-backward "\n\n" nil t) (point-min))))
8501            (goto-char
8502             (or (text-property-any start end 'x-face-image t);; x-face-e21
8503                 (text-property-any start end 'x-face-mule-bitmap-image t)
8504                 (, opoint)))))))
8505
8506   (defmacro gnus-summary-search-article-highlight-matched-text
8507     (backward treated x-face)
8508     "Highlight matched text in the function `gnus-summary-search-article'."
8509     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
8510              (end (set-marker (make-marker) (match-end 0)))
8511              (inhibit-read-only t)
8512              buffer-read-only)
8513          (unless treated
8514            (let ((,@
8515                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
8516                     (mapcar
8517                      (lambda (item) (setq items (delq item items)))
8518                      '(gnus-treat-buttonize
8519                        gnus-treat-fill-article
8520                        gnus-treat-fill-long-lines
8521                        gnus-treat-emphasize
8522                        gnus-treat-highlight-headers
8523                        gnus-treat-highlight-citation
8524                        gnus-treat-highlight-signature
8525                        gnus-treat-overstrike
8526                        gnus-treat-display-x-face
8527                        gnus-treat-buttonize-head
8528                        gnus-treat-decode-article-as-default-mime-charset))
8529                     (static-if (featurep 'xemacs)
8530                         items
8531                       (cons '(x-face-mule-delete-x-face-field
8532                               (quote never))
8533                             items))))
8534                  (gnus-treat-display-x-face
8535                   (when (, x-face) gnus-treat-display-x-face)))
8536              (gnus-article-prepare-mime-display)))
8537          (goto-char (if (, backward) start end))
8538          (when (, x-face)
8539            (gnus-summary-search-article-highlight-goto-x-face (point)))
8540          (setq gnus-summary-search-article-matched-data
8541                (list start end (buffer-substring start end)))
8542          (unless (eq start end);; matched text has been deleted. :-<
8543            (put-text-property start end 'face
8544                               (or (find-face 'isearch)
8545                                   'secondary-selection))))))
8546   )
8547
8548 (defun gnus-summary-search-article (regexp &optional backward)
8549   "Search for an article containing REGEXP.
8550 Optional argument BACKWARD means do search for backward.
8551 `gnus-select-article-hook' is not called during the search."
8552   ;; We have to require this here to make sure that the following
8553   ;; dynamic binding isn't shadowed by autoloading.
8554   (require 'gnus-async)
8555   (require 'gnus-art)
8556   (let ((gnus-select-article-hook nil)  ;Disable hook.
8557         (gnus-article-prepare-hook nil)
8558         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8559         (gnus-use-article-prefetch nil)
8560         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8561         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8562         (gnus-visual nil)
8563         (gnus-keep-backlog nil)
8564         (gnus-break-pages nil)
8565         (gnus-summary-display-arrow nil)
8566         (gnus-updated-mode-lines nil)
8567         (gnus-auto-center-summary nil)
8568         (sum (current-buffer))
8569         (found nil)
8570         point treated)
8571     (gnus-save-hidden-threads
8572       (static-if (featurep 'xemacs)
8573           (let ((gnus-inhibit-treatment t))
8574             (setq treated (eq 'old (gnus-summary-select-article)))
8575             (when (and treated
8576                        (not (and (gnus-buffer-live-p gnus-article-buffer)
8577                                  (window-live-p (get-buffer-window
8578                                                  gnus-article-buffer t)))))
8579               (gnus-summary-select-article nil t)
8580               (setq treated nil)))
8581         (let ((gnus-inhibit-treatment t)
8582               (x-face-mule-delete-x-face-field 'never))
8583           (setq treated (eq 'old (gnus-summary-select-article)))
8584           (when (and treated
8585                      (not
8586                       (and (gnus-buffer-live-p gnus-article-buffer)
8587                            (window-live-p (get-buffer-window
8588                                            gnus-article-buffer t))
8589                            (or (not (string-match "^\\^X-Face:" regexp))
8590                                (with-current-buffer gnus-article-buffer
8591                                  gnus-summary-search-article-matched-data)))))
8592             (gnus-summary-select-article nil t)
8593             (setq treated nil))))
8594       (set-buffer gnus-article-buffer)
8595       (widen)
8596       (if treated
8597           (progn
8598             (gnus-article-show-all-headers)
8599             (gnus-summary-search-article-position-point regexp backward))
8600         (goto-char (if backward (point-max) (point-min))))
8601       (while (not found)
8602         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8603         (if (if backward
8604                 (re-search-backward regexp nil t)
8605               (re-search-forward regexp nil t))
8606             ;; We found the regexp.
8607             (progn
8608               (gnus-summary-search-article-highlight-matched-text
8609                backward treated (string-match "^\\^X-Face:" regexp))
8610               (setq found 'found)
8611               (forward-line
8612                (/ (- 2 (window-height
8613                         (get-buffer-window gnus-article-buffer t)))
8614                   2))
8615               (set-window-start
8616                (get-buffer-window (current-buffer))
8617                (point))
8618               (set-buffer sum)
8619               (setq point (point)))
8620           ;; We didn't find it, so we go to the next article.
8621           (set-buffer sum)
8622           (setq found 'not)
8623           (while (eq found 'not)
8624             (if (not (if backward (gnus-summary-find-prev)
8625                        (gnus-summary-find-next)))
8626                 ;; No more articles.
8627                 (setq found t)
8628               ;; Select the next article and adjust point.
8629               (unless (gnus-summary-article-sparse-p
8630                        (gnus-summary-article-number))
8631                 (setq found nil)
8632                 (let ((gnus-inhibit-treatment t))
8633                   (gnus-summary-select-article))
8634                 (setq treated nil)
8635                 (set-buffer gnus-article-buffer)
8636                 (widen)
8637                 (goto-char (if backward (point-max) (point-min))))))))
8638       (gnus-message 7 ""))
8639     ;; Return whether we found the regexp.
8640     (when (eq found 'found)
8641       (goto-char point)
8642       (gnus-summary-show-thread)
8643       (gnus-summary-goto-subject gnus-current-article)
8644       (gnus-summary-position-point)
8645       t)))
8646
8647 (defun gnus-find-matching-articles (header regexp)
8648   "Return a list of all articles that match REGEXP on HEADER.
8649 This search includes all articles in the current group that Gnus has
8650 fetched headers for, whether they are displayed or not."
8651   (let ((articles nil)
8652         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8653         (case-fold-search t))
8654     (dolist (header gnus-newsgroup-headers)
8655       (when (string-match regexp (funcall func header))
8656         (push (mail-header-number header) articles)))
8657     (nreverse articles)))
8658
8659 (defun gnus-summary-find-matching (header regexp &optional backward unread
8660                                           not-case-fold not-matching)
8661   "Return a list of all articles that match REGEXP on HEADER.
8662 The search stars on the current article and goes forwards unless
8663 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8664 If UNREAD is non-nil, only unread articles will
8665 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8666 in the comparisons. If NOT-MATCHING, return a list of all articles that
8667 not match REGEXP on HEADER."
8668   (let ((case-fold-search (not not-case-fold))
8669         articles d func)
8670     (if (consp header)
8671         (if (eq (car header) 'extra)
8672             (setq func
8673                   `(lambda (h)
8674                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8675                          "")))
8676           (error "%s is an invalid header" header))
8677       (unless (fboundp (intern (concat "mail-header-" header)))
8678         (error "%s is not a valid header" header))
8679       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8680     (dolist (d (if (eq backward 'all)
8681                    gnus-newsgroup-data
8682                  (gnus-data-find-list
8683                   (gnus-summary-article-number)
8684                   (gnus-data-list backward))))
8685       (when (and (or (not unread)       ; We want all articles...
8686                      (gnus-data-unread-p d)) ; Or just unreads.
8687                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8688                  (if not-matching
8689                      (not (string-match
8690                            regexp
8691                            (funcall func (gnus-data-header d))))
8692                    (string-match regexp
8693                                  (funcall func (gnus-data-header d)))))
8694         (push (gnus-data-number d) articles))) ; Success!
8695     (nreverse articles)))
8696
8697 (defun gnus-summary-execute-command (header regexp command &optional backward)
8698   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8699 If HEADER is an empty string (or nil), the match is done on the entire
8700 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8701   (interactive
8702    (list (let ((completion-ignore-case t))
8703            (completing-read
8704             "Header name: "
8705             (mapcar (lambda (header) (list (format "%s" header)))
8706                     (append
8707                      '("Number" "Subject" "From" "Lines" "Date"
8708                        "Message-ID" "Xref" "References" "Body")
8709                      gnus-extra-headers))
8710             nil 'require-match))
8711          (read-string "Regexp: ")
8712          (read-key-sequence "Command: ")
8713          current-prefix-arg))
8714   (when (equal header "Body")
8715     (setq header ""))
8716   ;; Hidden thread subtrees must be searched as well.
8717   (gnus-summary-show-all-threads)
8718   ;; We don't want to change current point nor window configuration.
8719   (save-excursion
8720     (save-window-excursion
8721       (let (gnus-visual
8722             gnus-treat-strip-trailing-blank-lines
8723             gnus-treat-strip-leading-blank-lines
8724             gnus-treat-strip-multiple-blank-lines
8725             gnus-treat-hide-boring-headers
8726             gnus-treat-fold-newsgroups
8727             gnus-article-prepare-hook)
8728         (gnus-message 6 "Executing %s..." (key-description command))
8729         ;; We'd like to execute COMMAND interactively so as to give arguments.
8730         (gnus-execute header regexp
8731                       `(call-interactively ',(key-binding command))
8732                       backward)
8733         (gnus-message 6 "Executing %s...done" (key-description command))))))
8734
8735 (defun gnus-summary-beginning-of-article ()
8736   "Scroll the article back to the beginning."
8737   (interactive)
8738   (gnus-summary-select-article)
8739   (gnus-configure-windows 'article)
8740   (gnus-eval-in-buffer-window gnus-article-buffer
8741     (widen)
8742     (goto-char (point-min))
8743     (when gnus-break-pages
8744       (gnus-narrow-to-page))))
8745
8746 (defun gnus-summary-end-of-article ()
8747   "Scroll to the end of the article."
8748   (interactive)
8749   (gnus-summary-select-article)
8750   (gnus-configure-windows 'article)
8751   (gnus-eval-in-buffer-window gnus-article-buffer
8752     (widen)
8753     (goto-char (point-max))
8754     (recenter -3)
8755     (when gnus-break-pages
8756       (when (re-search-backward page-delimiter nil t)
8757         (narrow-to-region (match-end 0) (point-max)))
8758       (gnus-narrow-to-page))))
8759
8760 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8761   "Truncate to LEN and quote all \"(\"'s in STRING."
8762   (gnus-replace-in-string (if (and len (> (length string) len))
8763                               (substring string 0 len)
8764                             string)
8765                           "[()]" "\\\\\\&"))
8766
8767 (defun gnus-summary-print-article (&optional filename n)
8768   "Generate and print a PostScript image of the process-marked (mail) articles.
8769
8770 If used interactively, print the current article if none are
8771 process-marked.  With prefix arg, prompt the user for the name of the
8772 file to save in.
8773
8774 When used from Lisp, accept two optional args FILENAME and N.  N means
8775 to print the next N articles.  If N is negative, print the N previous
8776 articles.  If N is nil and articles have been marked with the process
8777 mark, print these instead.
8778
8779 If the optional first argument FILENAME is nil, send the image to the
8780 printer.  If FILENAME is a string, save the PostScript image in a file with
8781 that name.  If FILENAME is a number, prompt the user for the name of the file
8782 to save in."
8783   (interactive (list (ps-print-preprint current-prefix-arg)))
8784   (dolist (article (gnus-summary-work-articles n))
8785     (gnus-summary-select-article nil nil 'pseudo article)
8786     (gnus-eval-in-buffer-window gnus-article-buffer
8787       (gnus-print-buffer))
8788     (gnus-summary-remove-process-mark article))
8789   (ps-despool filename))
8790
8791 (defun gnus-print-buffer ()
8792   (let ((buffer (generate-new-buffer " *print*")))
8793     (unwind-protect
8794         (progn
8795           (copy-to-buffer buffer (point-min) (point-max))
8796           (set-buffer buffer)
8797           (gnus-remove-text-with-property 'gnus-decoration)
8798           (when (gnus-visual-p 'article-highlight 'highlight)
8799             ;; Copy-to-buffer doesn't copy overlay.  So redo
8800             ;; highlight.
8801             (let ((gnus-article-buffer buffer))
8802               (gnus-article-highlight-citation t)
8803               (gnus-article-highlight-signature)
8804               (gnus-article-emphasize)
8805               (gnus-article-delete-invisible-text)))
8806           (let ((ps-left-header
8807                  (list
8808                   (concat "("
8809                           (gnus-summary-print-truncate-and-quote
8810                            (mail-header-subject gnus-current-headers)
8811                            66) ")")
8812                   (concat "("
8813                           (gnus-summary-print-truncate-and-quote
8814                            (mail-header-from gnus-current-headers)
8815                            45) ")")))
8816                 (ps-right-header
8817                  (list
8818                   "/pagenumberstring load"
8819                   (concat "("
8820                           (mail-header-date gnus-current-headers) ")"))))
8821             (gnus-run-hooks 'gnus-ps-print-hook)
8822             (save-excursion
8823               (if window-system
8824                   (ps-spool-buffer-with-faces)
8825                 (ps-spool-buffer)))))
8826       (kill-buffer buffer))))
8827
8828 (defun gnus-summary-show-article (&optional arg)
8829   "Force redisplaying of the current article.
8830 If ARG (the prefix) is a number, show the article with the charset
8831 defined in `gnus-summary-show-article-charset-alist', or the charset
8832 input.
8833 If ARG (the prefix) is non-nil and not a number, show the raw article
8834 without any article massaging functions being run.  Normally, the key
8835 strokes are `C-u g'."
8836   (interactive "P")
8837   (cond
8838    ((numberp arg)
8839     (gnus-summary-show-article t)
8840     (let* ((gnus-newsgroup-charset
8841             (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8842                 (mm-read-coding-system
8843                  "View as charset: " ;; actually it is coding system.
8844                  (save-excursion
8845                    (set-buffer gnus-article-buffer)
8846                    (mm-detect-coding-region (point) (point-max))))))
8847            (default-mime-charset gnus-newsgroup-charset)
8848            (gnus-newsgroup-ignored-charsets 'gnus-all))
8849       (gnus-summary-select-article nil 'force)
8850       (let ((deps gnus-newsgroup-dependencies)
8851             head header lines)
8852         (save-excursion
8853           (set-buffer gnus-original-article-buffer)
8854           (save-restriction
8855             (message-narrow-to-head)
8856             (setq head (buffer-string))
8857             (goto-char (point-min))
8858             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8859               (goto-char (point-max))
8860               (widen)
8861               (setq lines (1- (count-lines (point) (point-max))))))
8862           (with-temp-buffer
8863             (insert (format "211 %d Article retrieved.\n"
8864                             (cdr gnus-article-current)))
8865             (insert head)
8866             (if lines (insert (format "Lines: %d\n" lines)))
8867             (insert ".\n")
8868             (let ((nntp-server-buffer (current-buffer)))
8869               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8870         (gnus-data-set-header
8871          (gnus-data-find (cdr gnus-article-current))
8872          header)
8873         (gnus-summary-update-article-line
8874          (cdr gnus-article-current) header)
8875         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8876           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8877    ((not arg)
8878     ;; Select the article the normal way.
8879     (gnus-summary-select-article nil 'force))
8880    (t
8881     ;; We have to require this here to make sure that the following
8882     ;; dynamic binding isn't shadowed by autoloading.
8883     (require 'gnus-async)
8884     (require 'gnus-art)
8885     ;; Bind the article treatment functions to nil.
8886     (let ((gnus-have-all-headers t)
8887           gnus-article-prepare-hook
8888           gnus-article-decode-hook
8889           gnus-break-pages
8890           gnus-show-mime
8891           (gnus-inhibit-treatment t))
8892       (gnus-summary-select-article nil 'force))))
8893   (gnus-summary-goto-subject gnus-current-article)
8894   (gnus-summary-position-point))
8895
8896 (defun gnus-summary-show-raw-article ()
8897   "Show the raw article without any article massaging functions being run."
8898   (interactive)
8899   (gnus-summary-show-article t))
8900
8901 (defun gnus-summary-verbose-headers (&optional arg)
8902   "Toggle permanent full header display.
8903 If ARG is a positive number, turn header display on.
8904 If ARG is a negative number, turn header display off."
8905   (interactive "P")
8906   (setq gnus-show-all-headers
8907         (cond ((or (not (numberp arg))
8908                    (zerop arg))
8909                (not gnus-show-all-headers))
8910               ((natnump arg)
8911                t)))
8912   (gnus-summary-show-article))
8913
8914 (defun gnus-summary-toggle-header (&optional arg)
8915   "Show the headers if they are hidden, or hide them if they are shown.
8916 If ARG is a positive number, show the entire header.
8917 If ARG is a negative number, hide the unwanted header lines."
8918   (interactive "P")
8919   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8920                      (get-buffer-window gnus-article-buffer t))))
8921     (with-current-buffer gnus-article-buffer
8922       (widen)
8923       (article-narrow-to-head)
8924       (let* ((buffer-read-only nil)
8925              (inhibit-point-motion-hooks t)
8926              (hidden (if (numberp arg)
8927                          (>= arg 0)
8928                        (gnus-article-hidden-text-p 'headers)))
8929              s e)
8930         (delete-region (point-min) (point-max))
8931         (with-current-buffer gnus-original-article-buffer
8932           (goto-char (setq s (point-min)))
8933           (setq e (if (search-forward "\n\n" nil t)
8934                       (1- (point))
8935                     (point-max))))
8936         (insert-buffer-substring gnus-original-article-buffer s e)
8937         ;; In T-gnus, gnus-article-decode-hook doesn't contain
8938         ;; article-decode-encoded-words by default.
8939         (article-decode-encoded-words)
8940         (run-hooks 'gnus-article-decode-hook)
8941         (if hidden
8942             (let ((gnus-treat-hide-headers nil)
8943                   (gnus-treat-hide-boring-headers nil))
8944               (gnus-delete-wash-type 'headers)
8945               (gnus-treat-article 'head))
8946           (gnus-treat-article 'head))
8947         (widen)
8948         (if window
8949             (set-window-start window (goto-char (point-min))))
8950         (if gnus-break-pages
8951             (gnus-narrow-to-page)
8952           (when (gnus-visual-p 'page-marker)
8953             (let ((buffer-read-only nil))
8954               (gnus-remove-text-with-property 'gnus-prev)
8955               (gnus-remove-text-with-property 'gnus-next))))
8956         (gnus-set-mode-line 'article)))))
8957
8958 (defun gnus-summary-show-all-headers ()
8959   "Make all header lines visible."
8960   (interactive)
8961   (gnus-summary-toggle-header 1))
8962
8963 (defun gnus-summary-toggle-mime (&optional arg)
8964   "Toggle MIME processing.
8965 If ARG is a positive number, turn MIME processing on."
8966   (interactive "P")
8967   (setq gnus-show-mime
8968         (if (null arg)
8969             (not gnus-show-mime)
8970           (> (prefix-numeric-value arg) 0)))
8971   (gnus-summary-select-article t 'force))
8972
8973 (defun gnus-summary-caesar-message (&optional arg)
8974   "Caesar rotate the current article by 13.
8975 The numerical prefix specifies how many places to rotate each letter
8976 forward."
8977   (interactive "P")
8978   (gnus-summary-select-article)
8979   (let ((mail-header-separator ""))
8980     (gnus-eval-in-buffer-window gnus-article-buffer
8981       (save-restriction
8982         (widen)
8983         (let ((start (window-start))
8984               buffer-read-only)
8985           (message-caesar-buffer-body arg)
8986           (set-window-start (get-buffer-window (current-buffer)) start))))))
8987
8988 (autoload 'unmorse-region "morse"
8989   "Convert morse coded text in region to ordinary ASCII text."
8990   t)
8991
8992 (defun gnus-summary-morse-message (&optional arg)
8993   "Morse decode the current article."
8994   (interactive "P")
8995   (gnus-summary-select-article)
8996   (let ((mail-header-separator ""))
8997     (gnus-eval-in-buffer-window gnus-article-buffer
8998       (save-excursion
8999         (save-restriction
9000           (widen)
9001           (let ((pos (window-start))
9002                 buffer-read-only)
9003             (goto-char (point-min))
9004             (when (message-goto-body)
9005               (gnus-narrow-to-body))
9006             (goto-char (point-min))
9007             (while (re-search-forward "·" (point-max) t)
9008               (replace-match "."))
9009             (unmorse-region (point-min) (point-max))
9010             (widen)
9011             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9012
9013 (defun gnus-summary-stop-page-breaking ()
9014   "Stop page breaking in the current article."
9015   (interactive)
9016   (gnus-summary-select-article)
9017   (gnus-eval-in-buffer-window gnus-article-buffer
9018     (widen)
9019     (when (gnus-visual-p 'page-marker)
9020       (let ((buffer-read-only nil))
9021         (gnus-remove-text-with-property 'gnus-prev)
9022         (gnus-remove-text-with-property 'gnus-next))
9023       (setq gnus-page-broken nil))))
9024
9025 (defun gnus-summary-move-article (&optional n to-newsgroup
9026                                             select-method action)
9027   "Move the current article to a different newsgroup.
9028 If N is a positive number, move the N next articles.
9029 If N is a negative number, move the N previous articles.
9030 If N is nil and any articles have been marked with the process mark,
9031 move those articles instead.
9032 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9033 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9034 re-spool using this method.
9035
9036 When called interactively with TO-NEWSGROUP being nil, the value of
9037 the variable `gnus-move-split-methods' is used for finding a default
9038 for the target newsgroup.
9039
9040 For this function to work, both the current newsgroup and the
9041 newsgroup that you want to move to have to support the `request-move'
9042 and `request-accept' functions.
9043
9044 ACTION can be either `move' (the default), `crosspost' or `copy'."
9045   (interactive "P")
9046   (unless action
9047     (setq action 'move))
9048   ;; Check whether the source group supports the required functions.
9049   (cond ((and (eq action 'move)
9050               (not (gnus-check-backend-function
9051                     'request-move-article gnus-newsgroup-name)))
9052          (error "The current group does not support article moving"))
9053         ((and (eq action 'crosspost)
9054               (not (gnus-check-backend-function
9055                     'request-replace-article gnus-newsgroup-name)))
9056          (error "The current group does not support article editing")))
9057   (let ((articles (gnus-summary-work-articles n))
9058         (prefix (if (gnus-check-backend-function
9059                      'request-move-article gnus-newsgroup-name)
9060                     (funcall gnus-move-group-prefix-function
9061                              gnus-newsgroup-name)
9062                   ""))
9063         (names '((move "Move" "Moving")
9064                  (copy "Copy" "Copying")
9065                  (crosspost "Crosspost" "Crossposting")))
9066         (copy-buf (save-excursion
9067                     (nnheader-set-temp-buffer " *copy article*")))
9068         art-group to-method new-xref article to-groups)
9069     (unless (assq action names)
9070       (error "Unknown action %s" action))
9071     ;; Read the newsgroup name.
9072     (when (and (not to-newsgroup)
9073                (not select-method))
9074       (if (and gnus-move-split-methods
9075                (not
9076                 (and (memq gnus-current-article articles)
9077                      (gnus-buffer-live-p gnus-original-article-buffer))))
9078           ;; When `gnus-move-split-methods' is non-nil, we have to
9079           ;; select an article to give `gnus-read-move-group-name' an
9080           ;; opportunity to suggest an appropriate default.  However,
9081           ;; we needn't render or mark the article.
9082           (let ((gnus-display-mime-function nil)
9083                 (gnus-article-prepare-hook nil)
9084                 (gnus-mark-article-hook nil))
9085             (gnus-summary-select-article nil nil nil (car articles))))
9086       (setq to-newsgroup
9087             (gnus-read-move-group-name
9088              (cadr (assq action names))
9089              (symbol-value (intern (format "gnus-current-%s-group" action)))
9090              articles prefix))
9091       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
9092     (setq to-method (or select-method
9093                         (gnus-server-to-method
9094                          (gnus-group-method to-newsgroup))))
9095     ;; Check the method we are to move this article to...
9096     (unless (gnus-check-backend-function
9097              'request-accept-article (car to-method))
9098       (error "%s does not support article copying" (car to-method)))
9099     (unless (gnus-check-server to-method)
9100       (error "Can't open server %s" (car to-method)))
9101     (gnus-message 6 "%s to %s: %s..."
9102                   (caddr (assq action names))
9103                   (or (car select-method) to-newsgroup) articles)
9104     (while articles
9105       (setq article (pop articles))
9106       (setq
9107        art-group
9108        (cond
9109         ;; Move the article.
9110         ((eq action 'move)
9111          ;; Remove this article from future suppression.
9112          (gnus-dup-unsuppress-article article)
9113          (gnus-request-move-article
9114           article                       ; Article to move
9115           gnus-newsgroup-name           ; From newsgroup
9116           (nth 1 (gnus-find-method-for-group
9117                   gnus-newsgroup-name)) ; Server
9118           (list 'gnus-request-accept-article
9119                 to-newsgroup (list 'quote select-method)
9120                 (not articles) t)       ; Accept form
9121           (not articles)))              ; Only save nov last time
9122         ;; Copy the article.
9123         ((eq action 'copy)
9124          (save-excursion
9125            (set-buffer copy-buf)
9126            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9127              (gnus-request-accept-article
9128               to-newsgroup select-method (not articles) t))))
9129         ;; Crosspost the article.
9130         ((eq action 'crosspost)
9131          (let ((xref (message-tokenize-header
9132                       (mail-header-xref (gnus-summary-article-header article))
9133                       " ")))
9134            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9135                                   ":" (number-to-string article)))
9136            (unless xref
9137              (setq xref (list (system-name))))
9138            (setq new-xref
9139                  (concat
9140                   (mapconcat 'identity
9141                              (delete "Xref:" (delete new-xref xref))
9142                              " ")
9143                   " " new-xref))
9144            (save-excursion
9145              (set-buffer copy-buf)
9146              ;; First put the article in the destination group.
9147              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9148              (when (consp (setq art-group
9149                                 (gnus-request-accept-article
9150                                  to-newsgroup select-method (not articles))))
9151                (setq new-xref (concat new-xref " " (car art-group)
9152                                       ":"
9153                                       (number-to-string (cdr art-group))))
9154                ;; Now we have the new Xrefs header, so we insert
9155                ;; it and replace the new article.
9156                (nnheader-replace-header "Xref" new-xref)
9157                (gnus-request-replace-article
9158                 (cdr art-group) to-newsgroup (current-buffer))
9159                art-group))))))
9160       (cond
9161        ((not art-group)
9162         (gnus-message 1 "Couldn't %s article %s: %s"
9163                       (cadr (assq action names)) article
9164                       (nnheader-get-report (car to-method))))
9165        ((eq art-group 'junk)
9166         (when (eq action 'move)
9167           (gnus-summary-mark-article article gnus-canceled-mark)
9168           (gnus-message 4 "Deleted article %s" article)
9169           ;; run the delete hook
9170           (run-hook-with-args 'gnus-summary-article-delete-hook
9171                               action
9172                               (gnus-data-header
9173                                (assoc article (gnus-data-list nil)))
9174                               gnus-newsgroup-name nil
9175                               select-method)))
9176        (t
9177         (let* ((pto-group (gnus-group-prefixed-name
9178                            (car art-group) to-method))
9179                (info (gnus-get-info pto-group))
9180                (to-group (gnus-info-group info))
9181                to-marks)
9182           ;; Update the group that has been moved to.
9183           (when (and info
9184                      (memq action '(move copy)))
9185             (unless (member to-group to-groups)
9186               (push to-group to-groups))
9187
9188             (unless (memq article gnus-newsgroup-unreads)
9189               (push 'read to-marks)
9190               (gnus-info-set-read
9191                info (gnus-add-to-range (gnus-info-read info)
9192                                        (list (cdr art-group)))))
9193
9194             ;; See whether the article is to be put in the cache.
9195             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9196                              gnus-article-mark-lists
9197                            (delete '(expirable . expire)
9198                                    (copy-sequence gnus-article-mark-lists))))
9199                   (to-article (cdr art-group)))
9200
9201               ;; Enter the article into the cache in the new group,
9202               ;; if that is required.
9203               (when gnus-use-cache
9204                 (gnus-cache-possibly-enter-article
9205                  to-group to-article
9206                  (let ((header (copy-sequence
9207                                 (gnus-summary-article-header article))))
9208                    (mail-header-set-number header to-article)
9209                    header)
9210                  (memq article gnus-newsgroup-marked)
9211                  (memq article gnus-newsgroup-dormant)
9212                  (memq article gnus-newsgroup-unreads)))
9213
9214               (when gnus-preserve-marks
9215                 ;; Copy any marks over to the new group.
9216                 (when (and (equal to-group gnus-newsgroup-name)
9217                            (not (memq article gnus-newsgroup-unreads)))
9218                   ;; Mark this article as read in this group.
9219                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9220                   (setcdr (gnus-active to-group) to-article)
9221                   (setcdr gnus-newsgroup-active to-article))
9222
9223                 (while marks
9224                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9225                     (when (memq article (symbol-value
9226                                          (intern (format "gnus-newsgroup-%s"
9227                                                          (caar marks)))))
9228                       (push (cdar marks) to-marks)
9229                       ;; If the other group is the same as this group,
9230                       ;; then we have to add the mark to the list.
9231                       (when (equal to-group gnus-newsgroup-name)
9232                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9233                              (cons to-article
9234                                    (symbol-value
9235                                     (intern (format "gnus-newsgroup-%s"
9236                                                     (caar marks)))))))
9237                       ;; Copy the marks to other group.
9238                       (gnus-add-marked-articles
9239                        to-group (cdar marks) (list to-article) info)))
9240                   (setq marks (cdr marks)))
9241
9242                 (gnus-request-set-mark
9243                  to-group (list (list (list to-article) 'add to-marks))))
9244
9245               (gnus-dribble-enter
9246                (concat "(gnus-group-set-info '"
9247                        (gnus-prin1-to-string (gnus-get-info to-group))
9248                        ")"))))
9249
9250           ;; Update the Xref header in this article to point to
9251           ;; the new crossposted article we have just created.
9252           (when (eq action 'crosspost)
9253             (save-excursion
9254               (set-buffer copy-buf)
9255               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9256               (nnheader-replace-header "Xref" new-xref)
9257               (gnus-request-replace-article
9258                article gnus-newsgroup-name (current-buffer))))
9259
9260           ;; run the move/copy/crosspost/respool hook
9261           (run-hook-with-args 'gnus-summary-article-move-hook
9262                               action
9263                               (gnus-data-header
9264                                (assoc article (gnus-data-list nil)))
9265                               gnus-newsgroup-name
9266                               to-newsgroup
9267                               select-method))
9268
9269         ;;;!!!Why is this necessary?
9270         (set-buffer gnus-summary-buffer)
9271         
9272         (gnus-summary-goto-subject article)
9273         (when (eq action 'move)
9274           (gnus-summary-mark-article article gnus-canceled-mark))))
9275       (gnus-summary-remove-process-mark article))
9276     ;; Re-activate all groups that have been moved to.
9277     (save-excursion
9278       (set-buffer gnus-group-buffer)
9279       (let ((gnus-group-marked to-groups))
9280         (gnus-group-get-new-news-this-group nil t)))
9281
9282     (gnus-kill-buffer copy-buf)
9283     (gnus-summary-position-point)
9284     (gnus-set-mode-line 'summary)))
9285
9286 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9287   "Move the current article to a different newsgroup.
9288 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9289 When called interactively, if TO-NEWSGROUP is nil, use the value of
9290 the variable `gnus-move-split-methods' for finding a default target
9291 newsgroup.
9292 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9293 re-spool using this method."
9294   (interactive "P")
9295   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9296
9297 (defun gnus-summary-crosspost-article (&optional n)
9298   "Crosspost the current article to some other group."
9299   (interactive "P")
9300   (gnus-summary-move-article n nil nil 'crosspost))
9301
9302 (defcustom gnus-summary-respool-default-method nil
9303   "Default method type for respooling an article.
9304 If nil, use to the current newsgroup method."
9305   :type 'symbol
9306   :group 'gnus-summary-mail)
9307
9308 (defcustom gnus-summary-display-while-building nil
9309   "If non-nil, show and update the summary buffer as it's being built.
9310 If the value is t, update the buffer after every line is inserted.  If
9311 the value is an integer (N), update the display every N lines."
9312   :group 'gnus-thread
9313   :type '(choice (const :tag "off" nil)
9314                  number
9315                  (const :tag "frequently" t)))
9316
9317 (defun gnus-summary-respool-article (&optional n method)
9318   "Respool the current article.
9319 The article will be squeezed through the mail spooling process again,
9320 which means that it will be put in some mail newsgroup or other
9321 depending on `nnmail-split-methods'.
9322 If N is a positive number, respool the N next articles.
9323 If N is a negative number, respool the N previous articles.
9324 If N is nil and any articles have been marked with the process mark,
9325 respool those articles instead.
9326
9327 Respooling can be done both from mail groups and \"real\" newsgroups.
9328 In the former case, the articles in question will be moved from the
9329 current group into whatever groups they are destined to.  In the
9330 latter case, they will be copied into the relevant groups."
9331   (interactive
9332    (list current-prefix-arg
9333          (let* ((methods (gnus-methods-using 'respool))
9334                 (methname
9335                  (symbol-name (or gnus-summary-respool-default-method
9336                                   (car (gnus-find-method-for-group
9337                                         gnus-newsgroup-name)))))
9338                 (method
9339                  (gnus-completing-read-with-default
9340                   methname "What backend do you want to use when respooling?"
9341                   methods nil t nil 'gnus-mail-method-history))
9342                 ms)
9343            (cond
9344             ((zerop (length (setq ms (gnus-servers-using-backend
9345                                       (intern method)))))
9346              (list (intern method) ""))
9347             ((= 1 (length ms))
9348              (car ms))
9349             (t
9350              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9351                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9352                            ms-alist))))))))
9353   (unless method
9354     (error "No method given for respooling"))
9355   (if (assoc (symbol-name
9356               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9357              (gnus-methods-using 'respool))
9358       (gnus-summary-move-article n nil method)
9359     (gnus-summary-copy-article n nil method)))
9360
9361 (defun gnus-summary-import-article (file &optional edit)
9362   "Import an arbitrary file into a mail newsgroup."
9363   (interactive "fImport file: \nP")
9364   (let ((group gnus-newsgroup-name)
9365         (now (current-time))
9366         atts lines group-art)
9367     (unless (gnus-check-backend-function 'request-accept-article group)
9368       (error "%s does not support article importing" group))
9369     (or (file-readable-p file)
9370         (not (file-regular-p file))
9371         (error "Can't read %s" file))
9372     (save-excursion
9373       (set-buffer (gnus-get-buffer-create " *import file*"))
9374       (erase-buffer)
9375       (nnheader-insert-file-contents file)
9376       (goto-char (point-min))
9377       (if (nnheader-article-p)
9378           (save-restriction
9379             (goto-char (point-min))
9380             (search-forward "\n\n" nil t)
9381             (narrow-to-region (point-min) (1- (point)))
9382             (goto-char (point-min))
9383             (unless (re-search-forward "^date:" nil t)
9384               (goto-char (point-max))
9385               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9386         ;; This doesn't look like an article, so we fudge some headers.
9387         (setq atts (file-attributes file)
9388               lines (count-lines (point-min) (point-max)))
9389         (insert "From: " (read-string "From: ") "\n"
9390                 "Subject: " (read-string "Subject: ") "\n"
9391                 "Date: " (message-make-date (nth 5 atts)) "\n"
9392                 "Message-ID: " (message-make-message-id) "\n"
9393                 "Lines: " (int-to-string lines) "\n"
9394                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9395       (setq group-art (gnus-request-accept-article group nil t))
9396       (kill-buffer (current-buffer)))
9397     (setq gnus-newsgroup-active (gnus-activate-group group))
9398     (forward-line 1)
9399     (gnus-summary-goto-article (cdr group-art) nil t)
9400     (when edit
9401       (gnus-summary-edit-article))))
9402
9403 (defun gnus-summary-create-article ()
9404   "Create an article in a mail newsgroup."
9405   (interactive)
9406   (let ((group gnus-newsgroup-name)
9407         (now (current-time))
9408         group-art)
9409     (unless (gnus-check-backend-function 'request-accept-article group)
9410       (error "%s does not support article importing" group))
9411     (save-excursion
9412       (set-buffer (gnus-get-buffer-create " *import file*"))
9413       (erase-buffer)
9414       (goto-char (point-min))
9415       ;; This doesn't look like an article, so we fudge some headers.
9416       (insert "From: " (read-string "From: ") "\n"
9417               "Subject: " (read-string "Subject: ") "\n"
9418               "Date: " (message-make-date now) "\n"
9419               "Message-ID: " (message-make-message-id) "\n")
9420       (setq group-art (gnus-request-accept-article group nil t))
9421       (kill-buffer (current-buffer)))
9422     (setq gnus-newsgroup-active (gnus-activate-group group))
9423     (forward-line 1)
9424     (gnus-summary-goto-article (cdr group-art) nil t)
9425     (gnus-summary-edit-article)))
9426
9427 (defun gnus-summary-article-posted-p ()
9428   "Say whether the current (mail) article is available from news as well.
9429 This will be the case if the article has both been mailed and posted."
9430   (interactive)
9431   (let ((id (mail-header-references (gnus-summary-article-header)))
9432         (gnus-override-method (car (gnus-refer-article-methods))))
9433     (if (gnus-request-head id "")
9434         (gnus-message 2 "The current message was found on %s"
9435                       gnus-override-method)
9436       (gnus-message 2 "The current message couldn't be found on %s"
9437                     gnus-override-method)
9438       nil)))
9439
9440 (defun gnus-summary-expire-articles (&optional now)
9441   "Expire all articles that are marked as expirable in the current group."
9442   (interactive)
9443   (when (and (not gnus-group-is-exiting-without-update-p)
9444              (gnus-check-backend-function
9445               'request-expire-articles gnus-newsgroup-name))
9446     ;; This backend supports expiry.
9447     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9448            (expirable (if total
9449                           (progn
9450                             ;; We need to update the info for
9451                             ;; this group for `gnus-list-of-read-articles'
9452                             ;; to give us the right answer.
9453                             (gnus-run-hooks 'gnus-exit-group-hook)
9454                             (gnus-summary-update-info)
9455                             (gnus-list-of-read-articles gnus-newsgroup-name))
9456                         (setq gnus-newsgroup-expirable
9457                               (sort gnus-newsgroup-expirable '<))))
9458            (expiry-wait (if now 'immediate
9459                           (gnus-group-find-parameter
9460                            gnus-newsgroup-name 'expiry-wait)))
9461            (nnmail-expiry-target
9462             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9463                 nnmail-expiry-target))
9464            es)
9465       (when expirable
9466         ;; There are expirable articles in this group, so we run them
9467         ;; through the expiry process.
9468         (gnus-message 6 "Expiring articles...")
9469         (unless (gnus-check-group gnus-newsgroup-name)
9470           (error "Can't open server for %s" gnus-newsgroup-name))
9471         ;; The list of articles that weren't expired is returned.
9472         (save-excursion
9473           (if expiry-wait
9474               (let ((nnmail-expiry-wait-function nil)
9475                     (nnmail-expiry-wait expiry-wait))
9476                 (setq es (gnus-request-expire-articles
9477                           expirable gnus-newsgroup-name)))
9478             (setq es (gnus-request-expire-articles
9479                       expirable gnus-newsgroup-name)))
9480           (unless total
9481             (setq gnus-newsgroup-expirable es))
9482           ;; We go through the old list of expirable, and mark all
9483           ;; really expired articles as nonexistent.
9484           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
9485             (let ((gnus-use-cache nil))
9486               (dolist (article expirable)
9487                 (when (and (not (memq article es))
9488                            (gnus-data-find article))
9489                   (gnus-summary-mark-article article gnus-canceled-mark)
9490                   (run-hook-with-args 'gnus-summary-article-expire-hook
9491                                       'delete
9492                                       (gnus-data-header
9493                                        (assoc article (gnus-data-list nil)))
9494                                       gnus-newsgroup-name
9495                                       nil
9496                                       nil))))))
9497         (gnus-message 6 "Expiring articles...done")))))
9498
9499 (defun gnus-summary-expire-articles-now ()
9500   "Expunge all expirable articles in the current group.
9501 This means that *all* articles that are marked as expirable will be
9502 deleted forever, right now."
9503   (interactive)
9504   (or gnus-expert-user
9505       (gnus-yes-or-no-p
9506        "Are you really, really, really sure you want to delete all these messages? ")
9507       (error "Phew!"))
9508   (gnus-summary-expire-articles t))
9509
9510 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9511 (defun gnus-summary-delete-article (&optional n)
9512   "Delete the N next (mail) articles.
9513 This command actually deletes articles.  This is not a marking
9514 command.  The article will disappear forever from your life, never to
9515 return.
9516
9517 If N is negative, delete backwards.
9518 If N is nil and articles have been marked with the process mark,
9519 delete these instead.
9520
9521 If `gnus-novice-user' is non-nil you will be asked for
9522 confirmation before the articles are deleted."
9523   (interactive "P")
9524   (unless (gnus-check-backend-function 'request-expire-articles
9525                                        gnus-newsgroup-name)
9526     (error "The current newsgroup does not support article deletion"))
9527   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9528     (error "Couldn't open server"))
9529   ;; Compute the list of articles to delete.
9530   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9531         (nnmail-expiry-target 'delete)
9532         not-deleted)
9533     (if (and gnus-novice-user
9534              (not (gnus-yes-or-no-p
9535                    (format "Do you really want to delete %s forever? "
9536                            (if (> (length articles) 1)
9537                                (format "these %s articles" (length articles))
9538                              "this article")))))
9539         ()
9540       ;; Delete the articles.
9541       (setq not-deleted (gnus-request-expire-articles
9542                          articles gnus-newsgroup-name 'force))
9543       (while articles
9544         (gnus-summary-remove-process-mark (car articles))
9545         ;; The backend might not have been able to delete the article
9546         ;; after all.
9547         (unless (memq (car articles) not-deleted)
9548           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9549         (let* ((article (car articles))
9550                (id (mail-header-id (gnus-data-header
9551                                     (assoc article (gnus-data-list nil))))))
9552           (run-hook-with-args 'gnus-summary-article-delete-hook
9553                               'delete id gnus-newsgroup-name nil
9554                               nil))
9555         (setq articles (cdr articles)))
9556       (when not-deleted
9557         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9558     (gnus-summary-position-point)
9559     (gnus-set-mode-line 'summary)
9560     not-deleted))
9561
9562 (defun gnus-summary-edit-article (&optional force)
9563   "Edit the current article.
9564 This will have permanent effect only in mail groups.
9565 If FORCE is non-nil, allow editing of articles even in read-only
9566 groups."
9567   (interactive "P")
9568   (save-excursion
9569     (set-buffer gnus-summary-buffer)
9570     (let ((mail-parse-charset gnus-newsgroup-charset)
9571           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9572       (gnus-set-global-variables)
9573       (when (and (not force)
9574                  (gnus-group-read-only-p))
9575         (error "The current newsgroup does not support article editing"))
9576       (gnus-summary-show-article t)
9577       (gnus-article-edit-article
9578        'ignore
9579        `(lambda (no-highlight)
9580           (let ((mail-parse-charset ',gnus-newsgroup-charset)
9581                 (message-options message-options)
9582                 (message-options-set-recipient)
9583                 (mail-parse-ignored-charsets
9584                  ',gnus-newsgroup-ignored-charsets))
9585             (gnus-summary-edit-article-done
9586              ,(or (mail-header-references gnus-current-headers) "")
9587              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
9588
9589 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9590
9591 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9592                                                  no-highlight)
9593   "Make edits to the current article permanent."
9594   (interactive)
9595   (save-excursion
9596     ;; The buffer restriction contains the entire article if it exists.
9597     (when (article-goto-body)
9598       (let ((lines (count-lines (point) (point-max)))
9599             (length (- (point-max) (point)))
9600             (case-fold-search t)
9601             (body (copy-marker (point))))
9602         (goto-char (point-min))
9603         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9604           (delete-region (match-beginning 1) (match-end 1))
9605           (insert (number-to-string length)))
9606         (goto-char (point-min))
9607         (when (re-search-forward
9608                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9609           (delete-region (match-beginning 1) (match-end 1))
9610           (insert (number-to-string length)))
9611         (goto-char (point-min))
9612         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9613           (delete-region (match-beginning 1) (match-end 1))
9614           (insert (number-to-string lines))))))
9615   ;; Replace the article.
9616   (let ((buf (current-buffer)))
9617     (with-temp-buffer
9618       (insert-buffer-substring buf)
9619
9620       (if (and (not read-only)
9621                (not (gnus-request-replace-article
9622                      (cdr gnus-article-current) (car gnus-article-current)
9623                      (current-buffer) t)))
9624           (error "Couldn't replace article")
9625         ;; Update the summary buffer.
9626         (if (and references
9627                  (equal (message-tokenize-header references " ")
9628                         (message-tokenize-header
9629                          (or (message-fetch-field "references") "") " ")))
9630             ;; We only have to update this line.
9631             (save-excursion
9632               (save-restriction
9633                 (message-narrow-to-head)
9634                 (let ((head (buffer-string))
9635                       header)
9636                   (with-temp-buffer
9637                     (insert (format "211 %d Article retrieved.\n"
9638                                     (cdr gnus-article-current)))
9639                     (insert head)
9640                     (insert ".\n")
9641                     (let ((nntp-server-buffer (current-buffer)))
9642                       (setq header (car (gnus-get-newsgroup-headers
9643                                          nil t))))
9644                     (save-excursion
9645                       (set-buffer gnus-summary-buffer)
9646                       (gnus-data-set-header
9647                        (gnus-data-find (cdr gnus-article-current))
9648                        header)
9649                       (gnus-summary-update-article-line
9650                        (cdr gnus-article-current) header)
9651                       (if (gnus-summary-goto-subject
9652                            (cdr gnus-article-current) nil t)
9653                           (gnus-summary-update-secondary-mark
9654                            (cdr gnus-article-current))))))))
9655           ;; Update threads.
9656           (set-buffer (or buffer gnus-summary-buffer))
9657           (gnus-summary-update-article (cdr gnus-article-current))
9658           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9659               (gnus-summary-update-secondary-mark
9660                (cdr gnus-article-current))))
9661         ;; Prettify the article buffer again.
9662         (unless no-highlight
9663           (save-excursion
9664             (set-buffer gnus-article-buffer)
9665             ;;;!!! Fix this -- article should be rehighlighted.
9666             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9667             (set-buffer gnus-original-article-buffer)
9668             (gnus-request-article
9669              (cdr gnus-article-current)
9670              (car gnus-article-current) (current-buffer))))
9671         ;; Prettify the summary buffer line.
9672         (when (gnus-visual-p 'summary-highlight 'highlight)
9673           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9674
9675 (defun gnus-summary-edit-wash (key)
9676   "Perform editing command KEY in the article buffer."
9677   (interactive
9678    (list
9679     (progn
9680       (message "%s" (concat (this-command-keys) "- "))
9681       (read-char))))
9682   (message "")
9683   (gnus-summary-edit-article)
9684   (execute-kbd-macro (concat (this-command-keys) key))
9685   (gnus-article-edit-done))
9686
9687 ;;; Respooling
9688
9689 (defun gnus-summary-respool-query (&optional silent trace)
9690   "Query where the respool algorithm would put this article."
9691   (interactive)
9692   (let (gnus-mark-article-hook)
9693     (gnus-summary-select-article)
9694     (save-excursion
9695       (set-buffer gnus-original-article-buffer)
9696       (let ((groups (nnmail-article-group 'identity trace)))
9697         (unless silent
9698           (if groups
9699               (message "This message would go to %s"
9700                        (mapconcat 'car groups ", "))
9701             (message "This message would go to no groups"))
9702           groups)))))
9703
9704 (defun gnus-summary-respool-trace ()
9705   "Trace where the respool algorithm would put this article.
9706 Display a buffer showing all fancy splitting patterns which matched."
9707   (interactive)
9708   (gnus-summary-respool-query nil t))
9709
9710 ;; Summary marking commands.
9711
9712 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9713   "Mark articles which has the same subject as read, and then select the next.
9714 If UNMARK is positive, remove any kind of mark.
9715 If UNMARK is negative, tick articles."
9716   (interactive "P")
9717   (when unmark
9718     (setq unmark (prefix-numeric-value unmark)))
9719   (let ((count
9720          (gnus-summary-mark-same-subject
9721           (gnus-summary-article-subject) unmark)))
9722     ;; Select next unread article.  If auto-select-same mode, should
9723     ;; select the first unread article.
9724     (gnus-summary-next-article t (and gnus-auto-select-same
9725                                       (gnus-summary-article-subject)))
9726     (gnus-message 7 "%d article%s marked as %s"
9727                   count (if (= count 1) " is" "s are")
9728                   (if unmark "unread" "read"))))
9729
9730 (defun gnus-summary-kill-same-subject (&optional unmark)
9731   "Mark articles which has the same subject as read.
9732 If UNMARK is positive, remove any kind of mark.
9733 If UNMARK is negative, tick articles."
9734   (interactive "P")
9735   (when unmark
9736     (setq unmark (prefix-numeric-value unmark)))
9737   (let ((count
9738          (gnus-summary-mark-same-subject
9739           (gnus-summary-article-subject) unmark)))
9740     ;; If marked as read, go to next unread subject.
9741     (when (null unmark)
9742       ;; Go to next unread subject.
9743       (gnus-summary-next-subject 1 t))
9744     (gnus-message 7 "%d articles are marked as %s"
9745                   count (if unmark "unread" "read"))))
9746
9747 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9748   "Mark articles with same SUBJECT as read, and return marked number.
9749 If optional argument UNMARK is positive, remove any kinds of marks.
9750 If optional argument UNMARK is negative, mark articles as unread instead."
9751   (let ((count 1))
9752     (save-excursion
9753       (cond
9754        ((null unmark)                   ; Mark as read.
9755         (while (and
9756                 (progn
9757                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9758                   (gnus-summary-show-thread) t)
9759                 (gnus-summary-find-subject subject))
9760           (setq count (1+ count))))
9761        ((> unmark 0)                    ; Tick.
9762         (while (and
9763                 (progn
9764                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9765                   (gnus-summary-show-thread) t)
9766                 (gnus-summary-find-subject subject))
9767           (setq count (1+ count))))
9768        (t                               ; Mark as unread.
9769         (while (and
9770                 (progn
9771                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9772                   (gnus-summary-show-thread) t)
9773                 (gnus-summary-find-subject subject))
9774           (setq count (1+ count)))))
9775       (gnus-set-mode-line 'summary)
9776       ;; Return the number of marked articles.
9777       count)))
9778
9779 (defun gnus-summary-mark-as-processable (n &optional unmark)
9780   "Set the process mark on the next N articles.
9781 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9782 the process mark instead.  The difference between N and the actual
9783 number of articles marked is returned."
9784   (interactive "P")
9785   (if (and (null n) (gnus-region-active-p))
9786       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9787     (setq n (prefix-numeric-value n))
9788     (let ((backward (< n 0))
9789           (n (abs n)))
9790       (while (and
9791               (> n 0)
9792               (if unmark
9793                   (gnus-summary-remove-process-mark
9794                    (gnus-summary-article-number))
9795                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9796               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9797         (setq n (1- n)))
9798       (when (/= 0 n)
9799         (gnus-message 7 "No more articles"))
9800       (gnus-summary-recenter)
9801       (gnus-summary-position-point)
9802       n)))
9803
9804 (defun gnus-summary-unmark-as-processable (n)
9805   "Remove the process mark from the next N articles.
9806 If N is negative, unmark backward instead.  The difference between N and
9807 the actual number of articles unmarked is returned."
9808   (interactive "P")
9809   (gnus-summary-mark-as-processable n t))
9810
9811 (defun gnus-summary-unmark-all-processable ()
9812   "Remove the process mark from all articles."
9813   (interactive)
9814   (save-excursion
9815     (while gnus-newsgroup-processable
9816       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9817   (gnus-summary-position-point))
9818
9819 (defun gnus-summary-add-mark (article type)
9820   "Mark ARTICLE with a mark of TYPE."
9821   (let ((vtype (car (assq type gnus-article-mark-lists)))
9822         var)
9823     (if (not vtype)
9824         (error "No such mark type: %s" type)
9825       (setq var (intern (format "gnus-newsgroup-%s" type)))
9826       (set var (cons article (symbol-value var)))
9827       (if (memq type '(processable cached replied forwarded recent saved))
9828           (gnus-summary-update-secondary-mark article)
9829         ;;; !!! This is bogus.  We should find out what primary
9830         ;;; !!! mark we want to set.
9831         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9832
9833 (defun gnus-summary-mark-as-expirable (n)
9834   "Mark N articles forward as expirable.
9835 If N is negative, mark backward instead.  The difference between N and
9836 the actual number of articles marked is returned."
9837   (interactive "p")
9838   (gnus-summary-mark-forward n gnus-expirable-mark))
9839
9840 (defun gnus-summary-mark-as-spam (n)
9841   "Mark N articles forward as spam.
9842 If N is negative, mark backward instead.  The difference between N and
9843 the actual number of articles marked is returned."
9844   (interactive "p")
9845   (gnus-summary-mark-forward n gnus-spam-mark))
9846
9847 (defun gnus-summary-mark-article-as-replied (article)
9848   "Mark ARTICLE as replied to and update the summary line.
9849 ARTICLE can also be a list of articles."
9850   (interactive (list (gnus-summary-article-number)))
9851   (let ((articles (if (listp article) article (list article))))
9852     (dolist (article articles)
9853       (unless (numberp article)
9854         (error "%s is not a number" article))
9855       (push article gnus-newsgroup-replied)
9856       (let ((buffer-read-only nil))
9857         (when (gnus-summary-goto-subject article nil t)
9858           (gnus-summary-update-secondary-mark article))))))
9859
9860 (defun gnus-summary-mark-article-as-forwarded (article)
9861   "Mark ARTICLE as forwarded and update the summary line.
9862 ARTICLE can also be a list of articles."
9863   (let ((articles (if (listp article) article (list article))))
9864     (dolist (article articles)
9865       (push article gnus-newsgroup-forwarded)
9866       (let ((buffer-read-only nil))
9867         (when (gnus-summary-goto-subject article nil t)
9868           (gnus-summary-update-secondary-mark article))))))
9869
9870 (defun gnus-summary-set-bookmark (article)
9871   "Set a bookmark in current article."
9872   (interactive (list (gnus-summary-article-number)))
9873   (when (or (not (get-buffer gnus-article-buffer))
9874             (not gnus-current-article)
9875             (not gnus-article-current)
9876             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9877     (error "No current article selected"))
9878   ;; Remove old bookmark, if one exists.
9879   (gnus-pull article gnus-newsgroup-bookmarks)
9880   ;; Set the new bookmark, which is on the form
9881   ;; (article-number . line-number-in-body).
9882   (push
9883    (cons article
9884          (save-excursion
9885            (set-buffer gnus-article-buffer)
9886            (count-lines
9887             (min (point)
9888                  (save-excursion
9889                    (article-goto-body)
9890                    (point)))
9891             (point))))
9892    gnus-newsgroup-bookmarks)
9893   (gnus-message 6 "A bookmark has been added to the current article."))
9894
9895 (defun gnus-summary-remove-bookmark (article)
9896   "Remove the bookmark from the current article."
9897   (interactive (list (gnus-summary-article-number)))
9898   ;; Remove old bookmark, if one exists.
9899   (if (not (assq article gnus-newsgroup-bookmarks))
9900       (gnus-message 6 "No bookmark in current article.")
9901     (gnus-pull article gnus-newsgroup-bookmarks)
9902     (gnus-message 6 "Removed bookmark.")))
9903
9904 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9905 (defun gnus-summary-mark-as-dormant (n)
9906   "Mark N articles forward as dormant.
9907 If N is negative, mark backward instead.  The difference between N and
9908 the actual number of articles marked is returned."
9909   (interactive "p")
9910   (gnus-summary-mark-forward n gnus-dormant-mark))
9911
9912 (defun gnus-summary-set-process-mark (article)
9913   "Set the process mark on ARTICLE and update the summary line."
9914   (setq gnus-newsgroup-processable
9915         (cons article
9916               (delq article gnus-newsgroup-processable)))
9917   (when (gnus-summary-goto-subject article)
9918     (gnus-summary-show-thread)
9919     (gnus-summary-goto-subject article)
9920     (gnus-summary-update-secondary-mark article)))
9921
9922 (defun gnus-summary-remove-process-mark (article)
9923   "Remove the process mark from ARTICLE and update the summary line."
9924   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9925   (when (gnus-summary-goto-subject article)
9926     (gnus-summary-show-thread)
9927     (gnus-summary-goto-subject article)
9928     (gnus-summary-update-secondary-mark article)))
9929
9930 (defun gnus-summary-set-saved-mark (article)
9931   "Set the process mark on ARTICLE and update the summary line."
9932   (push article gnus-newsgroup-saved)
9933   (when (gnus-summary-goto-subject article)
9934     (gnus-summary-update-secondary-mark article)))
9935
9936 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9937   "Mark N articles as read forwards.
9938 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9939 The difference between N and the actual number of articles marked is
9940 returned.
9941 If NO-EXPIRE, auto-expiry will be inhibited."
9942   (interactive "p")
9943   (gnus-summary-show-thread)
9944   (let ((backward (< n 0))
9945         (gnus-summary-goto-unread
9946          (and gnus-summary-goto-unread
9947               (not (eq gnus-summary-goto-unread 'never))
9948               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9949                                     gnus-ticked-mark gnus-dormant-mark)))))
9950         (n (abs n))
9951         (mark (or mark gnus-del-mark)))
9952     (while (and (> n 0)
9953                 (gnus-summary-mark-article nil mark no-expire)
9954                 (zerop (gnus-summary-next-subject
9955                         (if backward -1 1)
9956                         (and gnus-summary-goto-unread
9957                              (not (eq gnus-summary-goto-unread 'never)))
9958                         t)))
9959       (setq n (1- n)))
9960     (when (/= 0 n)
9961       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9962     (gnus-summary-recenter)
9963     (gnus-summary-position-point)
9964     (gnus-set-mode-line 'summary)
9965     n))
9966
9967 (defun gnus-summary-mark-article-as-read (mark)
9968   "Mark the current article quickly as read with MARK."
9969   (let ((article (gnus-summary-article-number)))
9970     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9971     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9972     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9973     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9974     (push (cons article mark) gnus-newsgroup-reads)
9975     ;; Possibly remove from cache, if that is used.
9976     (when gnus-use-cache
9977       (gnus-cache-enter-remove-article article))
9978     ;; Allow the backend to change the mark.
9979     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9980     ;; Check for auto-expiry.
9981     (when (and gnus-newsgroup-auto-expire
9982                (memq mark gnus-auto-expirable-marks))
9983       (setq mark gnus-expirable-mark)
9984       ;; Let the backend know about the mark change.
9985       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9986       (push article gnus-newsgroup-expirable))
9987     ;; Set the mark in the buffer.
9988     (gnus-summary-update-mark mark 'unread)
9989     t))
9990
9991 (defun gnus-summary-mark-article-as-unread (mark)
9992   "Mark the current article quickly as unread with MARK."
9993   (let* ((article (gnus-summary-article-number))
9994          (old-mark (gnus-summary-article-mark article)))
9995     ;; Allow the backend to change the mark.
9996     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9997     (if (eq mark old-mark)
9998         t
9999       (if (<= article 0)
10000           (progn
10001             (gnus-error 1 "Can't mark negative article numbers")
10002             nil)
10003         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10004         (setq gnus-newsgroup-spam-marked
10005               (delq article gnus-newsgroup-spam-marked))
10006         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10007         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10008         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10009         (cond ((= mark gnus-ticked-mark)
10010                (setq gnus-newsgroup-marked
10011                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10012                                               article)))
10013               ((= mark gnus-spam-mark)
10014                (setq gnus-newsgroup-spam-marked
10015                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10016                                               article)))
10017               ((= mark gnus-dormant-mark)
10018                (setq gnus-newsgroup-dormant
10019                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10020                                               article)))
10021               (t
10022                (setq gnus-newsgroup-unreads
10023                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10024                                               article))))
10025         (gnus-pull article gnus-newsgroup-reads)
10026
10027         ;; See whether the article is to be put in the cache.
10028         (and gnus-use-cache
10029              (vectorp (gnus-summary-article-header article))
10030              (save-excursion
10031                (gnus-cache-possibly-enter-article
10032                 gnus-newsgroup-name article
10033                 (gnus-summary-article-header article)
10034                 (= mark gnus-ticked-mark)
10035                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10036
10037         ;; Fix the mark.
10038         (gnus-summary-update-mark mark 'unread)
10039         t))))
10040
10041 (defun gnus-summary-mark-article (&optional article mark no-expire)
10042   "Mark ARTICLE with MARK.  MARK can be any character.
10043 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10044 `??' (dormant) and `?E' (expirable).
10045 If MARK is nil, then the default character `?r' is used.
10046 If ARTICLE is nil, then the article on the current line will be
10047 marked.
10048 If NO-EXPIRE, auto-expiry will be inhibited."
10049   ;; The mark might be a string.
10050   (when (stringp mark)
10051     (setq mark (aref mark 0)))
10052   ;; If no mark is given, then we check auto-expiring.
10053   (when (null mark)
10054     (setq mark gnus-del-mark))
10055   (when (and (not no-expire)
10056              gnus-newsgroup-auto-expire
10057              (memq mark gnus-auto-expirable-marks))
10058     (setq mark gnus-expirable-mark))
10059   (let ((article (or article (gnus-summary-article-number)))
10060         (old-mark (gnus-summary-article-mark article)))
10061     ;; Allow the backend to change the mark.
10062     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10063     (if (eq mark old-mark)
10064         t
10065       (unless article
10066         (error "No article on current line"))
10067       (if (not (if (or (= mark gnus-unread-mark)
10068                        (= mark gnus-ticked-mark)
10069                        (= mark gnus-spam-mark)
10070                        (= mark gnus-dormant-mark))
10071                    (gnus-mark-article-as-unread article mark)
10072                  (gnus-mark-article-as-read article mark)))
10073           t
10074         ;; See whether the article is to be put in the cache.
10075         (and gnus-use-cache
10076              (not (= mark gnus-canceled-mark))
10077              (vectorp (gnus-summary-article-header article))
10078              (save-excursion
10079                (gnus-cache-possibly-enter-article
10080                 gnus-newsgroup-name article
10081                 (gnus-summary-article-header article)
10082                 (= mark gnus-ticked-mark)
10083                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10084
10085         (when (gnus-summary-goto-subject article nil t)
10086           (let ((buffer-read-only nil))
10087             (gnus-summary-show-thread)
10088             ;; Fix the mark.
10089             (gnus-summary-update-mark mark 'unread)
10090             t))))))
10091
10092 (defun gnus-summary-update-secondary-mark (article)
10093   "Update the secondary (read, process, cache) mark."
10094   (gnus-summary-update-mark
10095    (cond ((memq article gnus-newsgroup-processable)
10096           gnus-process-mark)
10097          ((memq article gnus-newsgroup-cached)
10098           gnus-cached-mark)
10099          ((memq article gnus-newsgroup-replied)
10100           gnus-replied-mark)
10101          ((memq article gnus-newsgroup-forwarded)
10102           gnus-forwarded-mark)
10103          ((memq article gnus-newsgroup-saved)
10104           gnus-saved-mark)
10105          ((memq article gnus-newsgroup-recent)
10106           gnus-recent-mark)
10107          ((memq article gnus-newsgroup-unseen)
10108           gnus-unseen-mark)
10109          (t gnus-no-mark))
10110    'replied)
10111   (when (gnus-visual-p 'summary-highlight 'highlight)
10112     (gnus-run-hooks 'gnus-summary-update-hook))
10113   t)
10114
10115 (defun gnus-summary-update-download-mark (article)
10116   "Update the download mark."
10117   (gnus-summary-update-mark
10118    (cond ((memq article gnus-newsgroup-undownloaded)
10119           gnus-undownloaded-mark)
10120          (gnus-newsgroup-agentized
10121           gnus-downloaded-mark)
10122          (t
10123           gnus-no-mark))
10124    'download)
10125   (gnus-summary-update-line t)
10126   t)
10127
10128 (defun gnus-summary-update-mark (mark type)
10129   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10130         (buffer-read-only nil))
10131     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10132     (when forward
10133       (when (looking-at "\r")
10134         (incf forward))
10135       (when (<= (+ forward (point)) (point-max))
10136         ;; Go to the right position on the line.
10137         (goto-char (+ forward (point)))
10138         ;; Replace the old mark with the new mark.
10139         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10140         ;; Optionally update the marks by some user rule.
10141         (when (eq type 'unread)
10142           (gnus-data-set-mark
10143            (gnus-data-find (gnus-summary-article-number)) mark)
10144           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10145
10146 (defun gnus-mark-article-as-read (article &optional mark)
10147   "Enter ARTICLE in the pertinent lists and remove it from others."
10148   ;; Make the article expirable.
10149   (let ((mark (or mark gnus-del-mark)))
10150     (setq gnus-newsgroup-expirable
10151           (if (= mark gnus-expirable-mark)
10152               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10153             (delq article gnus-newsgroup-expirable)))
10154     ;; Remove from unread and marked lists.
10155     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10156     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10157     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10158     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10159     (push (cons article mark) gnus-newsgroup-reads)
10160     ;; Possibly remove from cache, if that is used.
10161     (when gnus-use-cache
10162       (gnus-cache-enter-remove-article article))
10163     t))
10164
10165 (defun gnus-mark-article-as-unread (article &optional mark)
10166   "Enter ARTICLE in the pertinent lists and remove it from others."
10167   (let ((mark (or mark gnus-ticked-mark)))
10168     (if (<= article 0)
10169         (progn
10170           (gnus-error 1 "Can't mark negative article numbers")
10171           nil)
10172       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10173             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10174             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10175             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10176             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10177
10178       ;; Unsuppress duplicates?
10179       (when gnus-suppress-duplicates
10180         (gnus-dup-unsuppress-article article))
10181
10182       (cond ((= mark gnus-ticked-mark)
10183              (setq gnus-newsgroup-marked
10184                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10185             ((= mark gnus-spam-mark)
10186              (setq gnus-newsgroup-spam-marked
10187                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10188                                             article)))
10189             ((= mark gnus-dormant-mark)
10190              (setq gnus-newsgroup-dormant
10191                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10192             (t
10193              (setq gnus-newsgroup-unreads
10194                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10195       (gnus-pull article gnus-newsgroup-reads)
10196       t)))
10197
10198 (defalias 'gnus-summary-mark-as-unread-forward
10199   'gnus-summary-tick-article-forward)
10200 (make-obsolete 'gnus-summary-mark-as-unread-forward
10201                'gnus-summary-tick-article-forward)
10202 (defun gnus-summary-tick-article-forward (n)
10203   "Tick N articles forwards.
10204 If N is negative, tick backwards instead.
10205 The difference between N and the number of articles ticked is returned."
10206   (interactive "p")
10207   (gnus-summary-mark-forward n gnus-ticked-mark))
10208
10209 (defalias 'gnus-summary-mark-as-unread-backward
10210   'gnus-summary-tick-article-backward)
10211 (make-obsolete 'gnus-summary-mark-as-unread-backward
10212                'gnus-summary-tick-article-backward)
10213 (defun gnus-summary-tick-article-backward (n)
10214   "Tick N articles backwards.
10215 The difference between N and the number of articles ticked is returned."
10216   (interactive "p")
10217   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10218
10219 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10220 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10221 (defun gnus-summary-tick-article (&optional article clear-mark)
10222   "Mark current article as unread.
10223 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10224 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10225   (interactive)
10226   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10227                                        gnus-ticked-mark)))
10228
10229 (defun gnus-summary-mark-as-read-forward (n)
10230   "Mark N articles as read forwards.
10231 If N is negative, mark backwards instead.
10232 The difference between N and the actual number of articles marked is
10233 returned."
10234   (interactive "p")
10235   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10236
10237 (defun gnus-summary-mark-as-read-backward (n)
10238   "Mark the N articles as read backwards.
10239 The difference between N and the actual number of articles marked is
10240 returned."
10241   (interactive "p")
10242   (gnus-summary-mark-forward
10243    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10244
10245 (defun gnus-summary-mark-as-read (&optional article mark)
10246   "Mark current article as read.
10247 ARTICLE specifies the article to be marked as read.
10248 MARK specifies a string to be inserted at the beginning of the line."
10249   (gnus-summary-mark-article article mark))
10250
10251 (defun gnus-summary-clear-mark-forward (n)
10252   "Clear marks from N articles forward.
10253 If N is negative, clear backward instead.
10254 The difference between N and the number of marks cleared is returned."
10255   (interactive "p")
10256   (gnus-summary-mark-forward n gnus-unread-mark))
10257
10258 (defun gnus-summary-clear-mark-backward (n)
10259   "Clear marks from N articles backward.
10260 The difference between N and the number of marks cleared is returned."
10261   (interactive "p")
10262   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10263
10264 (defun gnus-summary-mark-unread-as-read ()
10265   "Intended to be used by `gnus-summary-mark-article-hook'."
10266   (when (memq gnus-current-article gnus-newsgroup-unreads)
10267     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10268
10269 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10270   "Intended to be used by `gnus-summary-mark-article-hook'."
10271   (let ((mark (gnus-summary-article-mark)))
10272     (when (or (gnus-unread-mark-p mark)
10273               (gnus-read-mark-p mark))
10274       (gnus-summary-mark-article gnus-current-article
10275                                  (or new-mark gnus-read-mark)))))
10276
10277 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10278   "Intended to be used by `gnus-summary-mark-article-hook'."
10279   (let ((mark (gnus-summary-article-mark)))
10280     (when (or (gnus-unread-mark-p mark)
10281               (gnus-read-mark-p mark))
10282       (gnus-summary-mark-article (gnus-summary-article-number)
10283                                  (or new-mark gnus-read-mark)))))
10284
10285 (defun gnus-summary-mark-unread-as-ticked ()
10286   "Intended to be used by `gnus-summary-mark-article-hook'."
10287   (when (memq gnus-current-article gnus-newsgroup-unreads)
10288     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10289
10290 (defun gnus-summary-mark-region-as-read (point mark all)
10291   "Mark all unread articles between point and mark as read.
10292 If given a prefix, mark all articles between point and mark as read,
10293 even ticked and dormant ones."
10294   (interactive "r\nP")
10295   (save-excursion
10296     (let (article)
10297       (goto-char point)
10298       (beginning-of-line)
10299       (while (and
10300               (< (point) mark)
10301               (progn
10302                 (when (or all
10303                           (memq (setq article (gnus-summary-article-number))
10304                                 gnus-newsgroup-unreads))
10305                   (gnus-summary-mark-article article gnus-del-mark))
10306                 t)
10307               (gnus-summary-find-next))))))
10308
10309 (defun gnus-summary-mark-below (score mark)
10310   "Mark articles with score less than SCORE with MARK."
10311   (interactive "P\ncMark: ")
10312   (setq score (if score
10313                   (prefix-numeric-value score)
10314                 (or gnus-summary-default-score 0)))
10315   (save-excursion
10316     (set-buffer gnus-summary-buffer)
10317     (goto-char (point-min))
10318     (while
10319         (progn
10320           (and (< (gnus-summary-article-score) score)
10321                (gnus-summary-mark-article nil mark))
10322           (gnus-summary-find-next)))))
10323
10324 (defun gnus-summary-kill-below (&optional score)
10325   "Mark articles with score below SCORE as read."
10326   (interactive "P")
10327   (gnus-summary-mark-below score gnus-killed-mark))
10328
10329 (defun gnus-summary-clear-above (&optional score)
10330   "Clear all marks from articles with score above SCORE."
10331   (interactive "P")
10332   (gnus-summary-mark-above score gnus-unread-mark))
10333
10334 (defun gnus-summary-tick-above (&optional score)
10335   "Tick all articles with score above SCORE."
10336   (interactive "P")
10337   (gnus-summary-mark-above score gnus-ticked-mark))
10338
10339 (defun gnus-summary-mark-above (score mark)
10340   "Mark articles with score over SCORE with MARK."
10341   (interactive "P\ncMark: ")
10342   (setq score (if score
10343                   (prefix-numeric-value score)
10344                 (or gnus-summary-default-score 0)))
10345   (save-excursion
10346     (set-buffer gnus-summary-buffer)
10347     (goto-char (point-min))
10348     (while (and (progn
10349                   (when (> (gnus-summary-article-score) score)
10350                     (gnus-summary-mark-article nil mark))
10351                   t)
10352                 (gnus-summary-find-next)))))
10353
10354 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10355 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10356 (defun gnus-summary-limit-include-expunged (&optional no-error)
10357   "Display all the hidden articles that were expunged for low scores."
10358   (interactive)
10359   (let ((buffer-read-only nil))
10360     (let ((scored gnus-newsgroup-scored)
10361           headers h)
10362       (while scored
10363         (unless (gnus-summary-article-header (caar scored))
10364           (and (setq h (gnus-number-to-header (caar scored)))
10365                (< (cdar scored) gnus-summary-expunge-below)
10366                (push h headers)))
10367         (setq scored (cdr scored)))
10368       (if (not headers)
10369           (when (not no-error)
10370             (error "No expunged articles hidden"))
10371         (goto-char (point-min))
10372         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10373         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10374         (mapcar (lambda (x) (push (mail-header-number x)
10375                                   gnus-newsgroup-limit))
10376                 headers)
10377         (gnus-summary-prepare-unthreaded (nreverse headers))
10378         (goto-char (point-min))
10379         (gnus-summary-position-point)
10380         t))))
10381
10382 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10383   "Mark all unread articles in this newsgroup as read.
10384 If prefix argument ALL is non-nil, ticked and dormant articles will
10385 also be marked as read.
10386 If QUIETLY is non-nil, no questions will be asked.
10387
10388 If TO-HERE is non-nil, it should be a point in the buffer.  All
10389 articles before (after, if REVERSE is set) this point will be marked
10390 as read.
10391
10392 Note that this function will only catch up the unread article
10393 in the current summary buffer limitation.
10394
10395 The number of articles marked as read is returned."
10396   (interactive "P")
10397   (prog1
10398       (save-excursion
10399         (when (or quietly
10400                   (not gnus-interactive-catchup) ;Without confirmation?
10401                   gnus-expert-user
10402                   (gnus-y-or-n-p
10403                    (if all
10404                        "Mark absolutely all articles as read? "
10405                      "Mark all unread articles as read? ")))
10406           (if (and not-mark
10407                    (not gnus-newsgroup-adaptive)
10408                    (not gnus-newsgroup-auto-expire)
10409                    (not gnus-suppress-duplicates)
10410                    (or (not gnus-use-cache)
10411                        (eq gnus-use-cache 'passive)))
10412               (progn
10413                 (when all
10414                   (setq gnus-newsgroup-marked nil
10415                         gnus-newsgroup-spam-marked nil
10416                         gnus-newsgroup-dormant nil))
10417                 (setq gnus-newsgroup-unreads
10418                       (gnus-sorted-nunion
10419                        (gnus-intersection gnus-newsgroup-unreads
10420                                           gnus-newsgroup-downloadable)
10421                        gnus-newsgroup-unfetched)))
10422             ;; We actually mark all articles as canceled, which we
10423             ;; have to do when using auto-expiry or adaptive scoring.
10424             (gnus-summary-show-all-threads)
10425             (if (and to-here reverse)
10426                 (progn
10427                   (goto-char to-here)
10428                   (gnus-summary-mark-current-read-and-unread-as-read
10429                    gnus-catchup-mark)
10430                   (while (gnus-summary-find-next (not all))
10431                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10432               (when (gnus-summary-first-subject (not all))
10433                 (while (and
10434                         (if to-here (< (point) to-here) t)
10435                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10436                         (gnus-summary-find-next (not all))))))
10437             (gnus-set-mode-line 'summary))
10438           t))
10439     (gnus-summary-position-point)))
10440
10441 (defun gnus-summary-catchup-to-here (&optional all)
10442   "Mark all unticked articles before the current one as read.
10443 If ALL is non-nil, also mark ticked and dormant articles as read."
10444   (interactive "P")
10445   (save-excursion
10446     (gnus-save-hidden-threads
10447       (let ((beg (point)))
10448         ;; We check that there are unread articles.
10449         (when (or all (gnus-summary-find-prev))
10450           (gnus-summary-catchup all t beg)))))
10451   (gnus-summary-position-point))
10452
10453 (defun gnus-summary-catchup-from-here (&optional all)
10454   "Mark all unticked articles after (and including) the current one as read.
10455 If ALL is non-nil, also mark ticked and dormant articles as read."
10456   (interactive "P")
10457   (save-excursion
10458     (gnus-save-hidden-threads
10459       (let ((beg (point)))
10460         ;; We check that there are unread articles.
10461         (when (or all (gnus-summary-find-next))
10462           (gnus-summary-catchup all t beg nil t)))))
10463   (gnus-summary-position-point))
10464
10465 (defun gnus-summary-catchup-all (&optional quietly)
10466   "Mark all articles in this newsgroup as read.
10467 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10468 instead, which marks only unread articles as read."
10469   (interactive "P")
10470   (gnus-summary-catchup t quietly))
10471
10472 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10473   "Mark all unread articles in this group as read, then exit.
10474 If prefix argument ALL is non-nil, all articles are marked as read.
10475 If QUIETLY is non-nil, no questions will be asked."
10476   (interactive "P")
10477   (when (gnus-summary-catchup all quietly nil 'fast)
10478     ;; Select next newsgroup or exit.
10479     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10480              (eq gnus-auto-select-next 'quietly))
10481         (gnus-summary-next-group nil)
10482       (gnus-summary-exit))))
10483
10484 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10485   "Mark all articles in this newsgroup as read, and then exit.
10486 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10487 instead, which marks only unread articles as read."
10488   (interactive "P")
10489   (gnus-summary-catchup-and-exit t quietly))
10490
10491 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10492   "Mark all articles in this group as read and select the next group.
10493 If given a prefix, mark all articles, unread as well as ticked, as
10494 read."
10495   (interactive "P")
10496   (save-excursion
10497     (gnus-summary-catchup all))
10498   (gnus-summary-next-group))
10499
10500 ;;;
10501 ;;; with article
10502 ;;;
10503
10504 (defmacro gnus-with-article (article &rest forms)
10505   "Select ARTICLE and perform FORMS in the original article buffer.
10506 Then replace the article with the result."
10507   `(progn
10508      ;; We don't want the article to be marked as read.
10509      (let (gnus-mark-article-hook)
10510        (gnus-summary-select-article t t nil ,article))
10511      (set-buffer gnus-original-article-buffer)
10512      ,@forms
10513      (if (not (gnus-check-backend-function
10514                'request-replace-article (car gnus-article-current)))
10515          (gnus-message 5 "Read-only group; not replacing")
10516        (unless (gnus-request-replace-article
10517                 ,article (car gnus-article-current)
10518                 (current-buffer) t)
10519          (error "Couldn't replace article")))
10520      ;; The cache and backlog have to be flushed somewhat.
10521      (when gnus-keep-backlog
10522        (gnus-backlog-remove-article
10523         (car gnus-article-current) (cdr gnus-article-current)))
10524      (when gnus-use-cache
10525        (gnus-cache-update-article
10526         (car gnus-article-current) (cdr gnus-article-current)))))
10527
10528 (put 'gnus-with-article 'lisp-indent-function 1)
10529 (put 'gnus-with-article 'edebug-form-spec '(form body))
10530
10531 ;; Thread-based commands.
10532
10533 (defun gnus-summary-articles-in-thread (&optional article)
10534   "Return a list of all articles in the current thread.
10535 If ARTICLE is non-nil, return all articles in the thread that starts
10536 with that article."
10537   (let* ((article (or article (gnus-summary-article-number)))
10538          (data (gnus-data-find-list article))
10539          (top-level (gnus-data-level (car data)))
10540          (top-subject
10541           (cond ((null gnus-thread-operation-ignore-subject)
10542                  (gnus-simplify-subject-re
10543                   (mail-header-subject (gnus-data-header (car data)))))
10544                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10545                  (gnus-simplify-subject-fuzzy
10546                   (mail-header-subject (gnus-data-header (car data)))))
10547                 (t nil)))
10548          (end-point (save-excursion
10549                       (if (gnus-summary-go-to-next-thread)
10550                           (point) (point-max))))
10551          articles)
10552     (while (and data
10553                 (< (gnus-data-pos (car data)) end-point))
10554       (when (or (not top-subject)
10555                 (string= top-subject
10556                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10557                              (gnus-simplify-subject-fuzzy
10558                               (mail-header-subject
10559                                (gnus-data-header (car data))))
10560                            (gnus-simplify-subject-re
10561                             (mail-header-subject
10562                              (gnus-data-header (car data)))))))
10563         (push (gnus-data-number (car data)) articles))
10564       (unless (and (setq data (cdr data))
10565                    (> (gnus-data-level (car data)) top-level))
10566         (setq data nil)))
10567     ;; Return the list of articles.
10568     (nreverse articles)))
10569
10570 (defun gnus-summary-rethread-current ()
10571   "Rethread the thread the current article is part of."
10572   (interactive)
10573   (let* ((gnus-show-threads t)
10574          (article (gnus-summary-article-number))
10575          (id (mail-header-id (gnus-summary-article-header)))
10576          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10577     (unless id
10578       (error "No article on the current line"))
10579     (gnus-rebuild-thread id)
10580     (gnus-summary-goto-subject article)))
10581
10582 (defun gnus-summary-reparent-thread ()
10583   "Make the current article child of the marked (or previous) article.
10584
10585 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10586 is non-nil or the Subject: of both articles are the same."
10587   (interactive)
10588   (unless (not (gnus-group-read-only-p))
10589     (error "The current newsgroup does not support article editing"))
10590   (unless (<= (length gnus-newsgroup-processable) 1)
10591     (error "No more than one article may be marked"))
10592   (save-window-excursion
10593     (let ((gnus-article-buffer " *reparent*")
10594           (current-article (gnus-summary-article-number))
10595           ;; First grab the marked article, otherwise one line up.
10596           (parent-article (if (not (null gnus-newsgroup-processable))
10597                               (car gnus-newsgroup-processable)
10598                             (save-excursion
10599                               (if (eq (forward-line -1) 0)
10600                                   (gnus-summary-article-number)
10601                                 (error "Beginning of summary buffer"))))))
10602       (unless (not (eq current-article parent-article))
10603         (error "An article may not be self-referential"))
10604       (let ((message-id (mail-header-id
10605                          (gnus-summary-article-header parent-article))))
10606         (unless (and message-id (not (equal message-id "")))
10607           (error "No message-id in desired parent"))
10608         (gnus-with-article current-article
10609           (save-restriction
10610             (goto-char (point-min))
10611             (message-narrow-to-head)
10612             (if (re-search-forward "^References: " nil t)
10613                 (progn
10614                   (re-search-forward "^[^ \t]" nil t)
10615                   (forward-line -1)
10616                   (end-of-line)
10617                   (insert " " message-id))
10618               (insert "References: " message-id "\n"))))
10619         (set-buffer gnus-summary-buffer)
10620         (gnus-summary-unmark-all-processable)
10621         (gnus-summary-update-article current-article)
10622         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10623             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10624         (gnus-summary-rethread-current)
10625         (gnus-message 3 "Article %d is now the child of article %d"
10626                       current-article parent-article)))))
10627
10628 (defun gnus-summary-toggle-threads (&optional arg)
10629   "Toggle showing conversation threads.
10630 If ARG is positive number, turn showing conversation threads on."
10631   (interactive "P")
10632   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10633     (setq gnus-show-threads
10634           (if (null arg) (not gnus-show-threads)
10635             (> (prefix-numeric-value arg) 0)))
10636     (gnus-summary-prepare)
10637     (gnus-summary-goto-subject current)
10638     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10639     (gnus-summary-position-point)))
10640
10641 (defun gnus-summary-show-all-threads ()
10642   "Show all threads."
10643   (interactive)
10644   (save-excursion
10645     (let ((buffer-read-only nil))
10646       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10647   (gnus-summary-position-point))
10648
10649 (defun gnus-summary-show-thread ()
10650   "Show thread subtrees.
10651 Returns nil if no thread was there to be shown."
10652   (interactive)
10653   (let ((buffer-read-only nil)
10654         (orig (point))
10655         (end (point-at-eol))
10656         ;; Leave point at bol
10657         (beg (progn (beginning-of-line) (point))))
10658     (prog1
10659         ;; Any hidden lines here?
10660         (search-forward "\r" end t)
10661       (subst-char-in-region beg end ?\^M ?\n t)
10662       (goto-char orig)
10663       (gnus-summary-position-point))))
10664
10665 (defun gnus-summary-maybe-hide-threads ()
10666   "If requested, hide the threads that should be hidden."
10667   (when (and gnus-show-threads
10668              gnus-thread-hide-subtree)
10669     (gnus-summary-hide-all-threads
10670      (if (or (consp gnus-thread-hide-subtree)
10671              (functionp gnus-thread-hide-subtree))
10672          (gnus-make-predicate gnus-thread-hide-subtree)
10673        nil))))
10674
10675 ;;; Hiding predicates.
10676
10677 (defun gnus-article-unread-p (header)
10678   (memq (mail-header-number header) gnus-newsgroup-unreads))
10679
10680 (defun gnus-article-unseen-p (header)
10681   (memq (mail-header-number header) gnus-newsgroup-unseen))
10682
10683 (defun gnus-map-articles (predicate articles)
10684   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10685   (apply 'gnus-or (mapcar predicate
10686                           (mapcar 'gnus-summary-article-header articles))))
10687
10688 (defun gnus-summary-hide-all-threads (&optional predicate)
10689   "Hide all thread subtrees.
10690 If PREDICATE is supplied, threads that satisfy this predicate
10691 will not be hidden."
10692   (interactive)
10693   (save-excursion
10694     (goto-char (point-min))
10695     (let ((end nil))
10696       (while (not end)
10697         (when (or (not predicate)
10698                   (gnus-map-articles
10699                    predicate (gnus-summary-article-children)))
10700             (gnus-summary-hide-thread))
10701         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10702   (gnus-summary-position-point))
10703
10704 (defun gnus-summary-hide-thread ()
10705   "Hide thread subtrees.
10706 If PREDICATE is supplied, threads that satisfy this predicate
10707 will not be hidden.
10708 Returns nil if no threads were there to be hidden."
10709   (interactive)
10710   (let ((buffer-read-only nil)
10711         (start (point))
10712         (article (gnus-summary-article-number)))
10713     (goto-char start)
10714     ;; Go forward until either the buffer ends or the subthread
10715     ;; ends.
10716     (when (and (not (eobp))
10717                (or (zerop (gnus-summary-next-thread 1 t))
10718                    (goto-char (point-max))))
10719       (prog1
10720           (if (and (> (point) start)
10721                    (search-backward "\n" start t))
10722               (progn
10723                 (subst-char-in-region start (point) ?\n ?\^M)
10724                 (gnus-summary-goto-subject article))
10725             (goto-char start)
10726             nil)))))
10727
10728 (defun gnus-summary-go-to-next-thread (&optional previous)
10729   "Go to the same level (or less) next thread.
10730 If PREVIOUS is non-nil, go to previous thread instead.
10731 Return the article number moved to, or nil if moving was impossible."
10732   (let ((level (gnus-summary-thread-level))
10733         (way (if previous -1 1))
10734         (beg (point)))
10735     (forward-line way)
10736     (while (and (not (eobp))
10737                 (< level (gnus-summary-thread-level)))
10738       (forward-line way))
10739     (if (eobp)
10740         (progn
10741           (goto-char beg)
10742           nil)
10743       (setq beg (point))
10744       (prog1
10745           (gnus-summary-article-number)
10746         (goto-char beg)))))
10747
10748 (defun gnus-summary-next-thread (n &optional silent)
10749   "Go to the same level next N'th thread.
10750 If N is negative, search backward instead.
10751 Returns the difference between N and the number of skips actually
10752 done.
10753
10754 If SILENT, don't output messages."
10755   (interactive "p")
10756   (let ((backward (< n 0))
10757         (n (abs n)))
10758     (while (and (> n 0)
10759                 (gnus-summary-go-to-next-thread backward))
10760       (decf n))
10761     (unless silent
10762       (gnus-summary-position-point))
10763     (when (and (not silent) (/= 0 n))
10764       (gnus-message 7 "No more threads"))
10765     n))
10766
10767 (defun gnus-summary-prev-thread (n)
10768   "Go to the same level previous N'th thread.
10769 Returns the difference between N and the number of skips actually
10770 done."
10771   (interactive "p")
10772   (gnus-summary-next-thread (- n)))
10773
10774 (defun gnus-summary-go-down-thread ()
10775   "Go down one level in the current thread."
10776   (let ((children (gnus-summary-article-children)))
10777     (when children
10778       (gnus-summary-goto-subject (car children)))))
10779
10780 (defun gnus-summary-go-up-thread ()
10781   "Go up one level in the current thread."
10782   (let ((parent (gnus-summary-article-parent)))
10783     (when parent
10784       (gnus-summary-goto-subject parent))))
10785
10786 (defun gnus-summary-down-thread (n)
10787   "Go down thread N steps.
10788 If N is negative, go up instead.
10789 Returns the difference between N and how many steps down that were
10790 taken."
10791   (interactive "p")
10792   (let ((up (< n 0))
10793         (n (abs n)))
10794     (while (and (> n 0)
10795                 (if up (gnus-summary-go-up-thread)
10796                   (gnus-summary-go-down-thread)))
10797       (setq n (1- n)))
10798     (gnus-summary-position-point)
10799     (when (/= 0 n)
10800       (gnus-message 7 "Can't go further"))
10801     n))
10802
10803 (defun gnus-summary-up-thread (n)
10804   "Go up thread N steps.
10805 If N is negative, go down instead.
10806 Returns the difference between N and how many steps down that were
10807 taken."
10808   (interactive "p")
10809   (gnus-summary-down-thread (- n)))
10810
10811 (defun gnus-summary-top-thread ()
10812   "Go to the top of the thread."
10813   (interactive)
10814   (while (gnus-summary-go-up-thread))
10815   (gnus-summary-article-number))
10816
10817 (defun gnus-summary-kill-thread (&optional unmark)
10818   "Mark articles under current thread as read.
10819 If the prefix argument is positive, remove any kinds of marks.
10820 If the prefix argument is negative, tick articles instead."
10821   (interactive "P")
10822   (when unmark
10823     (setq unmark (prefix-numeric-value unmark)))
10824   (let ((articles (gnus-summary-articles-in-thread)))
10825     (save-excursion
10826       ;; Expand the thread.
10827       (gnus-summary-show-thread)
10828       ;; Mark all the articles.
10829       (while articles
10830         (gnus-summary-goto-subject (car articles))
10831         (cond ((null unmark)
10832                (gnus-summary-mark-article-as-read gnus-killed-mark))
10833               ((> unmark 0)
10834                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10835               (t
10836                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10837         (setq articles (cdr articles))))
10838     ;; Hide killed subtrees.
10839     (and (null unmark)
10840          gnus-thread-hide-killed
10841          (gnus-summary-hide-thread))
10842     ;; If marked as read, go to next unread subject.
10843     (when (null unmark)
10844       ;; Go to next unread subject.
10845       (gnus-summary-next-subject 1 t)))
10846   (gnus-set-mode-line 'summary))
10847
10848 ;; Summary sorting commands
10849
10850 (defun gnus-summary-sort-by-number (&optional reverse)
10851   "Sort the summary buffer by article number.
10852 Argument REVERSE means reverse order."
10853   (interactive "P")
10854   (gnus-summary-sort 'number reverse))
10855
10856 (defun gnus-summary-sort-by-random (&optional reverse)
10857   "Randomize the order in the summary buffer.
10858 Argument REVERSE means to randomize in reverse order."
10859   (interactive "P")
10860   (gnus-summary-sort 'random reverse))
10861
10862 (defun gnus-summary-sort-by-author (&optional reverse)
10863   "Sort the summary buffer by author name alphabetically.
10864 If `case-fold-search' is non-nil, case of letters is ignored.
10865 Argument REVERSE means reverse order."
10866   (interactive "P")
10867   (gnus-summary-sort 'author reverse))
10868
10869 (defun gnus-summary-sort-by-subject (&optional reverse)
10870   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10871 If `case-fold-search' is non-nil, case of letters is ignored.
10872 Argument REVERSE means reverse order."
10873   (interactive "P")
10874   (gnus-summary-sort 'subject reverse))
10875
10876 (defun gnus-summary-sort-by-date (&optional reverse)
10877   "Sort the summary buffer by date.
10878 Argument REVERSE means reverse order."
10879   (interactive "P")
10880   (gnus-summary-sort 'date reverse))
10881
10882 (defun gnus-summary-sort-by-score (&optional reverse)
10883   "Sort the summary buffer by score.
10884 Argument REVERSE means reverse order."
10885   (interactive "P")
10886   (gnus-summary-sort 'score reverse))
10887
10888 (defun gnus-summary-sort-by-lines (&optional reverse)
10889   "Sort the summary buffer by the number of lines.
10890 Argument REVERSE means reverse order."
10891   (interactive "P")
10892   (gnus-summary-sort 'lines reverse))
10893
10894 (defun gnus-summary-sort-by-chars (&optional reverse)
10895   "Sort the summary buffer by article length.
10896 Argument REVERSE means reverse order."
10897   (interactive "P")
10898   (gnus-summary-sort 'chars reverse))
10899
10900 (defun gnus-summary-sort-by-original (&optional reverse)
10901   "Sort the summary buffer using the default sorting method.
10902 Argument REVERSE means reverse order."
10903   (interactive "P")
10904   (let* ((buffer-read-only)
10905          (gnus-summary-prepare-hook nil))
10906     ;; We do the sorting by regenerating the threads.
10907     (gnus-summary-prepare)
10908     ;; Hide subthreads if needed.
10909     (gnus-summary-maybe-hide-threads)))
10910
10911 (defun gnus-summary-sort (predicate reverse)
10912   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10913   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10914          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10915          (gnus-thread-sort-functions
10916           (if (not reverse)
10917               thread
10918             `(lambda (t1 t2)
10919                (,thread t2 t1))))
10920          (gnus-sort-gathered-threads-function
10921           gnus-thread-sort-functions)
10922          (gnus-article-sort-functions
10923           (if (not reverse)
10924               article
10925             `(lambda (t1 t2)
10926                (,article t2 t1))))
10927          (buffer-read-only)
10928          (gnus-summary-prepare-hook nil))
10929     ;; We do the sorting by regenerating the threads.
10930     (gnus-summary-prepare)
10931     ;; Hide subthreads if needed.
10932     (gnus-summary-maybe-hide-threads)))
10933
10934 ;; Summary saving commands.
10935
10936 (defun gnus-summary-save-article (&optional n not-saved)
10937   "Save the current article using the default saver function.
10938 If N is a positive number, save the N next articles.
10939 If N is a negative number, save the N previous articles.
10940 If N is nil and any articles have been marked with the process mark,
10941 save those articles instead.
10942 The variable `gnus-default-article-saver' specifies the saver function."
10943   (interactive "P")
10944   (let* ((articles (gnus-summary-work-articles n))
10945          (save-buffer (save-excursion
10946                         (nnheader-set-temp-buffer " *Gnus Save*")))
10947          (num (length articles))
10948          header file)
10949     (dolist (article articles)
10950       (setq header (gnus-summary-article-header article))
10951       (if (not (vectorp header))
10952           ;; This is a pseudo-article.
10953           (if (assq 'name header)
10954               (gnus-copy-file (cdr (assq 'name header)))
10955             (gnus-message 1 "Article %d is unsaveable" article))
10956         ;; This is a real article.
10957         (save-window-excursion
10958           (let ((gnus-display-mime-function nil)
10959                 (gnus-article-prepare-hook nil))
10960             (gnus-summary-select-article t nil nil article)))
10961         (save-excursion
10962           (set-buffer save-buffer)
10963           (erase-buffer)
10964           (insert-buffer-substring gnus-original-article-buffer))
10965         (setq file (gnus-article-save save-buffer file num))
10966         (gnus-summary-remove-process-mark article)
10967         (unless not-saved
10968           (gnus-summary-set-saved-mark article))))
10969     (gnus-kill-buffer save-buffer)
10970     (gnus-summary-position-point)
10971     (gnus-set-mode-line 'summary)
10972     n))
10973
10974 (defun gnus-summary-pipe-output (&optional arg headers)
10975   "Pipe the current article to a subprocess.
10976 If N is a positive number, pipe the N next articles.
10977 If N is a negative number, pipe the N previous articles.
10978 If N is nil and any articles have been marked with the process mark,
10979 pipe those articles instead.
10980 If HEADERS (the symbolic prefix), include the headers, too."
10981   (interactive (gnus-interactive "P\ny"))
10982   (require 'gnus-art)
10983   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10984         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10985     (gnus-summary-save-article arg t))
10986   (let ((buffer (get-buffer "*Shell Command Output*")))
10987     (when (and buffer
10988                (not (zerop (buffer-size buffer))))
10989       (gnus-configure-windows 'pipe))))
10990
10991 (defun gnus-summary-save-article-mail (&optional arg)
10992   "Append the current article to an mail file.
10993 If N is a positive number, save the N next articles.
10994 If N is a negative number, save the N previous articles.
10995 If N is nil and any articles have been marked with the process mark,
10996 save those articles instead."
10997   (interactive "P")
10998   (require 'gnus-art)
10999   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11000     (gnus-summary-save-article arg)))
11001
11002 (defun gnus-summary-save-article-rmail (&optional arg)
11003   "Append the current article to an rmail file.
11004 If N is a positive number, save the N next articles.
11005 If N is a negative number, save the N previous articles.
11006 If N is nil and any articles have been marked with the process mark,
11007 save those articles instead."
11008   (interactive "P")
11009   (require 'gnus-art)
11010   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11011     (gnus-summary-save-article arg)))
11012
11013 (defun gnus-summary-save-article-file (&optional arg)
11014   "Append the current article to a file.
11015 If N is a positive number, save the N next articles.
11016 If N is a negative number, save the N previous articles.
11017 If N is nil and any articles have been marked with the process mark,
11018 save those articles instead."
11019   (interactive "P")
11020   (require 'gnus-art)
11021   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11022     (gnus-summary-save-article arg)))
11023
11024 (defun gnus-summary-write-article-file (&optional arg)
11025   "Write the current article to a file, deleting the previous file.
11026 If N is a positive number, save the N next articles.
11027 If N is a negative number, save the N previous articles.
11028 If N is nil and any articles have been marked with the process mark,
11029 save those articles instead."
11030   (interactive "P")
11031   (require 'gnus-art)
11032   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11033     (gnus-summary-save-article arg)))
11034
11035 (defun gnus-summary-save-article-body-file (&optional arg)
11036   "Append the current article body to a file.
11037 If N is a positive number, save the N next articles.
11038 If N is a negative number, save the N previous articles.
11039 If N is nil and any articles have been marked with the process mark,
11040 save those articles instead."
11041   (interactive "P")
11042   (require 'gnus-art)
11043   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11044     (gnus-summary-save-article arg)))
11045
11046 (defun gnus-summary-muttprint (&optional arg)
11047   "Print the current article using Muttprint.
11048 If N is a positive number, save the N next articles.
11049 If N is a negative number, save the N previous articles.
11050 If N is nil and any articles have been marked with the process mark,
11051 save those articles instead."
11052   (interactive "P")
11053   (require 'gnus-art)
11054   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11055     (gnus-summary-save-article arg t)))
11056
11057 (defun gnus-summary-pipe-message (program)
11058   "Pipe the current article through PROGRAM."
11059   (interactive "sProgram: ")
11060   (gnus-summary-select-article)
11061   (let ((mail-header-separator ""))
11062     (gnus-eval-in-buffer-window gnus-article-buffer
11063       (save-restriction
11064         (widen)
11065         (let ((start (window-start))
11066               buffer-read-only)
11067           (message-pipe-buffer-body program)
11068           (set-window-start (get-buffer-window (current-buffer)) start))))))
11069
11070 (defun gnus-get-split-value (methods)
11071   "Return a value based on the split METHODS."
11072   (let (split-name method result match)
11073     (when methods
11074       (save-excursion
11075         (set-buffer gnus-original-article-buffer)
11076         (save-restriction
11077           (nnheader-narrow-to-headers)
11078           (while (and methods (not split-name))
11079             (goto-char (point-min))
11080             (setq method (pop methods))
11081             (setq match (car method))
11082             (when (cond
11083                    ((stringp match)
11084                     ;; Regular expression.
11085                     (ignore-errors
11086                       (re-search-forward match nil t)))
11087                    ((functionp match)
11088                     ;; Function.
11089                     (save-restriction
11090                       (widen)
11091                       (setq result (funcall match gnus-newsgroup-name))))
11092                    ((consp match)
11093                     ;; Form.
11094                     (save-restriction
11095                       (widen)
11096                       (setq result (eval match)))))
11097               (setq split-name (cdr method))
11098               (cond ((stringp result)
11099                      (push (expand-file-name
11100                             result gnus-article-save-directory)
11101                            split-name))
11102                     ((consp result)
11103                      (setq split-name (append result split-name)))))))))
11104     (nreverse split-name)))
11105
11106 (defun gnus-valid-move-group-p (group)
11107   (and (boundp group)
11108        (symbol-name group)
11109        (symbol-value group)
11110        (gnus-get-function (gnus-find-method-for-group
11111                            (symbol-name group)) 'request-accept-article t)))
11112
11113 (defun gnus-read-move-group-name (prompt default articles prefix)
11114   "Read a group name."
11115   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11116          (minibuffer-confirm-incomplete nil) ; XEmacs
11117          (prom
11118           (format "%s %s to:"
11119                   prompt
11120                   (if (> (length articles) 1)
11121                       (format "these %d articles" (length articles))
11122                     "this article")))
11123          (to-newsgroup
11124           (cond
11125            ((null split-name)
11126             (gnus-completing-read-with-default
11127              default prom
11128              gnus-active-hashtb
11129              'gnus-valid-move-group-p
11130              nil prefix
11131              'gnus-group-history))
11132            ((= 1 (length split-name))
11133             (gnus-completing-read-with-default
11134              (car split-name) prom
11135              gnus-active-hashtb
11136              'gnus-valid-move-group-p
11137              nil nil
11138              'gnus-group-history))
11139            (t
11140             (gnus-completing-read-with-default
11141              nil prom
11142              (mapcar (lambda (el) (list el))
11143                      (nreverse split-name))
11144              nil nil nil
11145              'gnus-group-history))))
11146          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11147     (when to-newsgroup
11148       (if (or (string= to-newsgroup "")
11149               (string= to-newsgroup prefix))
11150           (setq to-newsgroup default))
11151       (unless to-newsgroup
11152         (error "No group name entered"))
11153       (or (gnus-active to-newsgroup)
11154           (gnus-activate-group to-newsgroup nil nil to-method)
11155           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11156                                      to-newsgroup))
11157               (or (and (gnus-request-create-group to-newsgroup to-method)
11158                        (gnus-activate-group
11159                         to-newsgroup nil nil to-method)
11160                        (gnus-subscribe-group to-newsgroup))
11161                   (error "Couldn't create group %s" to-newsgroup)))
11162           (error "No such group: %s" to-newsgroup)))
11163     to-newsgroup))
11164
11165 (defun gnus-summary-save-parts (type dir n &optional reverse)
11166   "Save parts matching TYPE to DIR.
11167 If REVERSE, save parts that do not match TYPE."
11168   (interactive
11169    (list (read-string "Save parts of type: "
11170                       (or (car gnus-summary-save-parts-type-history)
11171                           gnus-summary-save-parts-default-mime)
11172                       'gnus-summary-save-parts-type-history)
11173          (setq gnus-summary-save-parts-last-directory
11174                (read-file-name "Save to directory: "
11175                                gnus-summary-save-parts-last-directory
11176                                nil t))
11177          current-prefix-arg))
11178   (gnus-summary-iterate n
11179     (let ((gnus-display-mime-function nil)
11180           (gnus-inhibit-treatment t))
11181       (gnus-summary-select-article))
11182     (save-excursion
11183       (set-buffer gnus-article-buffer)
11184       (let ((handles (or gnus-article-mime-handles
11185                          (mm-dissect-buffer nil gnus-article-loose-mime)
11186                          (and gnus-article-emulate-mime
11187                               (mm-uu-dissect)))))
11188         (when handles
11189           (gnus-summary-save-parts-1 type dir handles reverse)
11190           (unless gnus-article-mime-handles ;; Don't destroy this case.
11191             (mm-destroy-parts handles)))))))
11192
11193 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11194   (if (stringp (car handle))
11195       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11196               (cdr handle))
11197     (when (if reverse
11198               (not (string-match type (mm-handle-media-type handle)))
11199             (string-match type (mm-handle-media-type handle)))
11200       (let ((file (expand-file-name
11201                    (gnus-map-function
11202                     mm-file-name-rewrite-functions
11203                     (file-name-nondirectory
11204                      (or
11205                       (mail-content-type-get
11206                        (mm-handle-disposition handle) 'filename)
11207                       (mail-content-type-get
11208                        (mm-handle-type handle) 'name)
11209                       (concat gnus-newsgroup-name
11210                               "." (number-to-string
11211                                    (cdr gnus-article-current))))))
11212                    dir)))
11213         (unless (file-exists-p file)
11214           (mm-save-part-to-file handle file))))))
11215
11216 ;; Summary extract commands
11217
11218 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11219   (let ((buffer-read-only nil)
11220         (article (gnus-summary-article-number))
11221         after-article b e)
11222     (unless (gnus-summary-goto-subject article)
11223       (error "No such article: %d" article))
11224     (gnus-summary-position-point)
11225     ;; If all commands are to be bunched up on one line, we collect
11226     ;; them here.
11227     (unless gnus-view-pseudos-separately
11228       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11229             files action)
11230         (while ps
11231           (setq action (cdr (assq 'action (car ps))))
11232           (setq files (list (cdr (assq 'name (car ps)))))
11233           (while (and ps (cdr ps)
11234                       (string= (or action "1")
11235                                (or (cdr (assq 'action (cadr ps))) "2")))
11236             (push (cdr (assq 'name (cadr ps))) files)
11237             (setcdr ps (cddr ps)))
11238           (when files
11239             (when (not (string-match "%s" action))
11240               (push " " files))
11241             (push " " files)
11242             (when (assq 'execute (car ps))
11243               (setcdr (assq 'execute (car ps))
11244                       (funcall (if (string-match "%s" action)
11245                                    'format 'concat)
11246                                action
11247                                (mapconcat
11248                                 (lambda (f)
11249                                   (if (equal f " ")
11250                                       f
11251                                     (shell-quote-argument f)))
11252                                 files " ")))))
11253           (setq ps (cdr ps)))))
11254     (if (and gnus-view-pseudos (not not-view))
11255         (while pslist
11256           (when (assq 'execute (car pslist))
11257             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11258                                   (eq gnus-view-pseudos 'not-confirm)))
11259           (setq pslist (cdr pslist)))
11260       (save-excursion
11261         (while pslist
11262           (setq after-article (or (cdr (assq 'article (car pslist)))
11263                                   (gnus-summary-article-number)))
11264           (gnus-summary-goto-subject after-article)
11265           (forward-line 1)
11266           (setq b (point))
11267           (insert "    " (file-name-nondirectory
11268                           (cdr (assq 'name (car pslist))))
11269                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11270           (setq e (point))
11271           (forward-line -1)             ; back to `b'
11272           (gnus-add-text-properties
11273            b (1- e) (list 'gnus-number gnus-reffed-article-number
11274                           gnus-mouse-face-prop gnus-mouse-face))
11275           (gnus-data-enter
11276            after-article gnus-reffed-article-number
11277            gnus-unread-mark b (car pslist) 0 (- e b))
11278           (setq gnus-newsgroup-unreads
11279                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11280                                          gnus-reffed-article-number))
11281           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11282           (setq pslist (cdr pslist)))))))
11283
11284 (defun gnus-pseudos< (p1 p2)
11285   (let ((c1 (cdr (assq 'action p1)))
11286         (c2 (cdr (assq 'action p2))))
11287     (and c1 c2 (string< c1 c2))))
11288
11289 (defun gnus-request-pseudo-article (props)
11290   (cond ((assq 'execute props)
11291          (gnus-execute-command (cdr (assq 'execute props)))))
11292   (let ((gnus-current-article (gnus-summary-article-number)))
11293     (gnus-run-hooks 'gnus-mark-article-hook)))
11294
11295 (defun gnus-execute-command (command &optional automatic)
11296   (save-excursion
11297     (gnus-article-setup-buffer)
11298     (set-buffer gnus-article-buffer)
11299     (setq buffer-read-only nil)
11300     (let ((command (if automatic command
11301                      (read-string "Command: " (cons command 0)))))
11302       (erase-buffer)
11303       (insert "$ " command "\n\n")
11304       (if gnus-view-pseudo-asynchronously
11305           (start-process "gnus-execute" (current-buffer) shell-file-name
11306                          shell-command-switch command)
11307         (call-process shell-file-name nil t nil
11308                       shell-command-switch command)))))
11309
11310 ;; Summary kill commands.
11311
11312 (defun gnus-summary-edit-global-kill (article)
11313   "Edit the \"global\" kill file."
11314   (interactive (list (gnus-summary-article-number)))
11315   (gnus-group-edit-global-kill article))
11316
11317 (defun gnus-summary-edit-local-kill ()
11318   "Edit a local kill file applied to the current newsgroup."
11319   (interactive)
11320   (setq gnus-current-headers (gnus-summary-article-header))
11321   (gnus-group-edit-local-kill
11322    (gnus-summary-article-number) gnus-newsgroup-name))
11323
11324 ;;; Header reading.
11325
11326 (defun gnus-read-header (id &optional header)
11327   "Read the headers of article ID and enter them into the Gnus system."
11328   (let ((group gnus-newsgroup-name)
11329         (gnus-override-method
11330          (or
11331           gnus-override-method
11332           (and (gnus-news-group-p gnus-newsgroup-name)
11333                (car (gnus-refer-article-methods)))))
11334         where)
11335     ;; First we check to see whether the header in question is already
11336     ;; fetched.
11337     (if (stringp id)
11338         ;; This is a Message-ID.
11339         (setq header (or header (gnus-id-to-header id)))
11340       ;; This is an article number.
11341       (setq header (or header (gnus-summary-article-header id))))
11342     (if (and header
11343              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11344         ;; We have found the header.
11345         header
11346       ;; We have to really fetch the header to this article.
11347       (save-excursion
11348         (set-buffer nntp-server-buffer)
11349         (when (setq where (gnus-request-head id group))
11350           (nnheader-fold-continuation-lines)
11351           (goto-char (point-max))
11352           (insert ".\n")
11353           (goto-char (point-min))
11354           (insert "211 ")
11355           (princ (cond
11356                   ((numberp id) id)
11357                   ((cdr where) (cdr where))
11358                   (header (mail-header-number header))
11359                   (t gnus-reffed-article-number))
11360                  (current-buffer))
11361           (insert " Article retrieved.\n"))
11362         (if (or (not where)
11363                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11364             ()                          ; Malformed head.
11365           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11366             (when (and (stringp id)
11367                        (not (string= (gnus-group-real-name group)
11368                                      (car where))))
11369               ;; If we fetched by Message-ID and the article came
11370               ;; from a different group, we fudge some bogus article
11371               ;; numbers for this article.
11372               (mail-header-set-number header gnus-reffed-article-number))
11373             (save-excursion
11374               (set-buffer gnus-summary-buffer)
11375               (decf gnus-reffed-article-number)
11376               (gnus-remove-header (mail-header-number header))
11377               (push header gnus-newsgroup-headers)
11378               (setq gnus-current-headers header)
11379               (push (mail-header-number header) gnus-newsgroup-limit)))
11380           header)))))
11381
11382 (defun gnus-remove-header (number)
11383   "Remove header NUMBER from `gnus-newsgroup-headers'."
11384   (if (and gnus-newsgroup-headers
11385            (= number (mail-header-number (car gnus-newsgroup-headers))))
11386       (pop gnus-newsgroup-headers)
11387     (let ((headers gnus-newsgroup-headers))
11388       (while (and (cdr headers)
11389                   (not (= number (mail-header-number (cadr headers)))))
11390         (pop headers))
11391       (when (cdr headers)
11392         (setcdr headers (cddr headers))))))
11393
11394 ;;;
11395 ;;; summary highlights
11396 ;;;
11397
11398 (defun gnus-highlight-selected-summary ()
11399   "Highlight selected article in summary buffer."
11400   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11401   (when gnus-summary-selected-face
11402     (save-excursion
11403       (let* ((beg (point-at-bol))
11404              (end (point-at-eol))
11405              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11406              (from (if (get-text-property beg gnus-mouse-face-prop)
11407                        beg
11408                      (or (next-single-property-change
11409                           beg gnus-mouse-face-prop nil end)
11410                          beg)))
11411              (to
11412               (if (= from end)
11413                   (- from 2)
11414                 (or (next-single-property-change
11415                      from gnus-mouse-face-prop nil end)
11416                     end))))
11417         ;; If no mouse-face prop on line we will have to = from = end,
11418         ;; so we highlight the entire line instead.
11419         (when (= (+ to 2) from)
11420           (setq from beg)
11421           (setq to end))
11422         (if gnus-newsgroup-selected-overlay
11423             ;; Move old overlay.
11424             (gnus-move-overlay
11425              gnus-newsgroup-selected-overlay from to (current-buffer))
11426           ;; Create new overlay.
11427           (gnus-overlay-put
11428            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11429            'face gnus-summary-selected-face))))))
11430
11431 (defvar gnus-summary-highlight-line-cached nil)
11432 (defvar gnus-summary-highlight-line-trigger nil)
11433
11434 (defun gnus-summary-highlight-line-0 ()
11435   (if (and (eq gnus-summary-highlight-line-trigger
11436                gnus-summary-highlight)
11437            gnus-summary-highlight-line-cached)
11438       gnus-summary-highlight-line-cached
11439     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11440           gnus-summary-highlight-line-cached
11441           (let* ((cond (list 'cond))
11442                  (c cond)
11443                  (list gnus-summary-highlight))
11444             (while list
11445               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11446                               nil))
11447               (setq c (cdr c)
11448                     list (cdr list)))
11449             (gnus-byte-compile (list 'lambda nil cond))))))
11450
11451 (defun gnus-summary-highlight-line ()
11452   "Highlight current line according to `gnus-summary-highlight'."
11453   (let* ((beg (point-at-bol))
11454          (article (or (gnus-summary-article-number) gnus-current-article))
11455          (score (or (cdr (assq article
11456                                gnus-newsgroup-scored))
11457                     gnus-summary-default-score 0))
11458          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11459          (inhibit-read-only t)
11460          (default gnus-summary-default-score)
11461          (default-high gnus-summary-default-high-score)
11462          (default-low gnus-summary-default-low-score)
11463          (uncached (and gnus-summary-use-undownloaded-faces
11464                         (memq article gnus-newsgroup-undownloaded)
11465                         (not (memq article gnus-newsgroup-cached)))))
11466     (let ((face (funcall (gnus-summary-highlight-line-0))))
11467       (unless (eq face (get-text-property beg 'face))
11468         (gnus-put-text-property-excluding-characters-with-faces
11469          beg (point-at-eol) 'face
11470          (setq face (if (boundp face) (symbol-value face) face)))
11471         (when gnus-summary-highlight-line-function
11472           (funcall gnus-summary-highlight-line-function article face))))))
11473
11474 (defun gnus-update-read-articles (group unread &optional compute)
11475   "Update the list of read articles in GROUP.
11476 UNREAD is a sorted list."
11477   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11478         (info (gnus-get-info group))
11479         (prev 1)
11480         read)
11481     (if (or (not info) (not active))
11482         ;; There is no info on this group if it was, in fact,
11483         ;; killed.  Gnus stores no information on killed groups, so
11484         ;; there's nothing to be done.
11485         ;; One could store the information somewhere temporarily,
11486         ;; perhaps...  Hmmm...
11487         ()
11488       ;; Remove any negative articles numbers.
11489       (while (and unread (< (car unread) 0))
11490         (setq unread (cdr unread)))
11491       ;; Remove any expired article numbers
11492       (while (and unread (< (car unread) (car active)))
11493         (setq unread (cdr unread)))
11494       ;; Compute the ranges of read articles by looking at the list of
11495       ;; unread articles.
11496       (while unread
11497         (when (/= (car unread) prev)
11498           (push (if (= prev (1- (car unread))) prev
11499                   (cons prev (1- (car unread))))
11500                 read))
11501         (setq prev (1+ (car unread)))
11502         (setq unread (cdr unread)))
11503       (when (<= prev (cdr active))
11504         (push (cons prev (cdr active)) read))
11505       (setq read (if (> (length read) 1) (nreverse read) read))
11506       (if compute
11507           read
11508         (save-excursion
11509           (let (setmarkundo)
11510             ;; Propagate the read marks to the backend.
11511             (when (gnus-check-backend-function 'request-set-mark group)
11512               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11513                     (add (gnus-remove-from-range read (gnus-info-read info))))
11514                 (when (or add del)
11515                   (unless (gnus-check-group group)
11516                     (error "Can't open server for %s" group))
11517                   (gnus-request-set-mark
11518                    group (delq nil (list (if add (list add 'add '(read)))
11519                                          (if del (list del 'del '(read))))))
11520                   (setq setmarkundo
11521                         `(gnus-request-set-mark
11522                           ,group
11523                           ',(delq nil (list
11524                                        (if del (list del 'add '(read)))
11525                                        (if add (list add 'del '(read))))))))))
11526             (set-buffer gnus-group-buffer)
11527             (gnus-undo-register
11528               `(progn
11529                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11530                  (gnus-info-set-read ',info ',(gnus-info-read info))
11531                  (gnus-get-unread-articles-in-group ',info
11532                                                     (gnus-active ,group))
11533                  (gnus-group-update-group ,group t)
11534                  ,setmarkundo))))
11535         ;; Enter this list into the group info.
11536         (gnus-info-set-read info read)
11537         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11538         (gnus-get-unread-articles-in-group info (gnus-active group))
11539         t))))
11540
11541 (defun gnus-offer-save-summaries ()
11542   "Offer to save all active summary buffers."
11543   (let (buffers)
11544     ;; Go through all buffers and find all summaries.
11545     (dolist (buffer (buffer-list))
11546       (when (and (setq buffer (buffer-name buffer))
11547                  (string-match "Summary" buffer)
11548                  (save-excursion
11549                    (set-buffer buffer)
11550                    ;; We check that this is, indeed, a summary buffer.
11551                    (and (eq major-mode 'gnus-summary-mode)
11552                         ;; Also make sure this isn't bogus.
11553                         gnus-newsgroup-prepared
11554                         ;; Also make sure that this isn't a
11555                         ;; dead summary buffer.
11556                         (not gnus-dead-summary-mode))))
11557         (push buffer buffers)))
11558     ;; Go through all these summary buffers and offer to save them.
11559     (when buffers
11560       (save-excursion
11561         (map-y-or-n-p
11562          "Update summary buffer %s? "
11563          (lambda (buf)
11564            (switch-to-buffer buf)
11565            (gnus-summary-exit))
11566          buffers)))))
11567
11568
11569 ;;; @ for mime-partial
11570 ;;;
11571
11572 (defun gnus-request-partial-message ()
11573   (save-excursion
11574     (let ((number (gnus-summary-article-number))
11575           (group gnus-newsgroup-name)
11576           (mother gnus-article-buffer))
11577       (set-buffer (get-buffer-create " *Partial Article*"))
11578       (erase-buffer)
11579       (setq mime-preview-buffer mother)
11580       (gnus-request-article-this-buffer number group)
11581       (mime-parse-buffer)
11582       )))
11583
11584 (autoload 'mime-combine-message/partial-pieces-automatically
11585   "mime-partial"
11586   "Internal method to combine message/partial messages automatically.")
11587
11588 (mime-add-condition
11589  'action '((type . message)(subtype . partial)
11590            (major-mode . gnus-original-article-mode)
11591            (method . mime-combine-message/partial-pieces-automatically)
11592            (summary-buffer-exp . gnus-summary-buffer)
11593            (request-partial-message-method . gnus-request-partial-message)
11594            ))
11595
11596
11597 ;;; @ for message/rfc822
11598 ;;;
11599
11600 (defun gnus-mime-extract-message/rfc822 (entity situation)
11601   "Burst a forwarded article."
11602   (save-excursion
11603     (set-buffer gnus-summary-buffer)
11604     (let* ((group (completing-read "Group: " gnus-active-hashtb nil t
11605                                    gnus-newsgroup-name 'gnus-group-history))
11606            (gnus-group-marked (list group))
11607            article info)
11608       (with-temp-buffer
11609         (mime-insert-entity-content entity)
11610         (setq article (gnus-request-accept-article group)))
11611       (when (and (consp article)
11612                  (numberp (setq article (cdr article))))
11613         (setq info (gnus-get-info group))
11614         (gnus-info-set-read info
11615                             (gnus-remove-from-range (gnus-info-read info)
11616                                                     (list article)))
11617         (when (string-equal group gnus-newsgroup-name)
11618           (forward-line 1)
11619           (let (gnus-show-threads)
11620             (gnus-summary-goto-subject article t))
11621           (gnus-summary-clear-mark-forward 1))
11622         (set-buffer gnus-group-buffer)
11623         (gnus-group-get-new-news-this-group nil t)))))
11624
11625 (mime-add-condition
11626  'action '((type . message)(subtype . rfc822)
11627            (major-mode . gnus-original-article-mode)
11628            (method . gnus-mime-extract-message/rfc822)
11629            (mode . "extract")
11630            ))
11631
11632 (mime-add-condition
11633  'action '((type . message)(subtype . news)
11634            (major-mode . gnus-original-article-mode)
11635            (method . gnus-mime-extract-message/rfc822)
11636            (mode . "extract")
11637            ))
11638
11639 (defun gnus-mime-extract-multipart (entity situation)
11640   (let ((children (mime-entity-children entity))
11641         mime-acting-situation-to-override
11642         f)
11643     (while children
11644       (mime-play-entity (car children)
11645                         (cons (assq 'mode situation)
11646                               mime-acting-situation-to-override))
11647       (setq children (cdr children)))
11648     (if (setq f (cdr (assq 'after-method
11649                            mime-acting-situation-to-override)))
11650         (eval f)
11651       )))
11652
11653 (mime-add-condition
11654  'action '((type . multipart)
11655            (method . gnus-mime-extract-multipart)
11656            (mode . "extract")
11657            )
11658  'with-default)
11659
11660
11661 ;;; @ end
11662 ;;;
11663
11664 (defun gnus-summary-inherit-default-charset ()
11665   "Import `default-mime-charset' from summary buffer.
11666 Also take care of `default-mime-charset-unlimited' if the LIMIT version
11667 of FLIM is used."
11668   (if (buffer-live-p gnus-summary-buffer)
11669       (let (d-m-c d-m-c-u)
11670         (with-current-buffer gnus-summary-buffer
11671           (setq d-m-c (if (local-variable-p 'default-mime-charset
11672                                             gnus-summary-buffer)
11673                           default-mime-charset
11674                         t)
11675                 ;; LIMIT
11676                 d-m-c-u (if (local-variable-p 'default-mime-charset-unlimited
11677                                               gnus-summary-buffer)
11678                             (symbol-value 'default-mime-charset-unlimited)
11679                           t)))
11680         (if (eq t d-m-c)
11681             (kill-local-variable 'default-mime-charset)
11682           (set (make-local-variable 'default-mime-charset) d-m-c))
11683         (if (eq t d-m-c-u)
11684             (kill-local-variable 'default-mime-charset-unlimited)
11685           (set (make-local-variable 'default-mime-charset-unlimited)
11686                d-m-c-u)))))
11687
11688 (defun gnus-summary-setup-default-charset ()
11689   "Setup newsgroup default charset."
11690   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11691       (progn
11692         (setq gnus-newsgroup-charset nil)
11693         (set (make-local-variable 'default-mime-charset) nil)
11694         (when (boundp 'default-mime-charset-unlimited);; LIMIT
11695           (set (make-local-variable 'default-mime-charset-unlimited) nil)))
11696     (let ((ignored-charsets
11697            (or gnus-newsgroup-ephemeral-ignored-charsets
11698                (append
11699                 (and gnus-newsgroup-name
11700                      (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11701                 gnus-newsgroup-ignored-charsets)))
11702           charset)
11703       (setq gnus-newsgroup-charset
11704             (or gnus-newsgroup-ephemeral-charset
11705                 (when (and gnus-newsgroup-name
11706                            (setq charset (gnus-parameter-charset
11707                                           gnus-newsgroup-name)))
11708                   (make-local-variable 'default-mime-charset)
11709                   (setq default-mime-charset charset))
11710                 gnus-default-charset))
11711       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11712            ignored-charsets))))
11713
11714 ;;;
11715 ;;; Mime Commands
11716 ;;;
11717
11718 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11719   "Display the current article buffer fully MIME-buttonized.
11720 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11721 treated as multipart/mixed."
11722   (interactive "P")
11723   (require 'gnus-art)
11724   (let ((gnus-unbuttonized-mime-types nil)
11725         (gnus-mime-display-multipart-as-mixed show-all-parts))
11726     (gnus-summary-show-article)))
11727
11728 (defun gnus-summary-repair-multipart (article)
11729   "Add a Content-Type header to a multipart article without one."
11730   (interactive (list (gnus-summary-article-number)))
11731   (gnus-with-article article
11732     (message-narrow-to-head)
11733     (message-remove-header "Mime-Version")
11734     (goto-char (point-max))
11735     (insert "Mime-Version: 1.0\n")
11736     (widen)
11737     (when (search-forward "\n--" nil t)
11738       (let ((separator (buffer-substring (point) (point-at-eol))))
11739         (message-narrow-to-head)
11740         (message-remove-header "Content-Type")
11741         (goto-char (point-max))
11742         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11743                         separator))
11744         (widen))))
11745   (let (gnus-mark-article-hook)
11746     (gnus-summary-select-article t t nil article)))
11747
11748 (defun gnus-summary-toggle-display-buttonized ()
11749   "Toggle the buttonizing of the article buffer."
11750   (interactive)
11751   (require 'gnus-art)
11752   (if (setq gnus-inhibit-mime-unbuttonizing
11753             (not gnus-inhibit-mime-unbuttonizing))
11754       (let ((gnus-unbuttonized-mime-types nil))
11755         (gnus-summary-show-article))
11756     (gnus-summary-show-article)))
11757
11758 ;;;
11759 ;;; Intelli-mouse commmands
11760 ;;;
11761
11762 (defun gnus-wheel-summary-scroll (event)
11763   (interactive "e")
11764   (let ((amount (if (memq 'shift (event-modifiers event))
11765                     (car gnus-wheel-scroll-amount)
11766                   (cdr gnus-wheel-scroll-amount)))
11767         (direction (- (* (static-if (featurep 'xemacs)
11768                              (event-button event)
11769                            (cond ((eq 'mouse-4 (event-basic-type event))
11770                                   4)
11771                                  ((eq 'mouse-5 (event-basic-type event))
11772                                   5)))
11773                          2) 9))
11774         edge)
11775     (gnus-summary-scroll-up (* amount direction))
11776     (when (gnus-eval-in-buffer-window gnus-article-buffer
11777             (save-restriction
11778               (widen)
11779               (and (if (< 0 direction)
11780                        (gnus-article-next-page 0)
11781                      (gnus-article-prev-page 0)
11782                      (bobp))
11783                    (if (setq edge (get-text-property
11784                                    (point-min) 'gnus-wheel-edge))
11785                        (setq edge (* edge direction))
11786                      (setq edge -1))
11787                    (or (plusp edge)
11788                        (let ((buffer-read-only nil)
11789                              (inhibit-read-only t))
11790                          (put-text-property (point-min) (point-max)
11791                                             'gnus-wheel-edge direction)
11792                          nil))
11793                    (or (> edge gnus-wheel-edge-resistance)
11794                        (let ((buffer-read-only nil)
11795                              (inhibit-read-only t))
11796                          (put-text-property (point-min) (point-max)
11797                                             'gnus-wheel-edge
11798                                             (* (1+ edge) direction))
11799                          nil))
11800                    (eq last-command 'gnus-wheel-summary-scroll))))
11801       (gnus-summary-next-article nil nil (minusp direction)))))
11802
11803 (defun gnus-wheel-install ()
11804   "Enable mouse wheel support on summary window."
11805   (when gnus-use-wheel
11806     (let ((keys
11807            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
11808       (dolist (key keys)
11809         (define-key gnus-summary-mode-map key
11810           'gnus-wheel-summary-scroll)))))
11811
11812 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
11813
11814 ;;;
11815 ;;; Traditional PGP commmands
11816 ;;;
11817
11818 (defun gnus-summary-decrypt-article (&optional force)
11819   "Decrypt the current article in traditional PGP way.
11820 This will have permanent effect only in mail groups.
11821 If FORCE is non-nil, allow editing of articles even in read-only
11822 groups."
11823   (interactive "P")
11824   (gnus-summary-select-article t)
11825   (gnus-eval-in-buffer-window gnus-article-buffer
11826     (save-excursion
11827       (save-restriction
11828         (widen)
11829         (goto-char (point-min))
11830         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
11831           (error "Not a traditional PGP message!"))
11832         (let ((armor-start (match-beginning 0)))
11833           (if (and (pgg-decrypt-region armor-start (point-max))
11834                    (or force (not (gnus-group-read-only-p))))
11835               (let ((inhibit-read-only t)
11836                     buffer-read-only)
11837                 (delete-region armor-start
11838                                (progn
11839                                  (re-search-forward "^-+END PGP" nil t)
11840                                  (beginning-of-line 2)
11841                                  (point)))
11842                 (insert-buffer-substring pgg-output-buffer))))))))
11843
11844 (defun gnus-summary-verify-article ()
11845   "Verify the current article in traditional PGP way."
11846   (interactive)
11847   (save-excursion
11848     (set-buffer gnus-original-article-buffer)
11849     (goto-char (point-min))
11850     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
11851       (error "Not a traditional PGP message!"))
11852     (re-search-forward "^-+END PGP" nil t)
11853     (beginning-of-line 2)
11854     (call-interactively (function pgg-verify-region))))
11855
11856 ;;;
11857 ;;; Generic summary marking commands
11858 ;;;
11859
11860 (defvar gnus-summary-marking-alist
11861   '((read gnus-del-mark "d")
11862     (unread gnus-unread-mark "u")
11863     (ticked gnus-ticked-mark "!")
11864     (dormant gnus-dormant-mark "?")
11865     (expirable gnus-expirable-mark "e"))
11866   "An alist of names/marks/keystrokes.")
11867
11868 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11869 (defvar gnus-summary-mark-map)
11870
11871 (defun gnus-summary-make-all-marking-commands ()
11872   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11873   (dolist (elem gnus-summary-marking-alist)
11874     (apply 'gnus-summary-make-marking-command elem)))
11875
11876 (defun gnus-summary-make-marking-command (name mark keystroke)
11877   (let ((map (make-sparse-keymap)))
11878     (define-key gnus-summary-generic-mark-map keystroke map)
11879     (dolist (lway `((next "next" next nil "n")
11880                     (next-unread "next unread" next t "N")
11881                     (prev "previous" prev nil "p")
11882                     (prev-unread "previous unread" prev t "P")
11883                     (nomove "" nil nil ,keystroke)))
11884       (let ((func (gnus-summary-make-marking-command-1
11885                    mark (car lway) lway name)))
11886         (setq func (eval func))
11887         (define-key map (nth 4 lway) func)))))
11888
11889 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11890   `(defun ,(intern
11891             (format "gnus-summary-put-mark-as-%s%s"
11892                     name (if (eq way 'nomove)
11893                              ""
11894                            (concat "-" (symbol-name way)))))
11895      (n)
11896      ,(format
11897        "Mark the current article as %s%s.
11898 If N, the prefix, then repeat N times.
11899 If N is negative, move in reverse order.
11900 The difference between N and the actual number of articles marked is
11901 returned."
11902        name (car (cdr lway)))
11903      (interactive "p")
11904      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11905
11906 (defun gnus-summary-generic-mark (n mark move unread)
11907   "Mark N articles with MARK."
11908   (unless (eq major-mode 'gnus-summary-mode)
11909     (error "This command can only be used in the summary buffer"))
11910   (gnus-summary-show-thread)
11911   (let ((nummove
11912          (cond
11913           ((eq move 'next) 1)
11914           ((eq move 'prev) -1)
11915           (t 0))))
11916     (if (zerop nummove)
11917         (setq n 1)
11918       (when (< n 0)
11919         (setq n (abs n)
11920               nummove (* -1 nummove))))
11921     (while (and (> n 0)
11922                 (gnus-summary-mark-article nil mark)
11923                 (zerop (gnus-summary-next-subject nummove unread t)))
11924       (setq n (1- n)))
11925     (when (/= 0 n)
11926       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11927     (gnus-summary-recenter)
11928     (gnus-summary-position-point)
11929     (gnus-set-mode-line 'summary)
11930     n))
11931
11932 (defun gnus-summary-insert-articles (articles)
11933   (when (setq articles
11934               (gnus-sorted-difference articles
11935                                       (mapcar (lambda (h)
11936                                                 (mail-header-number h))
11937                                               gnus-newsgroup-headers)))
11938     (setq gnus-newsgroup-headers
11939           (gnus-merge 'list
11940                       gnus-newsgroup-headers
11941                       (gnus-fetch-headers articles)
11942                       'gnus-article-sort-by-number))
11943     ;; Suppress duplicates?
11944     (when gnus-suppress-duplicates
11945       (gnus-dup-suppress-articles))
11946
11947     ;; We might want to build some more threads first.
11948     (when (and gnus-fetch-old-headers
11949                (eq gnus-headers-retrieved-by 'nov))
11950       (if (eq gnus-fetch-old-headers 'invisible)
11951           (gnus-build-all-threads)
11952         (gnus-build-old-threads)))
11953     ;; Let the Gnus agent mark articles as read.
11954     (when gnus-agent
11955       (gnus-agent-get-undownloaded-list))
11956     ;; Remove list identifiers from subject
11957     (when gnus-list-identifiers
11958       (gnus-summary-remove-list-identifiers))
11959     ;; First and last article in this newsgroup.
11960     (when gnus-newsgroup-headers
11961       (setq gnus-newsgroup-begin
11962             (mail-header-number (car gnus-newsgroup-headers))
11963             gnus-newsgroup-end
11964             (mail-header-number
11965              (gnus-last-element gnus-newsgroup-headers))))
11966     (when gnus-use-scoring
11967       (gnus-possibly-score-headers))))
11968
11969 (defun gnus-summary-insert-old-articles (&optional all)
11970   "Insert all old articles in this group.
11971 If ALL is non-nil, already read articles become readable.
11972 If ALL is a number, fetch this number of articles."
11973   (interactive "P")
11974   (prog1
11975       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11976             older len)
11977         (setq older
11978               ;; Some nntp servers lie about their active range.  When
11979               ;; this happens, the active range can be in the millions.
11980               ;; Use a compressed range to avoid creating a huge list.
11981               (gnus-range-difference (list gnus-newsgroup-active) old))
11982         (setq len (gnus-range-length older))
11983         (cond
11984          ((null older) nil)
11985          ((numberp all)
11986           (if (< all len)
11987               (let ((older-range (nreverse older)))
11988                 (setq older nil)
11989
11990                 (while (> all 0)
11991                   (let* ((r (pop older-range))
11992                          (min (if (numberp r) r (car r)))
11993                          (max (if (numberp r) r (cdr r))))
11994                     (while (and (<= min max)
11995                                 (> all 0))
11996                       (push max older)
11997                       (setq all (1- all)
11998                             max (1- max))))))
11999             (setq older (gnus-uncompress-range older))))
12000          (all
12001           (setq older (gnus-uncompress-range older)))
12002          (t
12003           (when (and (numberp gnus-large-newsgroup)
12004                    (> len gnus-large-newsgroup))
12005               (let* ((cursor-in-echo-area nil)
12006                      (initial (gnus-parameter-large-newsgroup-initial
12007                                gnus-newsgroup-name))
12008                      (input
12009                       (read-string
12010                        (format
12011                         "How many articles from %s (%s %d): "
12012                         (gnus-limit-string
12013                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
12014                         (if initial "max" "default")
12015                         len)
12016                        (if initial
12017                            (cons (number-to-string initial)
12018                                  0)))))
12019                 (unless (string-match "^[ \t]*$" input)
12020                   (setq all (string-to-number input))
12021                   (if (< all len)
12022                       (let ((older-range (nreverse older)))
12023                         (setq older nil)
12024
12025                         (while (> all 0)
12026                           (let* ((r (pop older-range))
12027                                  (min (if (numberp r) r (car r)))
12028                                  (max (if (numberp r) r (cdr r))))
12029                             (while (and (<= min max)
12030                                         (> all 0))
12031                               (push max older)
12032                               (setq all (1- all)
12033                                     max (1- max))))))))))
12034           (setq older (gnus-uncompress-range older))))
12035         (if (not older)
12036             (message "No old news.")
12037           (gnus-summary-insert-articles older)
12038           (gnus-summary-limit (gnus-sorted-nunion old older))))
12039     (gnus-summary-position-point)))
12040
12041 (defun gnus-summary-insert-new-articles ()
12042   "Insert all new articles in this group."
12043   (interactive)
12044   (prog1
12045       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12046             (old-active gnus-newsgroup-active)
12047             (nnmail-fetched-sources (list t))
12048             i new)
12049         (setq gnus-newsgroup-active
12050               (gnus-activate-group gnus-newsgroup-name 'scan))
12051         (setq i (cdr gnus-newsgroup-active))
12052         (while (> i (cdr old-active))
12053           (push i new)
12054           (decf i))
12055         (if (not new)
12056             (message "No gnus is bad news")
12057           (gnus-summary-insert-articles new)
12058           (setq gnus-newsgroup-unreads
12059                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12060           (gnus-summary-limit (gnus-sorted-nunion old new))))
12061     (gnus-summary-position-point)))
12062
12063 (gnus-summary-make-all-marking-commands)
12064
12065 (gnus-ems-redefine)
12066
12067 (provide 'gnus-sum)
12068
12069 (run-hooks 'gnus-sum-load-hook)
12070
12071 ;; Local Variables:
12072 ;; coding: iso-8859-1
12073 ;; End:
12074
12075 ;;; gnus-sum.el ends here