5e1bd7b1b678f96e7c042c2d0f827c6a11a743a6
[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
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 (require 'cl))
32
33 (require 'gnus)
34 (require 'gnus-group)
35 (require 'gnus-spec)
36 (require 'gnus-range)
37 (require 'gnus-int)
38 (require 'gnus-undo)
39 (require 'gnus-util)
40 ;; Recursive :-(.
41 ;; (require 'gnus-art)
42 (require 'nnoo)
43 (require 'mime-view)
44
45 (eval-when-compile
46   (require 'mime-play)
47   (require 'static))
48
49 (eval-and-compile
50   (autoload 'gnus-cache-articles-in-group "gnus-cache")
51   (autoload 'pgg-decrypt-region "pgg" nil t)
52   (autoload 'pgg-verify-region "pgg" nil t))
53
54 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
55 (autoload 'gnus-cache-write-active "gnus-cache")
56 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
57 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
58 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
59 (autoload 'mm-uu-dissect "mm-uu")
60
61 (defcustom gnus-kill-summary-on-exit t
62   "*If non-nil, kill the summary buffer when you exit from it.
63 If nil, the summary will become a \"*Dead Summary*\" buffer, and
64 it will be killed sometime later."
65   :group 'gnus-summary-exit
66   :type 'boolean)
67
68 (defcustom gnus-fetch-old-headers nil
69   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
70 If an unread article in the group refers to an older, already read (or
71 just marked as read) article, the old article will not normally be
72 displayed in the Summary buffer.  If this variable is non-nil, Gnus
73 will attempt to grab the headers to the old articles, and thereby
74 build complete threads.  If it has the value `some', only enough
75 headers to connect otherwise loose threads will be displayed.  This
76 variable can also be a number.  In that case, no more than that number
77 of old headers will be fetched.  If it has the value `invisible', all
78 old headers will be fetched, but none will be displayed.
79
80 The server has to support NOV for any of this to work."
81   :group 'gnus-thread
82   :type '(choice (const :tag "off" nil)
83                  (const some)
84                  number
85                  (sexp :menu-tag "other" t)))
86
87 (defcustom gnus-refer-thread-limit 200
88   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
89 If t, fetch all the available old headers."
90   :group 'gnus-thread
91   :type '(choice number
92                  (sexp :menu-tag "other" t)))
93
94 (defcustom gnus-summary-make-false-root 'adopt
95   "*nil means that Gnus won't gather loose threads.
96 If the root of a thread has expired or been read in a previous
97 session, the information necessary to build a complete thread has been
98 lost.  Instead of having many small sub-threads from this original thread
99 scattered all over the summary buffer, Gnus can gather them.
100
101 If non-nil, Gnus will try to gather all loose sub-threads from an
102 original thread into one large thread.
103
104 If this variable is non-nil, it should be one of `none', `adopt',
105 `dummy' or `empty'.
106
107 If this variable is `none', Gnus will not make a false root, but just
108 present the sub-threads after another.
109 If this variable is `dummy', Gnus will create a dummy root that will
110 have all the sub-threads as children.
111 If this variable is `adopt', Gnus will make one of the \"children\"
112 the parent and mark all the step-children as such.
113 If this variable is `empty', the \"children\" are printed with empty
114 subject fields.  (Or rather, they will be printed with a string
115 given by the `gnus-summary-same-subject' variable.)"
116   :group 'gnus-thread
117   :type '(choice (const :tag "off" nil)
118                  (const none)
119                  (const dummy)
120                  (const adopt)
121                  (const empty)))
122
123 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
124   "*A regexp to match subjects to be excluded from loose thread gathering.
125 As loose thread gathering is done on subjects only, that means that
126 there can be many false gatherings performed.  By rooting out certain
127 common subjects, gathering might become saner."
128   :group 'gnus-thread
129   :type 'regexp)
130
131 (defcustom gnus-summary-gather-subject-limit nil
132   "*Maximum length of subject comparisons when gathering loose threads.
133 Use nil to compare full subjects.  Setting this variable to a low
134 number will help gather threads that have been corrupted by
135 newsreaders chopping off subject lines, but it might also mean that
136 unrelated articles that have subject that happen to begin with the
137 same few characters will be incorrectly gathered.
138
139 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
140 comparing subjects."
141   :group 'gnus-thread
142   :type '(choice (const :tag "off" nil)
143                  (const fuzzy)
144                  (sexp :menu-tag "on" t)))
145
146 (defcustom gnus-simplify-subject-functions nil
147   "List of functions taking a string argument that simplify subjects.
148 The functions are applied recursively.
149
150 Useful functions to put in this list include: `gnus-simplify-subject-re',
151 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
152   :group 'gnus-thread
153   :type '(repeat function))
154
155 (defcustom gnus-simplify-ignored-prefixes nil
156   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
157   :group 'gnus-thread
158   :type '(choice (const :tag "off" nil)
159                  regexp))
160
161 (defcustom gnus-build-sparse-threads nil
162   "*If non-nil, fill in the gaps in threads.
163 If `some', only fill in the gaps that are needed to tie loose threads
164 together.  If `more', fill in all leaf nodes that Gnus can find.  If
165 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
166   :group 'gnus-thread
167   :type '(choice (const :tag "off" nil)
168                  (const some)
169                  (const more)
170                  (sexp :menu-tag "all" t)))
171
172 (defcustom gnus-summary-thread-gathering-function
173   'gnus-gather-threads-by-subject
174   "*Function used for gathering loose threads.
175 There are two pre-defined functions: `gnus-gather-threads-by-subject',
176 which only takes Subjects into consideration; and
177 `gnus-gather-threads-by-references', which compared the References
178 headers of the articles to find matches."
179   :group 'gnus-thread
180   :type '(radio (function-item gnus-gather-threads-by-subject)
181                 (function-item gnus-gather-threads-by-references)
182                 (function :tag "other")))
183
184 (defcustom gnus-summary-same-subject ""
185   "*String indicating that the current article has the same subject as the previous.
186 This variable will only be used if the value of
187 `gnus-summary-make-false-root' is `empty'."
188   :group 'gnus-summary-format
189   :type 'string)
190
191 (defcustom gnus-summary-goto-unread t
192   "*If t, many commands will go to the next unread article.
193 This applies to marking commands as well as other commands that
194 \"naturally\" select the next article, like, for instance, `SPC' at
195 the end of an article.
196
197 If nil, the marking commands do NOT go to the next unread article
198 (they go to the next article instead).  If `never', commands that
199 usually go to the next unread article, will go to the next article,
200 whether it is read or not."
201   :group 'gnus-summary-marks
202   :link '(custom-manual "(gnus)Setting Marks")
203   :type '(choice (const :tag "off" nil)
204                  (const never)
205                  (sexp :menu-tag "on" t)))
206
207 (defcustom gnus-summary-default-score 0
208   "*Default article score level.
209 All scores generated by the score files will be added to this score.
210 If this variable is nil, scoring will be disabled."
211   :group 'gnus-score-default
212   :type '(choice (const :tag "disable")
213                  integer))
214
215 (defcustom gnus-summary-zcore-fuzz 0
216   "*Fuzziness factor for the zcore in the summary buffer.
217 Articles with scores closer than this to `gnus-summary-default-score'
218 will not be marked."
219   :group 'gnus-summary-format
220   :type 'integer)
221
222 (defcustom gnus-simplify-subject-fuzzy-regexp nil
223   "*Strings to be removed when doing fuzzy matches.
224 This can either be a regular expression or list of regular expressions
225 that will be removed from subject strings if fuzzy subject
226 simplification is selected."
227   :group 'gnus-thread
228   :type '(repeat regexp))
229
230 (defcustom gnus-show-threads t
231   "*If non-nil, display threads in summary mode."
232   :group 'gnus-thread
233   :type 'boolean)
234
235 (defcustom gnus-thread-hide-subtree nil
236   "*If non-nil, hide all threads initially.
237 If threads are hidden, you have to run the command
238 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
239 to expose hidden threads."
240   :group 'gnus-thread
241   :type 'boolean)
242
243 (defcustom gnus-thread-hide-killed t
244   "*If non-nil, hide killed threads automatically."
245   :group 'gnus-thread
246   :type 'boolean)
247
248 (defcustom gnus-thread-ignore-subject t
249   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
250 If nil, articles that have different subjects from their parents will
251 start separate threads."
252   :group 'gnus-thread
253   :type 'boolean)
254
255 (defcustom gnus-thread-operation-ignore-subject t
256   "*If non-nil, subjects will be ignored when doing thread commands.
257 This affects commands like `gnus-summary-kill-thread' and
258 `gnus-summary-lower-thread'.
259
260 If this variable is nil, articles in the same thread with different
261 subjects will not be included in the operation in question.  If this
262 variable is `fuzzy', only articles that have subjects that are fuzzily
263 equal will be included."
264   :group 'gnus-thread
265   :type '(choice (const :tag "off" nil)
266                  (const fuzzy)
267                  (sexp :tag "on" t)))
268
269 (defcustom gnus-thread-indent-level 4
270   "*Number that says how much each sub-thread should be indented."
271   :group 'gnus-thread
272   :type 'integer)
273
274 (defcustom gnus-auto-extend-newsgroup t
275   "*If non-nil, extend newsgroup forward and backward when requested."
276   :group 'gnus-summary-choose
277   :type 'boolean)
278
279 (defcustom gnus-auto-select-first t
280   "*If nil, don't select the first unread article when entering a group.
281 If this variable is `best', select the highest-scored unread article
282 in the group.  If t, select the first unread article.
283
284 This variable can also be a function to place point on a likely
285 subject line.  Useful values include `gnus-summary-first-unread-subject',
286 `gnus-summary-first-unread-article' and
287 `gnus-summary-best-unread-article'.
288
289 If you want to prevent automatic selection of the first unread article
290 in some newsgroups, set the variable to nil in
291 `gnus-select-group-hook'."
292   :group 'gnus-group-select
293   :type '(choice (const :tag "none" nil)
294                  (const best)
295                  (sexp :menu-tag "first" t)
296                  (function-item gnus-summary-first-unread-subject)
297                  (function-item gnus-summary-first-unread-article)
298                  (function-item gnus-summary-best-unread-article)))
299
300 (defcustom gnus-dont-select-after-jump-to-other-group nil
301   "If non-nil, don't select the first unread article after entering the
302 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
303 it is depend on the value of `gnus-auto-select-first' whether to select
304 or not."
305   :group 'gnus-group-select
306   :type 'boolean)
307
308 (defcustom gnus-auto-select-next t
309   "*If non-nil, offer to go to the next group from the end of the previous.
310 If the value is t and the next newsgroup is empty, Gnus will exit
311 summary mode and go back to group mode.  If the value is neither nil
312 nor t, Gnus will select the following unread newsgroup.  In
313 particular, if the value is the symbol `quietly', the next unread
314 newsgroup will be selected without any confirmation, and if it is
315 `almost-quietly', the next group will be selected without any
316 confirmation if you are located on the last article in the group.
317 Finally, if this variable is `slightly-quietly', the `Z n' command
318 will go to the next group without confirmation."
319   :group 'gnus-summary-maneuvering
320   :type '(choice (const :tag "off" nil)
321                  (const quietly)
322                  (const almost-quietly)
323                  (const slightly-quietly)
324                  (sexp :menu-tag "on" t)))
325
326 (defcustom gnus-auto-select-same nil
327   "*If non-nil, select the next article with the same subject.
328 If there are no more articles with the same subject, go to
329 the first unread article."
330   :group 'gnus-summary-maneuvering
331   :type 'boolean)
332
333 (defcustom gnus-summary-check-current nil
334   "*If non-nil, consider the current article when moving.
335 The \"unread\" movement commands will stay on the same line if the
336 current article is unread."
337   :group 'gnus-summary-maneuvering
338   :type 'boolean)
339
340 (defcustom gnus-auto-center-summary t
341   "*If non-nil, always center the current summary buffer.
342 In particular, if `vertical' do only vertical recentering.  If non-nil
343 and non-`vertical', do both horizontal and vertical recentering."
344   :group 'gnus-summary-maneuvering
345   :type '(choice (const :tag "none" nil)
346                  (const vertical)
347                  (integer :tag "height")
348                  (sexp :menu-tag "both" t)))
349
350 (defcustom gnus-show-all-headers nil
351   "*If non-nil, don't hide any headers."
352   :group 'gnus-article-hiding
353   :group 'gnus-article-headers
354   :type 'boolean)
355
356 (defcustom gnus-summary-ignore-duplicates nil
357   "*If non-nil, ignore articles with identical Message-ID headers."
358   :group 'gnus-summary
359   :type 'boolean)
360
361 (defcustom gnus-single-article-buffer t
362   "*If non-nil, display all articles in the same buffer.
363 If nil, each group will get its own article buffer."
364   :group 'gnus-article-various
365   :type 'boolean)
366
367 (defcustom gnus-break-pages t
368   "*If non-nil, do page breaking on articles.
369 The page delimiter is specified by the `gnus-page-delimiter'
370 variable."
371   :group 'gnus-article-various
372   :type 'boolean)
373
374 (defcustom gnus-show-mime t
375   "*If non-nil, do mime processing of articles.
376 The articles will simply be fed to the function given by
377 `gnus-article-display-method-for-mime'."
378   :group 'gnus-article-mime
379   :type 'boolean)
380
381 (defcustom gnus-move-split-methods nil
382   "*Variable used to suggest where articles are to be moved to.
383 It uses the same syntax as the `gnus-split-methods' variable.
384 However, whereas `gnus-split-methods' specifies file names as targets,
385 this variable specifies group names."
386   :group 'gnus-summary-mail
387   :type '(repeat (choice (list :value (fun) function)
388                          (cons :value ("" "") regexp (repeat string))
389                          (sexp :value nil))))
390
391 (defcustom gnus-unread-mark ?  ;Whitespace
392   "*Mark used for unread articles."
393   :group 'gnus-summary-marks
394   :type 'character)
395
396 (defcustom gnus-ticked-mark ?!
397   "*Mark used for ticked articles."
398   :group 'gnus-summary-marks
399   :type 'character)
400
401 (defcustom gnus-dormant-mark ??
402   "*Mark used for dormant articles."
403   :group 'gnus-summary-marks
404   :type 'character)
405
406 (defcustom gnus-del-mark ?r
407   "*Mark used for del'd articles."
408   :group 'gnus-summary-marks
409   :type 'character)
410
411 (defcustom gnus-read-mark ?R
412   "*Mark used for read articles."
413   :group 'gnus-summary-marks
414   :type 'character)
415
416 (defcustom gnus-expirable-mark ?E
417   "*Mark used for expirable articles."
418   :group 'gnus-summary-marks
419   :type 'character)
420
421 (defcustom gnus-killed-mark ?K
422   "*Mark used for killed articles."
423   :group 'gnus-summary-marks
424   :type 'character)
425
426 (defcustom gnus-souped-mark ?F
427   "*Mark used for killed articles."
428   :group 'gnus-summary-marks
429   :type 'character)
430
431 (defcustom gnus-kill-file-mark ?X
432   "*Mark used for articles killed by kill files."
433   :group 'gnus-summary-marks
434   :type 'character)
435
436 (defcustom gnus-low-score-mark ?Y
437   "*Mark used for articles with a low score."
438   :group 'gnus-summary-marks
439   :type 'character)
440
441 (defcustom gnus-catchup-mark ?C
442   "*Mark used for articles that are caught up."
443   :group 'gnus-summary-marks
444   :type 'character)
445
446 (defcustom gnus-replied-mark ?A
447   "*Mark used for articles that have been replied to."
448   :group 'gnus-summary-marks
449   :type 'character)
450
451 (defcustom gnus-cached-mark ?*
452   "*Mark used for articles that are in the cache."
453   :group 'gnus-summary-marks
454   :type 'character)
455
456 (defcustom gnus-saved-mark ?S
457   "*Mark used for articles that have been saved to."
458   :group 'gnus-summary-marks
459   :type 'character)
460
461 (defcustom gnus-ancient-mark ?O
462   "*Mark used for ancient articles."
463   :group 'gnus-summary-marks
464   :type 'character)
465
466 (defcustom gnus-sparse-mark ?Q
467   "*Mark used for sparsely reffed articles."
468   :group 'gnus-summary-marks
469   :type 'character)
470
471 (defcustom gnus-canceled-mark ?G
472   "*Mark used for canceled articles."
473   :group 'gnus-summary-marks
474   :type 'character)
475
476 (defcustom gnus-duplicate-mark ?M
477   "*Mark used for duplicate articles."
478   :group 'gnus-summary-marks
479   :type 'character)
480
481 (defcustom gnus-undownloaded-mark ?@
482   "*Mark used for articles that weren't downloaded."
483   :group 'gnus-summary-marks
484   :type 'character)
485
486 (defcustom gnus-downloadable-mark ?%
487   "*Mark used for articles that are to be downloaded."
488   :group 'gnus-summary-marks
489   :type 'character)
490
491 (defcustom gnus-unsendable-mark ?=
492   "*Mark used for articles that won't be sent."
493   :group 'gnus-summary-marks
494   :type 'character)
495
496 (defcustom gnus-score-over-mark ?+
497   "*Score mark used for articles with high scores."
498   :group 'gnus-summary-marks
499   :type 'character)
500
501 (defcustom gnus-score-below-mark ?-
502   "*Score mark used for articles with low scores."
503   :group 'gnus-summary-marks
504   :type 'character)
505
506 (defcustom gnus-empty-thread-mark ?  ;Whitespace
507   "*There is no thread under the article."
508   :group 'gnus-summary-marks
509   :type 'character)
510
511 (defcustom gnus-not-empty-thread-mark ?=
512   "*There is a thread under the article."
513   :group 'gnus-summary-marks
514   :type 'character)
515
516 (defcustom gnus-view-pseudo-asynchronously nil
517   "*If non-nil, Gnus will view pseudo-articles asynchronously."
518   :group 'gnus-extract-view
519   :type 'boolean)
520
521 (defcustom gnus-auto-expirable-marks
522   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
523         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
524         gnus-souped-mark gnus-duplicate-mark)
525   "*The list of marks converted into expiration if a group is auto-expirable."
526   :version "21.1"
527   :group 'gnus-summary
528   :type '(repeat character))
529
530 (defcustom gnus-inhibit-user-auto-expire t
531   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
532   :version "21.1"
533   :group 'gnus-summary
534   :type 'boolean)
535
536 (defcustom gnus-view-pseudos nil
537   "*If `automatic', pseudo-articles will be viewed automatically.
538 If `not-confirm', pseudos will be viewed automatically, and the user
539 will not be asked to confirm the command."
540   :group 'gnus-extract-view
541   :type '(choice (const :tag "off" nil)
542                  (const automatic)
543                  (const not-confirm)))
544
545 (defcustom gnus-view-pseudos-separately t
546   "*If non-nil, one pseudo-article will be created for each file to be viewed.
547 If nil, all files that use the same viewing command will be given as a
548 list of parameters to that command."
549   :group 'gnus-extract-view
550   :type 'boolean)
551
552 (defcustom gnus-insert-pseudo-articles t
553   "*If non-nil, insert pseudo-articles when decoding articles."
554   :group 'gnus-extract-view
555   :type 'boolean)
556
557 (defcustom gnus-summary-dummy-line-format
558   "  %(:                          :%) %S\n"
559   "*The format specification for the dummy roots in the summary buffer.
560 It works along the same lines as a normal formatting string,
561 with some simple extensions.
562
563 %S  The subject"
564   :group 'gnus-threading
565   :type 'string)
566
567 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
568   "*The format specification for the summary mode line.
569 It works along the same lines as a normal formatting string,
570 with some simple extensions:
571
572 %G  Group name
573 %p  Unprefixed group name
574 %A  Current article number
575 %z  Current article score
576 %V  Gnus version
577 %U  Number of unread articles in the group
578 %e  Number of unselected articles in the group
579 %Z  A string with unread/unselected article counts
580 %g  Shortish group name
581 %S  Subject of the current article
582 %u  User-defined spec
583 %s  Current score file name
584 %d  Number of dormant articles
585 %r  Number of articles that have been marked as read in this session
586 %E  Number of articles expunged by the score files"
587   :group 'gnus-summary-format
588   :type 'string)
589
590 (defcustom gnus-list-identifiers nil
591   "Regexp that matches list identifiers to be removed from subject.
592 This can also be a list of regexps."
593   :version "21.1"
594   :group 'gnus-summary-format
595   :group 'gnus-article-hiding
596   :type '(choice (const :tag "none" nil)
597                  (regexp :value ".*")
598                  (repeat :value (".*") regexp)))
599
600 (defcustom gnus-summary-mark-below 0
601   "*Mark all articles with a score below this variable as read.
602 This variable is local to each summary buffer and usually set by the
603 score file."
604   :group 'gnus-score-default
605   :type 'integer)
606
607 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
608   "*List of functions used for sorting articles in the summary buffer.
609 This variable is only used when not using a threaded display."
610   :group 'gnus-summary-sort
611   :type '(repeat (choice (function-item gnus-article-sort-by-number)
612                          (function-item gnus-article-sort-by-author)
613                          (function-item gnus-article-sort-by-subject)
614                          (function-item gnus-article-sort-by-date)
615                          (function-item gnus-article-sort-by-score)
616                          (function :tag "other"))))
617
618 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
619   "*List of functions used for sorting threads in the summary buffer.
620 By default, threads are sorted by article number.
621
622 Each function takes two threads and return non-nil if the first thread
623 should be sorted before the other.  If you use more than one function,
624 the primary sort function should be the last.  You should probably
625 always include `gnus-thread-sort-by-number' in the list of sorting
626 functions -- preferably first.
627
628 Ready-made functions include `gnus-thread-sort-by-number',
629 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
630 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
631 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
632   :group 'gnus-summary-sort
633   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
634                          (function-item gnus-thread-sort-by-author)
635                          (function-item gnus-thread-sort-by-subject)
636                          (function-item gnus-thread-sort-by-date)
637                          (function-item gnus-thread-sort-by-score)
638                          (function-item gnus-thread-sort-by-total-score)
639                          (function :tag "other"))))
640
641 (defcustom gnus-thread-score-function '+
642   "*Function used for calculating the total score of a thread.
643
644 The function is called with the scores of the article and each
645 subthread and should then return the score of the thread.
646
647 Some functions you can use are `+', `max', or `min'."
648   :group 'gnus-summary-sort
649   :type 'function)
650
651 (defcustom gnus-summary-expunge-below nil
652   "All articles that have a score less than this variable will be expunged.
653 This variable is local to the summary buffers."
654   :group 'gnus-score-default
655   :type '(choice (const :tag "off" nil)
656                  integer))
657
658 (defcustom gnus-thread-expunge-below nil
659   "All threads that have a total score less than this variable will be expunged.
660 See `gnus-thread-score-function' for en explanation of what a
661 \"thread score\" is.
662
663 This variable is local to the summary buffers."
664   :group 'gnus-threading
665   :group 'gnus-score-default
666   :type '(choice (const :tag "off" nil)
667                  integer))
668
669 (defcustom gnus-summary-mode-hook nil
670   "*A hook for Gnus summary mode.
671 This hook is run before any variables are set in the summary buffer."
672   :options '(turn-on-gnus-mailing-list-mode)
673   :group 'gnus-summary-various
674   :type 'hook)
675
676 (defcustom gnus-summary-menu-hook nil
677   "*Hook run after the creation of the summary mode menu."
678   :group 'gnus-summary-visual
679   :type 'hook)
680
681 (defcustom gnus-summary-exit-hook nil
682   "*A hook called on exit from the summary buffer.
683 It will be called with point in the group buffer."
684   :group 'gnus-summary-exit
685   :type 'hook)
686
687 (defcustom gnus-summary-prepare-hook nil
688   "*A hook called after the summary buffer has been generated.
689 If you want to modify the summary buffer, you can use this hook."
690   :group 'gnus-summary-various
691   :type 'hook)
692
693 (defcustom gnus-summary-prepared-hook nil
694   "*A hook called as the last thing after the summary buffer has been generated."
695   :group 'gnus-summary-various
696   :type 'hook)
697
698 (defcustom gnus-summary-generate-hook nil
699   "*A hook run just before generating the summary buffer.
700 This hook is commonly used to customize threading variables and the
701 like."
702   :group 'gnus-summary-various
703   :type 'hook)
704
705 (defcustom gnus-select-group-hook nil
706   "*A hook called when a newsgroup is selected.
707
708 If you'd like to simplify subjects like the
709 `gnus-summary-next-same-subject' command does, you can use the
710 following hook:
711
712  (setq gnus-select-group-hook
713       (list
714         (lambda ()
715           (mapcar (lambda (header)
716                      (mail-header-set-subject
717                       header
718                       (gnus-simplify-subject
719                        (mail-header-subject header) 're-only)))
720                   gnus-newsgroup-headers))))"
721   :group 'gnus-group-select
722   :type 'hook)
723
724 (defcustom gnus-select-article-hook nil
725   "*A hook called when an article is selected."
726   :group 'gnus-summary-choose
727   :type 'hook)
728
729 (defcustom gnus-visual-mark-article-hook
730   (list 'gnus-highlight-selected-summary)
731   "*Hook run after selecting an article in the summary buffer.
732 It is meant to be used for highlighting the article in some way.  It
733 is not run if `gnus-visual' is nil."
734   :group 'gnus-summary-visual
735   :type 'hook)
736
737 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
738   "*A hook called before parsing the headers."
739   :group 'gnus-various
740   :type 'hook)
741
742 (defcustom gnus-exit-group-hook nil
743   "*A hook called when exiting summary mode.
744 This hook is not called from the non-updating exit commands like `Q'."
745   :group 'gnus-various
746   :type 'hook)
747
748 (defcustom gnus-summary-update-hook
749   (list 'gnus-summary-highlight-line)
750   "*A hook called when a summary line is changed.
751 The hook will not be called if `gnus-visual' is nil.
752
753 The default function `gnus-summary-highlight-line' will
754 highlight the line according to the `gnus-summary-highlight'
755 variable."
756   :group 'gnus-summary-visual
757   :type 'hook)
758
759 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
760   "*A hook called when an article is selected for the first time.
761 The hook is intended to mark an article as read (or unread)
762 automatically when it is selected."
763   :group 'gnus-summary-choose
764   :type 'hook)
765
766 (defcustom gnus-group-no-more-groups-hook nil
767   "*A hook run when returning to group mode having no more (unread) groups."
768   :group 'gnus-group-select
769   :type 'hook)
770
771 (defcustom gnus-ps-print-hook nil
772   "*A hook run before ps-printing something from Gnus."
773   :group 'gnus-summary
774   :type 'hook)
775
776 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
777   "Face used for highlighting the current article in the summary buffer."
778   :group 'gnus-summary-visual
779   :type 'face)
780
781 (defcustom gnus-summary-highlight
782   '(((= mark gnus-canceled-mark)
783      . gnus-summary-cancelled-face)
784     ((and (> score default)
785           (or (= mark gnus-dormant-mark)
786               (= mark gnus-ticked-mark)))
787      . gnus-summary-high-ticked-face)
788     ((and (< score default)
789           (or (= mark gnus-dormant-mark)
790               (= mark gnus-ticked-mark)))
791      . gnus-summary-low-ticked-face)
792     ((or (= mark gnus-dormant-mark)
793          (= mark gnus-ticked-mark))
794      . gnus-summary-normal-ticked-face)
795     ((and (> score default) (= mark gnus-ancient-mark))
796      . gnus-summary-high-ancient-face)
797     ((and (< score default) (= mark gnus-ancient-mark))
798      . gnus-summary-low-ancient-face)
799     ((= mark gnus-ancient-mark)
800      . gnus-summary-normal-ancient-face)
801     ((and (> score default) (= mark gnus-unread-mark))
802      . gnus-summary-high-unread-face)
803     ((and (< score default) (= mark gnus-unread-mark))
804      . gnus-summary-low-unread-face)
805     ((and (memq article gnus-newsgroup-incorporated)
806           (= mark gnus-unread-mark))
807      . gnus-summary-incorporated-face)
808     ((= mark gnus-unread-mark)
809      . gnus-summary-normal-unread-face)
810     ((and (> score default) (memq mark (list gnus-downloadable-mark
811                                              gnus-undownloaded-mark)))
812      . gnus-summary-high-unread-face)
813     ((and (< score default) (memq mark (list gnus-downloadable-mark
814                                              gnus-undownloaded-mark)))
815      . gnus-summary-low-unread-face)
816     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
817      . gnus-summary-normal-unread-face)
818     ((> score default)
819      . gnus-summary-high-read-face)
820     ((< score default)
821      . gnus-summary-low-read-face)
822     (t
823      . gnus-summary-normal-read-face))
824   "*Controls the highlighting of summary buffer lines.
825
826 A list of (FORM . FACE) pairs.  When deciding how a a particular
827 summary line should be displayed, each form is evaluated.  The content
828 of the face field after the first true form is used.  You can change
829 how those summary lines are displayed, by editing the face field.
830
831 You can use the following variables in the FORM field.
832
833 score:   The articles score
834 default: The default article score.
835 below:   The score below which articles are automatically marked as read.
836 mark:    The articles mark."
837   :group 'gnus-summary-visual
838   :type '(repeat (cons (sexp :tag "Form" nil)
839                        face)))
840
841 (defcustom gnus-alter-header-function nil
842   "Function called to allow alteration of article header structures.
843 The function is called with one parameter, the article header vector,
844 which it may alter in any way.")
845
846 (defvar gnus-decode-encoded-word-function
847   (mime-find-field-decoder 'From 'nov)
848   "Variable that says which function should be used to decode a string with encoded words.")
849
850 (defcustom gnus-extra-headers nil
851   "*Extra headers to parse."
852   :version "21.1"
853   :group 'gnus-summary
854   :type '(repeat symbol))
855
856 (defcustom gnus-ignored-from-addresses
857   (and user-mail-address (regexp-quote user-mail-address))
858   "*Regexp of From headers that may be suppressed in favor of To headers."
859   :version "21.1"
860   :group 'gnus-summary
861   :type 'regexp)
862
863 (defcustom gnus-group-charset-alist
864   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
865     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
866     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
867     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
868     ("^relcom\\>" koi8-r)
869     ("^fido7\\>" koi8-r)
870     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
871     ("^israel\\>" iso-8859-1)
872     ("^han\\>" euc-kr)
873     ("^alt.chinese.text.big5\\>" chinese-big5)
874     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
875     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
876     (".*" iso-8859-1))
877   "Alist of regexps (to match group names) and default charsets to be used when reading."
878   :type '(repeat (list (regexp :tag "Group")
879                        (symbol :tag "Charset")))
880   :group 'gnus-charset)
881
882 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
883   "List of charsets that should be ignored.
884 When these charsets are used in the \"charset\" parameter, the
885 default charset will be used instead."
886   :version "21.1"
887   :type '(repeat symbol)
888   :group 'gnus-charset)
889
890 (defcustom gnus-group-ignored-charsets-alist
891   '(("alt\\.chinese\\.text" iso-8859-1))
892   "Alist of regexps (to match group names) and charsets that should be ignored.
893 When these charsets are used in the \"charset\" parameter, the
894 default charset will be used instead."
895   :type '(repeat (cons (regexp :tag "Group")
896                        (repeat symbol)))
897   :group 'gnus-charset)
898
899 (defcustom gnus-group-highlight-words-alist nil
900   "Alist of group regexps and highlight regexps.
901 This variable uses the same syntax as `gnus-emphasis-alist'."
902   :version "21.1"
903   :type '(repeat (cons (regexp :tag "Group")
904                        (repeat (list (regexp :tag "Highlight regexp")
905                                      (number :tag "Group for entire word" 0)
906                                      (number :tag "Group for displayed part" 0)
907                                      (symbol :tag "Face"
908                                              gnus-emphasis-highlight-words)))))
909   :group 'gnus-summary-visual)
910
911 (defcustom gnus-use-wheel nil
912   "Use Intelli-mouse on summary movement"
913   :type 'boolean
914   :group 'gnus-summary-maneuvering)
915
916 (defcustom gnus-wheel-scroll-amount '(5 . 1)
917   "Amount to scroll messages by spinning the mouse wheel.
918 This is actually a cons cell, where the first item is the amount to scroll
919 on a normal wheel event, and the second is the amount to scroll when the
920 wheel is moved with the shift key depressed."
921   :type '(cons (integer :tag "Shift") integer)
922   :group 'gnus-summary-maneuvering)
923
924 (defcustom gnus-wheel-edge-resistance 2
925   "How hard it should be to change the current article
926 by moving the mouse over the edge of the article window."
927   :type 'integer
928   :group 'gnus-summary-maneuvering)
929
930 (defcustom gnus-summary-show-article-charset-alist
931   nil
932   "Alist of number and charset.
933 The article will be shown with the charset corresponding to the
934 numbered argument.
935 For example: ((1 . cn-gb-2312) (2 . big5))."
936   :version "21.1"
937   :type '(repeat (cons (number :tag "Argument" 1)
938                        (symbol :tag "Charset")))
939   :group 'gnus-charset)
940
941 (defcustom gnus-preserve-marks t
942   "Whether marks are preserved when moving, copying and respooling messages."
943   :version "21.1"
944   :type 'boolean
945   :group 'gnus-summary-marks)
946
947 (defcustom gnus-alter-articles-to-read-function nil
948   "Function to be called to alter the list of articles to be selected."
949   :type 'function
950   :group 'gnus-summary)
951
952 (defcustom gnus-orphan-score nil
953   "*All orphans get this score added.  Set in the score file."
954   :group 'gnus-score-default
955   :type '(choice (const nil)
956                  integer))
957
958 (defcustom gnus-summary-save-parts-default-mime "image/.*"
959   "*A regexp to match MIME parts when saving multiple parts of a message
960 with gnus-summary-save-parts (X m). This regexp will be used by default
961 when prompting the user for which type of files to save."
962   :group 'gnus-summary
963   :type 'regexp)
964
965
966 (defcustom gnus-summary-save-parts-default-mime "image/.*"
967   "*A regexp to match MIME parts when saving multiple parts of a message
968 with gnus-summary-save-parts (X m). This regexp will be used by default
969 when prompting the user for which type of files to save."
970   :group 'gnus-summary
971   :type 'regexp)
972
973
974 ;;; Internal variables
975
976 (defvar gnus-article-mime-handles nil)
977 (defvar gnus-article-decoded-p nil)
978 (defvar gnus-article-charset nil)
979 (defvar gnus-article-ignored-charsets nil)
980 (defvar gnus-scores-exclude-files nil)
981 (defvar gnus-page-broken nil)
982 (defvar gnus-inhibit-mime-unbuttonizing nil)
983
984 (defvar gnus-original-article nil)
985 (defvar gnus-article-internal-prepare-hook nil)
986 (defvar gnus-newsgroup-process-stack nil)
987
988 (defvar gnus-thread-indent-array nil)
989 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
990 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
991   "Function called to sort the articles within a thread after it has been gathered together.")
992
993 (defvar gnus-summary-save-parts-type-history nil)
994 (defvar gnus-summary-save-parts-last-directory nil)
995
996 (defvar gnus-summary-save-parts-type-history nil)
997 (defvar gnus-summary-save-parts-last-directory nil)
998
999 ;; Avoid highlighting in kill files.
1000 (defvar gnus-summary-inhibit-highlight nil)
1001 (defvar gnus-newsgroup-selected-overlay nil)
1002 (defvar gnus-inhibit-limiting nil)
1003 (defvar gnus-newsgroup-adaptive-score-file nil)
1004 (defvar gnus-current-score-file nil)
1005 (defvar gnus-current-move-group nil)
1006 (defvar gnus-current-copy-group nil)
1007 (defvar gnus-current-crosspost-group nil)
1008
1009 (defvar gnus-newsgroup-dependencies nil)
1010 (defvar gnus-newsgroup-adaptive nil)
1011 (defvar gnus-summary-display-article-function nil)
1012 (defvar gnus-summary-highlight-line-function nil
1013   "Function called after highlighting a summary line.")
1014
1015 (defvar gnus-summary-line-format-alist
1016   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1017     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1018     (?s gnus-tmp-subject-or-nil ?s)
1019     (?n gnus-tmp-name ?s)
1020     (?A (std11-address-string
1021          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1022     (?a (or (std11-full-name-string
1023              (car (mime-entity-read-field gnus-tmp-header 'From)))
1024             gnus-tmp-from) ?s)
1025     (?F gnus-tmp-from ?s)
1026     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1027     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1028     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1029     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1030     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1031     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1032     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1033     (?L gnus-tmp-lines ?d)
1034     (?I gnus-tmp-indentation ?s)
1035     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1036     (?R gnus-tmp-replied ?c)
1037     (?\[ gnus-tmp-opening-bracket ?c)
1038     (?\] gnus-tmp-closing-bracket ?c)
1039     (?\> (make-string gnus-tmp-level ? ) ?s)
1040     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1041     (?i gnus-tmp-score ?d)
1042     (?z gnus-tmp-score-char ?c)
1043     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1044     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1045     (?U gnus-tmp-unread ?c)
1046     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1047     (?t (gnus-summary-number-of-articles-in-thread
1048          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1049         ?d)
1050     (?e (gnus-summary-number-of-articles-in-thread
1051          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1052         ?c)
1053     (?u gnus-tmp-user-defined ?s)
1054     (?P (gnus-pick-line-number) ?d))
1055   "An alist of format specifications that can appear in summary lines.
1056 These are paired with what variables they correspond with, along with
1057 the type of the variable (string, integer, character, etc).")
1058
1059 (defvar gnus-summary-dummy-line-format-alist
1060   `((?S gnus-tmp-subject ?s)
1061     (?N gnus-tmp-number ?d)
1062     (?u gnus-tmp-user-defined ?s)))
1063
1064 (defvar gnus-summary-mode-line-format-alist
1065   `((?G gnus-tmp-group-name ?s)
1066     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1067     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1068     (?A gnus-tmp-article-number ?d)
1069     (?Z gnus-tmp-unread-and-unselected ?s)
1070     (?V gnus-version ?s)
1071     (?U gnus-tmp-unread-and-unticked ?d)
1072     (?S gnus-tmp-subject ?s)
1073     (?e gnus-tmp-unselected ?d)
1074     (?u gnus-tmp-user-defined ?s)
1075     (?d (length gnus-newsgroup-dormant) ?d)
1076     (?t (length gnus-newsgroup-marked) ?d)
1077     (?r (length gnus-newsgroup-reads) ?d)
1078     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1079     (?E gnus-newsgroup-expunged-tally ?d)
1080     (?s (gnus-current-score-file-nondirectory) ?s)))
1081
1082 (defvar gnus-last-search-regexp nil
1083   "Default regexp for article search command.")
1084
1085 (defvar gnus-summary-search-article-matched-data nil
1086   "Last matched data of article search command.  It is the local variable
1087 in `gnus-article-buffer' which consists of the list of start position,
1088 end position and text.")
1089
1090 (defvar gnus-last-shell-command nil
1091   "Default shell command on article.")
1092
1093 (defvar gnus-newsgroup-begin nil)
1094 (defvar gnus-newsgroup-end nil)
1095 (defvar gnus-newsgroup-last-rmail nil)
1096 (defvar gnus-newsgroup-last-mail nil)
1097 (defvar gnus-newsgroup-last-folder nil)
1098 (defvar gnus-newsgroup-last-file nil)
1099 (defvar gnus-newsgroup-auto-expire nil)
1100 (defvar gnus-newsgroup-active nil)
1101
1102 (defvar gnus-newsgroup-data nil)
1103 (defvar gnus-newsgroup-data-reverse nil)
1104 (defvar gnus-newsgroup-limit nil)
1105 (defvar gnus-newsgroup-limits nil)
1106
1107 (defvar gnus-newsgroup-unreads nil
1108   "List of unread articles in the current newsgroup.")
1109
1110 (defvar gnus-newsgroup-unselected nil
1111   "List of unselected unread articles in the current newsgroup.")
1112
1113 (defvar gnus-newsgroup-reads nil
1114   "Alist of read articles and article marks in the current newsgroup.")
1115
1116 (defvar gnus-newsgroup-expunged-tally nil)
1117
1118 (defvar gnus-newsgroup-marked nil
1119   "List of ticked articles in the current newsgroup (a subset of unread art).")
1120
1121 (defvar gnus-newsgroup-killed nil
1122   "List of ranges of articles that have been through the scoring process.")
1123
1124 (defvar gnus-newsgroup-cached nil
1125   "List of articles that come from the article cache.")
1126
1127 (defvar gnus-newsgroup-saved nil
1128   "List of articles that have been saved.")
1129
1130 (defvar gnus-newsgroup-kill-headers nil)
1131
1132 (defvar gnus-newsgroup-replied nil
1133   "List of articles that have been replied to in the current newsgroup.")
1134
1135 (defvar gnus-newsgroup-expirable nil
1136   "List of articles in the current newsgroup that can be expired.")
1137
1138 (defvar gnus-newsgroup-processable nil
1139   "List of articles in the current newsgroup that can be processed.")
1140
1141 (defvar gnus-newsgroup-downloadable nil
1142   "List of articles in the current newsgroup that can be processed.")
1143
1144 (defvar gnus-newsgroup-undownloaded nil
1145   "List of articles in the current newsgroup that haven't been downloaded..")
1146
1147 (defvar gnus-newsgroup-unsendable nil
1148   "List of articles in the current newsgroup that won't be sent.")
1149
1150 (defvar gnus-newsgroup-bookmarks nil
1151   "List of articles in the current newsgroup that have bookmarks.")
1152
1153 (defvar gnus-newsgroup-dormant nil
1154   "List of dormant articles in the current newsgroup.")
1155
1156 (defvar gnus-newsgroup-scored nil
1157   "List of scored articles in the current newsgroup.")
1158
1159 (defvar gnus-newsgroup-incorporated nil
1160   "List of incorporated articles in the current newsgroup.")
1161
1162 (defvar gnus-newsgroup-headers nil
1163   "List of article headers in the current newsgroup.")
1164
1165 (defvar gnus-newsgroup-threads nil)
1166
1167 (defvar gnus-newsgroup-prepared nil
1168   "Whether the current group has been prepared properly.")
1169
1170 (defvar gnus-newsgroup-ancient nil
1171   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1172
1173 (defvar gnus-newsgroup-sparse nil)
1174
1175 (defvar gnus-current-article nil)
1176 (defvar gnus-article-current nil)
1177 (defvar gnus-current-headers nil)
1178 (defvar gnus-have-all-headers nil)
1179 (defvar gnus-last-article nil)
1180 (defvar gnus-newsgroup-history nil)
1181 (defvar gnus-newsgroup-charset nil)
1182 (defvar gnus-newsgroup-ephemeral-charset nil)
1183 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1184
1185 (defvar gnus-article-before-search nil)
1186
1187 (defconst gnus-summary-local-variables
1188   '(gnus-newsgroup-name
1189     gnus-newsgroup-begin gnus-newsgroup-end
1190     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1191     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1192     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1193     gnus-newsgroup-unselected gnus-newsgroup-marked
1194     gnus-newsgroup-reads gnus-newsgroup-saved
1195     gnus-newsgroup-replied gnus-newsgroup-expirable
1196     gnus-newsgroup-processable gnus-newsgroup-killed
1197     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1198     gnus-newsgroup-unsendable
1199     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1200     gnus-newsgroup-headers gnus-newsgroup-threads
1201     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1202     gnus-current-article gnus-current-headers gnus-have-all-headers
1203     gnus-last-article gnus-article-internal-prepare-hook
1204     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1205     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1206     gnus-thread-expunge-below
1207     gnus-score-alist gnus-current-score-file
1208     (gnus-summary-expunge-below . global)
1209     (gnus-summary-mark-below . global)
1210     (gnus-orphan-score . global)
1211     gnus-newsgroup-active gnus-scores-exclude-files
1212     gnus-newsgroup-history gnus-newsgroup-ancient
1213     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1214     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1215     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1216     (gnus-newsgroup-expunged-tally . 0)
1217     gnus-cache-removable-articles gnus-newsgroup-cached
1218     gnus-newsgroup-data gnus-newsgroup-data-reverse
1219     gnus-newsgroup-limit gnus-newsgroup-limits
1220     gnus-newsgroup-charset
1221     gnus-newsgroup-incorporated)
1222   "Variables that are buffer-local to the summary buffers.")
1223
1224 (defvar gnus-newsgroup-variables nil
1225   "Variables that have separate values in the newsgroups.")
1226
1227 ;; Byte-compiler warning.
1228 (eval-when-compile (defvar gnus-article-mode-map))
1229
1230 ;; Subject simplification.
1231
1232 (defun gnus-simplify-whitespace (str)
1233   "Remove excessive whitespace from STR."
1234   (let ((mystr str))
1235     ;; Multiple spaces.
1236     (while (string-match "[ \t][ \t]+" mystr)
1237       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1238                           " "
1239                           (substring mystr (match-end 0)))))
1240     ;; Leading spaces.
1241     (when (string-match "^[ \t]+" mystr)
1242       (setq mystr (substring mystr (match-end 0))))
1243     ;; Trailing spaces.
1244     (when (string-match "[ \t]+$" mystr)
1245       (setq mystr (substring mystr 0 (match-beginning 0))))
1246     mystr))
1247
1248 (defsubst gnus-simplify-subject-re (subject)
1249   "Remove \"Re:\" from subject lines."
1250   (if (string-match "^[Rr][Ee]: *" subject)
1251       (substring subject (match-end 0))
1252     subject))
1253
1254 (defun gnus-simplify-subject (subject &optional re-only)
1255   "Remove `Re:' and words in parentheses.
1256 If RE-ONLY is non-nil, strip leading `Re:'s only."
1257   (let ((case-fold-search t))           ;Ignore case.
1258     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1259     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1260       (setq subject (substring subject (match-end 0))))
1261     ;; Remove uninteresting prefixes.
1262     (when (and (not re-only)
1263                gnus-simplify-ignored-prefixes
1264                (string-match gnus-simplify-ignored-prefixes subject))
1265       (setq subject (substring subject (match-end 0))))
1266     ;; Remove words in parentheses from end.
1267     (unless re-only
1268       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1269         (setq subject (substring subject 0 (match-beginning 0)))))
1270     ;; Return subject string.
1271     subject))
1272
1273 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1274 ;; all whitespace.
1275 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1276   (goto-char (point-min))
1277   (while (re-search-forward regexp nil t)
1278     (replace-match (or newtext ""))))
1279
1280 (defun gnus-simplify-buffer-fuzzy ()
1281   "Simplify string in the buffer fuzzily.
1282 The string in the accessible portion of the current buffer is simplified.
1283 It is assumed to be a single-line subject.
1284 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1285 matter is removed.  Additional things can be deleted by setting
1286 `gnus-simplify-subject-fuzzy-regexp'."
1287   (let ((case-fold-search t)
1288         (modified-tick))
1289     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1290
1291     (while (not (eq modified-tick (buffer-modified-tick)))
1292       (setq modified-tick (buffer-modified-tick))
1293       (cond
1294        ((listp gnus-simplify-subject-fuzzy-regexp)
1295         (mapcar 'gnus-simplify-buffer-fuzzy-step
1296                 gnus-simplify-subject-fuzzy-regexp))
1297        (gnus-simplify-subject-fuzzy-regexp
1298         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1299       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1300       (gnus-simplify-buffer-fuzzy-step
1301        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1302       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1303
1304     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1305     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1306     (gnus-simplify-buffer-fuzzy-step " $")
1307     (gnus-simplify-buffer-fuzzy-step "^ +")))
1308
1309 (defun gnus-simplify-subject-fuzzy (subject)
1310   "Simplify a subject string fuzzily.
1311 See `gnus-simplify-buffer-fuzzy' for details."
1312   (save-excursion
1313     (gnus-set-work-buffer)
1314     (let ((case-fold-search t))
1315       ;; Remove uninteresting prefixes.
1316       (when (and gnus-simplify-ignored-prefixes
1317                  (string-match gnus-simplify-ignored-prefixes subject))
1318         (setq subject (substring subject (match-end 0))))
1319       (insert subject)
1320       (inline (gnus-simplify-buffer-fuzzy))
1321       (buffer-string))))
1322
1323 (defsubst gnus-simplify-subject-fully (subject)
1324   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1325   (cond
1326    (gnus-simplify-subject-functions
1327     (gnus-map-function gnus-simplify-subject-functions subject))
1328    ((null gnus-summary-gather-subject-limit)
1329     (gnus-simplify-subject-re subject))
1330    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1331     (gnus-simplify-subject-fuzzy subject))
1332    ((numberp gnus-summary-gather-subject-limit)
1333     (gnus-limit-string (gnus-simplify-subject-re subject)
1334                        gnus-summary-gather-subject-limit))
1335    (t
1336     subject)))
1337
1338 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1339   "Check whether two subjects are equal.
1340 If optional argument simple-first is t, first argument is already
1341 simplified."
1342   (cond
1343    ((null simple-first)
1344     (equal (gnus-simplify-subject-fully s1)
1345            (gnus-simplify-subject-fully s2)))
1346    (t
1347     (equal s1
1348            (gnus-simplify-subject-fully s2)))))
1349
1350 (defun gnus-summary-bubble-group ()
1351   "Increase the score of the current group.
1352 This is a handy function to add to `gnus-summary-exit-hook' to
1353 increase the score of each group you read."
1354   (gnus-group-add-score gnus-newsgroup-name))
1355
1356 \f
1357 ;;;
1358 ;;; Gnus summary mode
1359 ;;;
1360
1361 (put 'gnus-summary-mode 'mode-class 'special)
1362
1363 (when t
1364   ;; Non-orthogonal keys
1365
1366   (gnus-define-keys gnus-summary-mode-map
1367     " " gnus-summary-next-page
1368     "\177" gnus-summary-prev-page
1369     [delete] gnus-summary-prev-page
1370     [backspace] gnus-summary-prev-page
1371     "\r" gnus-summary-scroll-up
1372     "\M-\r" gnus-summary-scroll-down
1373     "n" gnus-summary-next-unread-article
1374     "p" gnus-summary-prev-unread-article
1375     "N" gnus-summary-next-article
1376     "P" gnus-summary-prev-article
1377     "\M-\C-n" gnus-summary-next-same-subject
1378     "\M-\C-p" gnus-summary-prev-same-subject
1379     "\M-n" gnus-summary-next-unread-subject
1380     "\M-p" gnus-summary-prev-unread-subject
1381     "." gnus-summary-first-unread-article
1382     "," gnus-summary-best-unread-article
1383     "\M-s" gnus-summary-search-article-forward
1384     "\M-r" gnus-summary-search-article-backward
1385     "<" gnus-summary-beginning-of-article
1386     ">" gnus-summary-end-of-article
1387     "j" gnus-summary-goto-article
1388     "^" gnus-summary-refer-parent-article
1389     "\M-^" gnus-summary-refer-article
1390     "u" gnus-summary-tick-article-forward
1391     "!" gnus-summary-tick-article-forward
1392     "U" gnus-summary-tick-article-backward
1393     "d" gnus-summary-mark-as-read-forward
1394     "D" gnus-summary-mark-as-read-backward
1395     "E" gnus-summary-mark-as-expirable
1396     "\M-u" gnus-summary-clear-mark-forward
1397     "\M-U" gnus-summary-clear-mark-backward
1398     "k" gnus-summary-kill-same-subject-and-select
1399     "\C-k" gnus-summary-kill-same-subject
1400     "\M-\C-k" gnus-summary-kill-thread
1401     "\M-\C-l" gnus-summary-lower-thread
1402     "e" gnus-summary-edit-article
1403     "#" gnus-summary-mark-as-processable
1404     "\M-#" gnus-summary-unmark-as-processable
1405     "\M-\C-t" gnus-summary-toggle-threads
1406     "\M-\C-s" gnus-summary-show-thread
1407     "\M-\C-h" gnus-summary-hide-thread
1408     "\M-\C-f" gnus-summary-next-thread
1409     "\M-\C-b" gnus-summary-prev-thread
1410     [(meta down)] gnus-summary-next-thread
1411     [(meta up)] gnus-summary-prev-thread
1412     "\M-\C-u" gnus-summary-up-thread
1413     "\M-\C-d" gnus-summary-down-thread
1414     "&" gnus-summary-execute-command
1415     "c" gnus-summary-catchup-and-exit
1416     "\C-w" gnus-summary-mark-region-as-read
1417     "\C-t" gnus-summary-toggle-truncation
1418     "?" gnus-summary-mark-as-dormant
1419     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1420     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1421     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1422     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1423     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1424     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1425     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1426     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1427     "=" gnus-summary-expand-window
1428     "\C-x\C-s" gnus-summary-reselect-current-group
1429     "\M-g" gnus-summary-rescan-group
1430     "w" gnus-summary-stop-page-breaking
1431     "\C-c\C-r" gnus-summary-caesar-message
1432     "\M-t" gnus-summary-toggle-mime
1433     "f" gnus-summary-followup
1434     "F" gnus-summary-followup-with-original
1435     "C" gnus-summary-cancel-article
1436     "r" gnus-summary-reply
1437     "R" gnus-summary-reply-with-original
1438     "\C-c\C-f" gnus-summary-mail-forward
1439     "o" gnus-summary-save-article
1440     "\C-o" gnus-summary-save-article-mail
1441     "|" gnus-summary-pipe-output
1442     "\M-k" gnus-summary-edit-local-kill
1443     "\M-K" gnus-summary-edit-global-kill
1444     ;; "V" gnus-version
1445     "\C-c\C-d" gnus-summary-describe-group
1446     "q" gnus-summary-exit
1447     "Q" gnus-summary-exit-no-update
1448     "\C-c\C-i" gnus-info-find-node
1449     gnus-mouse-2 gnus-mouse-pick-article
1450     "m" gnus-summary-mail-other-window
1451     "a" gnus-summary-post-news
1452     "x" gnus-summary-limit-to-unread
1453     "s" gnus-summary-isearch-article
1454     "t" gnus-article-toggle-headers
1455     "g" gnus-summary-show-article
1456     "l" gnus-summary-goto-last-article
1457     "v" gnus-summary-preview-mime-message
1458     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1459     "\C-d" gnus-summary-enter-digest-group
1460     "\M-\C-d" gnus-summary-read-document
1461     "\M-\C-e" gnus-summary-edit-parameters
1462     "\M-\C-a" gnus-summary-customize-parameters
1463     "\C-c\C-b" gnus-bug
1464     "*" gnus-cache-enter-article
1465     "\M-*" gnus-cache-remove-article
1466     "\M-&" gnus-summary-universal-argument
1467     "\C-l" gnus-recenter
1468     "I" gnus-summary-increase-score
1469     "L" gnus-summary-lower-score
1470     "\M-i" gnus-symbolic-argument
1471     "h" gnus-summary-select-article-buffer
1472
1473     "V" gnus-summary-score-map
1474     "X" gnus-uu-extract-map
1475     "S" gnus-summary-send-map)
1476
1477   ;; Sort of orthogonal keymap
1478   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1479     "t" gnus-summary-tick-article-forward
1480     "!" gnus-summary-tick-article-forward
1481     "d" gnus-summary-mark-as-read-forward
1482     "r" gnus-summary-mark-as-read-forward
1483     "c" gnus-summary-clear-mark-forward
1484     " " gnus-summary-clear-mark-forward
1485     "e" gnus-summary-mark-as-expirable
1486     "x" gnus-summary-mark-as-expirable
1487     "?" gnus-summary-mark-as-dormant
1488     "b" gnus-summary-set-bookmark
1489     "B" gnus-summary-remove-bookmark
1490     "#" gnus-summary-mark-as-processable
1491     "\M-#" gnus-summary-unmark-as-processable
1492     "S" gnus-summary-limit-include-expunged
1493     "C" gnus-summary-catchup
1494     "H" gnus-summary-catchup-to-here
1495     "\C-c" gnus-summary-catchup-all
1496     "k" gnus-summary-kill-same-subject-and-select
1497     "K" gnus-summary-kill-same-subject
1498     "P" gnus-uu-mark-map)
1499
1500   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1501     "c" gnus-summary-clear-above
1502     "u" gnus-summary-tick-above
1503     "m" gnus-summary-mark-above
1504     "k" gnus-summary-kill-below)
1505
1506   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1507     "/" gnus-summary-limit-to-subject
1508     "n" gnus-summary-limit-to-articles
1509     "w" gnus-summary-pop-limit
1510     "s" gnus-summary-limit-to-subject
1511     "a" gnus-summary-limit-to-author
1512     "u" gnus-summary-limit-to-unread
1513     "m" gnus-summary-limit-to-marks
1514     "M" gnus-summary-limit-exclude-marks
1515     "v" gnus-summary-limit-to-score
1516     "*" gnus-summary-limit-include-cached
1517     "D" gnus-summary-limit-include-dormant
1518     "T" gnus-summary-limit-include-thread
1519     "d" gnus-summary-limit-exclude-dormant
1520     "t" gnus-summary-limit-to-age
1521     "x" gnus-summary-limit-to-extra
1522     "E" gnus-summary-limit-include-expunged
1523     "c" gnus-summary-limit-exclude-childless-dormant
1524     "C" gnus-summary-limit-mark-excluded-as-read)
1525
1526   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1527     "n" gnus-summary-next-unread-article
1528     "p" gnus-summary-prev-unread-article
1529     "N" gnus-summary-next-article
1530     "P" gnus-summary-prev-article
1531     "\C-n" gnus-summary-next-same-subject
1532     "\C-p" gnus-summary-prev-same-subject
1533     "\M-n" gnus-summary-next-unread-subject
1534     "\M-p" gnus-summary-prev-unread-subject
1535     "f" gnus-summary-first-unread-article
1536     "b" gnus-summary-best-unread-article
1537     "j" gnus-summary-goto-article
1538     "g" gnus-summary-goto-subject
1539     "l" gnus-summary-goto-last-article
1540     "o" gnus-summary-pop-article)
1541
1542   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1543     "k" gnus-summary-kill-thread
1544     "l" gnus-summary-lower-thread
1545     "i" gnus-summary-raise-thread
1546     "T" gnus-summary-toggle-threads
1547     "t" gnus-summary-rethread-current
1548     "^" gnus-summary-reparent-thread
1549     "s" gnus-summary-show-thread
1550     "S" gnus-summary-show-all-threads
1551     "h" gnus-summary-hide-thread
1552     "H" gnus-summary-hide-all-threads
1553     "n" gnus-summary-next-thread
1554     "p" gnus-summary-prev-thread
1555     "u" gnus-summary-up-thread
1556     "o" gnus-summary-top-thread
1557     "d" gnus-summary-down-thread
1558     "#" gnus-uu-mark-thread
1559     "\M-#" gnus-uu-unmark-thread)
1560
1561   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1562     "g" gnus-summary-prepare
1563     "c" gnus-summary-insert-cached-articles)
1564
1565   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1566     "c" gnus-summary-catchup-and-exit
1567     "C" gnus-summary-catchup-all-and-exit
1568     "E" gnus-summary-exit-no-update
1569     "J" gnus-summary-jump-to-other-group
1570     "Q" gnus-summary-exit
1571     "Z" gnus-summary-exit
1572     "n" gnus-summary-catchup-and-goto-next-group
1573     "R" gnus-summary-reselect-current-group
1574     "G" gnus-summary-rescan-group
1575     "N" gnus-summary-next-group
1576     "s" gnus-summary-save-newsrc
1577     "P" gnus-summary-prev-group)
1578
1579   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1580     " " gnus-summary-next-page
1581     "n" gnus-summary-next-page
1582     "\177" gnus-summary-prev-page
1583     [delete] gnus-summary-prev-page
1584     "p" gnus-summary-prev-page
1585     "\r" gnus-summary-scroll-up
1586     "\M-\r" gnus-summary-scroll-down
1587     "<" gnus-summary-beginning-of-article
1588     ">" gnus-summary-end-of-article
1589     "b" gnus-summary-beginning-of-article
1590     "e" gnus-summary-end-of-article
1591     "^" gnus-summary-refer-parent-article
1592     "r" gnus-summary-refer-parent-article
1593     "D" gnus-summary-enter-digest-group
1594     "R" gnus-summary-refer-references
1595     "T" gnus-summary-refer-thread
1596     "g" gnus-summary-show-article
1597     "s" gnus-summary-isearch-article
1598     "P" gnus-summary-print-article
1599     "M" gnus-mailing-list-insinuate
1600     "t" gnus-article-babel)
1601
1602   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1603     "b" gnus-article-add-buttons
1604     "B" gnus-article-add-buttons-to-head
1605     "o" gnus-article-treat-overstrike
1606     "e" gnus-article-emphasize
1607     "w" gnus-article-fill-cited-article
1608     "Q" gnus-article-fill-long-lines
1609     "C" gnus-article-capitalize-sentences
1610     "c" gnus-article-remove-cr
1611     "Z" gnus-article-decode-HZ
1612     "f" gnus-article-display-x-face
1613     "l" gnus-summary-stop-page-breaking
1614     "r" gnus-summary-caesar-message
1615     "t" gnus-article-toggle-headers
1616     "v" gnus-summary-verbose-headers
1617     "m" gnus-summary-toggle-mime
1618     "H" gnus-article-strip-headers-in-body
1619     "p" gnus-article-verify-x-pgp-sig
1620     "d" gnus-article-treat-dumbquotes
1621     "s" gnus-smiley-display)
1622
1623   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1624     "a" gnus-article-hide
1625     "h" gnus-article-toggle-headers
1626     "b" gnus-article-hide-boring-headers
1627     "s" gnus-article-hide-signature
1628     "c" gnus-article-hide-citation
1629     "C" gnus-article-hide-citation-in-followups
1630     "l" gnus-article-hide-list-identifiers
1631     "p" gnus-article-hide-pgp
1632     "B" gnus-article-strip-banner
1633     "P" gnus-article-hide-pem
1634     "\C-c" gnus-article-hide-citation-maybe)
1635
1636   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1637     "a" gnus-article-highlight
1638     "h" gnus-article-highlight-headers
1639     "c" gnus-article-highlight-citation
1640     "s" gnus-article-highlight-signature)
1641
1642   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1643     "z" gnus-article-date-ut
1644     "u" gnus-article-date-ut
1645     "l" gnus-article-date-local
1646     "e" gnus-article-date-lapsed
1647     "o" gnus-article-date-original
1648     "i" gnus-article-date-iso8601
1649     "s" gnus-article-date-user)
1650
1651   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1652     "t" gnus-article-remove-trailing-blank-lines
1653     "l" gnus-article-strip-leading-blank-lines
1654     "m" gnus-article-strip-multiple-blank-lines
1655     "a" gnus-article-strip-blank-lines
1656     "A" gnus-article-strip-all-blank-lines
1657     "s" gnus-article-strip-leading-space
1658     "e" gnus-article-strip-trailing-space)
1659
1660   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1661     "v" gnus-version
1662     "f" gnus-summary-fetch-faq
1663     "d" gnus-summary-describe-group
1664     "h" gnus-summary-describe-briefly
1665     "i" gnus-info-find-node)
1666
1667   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1668     "e" gnus-summary-expire-articles
1669     "\M-\C-e" gnus-summary-expire-articles-now
1670     "\177" gnus-summary-delete-article
1671     [delete] gnus-summary-delete-article
1672     [backspace] gnus-summary-delete-article
1673     "m" gnus-summary-move-article
1674     "r" gnus-summary-respool-article
1675     "w" gnus-summary-edit-article
1676     "c" gnus-summary-copy-article
1677     "B" gnus-summary-crosspost-article
1678     "q" gnus-summary-respool-query
1679     "t" gnus-summary-respool-trace
1680     "i" gnus-summary-import-article
1681     "p" gnus-summary-article-posted-p)
1682
1683   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1684     "o" gnus-summary-save-article
1685     "m" gnus-summary-save-article-mail
1686     "F" gnus-summary-write-article-file
1687     "r" gnus-summary-save-article-rmail
1688     "f" gnus-summary-save-article-file
1689     "b" gnus-summary-save-article-body-file
1690     "h" gnus-summary-save-article-folder
1691     "v" gnus-summary-save-article-vm
1692     "p" gnus-summary-pipe-output
1693     "s" gnus-soup-add-article)
1694
1695   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1696     "b" gnus-summary-display-buttonized
1697     "m" gnus-summary-repair-multipart
1698     "v" gnus-article-view-part
1699     "o" gnus-article-save-part
1700     "c" gnus-article-copy-part
1701     "C" gnus-article-view-part-as-charset
1702     "e" gnus-article-externalize-part
1703     "E" gnus-article-encrypt-body
1704     "i" gnus-article-inline-part
1705     "|" gnus-article-pipe-part))
1706
1707 (defun gnus-summary-make-menu-bar ()
1708   (gnus-turn-off-edit-menu 'summary)
1709
1710   (unless (boundp 'gnus-summary-misc-menu)
1711
1712     (easy-menu-define
1713      gnus-summary-kill-menu gnus-summary-mode-map ""
1714      (cons
1715       "Score"
1716       (nconc
1717        (list
1718         ["Enter score..." gnus-summary-score-entry t]
1719         ["Customize" gnus-score-customize t])
1720        (gnus-make-score-map 'increase)
1721        (gnus-make-score-map 'lower)
1722        '(("Mark"
1723           ["Kill below" gnus-summary-kill-below t]
1724           ["Mark above" gnus-summary-mark-above t]
1725           ["Tick above" gnus-summary-tick-above t]
1726           ["Clear above" gnus-summary-clear-above t])
1727          ["Current score" gnus-summary-current-score t]
1728          ["Set score" gnus-summary-set-score t]
1729          ["Switch current score file..." gnus-score-change-score-file t]
1730          ["Set mark below..." gnus-score-set-mark-below t]
1731          ["Set expunge below..." gnus-score-set-expunge-below t]
1732          ["Edit current score file" gnus-score-edit-current-scores t]
1733          ["Edit score file" gnus-score-edit-file t]
1734          ["Trace score" gnus-score-find-trace t]
1735          ["Find words" gnus-score-find-favourite-words t]
1736          ["Rescore buffer" gnus-summary-rescore t]
1737          ["Increase score..." gnus-summary-increase-score t]
1738          ["Lower score..." gnus-summary-lower-score t]))))
1739
1740     ;; Define both the Article menu in the summary buffer and the equivalent
1741     ;; Commands menu in the article buffer here for consistency.
1742     (let ((innards
1743            `(("Hide"
1744               ["All" gnus-article-hide t]
1745               ["Headers" gnus-article-toggle-headers t]
1746               ["Signature" gnus-article-hide-signature t]
1747               ["Citation" gnus-article-hide-citation t]
1748               ["List identifiers" gnus-article-hide-list-identifiers t]
1749               ["PGP" gnus-article-hide-pgp t]
1750               ["Banner" gnus-article-strip-banner t]
1751               ["Boring headers" gnus-article-hide-boring-headers t])
1752              ("Highlight"
1753               ["All" gnus-article-highlight t]
1754               ["Headers" gnus-article-highlight-headers t]
1755               ["Signature" gnus-article-highlight-signature t]
1756               ["Citation" gnus-article-highlight-citation t])
1757              ("Date"
1758               ["Local" gnus-article-date-local t]
1759               ["ISO8601" gnus-article-date-iso8601 t]
1760               ["UT" gnus-article-date-ut t]
1761               ["Original" gnus-article-date-original t]
1762               ["Lapsed" gnus-article-date-lapsed t]
1763               ["User-defined" gnus-article-date-user t])
1764              ("Washing"
1765               ("Remove Blanks"
1766                ["Leading" gnus-article-strip-leading-blank-lines t]
1767                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1768                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1769                ["All of the above" gnus-article-strip-blank-lines t]
1770                ["All" gnus-article-strip-all-blank-lines t]
1771                ["Leading space" gnus-article-strip-leading-space t]
1772                ["Trailing space" gnus-article-strip-trailing-space t])
1773               ["Overstrike" gnus-article-treat-overstrike t]
1774               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1775               ["Emphasis" gnus-article-emphasize t]
1776               ["Word wrap" gnus-article-fill-cited-article t]
1777               ["Fill long lines" gnus-article-fill-long-lines t]
1778               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1779               ["CR" gnus-article-remove-cr t]
1780               ["Show X-Face" gnus-article-display-x-face t]
1781               ["Rot 13" gnus-summary-caesar-message
1782                ,@(if (featurep 'xemacs) nil
1783                    '(:help "\"Caesar rotate\" article by 13"))]
1784               ["Unix pipe" gnus-summary-pipe-message t]
1785               ["Add buttons" gnus-article-add-buttons t]
1786               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1787               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1788               ["Toggle MIME" gnus-summary-toggle-mime t]
1789               ["Verbose header" gnus-summary-verbose-headers t]
1790               ["Toggle header" gnus-summary-toggle-header t]
1791               ["Toggle smileys" gnus-smiley-display t]
1792               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1793               ["HZ" gnus-article-decode-HZ t])
1794              ("Output"
1795               ["Save in default format" gnus-summary-save-article
1796                ,@(if (featurep 'xemacs) nil
1797                    '(:help "Save article using default method"))]
1798               ["Save in file" gnus-summary-save-article-file
1799                ,@(if (featurep 'xemacs) nil
1800                    '(:help "Save article in file"))]
1801               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1802               ["Save in MH folder" gnus-summary-save-article-folder t]
1803               ["Save in VM folder" gnus-summary-save-article-vm t]
1804               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1805               ["Save body in file" gnus-summary-save-article-body-file t]
1806               ["Pipe through a filter" gnus-summary-pipe-output t]
1807               ["Add to SOUP packet" gnus-soup-add-article t]
1808               ["Print" gnus-summary-print-article t])
1809              ("Backend"
1810               ["Respool article..." gnus-summary-respool-article t]
1811               ["Move article..." gnus-summary-move-article
1812                (gnus-check-backend-function
1813                 'request-move-article gnus-newsgroup-name)]
1814               ["Copy article..." gnus-summary-copy-article t]
1815               ["Crosspost article..." gnus-summary-crosspost-article
1816                (gnus-check-backend-function
1817                 'request-replace-article gnus-newsgroup-name)]
1818               ["Import file..." gnus-summary-import-article t]
1819               ["Check if posted" gnus-summary-article-posted-p t]
1820               ["Edit article" gnus-summary-edit-article
1821                (not (gnus-group-read-only-p))]
1822               ["Delete article" gnus-summary-delete-article
1823                (gnus-check-backend-function
1824                 'request-expire-articles gnus-newsgroup-name)]
1825               ["Query respool" gnus-summary-respool-query t]
1826               ["Trace respool" gnus-summary-respool-trace t]
1827               ["Delete expirable articles" gnus-summary-expire-articles-now
1828                (gnus-check-backend-function
1829                 'request-expire-articles gnus-newsgroup-name)])
1830              ("Extract"
1831               ["Uudecode" gnus-uu-decode-uu
1832                ,@(if (featurep 'xemacs) nil
1833                    '(:help "Decode uuencoded article(s)"))]
1834               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1835               ["Unshar" gnus-uu-decode-unshar t]
1836               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1837               ["Save" gnus-uu-decode-save t]
1838               ["Binhex" gnus-uu-decode-binhex t]
1839               ["Postscript" gnus-uu-decode-postscript t])
1840              ("Cache"
1841               ["Enter article" gnus-cache-enter-article t]
1842               ["Remove article" gnus-cache-remove-article t])
1843              ["Translate" gnus-article-babel t]
1844              ["Select article buffer" gnus-summary-select-article-buffer t]
1845              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1846              ["Isearch article..." gnus-summary-isearch-article t]
1847              ["Beginning of the article" gnus-summary-beginning-of-article t]
1848              ["End of the article" gnus-summary-end-of-article t]
1849              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1850              ["Fetch referenced articles" gnus-summary-refer-references t]
1851              ["Fetch current thread" gnus-summary-refer-thread t]
1852              ["Fetch article with id..." gnus-summary-refer-article t]
1853              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1854              ["Redisplay" gnus-summary-show-article t])))
1855       (easy-menu-define
1856        gnus-summary-article-menu gnus-summary-mode-map ""
1857        (cons "Article" innards))
1858
1859       (easy-menu-define
1860        gnus-article-commands-menu gnus-article-mode-map ""
1861        (cons "Commands" innards)))
1862
1863     (easy-menu-define
1864      gnus-summary-thread-menu gnus-summary-mode-map ""
1865      '("Threads"
1866        ["Toggle threading" gnus-summary-toggle-threads t]
1867        ["Hide threads" gnus-summary-hide-all-threads t]
1868        ["Show threads" gnus-summary-show-all-threads t]
1869        ["Hide thread" gnus-summary-hide-thread t]
1870        ["Show thread" gnus-summary-show-thread t]
1871        ["Go to next thread" gnus-summary-next-thread t]
1872        ["Go to previous thread" gnus-summary-prev-thread t]
1873        ["Go down thread" gnus-summary-down-thread t]
1874        ["Go up thread" gnus-summary-up-thread t]
1875        ["Top of thread" gnus-summary-top-thread t]
1876        ["Mark thread as read" gnus-summary-kill-thread t]
1877        ["Lower thread score" gnus-summary-lower-thread t]
1878        ["Raise thread score" gnus-summary-raise-thread t]
1879        ["Rethread current" gnus-summary-rethread-current t]))
1880
1881     (easy-menu-define
1882      gnus-summary-post-menu gnus-summary-mode-map ""
1883      `("Post"
1884        ["Post an article" gnus-summary-post-news
1885         ,@(if (featurep 'xemacs) nil
1886             '(:help "Post an article"))]
1887        ["Followup" gnus-summary-followup
1888         ,@(if (featurep 'xemacs) nil
1889             '(:help "Post followup to this article"))]
1890        ["Followup and yank" gnus-summary-followup-with-original
1891         ,@(if (featurep 'xemacs) nil
1892             '(:help "Post followup to this article, quoting its contents"))]
1893        ["Supersede article" gnus-summary-supersede-article t]
1894        ["Cancel article" gnus-summary-cancel-article
1895         ,@(if (featurep 'xemacs) nil
1896             '(:help "Cancel an article you posted"))]
1897        ["Reply" gnus-summary-reply t]
1898        ["Reply and yank" gnus-summary-reply-with-original t]
1899        ["Wide reply" gnus-summary-wide-reply t]
1900        ["Wide reply and yank" gnus-summary-wide-reply-with-original
1901         ,@(if (featurep 'xemacs) nil
1902             '(:help "Mail a reply, quoting this article"))]
1903        ["Mail forward" gnus-summary-mail-forward t]
1904        ["Post forward" gnus-summary-post-forward t]
1905        ["Digest and mail" gnus-uu-digest-mail-forward t]
1906        ["Digest and post" gnus-uu-digest-post-forward t]
1907        ["Resend message" gnus-summary-resend-message t]
1908        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1909        ["Send a mail" gnus-summary-mail-other-window t]
1910        ["Uuencode and post" gnus-uu-post-news
1911         ,@(if (featurep 'xemacs) nil
1912             '(:help "Post a uuencoded article"))]
1913        ["Followup via news" gnus-summary-followup-to-mail t]
1914        ["Followup via news and yank"
1915         gnus-summary-followup-to-mail-with-original t]
1916        ;;("Draft"
1917        ;;["Send" gnus-summary-send-draft t]
1918        ;;["Send bounced" gnus-resend-bounced-mail t])
1919        ))
1920
1921     (easy-menu-define
1922      gnus-summary-misc-menu gnus-summary-mode-map ""
1923      `("Misc"
1924        ("Mark Read"
1925         ["Mark as read" gnus-summary-mark-as-read-forward t]
1926         ["Mark same subject and select"
1927          gnus-summary-kill-same-subject-and-select t]
1928         ["Mark same subject" gnus-summary-kill-same-subject t]
1929         ["Catchup" gnus-summary-catchup
1930          ,@(if (featurep 'xemacs) nil
1931              '(:help "Mark unread articles in this group as read"))]
1932         ["Catchup all" gnus-summary-catchup-all t]
1933         ["Catchup to here" gnus-summary-catchup-to-here t]
1934         ["Catchup region" gnus-summary-mark-region-as-read t]
1935         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1936        ("Mark Various"
1937         ["Tick" gnus-summary-tick-article-forward t]
1938         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1939         ["Remove marks" gnus-summary-clear-mark-forward t]
1940         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1941         ["Set bookmark" gnus-summary-set-bookmark t]
1942         ["Remove bookmark" gnus-summary-remove-bookmark t])
1943        ("Mark Limit"
1944         ["Marks..." gnus-summary-limit-to-marks t]
1945         ["Subject..." gnus-summary-limit-to-subject t]
1946         ["Author..." gnus-summary-limit-to-author t]
1947         ["Age..." gnus-summary-limit-to-age t]
1948         ["Extra..." gnus-summary-limit-to-extra t]
1949         ["Score" gnus-summary-limit-to-score t]
1950         ["Unread" gnus-summary-limit-to-unread t]
1951         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1952         ["Articles" gnus-summary-limit-to-articles t]
1953         ["Pop limit" gnus-summary-pop-limit t]
1954         ["Show dormant" gnus-summary-limit-include-dormant t]
1955         ["Hide childless dormant"
1956          gnus-summary-limit-exclude-childless-dormant t]
1957         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1958         ["Hide marked" gnus-summary-limit-exclude-marks t]
1959         ["Show expunged" gnus-summary-show-all-expunged t])
1960        ("Process Mark"
1961         ["Set mark" gnus-summary-mark-as-processable t]
1962         ["Remove mark" gnus-summary-unmark-as-processable t]
1963         ["Remove all marks" gnus-summary-unmark-all-processable t]
1964         ["Mark above" gnus-uu-mark-over t]
1965         ["Mark series" gnus-uu-mark-series t]
1966         ["Mark region" gnus-uu-mark-region t]
1967         ["Unmark region" gnus-uu-unmark-region t]
1968         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1969         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1970         ["Mark all" gnus-uu-mark-all t]
1971         ["Mark buffer" gnus-uu-mark-buffer t]
1972         ["Mark sparse" gnus-uu-mark-sparse t]
1973         ["Mark thread" gnus-uu-mark-thread t]
1974         ["Unmark thread" gnus-uu-unmark-thread t]
1975         ("Process Mark Sets"
1976          ["Kill" gnus-summary-kill-process-mark t]
1977          ["Yank" gnus-summary-yank-process-mark
1978           gnus-newsgroup-process-stack]
1979          ["Save" gnus-summary-save-process-mark t]))
1980        ("Scroll article"
1981         ["Page forward" gnus-summary-next-page
1982          ,@(if (featurep 'xemacs) nil
1983              '(:help "Show next page of article"))]
1984         ["Page backward" gnus-summary-prev-page
1985          ,@(if (featurep 'xemacs) nil
1986              '(:help "Show previous page of article"))]
1987         ["Line forward" gnus-summary-scroll-up t])
1988        ("Move"
1989         ["Next unread article" gnus-summary-next-unread-article t]
1990         ["Previous unread article" gnus-summary-prev-unread-article t]
1991         ["Next article" gnus-summary-next-article t]
1992         ["Previous article" gnus-summary-prev-article t]
1993         ["Next unread subject" gnus-summary-next-unread-subject t]
1994         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1995         ["Next article same subject" gnus-summary-next-same-subject t]
1996         ["Previous article same subject" gnus-summary-prev-same-subject t]
1997         ["First unread article" gnus-summary-first-unread-article t]
1998         ["Best unread article" gnus-summary-best-unread-article t]
1999         ["Go to subject number..." gnus-summary-goto-subject t]
2000         ["Go to article number..." gnus-summary-goto-article t]
2001         ["Go to the last article" gnus-summary-goto-last-article t]
2002         ["Pop article off history" gnus-summary-pop-article t])
2003        ("Sort"
2004         ["Sort by number" gnus-summary-sort-by-number t]
2005         ["Sort by author" gnus-summary-sort-by-author t]
2006         ["Sort by subject" gnus-summary-sort-by-subject t]
2007         ["Sort by date" gnus-summary-sort-by-date t]
2008         ["Sort by score" gnus-summary-sort-by-score t]
2009         ["Sort by lines" gnus-summary-sort-by-lines t]
2010         ["Sort by characters" gnus-summary-sort-by-chars t])
2011        ("Help"
2012         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2013         ["Describe group" gnus-summary-describe-group t]
2014         ["Read manual" gnus-info-find-node t])
2015        ("Modes"
2016         ["Pick and read" gnus-pick-mode t]
2017         ["Binary" gnus-binary-mode t])
2018        ("Regeneration"
2019         ["Regenerate" gnus-summary-prepare t]
2020         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2021         ["Toggle threading" gnus-summary-toggle-threads t])
2022        ["Filter articles..." gnus-summary-execute-command t]
2023        ["Run command on subjects..." gnus-summary-universal-argument t]
2024        ["Search articles forward..." gnus-summary-search-article-forward t]
2025        ["Search articles backward..." gnus-summary-search-article-backward t]
2026        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2027        ["Expand window" gnus-summary-expand-window t]
2028        ["Expire expirable articles" gnus-summary-expire-articles
2029         (gnus-check-backend-function
2030          'request-expire-articles gnus-newsgroup-name)]
2031        ["Edit local kill file" gnus-summary-edit-local-kill t]
2032        ["Edit main kill file" gnus-summary-edit-global-kill t]
2033        ["Edit group parameters" gnus-summary-edit-parameters t]
2034        ["Customize group parameters" gnus-summary-customize-parameters t]
2035        ["Send a bug report" gnus-bug t]
2036        ("Exit"
2037         ["Catchup and exit" gnus-summary-catchup-and-exit
2038          ,@(if (featurep 'xemacs) nil
2039              '(:help "Mark unread articles in this group as read, then exit"))]
2040         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2041         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2042         ["Exit group" gnus-summary-exit
2043          ,@(if (featurep 'xemacs) nil
2044              '(:help "Exit current group, return to group selection mode"))]
2045         ["Exit group without updating" gnus-summary-exit-no-update t]
2046         ["Exit and goto next group" gnus-summary-next-group t]
2047         ["Exit and goto prev group" gnus-summary-prev-group t]
2048         ["Reselect group" gnus-summary-reselect-current-group t]
2049         ["Rescan group" gnus-summary-rescan-group t]
2050         ["Update dribble" gnus-summary-save-newsrc t])))
2051
2052     (gnus-run-hooks 'gnus-summary-menu-hook)))
2053
2054 (defvar gnus-summary-tool-bar-map nil)
2055
2056 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2057 (defun gnus-summary-make-tool-bar ()
2058   (if (and (fboundp 'tool-bar-add-item-from-menu)
2059            (default-value 'tool-bar-mode)
2060            (not gnus-summary-tool-bar-map))
2061       (setq gnus-summary-tool-bar-map
2062             (let ((tool-bar-map (make-sparse-keymap)))
2063               (tool-bar-add-item-from-menu
2064                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2065               (tool-bar-add-item-from-menu
2066                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2067               (tool-bar-add-item-from-menu
2068                'gnus-summary-post-news "post" gnus-summary-mode-map)
2069               (tool-bar-add-item-from-menu
2070                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2071               (tool-bar-add-item-from-menu
2072                'gnus-summary-followup "followup" gnus-summary-mode-map)
2073               (tool-bar-add-item-from-menu
2074                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2075               (tool-bar-add-item-from-menu
2076                'gnus-summary-reply "reply" gnus-summary-mode-map)
2077               (tool-bar-add-item-from-menu
2078                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2079               (tool-bar-add-item-from-menu
2080                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2081               (tool-bar-add-item-from-menu
2082                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2083               (tool-bar-add-item-from-menu
2084                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2085               (tool-bar-add-item-from-menu
2086                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2087               (tool-bar-add-item-from-menu
2088                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2089               (tool-bar-add-item-from-menu
2090                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2091               (tool-bar-add-item-from-menu
2092                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2093               tool-bar-map)))
2094   (if gnus-summary-tool-bar-map
2095       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2096
2097 (defun gnus-score-set-default (var value)
2098   "A version of set that updates the GNU Emacs menu-bar."
2099   (set var value)
2100   ;; It is the message that forces the active status to be updated.
2101   (message ""))
2102
2103 (defun gnus-make-score-map (type)
2104   "Make a summary score map of type TYPE."
2105   (if t
2106       nil
2107     (let ((headers '(("author" "from" string)
2108                      ("subject" "subject" string)
2109                      ("article body" "body" string)
2110                      ("article head" "head" string)
2111                      ("xref" "xref" string)
2112                      ("extra header" "extra" string)
2113                      ("lines" "lines" number)
2114                      ("followups to author" "followup" string)))
2115           (types '((number ("less than" <)
2116                            ("greater than" >)
2117                            ("equal" =))
2118                    (string ("substring" s)
2119                            ("exact string" e)
2120                            ("fuzzy string" f)
2121                            ("regexp" r))))
2122           (perms '(("temporary" (current-time-string))
2123                    ("permanent" nil)
2124                    ("immediate" now)))
2125           header)
2126       (list
2127        (apply
2128         'nconc
2129         (list
2130          (if (eq type 'lower)
2131              "Lower score"
2132            "Increase score"))
2133         (let (outh)
2134           (while headers
2135             (setq header (car headers))
2136             (setq outh
2137                   (cons
2138                    (apply
2139                     'nconc
2140                     (list (car header))
2141                     (let ((ts (cdr (assoc (nth 2 header) types)))
2142                           outt)
2143                       (while ts
2144                         (setq outt
2145                               (cons
2146                                (apply
2147                                 'nconc
2148                                 (list (caar ts))
2149                                 (let ((ps perms)
2150                                       outp)
2151                                   (while ps
2152                                     (setq outp
2153                                           (cons
2154                                            (vector
2155                                             (caar ps)
2156                                             (list
2157                                              'gnus-summary-score-entry
2158                                              (nth 1 header)
2159                                              (if (or (string= (nth 1 header)
2160                                                               "head")
2161                                                      (string= (nth 1 header)
2162                                                               "body"))
2163                                                  ""
2164                                                (list 'gnus-summary-header
2165                                                      (nth 1 header)))
2166                                              (list 'quote (nth 1 (car ts)))
2167                                              (list 'gnus-score-delta-default
2168                                                    nil)
2169                                              (nth 1 (car ps))
2170                                              t)
2171                                             t)
2172                                            outp))
2173                                     (setq ps (cdr ps)))
2174                                   (list (nreverse outp))))
2175                                outt))
2176                         (setq ts (cdr ts)))
2177                       (list (nreverse outt))))
2178                    outh))
2179             (setq headers (cdr headers)))
2180           (list (nreverse outh))))))))
2181
2182 \f
2183
2184 (defun gnus-summary-mode (&optional group)
2185   "Major mode for reading articles.
2186
2187 All normal editing commands are switched off.
2188 \\<gnus-summary-mode-map>
2189 Each line in this buffer represents one article.  To read an
2190 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2191 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2192 respectively.
2193
2194 You can also post articles and send mail from this buffer.  To
2195 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2196 of an article, type `\\[gnus-summary-reply]'.
2197
2198 There are approx. one gazillion commands you can execute in this
2199 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2200
2201 The following commands are available:
2202
2203 \\{gnus-summary-mode-map}"
2204   (interactive)
2205   (kill-all-local-variables)
2206   (when (gnus-visual-p 'summary-menu 'menu)
2207     (gnus-summary-make-menu-bar)
2208     (gnus-summary-make-tool-bar))
2209   (gnus-summary-make-local-variables)
2210   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2211     (gnus-summary-make-local-variables))
2212   (gnus-make-thread-indent-array)
2213   (gnus-simplify-mode-line)
2214   (setq major-mode 'gnus-summary-mode)
2215   (setq mode-name "Summary")
2216   (make-local-variable 'minor-mode-alist)
2217   (use-local-map gnus-summary-mode-map)
2218   (buffer-disable-undo)
2219   (setq buffer-read-only t)             ;Disable modification
2220   (setq truncate-lines t)
2221   (setq selective-display t)
2222   (setq selective-display-ellipses t)   ;Display `...'
2223   (gnus-summary-set-display-table)
2224   (gnus-set-default-directory)
2225   (setq gnus-newsgroup-name group)
2226   (unless (gnus-news-group-p group)
2227     (setq gnus-newsgroup-incorporated
2228           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2229   (make-local-variable 'gnus-summary-line-format)
2230   (make-local-variable 'gnus-summary-line-format-spec)
2231   (make-local-variable 'gnus-summary-dummy-line-format)
2232   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2233   (make-local-variable 'gnus-summary-mark-positions)
2234   (make-local-hook 'pre-command-hook)
2235   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2236   (gnus-run-hooks 'gnus-summary-mode-hook)
2237   (turn-on-gnus-mailing-list-mode)
2238   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2239   (gnus-update-summary-mark-positions))
2240
2241 (defun gnus-summary-make-local-variables ()
2242   "Make all the local summary buffer variables."
2243   (let (global)
2244     (dolist (local gnus-summary-local-variables)
2245       (if (consp local)
2246           (progn
2247             (if (eq (cdr local) 'global)
2248                 ;; Copy the global value of the variable.
2249                 (setq global (symbol-value (car local)))
2250               ;; Use the value from the list.
2251               (setq global (eval (cdr local))))
2252             (set (make-local-variable (car local)) global))
2253         ;; Simple nil-valued local variable.
2254         (set (make-local-variable local) nil)))))
2255
2256 (defun gnus-summary-clear-local-variables ()
2257   (let ((locals gnus-summary-local-variables))
2258     (while locals
2259       (if (consp (car locals))
2260           (and (vectorp (caar locals))
2261                (set (caar locals) nil))
2262         (and (vectorp (car locals))
2263              (set (car locals) nil)))
2264       (setq locals (cdr locals)))))
2265
2266 ;; Summary data functions.
2267
2268 (defmacro gnus-data-number (data)
2269   `(car ,data))
2270
2271 (defmacro gnus-data-set-number (data number)
2272   `(setcar ,data ,number))
2273
2274 (defmacro gnus-data-mark (data)
2275   `(nth 1 ,data))
2276
2277 (defmacro gnus-data-set-mark (data mark)
2278   `(setcar (nthcdr 1 ,data) ,mark))
2279
2280 (defmacro gnus-data-pos (data)
2281   `(nth 2 ,data))
2282
2283 (defmacro gnus-data-set-pos (data pos)
2284   `(setcar (nthcdr 2 ,data) ,pos))
2285
2286 (defmacro gnus-data-header (data)
2287   `(nth 3 ,data))
2288
2289 (defmacro gnus-data-set-header (data header)
2290   `(setcar (nthcdr 3 ,data) ,header))
2291
2292 (defmacro gnus-data-level (data)
2293   `(nth 4 ,data))
2294
2295 (defmacro gnus-data-unread-p (data)
2296   `(= (nth 1 ,data) gnus-unread-mark))
2297
2298 (defmacro gnus-data-read-p (data)
2299   `(/= (nth 1 ,data) gnus-unread-mark))
2300
2301 (defmacro gnus-data-pseudo-p (data)
2302   `(consp (nth 3 ,data)))
2303
2304 (defmacro gnus-data-find (number)
2305   `(assq ,number gnus-newsgroup-data))
2306
2307 (defmacro gnus-data-find-list (number &optional data)
2308   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2309      (memq (assq ,number bdata)
2310            bdata)))
2311
2312 (defmacro gnus-data-make (number mark pos header level)
2313   `(list ,number ,mark ,pos ,header ,level))
2314
2315 (defun gnus-data-enter (after-article number mark pos header level offset)
2316   (let ((data (gnus-data-find-list after-article)))
2317     (unless data
2318       (error "No such article: %d" after-article))
2319     (setcdr data (cons (gnus-data-make number mark pos header level)
2320                        (cdr data)))
2321     (setq gnus-newsgroup-data-reverse nil)
2322     (gnus-data-update-list (cddr data) offset)))
2323
2324 (defun gnus-data-enter-list (after-article list &optional offset)
2325   (when list
2326     (let ((data (and after-article (gnus-data-find-list after-article)))
2327           (ilist list))
2328       (if (not (or data
2329                    after-article))
2330           (let ((odata gnus-newsgroup-data))
2331             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2332             (when offset
2333               (gnus-data-update-list odata offset)))
2334         ;; Find the last element in the list to be spliced into the main
2335         ;; list.
2336         (while (cdr list)
2337           (setq list (cdr list)))
2338         (if (not data)
2339             (progn
2340               (setcdr list gnus-newsgroup-data)
2341               (setq gnus-newsgroup-data ilist)
2342               (when offset
2343                 (gnus-data-update-list (cdr list) offset)))
2344           (setcdr list (cdr data))
2345           (setcdr data ilist)
2346           (when offset
2347             (gnus-data-update-list (cdr list) offset))))
2348       (setq gnus-newsgroup-data-reverse nil))))
2349
2350 (defun gnus-data-remove (article &optional offset)
2351   (let ((data gnus-newsgroup-data))
2352     (if (= (gnus-data-number (car data)) article)
2353         (progn
2354           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2355                 gnus-newsgroup-data-reverse nil)
2356           (when offset
2357             (gnus-data-update-list gnus-newsgroup-data offset)))
2358       (while (cdr data)
2359         (when (= (gnus-data-number (cadr data)) article)
2360           (setcdr data (cddr data))
2361           (when offset
2362             (gnus-data-update-list (cdr data) offset))
2363           (setq data nil
2364                 gnus-newsgroup-data-reverse nil))
2365         (setq data (cdr data))))))
2366
2367 (defmacro gnus-data-list (backward)
2368   `(if ,backward
2369        (or gnus-newsgroup-data-reverse
2370            (setq gnus-newsgroup-data-reverse
2371                  (reverse gnus-newsgroup-data)))
2372      gnus-newsgroup-data))
2373
2374 (defun gnus-data-update-list (data offset)
2375   "Add OFFSET to the POS of all data entries in DATA."
2376   (setq gnus-newsgroup-data-reverse nil)
2377   (while data
2378     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2379     (setq data (cdr data))))
2380
2381 (defun gnus-summary-article-pseudo-p (article)
2382   "Say whether this article is a pseudo article or not."
2383   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2384
2385 (defmacro gnus-summary-article-sparse-p (article)
2386   "Say whether this article is a sparse article or not."
2387   `(memq ,article gnus-newsgroup-sparse))
2388
2389 (defmacro gnus-summary-article-ancient-p (article)
2390   "Say whether this article is a sparse article or not."
2391   `(memq ,article gnus-newsgroup-ancient))
2392
2393 (defun gnus-article-parent-p (number)
2394   "Say whether this article is a parent or not."
2395   (let ((data (gnus-data-find-list number)))
2396     (and (cdr data)                     ; There has to be an article after...
2397          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2398             (gnus-data-level (nth 1 data))))))
2399
2400 (defun gnus-article-children (number)
2401   "Return a list of all children to NUMBER."
2402   (let* ((data (gnus-data-find-list number))
2403          (level (gnus-data-level (car data)))
2404          children)
2405     (setq data (cdr data))
2406     (while (and data
2407                 (= (gnus-data-level (car data)) (1+ level)))
2408       (push (gnus-data-number (car data)) children)
2409       (setq data (cdr data)))
2410     children))
2411
2412 (defmacro gnus-summary-skip-intangible ()
2413   "If the current article is intangible, then jump to a different article."
2414   '(let ((to (get-text-property (point) 'gnus-intangible)))
2415      (and to (gnus-summary-goto-subject to))))
2416
2417 (defmacro gnus-summary-article-intangible-p ()
2418   "Say whether this article is intangible or not."
2419   '(get-text-property (point) 'gnus-intangible))
2420
2421 (defun gnus-article-read-p (article)
2422   "Say whether ARTICLE is read or not."
2423   (not (or (memq article gnus-newsgroup-marked)
2424            (memq article gnus-newsgroup-unreads)
2425            (memq article gnus-newsgroup-unselected)
2426            (memq article gnus-newsgroup-dormant))))
2427
2428 ;; Some summary mode macros.
2429
2430 (defmacro gnus-summary-article-number ()
2431   "The article number of the article on the current line.
2432 If there isn's an article number here, then we return the current
2433 article number."
2434   '(progn
2435      (gnus-summary-skip-intangible)
2436      (or (get-text-property (point) 'gnus-number)
2437          (gnus-summary-last-subject))))
2438
2439 (defmacro gnus-summary-article-header (&optional number)
2440   "Return the header of article NUMBER."
2441   `(gnus-data-header (gnus-data-find
2442                       ,(or number '(gnus-summary-article-number)))))
2443
2444 (defmacro gnus-summary-thread-level (&optional number)
2445   "Return the level of thread that starts with article NUMBER."
2446   `(if (and (eq gnus-summary-make-false-root 'dummy)
2447             (get-text-property (point) 'gnus-intangible))
2448        0
2449      (gnus-data-level (gnus-data-find
2450                        ,(or number '(gnus-summary-article-number))))))
2451
2452 (defmacro gnus-summary-article-mark (&optional number)
2453   "Return the mark of article NUMBER."
2454   `(gnus-data-mark (gnus-data-find
2455                     ,(or number '(gnus-summary-article-number)))))
2456
2457 (defmacro gnus-summary-article-pos (&optional number)
2458   "Return the position of the line of article NUMBER."
2459   `(gnus-data-pos (gnus-data-find
2460                    ,(or number '(gnus-summary-article-number)))))
2461
2462 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2463 (defmacro gnus-summary-article-subject (&optional number)
2464   "Return current subject string or nil if nothing."
2465   `(let ((headers
2466           ,(if number
2467                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2468              '(gnus-data-header (assq (gnus-summary-article-number)
2469                                       gnus-newsgroup-data)))))
2470      (and headers
2471           (vectorp headers)
2472           (mail-header-subject headers))))
2473
2474 (defmacro gnus-summary-article-score (&optional number)
2475   "Return current article score."
2476   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2477                   gnus-newsgroup-scored))
2478        gnus-summary-default-score 0))
2479
2480 (defun gnus-summary-article-children (&optional number)
2481   "Return a list of article numbers that are children of article NUMBER."
2482   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2483          (level (gnus-data-level (car data)))
2484          l children)
2485     (while (and (setq data (cdr data))
2486                 (> (setq l (gnus-data-level (car data))) level))
2487       (and (= (1+ level) l)
2488            (push (gnus-data-number (car data))
2489                  children)))
2490     (nreverse children)))
2491
2492 (defun gnus-summary-article-parent (&optional number)
2493   "Return the article number of the parent of article NUMBER."
2494   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2495                                     (gnus-data-list t)))
2496          (level (gnus-data-level (car data))))
2497     (if (zerop level)
2498         ()                              ; This is a root.
2499       ;; We search until we find an article with a level less than
2500       ;; this one.  That function has to be the parent.
2501       (while (and (setq data (cdr data))
2502                   (not (< (gnus-data-level (car data)) level))))
2503       (and data (gnus-data-number (car data))))))
2504
2505 (defun gnus-unread-mark-p (mark)
2506   "Say whether MARK is the unread mark."
2507   (= mark gnus-unread-mark))
2508
2509 (defun gnus-read-mark-p (mark)
2510   "Say whether MARK is one of the marks that mark as read.
2511 This is all marks except unread, ticked, dormant, and expirable."
2512   (not (or (= mark gnus-unread-mark)
2513            (= mark gnus-ticked-mark)
2514            (= mark gnus-dormant-mark)
2515            (= mark gnus-expirable-mark))))
2516
2517 (defmacro gnus-article-mark (number)
2518   "Return the MARK of article NUMBER.
2519 This macro should only be used when computing the mark the \"first\"
2520 time; i.e., when generating the summary lines.  After that,
2521 `gnus-summary-article-mark' should be used to examine the
2522 marks of articles."
2523   `(cond
2524     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2525     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2526     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2527     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2528     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2529     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2530     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2531     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2532            gnus-ancient-mark))))
2533
2534 ;; Saving hidden threads.
2535
2536 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2537 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2538
2539 (defmacro gnus-save-hidden-threads (&rest forms)
2540   "Save hidden threads, eval FORMS, and restore the hidden threads."
2541   (let ((config (make-symbol "config")))
2542     `(let ((,config (gnus-hidden-threads-configuration)))
2543        (unwind-protect
2544            (save-excursion
2545              ,@forms)
2546          (gnus-restore-hidden-threads-configuration ,config)))))
2547
2548 (defun gnus-data-compute-positions ()
2549   "Compute the positions of all articles."
2550   (setq gnus-newsgroup-data-reverse nil)
2551   (let ((data gnus-newsgroup-data))
2552     (save-excursion
2553       (gnus-save-hidden-threads
2554         (gnus-summary-show-all-threads)
2555         (goto-char (point-min))
2556         (while data
2557           (while (get-text-property (point) 'gnus-intangible)
2558             (forward-line 1))
2559           (gnus-data-set-pos (car data) (+ (point) 3))
2560           (setq data (cdr data))
2561           (forward-line 1))))))
2562
2563 (defun gnus-hidden-threads-configuration ()
2564   "Return the current hidden threads configuration."
2565   (save-excursion
2566     (let (config)
2567       (goto-char (point-min))
2568       (while (search-forward "\r" nil t)
2569         (push (1- (point)) config))
2570       config)))
2571
2572 (defun gnus-restore-hidden-threads-configuration (config)
2573   "Restore hidden threads configuration from CONFIG."
2574   (save-excursion
2575     (let (point buffer-read-only)
2576       (while (setq point (pop config))
2577         (when (and (< point (point-max))
2578                    (goto-char point)
2579                    (eq (char-after) ?\n))
2580           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2581
2582 ;; Various summary mode internalish functions.
2583
2584 (defun gnus-mouse-pick-article (e)
2585   (interactive "e")
2586   (mouse-set-point e)
2587   (gnus-summary-next-page nil t))
2588
2589 (defun gnus-summary-set-display-table ()
2590   "Change the display table.
2591 Odd characters have a tendency to mess
2592 up nicely formatted displays - we make all possible glyphs
2593 display only a single character."
2594
2595   ;; We start from the standard display table, if any.
2596   (let ((table (or (copy-sequence standard-display-table)
2597                    (make-display-table)))
2598         (i 32))
2599     ;; Nix out all the control chars...
2600     (while (>= (setq i (1- i)) 0)
2601       (aset table i [??]))
2602     ;; ... but not newline and cr, of course.  (cr is necessary for the
2603     ;; selective display).
2604     (aset table ?\n nil)
2605     (aset table ?\r nil)
2606     ;; We keep TAB as well.
2607     (aset table ?\t nil)
2608     ;; We nix out any glyphs over 126 that are not set already.
2609     (let ((i 256))
2610       (while (>= (setq i (1- i)) 127)
2611         ;; Only modify if the entry is nil.
2612         (unless (aref table i)
2613           (aset table i [??]))))
2614     (setq buffer-display-table table)))
2615
2616 (defun gnus-summary-setup-buffer (group)
2617   "Initialize summary buffer."
2618   (let ((buffer (concat "*Summary " group "*")))
2619     (if (get-buffer buffer)
2620         (progn
2621           (set-buffer buffer)
2622           (setq gnus-summary-buffer (current-buffer))
2623           (not gnus-newsgroup-prepared))
2624       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2625       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2626       (gnus-summary-mode group)
2627       (when gnus-carpal
2628         (gnus-carpal-setup-buffer 'summary))
2629       (unless gnus-single-article-buffer
2630         (make-local-variable 'gnus-article-buffer)
2631         (make-local-variable 'gnus-article-current)
2632         (make-local-variable 'gnus-original-article-buffer))
2633       (setq gnus-newsgroup-name group)
2634       t)))
2635
2636 (defun gnus-set-global-variables ()
2637   "Set the global equivalents of the buffer-local variables.
2638 They are set to the latest values they had.  These reflect the summary
2639 buffer that was in action when the last article was fetched."
2640   (when (eq major-mode 'gnus-summary-mode)
2641     (setq gnus-summary-buffer (current-buffer))
2642     (let ((name gnus-newsgroup-name)
2643           (marked gnus-newsgroup-marked)
2644           (unread gnus-newsgroup-unreads)
2645           (headers gnus-current-headers)
2646           (data gnus-newsgroup-data)
2647           (summary gnus-summary-buffer)
2648           (article-buffer gnus-article-buffer)
2649           (original gnus-original-article-buffer)
2650           (gac gnus-article-current)
2651           (reffed gnus-reffed-article-number)
2652           (score-file gnus-current-score-file)
2653           (default-charset gnus-newsgroup-charset)
2654           vlist)
2655       (let ((locals gnus-newsgroup-variables))
2656         (while locals
2657           (if (consp (car locals))
2658               (push (eval (caar locals)) vlist)
2659             (push (eval (car locals)) vlist))
2660           (setq locals (cdr locals)))
2661         (setq vlist (nreverse vlist)))
2662       (save-excursion
2663         (set-buffer gnus-group-buffer)
2664         (setq gnus-newsgroup-name name
2665               gnus-newsgroup-marked marked
2666               gnus-newsgroup-unreads unread
2667               gnus-current-headers headers
2668               gnus-newsgroup-data data
2669               gnus-article-current gac
2670               gnus-summary-buffer summary
2671               gnus-article-buffer article-buffer
2672               gnus-original-article-buffer original
2673               gnus-reffed-article-number reffed
2674               gnus-current-score-file score-file
2675               gnus-newsgroup-charset default-charset)
2676         (let ((locals gnus-newsgroup-variables))
2677           (while locals
2678             (if (consp (car locals))
2679                 (set (caar locals) (pop vlist))
2680               (set (car locals) (pop vlist)))
2681             (setq locals (cdr locals))))
2682         ;; The article buffer also has local variables.
2683         (when (gnus-buffer-live-p gnus-article-buffer)
2684           (set-buffer gnus-article-buffer)
2685           (setq gnus-summary-buffer summary))))))
2686
2687 (defun gnus-summary-article-unread-p (article)
2688   "Say whether ARTICLE is unread or not."
2689   (memq article gnus-newsgroup-unreads))
2690
2691 (defun gnus-summary-first-article-p (&optional article)
2692   "Return whether ARTICLE is the first article in the buffer."
2693   (if (not (setq article (or article (gnus-summary-article-number))))
2694       nil
2695     (eq article (caar gnus-newsgroup-data))))
2696
2697 (defun gnus-summary-last-article-p (&optional article)
2698   "Return whether ARTICLE is the last article in the buffer."
2699   (if (not (setq article (or article (gnus-summary-article-number))))
2700       ;; All non-existent numbers are the last article.  :-)
2701       t
2702     (not (cdr (gnus-data-find-list article)))))
2703
2704 (defun gnus-make-thread-indent-array ()
2705   (let ((n 200))
2706     (unless (and gnus-thread-indent-array
2707                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2708       (setq gnus-thread-indent-array (make-vector 201 "")
2709             gnus-thread-indent-array-level gnus-thread-indent-level)
2710       (while (>= n 0)
2711         (aset gnus-thread-indent-array n
2712               (make-string (* n gnus-thread-indent-level) ? ))
2713         (setq n (1- n))))))
2714
2715 (defun gnus-update-summary-mark-positions ()
2716   "Compute where the summary marks are to go."
2717   (save-excursion
2718     (when (gnus-buffer-exists-p gnus-summary-buffer)
2719       (set-buffer gnus-summary-buffer))
2720     (let ((gnus-replied-mark 129)
2721           (gnus-score-below-mark 130)
2722           (gnus-score-over-mark 130)
2723           (gnus-download-mark 131)
2724           (spec gnus-summary-line-format-spec)
2725           gnus-visual pos)
2726       (save-excursion
2727         (gnus-set-work-buffer)
2728         (let ((gnus-summary-line-format-spec spec)
2729               (gnus-newsgroup-downloadable '((0 . t))))
2730           (gnus-summary-insert-line
2731            (make-full-mail-header 0 "" "nobody" "" "" "" 0 0 "" nil)
2732            0 nil 128 t nil "" nil 1)
2733           (goto-char (point-min))
2734           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2735                                              (- (point) 2)))))
2736           (goto-char (point-min))
2737           (push (cons 'replied (and (search-forward "\201" nil t)
2738                                     (- (point) 2)))
2739                 pos)
2740           (goto-char (point-min))
2741           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2742                 pos)
2743           (goto-char (point-min))
2744           (push (cons 'download
2745                       (and (search-forward "\203" nil t) (- (point) 2)))
2746                 pos)))
2747       (setq gnus-summary-mark-positions pos))))
2748
2749 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2750   "Insert a dummy root in the summary buffer."
2751   (beginning-of-line)
2752   (gnus-add-text-properties
2753    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2754    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2755
2756 (defun gnus-summary-from-or-to-or-newsgroups (header)
2757   (let ((to (cdr (assq 'To (mail-header-extra header))))
2758         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2759         (default-mime-charset (with-current-buffer gnus-summary-buffer
2760                                 default-mime-charset)))
2761     (cond
2762      ((and to
2763            gnus-ignored-from-addresses
2764            (string-match gnus-ignored-from-addresses
2765                          (mail-header-from header)))
2766       (concat "-> "
2767               (or (car (funcall gnus-extract-address-components
2768                                 (funcall
2769                                  gnus-decode-encoded-word-function to)))
2770                   (funcall gnus-decode-encoded-word-function to))))
2771      ((and newsgroups
2772            gnus-ignored-from-addresses
2773            (string-match gnus-ignored-from-addresses
2774                          (mail-header-from header)))
2775       (concat "=> " newsgroups))
2776      (t
2777       (or (car (funcall gnus-extract-address-components
2778                         (mail-header-from header)))
2779           (mail-header-from header))))))
2780
2781 (defun gnus-summary-insert-line (gnus-tmp-header
2782                                  gnus-tmp-level gnus-tmp-current
2783                                  gnus-tmp-unread gnus-tmp-replied
2784                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2785                                  &optional gnus-tmp-dummy gnus-tmp-score
2786                                  gnus-tmp-process)
2787   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2788          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2789          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2790          (gnus-tmp-score-char
2791           (if (or (null gnus-summary-default-score)
2792                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2793                       gnus-summary-zcore-fuzz))
2794               ?  ;Whitespace
2795             (if (< gnus-tmp-score gnus-summary-default-score)
2796                 gnus-score-below-mark gnus-score-over-mark)))
2797          (gnus-tmp-replied
2798           (cond (gnus-tmp-process gnus-process-mark)
2799                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2800                  gnus-cached-mark)
2801                 (gnus-tmp-replied gnus-replied-mark)
2802                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2803                  gnus-saved-mark)
2804                 (t gnus-unread-mark)))
2805          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2806          (gnus-tmp-name
2807           (cond
2808            ((string-match "<[^>]+> *$" gnus-tmp-from)
2809             (let ((beg (match-beginning 0)))
2810               (or (and (string-match "^\".+\"" gnus-tmp-from)
2811                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2812                   (substring gnus-tmp-from 0 beg))))
2813            ((string-match "(.+)" gnus-tmp-from)
2814             (substring gnus-tmp-from
2815                        (1+ (match-beginning 0)) (1- (match-end 0))))
2816            (t gnus-tmp-from)))
2817          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2818          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2819          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2820          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2821          (buffer-read-only nil))
2822     (when (string= gnus-tmp-name "")
2823       (setq gnus-tmp-name gnus-tmp-from))
2824     (unless (numberp gnus-tmp-lines)
2825       (setq gnus-tmp-lines 0))
2826     (gnus-put-text-property-excluding-characters-with-faces
2827      (point)
2828      (progn (eval gnus-summary-line-format-spec) (point))
2829      'gnus-number gnus-tmp-number)
2830     (when (gnus-visual-p 'summary-highlight 'highlight)
2831       (forward-line -1)
2832       (gnus-run-hooks 'gnus-summary-update-hook)
2833       (forward-line 1))))
2834
2835 (defun gnus-summary-update-line (&optional dont-update)
2836   "Update summary line after change."
2837   (when (and gnus-summary-default-score
2838              (not gnus-summary-inhibit-highlight))
2839     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2840            (article (gnus-summary-article-number))
2841            (score (gnus-summary-article-score article)))
2842       (unless dont-update
2843         (if (and gnus-summary-mark-below
2844                  (< (gnus-summary-article-score)
2845                     gnus-summary-mark-below))
2846             ;; This article has a low score, so we mark it as read.
2847             (when (memq article gnus-newsgroup-unreads)
2848               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2849           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2850             ;; This article was previously marked as read on account
2851             ;; of a low score, but now it has risen, so we mark it as
2852             ;; unread.
2853             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2854         (gnus-summary-update-mark
2855          (if (or (null gnus-summary-default-score)
2856                  (<= (abs (- score gnus-summary-default-score))
2857                      gnus-summary-zcore-fuzz))
2858              ?  ;Whitespace
2859            (if (< score gnus-summary-default-score)
2860                gnus-score-below-mark gnus-score-over-mark))
2861          'score))
2862       ;; Do visual highlighting.
2863       (when (gnus-visual-p 'summary-highlight 'highlight)
2864         (gnus-run-hooks 'gnus-summary-update-hook)))))
2865
2866 (defvar gnus-tmp-new-adopts nil)
2867
2868 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2869   "Return the number of articles in THREAD.
2870 This may be 0 in some cases -- if none of the articles in
2871 the thread are to be displayed."
2872   (let* ((number
2873           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2874           (cond
2875            ((not (listp thread))
2876             1)
2877            ((and (consp thread) (cdr thread))
2878             (apply
2879              '+ 1 (mapcar
2880                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2881            ((null thread)
2882             1)
2883            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2884             1)
2885            (t 0))))
2886     (when (and level (zerop level) gnus-tmp-new-adopts)
2887       (incf number
2888             (apply '+ (mapcar
2889                        'gnus-summary-number-of-articles-in-thread
2890                        gnus-tmp-new-adopts))))
2891     (if char
2892         (if (> number 1) gnus-not-empty-thread-mark
2893           gnus-empty-thread-mark)
2894       number)))
2895
2896 (defun gnus-summary-set-local-parameters (group)
2897   "Go through the local params of GROUP and set all variable specs in that list."
2898   (let ((params (gnus-group-find-parameter group))
2899         elem)
2900     (while params
2901       (setq elem (car params)
2902             params (cdr params))
2903       (and (consp elem)                 ; Has to be a cons.
2904            (consp (cdr elem))           ; The cdr has to be a list.
2905            (symbolp (car elem))         ; Has to be a symbol in there.
2906            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2907            (ignore-errors               ; So we set it.
2908              (make-local-variable (car elem))
2909              (set (car elem) (eval (nth 1 elem))))))))
2910
2911 (defun gnus-summary-read-group (group &optional show-all no-article
2912                                       kill-buffer no-display backward
2913                                       select-articles)
2914   "Start reading news in newsgroup GROUP.
2915 If SHOW-ALL is non-nil, already read articles are also listed.
2916 If NO-ARTICLE is non-nil, no article is selected initially.
2917 If NO-DISPLAY, don't generate a summary buffer."
2918   (let (result)
2919     (while (and group
2920                 (null (setq result
2921                             (let ((gnus-auto-select-next nil))
2922                               (or (gnus-summary-read-group-1
2923                                    group show-all no-article
2924                                    kill-buffer no-display
2925                                    select-articles)
2926                                   (setq show-all nil
2927                                         select-articles nil)))))
2928                 (eq gnus-auto-select-next 'quietly))
2929       (set-buffer gnus-group-buffer)
2930       ;; The entry function called above goes to the next
2931       ;; group automatically, so we go two groups back
2932       ;; if we are searching for the previous group.
2933       (when backward
2934         (gnus-group-prev-unread-group 2))
2935       (if (not (equal group (gnus-group-group-name)))
2936           (setq group (gnus-group-group-name))
2937         (setq group nil)))
2938     result))
2939
2940 (defun gnus-summary-jump-to-other-group (group &optional show-all)
2941   "Directly jump to the other GROUP from summary buffer.
2942 If SHOW-ALL is non-nil, already read articles are also listed."
2943   (interactive
2944    (if (eq gnus-summary-buffer (current-buffer))
2945        (list (completing-read
2946               "Group: " gnus-active-hashtb nil t
2947               (when (and gnus-newsgroup-name
2948                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
2949                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
2950               'gnus-group-history)
2951              current-prefix-arg)
2952      (error "%s must be invoked from a gnus summary buffer." this-command)))
2953   (unless (or (zerop (length group))
2954               (and gnus-newsgroup-name
2955                    (string-equal gnus-newsgroup-name group)))
2956     (gnus-summary-exit)
2957     (gnus-summary-read-group group show-all
2958                              gnus-dont-select-after-jump-to-other-group)))
2959
2960 (defun gnus-summary-read-group-1 (group show-all no-article
2961                                         kill-buffer no-display
2962                                         &optional select-articles)
2963   ;; Killed foreign groups can't be entered.
2964   (when (and (not (gnus-group-native-p group))
2965              (not (gnus-gethash group gnus-newsrc-hashtb)))
2966     (error "Dead non-native groups can't be entered"))
2967   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2968   (let* ((new-group (gnus-summary-setup-buffer group))
2969          (quit-config (gnus-group-quit-config group))
2970          (did-select (and new-group (gnus-select-newsgroup
2971                                      group show-all select-articles))))
2972     (cond
2973      ;; This summary buffer exists already, so we just select it.
2974      ((not new-group)
2975       (gnus-set-global-variables)
2976       (when kill-buffer
2977         (gnus-kill-or-deaden-summary kill-buffer))
2978       (gnus-configure-windows 'summary 'force)
2979       (gnus-set-mode-line 'summary)
2980       (gnus-summary-position-point)
2981       (message "")
2982       t)
2983      ;; We couldn't select this group.
2984      ((null did-select)
2985       (when (and (eq major-mode 'gnus-summary-mode)
2986                  (not (equal (current-buffer) kill-buffer)))
2987         (kill-buffer (current-buffer))
2988         (if (not quit-config)
2989             (progn
2990               ;; Update the info -- marks might need to be removed,
2991               ;; for instance.
2992               (gnus-summary-update-info)
2993               (set-buffer gnus-group-buffer)
2994               (gnus-group-jump-to-group group)
2995               (gnus-group-next-unread-group 1))
2996           (gnus-handle-ephemeral-exit quit-config)))
2997       (gnus-message 3 "Can't select group")
2998       nil)
2999      ;; The user did a `C-g' while prompting for number of articles,
3000      ;; so we exit this group.
3001      ((eq did-select 'quit)
3002       (and (eq major-mode 'gnus-summary-mode)
3003            (not (equal (current-buffer) kill-buffer))
3004            (kill-buffer (current-buffer)))
3005       (when kill-buffer
3006         (gnus-kill-or-deaden-summary kill-buffer))
3007       (if (not quit-config)
3008           (progn
3009             (set-buffer gnus-group-buffer)
3010             (gnus-group-jump-to-group group)
3011             (gnus-group-next-unread-group 1)
3012             (gnus-configure-windows 'group 'force))
3013         (gnus-handle-ephemeral-exit quit-config))
3014       ;; Finally signal the quit.
3015       (signal 'quit nil))
3016      ;; The group was successfully selected.
3017      (t
3018       (gnus-set-global-variables)
3019       ;; Save the active value in effect when the group was entered.
3020       (setq gnus-newsgroup-active
3021             (gnus-copy-sequence
3022              (gnus-active gnus-newsgroup-name)))
3023       ;; You can change the summary buffer in some way with this hook.
3024       (gnus-run-hooks 'gnus-select-group-hook)
3025       ;; Set any local variables in the group parameters.
3026       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3027       (gnus-update-format-specifications
3028        nil 'summary 'summary-mode 'summary-dummy)
3029       (gnus-update-summary-mark-positions)
3030       ;; Do score processing.
3031       (when gnus-use-scoring
3032         (gnus-possibly-score-headers))
3033       ;; Check whether to fill in the gaps in the threads.
3034       (when gnus-build-sparse-threads
3035         (gnus-build-sparse-threads))
3036       ;; Find the initial limit.
3037       (if gnus-show-threads
3038           (if show-all
3039               (let ((gnus-newsgroup-dormant nil))
3040                 (gnus-summary-initial-limit show-all))
3041             (gnus-summary-initial-limit show-all))
3042         ;; When untreaded, all articles are always shown.
3043         (setq gnus-newsgroup-limit
3044               (mapcar
3045                (lambda (header) (mail-header-number header))
3046                gnus-newsgroup-headers)))
3047       ;; Generate the summary buffer.
3048       (unless no-display
3049         (gnus-summary-prepare))
3050       (when gnus-use-trees
3051         (gnus-tree-open group)
3052         (setq gnus-summary-highlight-line-function
3053               'gnus-tree-highlight-article))
3054       ;; If the summary buffer is empty, but there are some low-scored
3055       ;; articles or some excluded dormants, we include these in the
3056       ;; buffer.
3057       (when (and (zerop (buffer-size))
3058                  (not no-display))
3059         (cond (gnus-newsgroup-dormant
3060                (gnus-summary-limit-include-dormant))
3061               ((and gnus-newsgroup-scored show-all)
3062                (gnus-summary-limit-include-expunged t))))
3063       ;; Function `gnus-apply-kill-file' must be called in this hook.
3064       (gnus-run-hooks 'gnus-apply-kill-hook)
3065       (if (and (zerop (buffer-size))
3066                (not no-display))
3067           (progn
3068             ;; This newsgroup is empty.
3069             (gnus-summary-catchup-and-exit nil t)
3070             (gnus-message 6 "No unread news")
3071             (when kill-buffer
3072               (gnus-kill-or-deaden-summary kill-buffer))
3073             ;; Return nil from this function.
3074             nil)
3075         ;; Hide conversation thread subtrees.  We cannot do this in
3076         ;; gnus-summary-prepare-hook since kill processing may not
3077         ;; work with hidden articles.
3078         (and gnus-show-threads
3079              gnus-thread-hide-subtree
3080              (gnus-summary-hide-all-threads))
3081         (when kill-buffer
3082           (gnus-kill-or-deaden-summary kill-buffer))
3083         ;; Show first unread article if requested.
3084         (if (and (not no-article)
3085                  (not no-display)
3086                  gnus-newsgroup-unreads
3087                  gnus-auto-select-first)
3088             (progn
3089               (gnus-configure-windows 'summary)
3090               (cond
3091                ((eq gnus-auto-select-first 'best)
3092                 (gnus-summary-best-unread-article))
3093                ((eq gnus-auto-select-first t)
3094                 (gnus-summary-first-unread-article))
3095                ((gnus-functionp gnus-auto-select-first)
3096                 (funcall gnus-auto-select-first))))
3097           ;; Don't select any articles, just move point to the first
3098           ;; article in the group.
3099           (goto-char (point-min))
3100           (gnus-summary-position-point)
3101           (gnus-configure-windows 'summary 'force)
3102           (gnus-set-mode-line 'summary))
3103         (when (get-buffer-window gnus-group-buffer t)
3104           ;; Gotta use windows, because recenter does weird stuff if
3105           ;; the current buffer ain't the displayed window.
3106           (let ((owin (selected-window)))
3107             (select-window (get-buffer-window gnus-group-buffer t))
3108             (when (gnus-group-goto-group group)
3109               (recenter))
3110             (select-window owin)))
3111         ;; Mark this buffer as "prepared".
3112         (setq gnus-newsgroup-prepared t)
3113         (gnus-run-hooks 'gnus-summary-prepared-hook)
3114         t)))))
3115
3116 (defun gnus-summary-prepare ()
3117   "Generate the summary buffer."
3118   (interactive)
3119   (let ((buffer-read-only nil))
3120     (erase-buffer)
3121     (setq gnus-newsgroup-data nil
3122           gnus-newsgroup-data-reverse nil)
3123     (gnus-run-hooks 'gnus-summary-generate-hook)
3124     ;; Generate the buffer, either with threads or without.
3125     (when gnus-newsgroup-headers
3126       (gnus-summary-prepare-threads
3127        (if gnus-show-threads
3128            (gnus-sort-gathered-threads
3129             (funcall gnus-summary-thread-gathering-function
3130                      (gnus-sort-threads
3131                       (gnus-cut-threads (gnus-make-threads)))))
3132          ;; Unthreaded display.
3133          (gnus-sort-articles gnus-newsgroup-headers))))
3134     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3135     ;; Call hooks for modifying summary buffer.
3136     (goto-char (point-min))
3137     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3138
3139 (defsubst gnus-general-simplify-subject (subject)
3140   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3141   (setq subject
3142         (cond
3143          ;; Truncate the subject.
3144          (gnus-simplify-subject-functions
3145           (gnus-map-function gnus-simplify-subject-functions subject))
3146          ((numberp gnus-summary-gather-subject-limit)
3147           (setq subject (gnus-simplify-subject-re subject))
3148           (if (> (length subject) gnus-summary-gather-subject-limit)
3149               (substring subject 0 gnus-summary-gather-subject-limit)
3150             subject))
3151          ;; Fuzzily simplify it.
3152          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3153           (gnus-simplify-subject-fuzzy subject))
3154          ;; Just remove the leading "Re:".
3155          (t
3156           (gnus-simplify-subject-re subject))))
3157
3158   (if (and gnus-summary-gather-exclude-subject
3159            (string-match gnus-summary-gather-exclude-subject subject))
3160       nil                               ; This article shouldn't be gathered
3161     subject))
3162
3163 (defun gnus-summary-simplify-subject-query ()
3164   "Query where the respool algorithm would put this article."
3165   (interactive)
3166   (gnus-summary-select-article)
3167   (message "%s"
3168            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3169
3170 (defun gnus-gather-threads-by-subject (threads)
3171   "Gather threads by looking at Subject headers."
3172   (if (not gnus-summary-make-false-root)
3173       threads
3174     (let ((hashtb (gnus-make-hashtable 1024))
3175           (prev threads)
3176           (result threads)
3177           subject hthread whole-subject)
3178       (while threads
3179         (setq subject (gnus-general-simplify-subject
3180                        (setq whole-subject (mail-header-subject
3181                                             (caar threads)))))
3182         (when subject
3183           (if (setq hthread (gnus-gethash subject hashtb))
3184               (progn
3185                 ;; We enter a dummy root into the thread, if we
3186                 ;; haven't done that already.
3187                 (unless (stringp (caar hthread))
3188                   (setcar hthread (list whole-subject (car hthread))))
3189                 ;; We add this new gathered thread to this gathered
3190                 ;; thread.
3191                 (setcdr (car hthread)
3192                         (nconc (cdar hthread) (list (car threads))))
3193                 ;; Remove it from the list of threads.
3194                 (setcdr prev (cdr threads))
3195                 (setq threads prev))
3196             ;; Enter this thread into the hash table.
3197             (gnus-sethash subject threads hashtb)))
3198         (setq prev threads)
3199         (setq threads (cdr threads)))
3200       result)))
3201
3202 (defun gnus-gather-threads-by-references (threads)
3203   "Gather threads by looking at References headers."
3204   (let ((idhashtb (gnus-make-hashtable 1024))
3205         (thhashtb (gnus-make-hashtable 1024))
3206         (prev threads)
3207         (result threads)
3208         ids references id gthread gid entered ref)
3209     (while threads
3210       (when (setq references (mail-header-references (caar threads)))
3211         (setq id (mail-header-id (caar threads))
3212               ids (gnus-split-references references)
3213               entered nil)
3214         (while (setq ref (pop ids))
3215           (setq ids (delete ref ids))
3216           (if (not (setq gid (gnus-gethash ref idhashtb)))
3217               (progn
3218                 (gnus-sethash ref id idhashtb)
3219                 (gnus-sethash id threads thhashtb))
3220             (setq gthread (gnus-gethash gid thhashtb))
3221             (unless entered
3222               ;; We enter a dummy root into the thread, if we
3223               ;; haven't done that already.
3224               (unless (stringp (caar gthread))
3225                 (setcar gthread (list (mail-header-subject (caar gthread))
3226                                       (car gthread))))
3227               ;; We add this new gathered thread to this gathered
3228               ;; thread.
3229               (setcdr (car gthread)
3230                       (nconc (cdar gthread) (list (car threads)))))
3231             ;; Add it into the thread hash table.
3232             (gnus-sethash id gthread thhashtb)
3233             (setq entered t)
3234             ;; Remove it from the list of threads.
3235             (setcdr prev (cdr threads))
3236             (setq threads prev))))
3237       (setq prev threads)
3238       (setq threads (cdr threads)))
3239     result))
3240
3241 (defun gnus-sort-gathered-threads (threads)
3242   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3243   (let ((result threads))
3244     (while threads
3245       (when (stringp (caar threads))
3246         (setcdr (car threads)
3247                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3248       (setq threads (cdr threads)))
3249     result))
3250
3251 (defun gnus-thread-loop-p (root thread)
3252   "Say whether ROOT is in THREAD."
3253   (let ((stack (list thread))
3254         (infloop 0)
3255         th)
3256     (while (setq thread (pop stack))
3257       (setq th (cdr thread))
3258       (while (and th
3259                   (not (eq (caar th) root)))
3260         (pop th))
3261       (if th
3262           ;; We have found a loop.
3263           (let (ref-dep)
3264             (setcdr thread (delq (car th) (cdr thread)))
3265             (if (boundp (setq ref-dep (intern "none"
3266                                               gnus-newsgroup-dependencies)))
3267                 (setcdr (symbol-value ref-dep)
3268                         (nconc (cdr (symbol-value ref-dep))
3269                                (list (car th))))
3270               (set ref-dep (list nil (car th))))
3271             (setq infloop 1
3272                   stack nil))
3273         ;; Push all the subthreads onto the stack.
3274         (push (cdr thread) stack)))
3275     infloop))
3276
3277 (defun gnus-make-threads ()
3278   "Go through the dependency hashtb and find the roots.  Return all threads."
3279   (let (threads)
3280     (while (catch 'infloop
3281              (mapatoms
3282               (lambda (refs)
3283                 ;; Deal with self-referencing References loops.
3284                 (when (and (car (symbol-value refs))
3285                            (not (zerop
3286                                  (apply
3287                                   '+
3288                                   (mapcar
3289                                    (lambda (thread)
3290                                      (gnus-thread-loop-p
3291                                       (car (symbol-value refs)) thread))
3292                                    (cdr (symbol-value refs)))))))
3293                   (setq threads nil)
3294                   (throw 'infloop t))
3295                 (unless (car (symbol-value refs))
3296                   ;; These threads do not refer back to any other articles,
3297                   ;; so they're roots.
3298                   (setq threads (append (cdr (symbol-value refs)) threads))))
3299               gnus-newsgroup-dependencies)))
3300     threads))
3301
3302 ;; Build the thread tree.
3303 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3304   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3305
3306 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3307 if it was already present.
3308
3309 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3310 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3311 Message-IDs will be renamed be renamed to a unique Message-ID before
3312 being entered.
3313
3314 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3315   (let* ((id (mail-header-id header))
3316          (id-dep (and id (intern id dependencies)))
3317          ref ref-dep ref-header)
3318     ;; Enter this `header' in the `dependencies' table.
3319     (cond
3320      ((not id-dep)
3321       (setq header nil))
3322      ;; The first two cases do the normal part: enter a new `header'
3323      ;; in the `dependencies' table.
3324      ((not (boundp id-dep))
3325       (set id-dep (list header)))
3326      ((null (car (symbol-value id-dep)))
3327       (setcar (symbol-value id-dep) header))
3328
3329      ;; From here the `header' was already present in the
3330      ;; `dependencies' table.
3331      (force-new
3332       ;; Overrides an existing entry;
3333       ;; just set the header part of the entry.
3334       (setcar (symbol-value id-dep) header))
3335
3336      ;; Renames the existing `header' to a unique Message-ID.
3337      ((not gnus-summary-ignore-duplicates)
3338       ;; An article with this Message-ID has already been seen.
3339       ;; We rename the Message-ID.
3340       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3341            (list header))
3342       (mail-header-set-id header id))
3343
3344      ;; The last case ignores an existing entry, except it adds any
3345      ;; additional Xrefs (in case the two articles came from different
3346      ;; servers.
3347      ;; Also sets `header' to `nil' meaning that the `dependencies'
3348      ;; table was *not* modified.
3349      (t
3350       (mail-header-set-xref
3351        (car (symbol-value id-dep))
3352        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3353                    "")
3354                (or (mail-header-xref header) "")))
3355       (setq header nil)))
3356
3357     (when header
3358       ;; First check if that we are not creating a References loop.
3359       (setq ref (gnus-parent-id (mail-header-references header)))
3360       (while (and ref
3361                   (setq ref-dep (intern-soft ref dependencies))
3362                   (boundp ref-dep)
3363                   (setq ref-header (car (symbol-value ref-dep))))
3364         (if (string= id ref)
3365             ;; Yuk!  This is a reference loop.  Make the article be a
3366             ;; root article.
3367             (progn
3368               (mail-header-set-references (car (symbol-value id-dep)) "none")
3369               (setq ref nil))
3370           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3371       (setq ref (gnus-parent-id (mail-header-references header)))
3372       (setq ref-dep (intern (or ref "none") dependencies))
3373       (if (boundp ref-dep)
3374           (setcdr (symbol-value ref-dep)
3375                   (nconc (cdr (symbol-value ref-dep))
3376                          (list (symbol-value id-dep))))
3377         (set ref-dep (list nil (symbol-value id-dep)))))
3378     header))
3379
3380 (defun gnus-build-sparse-threads ()
3381   (let ((headers gnus-newsgroup-headers)
3382         (mail-parse-charset gnus-newsgroup-charset)
3383         (gnus-summary-ignore-duplicates t)
3384         header references generation relations
3385         subject child end new-child date)
3386     ;; First we create an alist of generations/relations, where
3387     ;; generations is how much we trust the relation, and the relation
3388     ;; is parent/child.
3389     (gnus-message 7 "Making sparse threads...")
3390     (save-excursion
3391       (nnheader-set-temp-buffer " *gnus sparse threads*")
3392       (while (setq header (pop headers))
3393         (when (and (setq references (mail-header-references header))
3394                    (not (string= references "")))
3395           (insert references)
3396           (setq child (mail-header-id header)
3397                 subject (mail-header-subject header)
3398                 date (mail-header-date header)
3399                 generation 0)
3400           (while (search-backward ">" nil t)
3401             (setq end (1+ (point)))
3402             (when (search-backward "<" nil t)
3403               (setq new-child (buffer-substring (point) end))
3404               (push (list (incf generation)
3405                           child (setq child new-child)
3406                           subject date)
3407                     relations)))
3408           (when child
3409             (push (list (1+ generation) child nil subject) relations))
3410           (erase-buffer)))
3411       (kill-buffer (current-buffer)))
3412     ;; Sort over trustworthiness.
3413     (mapcar
3414      (lambda (relation)
3415        (when (gnus-dependencies-add-header
3416               (make-full-mail-header-from-decoded-header
3417                gnus-reffed-article-number
3418                (nth 3 relation) "" (or (nth 4 relation) "")
3419                (nth 1 relation)
3420                (or (nth 2 relation) "") 0 0 "")
3421               gnus-newsgroup-dependencies nil)
3422          (push gnus-reffed-article-number gnus-newsgroup-limit)
3423          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3424          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3425                gnus-newsgroup-reads)
3426          (decf gnus-reffed-article-number)))
3427      (sort relations 'car-less-than-car))
3428     (gnus-message 7 "Making sparse threads...done")))
3429
3430 (defun gnus-build-old-threads ()
3431   ;; Look at all the articles that refer back to old articles, and
3432   ;; fetch the headers for the articles that aren't there.  This will
3433   ;; build complete threads - if the roots haven't been expired by the
3434   ;; server, that is.
3435   (let ((mail-parse-charset gnus-newsgroup-charset)
3436         id heads)
3437     (mapatoms
3438      (lambda (refs)
3439        (when (not (car (symbol-value refs)))
3440          (setq heads (cdr (symbol-value refs)))
3441          (while heads
3442            (if (memq (mail-header-number (caar heads))
3443                      gnus-newsgroup-dormant)
3444                (setq heads (cdr heads))
3445              (setq id (symbol-name refs))
3446              (while (and (setq id (gnus-build-get-header id))
3447                          (not (car (gnus-id-to-thread id)))))
3448              (setq heads nil)))))
3449      gnus-newsgroup-dependencies)))
3450
3451 ;; This function has to be called with point after the article number
3452 ;; on the beginning of the line.
3453 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3454   (let ((eol (gnus-point-at-eol))
3455         (buffer (current-buffer))
3456         header)
3457
3458     ;; overview: [num subject from date id refs chars lines misc]
3459     (unwind-protect
3460         (progn
3461           (narrow-to-region (point) eol)
3462           (unless (eobp)
3463             (forward-char))
3464
3465           (setq header
3466                 (make-full-mail-header
3467                  number                         ; number
3468                  (nnheader-nov-field)           ; subject
3469                  (nnheader-nov-field)           ; from
3470                  (nnheader-nov-field)           ; date
3471                  (nnheader-nov-read-message-id) ; id
3472                  (nnheader-nov-field)           ; refs
3473                  (nnheader-nov-read-integer)    ; chars
3474                  (nnheader-nov-read-integer)    ; lines
3475                  (unless (eobp)
3476                    (if (looking-at "Xref: ")
3477                        (goto-char (match-end 0)))
3478                    (nnheader-nov-field))        ; Xref
3479                  (nnheader-nov-parse-extra))))  ; extra
3480
3481       (widen))
3482
3483     (when gnus-alter-header-function
3484       (funcall gnus-alter-header-function header))
3485     (gnus-dependencies-add-header header dependencies force-new)))
3486
3487 (defun gnus-build-get-header (id)
3488   "Look through the buffer of NOV lines and find the header to ID.
3489 Enter this line into the dependencies hash table, and return
3490 the id of the parent article (if any)."
3491   (let ((deps gnus-newsgroup-dependencies)
3492         found header)
3493     (prog1
3494         (save-excursion
3495           (set-buffer nntp-server-buffer)
3496           (let ((case-fold-search nil))
3497             (goto-char (point-min))
3498             (while (and (not found)
3499                         (search-forward id nil t))
3500               (beginning-of-line)
3501               (setq found (looking-at
3502                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3503                                    (regexp-quote id))))
3504               (or found (beginning-of-line 2)))
3505             (when found
3506               (beginning-of-line)
3507               (and
3508                (setq header (gnus-nov-parse-line
3509                              (read (current-buffer)) deps))
3510                (gnus-parent-id (mail-header-references header))))))
3511       (when header
3512         (let ((number (mail-header-number header)))
3513           (push number gnus-newsgroup-limit)
3514           (push header gnus-newsgroup-headers)
3515           (if (memq number gnus-newsgroup-unselected)
3516               (progn
3517                 (push number gnus-newsgroup-unreads)
3518                 (setq gnus-newsgroup-unselected
3519                       (delq number gnus-newsgroup-unselected)))
3520             (push number gnus-newsgroup-ancient)))))))
3521
3522 (defun gnus-build-all-threads ()
3523   "Read all the headers."
3524   (let ((gnus-summary-ignore-duplicates t)
3525         (mail-parse-charset gnus-newsgroup-charset)
3526         (dependencies gnus-newsgroup-dependencies)
3527         header article)
3528     (save-excursion
3529       (set-buffer nntp-server-buffer)
3530       (let ((case-fold-search nil))
3531         (goto-char (point-min))
3532         (while (not (eobp))
3533           (ignore-errors
3534             (setq article (read (current-buffer))
3535                   header (gnus-nov-parse-line article dependencies)))
3536           (when header
3537             (save-excursion
3538               (set-buffer gnus-summary-buffer)
3539               (push header gnus-newsgroup-headers)
3540               (if (memq (setq article (mail-header-number header))
3541                         gnus-newsgroup-unselected)
3542                   (progn
3543                     (push article gnus-newsgroup-unreads)
3544                     (setq gnus-newsgroup-unselected
3545                           (delq article gnus-newsgroup-unselected)))
3546                 (push article gnus-newsgroup-ancient)))
3547             (forward-line 1)))))))
3548
3549 (defun gnus-summary-update-article-line (article header)
3550   "Update the line for ARTICLE using HEADERS."
3551   (let* ((id (mail-header-id header))
3552          (thread (gnus-id-to-thread id)))
3553     (unless thread
3554       (error "Article in no thread"))
3555     ;; Update the thread.
3556     (setcar thread header)
3557     (gnus-summary-goto-subject article)
3558     (let* ((datal (gnus-data-find-list article))
3559            (data (car datal))
3560            (length (when (cdr datal)
3561                      (- (gnus-data-pos data)
3562                         (gnus-data-pos (cadr datal)))))
3563            (buffer-read-only nil)
3564            (level (gnus-summary-thread-level)))
3565       (gnus-delete-line)
3566       (gnus-summary-insert-line
3567        header level nil (gnus-article-mark article)
3568        (memq article gnus-newsgroup-replied)
3569        (memq article gnus-newsgroup-expirable)
3570        ;; Only insert the Subject string when it's different
3571        ;; from the previous Subject string.
3572        (if (and
3573             gnus-show-threads
3574             (gnus-subject-equal
3575              (condition-case ()
3576                  (mail-header-subject
3577                   (gnus-data-header
3578                    (cadr
3579                     (gnus-data-find-list
3580                      article
3581                      (gnus-data-list t)))))
3582                ;; Error on the side of excessive subjects.
3583                (error ""))
3584              (mail-header-subject header)))
3585            ""
3586          (mail-header-subject header))
3587        nil (cdr (assq article gnus-newsgroup-scored))
3588        (memq article gnus-newsgroup-processable))
3589       (when length
3590         (gnus-data-update-list
3591          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3592
3593 (defun gnus-summary-update-article (article &optional iheader)
3594   "Update ARTICLE in the summary buffer."
3595   (set-buffer gnus-summary-buffer)
3596   (let* ((header (gnus-summary-article-header article))
3597          (id (mail-header-id header))
3598          (data (gnus-data-find article))
3599          (thread (gnus-id-to-thread id))
3600          (references (mail-header-references header))
3601          (parent
3602           (gnus-id-to-thread
3603            (or (gnus-parent-id
3604                 (when (and references
3605                            (not (equal "" references)))
3606                   references))
3607                "none")))
3608          (buffer-read-only nil)
3609          (old (car thread)))
3610     (when thread
3611       (unless iheader
3612         (setcar thread nil)
3613         (when parent
3614           (delq thread parent)))
3615       (if (gnus-summary-insert-subject id header)
3616           ;; Set the (possibly) new article number in the data structure.
3617           (gnus-data-set-number data (gnus-id-to-article id))
3618         (setcar thread old)
3619         nil))))
3620
3621 (defun gnus-rebuild-thread (id &optional line)
3622   "Rebuild the thread containing ID.
3623 If LINE, insert the rebuilt thread starting on line LINE."
3624   (let ((buffer-read-only nil)
3625         old-pos current thread data)
3626     (if (not gnus-show-threads)
3627         (setq thread (list (car (gnus-id-to-thread id))))
3628       ;; Get the thread this article is part of.
3629       (setq thread (gnus-remove-thread id)))
3630     (setq old-pos (gnus-point-at-bol))
3631     (setq current (save-excursion
3632                     (and (zerop (forward-line -1))
3633                          (gnus-summary-article-number))))
3634     ;; If this is a gathered thread, we have to go some re-gathering.
3635     (when (stringp (car thread))
3636       (let ((subject (car thread))
3637             roots thr)
3638         (setq thread (cdr thread))
3639         (while thread
3640           (unless (memq (setq thr (gnus-id-to-thread
3641                                    (gnus-root-id
3642                                     (mail-header-id (caar thread)))))
3643                         roots)
3644             (push thr roots))
3645           (setq thread (cdr thread)))
3646         ;; We now have all (unique) roots.
3647         (if (= (length roots) 1)
3648             ;; All the loose roots are now one solid root.
3649             (setq thread (car roots))
3650           (setq thread (cons subject (gnus-sort-threads roots))))))
3651     (let (threads)
3652       ;; We then insert this thread into the summary buffer.
3653       (when line
3654         (goto-char (point-min))
3655         (forward-line (1- line)))
3656       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3657         (if gnus-show-threads
3658             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3659           (gnus-summary-prepare-unthreaded thread))
3660         (setq data (nreverse gnus-newsgroup-data))
3661         (setq threads gnus-newsgroup-threads))
3662       ;; We splice the new data into the data structure.
3663       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3664       ;;!!! then we want to insert at the beginning of the buffer.
3665       ;;!!! That happens to be true with Gnus now, but that may
3666       ;;!!! change in the future.  Perhaps.
3667       (gnus-data-enter-list
3668        (if line nil current) data (- (point) old-pos))
3669       (setq gnus-newsgroup-threads
3670             (nconc threads gnus-newsgroup-threads))
3671       (gnus-data-compute-positions))))
3672
3673 (defun gnus-number-to-header (number)
3674   "Return the header for article NUMBER."
3675   (let ((headers gnus-newsgroup-headers))
3676     (while (and headers
3677                 (not (= number (mail-header-number (car headers)))))
3678       (pop headers))
3679     (when headers
3680       (car headers))))
3681
3682 (defun gnus-parent-headers (in-headers &optional generation)
3683   "Return the headers of the GENERATIONeth parent of HEADERS."
3684   (unless generation
3685     (setq generation 1))
3686   (let ((parent t)
3687         (headers in-headers)
3688         references)
3689     (while (and parent
3690                 (not (zerop generation))
3691                 (setq references (mail-header-references headers)))
3692       (setq headers (if (and references
3693                              (setq parent (gnus-parent-id references)))
3694                         (car (gnus-id-to-thread parent))
3695                       nil))
3696       (decf generation))
3697     (and (not (eq headers in-headers))
3698          headers)))
3699
3700 (defun gnus-id-to-thread (id)
3701   "Return the (sub-)thread where ID appears."
3702   (gnus-gethash id gnus-newsgroup-dependencies))
3703
3704 (defun gnus-id-to-article (id)
3705   "Return the article number of ID."
3706   (let ((thread (gnus-id-to-thread id)))
3707     (when (and thread
3708                (car thread))
3709       (mail-header-number (car thread)))))
3710
3711 (defun gnus-id-to-header (id)
3712   "Return the article headers of ID."
3713   (car (gnus-id-to-thread id)))
3714
3715 (defun gnus-article-displayed-root-p (article)
3716   "Say whether ARTICLE is a root(ish) article."
3717   (let ((level (gnus-summary-thread-level article))
3718         (refs (mail-header-references  (gnus-summary-article-header article)))
3719         particle)
3720     (cond
3721      ((null level) nil)
3722      ((zerop level) t)
3723      ((null refs) t)
3724      ((null (gnus-parent-id refs)) t)
3725      ((and (= 1 level)
3726            (null (setq particle (gnus-id-to-article
3727                                  (gnus-parent-id refs))))
3728            (null (gnus-summary-thread-level particle)))))))
3729
3730 (defun gnus-root-id (id)
3731   "Return the id of the root of the thread where ID appears."
3732   (let (last-id prev)
3733     (while (and id (setq prev (car (gnus-id-to-thread id))))
3734       (setq last-id id
3735             id (gnus-parent-id (mail-header-references prev))))
3736     last-id))
3737
3738 (defun gnus-articles-in-thread (thread)
3739   "Return the list of articles in THREAD."
3740   (cons (mail-header-number (car thread))
3741         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3742
3743 (defun gnus-remove-thread (id &optional dont-remove)
3744   "Remove the thread that has ID in it."
3745   (let (headers thread last-id)
3746     ;; First go up in this thread until we find the root.
3747     (setq last-id (gnus-root-id id)
3748           headers (message-flatten-list (gnus-id-to-thread last-id)))
3749     ;; We have now found the real root of this thread.  It might have
3750     ;; been gathered into some loose thread, so we have to search
3751     ;; through the threads to find the thread we wanted.
3752     (let ((threads gnus-newsgroup-threads)
3753           sub)
3754       (while threads
3755         (setq sub (car threads))
3756         (if (stringp (car sub))
3757             ;; This is a gathered thread, so we look at the roots
3758             ;; below it to find whether this article is in this
3759             ;; gathered root.
3760             (progn
3761               (setq sub (cdr sub))
3762               (while sub
3763                 (when (member (caar sub) headers)
3764                   (setq thread (car threads)
3765                         threads nil
3766                         sub nil))
3767                 (setq sub (cdr sub))))
3768           ;; It's an ordinary thread, so we check it.
3769           (when (eq (car sub) (car headers))
3770             (setq thread sub
3771                   threads nil)))
3772         (setq threads (cdr threads)))
3773       ;; If this article is in no thread, then it's a root.
3774       (if thread
3775           (unless dont-remove
3776             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3777         (setq thread (gnus-id-to-thread last-id)))
3778       (when thread
3779         (prog1
3780             thread                      ; We return this thread.
3781           (unless dont-remove
3782             (if (stringp (car thread))
3783                 (progn
3784                   ;; If we use dummy roots, then we have to remove the
3785                   ;; dummy root as well.
3786                   (when (eq gnus-summary-make-false-root 'dummy)
3787                     ;; We go to the dummy root by going to
3788                     ;; the first sub-"thread", and then one line up.
3789                     (gnus-summary-goto-article
3790                      (mail-header-number (caadr thread)))
3791                     (forward-line -1)
3792                     (gnus-delete-line)
3793                     (gnus-data-compute-positions))
3794                   (setq thread (cdr thread))
3795                   (while thread
3796                     (gnus-remove-thread-1 (car thread))
3797                     (setq thread (cdr thread))))
3798               (gnus-remove-thread-1 thread))))))))
3799
3800 (defun gnus-remove-thread-1 (thread)
3801   "Remove the thread THREAD recursively."
3802   (let ((number (mail-header-number (pop thread)))
3803         d)
3804     (setq thread (reverse thread))
3805     (while thread
3806       (gnus-remove-thread-1 (pop thread)))
3807     (when (setq d (gnus-data-find number))
3808       (goto-char (gnus-data-pos d))
3809       (gnus-summary-show-thread)
3810       (gnus-data-remove
3811        number
3812        (- (gnus-point-at-bol)
3813           (prog1
3814               (1+ (gnus-point-at-eol))
3815             (gnus-delete-line)))))))
3816
3817 (defun gnus-sort-threads-1 (threads func)
3818   (sort (mapcar (lambda (thread)
3819                   (cons (car thread)
3820                         (and (cdr thread)
3821                              (gnus-sort-threads-1 (cdr thread) func))))
3822                 threads) func))
3823
3824 (defun gnus-sort-threads (threads)
3825   "Sort THREADS."
3826   (if (not gnus-thread-sort-functions)
3827       threads
3828     (gnus-message 8 "Sorting threads...")
3829     (prog1
3830         (gnus-sort-threads-1 
3831          threads 
3832          (gnus-make-sort-function gnus-thread-sort-functions))
3833       (gnus-message 8 "Sorting threads...done"))))
3834
3835 (defun gnus-sort-articles (articles)
3836   "Sort ARTICLES."
3837   (when gnus-article-sort-functions
3838     (gnus-message 7 "Sorting articles...")
3839     (prog1
3840         (setq gnus-newsgroup-headers
3841               (sort articles (gnus-make-sort-function
3842                               gnus-article-sort-functions)))
3843       (gnus-message 7 "Sorting articles...done"))))
3844
3845 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3846 (defmacro gnus-thread-header (thread)
3847   "Return header of first article in THREAD.
3848 Note that THREAD must never, ever be anything else than a variable -
3849 using some other form will lead to serious barfage."
3850   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3851   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3852   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3853         (vector thread) 2))
3854
3855 (defsubst gnus-article-sort-by-number (h1 h2)
3856   "Sort articles by article number."
3857   (< (mail-header-number h1)
3858      (mail-header-number h2)))
3859
3860 (defun gnus-thread-sort-by-number (h1 h2)
3861   "Sort threads by root article number."
3862   (gnus-article-sort-by-number
3863    (gnus-thread-header h1) (gnus-thread-header h2)))
3864
3865 (defsubst gnus-article-sort-by-lines (h1 h2)
3866   "Sort articles by article Lines header."
3867   (< (mail-header-lines h1)
3868      (mail-header-lines h2)))
3869
3870 (defun gnus-thread-sort-by-lines (h1 h2)
3871   "Sort threads by root article Lines header."
3872   (gnus-article-sort-by-lines
3873    (gnus-thread-header h1) (gnus-thread-header h2)))
3874
3875 (defsubst gnus-article-sort-by-chars (h1 h2)
3876   "Sort articles by octet length."
3877   (< (mail-header-chars h1)
3878      (mail-header-chars h2)))
3879
3880 (defun gnus-thread-sort-by-chars (h1 h2)
3881   "Sort threads by root article octet length."
3882   (gnus-article-sort-by-chars
3883    (gnus-thread-header h1) (gnus-thread-header h2)))
3884
3885 (defsubst gnus-article-sort-by-author (h1 h2)
3886   "Sort articles by root author."
3887   (string-lessp
3888    (let ((addr (car (mime-entity-read-field h1 'From))))
3889      (or (std11-full-name-string addr)
3890          (std11-address-string addr)
3891          ""))
3892    (let ((addr (car (mime-entity-read-field h2 'From))))
3893      (or (std11-full-name-string addr)
3894          (std11-address-string addr)
3895          ""))
3896    ))
3897
3898 (defun gnus-thread-sort-by-author (h1 h2)
3899   "Sort threads by root author."
3900   (gnus-article-sort-by-author
3901    (gnus-thread-header h1)  (gnus-thread-header h2)))
3902
3903 (defsubst gnus-article-sort-by-subject (h1 h2)
3904   "Sort articles by root subject."
3905   (string-lessp
3906    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3907    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3908
3909 (defun gnus-thread-sort-by-subject (h1 h2)
3910   "Sort threads by root subject."
3911   (gnus-article-sort-by-subject
3912    (gnus-thread-header h1) (gnus-thread-header h2)))
3913
3914 (defsubst gnus-article-sort-by-date (h1 h2)
3915   "Sort articles by root article date."
3916   (time-less-p
3917    (gnus-date-get-time (mail-header-date h1))
3918    (gnus-date-get-time (mail-header-date h2))))
3919
3920 (defun gnus-thread-sort-by-date (h1 h2)
3921   "Sort threads by root article date."
3922   (gnus-article-sort-by-date
3923    (gnus-thread-header h1) (gnus-thread-header h2)))
3924
3925 (defsubst gnus-article-sort-by-score (h1 h2)
3926   "Sort articles by root article score.
3927 Unscored articles will be counted as having a score of zero."
3928   (> (or (cdr (assq (mail-header-number h1)
3929                     gnus-newsgroup-scored))
3930          gnus-summary-default-score 0)
3931      (or (cdr (assq (mail-header-number h2)
3932                     gnus-newsgroup-scored))
3933          gnus-summary-default-score 0)))
3934
3935 (defun gnus-thread-sort-by-score (h1 h2)
3936   "Sort threads by root article score."
3937   (gnus-article-sort-by-score
3938    (gnus-thread-header h1) (gnus-thread-header h2)))
3939
3940 (defun gnus-thread-sort-by-total-score (h1 h2)
3941   "Sort threads by the sum of all scores in the thread.
3942 Unscored articles will be counted as having a score of zero."
3943   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3944
3945 (defun gnus-thread-total-score (thread)
3946   ;; This function find the total score of THREAD.
3947   (cond ((null thread)
3948          0)
3949         ((consp thread)
3950          (if (stringp (car thread))
3951              (apply gnus-thread-score-function 0
3952                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3953            (gnus-thread-total-score-1 thread)))
3954         (t
3955          (gnus-thread-total-score-1 (list thread)))))
3956
3957 (defun gnus-thread-total-score-1 (root)
3958   ;; This function find the total score of the thread below ROOT.
3959   (setq root (car root))
3960   (apply gnus-thread-score-function
3961          (or (append
3962               (mapcar 'gnus-thread-total-score
3963                       (cdr (gnus-id-to-thread (mail-header-id root))))
3964               (when (> (mail-header-number root) 0)
3965                 (list (or (cdr (assq (mail-header-number root)
3966                                      gnus-newsgroup-scored))
3967                           gnus-summary-default-score 0))))
3968              (list gnus-summary-default-score)
3969              '(0))))
3970
3971 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3972 (defvar gnus-tmp-prev-subject nil)
3973 (defvar gnus-tmp-false-parent nil)
3974 (defvar gnus-tmp-root-expunged nil)
3975 (defvar gnus-tmp-dummy-line nil)
3976
3977 (eval-when-compile (defvar gnus-tmp-header))
3978 (defun gnus-extra-header (type &optional header)
3979   "Return the extra header of TYPE."
3980   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3981       ""))
3982
3983 (defun gnus-summary-prepare-threads (threads)
3984   "Prepare summary buffer from THREADS and indentation LEVEL.
3985 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3986 or a straight list of headers."
3987   (gnus-message 7 "Generating summary...")
3988
3989   (setq gnus-newsgroup-threads threads)
3990   (beginning-of-line)
3991
3992   (let ((gnus-tmp-level 0)
3993         (default-score (or gnus-summary-default-score 0))
3994         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3995         thread number subject stack state gnus-tmp-gathered beg-match
3996         new-roots gnus-tmp-new-adopts thread-end
3997         gnus-tmp-header gnus-tmp-unread
3998         gnus-tmp-replied gnus-tmp-subject-or-nil
3999         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4000         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4001         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
4002
4003     (setq gnus-tmp-prev-subject nil)
4004
4005     (if (vectorp (car threads))
4006         ;; If this is a straight (sic) list of headers, then a
4007         ;; threaded summary display isn't required, so we just create
4008         ;; an unthreaded one.
4009         (gnus-summary-prepare-unthreaded threads)
4010
4011       ;; Do the threaded display.
4012
4013       (while (or threads stack gnus-tmp-new-adopts new-roots)
4014
4015         (if (and (= gnus-tmp-level 0)
4016                  (or (not stack)
4017                      (= (caar stack) 0))
4018                  (not gnus-tmp-false-parent)
4019                  (or gnus-tmp-new-adopts new-roots))
4020             (if gnus-tmp-new-adopts
4021                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4022                       thread (list (car gnus-tmp-new-adopts))
4023                       gnus-tmp-header (caar thread)
4024                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4025               (when new-roots
4026                 (setq thread (list (car new-roots))
4027                       gnus-tmp-header (caar thread)
4028                       new-roots (cdr new-roots))))
4029
4030           (if threads
4031               ;; If there are some threads, we do them before the
4032               ;; threads on the stack.
4033               (setq thread threads
4034                     gnus-tmp-header (caar thread))
4035             ;; There were no current threads, so we pop something off
4036             ;; the stack.
4037             (setq state (car stack)
4038                   gnus-tmp-level (car state)
4039                   thread (cdr state)
4040                   stack (cdr stack)
4041                   gnus-tmp-header (caar thread))))
4042
4043         (setq gnus-tmp-false-parent nil)
4044         (setq gnus-tmp-root-expunged nil)
4045         (setq thread-end nil)
4046
4047         (if (stringp gnus-tmp-header)
4048             ;; The header is a dummy root.
4049             (cond
4050              ((eq gnus-summary-make-false-root 'adopt)
4051               ;; We let the first article adopt the rest.
4052               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4053                                                (cddar thread)))
4054               (setq gnus-tmp-gathered
4055                     (nconc (mapcar
4056                             (lambda (h) (mail-header-number (car h)))
4057                             (cddar thread))
4058                            gnus-tmp-gathered))
4059               (setq thread (cons (list (caar thread)
4060                                        (cadar thread))
4061                                  (cdr thread)))
4062               (setq gnus-tmp-level -1
4063                     gnus-tmp-false-parent t))
4064              ((eq gnus-summary-make-false-root 'empty)
4065               ;; We print adopted articles with empty subject fields.
4066               (setq gnus-tmp-gathered
4067                     (nconc (mapcar
4068                             (lambda (h) (mail-header-number (car h)))
4069                             (cddar thread))
4070                            gnus-tmp-gathered))
4071               (setq gnus-tmp-level -1))
4072              ((eq gnus-summary-make-false-root 'dummy)
4073               ;; We remember that we probably want to output a dummy
4074               ;; root.
4075               (setq gnus-tmp-dummy-line gnus-tmp-header)
4076               (setq gnus-tmp-prev-subject gnus-tmp-header))
4077              (t
4078               ;; We do not make a root for the gathered
4079               ;; sub-threads at all.
4080               (setq gnus-tmp-level -1)))
4081
4082           (setq number (mail-header-number gnus-tmp-header)
4083                 subject (mail-header-subject gnus-tmp-header))
4084
4085           (cond
4086            ;; If the thread has changed subject, we might want to make
4087            ;; this subthread into a root.
4088            ((and (null gnus-thread-ignore-subject)
4089                  (not (zerop gnus-tmp-level))
4090                  gnus-tmp-prev-subject
4091                  (not (inline
4092                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4093             (setq new-roots (nconc new-roots (list (car thread)))
4094                   thread-end t
4095                   gnus-tmp-header nil))
4096            ;; If the article lies outside the current limit,
4097            ;; then we do not display it.
4098            ((not (memq number gnus-newsgroup-limit))
4099             (setq gnus-tmp-gathered
4100                   (nconc (mapcar
4101                           (lambda (h) (mail-header-number (car h)))
4102                           (cdar thread))
4103                          gnus-tmp-gathered))
4104             (setq gnus-tmp-new-adopts (if (cdar thread)
4105                                           (append gnus-tmp-new-adopts
4106                                                   (cdar thread))
4107                                         gnus-tmp-new-adopts)
4108                   thread-end t
4109                   gnus-tmp-header nil)
4110             (when (zerop gnus-tmp-level)
4111               (setq gnus-tmp-root-expunged t)))
4112            ;; Perhaps this article is to be marked as read?
4113            ((and gnus-summary-mark-below
4114                  (< (or (cdr (assq number gnus-newsgroup-scored))
4115                         default-score)
4116                     gnus-summary-mark-below)
4117                  ;; Don't touch sparse articles.
4118                  (not (gnus-summary-article-sparse-p number))
4119                  (not (gnus-summary-article-ancient-p number)))
4120             (setq gnus-newsgroup-unreads
4121                   (delq number gnus-newsgroup-unreads))
4122             (if gnus-newsgroup-auto-expire
4123                 (push number gnus-newsgroup-expirable)
4124               (push (cons number gnus-low-score-mark)
4125                     gnus-newsgroup-reads))))
4126
4127           (when gnus-tmp-header
4128             ;; We may have an old dummy line to output before this
4129             ;; article.
4130             (when (and gnus-tmp-dummy-line
4131                        (gnus-subject-equal
4132                         gnus-tmp-dummy-line
4133                         (mail-header-subject gnus-tmp-header)))
4134               (gnus-summary-insert-dummy-line
4135                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4136               (setq gnus-tmp-dummy-line nil))
4137
4138             ;; Compute the mark.
4139             (setq gnus-tmp-unread (gnus-article-mark number))
4140
4141             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4142                                   gnus-tmp-header gnus-tmp-level)
4143                   gnus-newsgroup-data)
4144
4145             ;; Actually insert the line.
4146             (setq
4147              gnus-tmp-subject-or-nil
4148              (cond
4149               ((and gnus-thread-ignore-subject
4150                     gnus-tmp-prev-subject
4151                     (not (inline (gnus-subject-equal
4152                                   gnus-tmp-prev-subject subject))))
4153                subject)
4154               ((zerop gnus-tmp-level)
4155                (if (and (eq gnus-summary-make-false-root 'empty)
4156                         (memq number gnus-tmp-gathered)
4157                         gnus-tmp-prev-subject
4158                         (inline (gnus-subject-equal
4159                                  gnus-tmp-prev-subject subject)))
4160                    gnus-summary-same-subject
4161                  subject))
4162               (t gnus-summary-same-subject)))
4163             (if (and (eq gnus-summary-make-false-root 'adopt)
4164                      (= gnus-tmp-level 1)
4165                      (memq number gnus-tmp-gathered))
4166                 (setq gnus-tmp-opening-bracket ?\<
4167                       gnus-tmp-closing-bracket ?\>)
4168               (setq gnus-tmp-opening-bracket ?\[
4169                     gnus-tmp-closing-bracket ?\]))
4170             (setq
4171              gnus-tmp-indentation
4172              (aref gnus-thread-indent-array gnus-tmp-level)
4173              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4174              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4175                                 gnus-summary-default-score 0)
4176              gnus-tmp-score-char
4177              (if (or (null gnus-summary-default-score)
4178                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4179                          gnus-summary-zcore-fuzz))
4180                  ?  ;Whitespace
4181                (if (< gnus-tmp-score gnus-summary-default-score)
4182                    gnus-score-below-mark gnus-score-over-mark))
4183              gnus-tmp-replied
4184              (cond ((memq number gnus-newsgroup-processable)
4185                     gnus-process-mark)
4186                    ((memq number gnus-newsgroup-cached)
4187                     gnus-cached-mark)
4188                    ((memq number gnus-newsgroup-replied)
4189                     gnus-replied-mark)
4190                    ((memq number gnus-newsgroup-saved)
4191                     gnus-saved-mark)
4192                    (t gnus-unread-mark))
4193              gnus-tmp-from (mail-header-from gnus-tmp-header)
4194              gnus-tmp-name
4195              (cond
4196               ((string-match "<[^>]+> *$" gnus-tmp-from)
4197                (setq beg-match (match-beginning 0))
4198                (or (and (string-match "^\".+\"" gnus-tmp-from)
4199                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4200                    (substring gnus-tmp-from 0 beg-match)))
4201               ((string-match "(.+)" gnus-tmp-from)
4202                (substring gnus-tmp-from
4203                           (1+ (match-beginning 0)) (1- (match-end 0))))
4204               (t gnus-tmp-from)))
4205             (when (string= gnus-tmp-name "")
4206               (setq gnus-tmp-name gnus-tmp-from))
4207             (unless (numberp gnus-tmp-lines)
4208               (setq gnus-tmp-lines 0))
4209             (gnus-put-text-property
4210              (point)
4211              (progn (eval gnus-summary-line-format-spec) (point))
4212              'gnus-number number)
4213             (when gnus-visual-p
4214               (forward-line -1)
4215               (gnus-run-hooks 'gnus-summary-update-hook)
4216               (forward-line 1))
4217
4218             (setq gnus-tmp-prev-subject subject)))
4219
4220         (when (nth 1 thread)
4221           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4222         (incf gnus-tmp-level)
4223         (setq threads (if thread-end nil (cdar thread)))
4224         (unless threads
4225           (setq gnus-tmp-level 0)))))
4226   (gnus-message 7 "Generating summary...done"))
4227
4228 (defun gnus-summary-prepare-unthreaded (headers)
4229   "Generate an unthreaded summary buffer based on HEADERS."
4230   (let (header number mark)
4231
4232     (beginning-of-line)
4233
4234     (while headers
4235       ;; We may have to root out some bad articles...
4236       (when (memq (setq number (mail-header-number
4237                                 (setq header (pop headers))))
4238                   gnus-newsgroup-limit)
4239         ;; Mark article as read when it has a low score.
4240         (when (and gnus-summary-mark-below
4241                    (< (or (cdr (assq number gnus-newsgroup-scored))
4242                           gnus-summary-default-score 0)
4243                       gnus-summary-mark-below)
4244                    (not (gnus-summary-article-ancient-p number)))
4245           (setq gnus-newsgroup-unreads
4246                 (delq number gnus-newsgroup-unreads))
4247           (if gnus-newsgroup-auto-expire
4248               (push number gnus-newsgroup-expirable)
4249             (push (cons number gnus-low-score-mark)
4250                   gnus-newsgroup-reads)))
4251
4252         (setq mark (gnus-article-mark number))
4253         (push (gnus-data-make number mark (1+ (point)) header 0)
4254               gnus-newsgroup-data)
4255         (gnus-summary-insert-line
4256          header 0 number
4257          mark (memq number gnus-newsgroup-replied)
4258          (memq number gnus-newsgroup-expirable)
4259          (mail-header-subject header) nil
4260          (cdr (assq number gnus-newsgroup-scored))
4261          (memq number gnus-newsgroup-processable))))))
4262
4263 (defun gnus-summary-remove-list-identifiers ()
4264   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4265   (let ((regexp (if (stringp gnus-list-identifiers)
4266                     gnus-list-identifiers
4267                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4268     (dolist (header gnus-newsgroup-headers)
4269       (when (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp
4270                                   " *\\)\\)+\\(Re: +\\)?\\)")
4271                           (mail-header-subject header))
4272         (mail-header-set-subject
4273          header (concat (substring (mail-header-subject header)
4274                                    0 (match-beginning 1))
4275                         (or
4276                          (match-string 3 (mail-header-subject header))
4277                          (match-string 5 (mail-header-subject header)))
4278                         (substring (mail-header-subject header)
4279                                    (match-end 1))))))))
4280
4281 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4282   "Select newsgroup GROUP.
4283 If READ-ALL is non-nil, all articles in the group are selected.
4284 If SELECT-ARTICLES, only select those articles from GROUP."
4285   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4286          ;;!!! Dirty hack; should be removed.
4287          (gnus-summary-ignore-duplicates
4288           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4289               t
4290             gnus-summary-ignore-duplicates))
4291          (info (nth 2 entry))
4292          articles fetched-articles cached)
4293
4294     (unless (gnus-check-server
4295              (setq gnus-current-select-method
4296                    (gnus-find-method-for-group group)))
4297       (error "Couldn't open server"))
4298
4299     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4300         (gnus-activate-group group)     ; Or we can activate it...
4301         (progn                          ; Or we bug out.
4302           (when (equal major-mode 'gnus-summary-mode)
4303             (kill-buffer (current-buffer)))
4304           (error "Couldn't activate group %s: %s"
4305                  group (gnus-status-message group))))
4306
4307     (unless (gnus-request-group group t)
4308       (when (equal major-mode 'gnus-summary-mode)
4309         (kill-buffer (current-buffer)))
4310       (error "Couldn't request group %s: %s"
4311              group (gnus-status-message group)))
4312
4313     (setq gnus-newsgroup-name group)
4314     (setq gnus-newsgroup-unselected nil)
4315     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4316     (gnus-summary-setup-default-charset)
4317
4318     ;; Adjust and set lists of article marks.
4319     (when info
4320       (gnus-adjust-marked-articles info))
4321
4322     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4323     (setq cached
4324           (if (gnus-virtual-group-p group)
4325               gnus-newsgroup-cached
4326             (gnus-cache-articles-in-group group)))
4327
4328     (setq gnus-newsgroup-unreads
4329           (gnus-set-difference
4330            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4331            gnus-newsgroup-dormant))
4332
4333     (setq gnus-newsgroup-processable nil)
4334
4335     (gnus-update-read-articles group gnus-newsgroup-unreads)
4336
4337     (if (setq articles select-articles)
4338         (setq gnus-newsgroup-unselected
4339               (gnus-sorted-intersection
4340                gnus-newsgroup-unreads
4341                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4342       (setq articles (gnus-articles-to-read group read-all)))
4343
4344     (cond
4345      ((null articles)
4346       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4347       'quit)
4348      ((eq articles 0) nil)
4349      (t
4350       ;; Init the dependencies hash table.
4351       (setq gnus-newsgroup-dependencies
4352             (gnus-make-hashtable (length articles)))
4353       (gnus-set-global-variables)
4354       ;; Retrieve the headers and read them in.
4355       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4356       (setq gnus-newsgroup-headers
4357             (gnus-retrieve-parsed-headers
4358              articles gnus-newsgroup-name
4359              ;; We might want to fetch old headers, but
4360              ;; not if there is only 1 article.
4361              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4362                            (not (numberp gnus-fetch-old-headers)))
4363                       (> (length articles) 1))
4364                   gnus-fetch-old-headers)))
4365       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4366
4367       ;; Suppress duplicates?
4368       (when gnus-suppress-duplicates
4369         (gnus-dup-suppress-articles))
4370
4371       ;; Set the initial limit.
4372       (setq gnus-newsgroup-limit (copy-sequence articles))
4373       ;; Remove canceled articles from the list of unread articles.
4374       (setq gnus-newsgroup-unreads
4375             (gnus-set-sorted-intersection
4376              gnus-newsgroup-unreads
4377              (setq fetched-articles
4378                    (mapcar (lambda (headers) (mail-header-number headers))
4379                            gnus-newsgroup-headers))))
4380       ;; Removed marked articles that do not exist.
4381       (gnus-update-missing-marks
4382        (gnus-sorted-complement fetched-articles articles))
4383
4384       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4385       (when cached
4386         (setq gnus-newsgroup-cached cached))
4387
4388       ;; We might want to build some more threads first.
4389       (when (and gnus-fetch-old-headers
4390                  (eq gnus-headers-retrieved-by 'nov))
4391         (if (eq gnus-fetch-old-headers 'invisible)
4392             (gnus-build-all-threads)
4393           (gnus-build-old-threads)))
4394       ;; Let the Gnus agent mark articles as read.
4395       (when gnus-agent
4396         (gnus-agent-get-undownloaded-list))
4397       ;; Remove list identifiers from subject
4398       (when gnus-list-identifiers
4399         (gnus-summary-remove-list-identifiers))
4400       ;; Check whether auto-expire is to be done in this group.
4401       (setq gnus-newsgroup-auto-expire
4402             (gnus-group-auto-expirable-p group))
4403       ;; Set up the article buffer now, if necessary.
4404       (unless gnus-single-article-buffer
4405         (gnus-article-setup-buffer))
4406       ;; First and last article in this newsgroup.
4407       (when gnus-newsgroup-headers
4408         (setq gnus-newsgroup-begin
4409               (mail-header-number (car gnus-newsgroup-headers))
4410               gnus-newsgroup-end
4411               (mail-header-number
4412                (gnus-last-element gnus-newsgroup-headers))))
4413       ;; GROUP is successfully selected.
4414       (or gnus-newsgroup-headers t)))))
4415
4416 (defun gnus-articles-to-read (group &optional read-all)
4417   "Find out what articles the user wants to read."
4418   (let* ((articles
4419           ;; Select all articles if `read-all' is non-nil, or if there
4420           ;; are no unread articles.
4421           (if (or read-all
4422                   (and (zerop (length gnus-newsgroup-marked))
4423                        (zerop (length gnus-newsgroup-unreads)))
4424                   (eq (gnus-group-find-parameter group 'display)
4425                       'all))
4426               (or
4427                (gnus-uncompress-range (gnus-active group))
4428                (gnus-cache-articles-in-group group))
4429             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4430                           (copy-sequence gnus-newsgroup-unreads))
4431                   '<)))
4432          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4433          (scored (length scored-list))
4434          (number (length articles))
4435          (marked (+ (length gnus-newsgroup-marked)
4436                     (length gnus-newsgroup-dormant)))
4437          (select
4438           (cond
4439            ((numberp read-all)
4440             read-all)
4441            (t
4442             (condition-case ()
4443                 (cond
4444                  ((and (or (<= scored marked) (= scored number))
4445                        (natnump gnus-large-newsgroup)
4446                        (> number gnus-large-newsgroup))
4447                   (let* ((cursor-in-echo-area nil)
4448                          (input (read-from-minibuffer
4449                                  (format
4450                                   "How many articles from %s (max %d): "
4451                                   (gnus-limit-string gnus-newsgroup-name 35)
4452                                   number)
4453                                  (cons (number-to-string gnus-large-newsgroup)
4454                                        0))))
4455                     (if (string-match "^[ \t]*$" input)
4456                         number
4457                       input)))
4458                  ((and (> scored marked) (< scored number)
4459                        (> (- scored number) 20))
4460                   (let ((input
4461                          (read-string
4462                           (format "%s %s (%d scored, %d total): "
4463                                   "How many articles from"
4464                                   group scored number))))
4465                     (if (string-match "^[ \t]*$" input)
4466                         number input)))
4467                  (t number))
4468               (quit
4469                (message "Quit getting the articles to read")
4470                nil))))))
4471     (setq select (if (stringp select) (string-to-number select) select))
4472     (if (or (null select) (zerop select))
4473         select
4474       (if (and (not (zerop scored)) (<= (abs select) scored))
4475           (progn
4476             (setq articles (sort scored-list '<))
4477             (setq number (length articles)))
4478         (setq articles (copy-sequence articles)))
4479
4480       (when (< (abs select) number)
4481         (if (< select 0)
4482             ;; Select the N oldest articles.
4483             (setcdr (nthcdr (1- (abs select)) articles) nil)
4484           ;; Select the N most recent articles.
4485           (setq articles (nthcdr (- number select) articles))))
4486       (setq gnus-newsgroup-unselected
4487             (gnus-sorted-intersection
4488              gnus-newsgroup-unreads
4489              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4490       (when gnus-alter-articles-to-read-function
4491         (setq gnus-newsgroup-unreads
4492               (sort
4493                (funcall gnus-alter-articles-to-read-function
4494                         gnus-newsgroup-name gnus-newsgroup-unreads)
4495                '<)))
4496       articles)))
4497
4498 (defun gnus-killed-articles (killed articles)
4499   (let (out)
4500     (while articles
4501       (when (inline (gnus-member-of-range (car articles) killed))
4502         (push (car articles) out))
4503       (setq articles (cdr articles)))
4504     out))
4505
4506 (defun gnus-uncompress-marks (marks)
4507   "Uncompress the mark ranges in MARKS."
4508   (let ((uncompressed '(score bookmark))
4509         out)
4510     (while marks
4511       (if (memq (caar marks) uncompressed)
4512           (push (car marks) out)
4513         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4514       (setq marks (cdr marks)))
4515     out))
4516
4517 (defun gnus-adjust-marked-articles (info)
4518   "Set all article lists and remove all marks that are no longer valid."
4519   (let* ((marked-lists (gnus-info-marks info))
4520          (active (gnus-active (gnus-info-group info)))
4521          (min (car active))
4522          (max (cdr active))
4523          (types gnus-article-mark-lists)
4524          (uncompressed '(score bookmark killed))
4525          marks var articles article mark)
4526
4527     (while marked-lists
4528       (setq marks (pop marked-lists))
4529       (set (setq var (intern (format "gnus-newsgroup-%s"
4530                                      (car (rassq (setq mark (car marks))
4531                                                  types)))))
4532            (if (memq (car marks) uncompressed) (cdr marks)
4533              (gnus-uncompress-range (cdr marks))))
4534
4535       (setq articles (symbol-value var))
4536
4537       ;; All articles have to be subsets of the active articles.
4538       (cond
4539        ;; Adjust "simple" lists.
4540        ((memq mark '(tick dormant expire reply save))
4541         (while articles
4542           (when (or (< (setq article (pop articles)) min) (> article max))
4543             (set var (delq article (symbol-value var))))))
4544        ;; Adjust assocs.
4545        ((memq mark uncompressed)
4546         (when (not (listp (cdr (symbol-value var))))
4547           (set var (list (symbol-value var))))
4548         (when (not (listp (cdr articles)))
4549           (setq articles (list articles)))
4550         (while articles
4551           (when (or (not (consp (setq article (pop articles))))
4552                     (< (car article) min)
4553                     (> (car article) max))
4554             (set var (delq article (symbol-value var))))))))))
4555
4556 (defun gnus-update-missing-marks (missing)
4557   "Go through the list of MISSING articles and remove them from the mark lists."
4558   (when missing
4559     (let ((types gnus-article-mark-lists)
4560           var m)
4561       ;; Go through all types.
4562       (while types
4563         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4564         (when (symbol-value var)
4565           ;; This list has articles.  So we delete all missing articles
4566           ;; from it.
4567           (setq m missing)
4568           (while m
4569             (set var (delq (pop m) (symbol-value var)))))))))
4570
4571 (defun gnus-update-marks ()
4572   "Enter the various lists of marked articles into the newsgroup info list."
4573   (let ((types gnus-article-mark-lists)
4574         (info (gnus-get-info gnus-newsgroup-name))
4575         (uncompressed '(score bookmark killed))
4576         type list newmarked symbol delta-marks)
4577     (when info
4578       ;; Add all marks lists to the list of marks lists.
4579       (while (setq type (pop types))
4580         (setq list (symbol-value
4581                     (setq symbol
4582                           (intern (format "gnus-newsgroup-%s"
4583                                           (car type))))))
4584
4585         (when list
4586           ;; Get rid of the entries of the articles that have the
4587           ;; default score.
4588           (when (and (eq (cdr type) 'score)
4589                      gnus-save-score
4590                      list)
4591             (let* ((arts list)
4592                    (prev (cons nil list))
4593                    (all prev))
4594               (while arts
4595                 (if (or (not (consp (car arts)))
4596                         (= (cdar arts) gnus-summary-default-score))
4597                     (setcdr prev (cdr arts))
4598                   (setq prev arts))
4599                 (setq arts (cdr arts)))
4600               (setq list (cdr all)))))
4601
4602         (unless (memq (cdr type) uncompressed)
4603           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4604
4605         (when (gnus-check-backend-function
4606                'request-set-mark gnus-newsgroup-name)
4607           ;; propagate flags to server, with the following exceptions:
4608           ;; uncompressed:s are not proper flags (they are cons cells)
4609           ;; cache is a internal gnus flag
4610           ;; download are local to one gnus installation (well)
4611           ;; unsend are for nndraft groups only
4612           ;; xxx: generality of this?  this suits nnimap anyway
4613           (unless (memq (cdr type) (append '(cache download unsend)
4614                                            uncompressed))
4615             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4616                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4617                    (add (gnus-remove-from-range
4618                          (gnus-copy-sequence list) old)))
4619               (when add
4620                 (push (list add 'add (list (cdr type))) delta-marks))
4621               (when del
4622                 (push (list del 'del (list (cdr type))) delta-marks)))))
4623
4624         (when list
4625           (push (cons (cdr type) list) newmarked)))
4626
4627       (when delta-marks
4628         (unless (gnus-check-group gnus-newsgroup-name)
4629           (error "Can't open server for %s" gnus-newsgroup-name))
4630         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4631
4632       ;; Enter these new marks into the info of the group.
4633       (if (nthcdr 3 info)
4634           (setcar (nthcdr 3 info) newmarked)
4635         ;; Add the marks lists to the end of the info.
4636         (when newmarked
4637           (setcdr (nthcdr 2 info) (list newmarked))))
4638
4639       ;; Cut off the end of the info if there's nothing else there.
4640       (let ((i 5))
4641         (while (and (> i 2)
4642                     (not (nth i info)))
4643           (when (nthcdr (decf i) info)
4644             (setcdr (nthcdr i info) nil)))))))
4645
4646 (defun gnus-set-mode-line (where)
4647   "Set the mode line of the article or summary buffers.
4648 If WHERE is `summary', the summary mode line format will be used."
4649   ;; Is this mode line one we keep updated?
4650   (when (and (memq where gnus-updated-mode-lines)
4651              (symbol-value
4652               (intern (format "gnus-%s-mode-line-format-spec" where))))
4653     (let (mode-string)
4654       (save-excursion
4655         ;; We evaluate this in the summary buffer since these
4656         ;; variables are buffer-local to that buffer.
4657         (set-buffer gnus-summary-buffer)
4658         ;; We bind all these variables that are used in the `eval' form
4659         ;; below.
4660         (let* ((mformat (symbol-value
4661                          (intern
4662                           (format "gnus-%s-mode-line-format-spec" where))))
4663                (gnus-tmp-group-name (gnus-group-name-decode
4664                                      gnus-newsgroup-name
4665                                      (gnus-group-name-charset
4666                                       nil
4667                                       gnus-newsgroup-name)))
4668                (gnus-tmp-article-number (or gnus-current-article 0))
4669                (gnus-tmp-unread gnus-newsgroup-unreads)
4670                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4671                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4672                (gnus-tmp-unread-and-unselected
4673                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4674                             (zerop gnus-tmp-unselected))
4675                        "")
4676                       ((zerop gnus-tmp-unselected)
4677                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4678                       (t (format "{%d(+%d) more}"
4679                                  gnus-tmp-unread-and-unticked
4680                                  gnus-tmp-unselected))))
4681                (gnus-tmp-subject
4682                 (if (and gnus-current-headers
4683                          (vectorp gnus-current-headers))
4684                     (gnus-mode-string-quote
4685                      (mail-header-subject gnus-current-headers))
4686                   ""))
4687                bufname-length max-len
4688                gnus-tmp-header);; passed as argument to any user-format-funcs
4689           (setq mode-string (eval mformat))
4690           (setq bufname-length (if (string-match "%b" mode-string)
4691                                    (- (length
4692                                        (buffer-name
4693                                         (if (eq where 'summary)
4694                                             nil
4695                                           (get-buffer gnus-article-buffer))))
4696                                       2)
4697                                  0))
4698           (setq max-len (max 4 (if gnus-mode-non-string-length
4699                                    (- (window-width)
4700                                       gnus-mode-non-string-length
4701                                       bufname-length)
4702                                  (length mode-string))))
4703           ;; We might have to chop a bit of the string off...
4704           (when (> (length mode-string) max-len)
4705             (setq mode-string
4706                   (concat (gnus-truncate-string mode-string (- max-len 3))
4707                           "...")))
4708           ;; Pad the mode string a bit.
4709           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4710       ;; Update the mode line.
4711       (setq mode-line-buffer-identification
4712             (gnus-mode-line-buffer-identification (list mode-string)))
4713       (set-buffer-modified-p t))))
4714
4715 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4716   "Go through the HEADERS list and add all Xrefs to a hash table.
4717 The resulting hash table is returned, or nil if no Xrefs were found."
4718   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4719          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4720          (xref-hashtb (gnus-make-hashtable))
4721          start group entry number xrefs header)
4722     (while headers
4723       (setq header (pop headers))
4724       (when (and (setq xrefs (mail-header-xref header))
4725                  (not (memq (setq number (mail-header-number header))
4726                             unreads)))
4727         (setq start 0)
4728         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4729           (setq start (match-end 0))
4730           (setq group (if prefix
4731                           (concat prefix (substring xrefs (match-beginning 1)
4732                                                     (match-end 1)))
4733                         (substring xrefs (match-beginning 1) (match-end 1))))
4734           (setq number
4735                 (string-to-int (substring xrefs (match-beginning 2)
4736                                           (match-end 2))))
4737           (if (setq entry (gnus-gethash group xref-hashtb))
4738               (setcdr entry (cons number (cdr entry)))
4739             (gnus-sethash group (cons number nil) xref-hashtb)))))
4740     (and start xref-hashtb)))
4741
4742 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4743   "Look through all the headers and mark the Xrefs as read."
4744   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4745         name entry info xref-hashtb idlist method nth4)
4746     (save-excursion
4747       (set-buffer gnus-group-buffer)
4748       (when (setq xref-hashtb
4749                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4750         (mapatoms
4751          (lambda (group)
4752            (unless (string= from-newsgroup (setq name (symbol-name group)))
4753              (setq idlist (symbol-value group))
4754              ;; Dead groups are not updated.
4755              (and (prog1
4756                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4757                             info (nth 2 entry))
4758                     (when (stringp (setq nth4 (gnus-info-method info)))
4759                       (setq nth4 (gnus-server-to-method nth4))))
4760                   ;; Only do the xrefs if the group has the same
4761                   ;; select method as the group we have just read.
4762                   (or (gnus-methods-equal-p
4763                        nth4 (gnus-find-method-for-group from-newsgroup))
4764                       virtual
4765                       (equal nth4 (setq method (gnus-find-method-for-group
4766                                                 from-newsgroup)))
4767                       (and (equal (car nth4) (car method))
4768                            (equal (nth 1 nth4) (nth 1 method))))
4769                   gnus-use-cross-reference
4770                   (or (not (eq gnus-use-cross-reference t))
4771                       virtual
4772                       ;; Only do cross-references on subscribed
4773                       ;; groups, if that is what is wanted.
4774                       (<= (gnus-info-level info) gnus-level-subscribed))
4775                   (gnus-group-make-articles-read name idlist))))
4776          xref-hashtb)))))
4777
4778 (defun gnus-compute-read-articles (group articles)
4779   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4780          (info (nth 2 entry))
4781          (active (gnus-active group))
4782          ninfo)
4783     (when entry
4784       ;; First peel off all invalid article numbers.
4785       (when active
4786         (let ((ids articles)
4787               id first)
4788           (while (setq id (pop ids))
4789             (when (and first (> id (cdr active)))
4790               ;; We'll end up in this situation in one particular
4791               ;; obscure situation.  If you re-scan a group and get
4792               ;; a new article that is cross-posted to a different
4793               ;; group that has not been re-scanned, you might get
4794               ;; crossposted article that has a higher number than
4795               ;; Gnus believes possible.  So we re-activate this
4796               ;; group as well.  This might mean doing the
4797               ;; crossposting thingy will *increase* the number
4798               ;; of articles in some groups.  Tsk, tsk.
4799               (setq active (or (gnus-activate-group group) active)))
4800             (when (or (> id (cdr active))
4801                       (< id (car active)))
4802               (setq articles (delq id articles))))))
4803       ;; If the read list is nil, we init it.
4804       (if (and active
4805                (null (gnus-info-read info))
4806                (> (car active) 1))
4807           (setq ninfo (cons 1 (1- (car active))))
4808         (setq ninfo (gnus-info-read info)))
4809       ;; Then we add the read articles to the range.
4810       (gnus-add-to-range
4811        ninfo (setq articles (sort articles '<))))))
4812
4813 (defun gnus-group-make-articles-read (group articles)
4814   "Update the info of GROUP to say that ARTICLES are read."
4815   (let* ((num 0)
4816          (entry (gnus-gethash group gnus-newsrc-hashtb))
4817          (info (nth 2 entry))
4818          (active (gnus-active group))
4819          range)
4820     (when entry
4821       (setq range (gnus-compute-read-articles group articles))
4822       (save-excursion
4823         (set-buffer gnus-group-buffer)
4824         (gnus-undo-register
4825           `(progn
4826              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4827              (gnus-info-set-read ',info ',(gnus-info-read info))
4828              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4829              (gnus-group-update-group ,group t))))
4830       ;; Add the read articles to the range.
4831       (gnus-info-set-read info range)
4832       ;; Then we have to re-compute how many unread
4833       ;; articles there are in this group.
4834       (when active
4835         (cond
4836          ((not range)
4837           (setq num (- (1+ (cdr active)) (car active))))
4838          ((not (listp (cdr range)))
4839           (setq num (- (cdr active) (- (1+ (cdr range))
4840                                        (car range)))))
4841          (t
4842           (while range
4843             (if (numberp (car range))
4844                 (setq num (1+ num))
4845               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4846             (setq range (cdr range)))
4847           (setq num (- (cdr active) num))))
4848         ;; Update the number of unread articles.
4849         (setcar entry num)
4850         ;; Update the group buffer.
4851         (gnus-group-update-group group t)))))
4852
4853 (defvar gnus-newsgroup-none-id 0)
4854
4855 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4856   (let ((cur nntp-server-buffer)
4857         (dependencies
4858          (or dependencies
4859              (save-excursion (set-buffer gnus-summary-buffer)
4860                              gnus-newsgroup-dependencies)))
4861         headers id end ref
4862         (mail-parse-charset gnus-newsgroup-charset)
4863         (mail-parse-ignored-charsets
4864          (save-excursion (condition-case nil
4865                              (set-buffer gnus-summary-buffer)
4866                            (error))
4867                          gnus-newsgroup-ignored-charsets)))
4868     (save-excursion
4869       (set-buffer nntp-server-buffer)
4870       ;; Translate all TAB characters into SPACE characters.
4871       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4872       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4873       (gnus-run-hooks 'gnus-parse-headers-hook)
4874       (let ((case-fold-search t)
4875             in-reply-to header p lines chars ctype)
4876         (goto-char (point-min))
4877         ;; Search to the beginning of the next header.  Error messages
4878         ;; do not begin with 2 or 3.
4879         (while (re-search-forward "^[23][0-9]+ " nil t)
4880           (setq id nil
4881                 ref nil)
4882           ;; This implementation of this function, with nine
4883           ;; search-forwards instead of the one re-search-forward and
4884           ;; a case (which basically was the old function) is actually
4885           ;; about twice as fast, even though it looks messier.  You
4886           ;; can't have everything, I guess.  Speed and elegance
4887           ;; doesn't always go hand in hand.
4888           (setq
4889            header
4890            (make-full-mail-header
4891             ;; Number.
4892             (prog1
4893                 (read cur)
4894               (end-of-line)
4895               (setq p (point))
4896               (narrow-to-region (point)
4897                                 (or (and (search-forward "\n.\n" nil t)
4898                                          (- (point) 2))
4899                                     (point))))
4900             ;; Subject.
4901             (progn
4902               (goto-char p)
4903               (if (search-forward "\nsubject: " nil t)
4904                   (nnheader-header-value)
4905                 "(none)"))
4906             ;; From.
4907             (progn
4908               (goto-char p)
4909               (if (or (search-forward "\nfrom: " nil t)
4910                       (search-forward "\nfrom:" nil t))
4911                   (nnheader-header-value)
4912                 "(nobody)"))
4913             ;; Date.
4914             (progn
4915               (goto-char p)
4916               (if (search-forward "\ndate: " nil t)
4917                   (nnheader-header-value)
4918                 ""))
4919             ;; Message-ID.
4920             (progn
4921               (goto-char p)
4922               (setq id (if (re-search-forward
4923                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4924                            ;; We do it this way to make sure the Message-ID
4925                            ;; is (somewhat) syntactically valid.
4926                            (buffer-substring (match-beginning 1)
4927                                              (match-end 1))
4928                          ;; If there was no message-id, we just fake one
4929                          ;; to make subsequent routines simpler.
4930                          (nnheader-generate-fake-message-id))))
4931             ;; References.
4932             (progn
4933               (goto-char p)
4934               (if (search-forward "\nreferences: " nil t)
4935                   (progn
4936                     (setq end (point))
4937                     (prog1
4938                         (nnheader-header-value)
4939                       (setq ref
4940                             (buffer-substring
4941                              (progn
4942                                ;; (end-of-line)
4943                                (search-backward ">" end t)
4944                                (1+ (point)))
4945                              (progn
4946                                (search-backward "<" end t)
4947                                (point))))))
4948                 ;; Get the references from the in-reply-to header if there
4949                 ;; were no references and the in-reply-to header looks
4950                 ;; promising.
4951                 (if (and (search-forward "\nin-reply-to: " nil t)
4952                          (setq in-reply-to (nnheader-header-value))
4953                          (string-match "<[^>]+>" in-reply-to))
4954                     (let (ref2)
4955                       (setq ref (substring in-reply-to (match-beginning 0)
4956                                            (match-end 0)))
4957                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4958                         (setq ref2 (substring in-reply-to (match-beginning 0)
4959                                               (match-end 0)))
4960                         (when (> (length ref2) (length ref))
4961                           (setq ref ref2)))
4962                       ref)
4963                   (setq ref nil))))
4964             ;; Chars.
4965             (progn
4966               (goto-char p)
4967               (if (search-forward "\nchars: " nil t)
4968                   (if (numberp (setq chars (ignore-errors (read cur))))
4969                       chars 0)
4970                 0))
4971             ;; Lines.
4972             (progn
4973               (goto-char p)
4974               (if (search-forward "\nlines: " nil t)
4975                   (if (numberp (setq lines (ignore-errors (read cur))))
4976                       lines 0)
4977                 0))
4978             ;; Xref.
4979             (progn
4980               (goto-char p)
4981               (and (search-forward "\nxref: " nil t)
4982                    (nnheader-header-value)))
4983             ;; Extra.
4984             (when gnus-extra-headers
4985               (let ((extra gnus-extra-headers)
4986                     out)
4987                 (while extra
4988                   (goto-char p)
4989                   (when (search-forward
4990                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4991                     (push (cons (car extra) (nnheader-header-value)) out))
4992                   (pop extra))
4993                 out))))
4994           (goto-char p)
4995           (if (and (search-forward "\ncontent-type: " nil t)
4996                    (setq ctype (nnheader-header-value)))
4997               (mime-entity-set-content-type-internal
4998                header (mime-parse-Content-Type ctype)))
4999           (when (equal id ref)
5000             (setq ref nil))
5001
5002           (when gnus-alter-header-function
5003             (funcall gnus-alter-header-function header)
5004             (setq id (mail-header-id header)
5005                   ref (gnus-parent-id (mail-header-references header))))
5006
5007           (when (setq header
5008                       (gnus-dependencies-add-header
5009                        header dependencies force-new))
5010             (push header headers))
5011           (goto-char (point-max))
5012           (widen))
5013         (nreverse headers)))))
5014
5015 ;; Goes through the xover lines and returns a list of vectors
5016 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5017                                                   force-new dependencies
5018                                                   group also-fetch-heads)
5019   "Parse the news overview data in the server buffer.
5020 Return a list of headers that match SEQUENCE (see
5021 `nntp-retrieve-headers')."
5022   ;; Get the Xref when the users reads the articles since most/some
5023   ;; NNTP servers do not include Xrefs when using XOVER.
5024   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5025   (let ((mail-parse-charset gnus-newsgroup-charset)
5026         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5027         (cur nntp-server-buffer)
5028         (dependencies (or dependencies gnus-newsgroup-dependencies))
5029         number headers header)
5030     (save-excursion
5031       (set-buffer nntp-server-buffer)
5032       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5033       ;; Allow the user to mangle the headers before parsing them.
5034       (gnus-run-hooks 'gnus-parse-headers-hook)
5035       (goto-char (point-min))
5036       (while (not (eobp))
5037         (condition-case ()
5038             (while (and sequence (not (eobp)))
5039               (setq number (read cur))
5040               (while (and sequence
5041                           (< (car sequence) number))
5042                 (setq sequence (cdr sequence)))
5043               (and sequence
5044                    (eq number (car sequence))
5045                    (progn
5046                      (setq sequence (cdr sequence))
5047                      (setq header (inline
5048                                     (gnus-nov-parse-line
5049                                      number dependencies force-new))))
5050                    (push header headers))
5051               (forward-line 1))
5052           (error
5053            (gnus-error 4 "Strange nov line (%d)"
5054                        (count-lines (point-min) (point)))))
5055         (forward-line 1))
5056       ;; A common bug in inn is that if you have posted an article and
5057       ;; then retrieves the active file, it will answer correctly --
5058       ;; the new article is included.  However, a NOV entry for the
5059       ;; article may not have been generated yet, so this may fail.
5060       ;; We work around this problem by retrieving the last few
5061       ;; headers using HEAD.
5062       (if (or (not also-fetch-heads)
5063               (not sequence))
5064           ;; We (probably) got all the headers.
5065           (nreverse headers)
5066         (let ((gnus-nov-is-evil t))
5067           (nconc
5068            (nreverse headers)
5069            (gnus-retrieve-parsed-headers sequence group)
5070            ))))))
5071
5072 (defun gnus-article-get-xrefs ()
5073   "Fill in the Xref value in `gnus-current-headers', if necessary.
5074 This is meant to be called in `gnus-article-internal-prepare-hook'."
5075   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5076                                  gnus-current-headers)))
5077     (or (not gnus-use-cross-reference)
5078         (not headers)
5079         (and (mail-header-xref headers)
5080              (not (string= (mail-header-xref headers) "")))
5081         (let ((case-fold-search t)
5082               xref)
5083           (save-restriction
5084             (nnheader-narrow-to-headers)
5085             (goto-char (point-min))
5086             (when (or (and (not (eobp))
5087                            (eq (downcase (char-after)) ?x)
5088                            (looking-at "Xref:"))
5089                       (search-forward "\nXref:" nil t))
5090               (goto-char (1+ (match-end 0)))
5091               (setq xref (buffer-substring (point)
5092                                            (progn (end-of-line) (point))))
5093               (mail-header-set-xref headers xref)))))))
5094
5095 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5096   "Find article ID and insert the summary line for that article.
5097 OLD-HEADER can either be a header or a line number to insert
5098 the subject line on."
5099   (let* ((line (and (numberp old-header) old-header))
5100          (old-header (and (vectorp old-header) old-header))
5101          (header (cond ((and old-header use-old-header)
5102                         old-header)
5103                        ((and (numberp id)
5104                              (gnus-number-to-header id))
5105                         (gnus-number-to-header id))
5106                        (t
5107                         (gnus-read-header id))))
5108          (number (and (numberp id) id))
5109          d)
5110     (when header
5111       ;; Rebuild the thread that this article is part of and go to the
5112       ;; article we have fetched.
5113       (when (and (not gnus-show-threads)
5114                  old-header)
5115         (when (and number
5116                    (setq d (gnus-data-find (mail-header-number old-header))))
5117           (goto-char (gnus-data-pos d))
5118           (gnus-data-remove
5119            number
5120            (- (gnus-point-at-bol)
5121               (prog1
5122                   (1+ (gnus-point-at-eol))
5123                 (gnus-delete-line))))))
5124       (when old-header
5125         (mail-header-set-number header (mail-header-number old-header)))
5126       (setq gnus-newsgroup-sparse
5127             (delq (setq number (mail-header-number header))
5128                   gnus-newsgroup-sparse))
5129       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5130       (push number gnus-newsgroup-limit)
5131       (gnus-rebuild-thread (mail-header-id header) line)
5132       (gnus-summary-goto-subject number nil t))
5133     (when (and (numberp number)
5134                (> number 0))
5135       ;; We have to update the boundaries even if we can't fetch the
5136       ;; article if ID is a number -- so that the next `P' or `N'
5137       ;; command will fetch the previous (or next) article even
5138       ;; if the one we tried to fetch this time has been canceled.
5139       (when (> number gnus-newsgroup-end)
5140         (setq gnus-newsgroup-end number))
5141       (when (< number gnus-newsgroup-begin)
5142         (setq gnus-newsgroup-begin number))
5143       (setq gnus-newsgroup-unselected
5144             (delq number gnus-newsgroup-unselected)))
5145     ;; Report back a success?
5146     (and header (mail-header-number header))))
5147
5148 ;;; Process/prefix in the summary buffer
5149
5150 (defun gnus-summary-work-articles (n)
5151   "Return a list of articles to be worked upon.
5152 The prefix argument, the list of process marked articles, and the
5153 current article will be taken into consideration."
5154   (save-excursion
5155     (set-buffer gnus-summary-buffer)
5156     (cond
5157      (n
5158       ;; A numerical prefix has been given.
5159       (setq n (prefix-numeric-value n))
5160       (let ((backward (< n 0))
5161             (n (abs (prefix-numeric-value n)))
5162             articles article)
5163         (save-excursion
5164           (while
5165               (and (> n 0)
5166                    (push (setq article (gnus-summary-article-number))
5167                          articles)
5168                    (if backward
5169                        (gnus-summary-find-prev nil article)
5170                      (gnus-summary-find-next nil article)))
5171             (decf n)))
5172         (nreverse articles)))
5173      ((and (gnus-region-active-p) (mark))
5174       (message "region active")
5175       ;; Work on the region between point and mark.
5176       (let ((max (max (point) (mark)))
5177             articles article)
5178         (save-excursion
5179           (goto-char (min (point) (mark)))
5180           (while
5181               (and
5182                (push (setq article (gnus-summary-article-number)) articles)
5183                (gnus-summary-find-next nil article)
5184                (< (point) max)))
5185           (nreverse articles))))
5186      (gnus-newsgroup-processable
5187       ;; There are process-marked articles present.
5188       ;; Save current state.
5189       (gnus-summary-save-process-mark)
5190       ;; Return the list.
5191       (reverse gnus-newsgroup-processable))
5192      (t
5193       ;; Just return the current article.
5194       (list (gnus-summary-article-number))))))
5195
5196 (defmacro gnus-summary-iterate (arg &rest forms)
5197   "Iterate over the process/prefixed articles and do FORMS.
5198 ARG is the interactive prefix given to the command.  FORMS will be
5199 executed with point over the summary line of the articles."
5200   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5201     `(let ((,articles (gnus-summary-work-articles ,arg)))
5202        (while ,articles
5203          (gnus-summary-goto-subject (car ,articles))
5204          ,@forms
5205          (pop ,articles)))))
5206
5207 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5208 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5209
5210 (defun gnus-summary-save-process-mark ()
5211   "Push the current set of process marked articles on the stack."
5212   (interactive)
5213   (push (copy-sequence gnus-newsgroup-processable)
5214         gnus-newsgroup-process-stack))
5215
5216 (defun gnus-summary-kill-process-mark ()
5217   "Push the current set of process marked articles on the stack and unmark."
5218   (interactive)
5219   (gnus-summary-save-process-mark)
5220   (gnus-summary-unmark-all-processable))
5221
5222 (defun gnus-summary-yank-process-mark ()
5223   "Pop the last process mark state off the stack and restore it."
5224   (interactive)
5225   (unless gnus-newsgroup-process-stack
5226     (error "Empty mark stack"))
5227   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5228
5229 (defun gnus-summary-process-mark-set (set)
5230   "Make SET into the current process marked articles."
5231   (gnus-summary-unmark-all-processable)
5232   (while set
5233     (gnus-summary-set-process-mark (pop set))))
5234
5235 ;;; Searching and stuff
5236
5237 (defun gnus-summary-search-group (&optional backward use-level)
5238   "Search for next unread newsgroup.
5239 If optional argument BACKWARD is non-nil, search backward instead."
5240   (save-excursion
5241     (set-buffer gnus-group-buffer)
5242     (when (gnus-group-search-forward
5243            backward nil (if use-level (gnus-group-group-level) nil))
5244       (gnus-group-group-name))))
5245
5246 (defun gnus-summary-best-group (&optional exclude-group)
5247   "Find the name of the best unread group.
5248 If EXCLUDE-GROUP, do not go to this group."
5249   (save-excursion
5250     (set-buffer gnus-group-buffer)
5251     (save-excursion
5252       (gnus-group-best-unread-group exclude-group))))
5253
5254 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5255   (if backward (gnus-summary-find-prev)
5256     (let* ((dummy (gnus-summary-article-intangible-p))
5257            (article (or article (gnus-summary-article-number)))
5258            (arts (gnus-data-find-list article))
5259            result)
5260       (when (and (not dummy)
5261                  (or (not gnus-summary-check-current)
5262                      (not unread)
5263                      (not (gnus-data-unread-p (car arts)))))
5264         (setq arts (cdr arts)))
5265       (when (setq result
5266                   (if unread
5267                       (progn
5268                         (while arts
5269                           (when (or (and undownloaded
5270                                          (eq gnus-undownloaded-mark
5271                                              (gnus-data-mark (car arts))))
5272                                     (gnus-data-unread-p (car arts)))
5273                             (setq result (car arts)
5274                                   arts nil))
5275                           (setq arts (cdr arts)))
5276                         result)
5277                     (car arts)))
5278         (goto-char (gnus-data-pos result))
5279         (gnus-data-number result)))))
5280
5281 (defun gnus-summary-find-prev (&optional unread article)
5282   (let* ((eobp (eobp))
5283          (article (or article (gnus-summary-article-number)))
5284          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5285          result)
5286     (when (and (not eobp)
5287                (or (not gnus-summary-check-current)
5288                    (not unread)
5289                    (not (gnus-data-unread-p (car arts)))))
5290       (setq arts (cdr arts)))
5291     (when (setq result
5292                 (if unread
5293                     (progn
5294                       (while arts
5295                         (when (gnus-data-unread-p (car arts))
5296                           (setq result (car arts)
5297                                 arts nil))
5298                         (setq arts (cdr arts)))
5299                       result)
5300                   (car arts)))
5301       (goto-char (gnus-data-pos result))
5302       (gnus-data-number result))))
5303
5304 (defun gnus-summary-find-subject (subject &optional unread backward article)
5305   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5306          (article (or article (gnus-summary-article-number)))
5307          (articles (gnus-data-list backward))
5308          (arts (gnus-data-find-list article articles))
5309          result)
5310     (when (or (not gnus-summary-check-current)
5311               (not unread)
5312               (not (gnus-data-unread-p (car arts))))
5313       (setq arts (cdr arts)))
5314     (while arts
5315       (and (or (not unread)
5316                (gnus-data-unread-p (car arts)))
5317            (vectorp (gnus-data-header (car arts)))
5318            (gnus-subject-equal
5319             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5320            (setq result (car arts)
5321                  arts nil))
5322       (setq arts (cdr arts)))
5323     (and result
5324          (goto-char (gnus-data-pos result))
5325          (gnus-data-number result))))
5326
5327 (defun gnus-summary-search-forward (&optional unread subject backward)
5328   "Search forward for an article.
5329 If UNREAD, look for unread articles.  If SUBJECT, look for
5330 articles with that subject.  If BACKWARD, search backward instead."
5331   (cond (subject (gnus-summary-find-subject subject unread backward))
5332         (backward (gnus-summary-find-prev unread))
5333         (t (gnus-summary-find-next unread))))
5334
5335 (defun gnus-recenter (&optional n)
5336   "Center point in window and redisplay frame.
5337 Also do horizontal recentering."
5338   (interactive "P")
5339   (when (and gnus-auto-center-summary
5340              (not (eq gnus-auto-center-summary 'vertical)))
5341     (gnus-horizontal-recenter))
5342   (recenter n))
5343
5344 (defun gnus-summary-recenter ()
5345   "Center point in the summary window.
5346 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5347 displayed, no centering will be performed."
5348   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5349   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5350   (interactive)
5351   (let* ((top (cond ((< (window-height) 4) 0)
5352                     ((< (window-height) 7) 1)
5353                     (t (if (numberp gnus-auto-center-summary)
5354                            gnus-auto-center-summary
5355                          2))))
5356          (height (1- (window-height)))
5357          (bottom (save-excursion (goto-char (point-max))
5358                                  (forward-line (- height))
5359                                  (point)))
5360          (window (get-buffer-window (current-buffer))))
5361     ;; The user has to want it.
5362     (when gnus-auto-center-summary
5363       (when (get-buffer-window gnus-article-buffer)
5364         ;; Only do recentering when the article buffer is displayed,
5365         ;; Set the window start to either `bottom', which is the biggest
5366         ;; possible valid number, or the second line from the top,
5367         ;; whichever is the least.
5368         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5369           (if (> bottom top-pos)
5370               ;; Keep the second line from the top visible
5371               (set-window-start window top-pos t)
5372             ;; Try to keep the bottom line visible; if it's partially
5373             ;; obscured, either scroll one more line to make it fully
5374             ;; visible, or revert to using TOP-POS.
5375             (save-excursion
5376               (goto-char (point-max))
5377               (forward-line -1)
5378               (let ((last-line-start (point)))
5379                 (goto-char bottom)
5380                 (set-window-start window (point) t)
5381                 (when (not (pos-visible-in-window-p last-line-start window))
5382                   (forward-line 1)
5383                   (set-window-start window (min (point) top-pos) t)))))))
5384       ;; Do horizontal recentering while we're at it.
5385       (when (and (get-buffer-window (current-buffer) t)
5386                  (not (eq gnus-auto-center-summary 'vertical)))
5387         (let ((selected (selected-window)))
5388           (select-window (get-buffer-window (current-buffer) t))
5389           (gnus-summary-position-point)
5390           (gnus-horizontal-recenter)
5391           (select-window selected))))))
5392
5393 (defun gnus-summary-jump-to-group (newsgroup)
5394   "Move point to NEWSGROUP in group mode buffer."
5395   ;; Keep update point of group mode buffer if visible.
5396   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5397       (save-window-excursion
5398         ;; Take care of tree window mode.
5399         (when (get-buffer-window gnus-group-buffer)
5400           (pop-to-buffer gnus-group-buffer))
5401         (gnus-group-jump-to-group newsgroup))
5402     (save-excursion
5403       ;; Take care of tree window mode.
5404       (if (get-buffer-window gnus-group-buffer)
5405           (pop-to-buffer gnus-group-buffer)
5406         (set-buffer gnus-group-buffer))
5407       (gnus-group-jump-to-group newsgroup))))
5408
5409 ;; This function returns a list of article numbers based on the
5410 ;; difference between the ranges of read articles in this group and
5411 ;; the range of active articles.
5412 (defun gnus-list-of-unread-articles (group)
5413   (let* ((read (gnus-info-read (gnus-get-info group)))
5414          (active (or (gnus-active group) (gnus-activate-group group)))
5415          (last (cdr active))
5416          first nlast unread)
5417     ;; If none are read, then all are unread.
5418     (if (not read)
5419         (setq first (car active))
5420       ;; If the range of read articles is a single range, then the
5421       ;; first unread article is the article after the last read
5422       ;; article.  Sounds logical, doesn't it?
5423       (if (and (not (listp (cdr read)))
5424                (or (< (car read) (car active))
5425                    (progn (setq read (list read))
5426                           nil)))
5427           (setq first (max (car active) (1+ (cdr read))))
5428         ;; `read' is a list of ranges.
5429         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5430                                   (caar read)))
5431                   1)
5432           (setq first (car active)))
5433         (while read
5434           (when first
5435             (while (< first nlast)
5436               (push first unread)
5437               (setq first (1+ first))))
5438           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5439           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5440           (setq read (cdr read)))))
5441     ;; And add the last unread articles.
5442     (while (<= first last)
5443       (push first unread)
5444       (setq first (1+ first)))
5445     ;; Return the list of unread articles.
5446     (delq 0 (nreverse unread))))
5447
5448 (defun gnus-list-of-read-articles (group)
5449   "Return a list of unread, unticked and non-dormant articles."
5450   (let* ((info (gnus-get-info group))
5451          (marked (gnus-info-marks info))
5452          (active (gnus-active group)))
5453     (and info active
5454          (gnus-set-difference
5455           (gnus-sorted-complement
5456            (gnus-uncompress-range active)
5457            (gnus-list-of-unread-articles group))
5458           (append
5459            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5460            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5461
5462 ;; Various summary commands
5463
5464 (defun gnus-summary-select-article-buffer ()
5465   "Reconfigure windows to show article buffer."
5466   (interactive)
5467   (if (not (gnus-buffer-live-p gnus-article-buffer))
5468       (error "There is no article buffer for this summary buffer")
5469     (gnus-configure-windows 'article)
5470     (select-window (get-buffer-window gnus-article-buffer))))
5471
5472 (defun gnus-summary-universal-argument (arg)
5473   "Perform any operation on all articles that are process/prefixed."
5474   (interactive "P")
5475   (let ((articles (gnus-summary-work-articles arg))
5476         func article)
5477     (if (eq
5478          (setq
5479           func
5480           (key-binding
5481            (read-key-sequence
5482             (substitute-command-keys
5483              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5484          'undefined)
5485         (gnus-error 1 "Undefined key")
5486       (save-excursion
5487         (while articles
5488           (gnus-summary-goto-subject (setq article (pop articles)))
5489           (let (gnus-newsgroup-processable)
5490             (command-execute func))
5491           (gnus-summary-remove-process-mark article)))))
5492   (gnus-summary-position-point))
5493
5494 (defun gnus-summary-toggle-truncation (&optional arg)
5495   "Toggle truncation of summary lines.
5496 With arg, turn line truncation on iff arg is positive."
5497   (interactive "P")
5498   (setq truncate-lines
5499         (if (null arg) (not truncate-lines)
5500           (> (prefix-numeric-value arg) 0)))
5501   (redraw-display))
5502
5503 (defun gnus-summary-reselect-current-group (&optional all rescan)
5504   "Exit and then reselect the current newsgroup.
5505 The prefix argument ALL means to select all articles."
5506   (interactive "P")
5507   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5508     (error "Ephemeral groups can't be reselected"))
5509   (let ((current-subject (gnus-summary-article-number))
5510         (group gnus-newsgroup-name))
5511     (setq gnus-newsgroup-begin nil)
5512     (gnus-summary-exit)
5513     ;; We have to adjust the point of group mode buffer because
5514     ;; point was moved to the next unread newsgroup by exiting.
5515     (gnus-summary-jump-to-group group)
5516     (when rescan
5517       (save-excursion
5518         (save-window-excursion
5519           ;; Don't show group contents.
5520           (set-window-start (selected-window) (point-max))
5521           (gnus-group-get-new-news-this-group 1))))
5522     (gnus-group-read-group all t)
5523     (gnus-summary-goto-subject current-subject nil t)))
5524
5525 (defun gnus-summary-rescan-group (&optional all)
5526   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5527   (interactive "P")
5528   (gnus-summary-reselect-current-group all t))
5529
5530 (defun gnus-summary-update-info (&optional non-destructive)
5531   (save-excursion
5532     (let ((group gnus-newsgroup-name))
5533       (when group
5534         (when gnus-newsgroup-kill-headers
5535           (setq gnus-newsgroup-killed
5536                 (gnus-compress-sequence
5537                  (nconc
5538                   (gnus-set-sorted-intersection
5539                    (gnus-uncompress-range gnus-newsgroup-killed)
5540                    (setq gnus-newsgroup-unselected
5541                          (sort gnus-newsgroup-unselected '<)))
5542                   (setq gnus-newsgroup-unreads
5543                         (sort gnus-newsgroup-unreads '<)))
5544                  t)))
5545         (unless (listp (cdr gnus-newsgroup-killed))
5546           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5547         (let ((headers gnus-newsgroup-headers))
5548           ;; Set the new ranges of read articles.
5549           (save-excursion
5550             (set-buffer gnus-group-buffer)
5551             (gnus-undo-force-boundary))
5552           (gnus-update-read-articles
5553            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5554           ;; Set the current article marks.
5555           (let ((gnus-newsgroup-scored
5556                  (if (and (not gnus-save-score)
5557                           (not non-destructive))
5558                      nil
5559                    gnus-newsgroup-scored)))
5560             (save-excursion
5561               (gnus-update-marks)))
5562           ;; Do the cross-ref thing.
5563           (when gnus-use-cross-reference
5564             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5565           ;; Do not switch windows but change the buffer to work.
5566           (set-buffer gnus-group-buffer)
5567           (unless (gnus-ephemeral-group-p group)
5568             (gnus-group-update-group group)))))))
5569
5570 (defun gnus-summary-save-newsrc (&optional force)
5571   "Save the current number of read/marked articles in the dribble buffer.
5572 The dribble buffer will then be saved.
5573 If FORCE (the prefix), also save the .newsrc file(s)."
5574   (interactive "P")
5575   (gnus-summary-update-info t)
5576   (if force
5577       (gnus-save-newsrc-file)
5578     (gnus-dribble-save)))
5579
5580 (defun gnus-summary-exit (&optional temporary)
5581   "Exit reading current newsgroup, and then return to group selection mode.
5582 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5583   (interactive)
5584   (gnus-set-global-variables)
5585   (gnus-kill-save-kill-buffer)
5586   (gnus-async-halt-prefetch)
5587   (let* ((group gnus-newsgroup-name)
5588          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5589          (mode major-mode)
5590          (group-point nil)
5591          (buf (current-buffer)))
5592     (unless quit-config
5593       ;; Do adaptive scoring, and possibly save score files.
5594       (when gnus-newsgroup-adaptive
5595         (gnus-score-adaptive))
5596       (when gnus-use-scoring
5597         (gnus-score-save)))
5598     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5599     ;; If we have several article buffers, we kill them at exit.
5600     (unless gnus-single-article-buffer
5601       (gnus-kill-buffer gnus-original-article-buffer)
5602       (setq gnus-article-current nil))
5603     (when gnus-use-cache
5604       (gnus-cache-possibly-remove-articles)
5605       (gnus-cache-save-buffers))
5606     (gnus-async-prefetch-remove-group group)
5607     (when gnus-suppress-duplicates
5608       (gnus-dup-enter-articles))
5609     (when gnus-use-trees
5610       (gnus-tree-close group))
5611     (when gnus-use-cache
5612       (gnus-cache-write-active))
5613     ;; Remove entries for this group.
5614     (nnmail-purge-split-history (gnus-group-real-name group))
5615     ;; Make all changes in this group permanent.
5616     (unless quit-config
5617       (gnus-run-hooks 'gnus-exit-group-hook)
5618       (gnus-summary-update-info))
5619     (gnus-close-group group)
5620     ;; Make sure where we were, and go to next newsgroup.
5621     (set-buffer gnus-group-buffer)
5622     (unless quit-config
5623       (gnus-group-jump-to-group group))
5624     (gnus-run-hooks 'gnus-summary-exit-hook)
5625     (unless (or quit-config
5626                 ;; If this group has disappeared from the summary
5627                 ;; buffer, don't skip forwards.
5628                 (not (string= group (gnus-group-group-name))))
5629       (gnus-group-next-unread-group 1))
5630     (setq group-point (point))
5631     (if temporary
5632         nil                             ;Nothing to do.
5633       ;; If we have several article buffers, we kill them at exit.
5634       (unless gnus-single-article-buffer
5635         (gnus-kill-buffer gnus-article-buffer)
5636         (gnus-kill-buffer gnus-original-article-buffer)
5637         (setq gnus-article-current nil))
5638       (set-buffer buf)
5639       (if (not gnus-kill-summary-on-exit)
5640           (gnus-deaden-summary)
5641         ;; We set all buffer-local variables to nil.  It is unclear why
5642         ;; this is needed, but if we don't, buffer-local variables are
5643         ;; not garbage-collected, it seems.  This would the lead to en
5644         ;; ever-growing Emacs.
5645         (gnus-summary-clear-local-variables)
5646         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5647           (gnus-summary-clear-local-variables))
5648         (when (get-buffer gnus-article-buffer)
5649           (bury-buffer gnus-article-buffer))
5650         ;; We clear the global counterparts of the buffer-local
5651         ;; variables as well, just to be on the safe side.
5652         (set-buffer gnus-group-buffer)
5653         (gnus-summary-clear-local-variables)
5654         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5655           (gnus-summary-clear-local-variables))
5656         ;; Return to group mode buffer.
5657         (when (eq mode 'gnus-summary-mode)
5658           (gnus-kill-buffer buf)))
5659       (setq gnus-current-select-method gnus-select-method)
5660       (pop-to-buffer gnus-group-buffer)
5661       (if (not quit-config)
5662           (progn
5663             (goto-char group-point)
5664             (gnus-configure-windows 'group 'force)
5665             (unless (pos-visible-in-window-p)
5666               (forward-line (/ (static-if (featurep 'xemacs)
5667                                    (window-displayed-height)
5668                                  (1- (window-height)))
5669                                -2))
5670               (set-window-start (selected-window) (point))
5671               (goto-char group-point)))
5672         (gnus-handle-ephemeral-exit quit-config))
5673       ;; Clear the current group name.
5674       (unless quit-config
5675         (setq gnus-newsgroup-name nil)))))
5676
5677 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5678 (defun gnus-summary-exit-no-update (&optional no-questions)
5679   "Quit reading current newsgroup without updating read article info."
5680   (interactive)
5681   (let* ((group gnus-newsgroup-name)
5682          (quit-config (gnus-group-quit-config group)))
5683     (when (or no-questions
5684               gnus-expert-user
5685               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5686       (gnus-async-halt-prefetch)
5687       (mapcar 'funcall
5688               (delq 'gnus-summary-expire-articles
5689                     (copy-sequence gnus-summary-prepare-exit-hook)))
5690       ;; If we have several article buffers, we kill them at exit.
5691       (unless gnus-single-article-buffer
5692         (gnus-kill-buffer gnus-article-buffer)
5693         (gnus-kill-buffer gnus-original-article-buffer)
5694         (setq gnus-article-current nil))
5695       (if (not gnus-kill-summary-on-exit)
5696           (gnus-deaden-summary)
5697         (gnus-close-group group)
5698         (gnus-summary-clear-local-variables)
5699         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5700           (gnus-summary-clear-local-variables))
5701         (set-buffer gnus-group-buffer)
5702         (gnus-summary-clear-local-variables)
5703         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5704           (gnus-summary-clear-local-variables))
5705         (when (get-buffer gnus-summary-buffer)
5706           (kill-buffer gnus-summary-buffer)))
5707       (unless gnus-single-article-buffer
5708         (setq gnus-article-current nil))
5709       (when gnus-use-trees
5710         (gnus-tree-close group))
5711       (gnus-async-prefetch-remove-group group)
5712       (when (get-buffer gnus-article-buffer)
5713         (bury-buffer gnus-article-buffer))
5714       ;; Return to the group buffer.
5715       (gnus-configure-windows 'group 'force)
5716       ;; Clear the current group name.
5717       (setq gnus-newsgroup-name nil)
5718       (when (equal (gnus-group-group-name) group)
5719         (gnus-group-next-unread-group 1))
5720       (when quit-config
5721         (gnus-handle-ephemeral-exit quit-config)))))
5722
5723 (defun gnus-handle-ephemeral-exit (quit-config)
5724   "Handle movement when leaving an ephemeral group.
5725 The state which existed when entering the ephemeral is reset."
5726   (if (not (buffer-name (car quit-config)))
5727       (gnus-configure-windows 'group 'force)
5728     (set-buffer (car quit-config))
5729     (cond ((eq major-mode 'gnus-summary-mode)
5730            (gnus-set-global-variables))
5731           ((eq major-mode 'gnus-article-mode)
5732            (save-excursion
5733              ;; The `gnus-summary-buffer' variable may point
5734              ;; to the old summary buffer when using a single
5735              ;; article buffer.
5736              (unless (gnus-buffer-live-p gnus-summary-buffer)
5737                (set-buffer gnus-group-buffer))
5738              (set-buffer gnus-summary-buffer)
5739              (gnus-set-global-variables))))
5740     (if (or (eq (cdr quit-config) 'article)
5741             (eq (cdr quit-config) 'pick))
5742         (progn
5743           ;; The current article may be from the ephemeral group
5744           ;; thus it is best that we reload this article
5745           (gnus-summary-show-article)
5746           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5747               (gnus-configure-windows 'pick 'force)
5748             (gnus-configure-windows (cdr quit-config) 'force)))
5749       (gnus-configure-windows (cdr quit-config) 'force))
5750     (when (eq major-mode 'gnus-summary-mode)
5751       (gnus-summary-next-subject 1 nil t)
5752       (gnus-summary-recenter)
5753       (gnus-summary-position-point))))
5754
5755 (defun gnus-summary-preview-mime-message ()
5756   "MIME decode and play this message."
5757   (interactive)
5758   (let ((gnus-break-pages nil)
5759         (gnus-show-mime t))
5760     (gnus-summary-select-article gnus-show-all-headers t))
5761   (select-window (get-buffer-window gnus-article-buffer)))
5762
5763 ;;; Dead summaries.
5764
5765 (defvar gnus-dead-summary-mode-map nil)
5766
5767 (unless gnus-dead-summary-mode-map
5768   (setq gnus-dead-summary-mode-map (make-keymap))
5769   (suppress-keymap gnus-dead-summary-mode-map)
5770   (substitute-key-definition
5771    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5772   (let ((keys '("\C-d" "\r" "\177" [delete])))
5773     (while keys
5774       (define-key gnus-dead-summary-mode-map
5775         (pop keys) 'gnus-summary-wake-up-the-dead))))
5776
5777 (defvar gnus-dead-summary-mode nil
5778   "Minor mode for Gnus summary buffers.")
5779
5780 (defun gnus-dead-summary-mode (&optional arg)
5781   "Minor mode for Gnus summary buffers."
5782   (interactive "P")
5783   (when (eq major-mode 'gnus-summary-mode)
5784     (make-local-variable 'gnus-dead-summary-mode)
5785     (setq gnus-dead-summary-mode
5786           (if (null arg) (not gnus-dead-summary-mode)
5787             (> (prefix-numeric-value arg) 0)))
5788     (when gnus-dead-summary-mode
5789       (gnus-add-minor-mode
5790        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5791
5792 (defun gnus-deaden-summary ()
5793   "Make the current summary buffer into a dead summary buffer."
5794   ;; Kill any previous dead summary buffer.
5795   (when (and gnus-dead-summary
5796              (buffer-name gnus-dead-summary))
5797     (save-excursion
5798       (set-buffer gnus-dead-summary)
5799       (when gnus-dead-summary-mode
5800         (kill-buffer (current-buffer)))))
5801   ;; Make this the current dead summary.
5802   (setq gnus-dead-summary (current-buffer))
5803   (gnus-dead-summary-mode 1)
5804   (let ((name (buffer-name)))
5805     (when (string-match "Summary" name)
5806       (rename-buffer
5807        (concat (substring name 0 (match-beginning 0)) "Dead "
5808                (substring name (match-beginning 0)))
5809        t)
5810       (bury-buffer))))
5811
5812 (defun gnus-kill-or-deaden-summary (buffer)
5813   "Kill or deaden the summary BUFFER."
5814   (save-excursion
5815     (when (and (buffer-name buffer)
5816                (not gnus-single-article-buffer))
5817       (save-excursion
5818         (set-buffer buffer)
5819         (gnus-kill-buffer gnus-article-buffer)
5820         (gnus-kill-buffer gnus-original-article-buffer)))
5821     (cond (gnus-kill-summary-on-exit
5822            (when (and gnus-use-trees
5823                       (gnus-buffer-exists-p buffer))
5824              (save-excursion
5825                (set-buffer buffer)
5826                (gnus-tree-close gnus-newsgroup-name)))
5827            (gnus-kill-buffer buffer))
5828           ((gnus-buffer-exists-p buffer)
5829            (save-excursion
5830              (set-buffer buffer)
5831              (gnus-deaden-summary))))))
5832
5833 (defun gnus-summary-wake-up-the-dead (&rest args)
5834   "Wake up the dead summary buffer."
5835   (interactive)
5836   (gnus-dead-summary-mode -1)
5837   (let ((name (buffer-name)))
5838     (when (string-match "Dead " name)
5839       (rename-buffer
5840        (concat (substring name 0 (match-beginning 0))
5841                (substring name (match-end 0)))
5842        t)))
5843   (gnus-message 3 "This dead summary is now alive again"))
5844
5845 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5846 (defun gnus-summary-fetch-faq (&optional faq-dir)
5847   "Fetch the FAQ for the current group.
5848 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5849 in."
5850   (interactive
5851    (list
5852     (when current-prefix-arg
5853       (completing-read
5854        "Faq dir: " (and (listp gnus-group-faq-directory)
5855                         (mapcar (lambda (file) (list file))
5856                                 gnus-group-faq-directory))))))
5857   (let (gnus-faq-buffer)
5858     (when (setq gnus-faq-buffer
5859                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5860       (gnus-configure-windows 'summary-faq))))
5861
5862 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5863 (defun gnus-summary-describe-group (&optional force)
5864   "Describe the current newsgroup."
5865   (interactive "P")
5866   (gnus-group-describe-group force gnus-newsgroup-name))
5867
5868 (defun gnus-summary-describe-briefly ()
5869   "Describe summary mode commands briefly."
5870   (interactive)
5871   (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")))
5872
5873 ;; Walking around group mode buffer from summary mode.
5874
5875 (defun gnus-summary-next-group (&optional no-article target-group backward)
5876   "Exit current newsgroup and then select next unread newsgroup.
5877 If prefix argument NO-ARTICLE is non-nil, no article is selected
5878 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5879 previous group instead."
5880   (interactive "P")
5881   ;; Stop pre-fetching.
5882   (gnus-async-halt-prefetch)
5883   (let ((current-group gnus-newsgroup-name)
5884         (current-buffer (current-buffer))
5885         entered)
5886     ;; First we semi-exit this group to update Xrefs and all variables.
5887     ;; We can't do a real exit, because the window conf must remain
5888     ;; the same in case the user is prompted for info, and we don't
5889     ;; want the window conf to change before that...
5890     (gnus-summary-exit t)
5891     (while (not entered)
5892       ;; Then we find what group we are supposed to enter.
5893       (set-buffer gnus-group-buffer)
5894       (gnus-group-jump-to-group current-group)
5895       (setq target-group
5896             (or target-group
5897                 (if (eq gnus-keep-same-level 'best)
5898                     (gnus-summary-best-group gnus-newsgroup-name)
5899                   (gnus-summary-search-group backward gnus-keep-same-level))))
5900       (if (not target-group)
5901           ;; There are no further groups, so we return to the group
5902           ;; buffer.
5903           (progn
5904             (gnus-message 5 "Returning to the group buffer")
5905             (setq entered t)
5906             (when (gnus-buffer-live-p current-buffer)
5907               (set-buffer current-buffer)
5908               (gnus-summary-exit))
5909             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5910         ;; We try to enter the target group.
5911         (gnus-group-jump-to-group target-group)
5912         (let ((unreads (gnus-group-group-unread)))
5913           (if (and (or (eq t unreads)
5914                        (and unreads (not (zerop unreads))))
5915                    (gnus-summary-read-group
5916                     target-group nil no-article
5917                     (and (buffer-name current-buffer) current-buffer)
5918                     nil backward))
5919               (setq entered t)
5920             (setq current-group target-group
5921                   target-group nil)))))))
5922
5923 (defun gnus-summary-prev-group (&optional no-article)
5924   "Exit current newsgroup and then select previous unread newsgroup.
5925 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5926   (interactive "P")
5927   (gnus-summary-next-group no-article nil t))
5928
5929 ;; Walking around summary lines.
5930
5931 (defun gnus-summary-first-subject (&optional unread undownloaded)
5932   "Go to the first unread subject.
5933 If UNREAD is non-nil, go to the first unread article.
5934 Returns the article selected or nil if there are no unread articles."
5935   (interactive "P")
5936   (prog1
5937       (cond
5938        ;; Empty summary.
5939        ((null gnus-newsgroup-data)
5940         (gnus-message 3 "No articles in the group")
5941         nil)
5942        ;; Pick the first article.
5943        ((not unread)
5944         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5945         (gnus-data-number (car gnus-newsgroup-data)))
5946        ;; No unread articles.
5947        ((null gnus-newsgroup-unreads)
5948         (gnus-message 3 "No more unread articles")
5949         nil)
5950        ;; Find the first unread article.
5951        (t
5952         (let ((data gnus-newsgroup-data))
5953           (while (and data
5954                       (and (not (and undownloaded
5955                                      (eq gnus-undownloaded-mark
5956                                          (gnus-data-mark (car data)))))
5957                            (not (gnus-data-unread-p (car data)))))
5958             (setq data (cdr data)))
5959           (when data
5960             (goto-char (gnus-data-pos (car data)))
5961             (gnus-data-number (car data))))))
5962     (gnus-summary-position-point)))
5963
5964 (defun gnus-summary-next-subject (n &optional unread dont-display)
5965   "Go to next N'th summary line.
5966 If N is negative, go to the previous N'th subject line.
5967 If UNREAD is non-nil, only unread articles are selected.
5968 The difference between N and the actual number of steps taken is
5969 returned."
5970   (interactive "p")
5971   (let ((backward (< n 0))
5972         (n (abs n)))
5973     (while (and (> n 0)
5974                 (if backward
5975                     (gnus-summary-find-prev unread)
5976                   (gnus-summary-find-next unread)))
5977       (unless (zerop (setq n (1- n)))
5978         (gnus-summary-show-thread)))
5979     (when (/= 0 n)
5980       (gnus-message 7 "No more%s articles"
5981                     (if unread " unread" "")))
5982     (unless dont-display
5983       (gnus-summary-recenter)
5984       (gnus-summary-position-point))
5985     n))
5986
5987 (defun gnus-summary-next-unread-subject (n)
5988   "Go to next N'th unread summary line."
5989   (interactive "p")
5990   (gnus-summary-next-subject n t))
5991
5992 (defun gnus-summary-prev-subject (n &optional unread)
5993   "Go to previous N'th summary line.
5994 If optional argument UNREAD is non-nil, only unread article is selected."
5995   (interactive "p")
5996   (gnus-summary-next-subject (- n) unread))
5997
5998 (defun gnus-summary-prev-unread-subject (n)
5999   "Go to previous N'th unread summary line."
6000   (interactive "p")
6001   (gnus-summary-next-subject (- n) t))
6002
6003 (defun gnus-summary-goto-subject (article &optional force silent)
6004   "Go the subject line of ARTICLE.
6005 If FORCE, also allow jumping to articles not currently shown."
6006   (interactive "nArticle number: ")
6007   (let ((b (point))
6008         (data (gnus-data-find article)))
6009     ;; We read in the article if we have to.
6010     (and (not data)
6011          force
6012          (gnus-summary-insert-subject
6013           article
6014           (if (or (numberp force) (vectorp force)) force)
6015           t)
6016          (setq data (gnus-data-find article)))
6017     (goto-char b)
6018     (if (not data)
6019         (progn
6020           (unless silent
6021             (gnus-message 3 "Can't find article %d" article))
6022           nil)
6023       (goto-char (gnus-data-pos data))
6024       (gnus-summary-position-point)
6025       article)))
6026
6027 ;; Walking around summary lines with displaying articles.
6028
6029 (defun gnus-summary-expand-window (&optional arg)
6030   "Make the summary buffer take up the entire Emacs frame.
6031 Given a prefix, will force an `article' buffer configuration."
6032   (interactive "P")
6033   (if arg
6034       (gnus-configure-windows 'article 'force)
6035     (gnus-configure-windows 'summary 'force)))
6036
6037 (defun gnus-summary-display-article (article &optional all-header)
6038   "Display ARTICLE in article buffer."
6039   (when (gnus-buffer-live-p gnus-article-buffer)
6040     (with-current-buffer gnus-article-buffer
6041       (mm-enable-multibyte-mule4)))
6042   (gnus-set-global-variables)
6043   (when (gnus-buffer-live-p gnus-article-buffer)
6044     (with-current-buffer gnus-article-buffer
6045       (setq gnus-article-charset gnus-newsgroup-charset)
6046       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)))
6047   (if (null article)
6048       nil
6049     (prog1
6050         (if gnus-summary-display-article-function
6051             (funcall gnus-summary-display-article-function article all-header)
6052           (gnus-article-prepare article all-header))
6053       (with-current-buffer gnus-article-buffer
6054         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6055              nil))
6056       (gnus-run-hooks 'gnus-select-article-hook)
6057       (when (and gnus-current-article
6058                  (not (zerop gnus-current-article)))
6059         (gnus-summary-goto-subject gnus-current-article))
6060       (gnus-summary-recenter)
6061       (when (and gnus-use-trees gnus-show-threads)
6062         (gnus-possibly-generate-tree article)
6063         (gnus-highlight-selected-tree article))
6064       ;; Successfully display article.
6065       (gnus-article-set-window-start
6066        (cdr (assq article gnus-newsgroup-bookmarks))))))
6067
6068 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6069   "Select the current article.
6070 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6071 non-nil, the article will be re-fetched even if it already present in
6072 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6073 be displayed."
6074   ;; Make sure we are in the summary buffer to work around bbdb bug.
6075   (unless (eq major-mode 'gnus-summary-mode)
6076     (set-buffer gnus-summary-buffer))
6077   (let ((article (or article (gnus-summary-article-number)))
6078         (all-headers (not (not all-headers))) ;Must be T or NIL.
6079         gnus-summary-display-article-function)
6080     (and (not pseudo)
6081          (gnus-summary-article-pseudo-p article)
6082          (error "This is a pseudo-article"))
6083     (save-excursion
6084       (set-buffer gnus-summary-buffer)
6085       (if (or (and gnus-single-article-buffer
6086                    (or (null gnus-current-article)
6087                        (null gnus-article-current)
6088                        (null (get-buffer gnus-article-buffer))
6089                        (not (eq article (cdr gnus-article-current)))
6090                        (not (equal (car gnus-article-current)
6091                                    gnus-newsgroup-name))))
6092               (and (not gnus-single-article-buffer)
6093                    (or (null gnus-current-article)
6094                        (not (eq gnus-current-article article))))
6095               force)
6096           ;; The requested article is different from the current article.
6097           (progn
6098             (gnus-summary-display-article article all-headers)
6099             (when (or all-headers gnus-show-all-headers)
6100               (gnus-article-show-all-headers))
6101             (gnus-article-set-window-start
6102              (cdr (assq article gnus-newsgroup-bookmarks)))
6103             article)
6104         (when (or all-headers gnus-show-all-headers)
6105           (gnus-article-show-all-headers))
6106         'old))))
6107
6108 (defun gnus-summary-force-verify-and-decrypt ()
6109   (interactive)
6110   (let ((mm-verify-option 'known)
6111         (mm-decrypt-option 'known))
6112     (gnus-summary-select-article nil 'force)))
6113
6114 (defun gnus-summary-set-current-mark (&optional current-mark)
6115   "Obsolete function."
6116   nil)
6117
6118 (defun gnus-summary-next-article (&optional unread subject backward push)
6119   "Select the next article.
6120 If UNREAD, only unread articles are selected.
6121 If SUBJECT, only articles with SUBJECT are selected.
6122 If BACKWARD, the previous article is selected instead of the next."
6123   (interactive "P")
6124   (cond
6125    ;; Is there such an article?
6126    ((and (gnus-summary-search-forward unread subject backward)
6127          (or (gnus-summary-display-article (gnus-summary-article-number))
6128              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6129     (gnus-summary-position-point))
6130    ;; If not, we try the first unread, if that is wanted.
6131    ((and subject
6132          gnus-auto-select-same
6133          (gnus-summary-first-unread-article))
6134     (gnus-summary-position-point)
6135     (gnus-message 6 "Wrapped"))
6136    ;; Try to get next/previous article not displayed in this group.
6137    ((and gnus-auto-extend-newsgroup
6138          (not unread) (not subject))
6139     (gnus-summary-goto-article
6140      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6141      nil (count-lines (point-min) (point))))
6142    ;; Go to next/previous group.
6143    (t
6144     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6145       (gnus-summary-jump-to-group gnus-newsgroup-name))
6146     (let ((cmd last-command-char)
6147           (point
6148            (save-excursion
6149              (set-buffer gnus-group-buffer)
6150              (point)))
6151           (group
6152            (if (eq gnus-keep-same-level 'best)
6153                (gnus-summary-best-group gnus-newsgroup-name)
6154              (gnus-summary-search-group backward gnus-keep-same-level))))
6155       ;; For some reason, the group window gets selected.  We change
6156       ;; it back.
6157       (select-window (get-buffer-window (current-buffer)))
6158       ;; Select next unread newsgroup automagically.
6159       (cond
6160        ((or (not gnus-auto-select-next)
6161             (not cmd))
6162         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6163        ((or (eq gnus-auto-select-next 'quietly)
6164             (and (eq gnus-auto-select-next 'slightly-quietly)
6165                  push)
6166             (and (eq gnus-auto-select-next 'almost-quietly)
6167                  (gnus-summary-last-article-p)))
6168         ;; Select quietly.
6169         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6170             (gnus-summary-exit)
6171           (gnus-message 7 "No more%s articles (%s)..."
6172                         (if unread " unread" "")
6173                         (if group (concat "selecting " group)
6174                           "exiting"))
6175           (gnus-summary-next-group nil group backward)))
6176        (t
6177         (when (gnus-key-press-event-p last-input-event)
6178           (gnus-summary-walk-group-buffer
6179            gnus-newsgroup-name cmd unread backward point))))))))
6180
6181 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6182   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6183                       (?\C-p (gnus-group-prev-unread-group 1))))
6184         (cursor-in-echo-area t)
6185         keve key group ended)
6186     (save-excursion
6187       (set-buffer gnus-group-buffer)
6188       (goto-char start)
6189       (setq group
6190             (if (eq gnus-keep-same-level 'best)
6191                 (gnus-summary-best-group gnus-newsgroup-name)
6192               (gnus-summary-search-group backward gnus-keep-same-level))))
6193     (while (not ended)
6194       (gnus-message
6195        5 "No more%s articles%s" (if unread " unread" "")
6196        (if (and group
6197                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6198            (format " (Type %s for %s [%s])"
6199                    (single-key-description cmd) group
6200                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6201          (format " (Type %s to exit %s)"
6202                  (single-key-description cmd)
6203                  gnus-newsgroup-name)))
6204       ;; Confirm auto selection.
6205       (setq key (car (setq keve (gnus-read-event-char))))
6206       (setq ended t)
6207       (cond
6208        ((assq key keystrokes)
6209         (let ((obuf (current-buffer)))
6210           (switch-to-buffer gnus-group-buffer)
6211           (when group
6212             (gnus-group-jump-to-group group))
6213           (eval (cadr (assq key keystrokes)))
6214           (setq group (gnus-group-group-name))
6215           (switch-to-buffer obuf))
6216         (setq ended nil))
6217        ((equal key cmd)
6218         (if (or (not group)
6219                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6220             (gnus-summary-exit)
6221           (gnus-summary-next-group nil group backward)))
6222        (t
6223         (push (cdr keve) unread-command-events))))))
6224
6225 (defun gnus-summary-next-unread-article ()
6226   "Select unread article after current one."
6227   (interactive)
6228   (gnus-summary-next-article
6229    (or (not (eq gnus-summary-goto-unread 'never))
6230        (gnus-summary-last-article-p (gnus-summary-article-number)))
6231    (and gnus-auto-select-same
6232         (gnus-summary-article-subject))))
6233
6234 (defun gnus-summary-prev-article (&optional unread subject)
6235   "Select the article after the current one.
6236 If UNREAD is non-nil, only unread articles are selected."
6237   (interactive "P")
6238   (gnus-summary-next-article unread subject t))
6239
6240 (defun gnus-summary-prev-unread-article ()
6241   "Select unread article before current one."
6242   (interactive)
6243   (gnus-summary-prev-article
6244    (or (not (eq gnus-summary-goto-unread 'never))
6245        (gnus-summary-first-article-p (gnus-summary-article-number)))
6246    (and gnus-auto-select-same
6247         (gnus-summary-article-subject))))
6248
6249 (defun gnus-summary-next-page (&optional lines circular)
6250   "Show next page of the selected article.
6251 If at the end of the current article, select the next article.
6252 LINES says how many lines should be scrolled up.
6253
6254 If CIRCULAR is non-nil, go to the start of the article instead of
6255 selecting the next article when reaching the end of the current
6256 article."
6257   (interactive "P")
6258   (setq gnus-summary-buffer (current-buffer))
6259   (gnus-set-global-variables)
6260   (let ((article (gnus-summary-article-number))
6261         (article-window (get-buffer-window gnus-article-buffer t))
6262         endp)
6263     ;; If the buffer is empty, we have no article.
6264     (unless article
6265       (error "No article to select"))
6266     (gnus-configure-windows 'article)
6267     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6268         (if (and (eq gnus-summary-goto-unread 'never)
6269                  (not (gnus-summary-last-article-p article)))
6270             (gnus-summary-next-article)
6271           (gnus-summary-next-unread-article))
6272       (if (or (null gnus-current-article)
6273               (null gnus-article-current)
6274               (/= article (cdr gnus-article-current))
6275               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6276           ;; Selected subject is different from current article's.
6277           (gnus-summary-display-article article)
6278         (when article-window
6279           (gnus-eval-in-buffer-window gnus-article-buffer
6280             (setq endp (gnus-article-next-page lines)))
6281           (when endp
6282             (cond (circular
6283                    (gnus-summary-beginning-of-article))
6284                   (lines
6285                    (gnus-message 3 "End of message"))
6286                   ((null lines)
6287                    (if (and (eq gnus-summary-goto-unread 'never)
6288                             (not (gnus-summary-last-article-p article)))
6289                        (gnus-summary-next-article)
6290                      (gnus-summary-next-unread-article))))))))
6291     (gnus-summary-recenter)
6292     (gnus-summary-position-point)))
6293
6294 (defun gnus-summary-prev-page (&optional lines move)
6295   "Show previous page of selected article.
6296 Argument LINES specifies lines to be scrolled down.
6297 If MOVE, move to the previous unread article if point is at
6298 the beginning of the buffer."
6299   (interactive "P")
6300   (let ((article (gnus-summary-article-number))
6301         (article-window (get-buffer-window gnus-article-buffer t))
6302         endp)
6303     (gnus-configure-windows 'article)
6304     (if (or (null gnus-current-article)
6305             (null gnus-article-current)
6306             (/= article (cdr gnus-article-current))
6307             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6308         ;; Selected subject is different from current article's.
6309         (gnus-summary-display-article article)
6310       (gnus-summary-recenter)
6311       (when article-window
6312         (gnus-eval-in-buffer-window gnus-article-buffer
6313           (setq endp (gnus-article-prev-page lines)))
6314         (when (and move endp)
6315           (cond (lines
6316                  (gnus-message 3 "Beginning of message"))
6317                 ((null lines)
6318                  (if (and (eq gnus-summary-goto-unread 'never)
6319                           (not (gnus-summary-first-article-p article)))
6320                      (gnus-summary-prev-article)
6321                    (gnus-summary-prev-unread-article))))))))
6322   (gnus-summary-position-point))
6323
6324 (defun gnus-summary-prev-page-or-article (&optional lines)
6325   "Show previous page of selected article.
6326 Argument LINES specifies lines to be scrolled down.
6327 If at the beginning of the article, go to the next article."
6328   (interactive "P")
6329   (gnus-summary-prev-page lines t))
6330
6331 (defun gnus-summary-scroll-up (lines)
6332   "Scroll up (or down) one line current article.
6333 Argument LINES specifies lines to be scrolled up (or down if negative)."
6334   (interactive "p")
6335   (gnus-configure-windows 'article)
6336   (gnus-summary-show-thread)
6337   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6338     (gnus-eval-in-buffer-window gnus-article-buffer
6339       (cond ((> lines 0)
6340              (when (gnus-article-next-page lines)
6341                (gnus-message 3 "End of message")))
6342             ((< lines 0)
6343              (gnus-article-prev-page (- lines))))))
6344   (gnus-summary-recenter)
6345   (gnus-summary-position-point))
6346
6347 (defun gnus-summary-scroll-down (lines)
6348   "Scroll down (or up) one line current article.
6349 Argument LINES specifies lines to be scrolled down (or up if negative)."
6350   (interactive "p")
6351   (gnus-summary-scroll-up (- lines)))
6352
6353 (defun gnus-summary-next-same-subject ()
6354   "Select next article which has the same subject as current one."
6355   (interactive)
6356   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6357
6358 (defun gnus-summary-prev-same-subject ()
6359   "Select previous article which has the same subject as current one."
6360   (interactive)
6361   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6362
6363 (defun gnus-summary-next-unread-same-subject ()
6364   "Select next unread article which has the same subject as current one."
6365   (interactive)
6366   (gnus-summary-next-article t (gnus-summary-article-subject)))
6367
6368 (defun gnus-summary-prev-unread-same-subject ()
6369   "Select previous unread article which has the same subject as current one."
6370   (interactive)
6371   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6372
6373 (defun gnus-summary-first-unread-article ()
6374   "Select the first unread article.
6375 Return nil if there are no unread articles."
6376   (interactive)
6377   (prog1
6378       (when (gnus-summary-first-subject t)
6379         (gnus-summary-show-thread)
6380         (gnus-summary-first-subject t)
6381         (gnus-summary-display-article (gnus-summary-article-number)))
6382     (gnus-summary-position-point)))
6383
6384 (defun gnus-summary-first-unread-subject ()
6385   "Place the point on the subject line of the first unread article.
6386 Return nil if there are no unread articles."
6387   (interactive)
6388   (prog1
6389       (when (gnus-summary-first-subject t)
6390         (gnus-summary-show-thread)
6391         (gnus-summary-first-subject t))
6392     (gnus-summary-position-point)))
6393
6394 (defun gnus-summary-first-article ()
6395   "Select the first article.
6396 Return nil if there are no articles."
6397   (interactive)
6398   (prog1
6399       (when (gnus-summary-first-subject)
6400         (gnus-summary-show-thread)
6401         (gnus-summary-first-subject)
6402         (gnus-summary-display-article (gnus-summary-article-number)))
6403     (gnus-summary-position-point)))
6404
6405 (defun gnus-summary-best-unread-article ()
6406   "Select the unread article with the highest score."
6407   (interactive)
6408   (let ((best -1000000)
6409         (data gnus-newsgroup-data)
6410         article score)
6411     (while data
6412       (and (gnus-data-unread-p (car data))
6413            (> (setq score
6414                     (gnus-summary-article-score (gnus-data-number (car data))))
6415               best)
6416            (setq best score
6417                  article (gnus-data-number (car data))))
6418       (setq data (cdr data)))
6419     (prog1
6420         (if article
6421             (gnus-summary-goto-article article)
6422           (error "No unread articles"))
6423       (gnus-summary-position-point))))
6424
6425 (defun gnus-summary-last-subject ()
6426   "Go to the last displayed subject line in the group."
6427   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6428     (when article
6429       (gnus-summary-goto-subject article))))
6430
6431 (defun gnus-summary-goto-article (article &optional all-headers force)
6432   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6433 If ALL-HEADERS is non-nil, no header lines are hidden.
6434 If FORCE, go to the article even if it isn't displayed.  If FORCE
6435 is a number, it is the line the article is to be displayed on."
6436   (interactive
6437    (list
6438     (completing-read
6439      "Article number or Message-ID: "
6440      (mapcar (lambda (number) (list (int-to-string number)))
6441              gnus-newsgroup-limit))
6442     current-prefix-arg
6443     t))
6444   (prog1
6445       (if (and (stringp article)
6446                (string-match "@" article))
6447           (gnus-summary-refer-article article)
6448         (when (stringp article)
6449           (setq article (string-to-number article)))
6450         (if (gnus-summary-goto-subject article force)
6451             (gnus-summary-display-article article all-headers)
6452           (gnus-message 4 "Couldn't go to article %s" article) nil))
6453     (gnus-summary-position-point)))
6454
6455 (defun gnus-summary-goto-last-article ()
6456   "Go to the previously read article."
6457   (interactive)
6458   (prog1
6459       (when gnus-last-article
6460         (gnus-summary-goto-article gnus-last-article nil t))
6461     (gnus-summary-position-point)))
6462
6463 (defun gnus-summary-pop-article (number)
6464   "Pop one article off the history and go to the previous.
6465 NUMBER articles will be popped off."
6466   (interactive "p")
6467   (let (to)
6468     (setq gnus-newsgroup-history
6469           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6470     (if to
6471         (gnus-summary-goto-article (car to) nil t)
6472       (error "Article history empty")))
6473   (gnus-summary-position-point))
6474
6475 ;; Summary commands and functions for limiting the summary buffer.
6476
6477 (defun gnus-summary-limit-to-articles (n)
6478   "Limit the summary buffer to the next N articles.
6479 If not given a prefix, use the process marked articles instead."
6480   (interactive "P")
6481   (prog1
6482       (let ((articles (gnus-summary-work-articles n)))
6483         (setq gnus-newsgroup-processable nil)
6484         (gnus-summary-limit articles))
6485     (gnus-summary-position-point)))
6486
6487 (defun gnus-summary-pop-limit (&optional total)
6488   "Restore the previous limit.
6489 If given a prefix, remove all limits."
6490   (interactive "P")
6491   (when total
6492     (setq gnus-newsgroup-limits
6493           (list (mapcar (lambda (h) (mail-header-number h))
6494                         gnus-newsgroup-headers))))
6495   (unless gnus-newsgroup-limits
6496     (error "No limit to pop"))
6497   (prog1
6498       (gnus-summary-limit nil 'pop)
6499     (gnus-summary-position-point)))
6500
6501 (defun gnus-summary-limit-to-subject (subject &optional header)
6502   "Limit the summary buffer to articles that have subjects that match a regexp."
6503   (interactive "sLimit to subject (regexp): ")
6504   (unless header
6505     (setq header "subject"))
6506   (when (not (equal "" subject))
6507     (prog1
6508         (let ((articles (gnus-summary-find-matching
6509                          (or header "subject") subject 'all)))
6510           (unless articles
6511             (error "Found no matches for \"%s\"" subject))
6512           (gnus-summary-limit articles))
6513       (gnus-summary-position-point))))
6514
6515 (defun gnus-summary-limit-to-author (from)
6516   "Limit the summary buffer to articles that have authors that match a regexp."
6517   (interactive "sLimit to author (regexp): ")
6518   (gnus-summary-limit-to-subject from "from"))
6519
6520 (defun gnus-summary-limit-to-age (age &optional younger-p)
6521   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6522 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6523 articles that are younger than AGE days."
6524   (interactive
6525    (let ((younger current-prefix-arg)
6526          (days-got nil)
6527          days)
6528      (while (not days-got)
6529        (setq days (if younger
6530                       (read-string "Limit to articles within (in days): ")
6531                     (read-string "Limit to articles older than (in days): ")))
6532        (when (> (length days) 0)
6533          (setq days (read days)))
6534        (if (numberp days)
6535            (setq days-got t)
6536          (message "Please enter a number.")
6537          (sleep-for 1)))
6538      (list days younger)))
6539   (prog1
6540       (let ((data gnus-newsgroup-data)
6541             (cutoff (days-to-time age))
6542             articles d date is-younger)
6543         (while (setq d (pop data))
6544           (when (and (vectorp (gnus-data-header d))
6545                      (setq date (mail-header-date (gnus-data-header d))))
6546             (setq is-younger (time-less-p
6547                               (time-since (condition-case ()
6548                                               (date-to-time date)
6549                                             (error '(0 0))))
6550                               cutoff))
6551             (when (if younger-p
6552                       is-younger
6553                     (not is-younger))
6554               (push (gnus-data-number d) articles))))
6555         (gnus-summary-limit (nreverse articles)))
6556     (gnus-summary-position-point)))
6557
6558 (defun gnus-summary-limit-to-extra (header regexp)
6559   "Limit the summary buffer to articles that match an 'extra' header."
6560   (interactive
6561    (let ((header
6562           (intern
6563            (gnus-completing-read
6564             (symbol-name (car gnus-extra-headers))
6565             "Limit extra header:"
6566             (mapcar (lambda (x)
6567                       (cons (symbol-name x) x))
6568                     gnus-extra-headers)
6569             nil
6570             t))))
6571      (list header
6572            (read-string (format "Limit to header %s (regexp): " header)))))
6573   (when (not (equal "" regexp))
6574     (prog1
6575         (let ((articles (gnus-summary-find-matching
6576                          (cons 'extra header) regexp 'all)))
6577           (unless articles
6578             (error "Found no matches for \"%s\"" regexp))
6579           (gnus-summary-limit articles))
6580       (gnus-summary-position-point))))
6581
6582 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6583 (make-obsolete
6584  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6585
6586 (defun gnus-summary-limit-to-unread (&optional all)
6587   "Limit the summary buffer to articles that are not marked as read.
6588 If ALL is non-nil, limit strictly to unread articles."
6589   (interactive "P")
6590   (if all
6591       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6592     (gnus-summary-limit-to-marks
6593      ;; Concat all the marks that say that an article is read and have
6594      ;; those removed.
6595      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6596            gnus-killed-mark gnus-kill-file-mark
6597            gnus-low-score-mark gnus-expirable-mark
6598            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6599            gnus-duplicate-mark gnus-souped-mark)
6600      'reverse)))
6601
6602 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6603 (make-obsolete 'gnus-summary-delete-marked-with
6604                'gnus-summary-limit-exlude-marks)
6605
6606 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6607   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6608 If REVERSE, limit the summary buffer to articles that are marked
6609 with MARKS.  MARKS can either be a string of marks or a list of marks.
6610 Returns how many articles were removed."
6611   (interactive "sMarks: ")
6612   (gnus-summary-limit-to-marks marks t))
6613
6614 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6615   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6616 If REVERSE (the prefix), limit the summary buffer to articles that are
6617 not marked with MARKS.  MARKS can either be a string of marks or a
6618 list of marks.
6619 Returns how many articles were removed."
6620   (interactive "sMarks: \nP")
6621   (prog1
6622       (let ((data gnus-newsgroup-data)
6623             (marks (if (listp marks) marks
6624                      (append marks nil))) ; Transform to list.
6625             articles)
6626         (while data
6627           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6628                   (memq (gnus-data-mark (car data)) marks))
6629             (push (gnus-data-number (car data)) articles))
6630           (setq data (cdr data)))
6631         (gnus-summary-limit articles))
6632     (gnus-summary-position-point)))
6633
6634 (defun gnus-summary-limit-to-score (&optional score)
6635   "Limit to articles with score at or above SCORE."
6636   (interactive "P")
6637   (setq score (if score
6638                   (prefix-numeric-value score)
6639                 (or gnus-summary-default-score 0)))
6640   (let ((data gnus-newsgroup-data)
6641         articles)
6642     (while data
6643       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6644                 score)
6645         (push (gnus-data-number (car data)) articles))
6646       (setq data (cdr data)))
6647     (prog1
6648         (gnus-summary-limit articles)
6649       (gnus-summary-position-point))))
6650
6651 (defun gnus-summary-limit-include-thread (id)
6652   "Display all the hidden articles that in the current thread."
6653   (interactive (list (mail-header-id (gnus-summary-article-header))))
6654   (let ((articles (gnus-articles-in-thread
6655                    (gnus-id-to-thread (gnus-root-id id)))))
6656     (prog1
6657         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6658       (gnus-summary-position-point))))
6659
6660 (defun gnus-summary-limit-include-dormant ()
6661   "Display all the hidden articles that are marked as dormant.
6662 Note that this command only works on a subset of the articles currently
6663 fetched for this group."
6664   (interactive)
6665   (unless gnus-newsgroup-dormant
6666     (error "There are no dormant articles in this group"))
6667   (prog1
6668       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6669     (gnus-summary-position-point)))
6670
6671 (defun gnus-summary-limit-exclude-dormant ()
6672   "Hide all dormant articles."
6673   (interactive)
6674   (prog1
6675       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6676     (gnus-summary-position-point)))
6677
6678 (defun gnus-summary-limit-exclude-childless-dormant ()
6679   "Hide all dormant articles that have no children."
6680   (interactive)
6681   (let ((data (gnus-data-list t))
6682         articles d children)
6683     ;; Find all articles that are either not dormant or have
6684     ;; children.
6685     (while (setq d (pop data))
6686       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6687                 (and (setq children
6688                            (gnus-article-children (gnus-data-number d)))
6689                      (let (found)
6690                        (while children
6691                          (when (memq (car children) articles)
6692                            (setq children nil
6693                                  found t))
6694                          (pop children))
6695                        found)))
6696         (push (gnus-data-number d) articles)))
6697     ;; Do the limiting.
6698     (prog1
6699         (gnus-summary-limit articles)
6700       (gnus-summary-position-point))))
6701
6702 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6703   "Mark all unread excluded articles as read.
6704 If ALL, mark even excluded ticked and dormants as read."
6705   (interactive "P")
6706   (let ((articles (gnus-sorted-complement
6707                    (sort
6708                     (mapcar (lambda (h) (mail-header-number h))
6709                             gnus-newsgroup-headers)
6710                     '<)
6711                    (sort gnus-newsgroup-limit '<)))
6712         article)
6713     (setq gnus-newsgroup-unreads
6714           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6715     (if all
6716         (setq gnus-newsgroup-dormant nil
6717               gnus-newsgroup-marked nil
6718               gnus-newsgroup-reads
6719               (nconc
6720                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6721                gnus-newsgroup-reads))
6722       (while (setq article (pop articles))
6723         (unless (or (memq article gnus-newsgroup-dormant)
6724                     (memq article gnus-newsgroup-marked))
6725           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6726
6727 (defun gnus-summary-limit (articles &optional pop)
6728   (if pop
6729       ;; We pop the previous limit off the stack and use that.
6730       (setq articles (car gnus-newsgroup-limits)
6731             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6732     ;; We use the new limit, so we push the old limit on the stack.
6733     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6734   ;; Set the limit.
6735   (setq gnus-newsgroup-limit articles)
6736   (let ((total (length gnus-newsgroup-data))
6737         (data (gnus-data-find-list (gnus-summary-article-number)))
6738         (gnus-summary-mark-below nil)   ; Inhibit this.
6739         found)
6740     ;; This will do all the work of generating the new summary buffer
6741     ;; according to the new limit.
6742     (gnus-summary-prepare)
6743     ;; Hide any threads, possibly.
6744     (and gnus-show-threads
6745          gnus-thread-hide-subtree
6746          (gnus-summary-hide-all-threads))
6747     ;; Try to return to the article you were at, or one in the
6748     ;; neighborhood.
6749     (when data
6750       ;; We try to find some article after the current one.
6751       (while data
6752         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6753           (setq data nil
6754                 found t))
6755         (setq data (cdr data))))
6756     (unless found
6757       ;; If there is no data, that means that we were after the last
6758       ;; article.  The same goes when we can't find any articles
6759       ;; after the current one.
6760       (goto-char (point-max))
6761       (gnus-summary-find-prev))
6762     (gnus-set-mode-line 'summary)
6763     ;; We return how many articles were removed from the summary
6764     ;; buffer as a result of the new limit.
6765     (- total (length gnus-newsgroup-data))))
6766
6767 (defsubst gnus-invisible-cut-children (threads)
6768   (let ((num 0))
6769     (while threads
6770       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6771         (incf num))
6772       (pop threads))
6773     (< num 2)))
6774
6775 (defsubst gnus-cut-thread (thread)
6776   "Go forwards in the thread until we find an article that we want to display."
6777   (when (or (eq gnus-fetch-old-headers 'some)
6778             (eq gnus-fetch-old-headers 'invisible)
6779             (numberp gnus-fetch-old-headers)
6780             (eq gnus-build-sparse-threads 'some)
6781             (eq gnus-build-sparse-threads 'more))
6782     ;; Deal with old-fetched headers and sparse threads.
6783     (while (and
6784             thread
6785             (or
6786              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6787              (gnus-summary-article-ancient-p
6788               (mail-header-number (car thread))))
6789             (if (or (<= (length (cdr thread)) 1)
6790                     (eq gnus-fetch-old-headers 'invisible))
6791                 (setq gnus-newsgroup-limit
6792                       (delq (mail-header-number (car thread))
6793                             gnus-newsgroup-limit)
6794                       thread (cadr thread))
6795               (when (gnus-invisible-cut-children (cdr thread))
6796                 (let ((th (cdr thread)))
6797                   (while th
6798                     (if (memq (mail-header-number (caar th))
6799                               gnus-newsgroup-limit)
6800                         (setq thread (car th)
6801                               th nil)
6802                       (setq th (cdr th))))))))))
6803   thread)
6804
6805 (defun gnus-cut-threads (threads)
6806   "Cut off all uninteresting articles from the beginning of threads."
6807   (when (or (eq gnus-fetch-old-headers 'some)
6808             (eq gnus-fetch-old-headers 'invisible)
6809             (numberp gnus-fetch-old-headers)
6810             (eq gnus-build-sparse-threads 'some)
6811             (eq gnus-build-sparse-threads 'more))
6812     (let ((th threads))
6813       (while th
6814         (setcar th (gnus-cut-thread (car th)))
6815         (setq th (cdr th)))))
6816   ;; Remove nixed out threads.
6817   (delq nil threads))
6818
6819 (defun gnus-summary-initial-limit (&optional show-if-empty)
6820   "Figure out what the initial limit is supposed to be on group entry.
6821 This entails weeding out unwanted dormants, low-scored articles,
6822 fetch-old-headers verbiage, and so on."
6823   ;; Most groups have nothing to remove.
6824   (if (or gnus-inhibit-limiting
6825           (and (null gnus-newsgroup-dormant)
6826                (not (eq gnus-fetch-old-headers 'some))
6827                (not (numberp gnus-fetch-old-headers))
6828                (not (eq gnus-fetch-old-headers 'invisible))
6829                (null gnus-summary-expunge-below)
6830                (not (eq gnus-build-sparse-threads 'some))
6831                (not (eq gnus-build-sparse-threads 'more))
6832                (null gnus-thread-expunge-below)
6833                (not gnus-use-nocem)))
6834       ()                                ; Do nothing.
6835     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6836     (setq gnus-newsgroup-limit nil)
6837     (mapatoms
6838      (lambda (node)
6839        (unless (car (symbol-value node))
6840          ;; These threads have no parents -- they are roots.
6841          (let ((nodes (cdr (symbol-value node)))
6842                thread)
6843            (while nodes
6844              (if (and gnus-thread-expunge-below
6845                       (< (gnus-thread-total-score (car nodes))
6846                          gnus-thread-expunge-below))
6847                  (gnus-expunge-thread (pop nodes))
6848                (setq thread (pop nodes))
6849                (gnus-summary-limit-children thread))))))
6850      gnus-newsgroup-dependencies)
6851     ;; If this limitation resulted in an empty group, we might
6852     ;; pop the previous limit and use it instead.
6853     (when (and (not gnus-newsgroup-limit)
6854                show-if-empty)
6855       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6856     gnus-newsgroup-limit))
6857
6858 (defun gnus-summary-limit-children (thread)
6859   "Return 1 if this subthread is visible and 0 if it is not."
6860   ;; First we get the number of visible children to this thread.  This
6861   ;; is done by recursing down the thread using this function, so this
6862   ;; will really go down to a leaf article first, before slowly
6863   ;; working its way up towards the root.
6864   (when thread
6865     (let ((children
6866            (if (cdr thread)
6867                (apply '+ (mapcar 'gnus-summary-limit-children
6868                                  (cdr thread)))
6869              0))
6870           (number (mail-header-number (car thread)))
6871           score)
6872       (if (and
6873            (not (memq number gnus-newsgroup-marked))
6874            (or
6875             ;; If this article is dormant and has absolutely no visible
6876             ;; children, then this article isn't visible.
6877             (and (memq number gnus-newsgroup-dormant)
6878                  (zerop children))
6879             ;; If this is "fetch-old-headered" and there is no
6880             ;; visible children, then we don't want this article.
6881             (and (or (eq gnus-fetch-old-headers 'some)
6882                      (numberp gnus-fetch-old-headers))
6883                  (gnus-summary-article-ancient-p number)
6884                  (zerop children))
6885             ;; If this is "fetch-old-headered" and `invisible', then
6886             ;; we don't want this article.
6887             (and (eq gnus-fetch-old-headers 'invisible)
6888                  (gnus-summary-article-ancient-p number))
6889             ;; If this is a sparsely inserted article with no children,
6890             ;; we don't want it.
6891             (and (eq gnus-build-sparse-threads 'some)
6892                  (gnus-summary-article-sparse-p number)
6893                  (zerop children))
6894             ;; If we use expunging, and this article is really
6895             ;; low-scored, then we don't want this article.
6896             (when (and gnus-summary-expunge-below
6897                        (< (setq score
6898                                 (or (cdr (assq number gnus-newsgroup-scored))
6899                                     gnus-summary-default-score))
6900                           gnus-summary-expunge-below))
6901               ;; We increase the expunge-tally here, but that has
6902               ;; nothing to do with the limits, really.
6903               (incf gnus-newsgroup-expunged-tally)
6904               ;; We also mark as read here, if that's wanted.
6905               (when (and gnus-summary-mark-below
6906                          (< score gnus-summary-mark-below))
6907                 (setq gnus-newsgroup-unreads
6908                       (delq number gnus-newsgroup-unreads))
6909                 (if gnus-newsgroup-auto-expire
6910                     (push number gnus-newsgroup-expirable)
6911                   (push (cons number gnus-low-score-mark)
6912                         gnus-newsgroup-reads)))
6913               t)
6914             ;; Check NoCeM things.
6915             (if (and gnus-use-nocem
6916                      (gnus-nocem-unwanted-article-p
6917                       (mail-header-id (car thread))))
6918                 (progn
6919                   (setq gnus-newsgroup-unreads
6920                         (delq number gnus-newsgroup-unreads))
6921                   t))))
6922           ;; Nope, invisible article.
6923           0
6924         ;; Ok, this article is to be visible, so we add it to the limit
6925         ;; and return 1.
6926         (push number gnus-newsgroup-limit)
6927         1))))
6928
6929 (defun gnus-expunge-thread (thread)
6930   "Mark all articles in THREAD as read."
6931   (let* ((number (mail-header-number (car thread))))
6932     (incf gnus-newsgroup-expunged-tally)
6933     ;; We also mark as read here, if that's wanted.
6934     (setq gnus-newsgroup-unreads
6935           (delq number gnus-newsgroup-unreads))
6936     (if gnus-newsgroup-auto-expire
6937         (push number gnus-newsgroup-expirable)
6938       (push (cons number gnus-low-score-mark)
6939             gnus-newsgroup-reads)))
6940   ;; Go recursively through all subthreads.
6941   (mapcar 'gnus-expunge-thread (cdr thread)))
6942
6943 ;; Summary article oriented commands
6944
6945 (defun gnus-summary-refer-parent-article (n)
6946   "Refer parent article N times.
6947 If N is negative, go to ancestor -N instead.
6948 The difference between N and the number of articles fetched is returned."
6949   (interactive "p")
6950   (let ((skip 1)
6951         error header ref)
6952     (when (not (natnump n))
6953       (setq skip (abs n)
6954             n 1))
6955     (while (and (> n 0)
6956                 (not error))
6957       (setq header (gnus-summary-article-header))
6958       (if (and (eq (mail-header-number header)
6959                    (cdr gnus-article-current))
6960                (equal gnus-newsgroup-name
6961                       (car gnus-article-current)))
6962           ;; If we try to find the parent of the currently
6963           ;; displayed article, then we take a look at the actual
6964           ;; References header, since this is slightly more
6965           ;; reliable than the References field we got from the
6966           ;; server.
6967           (save-excursion
6968             (set-buffer gnus-original-article-buffer)
6969             (nnheader-narrow-to-headers)
6970             (unless (setq ref (message-fetch-field "references"))
6971               (setq ref (message-fetch-field "in-reply-to")))
6972             (widen))
6973         (setq ref
6974               ;; It's not the current article, so we take a bet on
6975               ;; the value we got from the server.
6976               (mail-header-references header)))
6977       (if (and ref
6978                (not (equal ref "")))
6979           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6980             (gnus-message 1 "Couldn't find parent"))
6981         (gnus-message 1 "No references in article %d"
6982                       (gnus-summary-article-number))
6983         (setq error t))
6984       (decf n))
6985     (gnus-summary-position-point)
6986     n))
6987
6988 (defun gnus-summary-refer-references ()
6989   "Fetch all articles mentioned in the References header.
6990 Return the number of articles fetched."
6991   (interactive)
6992   (let ((ref (mail-header-references (gnus-summary-article-header)))
6993         (current (gnus-summary-article-number))
6994         (n 0))
6995     (if (or (not ref)
6996             (equal ref ""))
6997         (error "No References in the current article")
6998       ;; For each Message-ID in the References header...
6999       (while (string-match "<[^>]*>" ref)
7000         (incf n)
7001         ;; ... fetch that article.
7002         (gnus-summary-refer-article
7003          (prog1 (match-string 0 ref)
7004            (setq ref (substring ref (match-end 0))))))
7005       (gnus-summary-goto-subject current)
7006       (gnus-summary-position-point)
7007       n)))
7008
7009 (defun gnus-summary-refer-thread (&optional limit)
7010   "Fetch all articles in the current thread.
7011 If LIMIT (the numerical prefix), fetch that many old headers instead
7012 of what's specified by the `gnus-refer-thread-limit' variable."
7013   (interactive "P")
7014   (let ((id (mail-header-id (gnus-summary-article-header)))
7015         (limit (if limit (prefix-numeric-value limit)
7016                  gnus-refer-thread-limit)))
7017     ;; We want to fetch LIMIT *old* headers, but we also have to
7018     ;; re-fetch all the headers in the current buffer, because many of
7019     ;; them may be undisplayed.  So we adjust LIMIT.
7020     (when (numberp limit)
7021       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7022     (unless (eq gnus-fetch-old-headers 'invisible)
7023       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7024       ;; Retrieve the headers and read them in.
7025       (if (eq (gnus-retrieve-headers
7026                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7027               'nov)
7028           (gnus-build-all-threads)
7029         (error "Can't fetch thread from backends that don't support NOV"))
7030       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7031     (gnus-summary-limit-include-thread id)))
7032
7033 (defun gnus-summary-refer-article (message-id)
7034   "Fetch an article specified by MESSAGE-ID."
7035   (interactive "sMessage-ID: ")
7036   (when (and (stringp message-id)
7037              (not (zerop (length message-id))))
7038     ;; Construct the correct Message-ID if necessary.
7039     ;; Suggested by tale@pawl.rpi.edu.
7040     (unless (string-match "^<" message-id)
7041       (setq message-id (concat "<" message-id)))
7042     (unless (string-match ">$" message-id)
7043       (setq message-id (concat message-id ">")))
7044     (let* ((header (gnus-id-to-header message-id))
7045            (sparse (and header
7046                         (gnus-summary-article-sparse-p
7047                          (mail-header-number header))
7048                         (memq (mail-header-number header)
7049                               gnus-newsgroup-limit)))
7050            number)
7051       (cond
7052        ;; If the article is present in the buffer we just go to it.
7053        ((and header
7054              (or (not (gnus-summary-article-sparse-p
7055                        (mail-header-number header)))
7056                  sparse))
7057         (prog1
7058             (gnus-summary-goto-article
7059              (mail-header-number header) nil t)
7060           (when sparse
7061             (gnus-summary-update-article (mail-header-number header)))))
7062        (t
7063         ;; We fetch the article.
7064         (catch 'found
7065           (dolist (gnus-override-method (gnus-refer-article-methods))
7066             (gnus-check-server gnus-override-method)
7067             ;; Fetch the header, and display the article.
7068             (when (setq number (gnus-summary-insert-subject message-id))
7069               (gnus-summary-select-article nil nil nil number)
7070               (throw 'found t)))
7071           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7072
7073 (defun gnus-refer-article-methods ()
7074   "Return a list of referrable methods."
7075   (cond
7076    ;; No method, so we default to current and native.
7077    ((null gnus-refer-article-method)
7078     (list gnus-current-select-method gnus-select-method))
7079    ;; Current.
7080    ((eq 'current gnus-refer-article-method)
7081     (list gnus-current-select-method))
7082    ;; List of select methods.
7083    ((not (and (symbolp (car gnus-refer-article-method))
7084               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7085     (let (out)
7086       (dolist (method gnus-refer-article-method)
7087         (push (if (eq 'current method)
7088                   gnus-current-select-method
7089                 method)
7090               out))
7091       (nreverse out)))
7092    ;; One single select method.
7093    (t
7094     (list gnus-refer-article-method))))
7095
7096 (defun gnus-summary-edit-parameters ()
7097   "Edit the group parameters of the current group."
7098   (interactive)
7099   (gnus-group-edit-group gnus-newsgroup-name 'params))
7100
7101 (defun gnus-summary-customize-parameters ()
7102   "Customize the group parameters of the current group."
7103   (interactive)
7104   (gnus-group-customize gnus-newsgroup-name))
7105
7106 (defun gnus-summary-enter-digest-group (&optional force)
7107   "Enter an nndoc group based on the current article.
7108 If FORCE, force a digest interpretation.  If not, try
7109 to guess what the document format is."
7110   (interactive "P")
7111   (let ((conf gnus-current-window-configuration))
7112     (save-excursion
7113       (gnus-summary-select-article))
7114     (setq gnus-current-window-configuration conf)
7115     (let* ((name (format "%s-%d"
7116                          (gnus-group-prefixed-name
7117                           gnus-newsgroup-name (list 'nndoc ""))
7118                          (save-excursion
7119                            (set-buffer gnus-summary-buffer)
7120                            gnus-current-article)))
7121            (ogroup gnus-newsgroup-name)
7122            (params (append (gnus-info-params (gnus-get-info ogroup))
7123                            (list (cons 'to-group ogroup))
7124                            (list (cons 'save-article-group ogroup))))
7125            (case-fold-search t)
7126            (buf (current-buffer))
7127            dig to-address)
7128       (save-excursion
7129         (set-buffer gnus-original-article-buffer)
7130         ;; Have the digest group inherit the main mail address of
7131         ;; the parent article.
7132         (when (setq to-address (or (message-fetch-field "reply-to")
7133                                    (message-fetch-field "from")))
7134           (setq params (append 
7135                         (list (cons 'to-address 
7136                                     (funcall gnus-decode-encoded-word-function
7137                                              to-address))))))
7138         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7139         (insert-buffer-substring gnus-original-article-buffer)
7140         ;; Remove lines that may lead nndoc to misinterpret the
7141         ;; document type.
7142         (narrow-to-region
7143          (goto-char (point-min))
7144          (or (search-forward "\n\n" nil t) (point)))
7145         (goto-char (point-min))
7146         (delete-matching-lines "^Path:\\|^From ")
7147         (widen))
7148       (unwind-protect
7149           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7150                     (gnus-newsgroup-ephemeral-ignored-charsets
7151                      gnus-newsgroup-ignored-charsets))
7152                 (gnus-group-read-ephemeral-group
7153                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7154                               (nndoc-article-type
7155                                ,(if force 'mbox 'guess))) t))
7156               ;; Make all postings to this group go to the parent group.
7157               (nconc (gnus-info-params (gnus-get-info name))
7158                      params)
7159             ;; Couldn't select this doc group.
7160             (switch-to-buffer buf)
7161             (gnus-set-global-variables)
7162             (gnus-configure-windows 'summary)
7163             (gnus-message 3 "Article couldn't be entered?"))
7164         (kill-buffer dig)))))
7165
7166 (defun gnus-summary-read-document (n)
7167   "Open a new group based on the current article(s).
7168 This will allow you to read digests and other similar
7169 documents as newsgroups.
7170 Obeys the standard process/prefix convention."
7171   (interactive "P")
7172   (let* ((articles (gnus-summary-work-articles n))
7173          (ogroup gnus-newsgroup-name)
7174          (params (append (gnus-info-params (gnus-get-info ogroup))
7175                          (list (cons 'to-group ogroup))))
7176          article group egroup groups vgroup)
7177     (while (setq article (pop articles))
7178       (setq group (format "%s-%d" gnus-newsgroup-name article))
7179       (gnus-summary-remove-process-mark article)
7180       (when (gnus-summary-display-article article)
7181         (save-excursion
7182           (with-temp-buffer
7183             (insert-buffer-substring gnus-original-article-buffer)
7184             ;; Remove some headers that may lead nndoc to make
7185             ;; the wrong guess.
7186             (message-narrow-to-head)
7187             (goto-char (point-min))
7188             (delete-matching-lines "^\\(Path\\):\\|^From ")
7189             (widen)
7190             (if (setq egroup
7191                       (gnus-group-read-ephemeral-group
7192                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7193                                      (nndoc-article-type guess))
7194                        t nil t))
7195                 (progn
7196                   ;; Make all postings to this group go to the parent group.
7197                   (nconc (gnus-info-params (gnus-get-info egroup))
7198                          params)
7199                   (push egroup groups))
7200               ;; Couldn't select this doc group.
7201               (gnus-error 3 "Article couldn't be entered"))))))
7202     ;; Now we have selected all the documents.
7203     (cond
7204      ((not groups)
7205       (error "None of the articles could be interpreted as documents"))
7206      ((gnus-group-read-ephemeral-group
7207        (setq vgroup (format
7208                      "nnvirtual:%s-%s" gnus-newsgroup-name
7209                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7210        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7211        t
7212        (cons (current-buffer) 'summary)))
7213      (t
7214       (error "Couldn't select virtual nndoc group")))))
7215
7216 (defun gnus-summary-isearch-article (&optional regexp-p)
7217   "Do incremental search forward on the current article.
7218 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7219   (interactive "P")
7220   (let* ((gnus-inhibit-treatment t)
7221          (old (gnus-summary-select-article)))
7222     (gnus-configure-windows 'article)
7223     (gnus-eval-in-buffer-window gnus-article-buffer
7224       (save-restriction
7225         (widen)
7226         (when (eq 'old old)
7227           (gnus-article-show-all-headers))
7228         (goto-char (point-min))
7229         (isearch-forward regexp-p)))))
7230
7231 (defun gnus-summary-search-article-forward (regexp &optional backward)
7232   "Search for an article containing REGEXP forward.
7233 If BACKWARD, search backward instead."
7234   (interactive
7235    (list (read-string
7236           (format "Search article %s (regexp%s): "
7237                   (if current-prefix-arg "backward" "forward")
7238                   (if gnus-last-search-regexp
7239                       (concat ", default " gnus-last-search-regexp)
7240                     "")))
7241          current-prefix-arg))
7242   (if (string-equal regexp "")
7243       (setq regexp (or gnus-last-search-regexp ""))
7244     (setq gnus-last-search-regexp regexp)
7245     (setq gnus-article-before-search gnus-current-article))
7246   ;; Intentionally set gnus-last-article.
7247   (setq gnus-last-article gnus-article-before-search)
7248   (let ((gnus-last-article gnus-last-article))
7249     (if (gnus-summary-search-article regexp backward)
7250         (gnus-summary-show-thread)
7251       (error "Search failed: \"%s\"" regexp))))
7252
7253 (defun gnus-summary-search-article-backward (regexp)
7254   "Search for an article containing REGEXP backward."
7255   (interactive
7256    (list (read-string
7257           (format "Search article backward (regexp%s): "
7258                   (if gnus-last-search-regexp
7259                       (concat ", default " gnus-last-search-regexp)
7260                     "")))))
7261   (gnus-summary-search-article-forward regexp 'backward))
7262
7263 (eval-when-compile
7264   (defmacro gnus-summary-search-article-position-point (regexp backward)
7265     "Dehighlight the last matched text and goto the beginning position."
7266     (` (if (and gnus-summary-search-article-matched-data
7267                 (let ((text (caddr gnus-summary-search-article-matched-data))
7268                       (inhibit-read-only t)
7269                       buffer-read-only)
7270                   (delete-region
7271                    (goto-char (car gnus-summary-search-article-matched-data))
7272                    (cadr gnus-summary-search-article-matched-data))
7273                   (insert text)
7274                   (string-match (, regexp) text)))
7275            (if (, backward) (beginning-of-line) (end-of-line))
7276          (goto-char (if (, backward) (point-max) (point-min))))))
7277
7278   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7279     "Place point where X-Face image is displayed."
7280     (if (featurep 'xemacs)
7281         (` (let ((end (if (search-forward "\n\n" nil t)
7282                           (goto-char (1- (point)))
7283                         (point-min)))
7284                  extent)
7285              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7286              (unless (and (re-search-forward "^From:" end t)
7287                           (setq extent (extent-at (point)))
7288                           (extent-begin-glyph extent))
7289                (goto-char (, opoint)))))
7290       (` (let ((end (if (search-forward "\n\n" nil t)
7291                         (goto-char (1- (point)))
7292                       (point-min)))
7293                (start (or (search-backward "\n\n" nil t) (point-min))))
7294            (goto-char
7295             (or (text-property-any start end 'x-face-image t);; x-face-e21
7296                 (text-property-any start end 'x-face-mule-bitmap-image t)
7297                 (, opoint)))))))
7298
7299   (defmacro gnus-summary-search-article-highlight-matched-text
7300     (backward treated x-face)
7301     "Highlight matched text in the function `gnus-summary-search-article'."
7302     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7303              (end (set-marker (make-marker) (match-end 0)))
7304              (inhibit-read-only t)
7305              buffer-read-only)
7306          (unless treated
7307            (let ((,@
7308                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7309                     (mapcar
7310                      (lambda (item) (setq items (delq item items)))
7311                      '(gnus-treat-buttonize
7312                        gnus-treat-fill-article
7313                        gnus-treat-fill-long-lines
7314                        gnus-treat-emphasize
7315                        gnus-treat-highlight-headers
7316                        gnus-treat-highlight-citation
7317                        gnus-treat-highlight-signature
7318                        gnus-treat-overstrike
7319                        gnus-treat-display-xface
7320                        gnus-treat-buttonize-head
7321                        gnus-treat-decode-article-as-default-mime-charset))
7322                     (static-if (featurep 'xemacs)
7323                         items
7324                       (cons '(x-face-mule-delete-x-face-field
7325                               (quote never))
7326                             items))))
7327                  (gnus-treat-display-xface
7328                   (when (, x-face) gnus-treat-display-xface)))
7329              (gnus-article-prepare-mime-display)))
7330          (goto-char (if (, backward) start end))
7331          (when (, x-face)
7332            (gnus-summary-search-article-highlight-goto-x-face (point)))
7333          (setq gnus-summary-search-article-matched-data
7334                (list start end (buffer-substring start end)))
7335          (unless (eq start end);; matched text has been deleted. :-<
7336            (put-text-property start end 'face
7337                               (or (find-face 'isearch)
7338                                   'secondary-selection))))))
7339   )
7340
7341 (defun gnus-summary-search-article (regexp &optional backward)
7342   "Search for an article containing REGEXP.
7343 Optional argument BACKWARD means do search for backward.
7344 `gnus-select-article-hook' is not called during the search."
7345   ;; We have to require this here to make sure that the following
7346   ;; dynamic binding isn't shadowed by autoloading.
7347   (require 'gnus-async)
7348   (require 'gnus-art)
7349   (let ((gnus-select-article-hook nil)  ;Disable hook.
7350         (gnus-article-display-hook nil)
7351         (gnus-article-prepare-hook nil)
7352         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7353         (gnus-use-article-prefetch nil)
7354         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7355         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7356         (sum (current-buffer))
7357         (found nil)
7358         point treated)
7359     (gnus-save-hidden-threads
7360       (static-if (featurep 'xemacs)
7361           (let ((gnus-inhibit-treatment t))
7362             (setq treated (eq 'old (gnus-summary-select-article)))
7363             (when (and treated
7364                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7365                                  (window-live-p (get-buffer-window
7366                                                  gnus-article-buffer t)))))
7367               (gnus-summary-select-article nil t)
7368               (setq treated nil)))
7369         (let ((gnus-inhibit-treatment t)
7370               (x-face-mule-delete-x-face-field 'never))
7371           (setq treated (eq 'old (gnus-summary-select-article)))
7372           (when (and treated
7373                      (not
7374                       (and (gnus-buffer-live-p gnus-article-buffer)
7375                            (window-live-p (get-buffer-window
7376                                            gnus-article-buffer t))
7377                            (or (not (string-match "^\\^X-Face:" regexp))
7378                                (with-current-buffer gnus-article-buffer
7379                                  gnus-summary-search-article-matched-data)))))
7380             (gnus-summary-select-article nil t)
7381             (setq treated nil))))
7382       (set-buffer gnus-article-buffer)
7383       (widen)
7384       (if treated
7385           (progn
7386             (gnus-article-show-all-headers)
7387             (gnus-summary-search-article-position-point regexp backward))
7388         (goto-char (if backward (point-max) (point-min))))
7389       (while (not found)
7390         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7391         (if (if backward
7392                 (re-search-backward regexp nil t)
7393               (re-search-forward regexp nil t))
7394             ;; We found the regexp.
7395             (progn
7396               (gnus-summary-search-article-highlight-matched-text
7397                backward treated (string-match "^\\^X-Face:" regexp))
7398               (setq found 'found)
7399               (forward-line
7400                (/ (- 2 (window-height
7401                         (get-buffer-window gnus-article-buffer t)))
7402                   2))
7403               (set-window-start
7404                (get-buffer-window (current-buffer))
7405                (point))
7406               (set-buffer sum)
7407               (setq point (point)))
7408           ;; We didn't find it, so we go to the next article.
7409           (set-buffer sum)
7410           (setq found 'not)
7411           (while (eq found 'not)
7412             (if (not (if backward (gnus-summary-find-prev)
7413                        (gnus-summary-find-next)))
7414                 ;; No more articles.
7415                 (setq found t)
7416               ;; Select the next article and adjust point.
7417               (unless (gnus-summary-article-sparse-p
7418                        (gnus-summary-article-number))
7419                 (setq found nil)
7420                 (let ((gnus-inhibit-treatment t))
7421                   (gnus-summary-select-article))
7422                 (setq treated nil)
7423                 (set-buffer gnus-article-buffer)
7424                 (widen)
7425                 (goto-char (if backward (point-max) (point-min))))))))
7426       (gnus-message 7 ""))
7427     ;; Return whether we found the regexp.
7428     (when (eq found 'found)
7429       (goto-char point)
7430       (gnus-summary-show-thread)
7431       (gnus-summary-goto-subject gnus-current-article)
7432       (gnus-summary-position-point)
7433       t)))
7434
7435 (defun gnus-summary-find-matching (header regexp &optional backward unread
7436                                           not-case-fold)
7437   "Return a list of all articles that match REGEXP on HEADER.
7438 The search stars on the current article and goes forwards unless
7439 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7440 If UNREAD is non-nil, only unread articles will
7441 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7442 in the comparisons."
7443   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7444                 (gnus-data-find-list
7445                  (gnus-summary-article-number) (gnus-data-list backward))))
7446         (case-fold-search (not not-case-fold))
7447         articles d func)
7448     (if (consp header)
7449         (if (eq (car header) 'extra)
7450             (setq func
7451                   `(lambda (h)
7452                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7453                          "")))
7454           (error "%s is an invalid header" header))
7455       (unless (fboundp (intern (concat "mail-header-" header)))
7456         (error "%s is not a valid header" header))
7457       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7458     (while data
7459       (setq d (car data))
7460       (and (or (not unread)             ; We want all articles...
7461                (gnus-data-unread-p d))  ; Or just unreads.
7462            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7463            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7464            (push (gnus-data-number d) articles)) ; Success!
7465       (setq data (cdr data)))
7466     (nreverse articles)))
7467
7468 (defun gnus-summary-execute-command (header regexp command &optional backward)
7469   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7470 If HEADER is an empty string (or nil), the match is done on the entire
7471 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7472   (interactive
7473    (list (let ((completion-ignore-case t))
7474            (completing-read
7475             "Header name: "
7476             (mapcar (lambda (string) (list string))
7477                     '("Number" "Subject" "From" "Lines" "Date"
7478                       "Message-ID" "Xref" "References" "Body"))
7479             nil 'require-match))
7480          (read-string "Regexp: ")
7481          (read-key-sequence "Command: ")
7482          current-prefix-arg))
7483   (when (equal header "Body")
7484     (setq header ""))
7485   ;; Hidden thread subtrees must be searched as well.
7486   (gnus-summary-show-all-threads)
7487   ;; We don't want to change current point nor window configuration.
7488   (save-excursion
7489     (save-window-excursion
7490       (gnus-message 6 "Executing %s..." (key-description command))
7491       ;; We'd like to execute COMMAND interactively so as to give arguments.
7492       (gnus-execute header regexp
7493                     `(call-interactively ',(key-binding command))
7494                     backward)
7495       (gnus-message 6 "Executing %s...done" (key-description command)))))
7496
7497 (defun gnus-summary-beginning-of-article ()
7498   "Scroll the article back to the beginning."
7499   (interactive)
7500   (gnus-summary-select-article)
7501   (gnus-configure-windows 'article)
7502   (gnus-eval-in-buffer-window gnus-article-buffer
7503     (widen)
7504     (goto-char (point-min))
7505     (when gnus-page-broken
7506       (gnus-narrow-to-page))))
7507
7508 (defun gnus-summary-end-of-article ()
7509   "Scroll to the end of the article."
7510   (interactive)
7511   (gnus-summary-select-article)
7512   (gnus-configure-windows 'article)
7513   (gnus-eval-in-buffer-window gnus-article-buffer
7514     (widen)
7515     (goto-char (point-max))
7516     (recenter -3)
7517     (when gnus-page-broken
7518       (gnus-narrow-to-page))))
7519
7520 (defun gnus-summary-print-article (&optional filename n)
7521   "Generate and print a PostScript image of the N next (mail) articles.
7522
7523 If N is negative, print the N previous articles.  If N is nil and articles
7524 have been marked with the process mark, print these instead.
7525
7526 If the optional first argument FILENAME is nil, send the image to the
7527 printer.  If FILENAME is a string, save the PostScript image in a file with
7528 that name.  If FILENAME is a number, prompt the user for the name of the file
7529 to save in."
7530   (interactive (list (ps-print-preprint current-prefix-arg)
7531                      current-prefix-arg))
7532   (dolist (article (gnus-summary-work-articles n))
7533     (gnus-summary-select-article nil nil 'pseudo article)
7534     (gnus-eval-in-buffer-window gnus-article-buffer
7535       (let ((buffer (generate-new-buffer " *print*")))
7536         (unwind-protect
7537             (progn
7538               (copy-to-buffer buffer (point-min) (point-max))
7539               (set-buffer buffer)
7540               (gnus-article-delete-invisible-text)
7541               (let ((ps-left-header
7542                      (list
7543                       (concat "("
7544                               (mail-header-subject gnus-current-headers) ")")
7545                       (concat "("
7546                               (mail-header-from gnus-current-headers) ")")))
7547                     (ps-right-header
7548                      (list
7549                       "/pagenumberstring load"
7550                       (concat "("
7551                               (mail-header-date gnus-current-headers) ")"))))
7552                 (gnus-run-hooks 'gnus-ps-print-hook)
7553                 (save-excursion
7554                   (ps-print-buffer-with-faces filename))))
7555           (kill-buffer buffer))))))
7556
7557 (defun gnus-summary-show-article (&optional arg)
7558   "Force re-fetching of the current article.
7559 If ARG (the prefix) is a number, show the article with the charset
7560 defined in `gnus-summary-show-article-charset-alist', or the charset
7561 inputed.
7562 If ARG (the prefix) is non-nil and not a number, show the raw article
7563 without any article massaging functions being run."
7564   (interactive "P")
7565   (cond
7566    ((numberp arg)
7567     (let ((gnus-newsgroup-charset
7568            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7569                (read-coding-system "Charset: ")))
7570           (gnus-newsgroup-ignored-charsets 'gnus-all))
7571       (gnus-summary-select-article nil 'force)
7572       (let ((deps gnus-newsgroup-dependencies)
7573             head header)
7574         (save-excursion
7575           (set-buffer gnus-original-article-buffer)
7576           (save-restriction
7577             (message-narrow-to-head)
7578             (setq head (buffer-string)))
7579           (with-temp-buffer
7580             (insert (format "211 %d Article retrieved.\n"
7581                             (cdr gnus-article-current)))
7582             (insert head)
7583             (insert ".\n")
7584             (let ((nntp-server-buffer (current-buffer)))
7585               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7586         (gnus-data-set-header
7587          (gnus-data-find (cdr gnus-article-current))
7588          header)
7589         (gnus-summary-update-article-line
7590          (cdr gnus-article-current) header))))
7591    ((not arg)
7592     ;; Select the article the normal way.
7593     (gnus-summary-select-article nil 'force))
7594    (t
7595     ;; We have to require this here to make sure that the following
7596     ;; dynamic binding isn't shadowed by autoloading.
7597     (require 'gnus-async)
7598     (require 'gnus-art)
7599     ;; Bind the article treatment functions to nil.
7600     (let ((gnus-have-all-headers t)
7601           gnus-article-display-hook
7602           gnus-article-prepare-hook
7603           gnus-article-decode-hook
7604           gnus-break-pages
7605           gnus-show-mime)
7606       (gnus-summary-select-article nil 'force))))
7607   (gnus-summary-goto-subject gnus-current-article)
7608   (gnus-summary-position-point))
7609
7610 (defun gnus-summary-verbose-headers (&optional arg)
7611   "Toggle permanent full header display.
7612 If ARG is a positive number, turn header display on.
7613 If ARG is a negative number, turn header display off."
7614   (interactive "P")
7615   (setq gnus-show-all-headers
7616         (cond ((or (not (numberp arg))
7617                    (zerop arg))
7618                (not gnus-show-all-headers))
7619               ((natnump arg)
7620                t)))
7621   (gnus-summary-show-article))
7622
7623 (defun gnus-summary-toggle-header (&optional arg)
7624   "Show the headers if they are hidden, or hide them if they are shown.
7625 If ARG is a positive number, show the entire header.
7626 If ARG is a negative number, hide the unwanted header lines."
7627   (interactive "P")
7628   (save-excursion
7629     (set-buffer gnus-article-buffer)
7630     (save-restriction
7631       (let* ((buffer-read-only nil)
7632              (inhibit-point-motion-hooks t)
7633              hidden e)
7634         (setq hidden
7635               (if (numberp arg)
7636                   (>= arg 0)
7637                 (save-restriction
7638                   (article-narrow-to-head)
7639                   (gnus-article-hidden-text-p 'headers))))
7640         (goto-char (point-min))
7641         (when (search-forward "\n\n" nil t)
7642           (delete-region (point-min) (1- (point))))
7643         (goto-char (point-min))
7644         (save-excursion
7645           (set-buffer gnus-original-article-buffer)
7646           (goto-char (point-min))
7647           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7648         (insert-buffer-substring gnus-original-article-buffer 1 e)
7649         (save-restriction
7650           (narrow-to-region (point-min) (point))
7651           (article-decode-encoded-words)
7652           (if  hidden
7653               (let ((gnus-treat-hide-headers nil)
7654                     (gnus-treat-hide-boring-headers nil))
7655                 (setq gnus-article-wash-types
7656                       (delq 'headers gnus-article-wash-types))
7657                 (gnus-treat-article 'head))
7658             (gnus-treat-article 'head)))
7659         (gnus-set-mode-line 'article)))))
7660
7661 (defun gnus-summary-show-all-headers ()
7662   "Make all header lines visible."
7663   (interactive)
7664   (gnus-article-show-all-headers))
7665
7666 (defun gnus-summary-toggle-mime (&optional arg)
7667   "Toggle MIME processing.
7668 If ARG is a positive number, turn MIME processing on."
7669   (interactive "P")
7670   (setq gnus-show-mime
7671         (if (null arg)
7672             (not gnus-show-mime)
7673           (> (prefix-numeric-value arg) 0)))
7674   (gnus-summary-select-article t 'force))
7675
7676 (defun gnus-summary-caesar-message (&optional arg)
7677   "Caesar rotate the current article by 13.
7678 The numerical prefix specifies how many places to rotate each letter
7679 forward."
7680   (interactive "P")
7681   (gnus-summary-select-article)
7682   (let ((mail-header-separator ""))
7683     (gnus-eval-in-buffer-window gnus-article-buffer
7684       (save-restriction
7685         (widen)
7686         (let ((start (window-start))
7687               buffer-read-only)
7688           (message-caesar-buffer-body arg)
7689           (set-window-start (get-buffer-window (current-buffer)) start))))))
7690
7691 (defun gnus-summary-stop-page-breaking ()
7692   "Stop page breaking in the current article."
7693   (interactive)
7694   (gnus-summary-select-article)
7695   (gnus-eval-in-buffer-window gnus-article-buffer
7696     (widen)
7697     (when (gnus-visual-p 'page-marker)
7698       (let ((buffer-read-only nil))
7699         (gnus-remove-text-with-property 'gnus-prev)
7700         (gnus-remove-text-with-property 'gnus-next))
7701       (setq gnus-page-broken nil))))
7702
7703 (defun gnus-summary-move-article (&optional n to-newsgroup
7704                                             select-method action)
7705   "Move the current article to a different newsgroup.
7706 If N is a positive number, move the N next articles.
7707 If N is a negative number, move the N previous articles.
7708 If N is nil and any articles have been marked with the process mark,
7709 move those articles instead.
7710 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7711 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7712 re-spool using this method.
7713
7714 For this function to work, both the current newsgroup and the
7715 newsgroup that you want to move to have to support the `request-move'
7716 and `request-accept' functions.
7717
7718 ACTION can be either `move' (the default), `crosspost' or `copy'."
7719   (interactive "P")
7720   (unless action
7721     (setq action 'move))
7722   ;; Disable marking as read.
7723   (let (gnus-mark-article-hook)
7724     (save-window-excursion
7725       (gnus-summary-select-article)))
7726   ;; Check whether the source group supports the required functions.
7727   (cond ((and (eq action 'move)
7728               (not (gnus-check-backend-function
7729                     'request-move-article gnus-newsgroup-name)))
7730          (error "The current group does not support article moving"))
7731         ((and (eq action 'crosspost)
7732               (not (gnus-check-backend-function
7733                     'request-replace-article gnus-newsgroup-name)))
7734          (error "The current group does not support article editing")))
7735   (let ((articles (gnus-summary-work-articles n))
7736         (prefix (if (gnus-check-backend-function
7737                     'request-move-article gnus-newsgroup-name)
7738                     (gnus-group-real-prefix gnus-newsgroup-name)
7739                   ""))
7740         (names '((move "Move" "Moving")
7741                  (copy "Copy" "Copying")
7742                  (crosspost "Crosspost" "Crossposting")))
7743         (copy-buf (save-excursion
7744                     (nnheader-set-temp-buffer " *copy article*")))
7745         (default-marks gnus-article-mark-lists)
7746         (no-expire-marks (delete '(expirable . expire)
7747                                  (copy-sequence gnus-article-mark-lists)))
7748         art-group to-method new-xref article to-groups)
7749     (unless (assq action names)
7750       (error "Unknown action %s" action))
7751     ;; Read the newsgroup name.
7752     (when (and (not to-newsgroup)
7753                (not select-method))
7754       (setq to-newsgroup
7755             (gnus-read-move-group-name
7756              (cadr (assq action names))
7757              (symbol-value (intern (format "gnus-current-%s-group" action)))
7758              articles prefix))
7759       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7760     (setq to-method (or select-method
7761                         (gnus-server-to-method
7762                          (gnus-group-method to-newsgroup))))
7763     ;; Check the method we are to move this article to...
7764     (unless (gnus-check-backend-function
7765              'request-accept-article (car to-method))
7766       (error "%s does not support article copying" (car to-method)))
7767     (unless (gnus-check-server to-method)
7768       (error "Can't open server %s" (car to-method)))
7769     (gnus-message 6 "%s to %s: %s..."
7770                   (caddr (assq action names))
7771                   (or (car select-method) to-newsgroup) articles)
7772     (while articles
7773       (setq article (pop articles))
7774       (setq
7775        art-group
7776        (cond
7777         ;; Move the article.
7778         ((eq action 'move)
7779          ;; Remove this article from future suppression.
7780          (gnus-dup-unsuppress-article article)
7781          (gnus-request-move-article
7782           article                       ; Article to move
7783           gnus-newsgroup-name           ; From newsgroup
7784           (nth 1 (gnus-find-method-for-group
7785                   gnus-newsgroup-name)) ; Server
7786           (list 'gnus-request-accept-article
7787                 to-newsgroup (list 'quote select-method)
7788                 (not articles) t)       ; Accept form
7789           (not articles)))              ; Only save nov last time
7790         ;; Copy the article.
7791         ((eq action 'copy)
7792          (save-excursion
7793            (set-buffer copy-buf)
7794            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7795              (gnus-request-accept-article
7796               to-newsgroup select-method (not articles) t))))
7797         ;; Crosspost the article.
7798         ((eq action 'crosspost)
7799          (let ((xref (message-tokenize-header
7800                       (mail-header-xref (gnus-summary-article-header article))
7801                       " ")))
7802            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7803                                   ":" article))
7804            (unless xref
7805              (setq xref (list (system-name))))
7806            (setq new-xref
7807                  (concat
7808                   (mapconcat 'identity
7809                              (delete "Xref:" (delete new-xref xref))
7810                              " ")
7811                   " " new-xref))
7812            (save-excursion
7813              (set-buffer copy-buf)
7814              ;; First put the article in the destination group.
7815              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7816              (when (consp (setq art-group
7817                                 (gnus-request-accept-article
7818                                  to-newsgroup select-method (not articles))))
7819                (setq new-xref (concat new-xref " " (car art-group)
7820                                       ":" (cdr art-group)))
7821                ;; Now we have the new Xrefs header, so we insert
7822                ;; it and replace the new article.
7823                (nnheader-replace-header "Xref" new-xref)
7824                (gnus-request-replace-article
7825                 (cdr art-group) to-newsgroup (current-buffer))
7826                art-group))))))
7827       (cond
7828        ((not art-group)
7829         (gnus-message 1 "Couldn't %s article %s: %s"
7830                       (cadr (assq action names)) article
7831                       (nnheader-get-report (car to-method))))
7832        ((eq art-group 'junk)
7833         (when (eq action 'move)
7834           (gnus-summary-mark-article article gnus-canceled-mark)
7835           (gnus-message 4 "Deleted article %s" article)))
7836        (t
7837         (let* ((pto-group (gnus-group-prefixed-name
7838                            (car art-group) to-method))
7839                (entry
7840                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7841                (info (nth 2 entry))
7842                (to-group (gnus-info-group info))
7843                to-marks)
7844           ;; Update the group that has been moved to.
7845           (when (and info
7846                      (memq action '(move copy)))
7847             (unless (member to-group to-groups)
7848               (push to-group to-groups))
7849
7850             (unless (memq article gnus-newsgroup-unreads)
7851               (push 'read to-marks)
7852               (gnus-info-set-read
7853                info (gnus-add-to-range (gnus-info-read info)
7854                                        (list (cdr art-group)))))
7855
7856             ;; See whether the article is to be put in the cache.
7857             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7858                              default-marks
7859                            no-expire-marks))
7860                   (to-article (cdr art-group)))
7861
7862               ;; Enter the article into the cache in the new group,
7863               ;; if that is required.
7864               (when gnus-use-cache
7865                 (gnus-cache-possibly-enter-article
7866                  to-group to-article
7867                  (let ((header (copy-sequence
7868                                 (gnus-summary-article-header article))))
7869                    (mail-header-set-number header to-article)
7870                    header)
7871                  (memq article gnus-newsgroup-marked)
7872                  (memq article gnus-newsgroup-dormant)
7873                  (memq article gnus-newsgroup-unreads)))
7874
7875               (when gnus-preserve-marks
7876                 ;; Copy any marks over to the new group.
7877                 (when (and (equal to-group gnus-newsgroup-name)
7878                            (not (memq article gnus-newsgroup-unreads)))
7879                   ;; Mark this article as read in this group.
7880                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7881                   (setcdr (gnus-active to-group) to-article)
7882                   (setcdr gnus-newsgroup-active to-article))
7883
7884                 (while marks
7885                   (when (memq article (symbol-value
7886                                        (intern (format "gnus-newsgroup-%s"
7887                                                        (caar marks)))))
7888                     (push (cdar marks) to-marks)
7889                     ;; If the other group is the same as this group,
7890                     ;; then we have to add the mark to the list.
7891                     (when (equal to-group gnus-newsgroup-name)
7892                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7893                            (cons to-article
7894                                  (symbol-value
7895                                   (intern (format "gnus-newsgroup-%s"
7896                                                   (caar marks)))))))
7897                     ;; Copy the marks to other group.
7898                     (gnus-add-marked-articles
7899                      to-group (cdar marks) (list to-article) info))
7900                   (setq marks (cdr marks)))
7901
7902                 (gnus-request-set-mark to-group (list (list (list to-article)
7903                                                             'set
7904                                                             to-marks))))
7905
7906               (gnus-dribble-enter
7907                (concat "(gnus-group-set-info '"
7908                        (gnus-prin1-to-string (gnus-get-info to-group))
7909                        ")"))))
7910
7911           ;; Update the Xref header in this article to point to
7912           ;; the new crossposted article we have just created.
7913           (when (eq action 'crosspost)
7914             (save-excursion
7915               (set-buffer copy-buf)
7916               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7917               (nnheader-replace-header "Xref" new-xref)
7918               (gnus-request-replace-article
7919                article gnus-newsgroup-name (current-buffer)))))
7920
7921         ;;;!!!Why is this necessary?
7922         (set-buffer gnus-summary-buffer)
7923
7924         (gnus-summary-goto-subject article)
7925         (when (eq action 'move)
7926           (gnus-summary-mark-article article gnus-canceled-mark))))
7927       (gnus-summary-remove-process-mark article))
7928     ;; Re-activate all groups that have been moved to.
7929     (while to-groups
7930       (save-excursion
7931         (set-buffer gnus-group-buffer)
7932         (when (gnus-group-goto-group (car to-groups) t)
7933           (gnus-group-get-new-news-this-group 1 t))
7934         (pop to-groups)))
7935
7936     (gnus-kill-buffer copy-buf)
7937     (gnus-summary-position-point)
7938     (gnus-set-mode-line 'summary)))
7939
7940 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7941   "Move the current article to a different newsgroup.
7942 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7943 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7944 re-spool using this method."
7945   (interactive "P")
7946   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7947
7948 (defun gnus-summary-crosspost-article (&optional n)
7949   "Crosspost the current article to some other group."
7950   (interactive "P")
7951   (gnus-summary-move-article n nil nil 'crosspost))
7952
7953 (defcustom gnus-summary-respool-default-method nil
7954   "Default method for respooling an article.
7955 If nil, use to the current newsgroup method."
7956   :type '(choice (gnus-select-method :value (nnml ""))
7957                  (const nil))
7958   :group 'gnus-summary-mail)
7959
7960 (defun gnus-summary-respool-article (&optional n method)
7961   "Respool the current article.
7962 The article will be squeezed through the mail spooling process again,
7963 which means that it will be put in some mail newsgroup or other
7964 depending on `nnmail-split-methods'.
7965 If N is a positive number, respool the N next articles.
7966 If N is a negative number, respool the N previous articles.
7967 If N is nil and any articles have been marked with the process mark,
7968 respool those articles instead.
7969
7970 Respooling can be done both from mail groups and \"real\" newsgroups.
7971 In the former case, the articles in question will be moved from the
7972 current group into whatever groups they are destined to.  In the
7973 latter case, they will be copied into the relevant groups."
7974   (interactive
7975    (list current-prefix-arg
7976          (let* ((methods (gnus-methods-using 'respool))
7977                 (methname
7978                  (symbol-name (or gnus-summary-respool-default-method
7979                                   (car (gnus-find-method-for-group
7980                                         gnus-newsgroup-name)))))
7981                 (method
7982                  (gnus-completing-read
7983                   methname "What backend do you want to use when respooling?"
7984                   methods nil t nil 'gnus-mail-method-history))
7985                 ms)
7986            (cond
7987             ((zerop (length (setq ms (gnus-servers-using-backend
7988                                       (intern method)))))
7989              (list (intern method) ""))
7990             ((= 1 (length ms))
7991              (car ms))
7992             (t
7993              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7994                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7995                            ms-alist))))))))
7996   (unless method
7997     (error "No method given for respooling"))
7998   (if (assoc (symbol-name
7999               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8000              (gnus-methods-using 'respool))
8001       (gnus-summary-move-article n nil method)
8002     (gnus-summary-copy-article n nil method)))
8003
8004 (defun gnus-summary-import-article (file)
8005   "Import an arbitrary file into a mail newsgroup."
8006   (interactive "fImport file: ")
8007   (let ((group gnus-newsgroup-name)
8008         (now (current-time))
8009         atts lines)
8010     (unless (gnus-check-backend-function 'request-accept-article group)
8011       (error "%s does not support article importing" group))
8012     (or (file-readable-p file)
8013         (not (file-regular-p file))
8014         (error "Can't read %s" file))
8015     (save-excursion
8016       (set-buffer (gnus-get-buffer-create " *import file*"))
8017       (erase-buffer)
8018       (nnheader-insert-file-contents file)
8019       (goto-char (point-min))
8020       (unless (nnheader-article-p)
8021         ;; This doesn't look like an article, so we fudge some headers.
8022         (setq atts (file-attributes file)
8023               lines (count-lines (point-min) (point-max)))
8024         (insert "From: " (read-string "From: ") "\n"
8025                 "Subject: " (read-string "Subject: ") "\n"
8026                 "Date: " (message-make-date (nth 5 atts))
8027                 "\n"
8028                 "Message-ID: " (message-make-message-id) "\n"
8029                 "Lines: " (int-to-string lines) "\n"
8030                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8031       (gnus-request-accept-article group nil t)
8032       (kill-buffer (current-buffer)))))
8033
8034 (defun gnus-summary-article-posted-p ()
8035   "Say whether the current (mail) article is available from news as well.
8036 This will be the case if the article has both been mailed and posted."
8037   (interactive)
8038   (let ((id (mail-header-references (gnus-summary-article-header)))
8039         (gnus-override-method (car (gnus-refer-article-methods))))
8040     (if (gnus-request-head id "")
8041         (gnus-message 2 "The current message was found on %s"
8042                       gnus-override-method)
8043       (gnus-message 2 "The current message couldn't be found on %s"
8044                     gnus-override-method)
8045       nil)))
8046
8047 (defun gnus-summary-expire-articles (&optional now)
8048   "Expire all articles that are marked as expirable in the current group."
8049   (interactive)
8050   (when (gnus-check-backend-function
8051          'request-expire-articles gnus-newsgroup-name)
8052     ;; This backend supports expiry.
8053     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8054            (expirable (if total
8055                           (progn
8056                             ;; We need to update the info for
8057                             ;; this group for `gnus-list-of-read-articles'
8058                             ;; to give us the right answer.
8059                             (gnus-run-hooks 'gnus-exit-group-hook)
8060                             (gnus-summary-update-info)
8061                             (gnus-list-of-read-articles gnus-newsgroup-name))
8062                         (setq gnus-newsgroup-expirable
8063                               (sort gnus-newsgroup-expirable '<))))
8064            (expiry-wait (if now 'immediate
8065                           (gnus-group-find-parameter
8066                            gnus-newsgroup-name 'expiry-wait)))
8067            (nnmail-expiry-target
8068             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8069                 nnmail-expiry-target))
8070            es)
8071       (when expirable
8072         ;; There are expirable articles in this group, so we run them
8073         ;; through the expiry process.
8074         (gnus-message 6 "Expiring articles...")
8075         (unless (gnus-check-group gnus-newsgroup-name)
8076           (error "Can't open server for %s" gnus-newsgroup-name))
8077         ;; The list of articles that weren't expired is returned.
8078         (save-excursion
8079           (if expiry-wait
8080               (let ((nnmail-expiry-wait-function nil)
8081                     (nnmail-expiry-wait expiry-wait))
8082                 (setq es (gnus-request-expire-articles
8083                           expirable gnus-newsgroup-name)))
8084             (setq es (gnus-request-expire-articles
8085                       expirable gnus-newsgroup-name)))
8086           (unless total
8087             (setq gnus-newsgroup-expirable es))
8088           ;; We go through the old list of expirable, and mark all
8089           ;; really expired articles as nonexistent.
8090           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8091             (let ((gnus-use-cache nil))
8092               (while expirable
8093                 (unless (memq (car expirable) es)
8094                   (when (gnus-data-find (car expirable))
8095                     (gnus-summary-mark-article
8096                      (car expirable) gnus-canceled-mark)))
8097                 (setq expirable (cdr expirable))))))
8098         (gnus-message 6 "Expiring articles...done")))))
8099
8100 (defun gnus-summary-expire-articles-now ()
8101   "Expunge all expirable articles in the current group.
8102 This means that *all* articles that are marked as expirable will be
8103 deleted forever, right now."
8104   (interactive)
8105   (or gnus-expert-user
8106       (gnus-yes-or-no-p
8107        "Are you really, really, really sure you want to delete all these messages? ")
8108       (error "Phew!"))
8109   (gnus-summary-expire-articles t))
8110
8111 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8112 (defun gnus-summary-delete-article (&optional n)
8113   "Delete the N next (mail) articles.
8114 This command actually deletes articles.  This is not a marking
8115 command.  The article will disappear forever from your life, never to
8116 return.
8117 If N is negative, delete backwards.
8118 If N is nil and articles have been marked with the process mark,
8119 delete these instead."
8120   (interactive "P")
8121   (unless (gnus-check-backend-function 'request-expire-articles
8122                                        gnus-newsgroup-name)
8123     (error "The current newsgroup does not support article deletion"))
8124   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8125     (error "Couldn't open server"))
8126   ;; Compute the list of articles to delete.
8127   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8128         not-deleted)
8129     (if (and gnus-novice-user
8130              (not (gnus-yes-or-no-p
8131                    (format "Do you really want to delete %s forever? "
8132                            (if (> (length articles) 1)
8133                                (format "these %s articles" (length articles))
8134                              "this article")))))
8135         ()
8136       ;; Delete the articles.
8137       (setq not-deleted (gnus-request-expire-articles
8138                          articles gnus-newsgroup-name 'force))
8139       (while articles
8140         (gnus-summary-remove-process-mark (car articles))
8141         ;; The backend might not have been able to delete the article
8142         ;; after all.
8143         (unless (memq (car articles) not-deleted)
8144           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8145         (setq articles (cdr articles)))
8146       (when not-deleted
8147         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8148     (gnus-summary-position-point)
8149     (gnus-set-mode-line 'summary)
8150     not-deleted))
8151
8152 (defun gnus-summary-edit-article (&optional force)
8153   "Edit the current article.
8154 This will have permanent effect only in mail groups.
8155 If FORCE is non-nil, allow editing of articles even in read-only
8156 groups."
8157   (interactive "P")
8158   (save-excursion
8159     (set-buffer gnus-summary-buffer)
8160     (let ((mail-parse-charset gnus-newsgroup-charset)
8161           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8162       (gnus-set-global-variables)
8163       (when (and (not force)
8164                  (gnus-group-read-only-p))
8165         (error "The current newsgroup does not support article editing"))
8166       (gnus-summary-show-article t)
8167       (gnus-article-edit-article
8168        'ignore
8169        `(lambda (no-highlight)
8170           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8171                 (message-options message-options)
8172                 (message-options-set-recipient)
8173                 (mail-parse-ignored-charsets
8174                  ',gnus-newsgroup-ignored-charsets))
8175             (gnus-summary-edit-article-done
8176              ,(or (mail-header-references gnus-current-headers) "")
8177              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8178
8179 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8180
8181 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8182                                                  no-highlight)
8183   "Make edits to the current article permanent."
8184   (interactive)
8185   (save-excursion
8186     ;; The buffer restriction contains the entire article if it exists.
8187     (when (article-goto-body)
8188       (let ((lines (count-lines (point) (point-max)))
8189             (length (- (point-max) (point)))
8190             (case-fold-search t)
8191             (body (copy-marker (point))))
8192         (goto-char (point-min))
8193         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8194           (delete-region (match-beginning 1) (match-end 1))
8195           (insert (number-to-string length)))
8196         (goto-char (point-min))
8197         (when (re-search-forward
8198                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8199           (delete-region (match-beginning 1) (match-end 1))
8200           (insert (number-to-string length)))
8201         (goto-char (point-min))
8202         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8203           (delete-region (match-beginning 1) (match-end 1))
8204           (insert (number-to-string lines))))))
8205   ;; Replace the article.
8206   (let ((buf (current-buffer)))
8207     (with-temp-buffer
8208       (insert-buffer-substring buf)
8209       
8210       (if (and (not read-only)
8211                (not (gnus-request-replace-article
8212                      (cdr gnus-article-current) (car gnus-article-current)
8213                      (current-buffer) t)))
8214           (error "Couldn't replace article")
8215         ;; Update the summary buffer.
8216         (if (and references
8217                  (equal (message-tokenize-header references " ")
8218                         (message-tokenize-header
8219                          (or (message-fetch-field "references") "") " ")))
8220             ;; We only have to update this line.
8221             (save-excursion
8222               (save-restriction
8223                 (message-narrow-to-head)
8224                 (let ((head (buffer-string))
8225                       header)
8226                   (with-temp-buffer
8227                     (insert (format "211 %d Article retrieved.\n"
8228                                     (cdr gnus-article-current)))
8229                     (insert head)
8230                     (insert ".\n")
8231                     (let ((nntp-server-buffer (current-buffer)))
8232                       (setq header (car (gnus-get-newsgroup-headers
8233                                          (save-excursion
8234                                            (set-buffer gnus-summary-buffer)
8235                                            gnus-newsgroup-dependencies)
8236                                          t))))
8237                     (save-excursion
8238                       (set-buffer gnus-summary-buffer)
8239                       (gnus-data-set-header
8240                        (gnus-data-find (cdr gnus-article-current))
8241                        header)
8242                       (gnus-summary-update-article-line
8243                        (cdr gnus-article-current) header))))))
8244           ;; Update threads.
8245           (set-buffer (or buffer gnus-summary-buffer))
8246           (gnus-summary-update-article (cdr gnus-article-current)))
8247         ;; Prettify the article buffer again.
8248         (unless no-highlight
8249           (save-excursion
8250             (set-buffer gnus-article-buffer)
8251             ;;;!!! Fix this -- article should be rehighlighted.
8252             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8253             (set-buffer gnus-original-article-buffer)
8254             (gnus-request-article
8255              (cdr gnus-article-current)
8256              (car gnus-article-current) (current-buffer))))
8257         ;; Prettify the summary buffer line.
8258         (when (gnus-visual-p 'summary-highlight 'highlight)
8259           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8260
8261 (defun gnus-summary-edit-wash (key)
8262   "Perform editing command KEY in the article buffer."
8263   (interactive
8264    (list
8265     (progn
8266       (message "%s" (concat (this-command-keys) "- "))
8267       (read-char))))
8268   (message "")
8269   (gnus-summary-edit-article)
8270   (execute-kbd-macro (concat (this-command-keys) key))
8271   (gnus-article-edit-done))
8272
8273 ;;; Respooling
8274
8275 (defun gnus-summary-respool-query (&optional silent trace)
8276   "Query where the respool algorithm would put this article."
8277   (interactive)
8278   (let (gnus-mark-article-hook)
8279     (gnus-summary-select-article)
8280     (save-excursion
8281       (set-buffer gnus-original-article-buffer)
8282       (save-restriction
8283         (message-narrow-to-head)
8284         (let ((groups (nnmail-article-group 'identity trace)))
8285           (unless silent
8286             (if groups
8287                 (message "This message would go to %s"
8288                          (mapconcat 'car groups ", "))
8289               (message "This message would go to no groups"))
8290             groups))))))
8291
8292 (defun gnus-summary-respool-trace ()
8293   "Trace where the respool algorithm would put this article.
8294 Display a buffer showing all fancy splitting patterns which matched."
8295   (interactive)
8296   (gnus-summary-respool-query nil t))
8297
8298 ;; Summary marking commands.
8299
8300 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8301   "Mark articles which has the same subject as read, and then select the next.
8302 If UNMARK is positive, remove any kind of mark.
8303 If UNMARK is negative, tick articles."
8304   (interactive "P")
8305   (when unmark
8306     (setq unmark (prefix-numeric-value unmark)))
8307   (let ((count
8308          (gnus-summary-mark-same-subject
8309           (gnus-summary-article-subject) unmark)))
8310     ;; Select next unread article.  If auto-select-same mode, should
8311     ;; select the first unread article.
8312     (gnus-summary-next-article t (and gnus-auto-select-same
8313                                       (gnus-summary-article-subject)))
8314     (gnus-message 7 "%d article%s marked as %s"
8315                   count (if (= count 1) " is" "s are")
8316                   (if unmark "unread" "read"))))
8317
8318 (defun gnus-summary-kill-same-subject (&optional unmark)
8319   "Mark articles which has the same subject as read.
8320 If UNMARK is positive, remove any kind of mark.
8321 If UNMARK is negative, tick articles."
8322   (interactive "P")
8323   (when unmark
8324     (setq unmark (prefix-numeric-value unmark)))
8325   (let ((count
8326          (gnus-summary-mark-same-subject
8327           (gnus-summary-article-subject) unmark)))
8328     ;; If marked as read, go to next unread subject.
8329     (when (null unmark)
8330       ;; Go to next unread subject.
8331       (gnus-summary-next-subject 1 t))
8332     (gnus-message 7 "%d articles are marked as %s"
8333                   count (if unmark "unread" "read"))))
8334
8335 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8336   "Mark articles with same SUBJECT as read, and return marked number.
8337 If optional argument UNMARK is positive, remove any kinds of marks.
8338 If optional argument UNMARK is negative, mark articles as unread instead."
8339   (let ((count 1))
8340     (save-excursion
8341       (cond
8342        ((null unmark)                   ; Mark as read.
8343         (while (and
8344                 (progn
8345                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8346                   (gnus-summary-show-thread) t)
8347                 (gnus-summary-find-subject subject))
8348           (setq count (1+ count))))
8349        ((> unmark 0)                    ; Tick.
8350         (while (and
8351                 (progn
8352                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8353                   (gnus-summary-show-thread) t)
8354                 (gnus-summary-find-subject subject))
8355           (setq count (1+ count))))
8356        (t                               ; Mark as unread.
8357         (while (and
8358                 (progn
8359                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8360                   (gnus-summary-show-thread) t)
8361                 (gnus-summary-find-subject subject))
8362           (setq count (1+ count)))))
8363       (gnus-set-mode-line 'summary)
8364       ;; Return the number of marked articles.
8365       count)))
8366
8367 (defun gnus-summary-mark-as-processable (n &optional unmark)
8368   "Set the process mark on the next N articles.
8369 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8370 the process mark instead.  The difference between N and the actual
8371 number of articles marked is returned."
8372   (interactive "p")
8373   (let ((backward (< n 0))
8374         (n (abs n)))
8375     (while (and
8376             (> n 0)
8377             (if unmark
8378                 (gnus-summary-remove-process-mark
8379                  (gnus-summary-article-number))
8380               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8381             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8382       (setq n (1- n)))
8383     (when (/= 0 n)
8384       (gnus-message 7 "No more articles"))
8385     (gnus-summary-recenter)
8386     (gnus-summary-position-point)
8387     n))
8388
8389 (defun gnus-summary-unmark-as-processable (n)
8390   "Remove the process mark from the next N articles.
8391 If N is negative, unmark backward instead.  The difference between N and
8392 the actual number of articles unmarked is returned."
8393   (interactive "p")
8394   (gnus-summary-mark-as-processable n t))
8395
8396 (defun gnus-summary-unmark-all-processable ()
8397   "Remove the process mark from all articles."
8398   (interactive)
8399   (save-excursion
8400     (while gnus-newsgroup-processable
8401       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8402   (gnus-summary-position-point))
8403
8404 (defun gnus-summary-mark-as-expirable (n)
8405   "Mark N articles forward as expirable.
8406 If N is negative, mark backward instead.  The difference between N and
8407 the actual number of articles marked is returned."
8408   (interactive "p")
8409   (gnus-summary-mark-forward n gnus-expirable-mark))
8410
8411 (defun gnus-summary-mark-article-as-replied (article)
8412   "Mark ARTICLE replied and update the summary line."
8413   (push article gnus-newsgroup-replied)
8414   (let ((buffer-read-only nil))
8415     (when (gnus-summary-goto-subject article nil t)
8416       (gnus-summary-update-secondary-mark article))))
8417
8418 (defun gnus-summary-set-bookmark (article)
8419   "Set a bookmark in current article."
8420   (interactive (list (gnus-summary-article-number)))
8421   (when (or (not (get-buffer gnus-article-buffer))
8422             (not gnus-current-article)
8423             (not gnus-article-current)
8424             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8425     (error "No current article selected"))
8426   ;; Remove old bookmark, if one exists.
8427   (let ((old (assq article gnus-newsgroup-bookmarks)))
8428     (when old
8429       (setq gnus-newsgroup-bookmarks
8430             (delq old gnus-newsgroup-bookmarks))))
8431   ;; Set the new bookmark, which is on the form
8432   ;; (article-number . line-number-in-body).
8433   (push
8434    (cons article
8435          (save-excursion
8436            (set-buffer gnus-article-buffer)
8437            (count-lines
8438             (min (point)
8439                  (save-excursion
8440                    (goto-char (point-min))
8441                    (search-forward "\n\n" nil t)
8442                    (point)))
8443             (point))))
8444    gnus-newsgroup-bookmarks)
8445   (gnus-message 6 "A bookmark has been added to the current article."))
8446
8447 (defun gnus-summary-remove-bookmark (article)
8448   "Remove the bookmark from the current article."
8449   (interactive (list (gnus-summary-article-number)))
8450   ;; Remove old bookmark, if one exists.
8451   (let ((old (assq article gnus-newsgroup-bookmarks)))
8452     (if old
8453         (progn
8454           (setq gnus-newsgroup-bookmarks
8455                 (delq old gnus-newsgroup-bookmarks))
8456           (gnus-message 6 "Removed bookmark."))
8457       (gnus-message 6 "No bookmark in current article."))))
8458
8459 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8460 (defun gnus-summary-mark-as-dormant (n)
8461   "Mark N articles forward as dormant.
8462 If N is negative, mark backward instead.  The difference between N and
8463 the actual number of articles marked is returned."
8464   (interactive "p")
8465   (gnus-summary-mark-forward n gnus-dormant-mark))
8466
8467 (defun gnus-summary-set-process-mark (article)
8468   "Set the process mark on ARTICLE and update the summary line."
8469   (setq gnus-newsgroup-processable
8470         (cons article
8471               (delq article gnus-newsgroup-processable)))
8472   (when (gnus-summary-goto-subject article)
8473     (gnus-summary-show-thread)
8474     (gnus-summary-goto-subject article)
8475     (gnus-summary-update-secondary-mark article)))
8476
8477 (defun gnus-summary-remove-process-mark (article)
8478   "Remove the process mark from ARTICLE and update the summary line."
8479   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8480   (when (gnus-summary-goto-subject article)
8481     (gnus-summary-show-thread)
8482     (gnus-summary-goto-subject article)
8483     (gnus-summary-update-secondary-mark article)))
8484
8485 (defun gnus-summary-set-saved-mark (article)
8486   "Set the process mark on ARTICLE and update the summary line."
8487   (push article gnus-newsgroup-saved)
8488   (when (gnus-summary-goto-subject article)
8489     (gnus-summary-update-secondary-mark article)))
8490
8491 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8492   "Mark N articles as read forwards.
8493 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8494 The difference between N and the actual number of articles marked is
8495 returned.
8496 Iff NO-EXPIRE, auto-expiry will be inhibited."
8497   (interactive "p")
8498   (gnus-summary-show-thread)
8499   (let ((backward (< n 0))
8500         (gnus-summary-goto-unread
8501          (and gnus-summary-goto-unread
8502               (not (eq gnus-summary-goto-unread 'never))
8503               (not (memq mark (list gnus-unread-mark
8504                                     gnus-ticked-mark gnus-dormant-mark)))))
8505         (n (abs n))
8506         (mark (or mark gnus-del-mark)))
8507     (while (and (> n 0)
8508                 (gnus-summary-mark-article nil mark no-expire)
8509                 (zerop (gnus-summary-next-subject
8510                         (if backward -1 1)
8511                         (and gnus-summary-goto-unread
8512                              (not (eq gnus-summary-goto-unread 'never)))
8513                         t)))
8514       (setq n (1- n)))
8515     (when (/= 0 n)
8516       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8517     (gnus-summary-recenter)
8518     (gnus-summary-position-point)
8519     (gnus-set-mode-line 'summary)
8520     n))
8521
8522 (defun gnus-summary-mark-article-as-read (mark)
8523   "Mark the current article quickly as read with MARK."
8524   (let ((article (gnus-summary-article-number)))
8525     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8526     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8527     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8528     (push (cons article mark) gnus-newsgroup-reads)
8529     ;; Possibly remove from cache, if that is used.
8530     (when gnus-use-cache
8531       (gnus-cache-enter-remove-article article))
8532     ;; Allow the backend to change the mark.
8533     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8534     ;; Check for auto-expiry.
8535     (when (and gnus-newsgroup-auto-expire
8536                (memq mark gnus-auto-expirable-marks))
8537       (setq mark gnus-expirable-mark)
8538       ;; Let the backend know about the mark change.
8539       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8540       (push article gnus-newsgroup-expirable))
8541     ;; Set the mark in the buffer.
8542     (gnus-summary-update-mark mark 'unread)
8543     t))
8544
8545 (defun gnus-summary-mark-article-as-unread (mark)
8546   "Mark the current article quickly as unread with MARK."
8547   (let* ((article (gnus-summary-article-number))
8548          (old-mark (gnus-summary-article-mark article)))
8549     ;; Allow the backend to change the mark.
8550     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8551     (if (eq mark old-mark)
8552         t
8553       (if (<= article 0)
8554           (progn
8555             (gnus-error 1 "Can't mark negative article numbers")
8556             nil)
8557         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8558         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8559         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8560         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8561         (cond ((= mark gnus-ticked-mark)
8562                (push article gnus-newsgroup-marked))
8563               ((= mark gnus-dormant-mark)
8564                (push article gnus-newsgroup-dormant))
8565               (t
8566                (push article gnus-newsgroup-unreads)))
8567         (gnus-pull article gnus-newsgroup-reads)
8568
8569         ;; See whether the article is to be put in the cache.
8570         (and gnus-use-cache
8571              (vectorp (gnus-summary-article-header article))
8572              (save-excursion
8573                (gnus-cache-possibly-enter-article
8574                 gnus-newsgroup-name article
8575                 (gnus-summary-article-header article)
8576                 (= mark gnus-ticked-mark)
8577                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8578
8579         ;; Fix the mark.
8580         (gnus-summary-update-mark mark 'unread)
8581         t))))
8582
8583 (defun gnus-summary-mark-article (&optional article mark no-expire)
8584   "Mark ARTICLE with MARK.  MARK can be any character.
8585 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8586 `??' (dormant) and `?E' (expirable).
8587 If MARK is nil, then the default character `?r' is used.
8588 If ARTICLE is nil, then the article on the current line will be
8589 marked.
8590 Iff NO-EXPIRE, auto-expiry will be inhibited."
8591   ;; The mark might be a string.
8592   (when (stringp mark)
8593     (setq mark (aref mark 0)))
8594   ;; If no mark is given, then we check auto-expiring.
8595   (when (null mark)
8596     (setq mark gnus-del-mark))
8597   (when (and (not no-expire)
8598              gnus-newsgroup-auto-expire
8599              (memq mark gnus-auto-expirable-marks))
8600     (setq mark gnus-expirable-mark))
8601   (let ((article (or article (gnus-summary-article-number)))
8602         (old-mark (gnus-summary-article-mark article)))
8603     ;; Allow the backend to change the mark.
8604     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8605     (if (eq mark old-mark)
8606         t
8607       (unless article
8608         (error "No article on current line"))
8609       (if (not (if (or (= mark gnus-unread-mark)
8610                        (= mark gnus-ticked-mark)
8611                        (= mark gnus-dormant-mark))
8612                    (gnus-mark-article-as-unread article mark)
8613                  (gnus-mark-article-as-read article mark)))
8614           t
8615         ;; See whether the article is to be put in the cache.
8616         (and gnus-use-cache
8617              (not (= mark gnus-canceled-mark))
8618              (vectorp (gnus-summary-article-header article))
8619              (save-excursion
8620                (gnus-cache-possibly-enter-article
8621                 gnus-newsgroup-name article
8622                 (gnus-summary-article-header article)
8623                 (= mark gnus-ticked-mark)
8624                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8625
8626         (when (gnus-summary-goto-subject article nil t)
8627           (let ((buffer-read-only nil))
8628             (gnus-summary-show-thread)
8629             ;; Fix the mark.
8630             (gnus-summary-update-mark mark 'unread)
8631             t))))))
8632
8633 (defun gnus-summary-update-secondary-mark (article)
8634   "Update the secondary (read, process, cache) mark."
8635   (gnus-summary-update-mark
8636    (cond ((memq article gnus-newsgroup-processable)
8637           gnus-process-mark)
8638          ((memq article gnus-newsgroup-cached)
8639           gnus-cached-mark)
8640          ((memq article gnus-newsgroup-replied)
8641           gnus-replied-mark)
8642          ((memq article gnus-newsgroup-saved)
8643           gnus-saved-mark)
8644          (t gnus-unread-mark))
8645    'replied)
8646   (when (gnus-visual-p 'summary-highlight 'highlight)
8647     (gnus-run-hooks 'gnus-summary-update-hook))
8648   t)
8649
8650 (defun gnus-summary-update-mark (mark type)
8651   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8652         (buffer-read-only nil))
8653     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8654     (when forward
8655       (when (looking-at "\r")
8656         (incf forward))
8657       (when (<= (+ forward (point)) (point-max))
8658         ;; Go to the right position on the line.
8659         (goto-char (+ forward (point)))
8660         ;; Replace the old mark with the new mark.
8661         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8662         ;; Optionally update the marks by some user rule.
8663         (when (eq type 'unread)
8664           (gnus-data-set-mark
8665            (gnus-data-find (gnus-summary-article-number)) mark)
8666           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8667
8668 (defun gnus-mark-article-as-read (article &optional mark)
8669   "Enter ARTICLE in the pertinent lists and remove it from others."
8670   ;; Make the article expirable.
8671   (let ((mark (or mark gnus-del-mark)))
8672     (if (= mark gnus-expirable-mark)
8673         (push article gnus-newsgroup-expirable)
8674       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8675     ;; Remove from unread and marked lists.
8676     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8677     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8678     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8679     (push (cons article mark) gnus-newsgroup-reads)
8680     ;; Possibly remove from cache, if that is used.
8681     (when gnus-use-cache
8682       (gnus-cache-enter-remove-article article))
8683     t))
8684
8685 (defun gnus-mark-article-as-unread (article &optional mark)
8686   "Enter ARTICLE in the pertinent lists and remove it from others."
8687   (let ((mark (or mark gnus-ticked-mark)))
8688     (if (<= article 0)
8689         (progn
8690           (gnus-error 1 "Can't mark negative article numbers")
8691           nil)
8692       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8693             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8694             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8695             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8696
8697       ;; Unsuppress duplicates?
8698       (when gnus-suppress-duplicates
8699         (gnus-dup-unsuppress-article article))
8700
8701       (cond ((= mark gnus-ticked-mark)
8702              (push article gnus-newsgroup-marked))
8703             ((= mark gnus-dormant-mark)
8704              (push article gnus-newsgroup-dormant))
8705             (t
8706              (push article gnus-newsgroup-unreads)))
8707       (gnus-pull article gnus-newsgroup-reads)
8708       t)))
8709
8710 (defalias 'gnus-summary-mark-as-unread-forward
8711   'gnus-summary-tick-article-forward)
8712 (make-obsolete 'gnus-summary-mark-as-unread-forward
8713                'gnus-summary-tick-article-forward)
8714 (defun gnus-summary-tick-article-forward (n)
8715   "Tick N articles forwards.
8716 If N is negative, tick backwards instead.
8717 The difference between N and the number of articles ticked is returned."
8718   (interactive "p")
8719   (gnus-summary-mark-forward n gnus-ticked-mark))
8720
8721 (defalias 'gnus-summary-mark-as-unread-backward
8722   'gnus-summary-tick-article-backward)
8723 (make-obsolete 'gnus-summary-mark-as-unread-backward
8724                'gnus-summary-tick-article-backward)
8725 (defun gnus-summary-tick-article-backward (n)
8726   "Tick N articles backwards.
8727 The difference between N and the number of articles ticked is returned."
8728   (interactive "p")
8729   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8730
8731 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8732 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8733 (defun gnus-summary-tick-article (&optional article clear-mark)
8734   "Mark current article as unread.
8735 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8736 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8737   (interactive)
8738   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8739                                        gnus-ticked-mark)))
8740
8741 (defun gnus-summary-mark-as-read-forward (n)
8742   "Mark N articles as read forwards.
8743 If N is negative, mark backwards instead.
8744 The difference between N and the actual number of articles marked is
8745 returned."
8746   (interactive "p")
8747   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8748
8749 (defun gnus-summary-mark-as-read-backward (n)
8750   "Mark the N articles as read backwards.
8751 The difference between N and the actual number of articles marked is
8752 returned."
8753   (interactive "p")
8754   (gnus-summary-mark-forward
8755    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8756
8757 (defun gnus-summary-mark-as-read (&optional article mark)
8758   "Mark current article as read.
8759 ARTICLE specifies the article to be marked as read.
8760 MARK specifies a string to be inserted at the beginning of the line."
8761   (gnus-summary-mark-article article mark))
8762
8763 (defun gnus-summary-clear-mark-forward (n)
8764   "Clear marks from N articles forward.
8765 If N is negative, clear backward instead.
8766 The difference between N and the number of marks cleared is returned."
8767   (interactive "p")
8768   (gnus-summary-mark-forward n gnus-unread-mark))
8769
8770 (defun gnus-summary-clear-mark-backward (n)
8771   "Clear marks from N articles backward.
8772 The difference between N and the number of marks cleared is returned."
8773   (interactive "p")
8774   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8775
8776 (defun gnus-summary-mark-unread-as-read ()
8777   "Intended to be used by `gnus-summary-mark-article-hook'."
8778   (when (memq gnus-current-article gnus-newsgroup-unreads)
8779     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8780
8781 (defun gnus-summary-mark-read-and-unread-as-read ()
8782   "Intended to be used by `gnus-summary-mark-article-hook'."
8783   (let ((mark (gnus-summary-article-mark)))
8784     (when (or (gnus-unread-mark-p mark)
8785               (gnus-read-mark-p mark))
8786       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8787
8788 (defun gnus-summary-mark-region-as-read (point mark all)
8789   "Mark all unread articles between point and mark as read.
8790 If given a prefix, mark all articles between point and mark as read,
8791 even ticked and dormant ones."
8792   (interactive "r\nP")
8793   (save-excursion
8794     (let (article)
8795       (goto-char point)
8796       (beginning-of-line)
8797       (while (and
8798               (< (point) mark)
8799               (progn
8800                 (when (or all
8801                           (memq (setq article (gnus-summary-article-number))
8802                                 gnus-newsgroup-unreads))
8803                   (gnus-summary-mark-article article gnus-del-mark))
8804                 t)
8805               (gnus-summary-find-next))))))
8806
8807 (defun gnus-summary-mark-below (score mark)
8808   "Mark articles with score less than SCORE with MARK."
8809   (interactive "P\ncMark: ")
8810   (setq score (if score
8811                   (prefix-numeric-value score)
8812                 (or gnus-summary-default-score 0)))
8813   (save-excursion
8814     (set-buffer gnus-summary-buffer)
8815     (goto-char (point-min))
8816     (while
8817         (progn
8818           (and (< (gnus-summary-article-score) score)
8819                (gnus-summary-mark-article nil mark))
8820           (gnus-summary-find-next)))))
8821
8822 (defun gnus-summary-kill-below (&optional score)
8823   "Mark articles with score below SCORE as read."
8824   (interactive "P")
8825   (gnus-summary-mark-below score gnus-killed-mark))
8826
8827 (defun gnus-summary-clear-above (&optional score)
8828   "Clear all marks from articles with score above SCORE."
8829   (interactive "P")
8830   (gnus-summary-mark-above score gnus-unread-mark))
8831
8832 (defun gnus-summary-tick-above (&optional score)
8833   "Tick all articles with score above SCORE."
8834   (interactive "P")
8835   (gnus-summary-mark-above score gnus-ticked-mark))
8836
8837 (defun gnus-summary-mark-above (score mark)
8838   "Mark articles with score over SCORE with MARK."
8839   (interactive "P\ncMark: ")
8840   (setq score (if score
8841                   (prefix-numeric-value score)
8842                 (or gnus-summary-default-score 0)))
8843   (save-excursion
8844     (set-buffer gnus-summary-buffer)
8845     (goto-char (point-min))
8846     (while (and (progn
8847                   (when (> (gnus-summary-article-score) score)
8848                     (gnus-summary-mark-article nil mark))
8849                   t)
8850                 (gnus-summary-find-next)))))
8851
8852 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8853 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8854 (defun gnus-summary-limit-include-expunged (&optional no-error)
8855   "Display all the hidden articles that were expunged for low scores."
8856   (interactive)
8857   (let ((buffer-read-only nil))
8858     (let ((scored gnus-newsgroup-scored)
8859           headers h)
8860       (while scored
8861         (unless (gnus-summary-goto-subject (caar scored))
8862           (and (setq h (gnus-summary-article-header (caar scored)))
8863                (< (cdar scored) gnus-summary-expunge-below)
8864                (push h headers)))
8865         (setq scored (cdr scored)))
8866       (if (not headers)
8867           (when (not no-error)
8868             (error "No expunged articles hidden"))
8869         (goto-char (point-min))
8870         (gnus-summary-prepare-unthreaded (nreverse headers))
8871         (goto-char (point-min))
8872         (gnus-summary-position-point)
8873         t))))
8874
8875 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8876   "Mark all unread articles in this newsgroup as read.
8877 If prefix argument ALL is non-nil, ticked and dormant articles will
8878 also be marked as read.
8879 If QUIETLY is non-nil, no questions will be asked.
8880 If TO-HERE is non-nil, it should be a point in the buffer.  All
8881 articles before this point will be marked as read.
8882 Note that this function will only catch up the unread article
8883 in the current summary buffer limitation.
8884 The number of articles marked as read is returned."
8885   (interactive "P")
8886   (prog1
8887       (save-excursion
8888         (when (or quietly
8889                   (not gnus-interactive-catchup) ;Without confirmation?
8890                   gnus-expert-user
8891                   (gnus-y-or-n-p
8892                    (if all
8893                        "Mark absolutely all articles as read? "
8894                      "Mark all unread articles as read? ")))
8895           (if (and not-mark
8896                    (not gnus-newsgroup-adaptive)
8897                    (not gnus-newsgroup-auto-expire)
8898                    (not gnus-suppress-duplicates)
8899                    (or (not gnus-use-cache)
8900                        (eq gnus-use-cache 'passive)))
8901               (progn
8902                 (when all
8903                   (setq gnus-newsgroup-marked nil
8904                         gnus-newsgroup-dormant nil))
8905                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8906             ;; We actually mark all articles as canceled, which we
8907             ;; have to do when using auto-expiry or adaptive scoring.
8908             (gnus-summary-show-all-threads)
8909             (when (gnus-summary-first-subject (not all) t)
8910               (while (and
8911                       (if to-here (< (point) to-here) t)
8912                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8913                       (gnus-summary-find-next (not all) nil nil t))))
8914             (gnus-set-mode-line 'summary))
8915           t))
8916     (gnus-summary-position-point)))
8917
8918 (defun gnus-summary-catchup-to-here (&optional all)
8919   "Mark all unticked articles before the current one as read.
8920 If ALL is non-nil, also mark ticked and dormant articles as read."
8921   (interactive "P")
8922   (save-excursion
8923     (gnus-save-hidden-threads
8924       (let ((beg (point)))
8925         ;; We check that there are unread articles.
8926         (when (or all (gnus-summary-find-prev))
8927           (gnus-summary-catchup all t beg)))))
8928   (gnus-summary-position-point))
8929
8930 (defun gnus-summary-catchup-all (&optional quietly)
8931   "Mark all articles in this newsgroup as read."
8932   (interactive "P")
8933   (gnus-summary-catchup t quietly))
8934
8935 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8936   "Mark all unread articles in this group as read, then exit.
8937 If prefix argument ALL is non-nil, all articles are marked as read."
8938   (interactive "P")
8939   (when (gnus-summary-catchup all quietly nil 'fast)
8940     ;; Select next newsgroup or exit.
8941     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8942              (eq gnus-auto-select-next 'quietly))
8943         (gnus-summary-next-group nil)
8944       (gnus-summary-exit))))
8945
8946 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8947   "Mark all articles in this newsgroup as read, and then exit."
8948   (interactive "P")
8949   (gnus-summary-catchup-and-exit t quietly))
8950
8951 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8952   "Mark all articles in this group as read and select the next group.
8953 If given a prefix, mark all articles, unread as well as ticked, as
8954 read."
8955   (interactive "P")
8956   (save-excursion
8957     (gnus-summary-catchup all))
8958   (gnus-summary-next-group))
8959
8960 ;;;
8961 ;;; with article
8962 ;;;
8963
8964 (defmacro gnus-with-article (article &rest forms)
8965   "Select ARTICLE and perform FORMS in the original article buffer.
8966 Then replace the article with the result."
8967   `(progn
8968      ;; We don't want the article to be marked as read.
8969      (let (gnus-mark-article-hook)
8970        (gnus-summary-select-article t t nil ,article))
8971      (set-buffer gnus-original-article-buffer)
8972      ,@forms
8973      (if (not (gnus-check-backend-function
8974                'request-replace-article (car gnus-article-current)))
8975          (gnus-message 5 "Read-only group; not replacing")
8976        (unless (gnus-request-replace-article
8977                 ,article (car gnus-article-current)
8978                 (current-buffer) t)
8979          (error "Couldn't replace article")))
8980      ;; The cache and backlog have to be flushed somewhat.
8981      (when gnus-keep-backlog
8982        (gnus-backlog-remove-article
8983         (car gnus-article-current) (cdr gnus-article-current)))
8984      (when gnus-use-cache
8985        (gnus-cache-update-article
8986         (car gnus-article-current) (cdr gnus-article-current)))))
8987
8988 (put 'gnus-with-article 'lisp-indent-function 1)
8989 (put 'gnus-with-article 'edebug-form-spec '(form body))
8990
8991 ;; Thread-based commands.
8992
8993 (defun gnus-summary-articles-in-thread (&optional article)
8994   "Return a list of all articles in the current thread.
8995 If ARTICLE is non-nil, return all articles in the thread that starts
8996 with that article."
8997   (let* ((article (or article (gnus-summary-article-number)))
8998          (data (gnus-data-find-list article))
8999          (top-level (gnus-data-level (car data)))
9000          (top-subject
9001           (cond ((null gnus-thread-operation-ignore-subject)
9002                  (gnus-simplify-subject-re
9003                   (mail-header-subject (gnus-data-header (car data)))))
9004                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9005                  (gnus-simplify-subject-fuzzy
9006                   (mail-header-subject (gnus-data-header (car data)))))
9007                 (t nil)))
9008          (end-point (save-excursion
9009                       (if (gnus-summary-go-to-next-thread)
9010                           (point) (point-max))))
9011          articles)
9012     (while (and data
9013                 (< (gnus-data-pos (car data)) end-point))
9014       (when (or (not top-subject)
9015                 (string= top-subject
9016                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9017                              (gnus-simplify-subject-fuzzy
9018                               (mail-header-subject
9019                                (gnus-data-header (car data))))
9020                            (gnus-simplify-subject-re
9021                             (mail-header-subject
9022                              (gnus-data-header (car data)))))))
9023         (push (gnus-data-number (car data)) articles))
9024       (unless (and (setq data (cdr data))
9025                    (> (gnus-data-level (car data)) top-level))
9026         (setq data nil)))
9027     ;; Return the list of articles.
9028     (nreverse articles)))
9029
9030 (defun gnus-summary-rethread-current ()
9031   "Rethread the thread the current article is part of."
9032   (interactive)
9033   (let* ((gnus-show-threads t)
9034          (article (gnus-summary-article-number))
9035          (id (mail-header-id (gnus-summary-article-header)))
9036          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9037     (unless id
9038       (error "No article on the current line"))
9039     (gnus-rebuild-thread id)
9040     (gnus-summary-goto-subject article)))
9041
9042 (defun gnus-summary-reparent-thread ()
9043   "Make the current article child of the marked (or previous) article.
9044
9045 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9046 is non-nil or the Subject: of both articles are the same."
9047   (interactive)
9048   (unless (not (gnus-group-read-only-p))
9049     (error "The current newsgroup does not support article editing"))
9050   (unless (<= (length gnus-newsgroup-processable) 1)
9051     (error "No more than one article may be marked"))
9052   (save-window-excursion
9053     (let ((gnus-article-buffer " *reparent*")
9054           (current-article (gnus-summary-article-number))
9055           ;; First grab the marked article, otherwise one line up.
9056           (parent-article (if (not (null gnus-newsgroup-processable))
9057                               (car gnus-newsgroup-processable)
9058                             (save-excursion
9059                               (if (eq (forward-line -1) 0)
9060                                   (gnus-summary-article-number)
9061                                 (error "Beginning of summary buffer"))))))
9062       (unless (not (eq current-article parent-article))
9063         (error "An article may not be self-referential"))
9064       (let ((message-id (mail-header-id
9065                          (gnus-summary-article-header parent-article))))
9066         (unless (and message-id (not (equal message-id "")))
9067           (error "No message-id in desired parent"))
9068         (gnus-with-article current-article
9069           (save-restriction
9070             (goto-char (point-min))
9071             (message-narrow-to-head)
9072             (if (re-search-forward "^References: " nil t)
9073                 (progn
9074                   (re-search-forward "^[^ \t]" nil t)
9075                   (forward-line -1)
9076                   (end-of-line)
9077                   (insert " " message-id))
9078               (insert "References: " message-id "\n"))))
9079         (set-buffer gnus-summary-buffer)
9080         (gnus-summary-unmark-all-processable)
9081         (gnus-summary-update-article current-article)
9082         (gnus-summary-rethread-current)
9083         (gnus-message 3 "Article %d is now the child of article %d"
9084                       current-article parent-article)))))
9085
9086 (defun gnus-summary-toggle-threads (&optional arg)
9087   "Toggle showing conversation threads.
9088 If ARG is positive number, turn showing conversation threads on."
9089   (interactive "P")
9090   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9091     (setq gnus-show-threads
9092           (if (null arg) (not gnus-show-threads)
9093             (> (prefix-numeric-value arg) 0)))
9094     (gnus-summary-prepare)
9095     (gnus-summary-goto-subject current)
9096     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9097     (gnus-summary-position-point)))
9098
9099 (defun gnus-summary-show-all-threads ()
9100   "Show all threads."
9101   (interactive)
9102   (save-excursion
9103     (let ((buffer-read-only nil))
9104       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9105   (gnus-summary-position-point))
9106
9107 (defun gnus-summary-show-thread ()
9108   "Show thread subtrees.
9109 Returns nil if no thread was there to be shown."
9110   (interactive)
9111   (let ((buffer-read-only nil)
9112         (orig (point))
9113         ;; first goto end then to beg, to have point at beg after let
9114         (end (progn (end-of-line) (point)))
9115         (beg (progn (beginning-of-line) (point))))
9116     (prog1
9117         ;; Any hidden lines here?
9118         (search-forward "\r" end t)
9119       (subst-char-in-region beg end ?\^M ?\n t)
9120       (goto-char orig)
9121       (gnus-summary-position-point))))
9122
9123 (defun gnus-summary-hide-all-threads ()
9124   "Hide all thread subtrees."
9125   (interactive)
9126   (save-excursion
9127     (goto-char (point-min))
9128     (gnus-summary-hide-thread)
9129     (while (zerop (gnus-summary-next-thread 1 t))
9130       (gnus-summary-hide-thread)))
9131   (gnus-summary-position-point))
9132
9133 (defun gnus-summary-hide-thread ()
9134   "Hide thread subtrees.
9135 Returns nil if no threads were there to be hidden."
9136   (interactive)
9137   (let ((buffer-read-only nil)
9138         (start (point))
9139         (article (gnus-summary-article-number)))
9140     (goto-char start)
9141     ;; Go forward until either the buffer ends or the subthread
9142     ;; ends.
9143     (when (and (not (eobp))
9144                (or (zerop (gnus-summary-next-thread 1 t))
9145                    (goto-char (point-max))))
9146       (prog1
9147           (if (and (> (point) start)
9148                    (search-backward "\n" start t))
9149               (progn
9150                 (subst-char-in-region start (point) ?\n ?\^M)
9151                 (gnus-summary-goto-subject article))
9152             (goto-char start)
9153             nil)))))
9154
9155 (defun gnus-summary-go-to-next-thread (&optional previous)
9156   "Go to the same level (or less) next thread.
9157 If PREVIOUS is non-nil, go to previous thread instead.
9158 Return the article number moved to, or nil if moving was impossible."
9159   (let ((level (gnus-summary-thread-level))
9160         (way (if previous -1 1))
9161         (beg (point)))
9162     (forward-line way)
9163     (while (and (not (eobp))
9164                 (< level (gnus-summary-thread-level)))
9165       (forward-line way))
9166     (if (eobp)
9167         (progn
9168           (goto-char beg)
9169           nil)
9170       (setq beg (point))
9171       (prog1
9172           (gnus-summary-article-number)
9173         (goto-char beg)))))
9174
9175 (defun gnus-summary-next-thread (n &optional silent)
9176   "Go to the same level next N'th thread.
9177 If N is negative, search backward instead.
9178 Returns the difference between N and the number of skips actually
9179 done.
9180
9181 If SILENT, don't output messages."
9182   (interactive "p")
9183   (let ((backward (< n 0))
9184         (n (abs n)))
9185     (while (and (> n 0)
9186                 (gnus-summary-go-to-next-thread backward))
9187       (decf n))
9188     (unless silent
9189       (gnus-summary-position-point))
9190     (when (and (not silent) (/= 0 n))
9191       (gnus-message 7 "No more threads"))
9192     n))
9193
9194 (defun gnus-summary-prev-thread (n)
9195   "Go to the same level previous N'th thread.
9196 Returns the difference between N and the number of skips actually
9197 done."
9198   (interactive "p")
9199   (gnus-summary-next-thread (- n)))
9200
9201 (defun gnus-summary-go-down-thread ()
9202   "Go down one level in the current thread."
9203   (let ((children (gnus-summary-article-children)))
9204     (when children
9205       (gnus-summary-goto-subject (car children)))))
9206
9207 (defun gnus-summary-go-up-thread ()
9208   "Go up one level in the current thread."
9209   (let ((parent (gnus-summary-article-parent)))
9210     (when parent
9211       (gnus-summary-goto-subject parent))))
9212
9213 (defun gnus-summary-down-thread (n)
9214   "Go down thread N steps.
9215 If N is negative, go up instead.
9216 Returns the difference between N and how many steps down that were
9217 taken."
9218   (interactive "p")
9219   (let ((up (< n 0))
9220         (n (abs n)))
9221     (while (and (> n 0)
9222                 (if up (gnus-summary-go-up-thread)
9223                   (gnus-summary-go-down-thread)))
9224       (setq n (1- n)))
9225     (gnus-summary-position-point)
9226     (when (/= 0 n)
9227       (gnus-message 7 "Can't go further"))
9228     n))
9229
9230 (defun gnus-summary-up-thread (n)
9231   "Go up thread N steps.
9232 If N is negative, go up instead.
9233 Returns the difference between N and how many steps down that were
9234 taken."
9235   (interactive "p")
9236   (gnus-summary-down-thread (- n)))
9237
9238 (defun gnus-summary-top-thread ()
9239   "Go to the top of the thread."
9240   (interactive)
9241   (while (gnus-summary-go-up-thread))
9242   (gnus-summary-article-number))
9243
9244 (defun gnus-summary-kill-thread (&optional unmark)
9245   "Mark articles under current thread as read.
9246 If the prefix argument is positive, remove any kinds of marks.
9247 If the prefix argument is negative, tick articles instead."
9248   (interactive "P")
9249   (when unmark
9250     (setq unmark (prefix-numeric-value unmark)))
9251   (let ((articles (gnus-summary-articles-in-thread)))
9252     (save-excursion
9253       ;; Expand the thread.
9254       (gnus-summary-show-thread)
9255       ;; Mark all the articles.
9256       (while articles
9257         (gnus-summary-goto-subject (car articles))
9258         (cond ((null unmark)
9259                (gnus-summary-mark-article-as-read gnus-killed-mark))
9260               ((> unmark 0)
9261                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9262               (t
9263                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9264         (setq articles (cdr articles))))
9265     ;; Hide killed subtrees.
9266     (and (null unmark)
9267          gnus-thread-hide-killed
9268          (gnus-summary-hide-thread))
9269     ;; If marked as read, go to next unread subject.
9270     (when (null unmark)
9271       ;; Go to next unread subject.
9272       (gnus-summary-next-subject 1 t)))
9273   (gnus-set-mode-line 'summary))
9274
9275 ;; Summary sorting commands
9276
9277 (defun gnus-summary-sort-by-number (&optional reverse)
9278   "Sort the summary buffer by article number.
9279 Argument REVERSE means reverse order."
9280   (interactive "P")
9281   (gnus-summary-sort 'number reverse))
9282
9283 (defun gnus-summary-sort-by-author (&optional reverse)
9284   "Sort the summary buffer by author name alphabetically.
9285 If `case-fold-search' is non-nil, case of letters is ignored.
9286 Argument REVERSE means reverse order."
9287   (interactive "P")
9288   (gnus-summary-sort 'author reverse))
9289
9290 (defun gnus-summary-sort-by-subject (&optional reverse)
9291   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9292 If `case-fold-search' is non-nil, case of letters is ignored.
9293 Argument REVERSE means reverse order."
9294   (interactive "P")
9295   (gnus-summary-sort 'subject reverse))
9296
9297 (defun gnus-summary-sort-by-date (&optional reverse)
9298   "Sort the summary buffer by date.
9299 Argument REVERSE means reverse order."
9300   (interactive "P")
9301   (gnus-summary-sort 'date reverse))
9302
9303 (defun gnus-summary-sort-by-score (&optional reverse)
9304   "Sort the summary buffer by score.
9305 Argument REVERSE means reverse order."
9306   (interactive "P")
9307   (gnus-summary-sort 'score reverse))
9308
9309 (defun gnus-summary-sort-by-lines (&optional reverse)
9310   "Sort the summary buffer by the number of lines.
9311 Argument REVERSE means reverse order."
9312   (interactive "P")
9313   (gnus-summary-sort 'lines reverse))
9314
9315 (defun gnus-summary-sort-by-chars (&optional reverse)
9316   "Sort the summary buffer by article length.
9317 Argument REVERSE means reverse order."
9318   (interactive "P")
9319   (gnus-summary-sort 'chars reverse))
9320
9321 (defun gnus-summary-sort (predicate reverse)
9322   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9323   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9324          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9325          (gnus-thread-sort-functions
9326           (if (not reverse)
9327               thread
9328             `(lambda (t1 t2)
9329                (,thread t2 t1))))
9330          (gnus-sort-gathered-threads-function
9331           gnus-thread-sort-functions)
9332          (gnus-article-sort-functions
9333           (if (not reverse)
9334               article
9335             `(lambda (t1 t2)
9336                (,article t2 t1))))
9337          (buffer-read-only)
9338          (gnus-summary-prepare-hook nil))
9339     ;; We do the sorting by regenerating the threads.
9340     (gnus-summary-prepare)
9341     ;; Hide subthreads if needed.
9342     (when (and gnus-show-threads gnus-thread-hide-subtree)
9343       (gnus-summary-hide-all-threads))))
9344
9345 ;; Summary saving commands.
9346
9347 (defun gnus-summary-save-article (&optional n not-saved)
9348   "Save the current article using the default saver function.
9349 If N is a positive number, save the N next articles.
9350 If N is a negative number, save the N previous articles.
9351 If N is nil and any articles have been marked with the process mark,
9352 save those articles instead.
9353 The variable `gnus-default-article-saver' specifies the saver function."
9354   (interactive "P")
9355   (let* ((articles (gnus-summary-work-articles n))
9356          (save-buffer (save-excursion
9357                         (nnheader-set-temp-buffer " *Gnus Save*")))
9358          (num (length articles))
9359          header file)
9360     (dolist (article articles)
9361       (setq header (gnus-summary-article-header article))
9362       (if (not (vectorp header))
9363           ;; This is a pseudo-article.
9364           (if (assq 'name header)
9365               (gnus-copy-file (cdr (assq 'name header)))
9366             (gnus-message 1 "Article %d is unsaveable" article))
9367         ;; This is a real article.
9368         (save-window-excursion
9369           (gnus-summary-select-article t nil nil article))
9370         (save-excursion
9371           (set-buffer save-buffer)
9372           (erase-buffer)
9373           (insert-buffer-substring gnus-original-article-buffer))
9374         (setq file (gnus-article-save save-buffer file num))
9375         (gnus-summary-remove-process-mark article)
9376         (unless not-saved
9377           (gnus-summary-set-saved-mark article))))
9378     (gnus-kill-buffer save-buffer)
9379     (gnus-summary-position-point)
9380     (gnus-set-mode-line 'summary)
9381     n))
9382
9383 (defun gnus-summary-pipe-output (&optional arg)
9384   "Pipe the current article to a subprocess.
9385 If N is a positive number, pipe the N next articles.
9386 If N is a negative number, pipe the N previous articles.
9387 If N is nil and any articles have been marked with the process mark,
9388 pipe those articles instead."
9389   (interactive "P")
9390   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9391     (gnus-summary-save-article arg t))
9392   (gnus-configure-windows 'pipe))
9393
9394 (defun gnus-summary-save-article-mail (&optional arg)
9395   "Append the current article to an mail file.
9396 If N is a positive number, save the N next articles.
9397 If N is a negative number, save the N previous articles.
9398 If N is nil and any articles have been marked with the process mark,
9399 save those articles instead."
9400   (interactive "P")
9401   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9402     (gnus-summary-save-article arg)))
9403
9404 (defun gnus-summary-save-article-rmail (&optional arg)
9405   "Append the current article to an rmail file.
9406 If N is a positive number, save the N next articles.
9407 If N is a negative number, save the N previous articles.
9408 If N is nil and any articles have been marked with the process mark,
9409 save those articles instead."
9410   (interactive "P")
9411   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9412     (gnus-summary-save-article arg)))
9413
9414 (defun gnus-summary-save-article-file (&optional arg)
9415   "Append the current article to a file.
9416 If N is a positive number, save the N next articles.
9417 If N is a negative number, save the N previous articles.
9418 If N is nil and any articles have been marked with the process mark,
9419 save those articles instead."
9420   (interactive "P")
9421   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9422     (gnus-summary-save-article arg)))
9423
9424 (defun gnus-summary-write-article-file (&optional arg)
9425   "Write the current article to a file, deleting the previous file.
9426 If N is a positive number, save the N next articles.
9427 If N is a negative number, save the N previous articles.
9428 If N is nil and any articles have been marked with the process mark,
9429 save those articles instead."
9430   (interactive "P")
9431   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9432     (gnus-summary-save-article arg)))
9433
9434 (defun gnus-summary-save-article-body-file (&optional arg)
9435   "Append the current article body to a file.
9436 If N is a positive number, save the N next articles.
9437 If N is a negative number, save the N previous articles.
9438 If N is nil and any articles have been marked with the process mark,
9439 save those articles instead."
9440   (interactive "P")
9441   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9442     (gnus-summary-save-article arg)))
9443
9444 (defun gnus-summary-pipe-message (program)
9445   "Pipe the current article through PROGRAM."
9446   (interactive "sProgram: ")
9447   (gnus-summary-select-article)
9448   (let ((mail-header-separator ""))
9449     (gnus-eval-in-buffer-window gnus-article-buffer
9450       (save-restriction
9451         (widen)
9452         (let ((start (window-start))
9453               buffer-read-only)
9454           (message-pipe-buffer-body program)
9455           (set-window-start (get-buffer-window (current-buffer)) start))))))
9456
9457 (defun gnus-get-split-value (methods)
9458   "Return a value based on the split METHODS."
9459   (let (split-name method result match)
9460     (when methods
9461       (save-excursion
9462         (set-buffer gnus-original-article-buffer)
9463         (save-restriction
9464           (nnheader-narrow-to-headers)
9465           (while (and methods (not split-name))
9466             (goto-char (point-min))
9467             (setq method (pop methods))
9468             (setq match (car method))
9469             (when (cond
9470                    ((stringp match)
9471                     ;; Regular expression.
9472                     (ignore-errors
9473                       (re-search-forward match nil t)))
9474                    ((gnus-functionp match)
9475                     ;; Function.
9476                     (save-restriction
9477                       (widen)
9478                       (setq result (funcall match gnus-newsgroup-name))))
9479                    ((consp match)
9480                     ;; Form.
9481                     (save-restriction
9482                       (widen)
9483                       (setq result (eval match)))))
9484               (setq split-name (cdr method))
9485               (cond ((stringp result)
9486                      (push (expand-file-name
9487                             result gnus-article-save-directory)
9488                            split-name))
9489                     ((consp result)
9490                      (setq split-name (append result split-name)))))))))
9491     (nreverse split-name)))
9492
9493 (defun gnus-valid-move-group-p (group)
9494   (and (boundp group)
9495        (symbol-name group)
9496        (symbol-value group)
9497        (gnus-get-function (gnus-find-method-for-group
9498                            (symbol-name group)) 'request-accept-article t)))
9499
9500 (defun gnus-read-move-group-name (prompt default articles prefix)
9501   "Read a group name."
9502   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9503          (minibuffer-confirm-incomplete nil) ; XEmacs
9504          (prom
9505           (format "%s %s to:"
9506                   prompt
9507                   (if (> (length articles) 1)
9508                       (format "these %d articles" (length articles))
9509                     "this article")))
9510          (to-newsgroup
9511           (cond
9512            ((null split-name)
9513             (gnus-completing-read default prom
9514                                   gnus-active-hashtb
9515                                   'gnus-valid-move-group-p
9516                                   nil prefix
9517                                   'gnus-group-history))
9518            ((= 1 (length split-name))
9519             (gnus-completing-read (car split-name) prom
9520                                   gnus-active-hashtb
9521                                   'gnus-valid-move-group-p
9522                                   nil nil
9523                                   'gnus-group-history))
9524            (t
9525             (gnus-completing-read nil prom
9526                                   (mapcar (lambda (el) (list el))
9527                                           (nreverse split-name))
9528                                   nil nil nil
9529                                   'gnus-group-history))))
9530          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9531     (when to-newsgroup
9532       (if (or (string= to-newsgroup "")
9533               (string= to-newsgroup prefix))
9534           (setq to-newsgroup default))
9535       (unless to-newsgroup
9536         (error "No group name entered"))
9537       (or (gnus-active to-newsgroup)
9538           (gnus-activate-group to-newsgroup nil nil to-method)
9539           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9540                                      to-newsgroup))
9541               (or (and (gnus-request-create-group to-newsgroup to-method)
9542                        (gnus-activate-group
9543                         to-newsgroup nil nil to-method)
9544                        (gnus-subscribe-group to-newsgroup))
9545                   (error "Couldn't create group %s" to-newsgroup)))
9546           (error "No such group: %s" to-newsgroup)))
9547     to-newsgroup))
9548
9549 (defun gnus-summary-save-parts (type dir n &optional reverse)
9550   "Save parts matching TYPE to DIR.
9551 If REVERSE, save parts that do not match TYPE."
9552   (interactive
9553    (list (read-string "Save parts of type: " 
9554                       (or (car gnus-summary-save-parts-type-history)
9555                           gnus-summary-save-parts-default-mime)
9556                       'gnus-summary-save-parts-type-history)
9557          (setq gnus-summary-save-parts-last-directory
9558                (read-file-name "Save to directory: " 
9559                                gnus-summary-save-parts-last-directory
9560                                nil t))
9561          current-prefix-arg))
9562   (gnus-summary-iterate n
9563     (let ((gnus-display-mime-function nil)
9564           (gnus-inhibit-treatment t))
9565       (gnus-summary-select-article))
9566     (save-excursion
9567       (set-buffer gnus-article-buffer)
9568       (let ((handles (or gnus-article-mime-handles
9569                          (mm-dissect-buffer) (mm-uu-dissect))))
9570         (when handles
9571           (gnus-summary-save-parts-1 type dir handles reverse)
9572           (unless gnus-article-mime-handles ;; Don't destroy this case.
9573             (mm-destroy-parts handles)))))))
9574
9575 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9576   (if (stringp (car handle))
9577       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9578               (cdr handle))
9579     (when (if reverse
9580               (not (string-match type (mm-handle-media-type handle)))
9581             (string-match type (mm-handle-media-type handle)))
9582       (let ((file (expand-file-name
9583                    (file-name-nondirectory
9584                     (or
9585                      (mail-content-type-get
9586                       (mm-handle-disposition handle) 'filename)
9587                      (concat gnus-newsgroup-name
9588                              "." (number-to-string
9589                                   (cdr gnus-article-current)))))
9590                    dir)))
9591         (unless (file-exists-p file)
9592           (mm-save-part-to-file handle file))))))
9593
9594 ;; Summary extract commands
9595
9596 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9597   (let ((buffer-read-only nil)
9598         (article (gnus-summary-article-number))
9599         after-article b e)
9600     (unless (gnus-summary-goto-subject article)
9601       (error "No such article: %d" article))
9602     (gnus-summary-position-point)
9603     ;; If all commands are to be bunched up on one line, we collect
9604     ;; them here.
9605     (unless gnus-view-pseudos-separately
9606       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9607             files action)
9608         (while ps
9609           (setq action (cdr (assq 'action (car ps))))
9610           (setq files (list (cdr (assq 'name (car ps)))))
9611           (while (and ps (cdr ps)
9612                       (string= (or action "1")
9613                                (or (cdr (assq 'action (cadr ps))) "2")))
9614             (push (cdr (assq 'name (cadr ps))) files)
9615             (setcdr ps (cddr ps)))
9616           (when files
9617             (when (not (string-match "%s" action))
9618               (push " " files))
9619             (push " " files)
9620             (when (assq 'execute (car ps))
9621               (setcdr (assq 'execute (car ps))
9622                       (funcall (if (string-match "%s" action)
9623                                    'format 'concat)
9624                                action
9625                                (mapconcat
9626                                 (lambda (f)
9627                                   (if (equal f " ")
9628                                       f
9629                                     (gnus-quote-arg-for-sh-or-csh f)))
9630                                 files " ")))))
9631           (setq ps (cdr ps)))))
9632     (if (and gnus-view-pseudos (not not-view))
9633         (while pslist
9634           (when (assq 'execute (car pslist))
9635             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9636                                   (eq gnus-view-pseudos 'not-confirm)))
9637           (setq pslist (cdr pslist)))
9638       (save-excursion
9639         (while pslist
9640           (setq after-article (or (cdr (assq 'article (car pslist)))
9641                                   (gnus-summary-article-number)))
9642           (gnus-summary-goto-subject after-article)
9643           (forward-line 1)
9644           (setq b (point))
9645           (insert "    " (file-name-nondirectory
9646                           (cdr (assq 'name (car pslist))))
9647                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9648           (setq e (point))
9649           (forward-line -1)             ; back to `b'
9650           (gnus-add-text-properties
9651            b (1- e) (list 'gnus-number gnus-reffed-article-number
9652                           gnus-mouse-face-prop gnus-mouse-face))
9653           (gnus-data-enter
9654            after-article gnus-reffed-article-number
9655            gnus-unread-mark b (car pslist) 0 (- e b))
9656           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9657           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9658           (setq pslist (cdr pslist)))))))
9659
9660 (defun gnus-pseudos< (p1 p2)
9661   (let ((c1 (cdr (assq 'action p1)))
9662         (c2 (cdr (assq 'action p2))))
9663     (and c1 c2 (string< c1 c2))))
9664
9665 (defun gnus-request-pseudo-article (props)
9666   (cond ((assq 'execute props)
9667          (gnus-execute-command (cdr (assq 'execute props)))))
9668   (let ((gnus-current-article (gnus-summary-article-number)))
9669     (gnus-run-hooks 'gnus-mark-article-hook)))
9670
9671 (defun gnus-execute-command (command &optional automatic)
9672   (save-excursion
9673     (gnus-article-setup-buffer)
9674     (set-buffer gnus-article-buffer)
9675     (setq buffer-read-only nil)
9676     (let ((command (if automatic command
9677                      (read-string "Command: " (cons command 0)))))
9678       (erase-buffer)
9679       (insert "$ " command "\n\n")
9680       (if gnus-view-pseudo-asynchronously
9681           (start-process "gnus-execute" (current-buffer) shell-file-name
9682                          shell-command-switch command)
9683         (call-process shell-file-name nil t nil
9684                       shell-command-switch command)))))
9685
9686 ;; Summary kill commands.
9687
9688 (defun gnus-summary-edit-global-kill (article)
9689   "Edit the \"global\" kill file."
9690   (interactive (list (gnus-summary-article-number)))
9691   (gnus-group-edit-global-kill article))
9692
9693 (defun gnus-summary-edit-local-kill ()
9694   "Edit a local kill file applied to the current newsgroup."
9695   (interactive)
9696   (setq gnus-current-headers (gnus-summary-article-header))
9697   (gnus-group-edit-local-kill
9698    (gnus-summary-article-number) gnus-newsgroup-name))
9699
9700 ;;; Header reading.
9701
9702 (defun gnus-read-header (id &optional header)
9703   "Read the headers of article ID and enter them into the Gnus system."
9704   (let ((group gnus-newsgroup-name)
9705         (gnus-override-method
9706          (or
9707           gnus-override-method
9708           (and (gnus-news-group-p gnus-newsgroup-name)
9709                (car (gnus-refer-article-methods)))))
9710         where)
9711     ;; First we check to see whether the header in question is already
9712     ;; fetched.
9713     (if (stringp id)
9714         ;; This is a Message-ID.
9715         (setq header (or header (gnus-id-to-header id)))
9716       ;; This is an article number.
9717       (setq header (or header (gnus-summary-article-header id))))
9718     (if (and header
9719              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9720         ;; We have found the header.
9721         header
9722       ;; If this is a sparse article, we have to nix out its
9723       ;; previous entry in the thread hashtb.
9724       (when (and header
9725                  (gnus-summary-article-sparse-p (mail-header-number header)))
9726         (let* ((parent (gnus-parent-id (mail-header-references header)))
9727                (thread (and parent (gnus-id-to-thread parent))))
9728           (when thread
9729             (delq (assq header thread) thread))))
9730       ;; We have to really fetch the header to this article.
9731       (save-excursion
9732         (set-buffer nntp-server-buffer)
9733         (when (setq where (gnus-request-head id group))
9734           (nnheader-fold-continuation-lines)
9735           (goto-char (point-max))
9736           (insert ".\n")
9737           (goto-char (point-min))
9738           (insert "211 ")
9739           (princ (cond
9740                   ((numberp id) id)
9741                   ((cdr where) (cdr where))
9742                   (header (mail-header-number header))
9743                   (t gnus-reffed-article-number))
9744                  (current-buffer))
9745           (insert " Article retrieved.\n"))
9746         (if (or (not where)
9747                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9748             ()                          ; Malformed head.
9749           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9750             (when (and (stringp id)
9751                        (not (string= (gnus-group-real-name group)
9752                                      (car where))))
9753               ;; If we fetched by Message-ID and the article came
9754               ;; from a different group, we fudge some bogus article
9755               ;; numbers for this article.
9756               (mail-header-set-number header gnus-reffed-article-number))
9757             (save-excursion
9758               (set-buffer gnus-summary-buffer)
9759               (decf gnus-reffed-article-number)
9760               (gnus-remove-header (mail-header-number header))
9761               (push header gnus-newsgroup-headers)
9762               (setq gnus-current-headers header)
9763               (push (mail-header-number header) gnus-newsgroup-limit)))
9764           header)))))
9765
9766 (defun gnus-remove-header (number)
9767   "Remove header NUMBER from `gnus-newsgroup-headers'."
9768   (if (and gnus-newsgroup-headers
9769            (= number (mail-header-number (car gnus-newsgroup-headers))))
9770       (pop gnus-newsgroup-headers)
9771     (let ((headers gnus-newsgroup-headers))
9772       (while (and (cdr headers)
9773                   (not (= number (mail-header-number (cadr headers)))))
9774         (pop headers))
9775       (when (cdr headers)
9776         (setcdr headers (cddr headers))))))
9777
9778 ;;;
9779 ;;; summary highlights
9780 ;;;
9781
9782 (defun gnus-highlight-selected-summary ()
9783   "Highlight selected article in summary buffer."
9784   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9785   (when gnus-summary-selected-face
9786     (save-excursion
9787       (let* ((beg (progn (beginning-of-line) (point)))
9788              (end (progn (end-of-line) (point)))
9789              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9790              (from (if (get-text-property beg gnus-mouse-face-prop)
9791                        beg
9792                      (or (next-single-property-change
9793                           beg gnus-mouse-face-prop nil end)
9794                          beg)))
9795              (to
9796               (if (= from end)
9797                   (- from 2)
9798                 (or (next-single-property-change
9799                      from gnus-mouse-face-prop nil end)
9800                     end))))
9801         ;; If no mouse-face prop on line we will have to = from = end,
9802         ;; so we highlight the entire line instead.
9803         (when (= (+ to 2) from)
9804           (setq from beg)
9805           (setq to end))
9806         (if gnus-newsgroup-selected-overlay
9807             ;; Move old overlay.
9808             (gnus-move-overlay
9809              gnus-newsgroup-selected-overlay from to (current-buffer))
9810           ;; Create new overlay.
9811           (gnus-overlay-put
9812            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9813            'face gnus-summary-selected-face))))))
9814
9815 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9816 (defun gnus-summary-highlight-line ()
9817   "Highlight current line according to `gnus-summary-highlight'."
9818   (let* ((list gnus-summary-highlight)
9819          (p (point))
9820          (end (progn (end-of-line) (point)))
9821          ;; now find out where the line starts and leave point there.
9822          (beg (progn (beginning-of-line) (point)))
9823          (article (gnus-summary-article-number))
9824          (score (or (cdr (assq (or article gnus-current-article)
9825                                gnus-newsgroup-scored))
9826                     gnus-summary-default-score 0))
9827          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9828          (inhibit-read-only t))
9829     ;; Eval the cars of the lists until we find a match.
9830     (let ((default gnus-summary-default-score))
9831       (while (and list
9832                   (not (eval (caar list))))
9833         (setq list (cdr list))))
9834     (let ((face (cdar list)))
9835       (unless (eq face (get-text-property beg 'face))
9836         (gnus-put-text-property-excluding-characters-with-faces
9837          beg end 'face
9838          (setq face (if (boundp face) (symbol-value face) face)))
9839         (when gnus-summary-highlight-line-function
9840           (funcall gnus-summary-highlight-line-function article face))))
9841     (goto-char p)))
9842
9843 (defun gnus-update-read-articles (group unread &optional compute)
9844   "Update the list of read articles in GROUP."
9845   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9846          (entry (gnus-gethash group gnus-newsrc-hashtb))
9847          (info (nth 2 entry))
9848          (prev 1)
9849          (unread (sort (copy-sequence unread) '<))
9850          read)
9851     (if (or (not info) (not active))
9852         ;; There is no info on this group if it was, in fact,
9853         ;; killed.  Gnus stores no information on killed groups, so
9854         ;; there's nothing to be done.
9855         ;; One could store the information somewhere temporarily,
9856         ;; perhaps...  Hmmm...
9857         ()
9858       ;; Remove any negative articles numbers.
9859       (while (and unread (< (car unread) 0))
9860         (setq unread (cdr unread)))
9861       ;; Remove any expired article numbers
9862       (while (and unread (< (car unread) (car active)))
9863         (setq unread (cdr unread)))
9864       ;; Compute the ranges of read articles by looking at the list of
9865       ;; unread articles.
9866       (while unread
9867         (when (/= (car unread) prev)
9868           (push (if (= prev (1- (car unread))) prev
9869                   (cons prev (1- (car unread))))
9870                 read))
9871         (setq prev (1+ (car unread)))
9872         (setq unread (cdr unread)))
9873       (when (<= prev (cdr active))
9874         (push (cons prev (cdr active)) read))
9875       (setq read (if (> (length read) 1) (nreverse read) read))
9876       (if compute
9877           read
9878         (save-excursion
9879           (let (setmarkundo)
9880             ;; Propagate the read marks to the backend.
9881             (when (gnus-check-backend-function 'request-set-mark group)
9882               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9883                     (add (gnus-remove-from-range read (gnus-info-read info))))
9884                 (when (or add del)
9885                   (unless (gnus-check-group group)
9886                     (error "Can't open server for %s" group))
9887                   (gnus-request-set-mark
9888                    group (delq nil (list (if add (list add 'add '(read)))
9889                                          (if del (list del 'del '(read))))))
9890                   (setq setmarkundo
9891                         `(gnus-request-set-mark
9892                           ,group
9893                           ',(delq nil (list
9894                                        (if del (list del 'add '(read)))
9895                                        (if add (list add 'del '(read))))))))))
9896             (set-buffer gnus-group-buffer)
9897             (gnus-undo-register
9898               `(progn
9899                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9900                  (gnus-info-set-read ',info ',(gnus-info-read info))
9901                  (gnus-get-unread-articles-in-group ',info 
9902                                                     (gnus-active ,group))
9903                  (gnus-group-update-group ,group t)
9904                  ,setmarkundo))))
9905         ;; Enter this list into the group info.
9906         (gnus-info-set-read info read)
9907         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9908         (gnus-get-unread-articles-in-group info (gnus-active group))
9909         t))))
9910
9911 (defun gnus-offer-save-summaries ()
9912   "Offer to save all active summary buffers."
9913   (save-excursion
9914     (let ((buflist (buffer-list))
9915           buffers bufname)
9916       ;; Go through all buffers and find all summaries.
9917       (while buflist
9918         (and (setq bufname (buffer-name (car buflist)))
9919              (string-match "Summary" bufname)
9920              (save-excursion
9921                (set-buffer bufname)
9922                ;; We check that this is, indeed, a summary buffer.
9923                (and (eq major-mode 'gnus-summary-mode)
9924                     ;; Also make sure this isn't bogus.
9925                     gnus-newsgroup-prepared
9926                     ;; Also make sure that this isn't a dead summary buffer.
9927                     (not gnus-dead-summary-mode)))
9928              (push bufname buffers))
9929         (setq buflist (cdr buflist)))
9930       ;; Go through all these summary buffers and offer to save them.
9931       (when buffers
9932         (map-y-or-n-p
9933          "Update summary buffer %s? "
9934          (lambda (buf)
9935            (switch-to-buffer buf)
9936            (gnus-summary-exit))
9937          buffers)))))
9938
9939
9940 ;;; @ for mime-partial
9941 ;;;
9942
9943 (defun gnus-request-partial-message ()
9944   (save-excursion
9945     (let ((number (gnus-summary-article-number))
9946           (group gnus-newsgroup-name)
9947           (mother gnus-article-buffer))
9948       (set-buffer (get-buffer-create " *Partial Article*"))
9949       (erase-buffer)
9950       (setq mime-preview-buffer mother)
9951       (gnus-request-article-this-buffer number group)
9952       (mime-parse-buffer)
9953       )))
9954
9955 (autoload 'mime-combine-message/partial-pieces-automatically
9956   "mime-partial"
9957   "Internal method to combine message/partial messages automatically.")
9958
9959 (mime-add-condition
9960  'action '((type . message)(subtype . partial)
9961            (major-mode . gnus-original-article-mode)
9962            (method . mime-combine-message/partial-pieces-automatically)
9963            (summary-buffer-exp . gnus-summary-buffer)
9964            (request-partial-message-method . gnus-request-partial-message)
9965            ))
9966
9967
9968 ;;; @ for message/rfc822
9969 ;;;
9970
9971 (defun gnus-mime-extract-message/rfc822 (entity situation)
9972   (let (group article num cwin swin cur)
9973     (with-temp-buffer
9974       (mime-insert-entity-content entity)
9975       (setq group (or (cdr (assq 'group situation))
9976                       (completing-read "Group: "
9977                                        gnus-active-hashtb
9978                                        nil
9979                                        (gnus-read-active-file-p)
9980                                        gnus-newsgroup-name))
9981             article (gnus-request-accept-article group)))
9982     (when (and (consp article)
9983                (numberp (setq article (cdr article))))
9984       (setq num (1+ (or (cdr (assq 'number situation)) 0))
9985             cwin (get-buffer-window (current-buffer) t))
9986       (save-window-excursion
9987         (if (setq swin (get-buffer-window gnus-summary-buffer t))
9988             (select-window swin)
9989           (set-buffer gnus-summary-buffer))
9990         (setq cur gnus-current-article)
9991         (forward-line num)
9992         (let (gnus-show-threads)
9993           (gnus-summary-goto-subject article t))
9994         (gnus-summary-clear-mark-forward 1)
9995         (gnus-summary-goto-subject cur))
9996       (when (and cwin (window-frame cwin))
9997         (select-frame (window-frame cwin)))
9998       (when (boundp 'mime-acting-situation-to-override)
9999         (set-alist 'mime-acting-situation-to-override
10000                    'group
10001                    group)
10002         (set-alist 'mime-acting-situation-to-override
10003                    'after-method
10004                    `(progn
10005                       (save-current-buffer
10006                         (set-buffer gnus-group-buffer)
10007                         (gnus-activate-group ,group))
10008                       (gnus-summary-goto-article ,cur
10009                                                  gnus-show-all-headers)))
10010         (set-alist 'mime-acting-situation-to-override
10011                    'number num)))))
10012
10013 (mime-add-condition
10014  'action '((type . message)(subtype . rfc822)
10015            (major-mode . gnus-original-article-mode)
10016            (method . gnus-mime-extract-message/rfc822)
10017            (mode . "extract")
10018            ))
10019
10020 (mime-add-condition
10021  'action '((type . message)(subtype . news)
10022            (major-mode . gnus-original-article-mode)
10023            (method . gnus-mime-extract-message/rfc822)
10024            (mode . "extract")
10025            ))
10026
10027 (defun gnus-mime-extract-multipart (entity situation)
10028   (let ((children (mime-entity-children entity))
10029         mime-acting-situation-to-override
10030         f)
10031     (while children
10032       (mime-play-entity (car children)
10033                         (cons (assq 'mode situation)
10034                               mime-acting-situation-to-override))
10035       (setq children (cdr children)))
10036     (if (setq f (cdr (assq 'after-method
10037                            mime-acting-situation-to-override)))
10038         (eval f)
10039       )))
10040
10041 (mime-add-condition
10042  'action '((type . multipart)
10043            (method . gnus-mime-extract-multipart)
10044            (mode . "extract")
10045            )
10046  'with-default)
10047
10048
10049 ;;; @ end
10050 ;;;
10051
10052 (defun gnus-summary-setup-default-charset ()
10053   "Setup newsgroup default charset."
10054   (if (equal gnus-newsgroup-name "nndraft:drafts")
10055       (setq gnus-newsgroup-charset nil)
10056     (let* ((name (and gnus-newsgroup-name
10057                       (gnus-group-real-name gnus-newsgroup-name)))
10058            (ignored-charsets
10059             (or gnus-newsgroup-ephemeral-ignored-charsets
10060                 (append
10061                  (and gnus-newsgroup-name
10062                       (or (gnus-group-find-parameter gnus-newsgroup-name
10063                                                      'ignored-charsets t)
10064                           (let ((alist gnus-group-ignored-charsets-alist)
10065                                 elem (charsets nil))
10066                             (while (setq elem (pop alist))
10067                               (when (and name
10068                                          (string-match (car elem) name))
10069                                 (setq alist nil
10070                                       charsets (cdr elem))))
10071                             charsets)))
10072                  gnus-newsgroup-ignored-charsets))))
10073       (setq gnus-newsgroup-charset
10074             (or gnus-newsgroup-ephemeral-charset
10075                 (and gnus-newsgroup-name
10076                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
10077                          (let ((alist gnus-group-charset-alist)
10078                                elem charset)
10079                            (while (setq elem (pop alist))
10080                              (when (and name
10081                                         (string-match (car elem) name))
10082                                (setq alist nil
10083                                      charset (cadr elem))))
10084                            charset)))
10085                 gnus-default-charset))
10086       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10087            ignored-charsets))))
10088
10089 ;;;
10090 ;;; Mime Commands
10091 ;;;
10092
10093 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10094   "Display the current article buffer fully MIME-buttonized.
10095 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10096 treated as multipart/mixed."
10097   (interactive "P")
10098   (require 'gnus-art)
10099   (let ((gnus-unbuttonized-mime-types nil)
10100         (gnus-mime-display-multipart-as-mixed show-all-parts))
10101     (gnus-summary-show-article)))
10102
10103 (defun gnus-summary-repair-multipart (article)
10104   "Add a Content-Type header to a multipart article without one."
10105   (interactive (list (gnus-summary-article-number)))
10106   (gnus-with-article article
10107     (message-narrow-to-head)
10108     (message-remove-header "Mime-Version")
10109     (goto-char (point-max))
10110     (insert "Mime-Version: 1.0\n")
10111     (widen)
10112     (when (search-forward "\n--" nil t)
10113       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10114         (message-narrow-to-head)
10115         (message-remove-header "Content-Type")
10116         (goto-char (point-max))
10117         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10118                         separator))
10119         (widen))))
10120   (let (gnus-mark-article-hook)
10121     (gnus-summary-select-article t t nil article)))
10122
10123 (defun gnus-summary-toggle-display-buttonized ()
10124   "Toggle the buttonizing of the article buffer."
10125   (interactive)
10126   (require 'gnus-art)
10127   (if (setq gnus-inhibit-mime-unbuttonizing
10128             (not gnus-inhibit-mime-unbuttonizing))
10129       (let ((gnus-unbuttonized-mime-types nil))
10130         (gnus-summary-show-article))
10131     (gnus-summary-show-article)))
10132
10133 ;;;
10134 ;;; Intelli-mouse commmands
10135 ;;;
10136
10137 (defun gnus-wheel-summary-scroll (event)
10138   (interactive "e")
10139   (let ((amount (if (memq 'shift (event-modifiers event))
10140                     (car gnus-wheel-scroll-amount)
10141                   (cdr gnus-wheel-scroll-amount)))
10142         (direction (- (* (static-if (featurep 'xemacs)
10143                              (event-button event)
10144                            (cond ((eq 'mouse-4 (event-basic-type event))
10145                                   4)
10146                                  ((eq 'mouse-5 (event-basic-type event))
10147                                   5)))
10148                          2) 9))
10149         edge)
10150     (gnus-summary-scroll-up (* amount direction))
10151     (when (gnus-eval-in-buffer-window gnus-article-buffer
10152             (save-restriction
10153               (widen)
10154               (and (if (< 0 direction)
10155                        (gnus-article-next-page 0)
10156                      (gnus-article-prev-page 0)
10157                      (bobp))
10158                    (if (setq edge (get-text-property
10159                                    (point-min) 'gnus-wheel-edge))
10160                        (setq edge (* edge direction))
10161                      (setq edge -1))
10162                    (or (plusp edge)
10163                        (let ((buffer-read-only nil)
10164                              (inhibit-read-only t))
10165                          (put-text-property (point-min) (point-max)
10166                                             'gnus-wheel-edge direction)
10167                          nil))
10168                    (or (> edge gnus-wheel-edge-resistance)
10169                        (let ((buffer-read-only nil)
10170                              (inhibit-read-only t))
10171                          (put-text-property (point-min) (point-max)
10172                                             'gnus-wheel-edge
10173                                             (* (1+ edge) direction))
10174                          nil))
10175                    (eq last-command 'gnus-wheel-summary-scroll))))
10176       (gnus-summary-next-article nil nil (minusp direction)))))
10177
10178 (defun gnus-wheel-install ()
10179   "Enable mouse wheel support on summary window."
10180   (when gnus-use-wheel
10181     (let ((keys
10182            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
10183       (dolist (key keys)
10184         (define-key gnus-summary-mode-map key
10185           'gnus-wheel-summary-scroll)))))
10186
10187 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
10188
10189 ;;;
10190 ;;; Traditional PGP commmands
10191 ;;;
10192
10193 (defun gnus-summary-decrypt-article (&optional force)
10194   "Decrypt the current article in traditional PGP way.
10195 This will have permanent effect only in mail groups.
10196 If FORCE is non-nil, allow editing of articles even in read-only
10197 groups."
10198   (interactive "P")
10199   (gnus-summary-select-article t)
10200   (gnus-eval-in-buffer-window gnus-article-buffer
10201     (save-excursion
10202       (save-restriction
10203         (widen)
10204         (goto-char (point-min))
10205         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10206           (error "Not a traditional PGP message!"))
10207         (let ((armor-start (match-beginning 0)))
10208           (if (and (pgg-decrypt-region armor-start (point-max))
10209                    (or force (not (gnus-group-read-only-p))))
10210               (let ((inhibit-read-only t)
10211                     buffer-read-only)
10212                 (delete-region armor-start
10213                                (progn
10214                                  (re-search-forward "^-+END PGP" nil t)
10215                                  (beginning-of-line 2)
10216                                  (point)))
10217                 (insert-buffer-substring pgg-output-buffer))))))))
10218
10219 (defun gnus-summary-verify-article ()
10220   "Verify the current article in traditional PGP way."
10221   (interactive)
10222   (save-excursion
10223     (set-buffer gnus-original-article-buffer)
10224     (goto-char (point-min))
10225     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10226       (error "Not a traditional PGP message!"))
10227     (re-search-forward "^-+END PGP" nil t)
10228     (beginning-of-line 2)
10229     (call-interactively (function pgg-verify-region))))
10230
10231 ;;;
10232 ;;; Generic summary marking commands
10233 ;;;
10234
10235 (defvar gnus-summary-marking-alist
10236   '((read gnus-del-mark "d")
10237     (unread gnus-unread-mark "u")
10238     (ticked gnus-ticked-mark "!")
10239     (dormant gnus-dormant-mark "?")
10240     (expirable gnus-expirable-mark "e"))
10241   "An alist of names/marks/keystrokes.")
10242
10243 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10244 (defvar gnus-summary-mark-map)
10245
10246 (defun gnus-summary-make-all-marking-commands ()
10247   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10248   (dolist (elem gnus-summary-marking-alist)
10249     (apply 'gnus-summary-make-marking-command elem)))
10250
10251 (defun gnus-summary-make-marking-command (name mark keystroke)
10252   (let ((map (make-sparse-keymap)))
10253     (define-key gnus-summary-generic-mark-map keystroke map)
10254     (dolist (lway `((next "next" next nil "n")
10255                     (next-unread "next unread" next t "N")
10256                     (prev "previous" prev nil "p")
10257                     (prev-unread "previous unread" prev t "P")
10258                     (nomove "" nil nil ,keystroke)))
10259       (let ((func (gnus-summary-make-marking-command-1
10260                    mark (car lway) lway name)))
10261         (setq func (eval func))
10262         (define-key map (nth 4 lway) func)))))
10263
10264 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10265   `(defun ,(intern
10266             (format "gnus-summary-put-mark-as-%s%s"
10267                     name (if (eq way 'nomove)
10268                              ""
10269                            (concat "-" (symbol-name way)))))
10270      (n)
10271      ,(format
10272        "Mark the current article as %s%s.
10273 If N, the prefix, then repeat N times.
10274 If N is negative, move in reverse order.
10275 The difference between N and the actual number of articles marked is
10276 returned."
10277        name (car (cdr lway)))
10278      (interactive "p")
10279      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10280
10281 (defun gnus-summary-generic-mark (n mark move unread)
10282   "Mark N articles with MARK."
10283   (unless (eq major-mode 'gnus-summary-mode)
10284     (error "This command can only be used in the summary buffer"))
10285   (gnus-summary-show-thread)
10286   (let ((nummove
10287          (cond
10288           ((eq move 'next) 1)
10289           ((eq move 'prev) -1)
10290           (t 0))))
10291     (if (zerop nummove)
10292         (setq n 1)
10293       (when (< n 0)
10294         (setq n (abs n)
10295               nummove (* -1 nummove))))
10296     (while (and (> n 0)
10297                 (gnus-summary-mark-article nil mark)
10298                 (zerop (gnus-summary-next-subject nummove unread t)))
10299       (setq n (1- n)))
10300     (when (/= 0 n)
10301       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10302     (gnus-summary-recenter)
10303     (gnus-summary-position-point)
10304     (gnus-set-mode-line 'summary)
10305     n))
10306
10307 (gnus-summary-make-all-marking-commands)
10308
10309 (gnus-ems-redefine)
10310
10311 (provide 'gnus-sum)
10312
10313 (run-hooks 'gnus-sum-load-hook)
10314
10315 ;;; gnus-sum.el ends here