Synch with Oort Gnus.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
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-no-mark ?  ;Whitespace
462   "*Mark used for articles that have no other secondary mark."
463   :group 'gnus-summary-marks
464   :type 'character)
465
466 (defcustom gnus-ancient-mark ?O
467   "*Mark used for ancient articles."
468   :group 'gnus-summary-marks
469   :type 'character)
470
471 (defcustom gnus-sparse-mark ?Q
472   "*Mark used for sparsely reffed articles."
473   :group 'gnus-summary-marks
474   :type 'character)
475
476 (defcustom gnus-canceled-mark ?G
477   "*Mark used for canceled articles."
478   :group 'gnus-summary-marks
479   :type 'character)
480
481 (defcustom gnus-duplicate-mark ?M
482   "*Mark used for duplicate articles."
483   :group 'gnus-summary-marks
484   :type 'character)
485
486 (defcustom gnus-undownloaded-mark ?@
487   "*Mark used for articles that weren't downloaded."
488   :group 'gnus-summary-marks
489   :type 'character)
490
491 (defcustom gnus-downloadable-mark ?%
492   "*Mark used for articles that are to be downloaded."
493   :group 'gnus-summary-marks
494   :type 'character)
495
496 (defcustom gnus-unsendable-mark ?=
497   "*Mark used for articles that won't be sent."
498   :group 'gnus-summary-marks
499   :type 'character)
500
501 (defcustom gnus-score-over-mark ?+
502   "*Score mark used for articles with high scores."
503   :group 'gnus-summary-marks
504   :type 'character)
505
506 (defcustom gnus-score-below-mark ?-
507   "*Score mark used for articles with low scores."
508   :group 'gnus-summary-marks
509   :type 'character)
510
511 (defcustom gnus-empty-thread-mark ?  ;Whitespace
512   "*There is no thread under the article."
513   :group 'gnus-summary-marks
514   :type 'character)
515
516 (defcustom gnus-not-empty-thread-mark ?=
517   "*There is a thread under the article."
518   :group 'gnus-summary-marks
519   :type 'character)
520
521 (defcustom gnus-view-pseudo-asynchronously nil
522   "*If non-nil, Gnus will view pseudo-articles asynchronously."
523   :group 'gnus-extract-view
524   :type 'boolean)
525
526 (defcustom gnus-auto-expirable-marks
527   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
528         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
529         gnus-souped-mark gnus-duplicate-mark)
530   "*The list of marks converted into expiration if a group is auto-expirable."
531   :version "21.1"
532   :group 'gnus-summary
533   :type '(repeat character))
534
535 (defcustom gnus-inhibit-user-auto-expire t
536   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
537   :version "21.1"
538   :group 'gnus-summary
539   :type 'boolean)
540
541 (defcustom gnus-view-pseudos nil
542   "*If `automatic', pseudo-articles will be viewed automatically.
543 If `not-confirm', pseudos will be viewed automatically, and the user
544 will not be asked to confirm the command."
545   :group 'gnus-extract-view
546   :type '(choice (const :tag "off" nil)
547                  (const automatic)
548                  (const not-confirm)))
549
550 (defcustom gnus-view-pseudos-separately t
551   "*If non-nil, one pseudo-article will be created for each file to be viewed.
552 If nil, all files that use the same viewing command will be given as a
553 list of parameters to that command."
554   :group 'gnus-extract-view
555   :type 'boolean)
556
557 (defcustom gnus-insert-pseudo-articles t
558   "*If non-nil, insert pseudo-articles when decoding articles."
559   :group 'gnus-extract-view
560   :type 'boolean)
561
562 (defcustom gnus-summary-dummy-line-format
563   "  %(:                          :%) %S\n"
564   "*The format specification for the dummy roots in the summary buffer.
565 It works along the same lines as a normal formatting string,
566 with some simple extensions.
567
568 %S  The subject"
569   :group 'gnus-threading
570   :type 'string)
571
572 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
573   "*The format specification for the summary mode line.
574 It works along the same lines as a normal formatting string,
575 with some simple extensions:
576
577 %G  Group name
578 %p  Unprefixed group name
579 %A  Current article number
580 %z  Current article score
581 %V  Gnus version
582 %U  Number of unread articles in the group
583 %e  Number of unselected articles in the group
584 %Z  A string with unread/unselected article counts
585 %g  Shortish group name
586 %S  Subject of the current article
587 %u  User-defined spec
588 %s  Current score file name
589 %d  Number of dormant articles
590 %r  Number of articles that have been marked as read in this session
591 %E  Number of articles expunged by the score files"
592   :group 'gnus-summary-format
593   :type 'string)
594
595 (defcustom gnus-list-identifiers nil
596   "Regexp that matches list identifiers to be removed from subject.
597 This can also be a list of regexps."
598   :version "21.1"
599   :group 'gnus-summary-format
600   :group 'gnus-article-hiding
601   :type '(choice (const :tag "none" nil)
602                  (regexp :value ".*")
603                  (repeat :value (".*") regexp)))
604
605 (defcustom gnus-summary-mark-below 0
606   "*Mark all articles with a score below this variable as read.
607 This variable is local to each summary buffer and usually set by the
608 score file."
609   :group 'gnus-score-default
610   :type 'integer)
611
612 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
613   "*List of functions used for sorting articles in the summary buffer.
614 This variable is only used when not using a threaded display."
615   :group 'gnus-summary-sort
616   :type '(repeat (choice (function-item gnus-article-sort-by-number)
617                          (function-item gnus-article-sort-by-author)
618                          (function-item gnus-article-sort-by-subject)
619                          (function-item gnus-article-sort-by-date)
620                          (function-item gnus-article-sort-by-score)
621                          (function :tag "other"))))
622
623 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
624   "*List of functions used for sorting threads in the summary buffer.
625 By default, threads are sorted by article number.
626
627 Each function takes two threads and return non-nil if the first thread
628 should be sorted before the other.  If you use more than one function,
629 the primary sort function should be the last.  You should probably
630 always include `gnus-thread-sort-by-number' in the list of sorting
631 functions -- preferably first.
632
633 Ready-made functions include `gnus-thread-sort-by-number',
634 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
635 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
636 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
637   :group 'gnus-summary-sort
638   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
639                          (function-item gnus-thread-sort-by-author)
640                          (function-item gnus-thread-sort-by-subject)
641                          (function-item gnus-thread-sort-by-date)
642                          (function-item gnus-thread-sort-by-score)
643                          (function-item gnus-thread-sort-by-total-score)
644                          (function :tag "other"))))
645
646 (defcustom gnus-thread-score-function '+
647   "*Function used for calculating the total score of a thread.
648
649 The function is called with the scores of the article and each
650 subthread and should then return the score of the thread.
651
652 Some functions you can use are `+', `max', or `min'."
653   :group 'gnus-summary-sort
654   :type 'function)
655
656 (defcustom gnus-summary-expunge-below nil
657   "All articles that have a score less than this variable will be expunged.
658 This variable is local to the summary buffers."
659   :group 'gnus-score-default
660   :type '(choice (const :tag "off" nil)
661                  integer))
662
663 (defcustom gnus-thread-expunge-below nil
664   "All threads that have a total score less than this variable will be expunged.
665 See `gnus-thread-score-function' for en explanation of what a
666 \"thread score\" is.
667
668 This variable is local to the summary buffers."
669   :group 'gnus-threading
670   :group 'gnus-score-default
671   :type '(choice (const :tag "off" nil)
672                  integer))
673
674 (defcustom gnus-summary-mode-hook nil
675   "*A hook for Gnus summary mode.
676 This hook is run before any variables are set in the summary buffer."
677   :options '(turn-on-gnus-mailing-list-mode)
678   :group 'gnus-summary-various
679   :type 'hook)
680
681 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
682 (when (featurep 'xemacs)
683   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
684   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
685   (add-hook 'gnus-summary-mode-hook
686             'gnus-xmas-switch-horizontal-scrollbar-off))
687
688 (defcustom gnus-summary-menu-hook nil
689   "*Hook run after the creation of the summary mode menu."
690   :group 'gnus-summary-visual
691   :type 'hook)
692
693 (defcustom gnus-summary-exit-hook nil
694   "*A hook called on exit from the summary buffer.
695 It will be called with point in the group buffer."
696   :group 'gnus-summary-exit
697   :type 'hook)
698
699 (defcustom gnus-summary-prepare-hook nil
700   "*A hook called after the summary buffer has been generated.
701 If you want to modify the summary buffer, you can use this hook."
702   :group 'gnus-summary-various
703   :type 'hook)
704
705 (defcustom gnus-summary-prepared-hook nil
706   "*A hook called as the last thing after the summary buffer has been generated."
707   :group 'gnus-summary-various
708   :type 'hook)
709
710 (defcustom gnus-summary-generate-hook nil
711   "*A hook run just before generating the summary buffer.
712 This hook is commonly used to customize threading variables and the
713 like."
714   :group 'gnus-summary-various
715   :type 'hook)
716
717 (defcustom gnus-select-group-hook nil
718   "*A hook called when a newsgroup is selected.
719
720 If you'd like to simplify subjects like the
721 `gnus-summary-next-same-subject' command does, you can use the
722 following hook:
723
724  (setq gnus-select-group-hook
725       (list
726         (lambda ()
727           (mapcar (lambda (header)
728                      (mail-header-set-subject
729                       header
730                       (gnus-simplify-subject
731                        (mail-header-subject header) 're-only)))
732                   gnus-newsgroup-headers))))"
733   :group 'gnus-group-select
734   :type 'hook)
735
736 (defcustom gnus-select-article-hook nil
737   "*A hook called when an article is selected."
738   :group 'gnus-summary-choose
739   :type 'hook)
740
741 (defcustom gnus-visual-mark-article-hook
742   (list 'gnus-highlight-selected-summary)
743   "*Hook run after selecting an article in the summary buffer.
744 It is meant to be used for highlighting the article in some way.  It
745 is not run if `gnus-visual' is nil."
746   :group 'gnus-summary-visual
747   :type 'hook)
748
749 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
750   "*A hook called before parsing the headers."
751   :group 'gnus-various
752   :type 'hook)
753
754 (defcustom gnus-exit-group-hook nil
755   "*A hook called when exiting summary mode.
756 This hook is not called from the non-updating exit commands like `Q'."
757   :group 'gnus-various
758   :type 'hook)
759
760 (defcustom gnus-summary-update-hook
761   (list 'gnus-summary-highlight-line)
762   "*A hook called when a summary line is changed.
763 The hook will not be called if `gnus-visual' is nil.
764
765 The default function `gnus-summary-highlight-line' will
766 highlight the line according to the `gnus-summary-highlight'
767 variable."
768   :group 'gnus-summary-visual
769   :type 'hook)
770
771 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
772   "*A hook called when an article is selected for the first time.
773 The hook is intended to mark an article as read (or unread)
774 automatically when it is selected."
775   :group 'gnus-summary-choose
776   :type 'hook)
777
778 (defcustom gnus-group-no-more-groups-hook nil
779   "*A hook run when returning to group mode having no more (unread) groups."
780   :group 'gnus-group-select
781   :type 'hook)
782
783 (defcustom gnus-ps-print-hook nil
784   "*A hook run before ps-printing something from Gnus."
785   :group 'gnus-summary
786   :type 'hook)
787
788 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
789   "Face used for highlighting the current article in the summary buffer."
790   :group 'gnus-summary-visual
791   :type 'face)
792
793 (defcustom gnus-summary-highlight
794   '(((= mark gnus-canceled-mark)
795      . gnus-summary-cancelled-face)
796     ((and (> score default)
797           (or (= mark gnus-dormant-mark)
798               (= mark gnus-ticked-mark)))
799      . gnus-summary-high-ticked-face)
800     ((and (< score default)
801           (or (= mark gnus-dormant-mark)
802               (= mark gnus-ticked-mark)))
803      . gnus-summary-low-ticked-face)
804     ((or (= mark gnus-dormant-mark)
805          (= mark gnus-ticked-mark))
806      . gnus-summary-normal-ticked-face)
807     ((and (> score default) (= mark gnus-ancient-mark))
808      . gnus-summary-high-ancient-face)
809     ((and (< score default) (= mark gnus-ancient-mark))
810      . gnus-summary-low-ancient-face)
811     ((= mark gnus-ancient-mark)
812      . gnus-summary-normal-ancient-face)
813     ((and (> score default) (= mark gnus-unread-mark))
814      . gnus-summary-high-unread-face)
815     ((and (< score default) (= mark gnus-unread-mark))
816      . gnus-summary-low-unread-face)
817     ((and (memq article gnus-newsgroup-incorporated)
818           (= mark gnus-unread-mark))
819      . gnus-summary-incorporated-face)
820     ((= mark gnus-unread-mark)
821      . gnus-summary-normal-unread-face)
822     ((and (> score default) (memq mark (list gnus-downloadable-mark
823                                              gnus-undownloaded-mark)))
824      . gnus-summary-high-unread-face)
825     ((and (< score default) (memq mark (list gnus-downloadable-mark
826                                              gnus-undownloaded-mark)))
827      . gnus-summary-low-unread-face)
828     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
829      . gnus-summary-normal-unread-face)
830     ((> score default)
831      . gnus-summary-high-read-face)
832     ((< score default)
833      . gnus-summary-low-read-face)
834     (t
835      . gnus-summary-normal-read-face))
836   "*Controls the highlighting of summary buffer lines.
837
838 A list of (FORM . FACE) pairs.  When deciding how a a particular
839 summary line should be displayed, each form is evaluated.  The content
840 of the face field after the first true form is used.  You can change
841 how those summary lines are displayed, by editing the face field.
842
843 You can use the following variables in the FORM field.
844
845 score:   The articles score
846 default: The default article score.
847 below:   The score below which articles are automatically marked as read.
848 mark:    The articles mark."
849   :group 'gnus-summary-visual
850   :type '(repeat (cons (sexp :tag "Form" nil)
851                        face)))
852
853 (defcustom gnus-alter-header-function nil
854   "Function called to allow alteration of article header structures.
855 The function is called with one parameter, the article header vector,
856 which it may alter in any way.")
857
858 (defvar gnus-decode-encoded-word-function
859   (mime-find-field-decoder 'From 'nov)
860   "Variable that says which function should be used to decode a string with encoded words.")
861
862 (defcustom gnus-extra-headers nil
863   "*Extra headers to parse."
864   :version "21.1"
865   :group 'gnus-summary
866   :type '(repeat symbol))
867
868 (defcustom gnus-ignored-from-addresses
869   (and user-mail-address (regexp-quote user-mail-address))
870   "*Regexp of From headers that may be suppressed in favor of To headers."
871   :version "21.1"
872   :group 'gnus-summary
873   :type 'regexp)
874
875 (gnus-define-group-parameter
876  charset
877  :function-document
878  "Return the default charset of GROUP."
879  :variable gnus-group-charset-alist
880  :variable-default 
881  '(("\\(^\\|:\\)hk\\>\\|\\(^\\|:\\)tw\\>\\|\\<big5\\>" cn-big5)
882    ("\\(^\\|:\\)cn\\>\\|\\<chinese\\>" cn-gb-2312)
883    ("\\(^\\|:\\)fj\\>\\|\\(^\\|:\\)japan\\>" iso-2022-jp-2)
884    ("\\(^\\|:\\)tnn\\>\\|\\(^\\|:\\)pin\\>\\|\\(^\\|:\\)sci.lang.japan" iso-2022-7bit)
885    ("\\(^\\|:\\)relcom\\>" koi8-r)
886    ("\\(^\\|:\\)fido7\\>" koi8-r)
887    ("\\(^\\|:\\)\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
888    ("\\(^\\|:\\)israel\\>" iso-8859-1)
889    ("\\(^\\|:\\)han\\>" euc-kr)
890    ("\\(^\\|:\\)alt.chinese.text.big5\\>" chinese-big5)
891    ("\\(^\\|:\\)soc.culture.vietnamese\\>" vietnamese-viqr)
892    ("\\(^\\|:\\)\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
893    (".*" iso-8859-1))
894  :variable-document
895   "Alist of regexps (to match group names) and default charsets to be used when reading."
896   :variable-group gnus-charset
897   :variable-type '(repeat (list (regexp :tag "Group")
898                                 (symbol :tag "Charset")))
899   :parameter-type '(symbol :tag "Charset")
900   :parameter-document "\
901 The default charset to use in the group.")
902
903 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
904   "List of charsets that should be ignored.
905 When these charsets are used in the \"charset\" parameter, the
906 default charset will be used instead."
907   :version "21.1"
908   :type '(repeat symbol)
909   :group 'gnus-charset)
910
911 (gnus-define-group-parameter
912  ignored-charsets
913  :type list
914  :function-document
915  "Return the ignored charsets of GROUP."
916  :variable gnus-group-ignored-charsets-alist
917  :variable-default 
918  '(("alt\\.chinese\\.text" iso-8859-1))
919  :variable-document
920  "Alist of regexps (to match group names) and charsets that should be ignored.
921 When these charsets are used in the \"charset\" parameter, the
922 default charset will be used instead."
923  :variable-group gnus-charset
924  :variable-type '(repeat (cons (regexp :tag "Group")
925                                (repeat symbol)))
926  :parameter-type '(choice :tag "Ignored charsets" 
927                           :value nil
928                           (repeat (symbol)))
929  :parameter-document       "\
930 List of charsets that should be ignored.
931
932 When these charsets are used in the \"charset\" parameter, the
933 default charset will be used instead.")
934
935 (defcustom gnus-group-highlight-words-alist nil
936   "Alist of group regexps and highlight regexps.
937 This variable uses the same syntax as `gnus-emphasis-alist'."
938   :version "21.1"
939   :type '(repeat (cons (regexp :tag "Group")
940                        (repeat (list (regexp :tag "Highlight regexp")
941                                      (number :tag "Group for entire word" 0)
942                                      (number :tag "Group for displayed part" 0)
943                                      (symbol :tag "Face"
944                                              gnus-emphasis-highlight-words)))))
945   :group 'gnus-summary-visual)
946
947 (defcustom gnus-use-wheel nil
948   "Use Intelli-mouse on summary movement"
949   :type 'boolean
950   :group 'gnus-summary-maneuvering)
951
952 (defcustom gnus-wheel-scroll-amount '(5 . 1)
953   "Amount to scroll messages by spinning the mouse wheel.
954 This is actually a cons cell, where the first item is the amount to scroll
955 on a normal wheel event, and the second is the amount to scroll when the
956 wheel is moved with the shift key depressed."
957   :type '(cons (integer :tag "Shift") integer)
958   :group 'gnus-summary-maneuvering)
959
960 (defcustom gnus-wheel-edge-resistance 2
961   "How hard it should be to change the current article
962 by moving the mouse over the edge of the article window."
963   :type 'integer
964   :group 'gnus-summary-maneuvering)
965
966 (defcustom gnus-summary-show-article-charset-alist
967   nil
968   "Alist of number and charset.
969 The article will be shown with the charset corresponding to the
970 numbered argument.
971 For example: ((1 . cn-gb-2312) (2 . big5))."
972   :version "21.1"
973   :type '(repeat (cons (number :tag "Argument" 1)
974                        (symbol :tag "Charset")))
975   :group 'gnus-charset)
976
977 (defcustom gnus-preserve-marks t
978   "Whether marks are preserved when moving, copying and respooling messages."
979   :version "21.1"
980   :type 'boolean
981   :group 'gnus-summary-marks)
982
983 (defcustom gnus-alter-articles-to-read-function nil
984   "Function to be called to alter the list of articles to be selected."
985   :type 'function
986   :group 'gnus-summary)
987
988 (defcustom gnus-orphan-score nil
989   "*All orphans get this score added.  Set in the score file."
990   :group 'gnus-score-default
991   :type '(choice (const nil)
992                  integer))
993
994 (defcustom gnus-summary-save-parts-default-mime "image/.*"
995   "*A regexp to match MIME parts when saving multiple parts of a message
996 with gnus-summary-save-parts (X m). This regexp will be used by default
997 when prompting the user for which type of files to save."
998   :group 'gnus-summary
999   :type 'regexp)
1000
1001
1002 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1003   "*A regexp to match MIME parts when saving multiple parts of a message
1004 with gnus-summary-save-parts (X m). This regexp will be used by default
1005 when prompting the user for which type of files to save."
1006   :group 'gnus-summary
1007   :type 'regexp)
1008
1009
1010 ;;; Internal variables
1011
1012 (defvar gnus-article-mime-handles nil)
1013 (defvar gnus-article-decoded-p nil)
1014 (defvar gnus-article-charset nil)
1015 (defvar gnus-article-ignored-charsets nil)
1016 (defvar gnus-scores-exclude-files nil)
1017 (defvar gnus-page-broken nil)
1018 (defvar gnus-inhibit-mime-unbuttonizing nil)
1019
1020 (defvar gnus-original-article nil)
1021 (defvar gnus-article-internal-prepare-hook nil)
1022 (defvar gnus-newsgroup-process-stack nil)
1023
1024 (defvar gnus-thread-indent-array nil)
1025 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1026 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1027   "Function called to sort the articles within a thread after it has been gathered together.")
1028
1029 (defvar gnus-summary-save-parts-type-history nil)
1030 (defvar gnus-summary-save-parts-last-directory nil)
1031
1032 (defvar gnus-summary-save-parts-type-history nil)
1033 (defvar gnus-summary-save-parts-last-directory nil)
1034
1035 ;; Avoid highlighting in kill files.
1036 (defvar gnus-summary-inhibit-highlight nil)
1037 (defvar gnus-newsgroup-selected-overlay nil)
1038 (defvar gnus-inhibit-limiting nil)
1039 (defvar gnus-newsgroup-adaptive-score-file nil)
1040 (defvar gnus-current-score-file nil)
1041 (defvar gnus-current-move-group nil)
1042 (defvar gnus-current-copy-group nil)
1043 (defvar gnus-current-crosspost-group nil)
1044
1045 (defvar gnus-newsgroup-dependencies nil)
1046 (defvar gnus-newsgroup-adaptive nil)
1047 (defvar gnus-summary-display-article-function nil)
1048 (defvar gnus-summary-highlight-line-function nil
1049   "Function called after highlighting a summary line.")
1050
1051 (defvar gnus-summary-line-format-alist
1052   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1053     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1054     (?s gnus-tmp-subject-or-nil ?s)
1055     (?n gnus-tmp-name ?s)
1056     (?A (std11-address-string
1057          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1058     (?a (or (std11-full-name-string
1059              (car (mime-entity-read-field gnus-tmp-header 'From)))
1060             gnus-tmp-from) ?s)
1061     (?F gnus-tmp-from ?s)
1062     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1063     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1064     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1065     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1066     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1067     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1068     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1069     (?L gnus-tmp-lines ?d)
1070     (?I gnus-tmp-indentation ?s)
1071     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1072     (?R gnus-tmp-replied ?c)
1073     (?\[ gnus-tmp-opening-bracket ?c)
1074     (?\] gnus-tmp-closing-bracket ?c)
1075     (?\> (make-string gnus-tmp-level ? ) ?s)
1076     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1077     (?i gnus-tmp-score ?d)
1078     (?z gnus-tmp-score-char ?c)
1079     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1080     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1081     (?U gnus-tmp-unread ?c)
1082     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1083     (?t (gnus-summary-number-of-articles-in-thread
1084          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1085         ?d)
1086     (?e (gnus-summary-number-of-articles-in-thread
1087          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1088         ?c)
1089     (?u gnus-tmp-user-defined ?s)
1090     (?P (gnus-pick-line-number) ?d))
1091   "An alist of format specifications that can appear in summary lines.
1092 These are paired with what variables they correspond with, along with
1093 the type of the variable (string, integer, character, etc).")
1094
1095 (defvar gnus-summary-dummy-line-format-alist
1096   `((?S gnus-tmp-subject ?s)
1097     (?N gnus-tmp-number ?d)
1098     (?u gnus-tmp-user-defined ?s)))
1099
1100 (defvar gnus-summary-mode-line-format-alist
1101   `((?G gnus-tmp-group-name ?s)
1102     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1103     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1104     (?A gnus-tmp-article-number ?d)
1105     (?Z gnus-tmp-unread-and-unselected ?s)
1106     (?V gnus-version ?s)
1107     (?U gnus-tmp-unread-and-unticked ?d)
1108     (?S gnus-tmp-subject ?s)
1109     (?e gnus-tmp-unselected ?d)
1110     (?u gnus-tmp-user-defined ?s)
1111     (?d (length gnus-newsgroup-dormant) ?d)
1112     (?t (length gnus-newsgroup-marked) ?d)
1113     (?r (length gnus-newsgroup-reads) ?d)
1114     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1115     (?E gnus-newsgroup-expunged-tally ?d)
1116     (?s (gnus-current-score-file-nondirectory) ?s)))
1117
1118 (defvar gnus-last-search-regexp nil
1119   "Default regexp for article search command.")
1120
1121 (defvar gnus-summary-search-article-matched-data nil
1122   "Last matched data of article search command.  It is the local variable
1123 in `gnus-article-buffer' which consists of the list of start position,
1124 end position and text.")
1125
1126 (defvar gnus-last-shell-command nil
1127   "Default shell command on article.")
1128
1129 (defvar gnus-newsgroup-begin nil)
1130 (defvar gnus-newsgroup-end nil)
1131 (defvar gnus-newsgroup-last-rmail nil)
1132 (defvar gnus-newsgroup-last-mail nil)
1133 (defvar gnus-newsgroup-last-folder nil)
1134 (defvar gnus-newsgroup-last-file nil)
1135 (defvar gnus-newsgroup-auto-expire nil)
1136 (defvar gnus-newsgroup-active nil)
1137
1138 (defvar gnus-newsgroup-data nil)
1139 (defvar gnus-newsgroup-data-reverse nil)
1140 (defvar gnus-newsgroup-limit nil)
1141 (defvar gnus-newsgroup-limits nil)
1142
1143 (defvar gnus-newsgroup-unreads nil
1144   "List of unread articles in the current newsgroup.")
1145
1146 (defvar gnus-newsgroup-unselected nil
1147   "List of unselected unread articles in the current newsgroup.")
1148
1149 (defvar gnus-newsgroup-reads nil
1150   "Alist of read articles and article marks in the current newsgroup.")
1151
1152 (defvar gnus-newsgroup-expunged-tally nil)
1153
1154 (defvar gnus-newsgroup-marked nil
1155   "List of ticked articles in the current newsgroup (a subset of unread art).")
1156
1157 (defvar gnus-newsgroup-killed nil
1158   "List of ranges of articles that have been through the scoring process.")
1159
1160 (defvar gnus-newsgroup-cached nil
1161   "List of articles that come from the article cache.")
1162
1163 (defvar gnus-newsgroup-saved nil
1164   "List of articles that have been saved.")
1165
1166 (defvar gnus-newsgroup-kill-headers nil)
1167
1168 (defvar gnus-newsgroup-replied nil
1169   "List of articles that have been replied to in the current newsgroup.")
1170
1171 (defvar gnus-newsgroup-expirable nil
1172   "List of articles in the current newsgroup that can be expired.")
1173
1174 (defvar gnus-newsgroup-processable nil
1175   "List of articles in the current newsgroup that can be processed.")
1176
1177 (defvar gnus-newsgroup-downloadable nil
1178   "List of articles in the current newsgroup that can be processed.")
1179
1180 (defvar gnus-newsgroup-undownloaded nil
1181   "List of articles in the current newsgroup that haven't been downloaded..")
1182
1183 (defvar gnus-newsgroup-unsendable nil
1184   "List of articles in the current newsgroup that won't be sent.")
1185
1186 (defvar gnus-newsgroup-bookmarks nil
1187   "List of articles in the current newsgroup that have bookmarks.")
1188
1189 (defvar gnus-newsgroup-dormant nil
1190   "List of dormant articles in the current newsgroup.")
1191
1192 (defvar gnus-newsgroup-scored nil
1193   "List of scored articles in the current newsgroup.")
1194
1195 (defvar gnus-newsgroup-incorporated nil
1196   "List of incorporated articles in the current newsgroup.")
1197
1198 (defvar gnus-newsgroup-headers nil
1199   "List of article headers in the current newsgroup.")
1200
1201 (defvar gnus-newsgroup-threads nil)
1202
1203 (defvar gnus-newsgroup-prepared nil
1204   "Whether the current group has been prepared properly.")
1205
1206 (defvar gnus-newsgroup-ancient nil
1207   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1208
1209 (defvar gnus-newsgroup-sparse nil)
1210
1211 (defvar gnus-current-article nil)
1212 (defvar gnus-article-current nil)
1213 (defvar gnus-current-headers nil)
1214 (defvar gnus-have-all-headers nil)
1215 (defvar gnus-last-article nil)
1216 (defvar gnus-newsgroup-history nil)
1217 (defvar gnus-newsgroup-charset nil)
1218 (defvar gnus-newsgroup-ephemeral-charset nil)
1219 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1220
1221 (defvar gnus-article-before-search nil)
1222
1223 (defconst gnus-summary-local-variables
1224   '(gnus-newsgroup-name
1225     gnus-newsgroup-begin gnus-newsgroup-end
1226     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1227     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1228     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1229     gnus-newsgroup-unselected gnus-newsgroup-marked
1230     gnus-newsgroup-reads gnus-newsgroup-saved
1231     gnus-newsgroup-replied gnus-newsgroup-expirable
1232     gnus-newsgroup-processable gnus-newsgroup-killed
1233     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1234     gnus-newsgroup-unsendable
1235     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1236     gnus-newsgroup-headers gnus-newsgroup-threads
1237     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1238     gnus-current-article gnus-current-headers gnus-have-all-headers
1239     gnus-last-article gnus-article-internal-prepare-hook
1240     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1241     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1242     gnus-thread-expunge-below
1243     gnus-score-alist gnus-current-score-file
1244     (gnus-summary-expunge-below . global)
1245     (gnus-summary-mark-below . global)
1246     (gnus-orphan-score . global)
1247     gnus-newsgroup-active gnus-scores-exclude-files
1248     gnus-newsgroup-history gnus-newsgroup-ancient
1249     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1250     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1251     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1252     (gnus-newsgroup-expunged-tally . 0)
1253     gnus-cache-removable-articles gnus-newsgroup-cached
1254     gnus-newsgroup-data gnus-newsgroup-data-reverse
1255     gnus-newsgroup-limit gnus-newsgroup-limits
1256     gnus-newsgroup-charset
1257     gnus-newsgroup-incorporated)
1258   "Variables that are buffer-local to the summary buffers.")
1259
1260 (defvar gnus-newsgroup-variables nil
1261   "Variables that have separate values in the newsgroups.")
1262
1263 ;; Byte-compiler warning.
1264 (eval-when-compile (defvar gnus-article-mode-map))
1265
1266 ;; Subject simplification.
1267
1268 (defun gnus-simplify-whitespace (str)
1269   "Remove excessive whitespace from STR."
1270   (let ((mystr str))
1271     ;; Multiple spaces.
1272     (while (string-match "[ \t][ \t]+" mystr)
1273       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1274                           " "
1275                           (substring mystr (match-end 0)))))
1276     ;; Leading spaces.
1277     (when (string-match "^[ \t]+" mystr)
1278       (setq mystr (substring mystr (match-end 0))))
1279     ;; Trailing spaces.
1280     (when (string-match "[ \t]+$" mystr)
1281       (setq mystr (substring mystr 0 (match-beginning 0))))
1282     mystr))
1283
1284 (defsubst gnus-simplify-subject-re (subject)
1285   "Remove \"Re:\" from subject lines."
1286   (if (string-match message-subject-re-regexp subject)
1287       (substring subject (match-end 0))
1288     subject))
1289
1290 (defun gnus-simplify-subject (subject &optional re-only)
1291   "Remove `Re:' and words in parentheses.
1292 If RE-ONLY is non-nil, strip leading `Re:'s only."
1293   (let ((case-fold-search t))           ;Ignore case.
1294     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1295     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1296       (setq subject (substring subject (match-end 0))))
1297     ;; Remove uninteresting prefixes.
1298     (when (and (not re-only)
1299                gnus-simplify-ignored-prefixes
1300                (string-match gnus-simplify-ignored-prefixes subject))
1301       (setq subject (substring subject (match-end 0))))
1302     ;; Remove words in parentheses from end.
1303     (unless re-only
1304       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1305         (setq subject (substring subject 0 (match-beginning 0)))))
1306     ;; Return subject string.
1307     subject))
1308
1309 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1310 ;; all whitespace.
1311 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1312   (goto-char (point-min))
1313   (while (re-search-forward regexp nil t)
1314     (replace-match (or newtext ""))))
1315
1316 (defun gnus-simplify-buffer-fuzzy ()
1317   "Simplify string in the buffer fuzzily.
1318 The string in the accessible portion of the current buffer is simplified.
1319 It is assumed to be a single-line subject.
1320 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1321 matter is removed.  Additional things can be deleted by setting
1322 `gnus-simplify-subject-fuzzy-regexp'."
1323   (let ((case-fold-search t)
1324         (modified-tick))
1325     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1326
1327     (while (not (eq modified-tick (buffer-modified-tick)))
1328       (setq modified-tick (buffer-modified-tick))
1329       (cond
1330        ((listp gnus-simplify-subject-fuzzy-regexp)
1331         (mapcar 'gnus-simplify-buffer-fuzzy-step
1332                 gnus-simplify-subject-fuzzy-regexp))
1333        (gnus-simplify-subject-fuzzy-regexp
1334         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1335       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1336       (gnus-simplify-buffer-fuzzy-step
1337        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1338       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1339
1340     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1341     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1342     (gnus-simplify-buffer-fuzzy-step " $")
1343     (gnus-simplify-buffer-fuzzy-step "^ +")))
1344
1345 (defun gnus-simplify-subject-fuzzy (subject)
1346   "Simplify a subject string fuzzily.
1347 See `gnus-simplify-buffer-fuzzy' for details."
1348   (save-excursion
1349     (gnus-set-work-buffer)
1350     (let ((case-fold-search t))
1351       ;; Remove uninteresting prefixes.
1352       (when (and gnus-simplify-ignored-prefixes
1353                  (string-match gnus-simplify-ignored-prefixes subject))
1354         (setq subject (substring subject (match-end 0))))
1355       (insert subject)
1356       (inline (gnus-simplify-buffer-fuzzy))
1357       (buffer-string))))
1358
1359 (defsubst gnus-simplify-subject-fully (subject)
1360   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1361   (cond
1362    (gnus-simplify-subject-functions
1363     (gnus-map-function gnus-simplify-subject-functions subject))
1364    ((null gnus-summary-gather-subject-limit)
1365     (gnus-simplify-subject-re subject))
1366    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1367     (gnus-simplify-subject-fuzzy subject))
1368    ((numberp gnus-summary-gather-subject-limit)
1369     (gnus-limit-string (gnus-simplify-subject-re subject)
1370                        gnus-summary-gather-subject-limit))
1371    (t
1372     subject)))
1373
1374 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1375   "Check whether two subjects are equal.
1376 If optional argument simple-first is t, first argument is already
1377 simplified."
1378   (cond
1379    ((null simple-first)
1380     (equal (gnus-simplify-subject-fully s1)
1381            (gnus-simplify-subject-fully s2)))
1382    (t
1383     (equal s1
1384            (gnus-simplify-subject-fully s2)))))
1385
1386 (defun gnus-summary-bubble-group ()
1387   "Increase the score of the current group.
1388 This is a handy function to add to `gnus-summary-exit-hook' to
1389 increase the score of each group you read."
1390   (gnus-group-add-score gnus-newsgroup-name))
1391
1392 \f
1393 ;;;
1394 ;;; Gnus summary mode
1395 ;;;
1396
1397 (put 'gnus-summary-mode 'mode-class 'special)
1398
1399 (defvar gnus-article-commands-menu)
1400
1401 (when t
1402   ;; Non-orthogonal keys
1403
1404   (gnus-define-keys gnus-summary-mode-map
1405     " " gnus-summary-next-page
1406     "\177" gnus-summary-prev-page
1407     [delete] gnus-summary-prev-page
1408     [backspace] gnus-summary-prev-page
1409     "\r" gnus-summary-scroll-up
1410     "\M-\r" gnus-summary-scroll-down
1411     "n" gnus-summary-next-unread-article
1412     "p" gnus-summary-prev-unread-article
1413     "N" gnus-summary-next-article
1414     "P" gnus-summary-prev-article
1415     "\M-\C-n" gnus-summary-next-same-subject
1416     "\M-\C-p" gnus-summary-prev-same-subject
1417     "\M-n" gnus-summary-next-unread-subject
1418     "\M-p" gnus-summary-prev-unread-subject
1419     "." gnus-summary-first-unread-article
1420     "," gnus-summary-best-unread-article
1421     "\M-s" gnus-summary-search-article-forward
1422     "\M-r" gnus-summary-search-article-backward
1423     "<" gnus-summary-beginning-of-article
1424     ">" gnus-summary-end-of-article
1425     "j" gnus-summary-goto-article
1426     "^" gnus-summary-refer-parent-article
1427     "\M-^" gnus-summary-refer-article
1428     "u" gnus-summary-tick-article-forward
1429     "!" gnus-summary-tick-article-forward
1430     "U" gnus-summary-tick-article-backward
1431     "d" gnus-summary-mark-as-read-forward
1432     "D" gnus-summary-mark-as-read-backward
1433     "E" gnus-summary-mark-as-expirable
1434     "\M-u" gnus-summary-clear-mark-forward
1435     "\M-U" gnus-summary-clear-mark-backward
1436     "k" gnus-summary-kill-same-subject-and-select
1437     "\C-k" gnus-summary-kill-same-subject
1438     "\M-\C-k" gnus-summary-kill-thread
1439     "\M-\C-l" gnus-summary-lower-thread
1440     "e" gnus-summary-edit-article
1441     "#" gnus-summary-mark-as-processable
1442     "\M-#" gnus-summary-unmark-as-processable
1443     "\M-\C-t" gnus-summary-toggle-threads
1444     "\M-\C-s" gnus-summary-show-thread
1445     "\M-\C-h" gnus-summary-hide-thread
1446     "\M-\C-f" gnus-summary-next-thread
1447     "\M-\C-b" gnus-summary-prev-thread
1448     [(meta down)] gnus-summary-next-thread
1449     [(meta up)] gnus-summary-prev-thread
1450     "\M-\C-u" gnus-summary-up-thread
1451     "\M-\C-d" gnus-summary-down-thread
1452     "&" gnus-summary-execute-command
1453     "c" gnus-summary-catchup-and-exit
1454     "\C-w" gnus-summary-mark-region-as-read
1455     "\C-t" gnus-summary-toggle-truncation
1456     "?" gnus-summary-mark-as-dormant
1457     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1458     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1459     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1460     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1461     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1462     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1463     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1464     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1465     "=" gnus-summary-expand-window
1466     "\C-x\C-s" gnus-summary-reselect-current-group
1467     "\M-g" gnus-summary-rescan-group
1468     "w" gnus-summary-stop-page-breaking
1469     "\C-c\C-r" gnus-summary-caesar-message
1470     "\M-t" gnus-summary-toggle-mime
1471     "f" gnus-summary-followup
1472     "F" gnus-summary-followup-with-original
1473     "C" gnus-summary-cancel-article
1474     "r" gnus-summary-reply
1475     "R" gnus-summary-reply-with-original
1476     "\C-c\C-f" gnus-summary-mail-forward
1477     "o" gnus-summary-save-article
1478     "\C-o" gnus-summary-save-article-mail
1479     "|" gnus-summary-pipe-output
1480     "\M-k" gnus-summary-edit-local-kill
1481     "\M-K" gnus-summary-edit-global-kill
1482     ;; "V" gnus-version
1483     "\C-c\C-d" gnus-summary-describe-group
1484     "q" gnus-summary-exit
1485     "Q" gnus-summary-exit-no-update
1486     "\C-c\C-i" gnus-info-find-node
1487     gnus-mouse-2 gnus-mouse-pick-article
1488     "m" gnus-summary-mail-other-window
1489     "a" gnus-summary-post-news
1490     "x" gnus-summary-limit-to-unread
1491     "s" gnus-summary-isearch-article
1492     "t" gnus-article-toggle-headers
1493     "g" gnus-summary-show-article
1494     "l" gnus-summary-goto-last-article
1495     "v" gnus-summary-preview-mime-message
1496     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1497     "\C-d" gnus-summary-enter-digest-group
1498     "\M-\C-d" gnus-summary-read-document
1499     "\M-\C-e" gnus-summary-edit-parameters
1500     "\M-\C-a" gnus-summary-customize-parameters
1501     "\C-c\C-b" gnus-bug
1502     "*" gnus-cache-enter-article
1503     "\M-*" gnus-cache-remove-article
1504     "\M-&" gnus-summary-universal-argument
1505     "\C-l" gnus-recenter
1506     "I" gnus-summary-increase-score
1507     "L" gnus-summary-lower-score
1508     "\M-i" gnus-symbolic-argument
1509     "h" gnus-summary-select-article-buffer
1510
1511     "V" gnus-summary-score-map
1512     "X" gnus-uu-extract-map
1513     "S" gnus-summary-send-map)
1514
1515   ;; Sort of orthogonal keymap
1516   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1517     "t" gnus-summary-tick-article-forward
1518     "!" gnus-summary-tick-article-forward
1519     "d" gnus-summary-mark-as-read-forward
1520     "r" gnus-summary-mark-as-read-forward
1521     "c" gnus-summary-clear-mark-forward
1522     " " gnus-summary-clear-mark-forward
1523     "e" gnus-summary-mark-as-expirable
1524     "x" gnus-summary-mark-as-expirable
1525     "?" gnus-summary-mark-as-dormant
1526     "b" gnus-summary-set-bookmark
1527     "B" gnus-summary-remove-bookmark
1528     "#" gnus-summary-mark-as-processable
1529     "\M-#" gnus-summary-unmark-as-processable
1530     "S" gnus-summary-limit-include-expunged
1531     "C" gnus-summary-catchup
1532     "H" gnus-summary-catchup-to-here
1533     "\C-c" gnus-summary-catchup-all
1534     "k" gnus-summary-kill-same-subject-and-select
1535     "K" gnus-summary-kill-same-subject
1536     "P" gnus-uu-mark-map)
1537
1538   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1539     "c" gnus-summary-clear-above
1540     "u" gnus-summary-tick-above
1541     "m" gnus-summary-mark-above
1542     "k" gnus-summary-kill-below)
1543
1544   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1545     "/" gnus-summary-limit-to-subject
1546     "n" gnus-summary-limit-to-articles
1547     "w" gnus-summary-pop-limit
1548     "s" gnus-summary-limit-to-subject
1549     "a" gnus-summary-limit-to-author
1550     "u" gnus-summary-limit-to-unread
1551     "m" gnus-summary-limit-to-marks
1552     "M" gnus-summary-limit-exclude-marks
1553     "v" gnus-summary-limit-to-score
1554     "*" gnus-summary-limit-include-cached
1555     "D" gnus-summary-limit-include-dormant
1556     "T" gnus-summary-limit-include-thread
1557     "d" gnus-summary-limit-exclude-dormant
1558     "t" gnus-summary-limit-to-age
1559     "x" gnus-summary-limit-to-extra
1560     "E" gnus-summary-limit-include-expunged
1561     "c" gnus-summary-limit-exclude-childless-dormant
1562     "C" gnus-summary-limit-mark-excluded-as-read)
1563
1564   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1565     "n" gnus-summary-next-unread-article
1566     "p" gnus-summary-prev-unread-article
1567     "N" gnus-summary-next-article
1568     "P" gnus-summary-prev-article
1569     "\C-n" gnus-summary-next-same-subject
1570     "\C-p" gnus-summary-prev-same-subject
1571     "\M-n" gnus-summary-next-unread-subject
1572     "\M-p" gnus-summary-prev-unread-subject
1573     "f" gnus-summary-first-unread-article
1574     "b" gnus-summary-best-unread-article
1575     "j" gnus-summary-goto-article
1576     "g" gnus-summary-goto-subject
1577     "l" gnus-summary-goto-last-article
1578     "o" gnus-summary-pop-article)
1579
1580   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1581     "k" gnus-summary-kill-thread
1582     "l" gnus-summary-lower-thread
1583     "i" gnus-summary-raise-thread
1584     "T" gnus-summary-toggle-threads
1585     "t" gnus-summary-rethread-current
1586     "^" gnus-summary-reparent-thread
1587     "s" gnus-summary-show-thread
1588     "S" gnus-summary-show-all-threads
1589     "h" gnus-summary-hide-thread
1590     "H" gnus-summary-hide-all-threads
1591     "n" gnus-summary-next-thread
1592     "p" gnus-summary-prev-thread
1593     "u" gnus-summary-up-thread
1594     "o" gnus-summary-top-thread
1595     "d" gnus-summary-down-thread
1596     "#" gnus-uu-mark-thread
1597     "\M-#" gnus-uu-unmark-thread)
1598
1599   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1600     "g" gnus-summary-prepare
1601     "c" gnus-summary-insert-cached-articles)
1602
1603   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1604     "c" gnus-summary-catchup-and-exit
1605     "C" gnus-summary-catchup-all-and-exit
1606     "E" gnus-summary-exit-no-update
1607     "J" gnus-summary-jump-to-other-group
1608     "Q" gnus-summary-exit
1609     "Z" gnus-summary-exit
1610     "n" gnus-summary-catchup-and-goto-next-group
1611     "R" gnus-summary-reselect-current-group
1612     "G" gnus-summary-rescan-group
1613     "N" gnus-summary-next-group
1614     "s" gnus-summary-save-newsrc
1615     "P" gnus-summary-prev-group)
1616
1617   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1618     " " gnus-summary-next-page
1619     "n" gnus-summary-next-page
1620     "\177" gnus-summary-prev-page
1621     [delete] gnus-summary-prev-page
1622     "p" gnus-summary-prev-page
1623     "\r" gnus-summary-scroll-up
1624     "\M-\r" gnus-summary-scroll-down
1625     "<" gnus-summary-beginning-of-article
1626     ">" gnus-summary-end-of-article
1627     "b" gnus-summary-beginning-of-article
1628     "e" gnus-summary-end-of-article
1629     "^" gnus-summary-refer-parent-article
1630     "r" gnus-summary-refer-parent-article
1631     "D" gnus-summary-enter-digest-group
1632     "R" gnus-summary-refer-references
1633     "T" gnus-summary-refer-thread
1634     "g" gnus-summary-show-article
1635     "s" gnus-summary-isearch-article
1636     "P" gnus-summary-print-article
1637     "M" gnus-mailing-list-insinuate
1638     "t" gnus-article-babel)
1639
1640   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1641     "b" gnus-article-add-buttons
1642     "B" gnus-article-add-buttons-to-head
1643     "o" gnus-article-treat-overstrike
1644     "e" gnus-article-emphasize
1645     "w" gnus-article-fill-cited-article
1646     "Q" gnus-article-fill-long-lines
1647     "C" gnus-article-capitalize-sentences
1648     "c" gnus-article-remove-cr
1649     "Z" gnus-article-decode-HZ
1650     "f" gnus-article-display-x-face
1651     "l" gnus-summary-stop-page-breaking
1652     "r" gnus-summary-caesar-message
1653     "t" gnus-article-toggle-headers
1654     "v" gnus-summary-verbose-headers
1655     "m" gnus-summary-toggle-mime
1656     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1657     "p" gnus-article-verify-x-pgp-sig
1658     "d" gnus-article-treat-dumbquotes
1659     "s" gnus-smiley-display)
1660
1661   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1662     "a" gnus-article-hide
1663     "h" gnus-article-toggle-headers
1664     "b" gnus-article-hide-boring-headers
1665     "s" gnus-article-hide-signature
1666     "c" gnus-article-hide-citation
1667     "C" gnus-article-hide-citation-in-followups
1668     "l" gnus-article-hide-list-identifiers
1669     "p" gnus-article-hide-pgp
1670     "B" gnus-article-strip-banner
1671     "P" gnus-article-hide-pem
1672     "\C-c" gnus-article-hide-citation-maybe)
1673
1674   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1675     "a" gnus-article-highlight
1676     "h" gnus-article-highlight-headers
1677     "c" gnus-article-highlight-citation
1678     "s" gnus-article-highlight-signature)
1679
1680   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1681     "z" gnus-article-date-ut
1682     "u" gnus-article-date-ut
1683     "l" gnus-article-date-local
1684     "p" gnus-article-date-english
1685     "e" gnus-article-date-lapsed
1686     "o" gnus-article-date-original
1687     "i" gnus-article-date-iso8601
1688     "s" gnus-article-date-user)
1689
1690   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1691     "t" gnus-article-remove-trailing-blank-lines
1692     "l" gnus-article-strip-leading-blank-lines
1693     "m" gnus-article-strip-multiple-blank-lines
1694     "a" gnus-article-strip-blank-lines
1695     "A" gnus-article-strip-all-blank-lines
1696     "s" gnus-article-strip-leading-space
1697     "e" gnus-article-strip-trailing-space
1698     "w" gnus-article-remove-leading-whitespace)
1699
1700   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1701     "v" gnus-version
1702     "f" gnus-summary-fetch-faq
1703     "d" gnus-summary-describe-group
1704     "h" gnus-summary-describe-briefly
1705     "i" gnus-info-find-node)
1706
1707   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1708     "e" gnus-summary-expire-articles
1709     "\M-\C-e" gnus-summary-expire-articles-now
1710     "\177" gnus-summary-delete-article
1711     [delete] gnus-summary-delete-article
1712     [backspace] gnus-summary-delete-article
1713     "m" gnus-summary-move-article
1714     "r" gnus-summary-respool-article
1715     "w" gnus-summary-edit-article
1716     "c" gnus-summary-copy-article
1717     "B" gnus-summary-crosspost-article
1718     "q" gnus-summary-respool-query
1719     "t" gnus-summary-respool-trace
1720     "i" gnus-summary-import-article
1721     "p" gnus-summary-article-posted-p)
1722
1723   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1724     "o" gnus-summary-save-article
1725     "m" gnus-summary-save-article-mail
1726     "F" gnus-summary-write-article-file
1727     "r" gnus-summary-save-article-rmail
1728     "f" gnus-summary-save-article-file
1729     "b" gnus-summary-save-article-body-file
1730     "h" gnus-summary-save-article-folder
1731     "v" gnus-summary-save-article-vm
1732     "p" gnus-summary-pipe-output
1733     "s" gnus-soup-add-article)
1734
1735   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1736     "b" gnus-summary-display-buttonized
1737     "m" gnus-summary-repair-multipart
1738     "v" gnus-article-view-part
1739     "o" gnus-article-save-part
1740     "c" gnus-article-copy-part
1741     "C" gnus-article-view-part-as-charset
1742     "e" gnus-article-externalize-part
1743     "E" gnus-article-encrypt-body
1744     "i" gnus-article-inline-part
1745     "|" gnus-article-pipe-part))
1746
1747 (defun gnus-summary-make-menu-bar ()
1748   (gnus-turn-off-edit-menu 'summary)
1749
1750   (unless (boundp 'gnus-summary-misc-menu)
1751
1752     (easy-menu-define
1753      gnus-summary-kill-menu gnus-summary-mode-map ""
1754      (cons
1755       "Score"
1756       (nconc
1757        (list
1758         ["Customize" gnus-score-customize t])
1759        (gnus-make-score-map 'increase)
1760        (gnus-make-score-map 'lower)
1761        '(("Mark"
1762           ["Kill below" gnus-summary-kill-below t]
1763           ["Mark above" gnus-summary-mark-above t]
1764           ["Tick above" gnus-summary-tick-above t]
1765           ["Clear above" gnus-summary-clear-above t])
1766          ["Current score" gnus-summary-current-score t]
1767          ["Set score" gnus-summary-set-score t]
1768          ["Switch current score file..." gnus-score-change-score-file t]
1769          ["Set mark below..." gnus-score-set-mark-below t]
1770          ["Set expunge below..." gnus-score-set-expunge-below t]
1771          ["Edit current score file" gnus-score-edit-current-scores t]
1772          ["Edit score file" gnus-score-edit-file t]
1773          ["Trace score" gnus-score-find-trace t]
1774          ["Find words" gnus-score-find-favourite-words t]
1775          ["Rescore buffer" gnus-summary-rescore t]
1776          ["Increase score..." gnus-summary-increase-score t]
1777          ["Lower score..." gnus-summary-lower-score t]))))
1778
1779     ;; Define both the Article menu in the summary buffer and the equivalent
1780     ;; Commands menu in the article buffer here for consistency.
1781     (let ((innards
1782            `(("Hide"
1783               ["All" gnus-article-hide t]
1784               ["Headers" gnus-article-toggle-headers t]
1785               ["Signature" gnus-article-hide-signature t]
1786               ["Citation" gnus-article-hide-citation t]
1787               ["List identifiers" gnus-article-hide-list-identifiers t]
1788               ["PGP" gnus-article-hide-pgp t]
1789               ["Banner" gnus-article-strip-banner t]
1790               ["Boring headers" gnus-article-hide-boring-headers t])
1791              ("Highlight"
1792               ["All" gnus-article-highlight t]
1793               ["Headers" gnus-article-highlight-headers t]
1794               ["Signature" gnus-article-highlight-signature t]
1795               ["Citation" gnus-article-highlight-citation t])
1796              ("Date"
1797               ["Local" gnus-article-date-local t]
1798               ["ISO8601" gnus-article-date-iso8601 t]
1799               ["UT" gnus-article-date-ut t]
1800               ["Original" gnus-article-date-original t]
1801               ["Lapsed" gnus-article-date-lapsed t]
1802               ["User-defined" gnus-article-date-user t])
1803              ("Washing"
1804               ("Remove Blanks"
1805                ["Leading" gnus-article-strip-leading-blank-lines t]
1806                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1807                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1808                ["All of the above" gnus-article-strip-blank-lines t]
1809                ["All" gnus-article-strip-all-blank-lines t]
1810                ["Leading space" gnus-article-strip-leading-space t]
1811                ["Trailing space" gnus-article-strip-trailing-space t]
1812                ["Leading space in headers" 
1813                 gnus-article-remove-leading-whitespace t])
1814               ["Overstrike" gnus-article-treat-overstrike t]
1815               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1816               ["Emphasis" gnus-article-emphasize t]
1817               ["Word wrap" gnus-article-fill-cited-article t]
1818               ["Fill long lines" gnus-article-fill-long-lines t]
1819               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1820               ["CR" gnus-article-remove-cr t]
1821               ["Show X-Face" gnus-article-display-x-face t]
1822               ["Rot 13" gnus-summary-caesar-message
1823                ,@(if (featurep 'xemacs) '(t)
1824                    '(:help "\"Caesar rotate\" article by 13"))]
1825               ["Unix pipe" gnus-summary-pipe-message t]
1826               ["Add buttons" gnus-article-add-buttons t]
1827               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1828               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1829               ["Toggle MIME" gnus-summary-toggle-mime t]
1830               ["Verbose header" gnus-summary-verbose-headers t]
1831               ["Toggle header" gnus-summary-toggle-header t]
1832               ["Toggle smileys" gnus-smiley-display t]
1833               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1834               ["HZ" gnus-article-decode-HZ t])
1835              ("Output"
1836               ["Save in default format" gnus-summary-save-article
1837                ,@(if (featurep 'xemacs) '(t)
1838                    '(:help "Save article using default method"))]
1839               ["Save in file" gnus-summary-save-article-file
1840                ,@(if (featurep 'xemacs) '(t)
1841                    '(:help "Save article in file"))]
1842               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1843               ["Save in MH folder" gnus-summary-save-article-folder t]
1844               ["Save in VM folder" gnus-summary-save-article-vm t]
1845               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1846               ["Save body in file" gnus-summary-save-article-body-file t]
1847               ["Pipe through a filter" gnus-summary-pipe-output t]
1848               ["Add to SOUP packet" gnus-soup-add-article t]
1849               ["Print" gnus-summary-print-article t])
1850              ("Backend"
1851               ["Respool article..." gnus-summary-respool-article t]
1852               ["Move article..." gnus-summary-move-article
1853                (gnus-check-backend-function
1854                 'request-move-article gnus-newsgroup-name)]
1855               ["Copy article..." gnus-summary-copy-article t]
1856               ["Crosspost article..." gnus-summary-crosspost-article
1857                (gnus-check-backend-function
1858                 'request-replace-article gnus-newsgroup-name)]
1859               ["Import file..." gnus-summary-import-article t]
1860               ["Check if posted" gnus-summary-article-posted-p t]
1861               ["Edit article" gnus-summary-edit-article
1862                (not (gnus-group-read-only-p))]
1863               ["Delete article" gnus-summary-delete-article
1864                (gnus-check-backend-function
1865                 'request-expire-articles gnus-newsgroup-name)]
1866               ["Query respool" gnus-summary-respool-query t]
1867               ["Trace respool" gnus-summary-respool-trace t]
1868               ["Delete expirable articles" gnus-summary-expire-articles-now
1869                (gnus-check-backend-function
1870                 'request-expire-articles gnus-newsgroup-name)])
1871              ("Extract"
1872               ["Uudecode" gnus-uu-decode-uu
1873                ,@(if (featurep 'xemacs) '(t)
1874                    '(:help "Decode uuencoded article(s)"))]
1875               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1876               ["Unshar" gnus-uu-decode-unshar t]
1877               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1878               ["Save" gnus-uu-decode-save t]
1879               ["Binhex" gnus-uu-decode-binhex t]
1880               ["Postscript" gnus-uu-decode-postscript t])
1881              ("Cache"
1882               ["Enter article" gnus-cache-enter-article t]
1883               ["Remove article" gnus-cache-remove-article t])
1884              ["Translate" gnus-article-babel t]
1885              ["Select article buffer" gnus-summary-select-article-buffer t]
1886              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1887              ["Isearch article..." gnus-summary-isearch-article t]
1888              ["Beginning of the article" gnus-summary-beginning-of-article t]
1889              ["End of the article" gnus-summary-end-of-article t]
1890              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1891              ["Fetch referenced articles" gnus-summary-refer-references t]
1892              ["Fetch current thread" gnus-summary-refer-thread t]
1893              ["Fetch article with id..." gnus-summary-refer-article t]
1894              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1895              ["Redisplay" gnus-summary-show-article t])))
1896       (easy-menu-define
1897        gnus-summary-article-menu gnus-summary-mode-map ""
1898        (cons "Article" innards))
1899
1900       (if (not (keymapp gnus-summary-article-menu))
1901           (easy-menu-define
1902             gnus-article-commands-menu gnus-article-mode-map ""
1903             (cons "Commands" innards))
1904         ;; in Emacs, don't share menu.
1905         (setq gnus-article-commands-menu 
1906               (copy-keymap gnus-summary-article-menu))
1907         (define-key gnus-article-mode-map [menu-bar commands]
1908           (cons "Commands" gnus-article-commands-menu))))
1909
1910     (easy-menu-define
1911      gnus-summary-thread-menu gnus-summary-mode-map ""
1912      '("Threads"
1913        ["Toggle threading" gnus-summary-toggle-threads t]
1914        ["Hide threads" gnus-summary-hide-all-threads t]
1915        ["Show threads" gnus-summary-show-all-threads t]
1916        ["Hide thread" gnus-summary-hide-thread t]
1917        ["Show thread" gnus-summary-show-thread t]
1918        ["Go to next thread" gnus-summary-next-thread t]
1919        ["Go to previous thread" gnus-summary-prev-thread t]
1920        ["Go down thread" gnus-summary-down-thread t]
1921        ["Go up thread" gnus-summary-up-thread t]
1922        ["Top of thread" gnus-summary-top-thread t]
1923        ["Mark thread as read" gnus-summary-kill-thread t]
1924        ["Lower thread score" gnus-summary-lower-thread t]
1925        ["Raise thread score" gnus-summary-raise-thread t]
1926        ["Rethread current" gnus-summary-rethread-current t]))
1927
1928     (easy-menu-define
1929      gnus-summary-post-menu gnus-summary-mode-map ""
1930      `("Post"
1931        ["Post an article" gnus-summary-post-news
1932         ,@(if (featurep 'xemacs) '(t)
1933             '(:help "Post an article"))]
1934        ["Followup" gnus-summary-followup
1935         ,@(if (featurep 'xemacs) '(t)
1936             '(:help "Post followup to this article"))]
1937        ["Followup and yank" gnus-summary-followup-with-original
1938         ,@(if (featurep 'xemacs) '(t)
1939             '(:help "Post followup to this article, quoting its contents"))]
1940        ["Supersede article" gnus-summary-supersede-article t]
1941        ["Cancel article" gnus-summary-cancel-article
1942         ,@(if (featurep 'xemacs) '(t)
1943             '(:help "Cancel an article you posted"))]
1944        ["Reply" gnus-summary-reply t]
1945        ["Reply and yank" gnus-summary-reply-with-original t]
1946        ["Wide reply" gnus-summary-wide-reply t]
1947        ["Wide reply and yank" gnus-summary-wide-reply-with-original
1948         ,@(if (featurep 'xemacs) '(t)
1949             '(:help "Mail a reply, quoting this article"))]
1950        ["Mail forward" gnus-summary-mail-forward t]
1951        ["Post forward" gnus-summary-post-forward t]
1952        ["Digest and mail" gnus-uu-digest-mail-forward t]
1953        ["Digest and post" gnus-uu-digest-post-forward t]
1954        ["Resend message" gnus-summary-resend-message t]
1955        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1956        ["Send a mail" gnus-summary-mail-other-window t]
1957        ["Uuencode and post" gnus-uu-post-news
1958         ,@(if (featurep 'xemacs) '(t)
1959             '(:help "Post a uuencoded article"))]
1960        ["Followup via news" gnus-summary-followup-to-mail t]
1961        ["Followup via news and yank"
1962         gnus-summary-followup-to-mail-with-original t]
1963        ;;("Draft"
1964        ;;["Send" gnus-summary-send-draft t]
1965        ;;["Send bounced" gnus-resend-bounced-mail t])
1966        ))
1967
1968     (easy-menu-define
1969      gnus-summary-misc-menu gnus-summary-mode-map ""
1970      `("Misc"
1971        ("Mark Read"
1972         ["Mark as read" gnus-summary-mark-as-read-forward t]
1973         ["Mark same subject and select"
1974          gnus-summary-kill-same-subject-and-select t]
1975         ["Mark same subject" gnus-summary-kill-same-subject t]
1976         ["Catchup" gnus-summary-catchup
1977          ,@(if (featurep 'xemacs) '(t)
1978              '(:help "Mark unread articles in this group as read"))]
1979         ["Catchup all" gnus-summary-catchup-all t]
1980         ["Catchup to here" gnus-summary-catchup-to-here t]
1981         ["Catchup region" gnus-summary-mark-region-as-read t]
1982         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1983        ("Mark Various"
1984         ["Tick" gnus-summary-tick-article-forward t]
1985         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1986         ["Remove marks" gnus-summary-clear-mark-forward t]
1987         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1988         ["Set bookmark" gnus-summary-set-bookmark t]
1989         ["Remove bookmark" gnus-summary-remove-bookmark t])
1990        ("Mark Limit"
1991         ["Marks..." gnus-summary-limit-to-marks t]
1992         ["Subject..." gnus-summary-limit-to-subject t]
1993         ["Author..." gnus-summary-limit-to-author t]
1994         ["Age..." gnus-summary-limit-to-age t]
1995         ["Extra..." gnus-summary-limit-to-extra t]
1996         ["Score" gnus-summary-limit-to-score t]
1997         ["Unread" gnus-summary-limit-to-unread t]
1998         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1999         ["Articles" gnus-summary-limit-to-articles t]
2000         ["Pop limit" gnus-summary-pop-limit t]
2001         ["Show dormant" gnus-summary-limit-include-dormant t]
2002         ["Hide childless dormant"
2003          gnus-summary-limit-exclude-childless-dormant t]
2004         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2005         ["Hide marked" gnus-summary-limit-exclude-marks t]
2006         ["Show expunged" gnus-summary-show-all-expunged t])
2007        ("Process Mark"
2008         ["Set mark" gnus-summary-mark-as-processable t]
2009         ["Remove mark" gnus-summary-unmark-as-processable t]
2010         ["Remove all marks" gnus-summary-unmark-all-processable t]
2011         ["Mark above" gnus-uu-mark-over t]
2012         ["Mark series" gnus-uu-mark-series t]
2013         ["Mark region" gnus-uu-mark-region t]
2014         ["Unmark region" gnus-uu-unmark-region t]
2015         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2016         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2017         ["Mark all" gnus-uu-mark-all t]
2018         ["Mark buffer" gnus-uu-mark-buffer t]
2019         ["Mark sparse" gnus-uu-mark-sparse t]
2020         ["Mark thread" gnus-uu-mark-thread t]
2021         ["Unmark thread" gnus-uu-unmark-thread t]
2022         ("Process Mark Sets"
2023          ["Kill" gnus-summary-kill-process-mark t]
2024          ["Yank" gnus-summary-yank-process-mark
2025           gnus-newsgroup-process-stack]
2026          ["Save" gnus-summary-save-process-mark t]))
2027        ("Scroll article"
2028         ["Page forward" gnus-summary-next-page
2029          ,@(if (featurep 'xemacs) '(t)
2030              '(:help "Show next page of article"))]
2031         ["Page backward" gnus-summary-prev-page
2032          ,@(if (featurep 'xemacs) '(t)
2033              '(:help "Show previous page of article"))]
2034         ["Line forward" gnus-summary-scroll-up t])
2035        ("Move"
2036         ["Next unread article" gnus-summary-next-unread-article t]
2037         ["Previous unread article" gnus-summary-prev-unread-article t]
2038         ["Next article" gnus-summary-next-article t]
2039         ["Previous article" gnus-summary-prev-article t]
2040         ["Next unread subject" gnus-summary-next-unread-subject t]
2041         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2042         ["Next article same subject" gnus-summary-next-same-subject t]
2043         ["Previous article same subject" gnus-summary-prev-same-subject t]
2044         ["First unread article" gnus-summary-first-unread-article t]
2045         ["Best unread article" gnus-summary-best-unread-article t]
2046         ["Go to subject number..." gnus-summary-goto-subject t]
2047         ["Go to article number..." gnus-summary-goto-article t]
2048         ["Go to the last article" gnus-summary-goto-last-article t]
2049         ["Pop article off history" gnus-summary-pop-article t])
2050        ("Sort"
2051         ["Sort by number" gnus-summary-sort-by-number t]
2052         ["Sort by author" gnus-summary-sort-by-author t]
2053         ["Sort by subject" gnus-summary-sort-by-subject t]
2054         ["Sort by date" gnus-summary-sort-by-date t]
2055         ["Sort by score" gnus-summary-sort-by-score t]
2056         ["Sort by lines" gnus-summary-sort-by-lines t]
2057         ["Sort by characters" gnus-summary-sort-by-chars t])
2058        ("Help"
2059         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2060         ["Describe group" gnus-summary-describe-group t]
2061         ["Read manual" gnus-info-find-node t])
2062        ("Modes"
2063         ["Pick and read" gnus-pick-mode t]
2064         ["Binary" gnus-binary-mode t])
2065        ("Regeneration"
2066         ["Regenerate" gnus-summary-prepare t]
2067         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2068         ["Toggle threading" gnus-summary-toggle-threads t])
2069        ["Filter articles..." gnus-summary-execute-command t]
2070        ["Run command on subjects..." gnus-summary-universal-argument t]
2071        ["Search articles forward..." gnus-summary-search-article-forward t]
2072        ["Search articles backward..." gnus-summary-search-article-backward t]
2073        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2074        ["Expand window" gnus-summary-expand-window t]
2075        ["Expire expirable articles" gnus-summary-expire-articles
2076         (gnus-check-backend-function
2077          'request-expire-articles gnus-newsgroup-name)]
2078        ["Edit local kill file" gnus-summary-edit-local-kill t]
2079        ["Edit main kill file" gnus-summary-edit-global-kill t]
2080        ["Edit group parameters" gnus-summary-edit-parameters t]
2081        ["Customize group parameters" gnus-summary-customize-parameters t]
2082        ["Send a bug report" gnus-bug t]
2083        ("Exit"
2084         ["Catchup and exit" gnus-summary-catchup-and-exit
2085          ,@(if (featurep 'xemacs) '(t)
2086              '(:help "Mark unread articles in this group as read, then exit"))]
2087         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2088         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2089         ["Exit group" gnus-summary-exit
2090          ,@(if (featurep 'xemacs) '(t)
2091              '(:help "Exit current group, return to group selection mode"))]
2092         ["Exit group without updating" gnus-summary-exit-no-update t]
2093         ["Exit and goto next group" gnus-summary-next-group t]
2094         ["Exit and goto prev group" gnus-summary-prev-group t]
2095         ["Reselect group" gnus-summary-reselect-current-group t]
2096         ["Rescan group" gnus-summary-rescan-group t]
2097         ["Update dribble" gnus-summary-save-newsrc t])))
2098
2099     (gnus-run-hooks 'gnus-summary-menu-hook)))
2100
2101 (defvar gnus-summary-tool-bar-map nil)
2102
2103 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2104 (defun gnus-summary-make-tool-bar ()
2105   (if (and (fboundp 'tool-bar-add-item-from-menu)
2106            (default-value 'tool-bar-mode)
2107            (not gnus-summary-tool-bar-map))
2108       (setq gnus-summary-tool-bar-map
2109             (let ((tool-bar-map (make-sparse-keymap))
2110                   (load-path (mm-image-load-path)))
2111               (tool-bar-add-item-from-menu
2112                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2113               (tool-bar-add-item-from-menu
2114                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2115               (tool-bar-add-item-from-menu
2116                'gnus-summary-post-news "post" gnus-summary-mode-map)
2117               (tool-bar-add-item-from-menu
2118                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2119               (tool-bar-add-item-from-menu
2120                'gnus-summary-followup "followup" gnus-summary-mode-map)
2121               (tool-bar-add-item-from-menu
2122                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2123               (tool-bar-add-item-from-menu
2124                'gnus-summary-reply "reply" gnus-summary-mode-map)
2125               (tool-bar-add-item-from-menu
2126                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2127               (tool-bar-add-item-from-menu
2128                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2129               (tool-bar-add-item-from-menu
2130                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2131               (tool-bar-add-item-from-menu
2132                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2133               (tool-bar-add-item-from-menu
2134                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2135               (tool-bar-add-item-from-menu
2136                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2137               (tool-bar-add-item-from-menu
2138                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2139               (tool-bar-add-item-from-menu
2140                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2141               tool-bar-map)))
2142   (if gnus-summary-tool-bar-map
2143       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2144
2145 (defun gnus-score-set-default (var value)
2146   "A version of set that updates the GNU Emacs menu-bar."
2147   (set var value)
2148   ;; It is the message that forces the active status to be updated.
2149   (message ""))
2150
2151 (defun gnus-make-score-map (type)
2152   "Make a summary score map of type TYPE."
2153   (if t
2154       nil
2155     (let ((headers '(("author" "from" string)
2156                      ("subject" "subject" string)
2157                      ("article body" "body" string)
2158                      ("article head" "head" string)
2159                      ("xref" "xref" string)
2160                      ("extra header" "extra" string)
2161                      ("lines" "lines" number)
2162                      ("followups to author" "followup" string)))
2163           (types '((number ("less than" <)
2164                            ("greater than" >)
2165                            ("equal" =))
2166                    (string ("substring" s)
2167                            ("exact string" e)
2168                            ("fuzzy string" f)
2169                            ("regexp" r))))
2170           (perms '(("temporary" (current-time-string))
2171                    ("permanent" nil)
2172                    ("immediate" now)))
2173           header)
2174       (list
2175        (apply
2176         'nconc
2177         (list
2178          (if (eq type 'lower)
2179              "Lower score"
2180            "Increase score"))
2181         (let (outh)
2182           (while headers
2183             (setq header (car headers))
2184             (setq outh
2185                   (cons
2186                    (apply
2187                     'nconc
2188                     (list (car header))
2189                     (let ((ts (cdr (assoc (nth 2 header) types)))
2190                           outt)
2191                       (while ts
2192                         (setq outt
2193                               (cons
2194                                (apply
2195                                 'nconc
2196                                 (list (caar ts))
2197                                 (let ((ps perms)
2198                                       outp)
2199                                   (while ps
2200                                     (setq outp
2201                                           (cons
2202                                            (vector
2203                                             (caar ps)
2204                                             (list
2205                                              'gnus-summary-score-entry
2206                                              (nth 1 header)
2207                                              (if (or (string= (nth 1 header)
2208                                                               "head")
2209                                                      (string= (nth 1 header)
2210                                                               "body"))
2211                                                  ""
2212                                                (list 'gnus-summary-header
2213                                                      (nth 1 header)))
2214                                              (list 'quote (nth 1 (car ts)))
2215                                              (list 'gnus-score-delta-default
2216                                                    nil)
2217                                              (nth 1 (car ps))
2218                                              t)
2219                                             t)
2220                                            outp))
2221                                     (setq ps (cdr ps)))
2222                                   (list (nreverse outp))))
2223                                outt))
2224                         (setq ts (cdr ts)))
2225                       (list (nreverse outt))))
2226                    outh))
2227             (setq headers (cdr headers)))
2228           (list (nreverse outh))))))))
2229
2230 \f
2231
2232 (defun gnus-summary-mode (&optional group)
2233   "Major mode for reading articles.
2234
2235 All normal editing commands are switched off.
2236 \\<gnus-summary-mode-map>
2237 Each line in this buffer represents one article.  To read an
2238 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2239 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2240 respectively.
2241
2242 You can also post articles and send mail from this buffer.  To
2243 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2244 of an article, type `\\[gnus-summary-reply]'.
2245
2246 There are approx. one gazillion commands you can execute in this
2247 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2248
2249 The following commands are available:
2250
2251 \\{gnus-summary-mode-map}"
2252   (interactive)
2253   (kill-all-local-variables)
2254   (when (gnus-visual-p 'summary-menu 'menu)
2255     (gnus-summary-make-menu-bar)
2256     (gnus-summary-make-tool-bar))
2257   (gnus-summary-make-local-variables)
2258   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2259     (gnus-summary-make-local-variables))
2260   (gnus-make-thread-indent-array)
2261   (gnus-simplify-mode-line)
2262   (setq major-mode 'gnus-summary-mode)
2263   (setq mode-name "Summary")
2264   (make-local-variable 'minor-mode-alist)
2265   (use-local-map gnus-summary-mode-map)
2266   (buffer-disable-undo)
2267   (setq buffer-read-only t)             ;Disable modification
2268   (setq truncate-lines t)
2269   (setq selective-display t)
2270   (setq selective-display-ellipses t)   ;Display `...'
2271   (gnus-summary-set-display-table)
2272   (gnus-set-default-directory)
2273   (setq gnus-newsgroup-name group)
2274   (unless (gnus-news-group-p group)
2275     (setq gnus-newsgroup-incorporated
2276           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2277   (make-local-variable 'gnus-summary-line-format)
2278   (make-local-variable 'gnus-summary-line-format-spec)
2279   (make-local-variable 'gnus-summary-dummy-line-format)
2280   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2281   (make-local-variable 'gnus-summary-mark-positions)
2282   (make-local-hook 'pre-command-hook)
2283   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2284   (gnus-run-hooks 'gnus-summary-mode-hook)
2285   (turn-on-gnus-mailing-list-mode)
2286   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2287   (gnus-update-summary-mark-positions))
2288
2289 (defun gnus-summary-make-local-variables ()
2290   "Make all the local summary buffer variables."
2291   (let (global)
2292     (dolist (local gnus-summary-local-variables)
2293       (if (consp local)
2294           (progn
2295             (if (eq (cdr local) 'global)
2296                 ;; Copy the global value of the variable.
2297                 (setq global (symbol-value (car local)))
2298               ;; Use the value from the list.
2299               (setq global (eval (cdr local))))
2300             (set (make-local-variable (car local)) global))
2301         ;; Simple nil-valued local variable.
2302         (set (make-local-variable local) nil)))))
2303
2304 (defun gnus-summary-clear-local-variables ()
2305   (let ((locals gnus-summary-local-variables))
2306     (while locals
2307       (if (consp (car locals))
2308           (and (vectorp (caar locals))
2309                (set (caar locals) nil))
2310         (and (vectorp (car locals))
2311              (set (car locals) nil)))
2312       (setq locals (cdr locals)))))
2313
2314 ;; Summary data functions.
2315
2316 (defmacro gnus-data-number (data)
2317   `(car ,data))
2318
2319 (defmacro gnus-data-set-number (data number)
2320   `(setcar ,data ,number))
2321
2322 (defmacro gnus-data-mark (data)
2323   `(nth 1 ,data))
2324
2325 (defmacro gnus-data-set-mark (data mark)
2326   `(setcar (nthcdr 1 ,data) ,mark))
2327
2328 (defmacro gnus-data-pos (data)
2329   `(nth 2 ,data))
2330
2331 (defmacro gnus-data-set-pos (data pos)
2332   `(setcar (nthcdr 2 ,data) ,pos))
2333
2334 (defmacro gnus-data-header (data)
2335   `(nth 3 ,data))
2336
2337 (defmacro gnus-data-set-header (data header)
2338   `(setcar (nthcdr 3 ,data) ,header))
2339
2340 (defmacro gnus-data-level (data)
2341   `(nth 4 ,data))
2342
2343 (defmacro gnus-data-unread-p (data)
2344   `(= (nth 1 ,data) gnus-unread-mark))
2345
2346 (defmacro gnus-data-read-p (data)
2347   `(/= (nth 1 ,data) gnus-unread-mark))
2348
2349 (defmacro gnus-data-pseudo-p (data)
2350   `(consp (nth 3 ,data)))
2351
2352 (defmacro gnus-data-find (number)
2353   `(assq ,number gnus-newsgroup-data))
2354
2355 (defmacro gnus-data-find-list (number &optional data)
2356   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2357      (memq (assq ,number bdata)
2358            bdata)))
2359
2360 (defmacro gnus-data-make (number mark pos header level)
2361   `(list ,number ,mark ,pos ,header ,level))
2362
2363 (defun gnus-data-enter (after-article number mark pos header level offset)
2364   (let ((data (gnus-data-find-list after-article)))
2365     (unless data
2366       (error "No such article: %d" after-article))
2367     (setcdr data (cons (gnus-data-make number mark pos header level)
2368                        (cdr data)))
2369     (setq gnus-newsgroup-data-reverse nil)
2370     (gnus-data-update-list (cddr data) offset)))
2371
2372 (defun gnus-data-enter-list (after-article list &optional offset)
2373   (when list
2374     (let ((data (and after-article (gnus-data-find-list after-article)))
2375           (ilist list))
2376       (if (not (or data
2377                    after-article))
2378           (let ((odata gnus-newsgroup-data))
2379             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2380             (when offset
2381               (gnus-data-update-list odata offset)))
2382         ;; Find the last element in the list to be spliced into the main
2383         ;; list.
2384         (while (cdr list)
2385           (setq list (cdr list)))
2386         (if (not data)
2387             (progn
2388               (setcdr list gnus-newsgroup-data)
2389               (setq gnus-newsgroup-data ilist)
2390               (when offset
2391                 (gnus-data-update-list (cdr list) offset)))
2392           (setcdr list (cdr data))
2393           (setcdr data ilist)
2394           (when offset
2395             (gnus-data-update-list (cdr list) offset))))
2396       (setq gnus-newsgroup-data-reverse nil))))
2397
2398 (defun gnus-data-remove (article &optional offset)
2399   (let ((data gnus-newsgroup-data))
2400     (if (= (gnus-data-number (car data)) article)
2401         (progn
2402           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2403                 gnus-newsgroup-data-reverse nil)
2404           (when offset
2405             (gnus-data-update-list gnus-newsgroup-data offset)))
2406       (while (cdr data)
2407         (when (= (gnus-data-number (cadr data)) article)
2408           (setcdr data (cddr data))
2409           (when offset
2410             (gnus-data-update-list (cdr data) offset))
2411           (setq data nil
2412                 gnus-newsgroup-data-reverse nil))
2413         (setq data (cdr data))))))
2414
2415 (defmacro gnus-data-list (backward)
2416   `(if ,backward
2417        (or gnus-newsgroup-data-reverse
2418            (setq gnus-newsgroup-data-reverse
2419                  (reverse gnus-newsgroup-data)))
2420      gnus-newsgroup-data))
2421
2422 (defun gnus-data-update-list (data offset)
2423   "Add OFFSET to the POS of all data entries in DATA."
2424   (setq gnus-newsgroup-data-reverse nil)
2425   (while data
2426     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2427     (setq data (cdr data))))
2428
2429 (defun gnus-summary-article-pseudo-p (article)
2430   "Say whether this article is a pseudo article or not."
2431   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2432
2433 (defmacro gnus-summary-article-sparse-p (article)
2434   "Say whether this article is a sparse article or not."
2435   `(memq ,article gnus-newsgroup-sparse))
2436
2437 (defmacro gnus-summary-article-ancient-p (article)
2438   "Say whether this article is a sparse article or not."
2439   `(memq ,article gnus-newsgroup-ancient))
2440
2441 (defun gnus-article-parent-p (number)
2442   "Say whether this article is a parent or not."
2443   (let ((data (gnus-data-find-list number)))
2444     (and (cdr data)                     ; There has to be an article after...
2445          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2446             (gnus-data-level (nth 1 data))))))
2447
2448 (defun gnus-article-children (number)
2449   "Return a list of all children to NUMBER."
2450   (let* ((data (gnus-data-find-list number))
2451          (level (gnus-data-level (car data)))
2452          children)
2453     (setq data (cdr data))
2454     (while (and data
2455                 (= (gnus-data-level (car data)) (1+ level)))
2456       (push (gnus-data-number (car data)) children)
2457       (setq data (cdr data)))
2458     children))
2459
2460 (defmacro gnus-summary-skip-intangible ()
2461   "If the current article is intangible, then jump to a different article."
2462   '(let ((to (get-text-property (point) 'gnus-intangible)))
2463      (and to (gnus-summary-goto-subject to))))
2464
2465 (defmacro gnus-summary-article-intangible-p ()
2466   "Say whether this article is intangible or not."
2467   '(get-text-property (point) 'gnus-intangible))
2468
2469 (defun gnus-article-read-p (article)
2470   "Say whether ARTICLE is read or not."
2471   (not (or (memq article gnus-newsgroup-marked)
2472            (memq article gnus-newsgroup-unreads)
2473            (memq article gnus-newsgroup-unselected)
2474            (memq article gnus-newsgroup-dormant))))
2475
2476 ;; Some summary mode macros.
2477
2478 (defmacro gnus-summary-article-number ()
2479   "The article number of the article on the current line.
2480 If there isn's an article number here, then we return the current
2481 article number."
2482   '(progn
2483      (gnus-summary-skip-intangible)
2484      (or (get-text-property (point) 'gnus-number)
2485          (gnus-summary-last-subject))))
2486
2487 (defmacro gnus-summary-article-header (&optional number)
2488   "Return the header of article NUMBER."
2489   `(gnus-data-header (gnus-data-find
2490                       ,(or number '(gnus-summary-article-number)))))
2491
2492 (defmacro gnus-summary-thread-level (&optional number)
2493   "Return the level of thread that starts with article NUMBER."
2494   `(if (and (eq gnus-summary-make-false-root 'dummy)
2495             (get-text-property (point) 'gnus-intangible))
2496        0
2497      (gnus-data-level (gnus-data-find
2498                        ,(or number '(gnus-summary-article-number))))))
2499
2500 (defmacro gnus-summary-article-mark (&optional number)
2501   "Return the mark of article NUMBER."
2502   `(gnus-data-mark (gnus-data-find
2503                     ,(or number '(gnus-summary-article-number)))))
2504
2505 (defmacro gnus-summary-article-pos (&optional number)
2506   "Return the position of the line of article NUMBER."
2507   `(gnus-data-pos (gnus-data-find
2508                    ,(or number '(gnus-summary-article-number)))))
2509
2510 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2511 (defmacro gnus-summary-article-subject (&optional number)
2512   "Return current subject string or nil if nothing."
2513   `(let ((headers
2514           ,(if number
2515                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2516              '(gnus-data-header (assq (gnus-summary-article-number)
2517                                       gnus-newsgroup-data)))))
2518      (and headers
2519           (vectorp headers)
2520           (mail-header-subject headers))))
2521
2522 (defmacro gnus-summary-article-score (&optional number)
2523   "Return current article score."
2524   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2525                   gnus-newsgroup-scored))
2526        gnus-summary-default-score 0))
2527
2528 (defun gnus-summary-article-children (&optional number)
2529   "Return a list of article numbers that are children of article NUMBER."
2530   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2531          (level (gnus-data-level (car data)))
2532          l children)
2533     (while (and (setq data (cdr data))
2534                 (> (setq l (gnus-data-level (car data))) level))
2535       (and (= (1+ level) l)
2536            (push (gnus-data-number (car data))
2537                  children)))
2538     (nreverse children)))
2539
2540 (defun gnus-summary-article-parent (&optional number)
2541   "Return the article number of the parent of article NUMBER."
2542   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2543                                     (gnus-data-list t)))
2544          (level (gnus-data-level (car data))))
2545     (if (zerop level)
2546         ()                              ; This is a root.
2547       ;; We search until we find an article with a level less than
2548       ;; this one.  That function has to be the parent.
2549       (while (and (setq data (cdr data))
2550                   (not (< (gnus-data-level (car data)) level))))
2551       (and data (gnus-data-number (car data))))))
2552
2553 (defun gnus-unread-mark-p (mark)
2554   "Say whether MARK is the unread mark."
2555   (= mark gnus-unread-mark))
2556
2557 (defun gnus-read-mark-p (mark)
2558   "Say whether MARK is one of the marks that mark as read.
2559 This is all marks except unread, ticked, dormant, and expirable."
2560   (not (or (= mark gnus-unread-mark)
2561            (= mark gnus-ticked-mark)
2562            (= mark gnus-dormant-mark)
2563            (= mark gnus-expirable-mark))))
2564
2565 (defmacro gnus-article-mark (number)
2566   "Return the MARK of article NUMBER.
2567 This macro should only be used when computing the mark the \"first\"
2568 time; i.e., when generating the summary lines.  After that,
2569 `gnus-summary-article-mark' should be used to examine the
2570 marks of articles."
2571   `(cond
2572     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2573     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2574     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2575     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2576     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2577     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2578     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2579     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2580            gnus-ancient-mark))))
2581
2582 ;; Saving hidden threads.
2583
2584 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2585 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2586
2587 (defmacro gnus-save-hidden-threads (&rest forms)
2588   "Save hidden threads, eval FORMS, and restore the hidden threads."
2589   (let ((config (make-symbol "config")))
2590     `(let ((,config (gnus-hidden-threads-configuration)))
2591        (unwind-protect
2592            (save-excursion
2593              ,@forms)
2594          (gnus-restore-hidden-threads-configuration ,config)))))
2595
2596 (defun gnus-data-compute-positions ()
2597   "Compute the positions of all articles."
2598   (setq gnus-newsgroup-data-reverse nil)
2599   (let ((data gnus-newsgroup-data))
2600     (save-excursion
2601       (gnus-save-hidden-threads
2602         (gnus-summary-show-all-threads)
2603         (goto-char (point-min))
2604         (while data
2605           (while (get-text-property (point) 'gnus-intangible)
2606             (forward-line 1))
2607           (gnus-data-set-pos (car data) (+ (point) 3))
2608           (setq data (cdr data))
2609           (forward-line 1))))))
2610
2611 (defun gnus-hidden-threads-configuration ()
2612   "Return the current hidden threads configuration."
2613   (save-excursion
2614     (let (config)
2615       (goto-char (point-min))
2616       (while (search-forward "\r" nil t)
2617         (push (1- (point)) config))
2618       config)))
2619
2620 (defun gnus-restore-hidden-threads-configuration (config)
2621   "Restore hidden threads configuration from CONFIG."
2622   (save-excursion
2623     (let (point buffer-read-only)
2624       (while (setq point (pop config))
2625         (when (and (< point (point-max))
2626                    (goto-char point)
2627                    (eq (char-after) ?\n))
2628           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2629
2630 ;; Various summary mode internalish functions.
2631
2632 (defun gnus-mouse-pick-article (e)
2633   (interactive "e")
2634   (mouse-set-point e)
2635   (gnus-summary-next-page nil t))
2636
2637 (defun gnus-summary-set-display-table ()
2638   "Change the display table.
2639 Odd characters have a tendency to mess
2640 up nicely formatted displays - we make all possible glyphs
2641 display only a single character."
2642
2643   ;; We start from the standard display table, if any.
2644   (let ((table (or (copy-sequence standard-display-table)
2645                    (make-display-table)))
2646         (i 32))
2647     ;; Nix out all the control chars...
2648     (while (>= (setq i (1- i)) 0)
2649       (aset table i [??]))
2650     ;; ... but not newline and cr, of course.  (cr is necessary for the
2651     ;; selective display).
2652     (aset table ?\n nil)
2653     (aset table ?\r nil)
2654     ;; We keep TAB as well.
2655     (aset table ?\t nil)
2656     ;; We nix out any glyphs over 126 that are not set already.
2657     (let ((i 256))
2658       (while (>= (setq i (1- i)) 127)
2659         ;; Only modify if the entry is nil.
2660         (unless (aref table i)
2661           (aset table i [??]))))
2662     (setq buffer-display-table table)))
2663
2664 (defun gnus-summary-buffer-name (group)
2665   "Return the summary buffer name of GROUP."
2666   (concat "*Summary " group "*"))
2667
2668 (defun gnus-summary-setup-buffer (group)
2669   "Initialize summary buffer."
2670   (let ((buffer (gnus-summary-buffer-name group)))
2671     (if (get-buffer buffer)
2672         (progn
2673           (set-buffer buffer)
2674           (setq gnus-summary-buffer (current-buffer))
2675           (not gnus-newsgroup-prepared))
2676       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2677       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2678       (gnus-summary-mode group)
2679       (when gnus-carpal
2680         (gnus-carpal-setup-buffer 'summary))
2681       (unless gnus-single-article-buffer
2682         (make-local-variable 'gnus-article-buffer)
2683         (make-local-variable 'gnus-article-current)
2684         (make-local-variable 'gnus-original-article-buffer))
2685       (setq gnus-newsgroup-name group)
2686       ;; Set any local variables in the group parameters.
2687       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2688       t)))
2689
2690 (defun gnus-set-global-variables ()
2691   "Set the global equivalents of the buffer-local variables.
2692 They are set to the latest values they had.  These reflect the summary
2693 buffer that was in action when the last article was fetched."
2694   (when (eq major-mode 'gnus-summary-mode)
2695     (setq gnus-summary-buffer (current-buffer))
2696     (let ((name gnus-newsgroup-name)
2697           (marked gnus-newsgroup-marked)
2698           (unread gnus-newsgroup-unreads)
2699           (headers gnus-current-headers)
2700           (data gnus-newsgroup-data)
2701           (summary gnus-summary-buffer)
2702           (article-buffer gnus-article-buffer)
2703           (original gnus-original-article-buffer)
2704           (gac gnus-article-current)
2705           (reffed gnus-reffed-article-number)
2706           (score-file gnus-current-score-file)
2707           (default-charset gnus-newsgroup-charset)
2708           vlist)
2709       (let ((locals gnus-newsgroup-variables))
2710         (while locals
2711           (if (consp (car locals))
2712               (push (eval (caar locals)) vlist)
2713             (push (eval (car locals)) vlist))
2714           (setq locals (cdr locals)))
2715         (setq vlist (nreverse vlist)))
2716       (save-excursion
2717         (set-buffer gnus-group-buffer)
2718         (setq gnus-newsgroup-name name
2719               gnus-newsgroup-marked marked
2720               gnus-newsgroup-unreads unread
2721               gnus-current-headers headers
2722               gnus-newsgroup-data data
2723               gnus-article-current gac
2724               gnus-summary-buffer summary
2725               gnus-article-buffer article-buffer
2726               gnus-original-article-buffer original
2727               gnus-reffed-article-number reffed
2728               gnus-current-score-file score-file
2729               gnus-newsgroup-charset default-charset)
2730         (let ((locals gnus-newsgroup-variables))
2731           (while locals
2732             (if (consp (car locals))
2733                 (set (caar locals) (pop vlist))
2734               (set (car locals) (pop vlist)))
2735             (setq locals (cdr locals))))
2736         ;; The article buffer also has local variables.
2737         (when (gnus-buffer-live-p gnus-article-buffer)
2738           (set-buffer gnus-article-buffer)
2739           (setq gnus-summary-buffer summary))))))
2740
2741 (defun gnus-summary-article-unread-p (article)
2742   "Say whether ARTICLE is unread or not."
2743   (memq article gnus-newsgroup-unreads))
2744
2745 (defun gnus-summary-first-article-p (&optional article)
2746   "Return whether ARTICLE is the first article in the buffer."
2747   (if (not (setq article (or article (gnus-summary-article-number))))
2748       nil
2749     (eq article (caar gnus-newsgroup-data))))
2750
2751 (defun gnus-summary-last-article-p (&optional article)
2752   "Return whether ARTICLE is the last article in the buffer."
2753   (if (not (setq article (or article (gnus-summary-article-number))))
2754       ;; All non-existent numbers are the last article.  :-)
2755       t
2756     (not (cdr (gnus-data-find-list article)))))
2757
2758 (defun gnus-make-thread-indent-array ()
2759   (let ((n 200))
2760     (unless (and gnus-thread-indent-array
2761                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2762       (setq gnus-thread-indent-array (make-vector 201 "")
2763             gnus-thread-indent-array-level gnus-thread-indent-level)
2764       (while (>= n 0)
2765         (aset gnus-thread-indent-array n
2766               (make-string (* n gnus-thread-indent-level) ? ))
2767         (setq n (1- n))))))
2768
2769 (defun gnus-update-summary-mark-positions ()
2770   "Compute where the summary marks are to go."
2771   (save-excursion
2772     (when (gnus-buffer-exists-p gnus-summary-buffer)
2773       (set-buffer gnus-summary-buffer))
2774     (let ((gnus-replied-mark 129)
2775           (gnus-score-below-mark 130)
2776           (gnus-score-over-mark 130)
2777           (gnus-download-mark 131)
2778           (spec gnus-summary-line-format-spec)
2779           gnus-visual pos)
2780       (save-excursion
2781         (gnus-set-work-buffer)
2782         (let ((gnus-summary-line-format-spec spec)
2783               (gnus-newsgroup-downloadable '((0 . t))))
2784           (gnus-summary-insert-line
2785            (make-full-mail-header 0 "" "nobody" "" "" "" 0 0 "" nil)
2786            0 nil 128 t nil "" nil 1)
2787           (goto-char (point-min))
2788           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2789                                              (- (point) 2)))))
2790           (goto-char (point-min))
2791           (push (cons 'replied (and (search-forward "\201" nil t)
2792                                     (- (point) 2)))
2793                 pos)
2794           (goto-char (point-min))
2795           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2796                 pos)
2797           (goto-char (point-min))
2798           (push (cons 'download
2799                       (and (search-forward "\203" nil t) (- (point) 2)))
2800                 pos)))
2801       (setq gnus-summary-mark-positions pos))))
2802
2803 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2804   "Insert a dummy root in the summary buffer."
2805   (beginning-of-line)
2806   (gnus-add-text-properties
2807    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2808    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2809
2810 (defun gnus-summary-from-or-to-or-newsgroups (header)
2811   (let ((to (cdr (assq 'To (mail-header-extra header))))
2812         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2813         (default-mime-charset (with-current-buffer gnus-summary-buffer
2814                                 default-mime-charset)))
2815     (cond
2816      ((and to
2817            gnus-ignored-from-addresses
2818            (string-match gnus-ignored-from-addresses
2819                          (mail-header-from header)))
2820       (concat "-> "
2821               (or (car (funcall gnus-extract-address-components
2822                                 (funcall
2823                                  gnus-decode-encoded-word-function to)))
2824                   (funcall gnus-decode-encoded-word-function to))))
2825      ((and newsgroups
2826            gnus-ignored-from-addresses
2827            (string-match gnus-ignored-from-addresses
2828                          (mail-header-from header)))
2829       (concat "=> " newsgroups))
2830      (t
2831       (or (car (funcall gnus-extract-address-components
2832                         (mail-header-from header)))
2833           (mail-header-from header))))))
2834
2835 (defun gnus-summary-insert-line (gnus-tmp-header
2836                                  gnus-tmp-level gnus-tmp-current
2837                                  gnus-tmp-unread gnus-tmp-replied
2838                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2839                                  &optional gnus-tmp-dummy gnus-tmp-score
2840                                  gnus-tmp-process)
2841   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2842          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2843          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2844          (gnus-tmp-score-char
2845           (if (or (null gnus-summary-default-score)
2846                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2847                       gnus-summary-zcore-fuzz))
2848               ?  ;Whitespace
2849             (if (< gnus-tmp-score gnus-summary-default-score)
2850                 gnus-score-below-mark gnus-score-over-mark)))
2851          (gnus-tmp-replied
2852           (cond (gnus-tmp-process gnus-process-mark)
2853                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2854                  gnus-cached-mark)
2855                 (gnus-tmp-replied gnus-replied-mark)
2856                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2857                  gnus-saved-mark)
2858                 (t gnus-no-mark)))
2859          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2860          (gnus-tmp-name
2861           (cond
2862            ((string-match "<[^>]+> *$" gnus-tmp-from)
2863             (let ((beg (match-beginning 0)))
2864               (or (and (string-match "^\".+\"" gnus-tmp-from)
2865                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2866                   (substring gnus-tmp-from 0 beg))))
2867            ((string-match "(.+)" gnus-tmp-from)
2868             (substring gnus-tmp-from
2869                        (1+ (match-beginning 0)) (1- (match-end 0))))
2870            (t gnus-tmp-from)))
2871          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2872          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2873          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2874          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2875          (buffer-read-only nil))
2876     (when (string= gnus-tmp-name "")
2877       (setq gnus-tmp-name gnus-tmp-from))
2878     (unless (numberp gnus-tmp-lines)
2879       (setq gnus-tmp-lines 0))
2880     (gnus-put-text-property-excluding-characters-with-faces
2881      (point)
2882      (progn (eval gnus-summary-line-format-spec) (point))
2883      'gnus-number gnus-tmp-number)
2884     (when (gnus-visual-p 'summary-highlight 'highlight)
2885       (forward-line -1)
2886       (gnus-run-hooks 'gnus-summary-update-hook)
2887       (forward-line 1))))
2888
2889 (defun gnus-summary-update-line (&optional dont-update)
2890   "Update summary line after change."
2891   (when (and gnus-summary-default-score
2892              (not gnus-summary-inhibit-highlight))
2893     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2894            (article (gnus-summary-article-number))
2895            (score (gnus-summary-article-score article)))
2896       (unless dont-update
2897         (if (and gnus-summary-mark-below
2898                  (< (gnus-summary-article-score)
2899                     gnus-summary-mark-below))
2900             ;; This article has a low score, so we mark it as read.
2901             (when (memq article gnus-newsgroup-unreads)
2902               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2903           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2904             ;; This article was previously marked as read on account
2905             ;; of a low score, but now it has risen, so we mark it as
2906             ;; unread.
2907             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2908         (gnus-summary-update-mark
2909          (if (or (null gnus-summary-default-score)
2910                  (<= (abs (- score gnus-summary-default-score))
2911                      gnus-summary-zcore-fuzz))
2912              ?  ;Whitespace
2913            (if (< score gnus-summary-default-score)
2914                gnus-score-below-mark gnus-score-over-mark))
2915          'score))
2916       ;; Do visual highlighting.
2917       (when (gnus-visual-p 'summary-highlight 'highlight)
2918         (gnus-run-hooks 'gnus-summary-update-hook)))))
2919
2920 (defvar gnus-tmp-new-adopts nil)
2921
2922 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2923   "Return the number of articles in THREAD.
2924 This may be 0 in some cases -- if none of the articles in
2925 the thread are to be displayed."
2926   (let* ((number
2927           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2928           (cond
2929            ((not (listp thread))
2930             1)
2931            ((and (consp thread) (cdr thread))
2932             (apply
2933              '+ 1 (mapcar
2934                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2935            ((null thread)
2936             1)
2937            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2938             1)
2939            (t 0))))
2940     (when (and level (zerop level) gnus-tmp-new-adopts)
2941       (incf number
2942             (apply '+ (mapcar
2943                        'gnus-summary-number-of-articles-in-thread
2944                        gnus-tmp-new-adopts))))
2945     (if char
2946         (if (> number 1) gnus-not-empty-thread-mark
2947           gnus-empty-thread-mark)
2948       number)))
2949
2950 (defun gnus-summary-set-local-parameters (group)
2951   "Go through the local params of GROUP and set all variable specs in that list."
2952   (let ((params (gnus-group-find-parameter group))
2953         elem)
2954     (while params
2955       (setq elem (car params)
2956             params (cdr params))
2957       (and (consp elem)                 ; Has to be a cons.
2958            (consp (cdr elem))           ; The cdr has to be a list.
2959            (symbolp (car elem))         ; Has to be a symbol in there.
2960            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2961            (ignore-errors               ; So we set it.
2962              (make-local-variable (car elem))
2963              (set (car elem) (eval (nth 1 elem))))))))
2964
2965 (defun gnus-summary-read-group (group &optional show-all no-article
2966                                       kill-buffer no-display backward
2967                                       select-articles)
2968   "Start reading news in newsgroup GROUP.
2969 If SHOW-ALL is non-nil, already read articles are also listed.
2970 If NO-ARTICLE is non-nil, no article is selected initially.
2971 If NO-DISPLAY, don't generate a summary buffer."
2972   (let (result)
2973     (while (and group
2974                 (null (setq result
2975                             (let ((gnus-auto-select-next nil))
2976                               (or (gnus-summary-read-group-1
2977                                    group show-all no-article
2978                                    kill-buffer no-display
2979                                    select-articles)
2980                                   (setq show-all nil
2981                                         select-articles nil)))))
2982                 (eq gnus-auto-select-next 'quietly))
2983       (set-buffer gnus-group-buffer)
2984       ;; The entry function called above goes to the next
2985       ;; group automatically, so we go two groups back
2986       ;; if we are searching for the previous group.
2987       (when backward
2988         (gnus-group-prev-unread-group 2))
2989       (if (not (equal group (gnus-group-group-name)))
2990           (setq group (gnus-group-group-name))
2991         (setq group nil)))
2992     result))
2993
2994 (defun gnus-summary-jump-to-other-group (group &optional show-all)
2995   "Directly jump to the other GROUP from summary buffer.
2996 If SHOW-ALL is non-nil, already read articles are also listed."
2997   (interactive
2998    (if (eq gnus-summary-buffer (current-buffer))
2999        (list (completing-read
3000               "Group: " gnus-active-hashtb nil t
3001               (when (and gnus-newsgroup-name
3002                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3003                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3004               'gnus-group-history)
3005              current-prefix-arg)
3006      (error "%s must be invoked from a gnus summary buffer." this-command)))
3007   (unless (or (zerop (length group))
3008               (and gnus-newsgroup-name
3009                    (string-equal gnus-newsgroup-name group)))
3010     (gnus-summary-exit)
3011     (gnus-summary-read-group group show-all
3012                              gnus-dont-select-after-jump-to-other-group)))
3013
3014 (defun gnus-summary-read-group-1 (group show-all no-article
3015                                         kill-buffer no-display
3016                                         &optional select-articles)
3017   ;; Killed foreign groups can't be entered.
3018   ;;  (when (and (not (gnus-group-native-p group))
3019   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3020   ;;    (error "Dead non-native groups can't be entered"))
3021   (gnus-message 5 "Retrieving newsgroup: %s..." group)
3022   (let* ((new-group (gnus-summary-setup-buffer group))
3023          (quit-config (gnus-group-quit-config group))
3024          (did-select (and new-group (gnus-select-newsgroup
3025                                      group show-all select-articles))))
3026     (cond
3027      ;; This summary buffer exists already, so we just select it.
3028      ((not new-group)
3029       (gnus-set-global-variables)
3030       (when kill-buffer
3031         (gnus-kill-or-deaden-summary kill-buffer))
3032       (gnus-configure-windows 'summary 'force)
3033       (gnus-set-mode-line 'summary)
3034       (gnus-summary-position-point)
3035       (message "")
3036       t)
3037      ;; We couldn't select this group.
3038      ((null did-select)
3039       (when (and (eq major-mode 'gnus-summary-mode)
3040                  (not (equal (current-buffer) kill-buffer)))
3041         (kill-buffer (current-buffer))
3042         (if (not quit-config)
3043             (progn
3044               ;; Update the info -- marks might need to be removed,
3045               ;; for instance.
3046               (gnus-summary-update-info)
3047               (set-buffer gnus-group-buffer)
3048               (gnus-group-jump-to-group group)
3049               (gnus-group-next-unread-group 1))
3050           (gnus-handle-ephemeral-exit quit-config)))
3051       (let ((grpinfo (gnus-get-info group)))
3052         (if (null (gnus-info-read grpinfo))
3053             (gnus-message 3 "Group %s contains no messages" group)
3054           (gnus-message 3 "Can't select group")))
3055       nil)
3056      ;; The user did a `C-g' while prompting for number of articles,
3057      ;; so we exit this group.
3058      ((eq did-select 'quit)
3059       (and (eq major-mode 'gnus-summary-mode)
3060            (not (equal (current-buffer) kill-buffer))
3061            (kill-buffer (current-buffer)))
3062       (when kill-buffer
3063         (gnus-kill-or-deaden-summary kill-buffer))
3064       (if (not quit-config)
3065           (progn
3066             (set-buffer gnus-group-buffer)
3067             (gnus-group-jump-to-group group)
3068             (gnus-group-next-unread-group 1)
3069             (gnus-configure-windows 'group 'force))
3070         (gnus-handle-ephemeral-exit quit-config))
3071       ;; Finally signal the quit.
3072       (signal 'quit nil))
3073      ;; The group was successfully selected.
3074      (t
3075       (gnus-set-global-variables)
3076       ;; Save the active value in effect when the group was entered.
3077       (setq gnus-newsgroup-active
3078             (gnus-copy-sequence
3079              (gnus-active gnus-newsgroup-name)))
3080       ;; You can change the summary buffer in some way with this hook.
3081       (gnus-run-hooks 'gnus-select-group-hook)
3082       (gnus-update-format-specifications
3083        nil 'summary 'summary-mode 'summary-dummy)
3084       (gnus-update-summary-mark-positions)
3085       ;; Do score processing.
3086       (when gnus-use-scoring
3087         (gnus-possibly-score-headers))
3088       ;; Check whether to fill in the gaps in the threads.
3089       (when gnus-build-sparse-threads
3090         (gnus-build-sparse-threads))
3091       ;; Find the initial limit.
3092       (if gnus-show-threads
3093           (if show-all
3094               (let ((gnus-newsgroup-dormant nil))
3095                 (gnus-summary-initial-limit show-all))
3096             (gnus-summary-initial-limit show-all))
3097         ;; When untreaded, all articles are always shown.
3098         (setq gnus-newsgroup-limit
3099               (mapcar
3100                (lambda (header) (mail-header-number header))
3101                gnus-newsgroup-headers)))
3102       ;; Generate the summary buffer.
3103       (unless no-display
3104         (gnus-summary-prepare))
3105       (when gnus-use-trees
3106         (gnus-tree-open group)
3107         (setq gnus-summary-highlight-line-function
3108               'gnus-tree-highlight-article))
3109       ;; If the summary buffer is empty, but there are some low-scored
3110       ;; articles or some excluded dormants, we include these in the
3111       ;; buffer.
3112       (when (and (zerop (buffer-size))
3113                  (not no-display))
3114         (cond (gnus-newsgroup-dormant
3115                (gnus-summary-limit-include-dormant))
3116               ((and gnus-newsgroup-scored show-all)
3117                (gnus-summary-limit-include-expunged t))))
3118       ;; Function `gnus-apply-kill-file' must be called in this hook.
3119       (gnus-run-hooks 'gnus-apply-kill-hook)
3120       (if (and (zerop (buffer-size))
3121                (not no-display))
3122           (progn
3123             ;; This newsgroup is empty.
3124             (gnus-summary-catchup-and-exit nil t)
3125             (gnus-message 6 "No unread news")
3126             (when kill-buffer
3127               (gnus-kill-or-deaden-summary kill-buffer))
3128             ;; Return nil from this function.
3129             nil)
3130         ;; Hide conversation thread subtrees.  We cannot do this in
3131         ;; gnus-summary-prepare-hook since kill processing may not
3132         ;; work with hidden articles.
3133         (and gnus-show-threads
3134              gnus-thread-hide-subtree
3135              (gnus-summary-hide-all-threads))
3136         (when kill-buffer
3137           (gnus-kill-or-deaden-summary kill-buffer))
3138         ;; Show first unread article if requested.
3139         (if (and (not no-article)
3140                  (not no-display)
3141                  gnus-newsgroup-unreads
3142                  gnus-auto-select-first)
3143             (progn
3144               (gnus-configure-windows 'summary)
3145               (cond
3146                ((eq gnus-auto-select-first 'best)
3147                 (gnus-summary-best-unread-article))
3148                ((eq gnus-auto-select-first t)
3149                 (gnus-summary-first-unread-article))
3150                ((gnus-functionp gnus-auto-select-first)
3151                 (funcall gnus-auto-select-first))))
3152           ;; Don't select any articles, just move point to the first
3153           ;; article in the group.
3154           (goto-char (point-min))
3155           (gnus-summary-position-point)
3156           (gnus-configure-windows 'summary 'force)
3157           (gnus-set-mode-line 'summary))
3158         (when (get-buffer-window gnus-group-buffer t)
3159           ;; Gotta use windows, because recenter does weird stuff if
3160           ;; the current buffer ain't the displayed window.
3161           (let ((owin (selected-window)))
3162             (select-window (get-buffer-window gnus-group-buffer t))
3163             (when (gnus-group-goto-group group)
3164               (recenter))
3165             (select-window owin)))
3166         ;; Mark this buffer as "prepared".
3167         (setq gnus-newsgroup-prepared t)
3168         (gnus-run-hooks 'gnus-summary-prepared-hook)
3169         t)))))
3170
3171 (defun gnus-summary-prepare ()
3172   "Generate the summary buffer."
3173   (interactive)
3174   (let ((buffer-read-only nil))
3175     (erase-buffer)
3176     (setq gnus-newsgroup-data nil
3177           gnus-newsgroup-data-reverse nil)
3178     (gnus-run-hooks 'gnus-summary-generate-hook)
3179     ;; Generate the buffer, either with threads or without.
3180     (when gnus-newsgroup-headers
3181       (gnus-summary-prepare-threads
3182        (if gnus-show-threads
3183            (gnus-sort-gathered-threads
3184             (funcall gnus-summary-thread-gathering-function
3185                      (gnus-sort-threads
3186                       (gnus-cut-threads (gnus-make-threads)))))
3187          ;; Unthreaded display.
3188          (gnus-sort-articles gnus-newsgroup-headers))))
3189     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3190     ;; Call hooks for modifying summary buffer.
3191     (goto-char (point-min))
3192     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3193
3194 (defsubst gnus-general-simplify-subject (subject)
3195   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3196   (setq subject
3197         (cond
3198          ;; Truncate the subject.
3199          (gnus-simplify-subject-functions
3200           (gnus-map-function gnus-simplify-subject-functions subject))
3201          ((numberp gnus-summary-gather-subject-limit)
3202           (setq subject (gnus-simplify-subject-re subject))
3203           (if (> (length subject) gnus-summary-gather-subject-limit)
3204               (substring subject 0 gnus-summary-gather-subject-limit)
3205             subject))
3206          ;; Fuzzily simplify it.
3207          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3208           (gnus-simplify-subject-fuzzy subject))
3209          ;; Just remove the leading "Re:".
3210          (t
3211           (gnus-simplify-subject-re subject))))
3212
3213   (if (and gnus-summary-gather-exclude-subject
3214            (string-match gnus-summary-gather-exclude-subject subject))
3215       nil                               ; This article shouldn't be gathered
3216     subject))
3217
3218 (defun gnus-summary-simplify-subject-query ()
3219   "Query where the respool algorithm would put this article."
3220   (interactive)
3221   (gnus-summary-select-article)
3222   (message "%s"
3223            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3224
3225 (defun gnus-gather-threads-by-subject (threads)
3226   "Gather threads by looking at Subject headers."
3227   (if (not gnus-summary-make-false-root)
3228       threads
3229     (let ((hashtb (gnus-make-hashtable 1024))
3230           (prev threads)
3231           (result threads)
3232           subject hthread whole-subject)
3233       (while threads
3234         (setq subject (gnus-general-simplify-subject
3235                        (setq whole-subject (mail-header-subject
3236                                             (caar threads)))))
3237         (when subject
3238           (if (setq hthread (gnus-gethash subject hashtb))
3239               (progn
3240                 ;; We enter a dummy root into the thread, if we
3241                 ;; haven't done that already.
3242                 (unless (stringp (caar hthread))
3243                   (setcar hthread (list whole-subject (car hthread))))
3244                 ;; We add this new gathered thread to this gathered
3245                 ;; thread.
3246                 (setcdr (car hthread)
3247                         (nconc (cdar hthread) (list (car threads))))
3248                 ;; Remove it from the list of threads.
3249                 (setcdr prev (cdr threads))
3250                 (setq threads prev))
3251             ;; Enter this thread into the hash table.
3252             (gnus-sethash subject threads hashtb)))
3253         (setq prev threads)
3254         (setq threads (cdr threads)))
3255       result)))
3256
3257 (defun gnus-gather-threads-by-references (threads)
3258   "Gather threads by looking at References headers."
3259   (let ((idhashtb (gnus-make-hashtable 1024))
3260         (thhashtb (gnus-make-hashtable 1024))
3261         (prev threads)
3262         (result threads)
3263         ids references id gthread gid entered ref)
3264     (while threads
3265       (when (setq references (mail-header-references (caar threads)))
3266         (setq id (mail-header-id (caar threads))
3267               ids (gnus-split-references references)
3268               entered nil)
3269         (while (setq ref (pop ids))
3270           (setq ids (delete ref ids))
3271           (if (not (setq gid (gnus-gethash ref idhashtb)))
3272               (progn
3273                 (gnus-sethash ref id idhashtb)
3274                 (gnus-sethash id threads thhashtb))
3275             (setq gthread (gnus-gethash gid thhashtb))
3276             (unless entered
3277               ;; We enter a dummy root into the thread, if we
3278               ;; haven't done that already.
3279               (unless (stringp (caar gthread))
3280                 (setcar gthread (list (mail-header-subject (caar gthread))
3281                                       (car gthread))))
3282               ;; We add this new gathered thread to this gathered
3283               ;; thread.
3284               (setcdr (car gthread)
3285                       (nconc (cdar gthread) (list (car threads)))))
3286             ;; Add it into the thread hash table.
3287             (gnus-sethash id gthread thhashtb)
3288             (setq entered t)
3289             ;; Remove it from the list of threads.
3290             (setcdr prev (cdr threads))
3291             (setq threads prev))))
3292       (setq prev threads)
3293       (setq threads (cdr threads)))
3294     result))
3295
3296 (defun gnus-sort-gathered-threads (threads)
3297   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3298   (let ((result threads))
3299     (while threads
3300       (when (stringp (caar threads))
3301         (setcdr (car threads)
3302                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3303       (setq threads (cdr threads)))
3304     result))
3305
3306 (defun gnus-thread-loop-p (root thread)
3307   "Say whether ROOT is in THREAD."
3308   (let ((stack (list thread))
3309         (infloop 0)
3310         th)
3311     (while (setq thread (pop stack))
3312       (setq th (cdr thread))
3313       (while (and th
3314                   (not (eq (caar th) root)))
3315         (pop th))
3316       (if th
3317           ;; We have found a loop.
3318           (let (ref-dep)
3319             (setcdr thread (delq (car th) (cdr thread)))
3320             (if (boundp (setq ref-dep (intern "none"
3321                                               gnus-newsgroup-dependencies)))
3322                 (setcdr (symbol-value ref-dep)
3323                         (nconc (cdr (symbol-value ref-dep))
3324                                (list (car th))))
3325               (set ref-dep (list nil (car th))))
3326             (setq infloop 1
3327                   stack nil))
3328         ;; Push all the subthreads onto the stack.
3329         (push (cdr thread) stack)))
3330     infloop))
3331
3332 (defun gnus-make-threads ()
3333   "Go through the dependency hashtb and find the roots.  Return all threads."
3334   (let (threads)
3335     (while (catch 'infloop
3336              (mapatoms
3337               (lambda (refs)
3338                 ;; Deal with self-referencing References loops.
3339                 (when (and (car (symbol-value refs))
3340                            (not (zerop
3341                                  (apply
3342                                   '+
3343                                   (mapcar
3344                                    (lambda (thread)
3345                                      (gnus-thread-loop-p
3346                                       (car (symbol-value refs)) thread))
3347                                    (cdr (symbol-value refs)))))))
3348                   (setq threads nil)
3349                   (throw 'infloop t))
3350                 (unless (car (symbol-value refs))
3351                   ;; These threads do not refer back to any other articles,
3352                   ;; so they're roots.
3353                   (setq threads (append (cdr (symbol-value refs)) threads))))
3354               gnus-newsgroup-dependencies)))
3355     threads))
3356
3357 ;; Build the thread tree.
3358 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3359   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3360
3361 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3362 if it was already present.
3363
3364 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3365 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3366 Message-IDs will be renamed be renamed to a unique Message-ID before
3367 being entered.
3368
3369 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3370   (let* ((id (mail-header-id header))
3371          (id-dep (and id (intern id dependencies)))
3372          ref ref-dep ref-header)
3373     ;; Enter this `header' in the `dependencies' table.
3374     (cond
3375      ((not id-dep)
3376       (setq header nil))
3377      ;; The first two cases do the normal part: enter a new `header'
3378      ;; in the `dependencies' table.
3379      ((not (boundp id-dep))
3380       (set id-dep (list header)))
3381      ((null (car (symbol-value id-dep)))
3382       (setcar (symbol-value id-dep) header))
3383
3384      ;; From here the `header' was already present in the
3385      ;; `dependencies' table.
3386      (force-new
3387       ;; Overrides an existing entry;
3388       ;; just set the header part of the entry.
3389       (setcar (symbol-value id-dep) header))
3390
3391      ;; Renames the existing `header' to a unique Message-ID.
3392      ((not gnus-summary-ignore-duplicates)
3393       ;; An article with this Message-ID has already been seen.
3394       ;; We rename the Message-ID.
3395       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3396            (list header))
3397       (mail-header-set-id header id))
3398
3399      ;; The last case ignores an existing entry, except it adds any
3400      ;; additional Xrefs (in case the two articles came from different
3401      ;; servers.
3402      ;; Also sets `header' to `nil' meaning that the `dependencies'
3403      ;; table was *not* modified.
3404      (t
3405       (mail-header-set-xref
3406        (car (symbol-value id-dep))
3407        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3408                    "")
3409                (or (mail-header-xref header) "")))
3410       (setq header nil)))
3411
3412     (when header
3413       ;; First check if that we are not creating a References loop.
3414       (setq ref (gnus-parent-id (mail-header-references header)))
3415       (while (and ref
3416                   (setq ref-dep (intern-soft ref dependencies))
3417                   (boundp ref-dep)
3418                   (setq ref-header (car (symbol-value ref-dep))))
3419         (if (string= id ref)
3420             ;; Yuk!  This is a reference loop.  Make the article be a
3421             ;; root article.
3422             (progn
3423               (mail-header-set-references (car (symbol-value id-dep)) "none")
3424               (setq ref nil))
3425           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3426       (setq ref (gnus-parent-id (mail-header-references header)))
3427       (setq ref-dep (intern (or ref "none") dependencies))
3428       (if (boundp ref-dep)
3429           (setcdr (symbol-value ref-dep)
3430                   (nconc (cdr (symbol-value ref-dep))
3431                          (list (symbol-value id-dep))))
3432         (set ref-dep (list nil (symbol-value id-dep)))))
3433     header))
3434
3435 (defun gnus-build-sparse-threads ()
3436   (let ((headers gnus-newsgroup-headers)
3437         (mail-parse-charset gnus-newsgroup-charset)
3438         (gnus-summary-ignore-duplicates t)
3439         header references generation relations
3440         subject child end new-child date)
3441     ;; First we create an alist of generations/relations, where
3442     ;; generations is how much we trust the relation, and the relation
3443     ;; is parent/child.
3444     (gnus-message 7 "Making sparse threads...")
3445     (save-excursion
3446       (nnheader-set-temp-buffer " *gnus sparse threads*")
3447       (while (setq header (pop headers))
3448         (when (and (setq references (mail-header-references header))
3449                    (not (string= references "")))
3450           (insert references)
3451           (setq child (mail-header-id header)
3452                 subject (mail-header-subject header)
3453                 date (mail-header-date header)
3454                 generation 0)
3455           (while (search-backward ">" nil t)
3456             (setq end (1+ (point)))
3457             (when (search-backward "<" nil t)
3458               (setq new-child (buffer-substring (point) end))
3459               (push (list (incf generation)
3460                           child (setq child new-child)
3461                           subject date)
3462                     relations)))
3463           (when child
3464             (push (list (1+ generation) child nil subject) relations))
3465           (erase-buffer)))
3466       (kill-buffer (current-buffer)))
3467     ;; Sort over trustworthiness.
3468     (mapcar
3469      (lambda (relation)
3470        (when (gnus-dependencies-add-header
3471               (make-full-mail-header-from-decoded-header
3472                gnus-reffed-article-number
3473                (nth 3 relation) "" (or (nth 4 relation) "")
3474                (nth 1 relation)
3475                (or (nth 2 relation) "") 0 0 "")
3476               gnus-newsgroup-dependencies nil)
3477          (push gnus-reffed-article-number gnus-newsgroup-limit)
3478          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3479          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3480                gnus-newsgroup-reads)
3481          (decf gnus-reffed-article-number)))
3482      (sort relations 'car-less-than-car))
3483     (gnus-message 7 "Making sparse threads...done")))
3484
3485 (defun gnus-build-old-threads ()
3486   ;; Look at all the articles that refer back to old articles, and
3487   ;; fetch the headers for the articles that aren't there.  This will
3488   ;; build complete threads - if the roots haven't been expired by the
3489   ;; server, that is.
3490   (let ((mail-parse-charset gnus-newsgroup-charset)
3491         id heads)
3492     (mapatoms
3493      (lambda (refs)
3494        (when (not (car (symbol-value refs)))
3495          (setq heads (cdr (symbol-value refs)))
3496          (while heads
3497            (if (memq (mail-header-number (caar heads))
3498                      gnus-newsgroup-dormant)
3499                (setq heads (cdr heads))
3500              (setq id (symbol-name refs))
3501              (while (and (setq id (gnus-build-get-header id))
3502                          (not (car (gnus-id-to-thread id)))))
3503              (setq heads nil)))))
3504      gnus-newsgroup-dependencies)))
3505
3506 ;; This function has to be called with point after the article number
3507 ;; on the beginning of the line.
3508 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3509   (let ((eol (gnus-point-at-eol))
3510         (buffer (current-buffer))
3511         header)
3512
3513     ;; overview: [num subject from date id refs chars lines misc]
3514     (unwind-protect
3515         (progn
3516           (narrow-to-region (point) eol)
3517           (unless (eobp)
3518             (forward-char))
3519
3520           (setq header
3521                 (make-full-mail-header
3522                  number                         ; number
3523                  (nnheader-nov-field)           ; subject
3524                  (nnheader-nov-field)           ; from
3525                  (nnheader-nov-field)           ; date
3526                  (nnheader-nov-read-message-id) ; id
3527                  (nnheader-nov-field)           ; refs
3528                  (nnheader-nov-read-integer)    ; chars
3529                  (nnheader-nov-read-integer)    ; lines
3530                  (unless (eobp)
3531                    (if (looking-at "Xref: ")
3532                        (goto-char (match-end 0)))
3533                    (nnheader-nov-field))        ; Xref
3534                  (nnheader-nov-parse-extra))))  ; extra
3535
3536       (widen))
3537
3538     (when gnus-alter-header-function
3539       (funcall gnus-alter-header-function header))
3540     (gnus-dependencies-add-header header dependencies force-new)))
3541
3542 (defun gnus-build-get-header (id)
3543   "Look through the buffer of NOV lines and find the header to ID.
3544 Enter this line into the dependencies hash table, and return
3545 the id of the parent article (if any)."
3546   (let ((deps gnus-newsgroup-dependencies)
3547         found header)
3548     (prog1
3549         (save-excursion
3550           (set-buffer nntp-server-buffer)
3551           (let ((case-fold-search nil))
3552             (goto-char (point-min))
3553             (while (and (not found)
3554                         (search-forward id nil t))
3555               (beginning-of-line)
3556               (setq found (looking-at
3557                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3558                                    (regexp-quote id))))
3559               (or found (beginning-of-line 2)))
3560             (when found
3561               (beginning-of-line)
3562               (and
3563                (setq header (gnus-nov-parse-line
3564                              (read (current-buffer)) deps))
3565                (gnus-parent-id (mail-header-references header))))))
3566       (when header
3567         (let ((number (mail-header-number header)))
3568           (push number gnus-newsgroup-limit)
3569           (push header gnus-newsgroup-headers)
3570           (if (memq number gnus-newsgroup-unselected)
3571               (progn
3572                 (push number gnus-newsgroup-unreads)
3573                 (setq gnus-newsgroup-unselected
3574                       (delq number gnus-newsgroup-unselected)))
3575             (push number gnus-newsgroup-ancient)))))))
3576
3577 (defun gnus-build-all-threads ()
3578   "Read all the headers."
3579   (let ((gnus-summary-ignore-duplicates t)
3580         (mail-parse-charset gnus-newsgroup-charset)
3581         (dependencies gnus-newsgroup-dependencies)
3582         header article)
3583     (save-excursion
3584       (set-buffer nntp-server-buffer)
3585       (let ((case-fold-search nil))
3586         (goto-char (point-min))
3587         (while (not (eobp))
3588           (ignore-errors
3589             (setq article (read (current-buffer))
3590                   header (gnus-nov-parse-line article dependencies)))
3591           (when header
3592             (save-excursion
3593               (set-buffer gnus-summary-buffer)
3594               (push header gnus-newsgroup-headers)
3595               (if (memq (setq article (mail-header-number header))
3596                         gnus-newsgroup-unselected)
3597                   (progn
3598                     (push article gnus-newsgroup-unreads)
3599                     (setq gnus-newsgroup-unselected
3600                           (delq article gnus-newsgroup-unselected)))
3601                 (push article gnus-newsgroup-ancient)))
3602             (forward-line 1)))))))
3603
3604 (defun gnus-summary-update-article-line (article header)
3605   "Update the line for ARTICLE using HEADERS."
3606   (let* ((id (mail-header-id header))
3607          (thread (gnus-id-to-thread id)))
3608     (unless thread
3609       (error "Article in no thread"))
3610     ;; Update the thread.
3611     (setcar thread header)
3612     (gnus-summary-goto-subject article)
3613     (let* ((datal (gnus-data-find-list article))
3614            (data (car datal))
3615            (length (when (cdr datal)
3616                      (- (gnus-data-pos data)
3617                         (gnus-data-pos (cadr datal)))))
3618            (buffer-read-only nil)
3619            (level (gnus-summary-thread-level)))
3620       (gnus-delete-line)
3621       (gnus-summary-insert-line
3622        header level nil (gnus-article-mark article)
3623        (memq article gnus-newsgroup-replied)
3624        (memq article gnus-newsgroup-expirable)
3625        ;; Only insert the Subject string when it's different
3626        ;; from the previous Subject string.
3627        (if (and
3628             gnus-show-threads
3629             (gnus-subject-equal
3630              (condition-case ()
3631                  (mail-header-subject
3632                   (gnus-data-header
3633                    (cadr
3634                     (gnus-data-find-list
3635                      article
3636                      (gnus-data-list t)))))
3637                ;; Error on the side of excessive subjects.
3638                (error ""))
3639              (mail-header-subject header)))
3640            ""
3641          (mail-header-subject header))
3642        nil (cdr (assq article gnus-newsgroup-scored))
3643        (memq article gnus-newsgroup-processable))
3644       (when length
3645         (gnus-data-update-list
3646          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3647
3648 (defun gnus-summary-update-article (article &optional iheader)
3649   "Update ARTICLE in the summary buffer."
3650   (set-buffer gnus-summary-buffer)
3651   (let* ((header (gnus-summary-article-header article))
3652          (id (mail-header-id header))
3653          (data (gnus-data-find article))
3654          (thread (gnus-id-to-thread id))
3655          (references (mail-header-references header))
3656          (parent
3657           (gnus-id-to-thread
3658            (or (gnus-parent-id
3659                 (when (and references
3660                            (not (equal "" references)))
3661                   references))
3662                "none")))
3663          (buffer-read-only nil)
3664          (old (car thread)))
3665     (when thread
3666       (unless iheader
3667         (setcar thread nil)
3668         (when parent
3669           (delq thread parent)))
3670       (if (gnus-summary-insert-subject id header)
3671           ;; Set the (possibly) new article number in the data structure.
3672           (gnus-data-set-number data (gnus-id-to-article id))
3673         (setcar thread old)
3674         nil))))
3675
3676 (defun gnus-rebuild-thread (id &optional line)
3677   "Rebuild the thread containing ID.
3678 If LINE, insert the rebuilt thread starting on line LINE."
3679   (let ((buffer-read-only nil)
3680         old-pos current thread data)
3681     (if (not gnus-show-threads)
3682         (setq thread (list (car (gnus-id-to-thread id))))
3683       ;; Get the thread this article is part of.
3684       (setq thread (gnus-remove-thread id)))
3685     (setq old-pos (gnus-point-at-bol))
3686     (setq current (save-excursion
3687                     (and (zerop (forward-line -1))
3688                          (gnus-summary-article-number))))
3689     ;; If this is a gathered thread, we have to go some re-gathering.
3690     (when (stringp (car thread))
3691       (let ((subject (car thread))
3692             roots thr)
3693         (setq thread (cdr thread))
3694         (while thread
3695           (unless (memq (setq thr (gnus-id-to-thread
3696                                    (gnus-root-id
3697                                     (mail-header-id (caar thread)))))
3698                         roots)
3699             (push thr roots))
3700           (setq thread (cdr thread)))
3701         ;; We now have all (unique) roots.
3702         (if (= (length roots) 1)
3703             ;; All the loose roots are now one solid root.
3704             (setq thread (car roots))
3705           (setq thread (cons subject (gnus-sort-threads roots))))))
3706     (let (threads)
3707       ;; We then insert this thread into the summary buffer.
3708       (when line
3709         (goto-char (point-min))
3710         (forward-line (1- line)))
3711       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3712         (if gnus-show-threads
3713             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3714           (gnus-summary-prepare-unthreaded thread))
3715         (setq data (nreverse gnus-newsgroup-data))
3716         (setq threads gnus-newsgroup-threads))
3717       ;; We splice the new data into the data structure.
3718       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3719       ;;!!! then we want to insert at the beginning of the buffer.
3720       ;;!!! That happens to be true with Gnus now, but that may
3721       ;;!!! change in the future.  Perhaps.
3722       (gnus-data-enter-list
3723        (if line nil current) data (- (point) old-pos))
3724       (setq gnus-newsgroup-threads
3725             (nconc threads gnus-newsgroup-threads))
3726       (gnus-data-compute-positions))))
3727
3728 (defun gnus-number-to-header (number)
3729   "Return the header for article NUMBER."
3730   (let ((headers gnus-newsgroup-headers))
3731     (while (and headers
3732                 (not (= number (mail-header-number (car headers)))))
3733       (pop headers))
3734     (when headers
3735       (car headers))))
3736
3737 (defun gnus-parent-headers (in-headers &optional generation)
3738   "Return the headers of the GENERATIONeth parent of HEADERS."
3739   (unless generation
3740     (setq generation 1))
3741   (let ((parent t)
3742         (headers in-headers)
3743         references)
3744     (while (and parent
3745                 (not (zerop generation))
3746                 (setq references (mail-header-references headers)))
3747       (setq headers (if (and references
3748                              (setq parent (gnus-parent-id references)))
3749                         (car (gnus-id-to-thread parent))
3750                       nil))
3751       (decf generation))
3752     (and (not (eq headers in-headers))
3753          headers)))
3754
3755 (defun gnus-id-to-thread (id)
3756   "Return the (sub-)thread where ID appears."
3757   (gnus-gethash id gnus-newsgroup-dependencies))
3758
3759 (defun gnus-id-to-article (id)
3760   "Return the article number of ID."
3761   (let ((thread (gnus-id-to-thread id)))
3762     (when (and thread
3763                (car thread))
3764       (mail-header-number (car thread)))))
3765
3766 (defun gnus-id-to-header (id)
3767   "Return the article headers of ID."
3768   (car (gnus-id-to-thread id)))
3769
3770 (defun gnus-article-displayed-root-p (article)
3771   "Say whether ARTICLE is a root(ish) article."
3772   (let ((level (gnus-summary-thread-level article))
3773         (refs (mail-header-references  (gnus-summary-article-header article)))
3774         particle)
3775     (cond
3776      ((null level) nil)
3777      ((zerop level) t)
3778      ((null refs) t)
3779      ((null (gnus-parent-id refs)) t)
3780      ((and (= 1 level)
3781            (null (setq particle (gnus-id-to-article
3782                                  (gnus-parent-id refs))))
3783            (null (gnus-summary-thread-level particle)))))))
3784
3785 (defun gnus-root-id (id)
3786   "Return the id of the root of the thread where ID appears."
3787   (let (last-id prev)
3788     (while (and id (setq prev (car (gnus-id-to-thread id))))
3789       (setq last-id id
3790             id (gnus-parent-id (mail-header-references prev))))
3791     last-id))
3792
3793 (defun gnus-articles-in-thread (thread)
3794   "Return the list of articles in THREAD."
3795   (cons (mail-header-number (car thread))
3796         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3797
3798 (defun gnus-remove-thread (id &optional dont-remove)
3799   "Remove the thread that has ID in it."
3800   (let (headers thread last-id)
3801     ;; First go up in this thread until we find the root.
3802     (setq last-id (gnus-root-id id)
3803           headers (message-flatten-list (gnus-id-to-thread last-id)))
3804     ;; We have now found the real root of this thread.  It might have
3805     ;; been gathered into some loose thread, so we have to search
3806     ;; through the threads to find the thread we wanted.
3807     (let ((threads gnus-newsgroup-threads)
3808           sub)
3809       (while threads
3810         (setq sub (car threads))
3811         (if (stringp (car sub))
3812             ;; This is a gathered thread, so we look at the roots
3813             ;; below it to find whether this article is in this
3814             ;; gathered root.
3815             (progn
3816               (setq sub (cdr sub))
3817               (while sub
3818                 (when (member (caar sub) headers)
3819                   (setq thread (car threads)
3820                         threads nil
3821                         sub nil))
3822                 (setq sub (cdr sub))))
3823           ;; It's an ordinary thread, so we check it.
3824           (when (eq (car sub) (car headers))
3825             (setq thread sub
3826                   threads nil)))
3827         (setq threads (cdr threads)))
3828       ;; If this article is in no thread, then it's a root.
3829       (if thread
3830           (unless dont-remove
3831             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3832         (setq thread (gnus-id-to-thread last-id)))
3833       (when thread
3834         (prog1
3835             thread                      ; We return this thread.
3836           (unless dont-remove
3837             (if (stringp (car thread))
3838                 (progn
3839                   ;; If we use dummy roots, then we have to remove the
3840                   ;; dummy root as well.
3841                   (when (eq gnus-summary-make-false-root 'dummy)
3842                     ;; We go to the dummy root by going to
3843                     ;; the first sub-"thread", and then one line up.
3844                     (gnus-summary-goto-article
3845                      (mail-header-number (caadr thread)))
3846                     (forward-line -1)
3847                     (gnus-delete-line)
3848                     (gnus-data-compute-positions))
3849                   (setq thread (cdr thread))
3850                   (while thread
3851                     (gnus-remove-thread-1 (car thread))
3852                     (setq thread (cdr thread))))
3853               (gnus-remove-thread-1 thread))))))))
3854
3855 (defun gnus-remove-thread-1 (thread)
3856   "Remove the thread THREAD recursively."
3857   (let ((number (mail-header-number (pop thread)))
3858         d)
3859     (setq thread (reverse thread))
3860     (while thread
3861       (gnus-remove-thread-1 (pop thread)))
3862     (when (setq d (gnus-data-find number))
3863       (goto-char (gnus-data-pos d))
3864       (gnus-summary-show-thread)
3865       (gnus-data-remove
3866        number
3867        (- (gnus-point-at-bol)
3868           (prog1
3869               (1+ (gnus-point-at-eol))
3870             (gnus-delete-line)))))))
3871
3872 (defun gnus-sort-threads-1 (threads func)
3873   (sort (mapcar (lambda (thread)
3874                   (cons (car thread)
3875                         (and (cdr thread)
3876                              (gnus-sort-threads-1 (cdr thread) func))))
3877                 threads) func))
3878
3879 (defun gnus-sort-threads (threads)
3880   "Sort THREADS."
3881   (if (not gnus-thread-sort-functions)
3882       threads
3883     (gnus-message 8 "Sorting threads...")
3884     (prog1
3885         (gnus-sort-threads-1
3886          threads
3887          (gnus-make-sort-function gnus-thread-sort-functions))
3888       (gnus-message 8 "Sorting threads...done"))))
3889
3890 (defun gnus-sort-articles (articles)
3891   "Sort ARTICLES."
3892   (when gnus-article-sort-functions
3893     (gnus-message 7 "Sorting articles...")
3894     (prog1
3895         (setq gnus-newsgroup-headers
3896               (sort articles (gnus-make-sort-function
3897                               gnus-article-sort-functions)))
3898       (gnus-message 7 "Sorting articles...done"))))
3899
3900 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3901 (defmacro gnus-thread-header (thread)
3902   "Return header of first article in THREAD.
3903 Note that THREAD must never, ever be anything else than a variable -
3904 using some other form will lead to serious barfage."
3905   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3906   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3907   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3908         (vector thread) 2))
3909
3910 (defsubst gnus-article-sort-by-number (h1 h2)
3911   "Sort articles by article number."
3912   (< (mail-header-number h1)
3913      (mail-header-number h2)))
3914
3915 (defun gnus-thread-sort-by-number (h1 h2)
3916   "Sort threads by root article number."
3917   (gnus-article-sort-by-number
3918    (gnus-thread-header h1) (gnus-thread-header h2)))
3919
3920 (defsubst gnus-article-sort-by-lines (h1 h2)
3921   "Sort articles by article Lines header."
3922   (< (mail-header-lines h1)
3923      (mail-header-lines h2)))
3924
3925 (defun gnus-thread-sort-by-lines (h1 h2)
3926   "Sort threads by root article Lines header."
3927   (gnus-article-sort-by-lines
3928    (gnus-thread-header h1) (gnus-thread-header h2)))
3929
3930 (defsubst gnus-article-sort-by-chars (h1 h2)
3931   "Sort articles by octet length."
3932   (< (mail-header-chars h1)
3933      (mail-header-chars h2)))
3934
3935 (defun gnus-thread-sort-by-chars (h1 h2)
3936   "Sort threads by root article octet length."
3937   (gnus-article-sort-by-chars
3938    (gnus-thread-header h1) (gnus-thread-header h2)))
3939
3940 (defsubst gnus-article-sort-by-author (h1 h2)
3941   "Sort articles by root author."
3942   (string-lessp
3943    (let ((addr (car (mime-entity-read-field h1 'From))))
3944      (or (std11-full-name-string addr)
3945          (std11-address-string addr)
3946          ""))
3947    (let ((addr (car (mime-entity-read-field h2 'From))))
3948      (or (std11-full-name-string addr)
3949          (std11-address-string addr)
3950          ""))
3951    ))
3952
3953 (defun gnus-thread-sort-by-author (h1 h2)
3954   "Sort threads by root author."
3955   (gnus-article-sort-by-author
3956    (gnus-thread-header h1)  (gnus-thread-header h2)))
3957
3958 (defsubst gnus-article-sort-by-subject (h1 h2)
3959   "Sort articles by root subject."
3960   (string-lessp
3961    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3962    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3963
3964 (defun gnus-thread-sort-by-subject (h1 h2)
3965   "Sort threads by root subject."
3966   (gnus-article-sort-by-subject
3967    (gnus-thread-header h1) (gnus-thread-header h2)))
3968
3969 (defsubst gnus-article-sort-by-date (h1 h2)
3970   "Sort articles by root article date."
3971   (time-less-p
3972    (gnus-date-get-time (mail-header-date h1))
3973    (gnus-date-get-time (mail-header-date h2))))
3974
3975 (defun gnus-thread-sort-by-date (h1 h2)
3976   "Sort threads by root article date."
3977   (gnus-article-sort-by-date
3978    (gnus-thread-header h1) (gnus-thread-header h2)))
3979
3980 (defsubst gnus-article-sort-by-score (h1 h2)
3981   "Sort articles by root article score.
3982 Unscored articles will be counted as having a score of zero."
3983   (> (or (cdr (assq (mail-header-number h1)
3984                     gnus-newsgroup-scored))
3985          gnus-summary-default-score 0)
3986      (or (cdr (assq (mail-header-number h2)
3987                     gnus-newsgroup-scored))
3988          gnus-summary-default-score 0)))
3989
3990 (defun gnus-thread-sort-by-score (h1 h2)
3991   "Sort threads by root article score."
3992   (gnus-article-sort-by-score
3993    (gnus-thread-header h1) (gnus-thread-header h2)))
3994
3995 (defun gnus-thread-sort-by-total-score (h1 h2)
3996   "Sort threads by the sum of all scores in the thread.
3997 Unscored articles will be counted as having a score of zero."
3998   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3999
4000 (defun gnus-thread-total-score (thread)
4001   ;; This function find the total score of THREAD.
4002   (cond ((null thread)
4003          0)
4004         ((consp thread)
4005          (if (stringp (car thread))
4006              (apply gnus-thread-score-function 0
4007                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4008            (gnus-thread-total-score-1 thread)))
4009         (t
4010          (gnus-thread-total-score-1 (list thread)))))
4011
4012 (defun gnus-thread-total-score-1 (root)
4013   ;; This function find the total score of the thread below ROOT.
4014   (setq root (car root))
4015   (apply gnus-thread-score-function
4016          (or (append
4017               (mapcar 'gnus-thread-total-score
4018                       (cdr (gnus-id-to-thread (mail-header-id root))))
4019               (when (> (mail-header-number root) 0)
4020                 (list (or (cdr (assq (mail-header-number root)
4021                                      gnus-newsgroup-scored))
4022                           gnus-summary-default-score 0))))
4023              (list gnus-summary-default-score)
4024              '(0))))
4025
4026 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4027 (defvar gnus-tmp-prev-subject nil)
4028 (defvar gnus-tmp-false-parent nil)
4029 (defvar gnus-tmp-root-expunged nil)
4030 (defvar gnus-tmp-dummy-line nil)
4031
4032 (eval-when-compile (defvar gnus-tmp-header))
4033 (defun gnus-extra-header (type &optional header)
4034   "Return the extra header of TYPE."
4035   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4036       ""))
4037
4038 (defun gnus-summary-prepare-threads (threads)
4039   "Prepare summary buffer from THREADS and indentation LEVEL.
4040 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4041 or a straight list of headers."
4042   (gnus-message 7 "Generating summary...")
4043
4044   (setq gnus-newsgroup-threads threads)
4045   (beginning-of-line)
4046
4047   (let ((gnus-tmp-level 0)
4048         (default-score (or gnus-summary-default-score 0))
4049         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4050         thread number subject stack state gnus-tmp-gathered beg-match
4051         new-roots gnus-tmp-new-adopts thread-end
4052         gnus-tmp-header gnus-tmp-unread
4053         gnus-tmp-replied gnus-tmp-subject-or-nil
4054         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4055         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4056         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
4057
4058     (setq gnus-tmp-prev-subject nil)
4059
4060     (if (vectorp (car threads))
4061         ;; If this is a straight (sic) list of headers, then a
4062         ;; threaded summary display isn't required, so we just create
4063         ;; an unthreaded one.
4064         (gnus-summary-prepare-unthreaded threads)
4065
4066       ;; Do the threaded display.
4067
4068       (while (or threads stack gnus-tmp-new-adopts new-roots)
4069
4070         (if (and (= gnus-tmp-level 0)
4071                  (or (not stack)
4072                      (= (caar stack) 0))
4073                  (not gnus-tmp-false-parent)
4074                  (or gnus-tmp-new-adopts new-roots))
4075             (if gnus-tmp-new-adopts
4076                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4077                       thread (list (car gnus-tmp-new-adopts))
4078                       gnus-tmp-header (caar thread)
4079                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4080               (when new-roots
4081                 (setq thread (list (car new-roots))
4082                       gnus-tmp-header (caar thread)
4083                       new-roots (cdr new-roots))))
4084
4085           (if threads
4086               ;; If there are some threads, we do them before the
4087               ;; threads on the stack.
4088               (setq thread threads
4089                     gnus-tmp-header (caar thread))
4090             ;; There were no current threads, so we pop something off
4091             ;; the stack.
4092             (setq state (car stack)
4093                   gnus-tmp-level (car state)
4094                   thread (cdr state)
4095                   stack (cdr stack)
4096                   gnus-tmp-header (caar thread))))
4097
4098         (setq gnus-tmp-false-parent nil)
4099         (setq gnus-tmp-root-expunged nil)
4100         (setq thread-end nil)
4101
4102         (if (stringp gnus-tmp-header)
4103             ;; The header is a dummy root.
4104             (cond
4105              ((eq gnus-summary-make-false-root 'adopt)
4106               ;; We let the first article adopt the rest.
4107               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4108                                                (cddar thread)))
4109               (setq gnus-tmp-gathered
4110                     (nconc (mapcar
4111                             (lambda (h) (mail-header-number (car h)))
4112                             (cddar thread))
4113                            gnus-tmp-gathered))
4114               (setq thread (cons (list (caar thread)
4115                                        (cadar thread))
4116                                  (cdr thread)))
4117               (setq gnus-tmp-level -1
4118                     gnus-tmp-false-parent t))
4119              ((eq gnus-summary-make-false-root 'empty)
4120               ;; We print adopted articles with empty subject fields.
4121               (setq gnus-tmp-gathered
4122                     (nconc (mapcar
4123                             (lambda (h) (mail-header-number (car h)))
4124                             (cddar thread))
4125                            gnus-tmp-gathered))
4126               (setq gnus-tmp-level -1))
4127              ((eq gnus-summary-make-false-root 'dummy)
4128               ;; We remember that we probably want to output a dummy
4129               ;; root.
4130               (setq gnus-tmp-dummy-line gnus-tmp-header)
4131               (setq gnus-tmp-prev-subject gnus-tmp-header))
4132              (t
4133               ;; We do not make a root for the gathered
4134               ;; sub-threads at all.
4135               (setq gnus-tmp-level -1)))
4136
4137           (setq number (mail-header-number gnus-tmp-header)
4138                 subject (mail-header-subject gnus-tmp-header))
4139
4140           (cond
4141            ;; If the thread has changed subject, we might want to make
4142            ;; this subthread into a root.
4143            ((and (null gnus-thread-ignore-subject)
4144                  (not (zerop gnus-tmp-level))
4145                  gnus-tmp-prev-subject
4146                  (not (inline
4147                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4148             (setq new-roots (nconc new-roots (list (car thread)))
4149                   thread-end t
4150                   gnus-tmp-header nil))
4151            ;; If the article lies outside the current limit,
4152            ;; then we do not display it.
4153            ((not (memq number gnus-newsgroup-limit))
4154             (setq gnus-tmp-gathered
4155                   (nconc (mapcar
4156                           (lambda (h) (mail-header-number (car h)))
4157                           (cdar thread))
4158                          gnus-tmp-gathered))
4159             (setq gnus-tmp-new-adopts (if (cdar thread)
4160                                           (append gnus-tmp-new-adopts
4161                                                   (cdar thread))
4162                                         gnus-tmp-new-adopts)
4163                   thread-end t
4164                   gnus-tmp-header nil)
4165             (when (zerop gnus-tmp-level)
4166               (setq gnus-tmp-root-expunged t)))
4167            ;; Perhaps this article is to be marked as read?
4168            ((and gnus-summary-mark-below
4169                  (< (or (cdr (assq number gnus-newsgroup-scored))
4170                         default-score)
4171                     gnus-summary-mark-below)
4172                  ;; Don't touch sparse articles.
4173                  (not (gnus-summary-article-sparse-p number))
4174                  (not (gnus-summary-article-ancient-p number)))
4175             (setq gnus-newsgroup-unreads
4176                   (delq number gnus-newsgroup-unreads))
4177             (if gnus-newsgroup-auto-expire
4178                 (push number gnus-newsgroup-expirable)
4179               (push (cons number gnus-low-score-mark)
4180                     gnus-newsgroup-reads))))
4181
4182           (when gnus-tmp-header
4183             ;; We may have an old dummy line to output before this
4184             ;; article.
4185             (when (and gnus-tmp-dummy-line
4186                        (gnus-subject-equal
4187                         gnus-tmp-dummy-line
4188                         (mail-header-subject gnus-tmp-header)))
4189               (gnus-summary-insert-dummy-line
4190                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4191               (setq gnus-tmp-dummy-line nil))
4192
4193             ;; Compute the mark.
4194             (setq gnus-tmp-unread (gnus-article-mark number))
4195
4196             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4197                                   gnus-tmp-header gnus-tmp-level)
4198                   gnus-newsgroup-data)
4199
4200             ;; Actually insert the line.
4201             (setq
4202              gnus-tmp-subject-or-nil
4203              (cond
4204               ((and gnus-thread-ignore-subject
4205                     gnus-tmp-prev-subject
4206                     (not (inline (gnus-subject-equal
4207                                   gnus-tmp-prev-subject subject))))
4208                subject)
4209               ((zerop gnus-tmp-level)
4210                (if (and (eq gnus-summary-make-false-root 'empty)
4211                         (memq number gnus-tmp-gathered)
4212                         gnus-tmp-prev-subject
4213                         (inline (gnus-subject-equal
4214                                  gnus-tmp-prev-subject subject)))
4215                    gnus-summary-same-subject
4216                  subject))
4217               (t gnus-summary-same-subject)))
4218             (if (and (eq gnus-summary-make-false-root 'adopt)
4219                      (= gnus-tmp-level 1)
4220                      (memq number gnus-tmp-gathered))
4221                 (setq gnus-tmp-opening-bracket ?\<
4222                       gnus-tmp-closing-bracket ?\>)
4223               (setq gnus-tmp-opening-bracket ?\[
4224                     gnus-tmp-closing-bracket ?\]))
4225             (setq
4226              gnus-tmp-indentation
4227              (aref gnus-thread-indent-array gnus-tmp-level)
4228              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4229              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4230                                 gnus-summary-default-score 0)
4231              gnus-tmp-score-char
4232              (if (or (null gnus-summary-default-score)
4233                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4234                          gnus-summary-zcore-fuzz))
4235                  ?  ;Whitespace
4236                (if (< gnus-tmp-score gnus-summary-default-score)
4237                    gnus-score-below-mark gnus-score-over-mark))
4238              gnus-tmp-replied
4239              (cond ((memq number gnus-newsgroup-processable)
4240                     gnus-process-mark)
4241                    ((memq number gnus-newsgroup-cached)
4242                     gnus-cached-mark)
4243                    ((memq number gnus-newsgroup-replied)
4244                     gnus-replied-mark)
4245                    ((memq number gnus-newsgroup-saved)
4246                     gnus-saved-mark)
4247                    (t gnus-no-mark))
4248              gnus-tmp-from (mail-header-from gnus-tmp-header)
4249              gnus-tmp-name
4250              (cond
4251               ((string-match "<[^>]+> *$" gnus-tmp-from)
4252                (setq beg-match (match-beginning 0))
4253                (or (and (string-match "^\".+\"" gnus-tmp-from)
4254                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4255                    (substring gnus-tmp-from 0 beg-match)))
4256               ((string-match "(.+)" gnus-tmp-from)
4257                (substring gnus-tmp-from
4258                           (1+ (match-beginning 0)) (1- (match-end 0))))
4259               (t gnus-tmp-from)))
4260             (when (string= gnus-tmp-name "")
4261               (setq gnus-tmp-name gnus-tmp-from))
4262             (unless (numberp gnus-tmp-lines)
4263               (setq gnus-tmp-lines 0))
4264             (gnus-put-text-property
4265              (point)
4266              (progn (eval gnus-summary-line-format-spec) (point))
4267              'gnus-number number)
4268             (when gnus-visual-p
4269               (forward-line -1)
4270               (gnus-run-hooks 'gnus-summary-update-hook)
4271               (forward-line 1))
4272
4273             (setq gnus-tmp-prev-subject subject)))
4274
4275         (when (nth 1 thread)
4276           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4277         (incf gnus-tmp-level)
4278         (setq threads (if thread-end nil (cdar thread)))
4279         (unless threads
4280           (setq gnus-tmp-level 0)))))
4281   (gnus-message 7 "Generating summary...done"))
4282
4283 (defun gnus-summary-prepare-unthreaded (headers)
4284   "Generate an unthreaded summary buffer based on HEADERS."
4285   (let (header number mark)
4286
4287     (beginning-of-line)
4288
4289     (while headers
4290       ;; We may have to root out some bad articles...
4291       (when (memq (setq number (mail-header-number
4292                                 (setq header (pop headers))))
4293                   gnus-newsgroup-limit)
4294         ;; Mark article as read when it has a low score.
4295         (when (and gnus-summary-mark-below
4296                    (< (or (cdr (assq number gnus-newsgroup-scored))
4297                           gnus-summary-default-score 0)
4298                       gnus-summary-mark-below)
4299                    (not (gnus-summary-article-ancient-p number)))
4300           (setq gnus-newsgroup-unreads
4301                 (delq number gnus-newsgroup-unreads))
4302           (if gnus-newsgroup-auto-expire
4303               (push number gnus-newsgroup-expirable)
4304             (push (cons number gnus-low-score-mark)
4305                   gnus-newsgroup-reads)))
4306
4307         (setq mark (gnus-article-mark number))
4308         (push (gnus-data-make number mark (1+ (point)) header 0)
4309               gnus-newsgroup-data)
4310         (gnus-summary-insert-line
4311          header 0 number
4312          mark (memq number gnus-newsgroup-replied)
4313          (memq number gnus-newsgroup-expirable)
4314          (mail-header-subject header) nil
4315          (cdr (assq number gnus-newsgroup-scored))
4316          (memq number gnus-newsgroup-processable))))))
4317
4318 (defun gnus-summary-remove-list-identifiers ()
4319   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4320   (let ((regexp (if (consp gnus-list-identifiers)
4321                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4322                   gnus-list-identifiers))
4323         changed subject)
4324     (when regexp
4325       (dolist (header gnus-newsgroup-headers)
4326         (setq subject (mail-header-subject header)
4327               changed nil)
4328         (while (string-match
4329                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4330                 subject)
4331           (setq subject
4332                 (concat (substring subject 0 (match-beginning 2))
4333                         (substring subject (match-end 0)))
4334                 changed t))
4335         (when (and changed
4336                    (string-match
4337                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4338           (setq subject
4339                 (concat (substring subject 0 (match-beginning 1))
4340                         (substring subject (match-end 1)))))
4341         (when changed
4342           (mail-header-set-subject header subject))))))
4343
4344 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4345   "Select newsgroup GROUP.
4346 If READ-ALL is non-nil, all articles in the group are selected.
4347 If SELECT-ARTICLES, only select those articles from GROUP."
4348   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4349          ;;!!! Dirty hack; should be removed.
4350          (gnus-summary-ignore-duplicates
4351           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4352               t
4353             gnus-summary-ignore-duplicates))
4354          (info (nth 2 entry))
4355          articles fetched-articles cached)
4356
4357     (unless (gnus-check-server
4358              (setq gnus-current-select-method
4359                    (gnus-find-method-for-group group)))
4360       (error "Couldn't open server"))
4361
4362     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4363         (gnus-activate-group group)     ; Or we can activate it...
4364         (progn                          ; Or we bug out.
4365           (when (equal major-mode 'gnus-summary-mode)
4366             (kill-buffer (current-buffer)))
4367           (error "Couldn't activate group %s: %s"
4368                  group (gnus-status-message group))))
4369
4370     (unless (gnus-request-group group t)
4371       (when (equal major-mode 'gnus-summary-mode)
4372         (kill-buffer (current-buffer)))
4373       (error "Couldn't request group %s: %s"
4374              group (gnus-status-message group)))
4375
4376     (setq gnus-newsgroup-name group)
4377     (setq gnus-newsgroup-unselected nil)
4378     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4379     (gnus-summary-setup-default-charset)
4380
4381     ;; Adjust and set lists of article marks.
4382     (when info
4383       (gnus-adjust-marked-articles info))
4384
4385     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4386     (setq cached
4387           (if (gnus-virtual-group-p group)
4388               gnus-newsgroup-cached
4389             (gnus-cache-articles-in-group group)))
4390
4391     (setq gnus-newsgroup-unreads
4392           (gnus-set-difference
4393            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4394            gnus-newsgroup-dormant))
4395
4396     (setq gnus-newsgroup-processable nil)
4397
4398     (gnus-update-read-articles group gnus-newsgroup-unreads)
4399
4400     (if (setq articles select-articles)
4401         (setq gnus-newsgroup-unselected
4402               (gnus-sorted-intersection
4403                gnus-newsgroup-unreads
4404                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4405       (setq articles (gnus-articles-to-read group read-all)))
4406
4407     (cond
4408      ((null articles)
4409       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4410       'quit)
4411      ((eq articles 0) nil)
4412      (t
4413       ;; Init the dependencies hash table.
4414       (setq gnus-newsgroup-dependencies
4415             (gnus-make-hashtable (length articles)))
4416       (gnus-set-global-variables)
4417       ;; Retrieve the headers and read them in.
4418       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4419       (setq gnus-newsgroup-headers
4420             (gnus-retrieve-parsed-headers
4421              articles gnus-newsgroup-name
4422              ;; We might want to fetch old headers, but
4423              ;; not if there is only 1 article.
4424              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4425                            (not (numberp gnus-fetch-old-headers)))
4426                       (> (length articles) 1))
4427                   gnus-fetch-old-headers)))
4428       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4429
4430       ;; Suppress duplicates?
4431       (when gnus-suppress-duplicates
4432         (gnus-dup-suppress-articles))
4433
4434       ;; Set the initial limit.
4435       (setq gnus-newsgroup-limit (copy-sequence articles))
4436       ;; Remove canceled articles from the list of unread articles.
4437       (setq gnus-newsgroup-unreads
4438             (gnus-set-sorted-intersection
4439              gnus-newsgroup-unreads
4440              (setq fetched-articles
4441                    (mapcar (lambda (headers) (mail-header-number headers))
4442                            gnus-newsgroup-headers))))
4443       ;; Removed marked articles that do not exist.
4444       (gnus-update-missing-marks
4445        (gnus-sorted-complement fetched-articles articles))
4446
4447       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4448       (when cached
4449         (setq gnus-newsgroup-cached cached))
4450
4451       ;; We might want to build some more threads first.
4452       (when (and gnus-fetch-old-headers
4453                  (eq gnus-headers-retrieved-by 'nov))
4454         (if (eq gnus-fetch-old-headers 'invisible)
4455             (gnus-build-all-threads)
4456           (gnus-build-old-threads)))
4457       ;; Let the Gnus agent mark articles as read.
4458       (when gnus-agent
4459         (gnus-agent-get-undownloaded-list))
4460       ;; Remove list identifiers from subject
4461       (when gnus-list-identifiers
4462         (gnus-summary-remove-list-identifiers))
4463       ;; Check whether auto-expire is to be done in this group.
4464       (setq gnus-newsgroup-auto-expire
4465             (gnus-group-auto-expirable-p group))
4466       ;; Set up the article buffer now, if necessary.
4467       (unless gnus-single-article-buffer
4468         (gnus-article-setup-buffer))
4469       ;; First and last article in this newsgroup.
4470       (when gnus-newsgroup-headers
4471         (setq gnus-newsgroup-begin
4472               (mail-header-number (car gnus-newsgroup-headers))
4473               gnus-newsgroup-end
4474               (mail-header-number
4475                (gnus-last-element gnus-newsgroup-headers))))
4476       ;; GROUP is successfully selected.
4477       (or gnus-newsgroup-headers t)))))
4478
4479 (defun gnus-articles-to-read (group &optional read-all)
4480   "Find out what articles the user wants to read."
4481   (let* ((articles
4482           ;; Select all articles if `read-all' is non-nil, or if there
4483           ;; are no unread articles.
4484           (if (or read-all
4485                   (and (zerop (length gnus-newsgroup-marked))
4486                        (zerop (length gnus-newsgroup-unreads)))
4487                   (eq (gnus-group-find-parameter group 'display)
4488                       'all))
4489               (or
4490                (gnus-uncompress-range (gnus-active group))
4491                (gnus-cache-articles-in-group group))
4492             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4493                           (copy-sequence gnus-newsgroup-unreads))
4494                   '<)))
4495          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4496          (scored (length scored-list))
4497          (number (length articles))
4498          (marked (+ (length gnus-newsgroup-marked)
4499                     (length gnus-newsgroup-dormant)))
4500          (select
4501           (cond
4502            ((numberp read-all)
4503             read-all)
4504            (t
4505             (condition-case ()
4506                 (cond
4507                  ((and (or (<= scored marked) (= scored number))
4508                        (natnump gnus-large-newsgroup)
4509                        (> number gnus-large-newsgroup))
4510                   (let* ((cursor-in-echo-area nil)
4511                          (input (read-from-minibuffer
4512                                  (format
4513                                   "How many articles from %s (max %d): "
4514                                   (gnus-limit-string gnus-newsgroup-name 35)
4515                                   number)
4516                                  (cons (number-to-string gnus-large-newsgroup)
4517                                        0))))
4518                     (if (string-match "^[ \t]*$" input)
4519                         number
4520                       input)))
4521                  ((and (> scored marked) (< scored number)
4522                        (> (- scored number) 20))
4523                   (let ((input
4524                          (read-string
4525                           (format "%s %s (%d scored, %d total): "
4526                                   "How many articles from"
4527                                   group scored number))))
4528                     (if (string-match "^[ \t]*$" input)
4529                         number input)))
4530                  (t number))
4531               (quit
4532                (message "Quit getting the articles to read")
4533                nil))))))
4534     (setq select (if (stringp select) (string-to-number select) select))
4535     (if (or (null select) (zerop select))
4536         select
4537       (if (and (not (zerop scored)) (<= (abs select) scored))
4538           (progn
4539             (setq articles (sort scored-list '<))
4540             (setq number (length articles)))
4541         (setq articles (copy-sequence articles)))
4542
4543       (when (< (abs select) number)
4544         (if (< select 0)
4545             ;; Select the N oldest articles.
4546             (setcdr (nthcdr (1- (abs select)) articles) nil)
4547           ;; Select the N most recent articles.
4548           (setq articles (nthcdr (- number select) articles))))
4549       (setq gnus-newsgroup-unselected
4550             (gnus-sorted-intersection
4551              gnus-newsgroup-unreads
4552              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4553       (when gnus-alter-articles-to-read-function
4554         (setq gnus-newsgroup-unreads
4555               (sort
4556                (funcall gnus-alter-articles-to-read-function
4557                         gnus-newsgroup-name gnus-newsgroup-unreads)
4558                '<)))
4559       articles)))
4560
4561 (defun gnus-killed-articles (killed articles)
4562   (let (out)
4563     (while articles
4564       (when (inline (gnus-member-of-range (car articles) killed))
4565         (push (car articles) out))
4566       (setq articles (cdr articles)))
4567     out))
4568
4569 (defun gnus-uncompress-marks (marks)
4570   "Uncompress the mark ranges in MARKS."
4571   (let ((uncompressed '(score bookmark))
4572         out)
4573     (while marks
4574       (if (memq (caar marks) uncompressed)
4575           (push (car marks) out)
4576         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4577       (setq marks (cdr marks)))
4578     out))
4579
4580 (defun gnus-adjust-marked-articles (info)
4581   "Set all article lists and remove all marks that are no longer valid."
4582   (let* ((marked-lists (gnus-info-marks info))
4583          (active (gnus-active (gnus-info-group info)))
4584          (min (car active))
4585          (max (cdr active))
4586          (types gnus-article-mark-lists)
4587          (uncompressed '(score bookmark killed))
4588          marks var articles article mark)
4589
4590     (while marked-lists
4591       (setq marks (pop marked-lists))
4592       (set (setq var (intern (format "gnus-newsgroup-%s"
4593                                      (car (rassq (setq mark (car marks))
4594                                                  types)))))
4595            (if (memq (car marks) uncompressed) (cdr marks)
4596              (gnus-uncompress-range (cdr marks))))
4597
4598       (setq articles (symbol-value var))
4599
4600       ;; All articles have to be subsets of the active articles.
4601       (cond
4602        ;; Adjust "simple" lists.
4603        ((memq mark '(tick dormant expire reply save))
4604         (while articles
4605           (when (or (< (setq article (pop articles)) min) (> article max))
4606             (set var (delq article (symbol-value var))))))
4607        ;; Adjust assocs.
4608        ((memq mark uncompressed)
4609         (when (not (listp (cdr (symbol-value var))))
4610           (set var (list (symbol-value var))))
4611         (when (not (listp (cdr articles)))
4612           (setq articles (list articles)))
4613         (while articles
4614           (when (or (not (consp (setq article (pop articles))))
4615                     (< (car article) min)
4616                     (> (car article) max))
4617             (set var (delq article (symbol-value var))))))))))
4618
4619 (defun gnus-update-missing-marks (missing)
4620   "Go through the list of MISSING articles and remove them from the mark lists."
4621   (when missing
4622     (let ((types gnus-article-mark-lists)
4623           var m)
4624       ;; Go through all types.
4625       (while types
4626         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4627         (when (symbol-value var)
4628           ;; This list has articles.  So we delete all missing articles
4629           ;; from it.
4630           (setq m missing)
4631           (while m
4632             (set var (delq (pop m) (symbol-value var)))))))))
4633
4634 (defun gnus-update-marks ()
4635   "Enter the various lists of marked articles into the newsgroup info list."
4636   (let ((types gnus-article-mark-lists)
4637         (info (gnus-get-info gnus-newsgroup-name))
4638         (uncompressed '(score bookmark killed))
4639         type list newmarked symbol delta-marks)
4640     (when info
4641       ;; Add all marks lists to the list of marks lists.
4642       (while (setq type (pop types))
4643         (setq list (symbol-value
4644                     (setq symbol
4645                           (intern (format "gnus-newsgroup-%s"
4646                                           (car type))))))
4647
4648         (when list
4649           ;; Get rid of the entries of the articles that have the
4650           ;; default score.
4651           (when (and (eq (cdr type) 'score)
4652                      gnus-save-score
4653                      list)
4654             (let* ((arts list)
4655                    (prev (cons nil list))
4656                    (all prev))
4657               (while arts
4658                 (if (or (not (consp (car arts)))
4659                         (= (cdar arts) gnus-summary-default-score))
4660                     (setcdr prev (cdr arts))
4661                   (setq prev arts))
4662                 (setq arts (cdr arts)))
4663               (setq list (cdr all)))))
4664
4665         (unless (memq (cdr type) uncompressed)
4666           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4667
4668         (when (gnus-check-backend-function
4669                'request-set-mark gnus-newsgroup-name)
4670           ;; propagate flags to server, with the following exceptions:
4671           ;; uncompressed:s are not proper flags (they are cons cells)
4672           ;; cache is a internal gnus flag
4673           ;; download are local to one gnus installation (well)
4674           ;; unsend are for nndraft groups only
4675           ;; xxx: generality of this?  this suits nnimap anyway
4676           (unless (memq (cdr type) (append '(cache download unsend)
4677                                            uncompressed))
4678             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4679                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4680                    (add (gnus-remove-from-range
4681                          (gnus-copy-sequence list) old)))
4682               (when add
4683                 (push (list add 'add (list (cdr type))) delta-marks))
4684               (when del
4685                 (push (list del 'del (list (cdr type))) delta-marks)))))
4686
4687         (when list
4688           (push (cons (cdr type) list) newmarked)))
4689
4690       (when delta-marks
4691         (unless (gnus-check-group gnus-newsgroup-name)
4692           (error "Can't open server for %s" gnus-newsgroup-name))
4693         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4694
4695       ;; Enter these new marks into the info of the group.
4696       (if (nthcdr 3 info)
4697           (setcar (nthcdr 3 info) newmarked)
4698         ;; Add the marks lists to the end of the info.
4699         (when newmarked
4700           (setcdr (nthcdr 2 info) (list newmarked))))
4701
4702       ;; Cut off the end of the info if there's nothing else there.
4703       (let ((i 5))
4704         (while (and (> i 2)
4705                     (not (nth i info)))
4706           (when (nthcdr (decf i) info)
4707             (setcdr (nthcdr i info) nil)))))))
4708
4709 (defun gnus-set-mode-line (where)
4710   "Set the mode line of the article or summary buffers.
4711 If WHERE is `summary', the summary mode line format will be used."
4712   ;; Is this mode line one we keep updated?
4713   (when (and (memq where gnus-updated-mode-lines)
4714              (symbol-value
4715               (intern (format "gnus-%s-mode-line-format-spec" where))))
4716     (let (mode-string)
4717       (save-excursion
4718         ;; We evaluate this in the summary buffer since these
4719         ;; variables are buffer-local to that buffer.
4720         (set-buffer gnus-summary-buffer)
4721         ;; We bind all these variables that are used in the `eval' form
4722         ;; below.
4723         (let* ((mformat (symbol-value
4724                          (intern
4725                           (format "gnus-%s-mode-line-format-spec" where))))
4726                (gnus-tmp-group-name (gnus-group-name-decode
4727                                      gnus-newsgroup-name
4728                                      (gnus-group-name-charset
4729                                       nil
4730                                       gnus-newsgroup-name)))
4731                (gnus-tmp-article-number (or gnus-current-article 0))
4732                (gnus-tmp-unread gnus-newsgroup-unreads)
4733                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4734                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4735                (gnus-tmp-unread-and-unselected
4736                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4737                             (zerop gnus-tmp-unselected))
4738                        "")
4739                       ((zerop gnus-tmp-unselected)
4740                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4741                       (t (format "{%d(+%d) more}"
4742                                  gnus-tmp-unread-and-unticked
4743                                  gnus-tmp-unselected))))
4744                (gnus-tmp-subject
4745                 (if (and gnus-current-headers
4746                          (vectorp gnus-current-headers))
4747                     (gnus-mode-string-quote
4748                      (mail-header-subject gnus-current-headers))
4749                   ""))
4750                bufname-length max-len
4751                gnus-tmp-header);; passed as argument to any user-format-funcs
4752           (setq mode-string (eval mformat))
4753           (setq bufname-length (if (string-match "%b" mode-string)
4754                                    (- (length
4755                                        (buffer-name
4756                                         (if (eq where 'summary)
4757                                             nil
4758                                           (get-buffer gnus-article-buffer))))
4759                                       2)
4760                                  0))
4761           (setq max-len (max 4 (if gnus-mode-non-string-length
4762                                    (- (window-width)
4763                                       gnus-mode-non-string-length
4764                                       bufname-length)
4765                                  (length mode-string))))
4766           ;; We might have to chop a bit of the string off...
4767           (when (> (length mode-string) max-len)
4768             (setq mode-string
4769                   (concat (gnus-truncate-string mode-string (- max-len 3))
4770                           "...")))
4771           ;; Pad the mode string a bit.
4772           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4773       ;; Update the mode line.
4774       (setq mode-line-buffer-identification
4775             (gnus-mode-line-buffer-identification (list mode-string)))
4776       (set-buffer-modified-p t))))
4777
4778 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4779   "Go through the HEADERS list and add all Xrefs to a hash table.
4780 The resulting hash table is returned, or nil if no Xrefs were found."
4781   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4782          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4783          (xref-hashtb (gnus-make-hashtable))
4784          start group entry number xrefs header)
4785     (while headers
4786       (setq header (pop headers))
4787       (when (and (setq xrefs (mail-header-xref header))
4788                  (not (memq (setq number (mail-header-number header))
4789                             unreads)))
4790         (setq start 0)
4791         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4792           (setq start (match-end 0))
4793           (setq group (if prefix
4794                           (concat prefix (substring xrefs (match-beginning 1)
4795                                                     (match-end 1)))
4796                         (substring xrefs (match-beginning 1) (match-end 1))))
4797           (setq number
4798                 (string-to-int (substring xrefs (match-beginning 2)
4799                                           (match-end 2))))
4800           (if (setq entry (gnus-gethash group xref-hashtb))
4801               (setcdr entry (cons number (cdr entry)))
4802             (gnus-sethash group (cons number nil) xref-hashtb)))))
4803     (and start xref-hashtb)))
4804
4805 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4806   "Look through all the headers and mark the Xrefs as read."
4807   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4808         name entry info xref-hashtb idlist method nth4)
4809     (save-excursion
4810       (set-buffer gnus-group-buffer)
4811       (when (setq xref-hashtb
4812                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4813         (mapatoms
4814          (lambda (group)
4815            (unless (string= from-newsgroup (setq name (symbol-name group)))
4816              (setq idlist (symbol-value group))
4817              ;; Dead groups are not updated.
4818              (and (prog1
4819                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4820                             info (nth 2 entry))
4821                     (when (stringp (setq nth4 (gnus-info-method info)))
4822                       (setq nth4 (gnus-server-to-method nth4))))
4823                   ;; Only do the xrefs if the group has the same
4824                   ;; select method as the group we have just read.
4825                   (or (gnus-methods-equal-p
4826                        nth4 (gnus-find-method-for-group from-newsgroup))
4827                       virtual
4828                       (equal nth4 (setq method (gnus-find-method-for-group
4829                                                 from-newsgroup)))
4830                       (and (equal (car nth4) (car method))
4831                            (equal (nth 1 nth4) (nth 1 method))))
4832                   gnus-use-cross-reference
4833                   (or (not (eq gnus-use-cross-reference t))
4834                       virtual
4835                       ;; Only do cross-references on subscribed
4836                       ;; groups, if that is what is wanted.
4837                       (<= (gnus-info-level info) gnus-level-subscribed))
4838                   (gnus-group-make-articles-read name idlist))))
4839          xref-hashtb)))))
4840
4841 (defun gnus-compute-read-articles (group articles)
4842   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4843          (info (nth 2 entry))
4844          (active (gnus-active group))
4845          ninfo)
4846     (when entry
4847       ;; First peel off all invalid article numbers.
4848       (when active
4849         (let ((ids articles)
4850               id first)
4851           (while (setq id (pop ids))
4852             (when (and first (> id (cdr active)))
4853               ;; We'll end up in this situation in one particular
4854               ;; obscure situation.  If you re-scan a group and get
4855               ;; a new article that is cross-posted to a different
4856               ;; group that has not been re-scanned, you might get
4857               ;; crossposted article that has a higher number than
4858               ;; Gnus believes possible.  So we re-activate this
4859               ;; group as well.  This might mean doing the
4860               ;; crossposting thingy will *increase* the number
4861               ;; of articles in some groups.  Tsk, tsk.
4862               (setq active (or (gnus-activate-group group) active)))
4863             (when (or (> id (cdr active))
4864                       (< id (car active)))
4865               (setq articles (delq id articles))))))
4866       ;; If the read list is nil, we init it.
4867       (if (and active
4868                (null (gnus-info-read info))
4869                (> (car active) 1))
4870           (setq ninfo (cons 1 (1- (car active))))
4871         (setq ninfo (gnus-info-read info)))
4872       ;; Then we add the read articles to the range.
4873       (gnus-add-to-range
4874        ninfo (setq articles (sort articles '<))))))
4875
4876 (defun gnus-group-make-articles-read (group articles)
4877   "Update the info of GROUP to say that ARTICLES are read."
4878   (let* ((num 0)
4879          (entry (gnus-gethash group gnus-newsrc-hashtb))
4880          (info (nth 2 entry))
4881          (active (gnus-active group))
4882          range)
4883     (when entry
4884       (setq range (gnus-compute-read-articles group articles))
4885       (save-excursion
4886         (set-buffer gnus-group-buffer)
4887         (gnus-undo-register
4888           `(progn
4889              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4890              (gnus-info-set-read ',info ',(gnus-info-read info))
4891              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4892              (gnus-group-update-group ,group t))))
4893       ;; Add the read articles to the range.
4894       (gnus-info-set-read info range)
4895       ;; Then we have to re-compute how many unread
4896       ;; articles there are in this group.
4897       (when active
4898         (cond
4899          ((not range)
4900           (setq num (- (1+ (cdr active)) (car active))))
4901          ((not (listp (cdr range)))
4902           (setq num (- (cdr active) (- (1+ (cdr range))
4903                                        (car range)))))
4904          (t
4905           (while range
4906             (if (numberp (car range))
4907                 (setq num (1+ num))
4908               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4909             (setq range (cdr range)))
4910           (setq num (- (cdr active) num))))
4911         ;; Update the number of unread articles.
4912         (setcar entry num)
4913         ;; Update the group buffer.
4914         (gnus-group-update-group group t)))))
4915
4916 (defvar gnus-newsgroup-none-id 0)
4917
4918 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4919   (let ((cur nntp-server-buffer)
4920         (dependencies
4921          (or dependencies
4922              (save-excursion (set-buffer gnus-summary-buffer)
4923                              gnus-newsgroup-dependencies)))
4924         headers id end ref
4925         (mail-parse-charset gnus-newsgroup-charset)
4926         (mail-parse-ignored-charsets
4927          (save-excursion (condition-case nil
4928                              (set-buffer gnus-summary-buffer)
4929                            (error))
4930                          gnus-newsgroup-ignored-charsets)))
4931     (save-excursion
4932       (set-buffer nntp-server-buffer)
4933       ;; Translate all TAB characters into SPACE characters.
4934       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4935       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4936       (gnus-run-hooks 'gnus-parse-headers-hook)
4937       (let ((case-fold-search t)
4938             in-reply-to header p lines chars ctype)
4939         (goto-char (point-min))
4940         ;; Search to the beginning of the next header.  Error messages
4941         ;; do not begin with 2 or 3.
4942         (while (re-search-forward "^[23][0-9]+ " nil t)
4943           (setq id nil
4944                 ref nil)
4945           ;; This implementation of this function, with nine
4946           ;; search-forwards instead of the one re-search-forward and
4947           ;; a case (which basically was the old function) is actually
4948           ;; about twice as fast, even though it looks messier.  You
4949           ;; can't have everything, I guess.  Speed and elegance
4950           ;; doesn't always go hand in hand.
4951           (setq
4952            header
4953            (make-full-mail-header
4954             ;; Number.
4955             (prog1
4956                 (read cur)
4957               (end-of-line)
4958               (setq p (point))
4959               (narrow-to-region (point)
4960                                 (or (and (search-forward "\n.\n" nil t)
4961                                          (- (point) 2))
4962                                     (point))))
4963             ;; Subject.
4964             (progn
4965               (goto-char p)
4966               (if (search-forward "\nsubject: " nil t)
4967                   (nnheader-header-value)
4968                 "(none)"))
4969             ;; From.
4970             (progn
4971               (goto-char p)
4972               (if (or (search-forward "\nfrom: " nil t)
4973                       (search-forward "\nfrom:" nil t))
4974                   (nnheader-header-value)
4975                 "(nobody)"))
4976             ;; Date.
4977             (progn
4978               (goto-char p)
4979               (if (search-forward "\ndate: " nil t)
4980                   (nnheader-header-value)
4981                 ""))
4982             ;; Message-ID.
4983             (progn
4984               (goto-char p)
4985               (setq id (if (re-search-forward
4986                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4987                            ;; We do it this way to make sure the Message-ID
4988                            ;; is (somewhat) syntactically valid.
4989                            (buffer-substring (match-beginning 1)
4990                                              (match-end 1))
4991                          ;; If there was no message-id, we just fake one
4992                          ;; to make subsequent routines simpler.
4993                          (nnheader-generate-fake-message-id))))
4994             ;; References.
4995             (progn
4996               (goto-char p)
4997               (if (search-forward "\nreferences: " nil t)
4998                   (progn
4999                     (setq end (point))
5000                     (prog1
5001                         (nnheader-header-value)
5002                       (setq ref
5003                             (buffer-substring
5004                              (progn
5005                                ;; (end-of-line)
5006                                (search-backward ">" end t)
5007                                (1+ (point)))
5008                              (progn
5009                                (search-backward "<" end t)
5010                                (point))))))
5011                 ;; Get the references from the in-reply-to header if there
5012                 ;; were no references and the in-reply-to header looks
5013                 ;; promising.
5014                 (if (and (search-forward "\nin-reply-to: " nil t)
5015                          (setq in-reply-to (nnheader-header-value))
5016                          (string-match "<[^>]+>" in-reply-to))
5017                     (let (ref2)
5018                       (setq ref (substring in-reply-to (match-beginning 0)
5019                                            (match-end 0)))
5020                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5021                         (setq ref2 (substring in-reply-to (match-beginning 0)
5022                                               (match-end 0)))
5023                         (when (> (length ref2) (length ref))
5024                           (setq ref ref2)))
5025                       ref)
5026                   (setq ref nil))))
5027             ;; Chars.
5028             (progn
5029               (goto-char p)
5030               (if (search-forward "\nchars: " nil t)
5031                   (if (numberp (setq chars (ignore-errors (read cur))))
5032                       chars 0)
5033                 0))
5034             ;; Lines.
5035             (progn
5036               (goto-char p)
5037               (if (search-forward "\nlines: " nil t)
5038                   (if (numberp (setq lines (ignore-errors (read cur))))
5039                       lines 0)
5040                 0))
5041             ;; Xref.
5042             (progn
5043               (goto-char p)
5044               (and (search-forward "\nxref: " nil t)
5045                    (nnheader-header-value)))
5046             ;; Extra.
5047             (when gnus-extra-headers
5048               (let ((extra gnus-extra-headers)
5049                     out)
5050                 (while extra
5051                   (goto-char p)
5052                   (when (search-forward
5053                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
5054                     (push (cons (car extra) (nnheader-header-value)) out))
5055                   (pop extra))
5056                 out))))
5057           (goto-char p)
5058           (if (and (search-forward "\ncontent-type: " nil t)
5059                    (setq ctype (nnheader-header-value)))
5060               (mime-entity-set-content-type-internal
5061                header (mime-parse-Content-Type ctype)))
5062           (when (equal id ref)
5063             (setq ref nil))
5064
5065           (when gnus-alter-header-function
5066             (funcall gnus-alter-header-function header)
5067             (setq id (mail-header-id header)
5068                   ref (gnus-parent-id (mail-header-references header))))
5069
5070           (when (setq header
5071                       (gnus-dependencies-add-header
5072                        header dependencies force-new))
5073             (push header headers))
5074           (goto-char (point-max))
5075           (widen))
5076         (nreverse headers)))))
5077
5078 ;; Goes through the xover lines and returns a list of vectors
5079 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5080                                                   force-new dependencies
5081                                                   group also-fetch-heads)
5082   "Parse the news overview data in the server buffer.
5083 Return a list of headers that match SEQUENCE (see
5084 `nntp-retrieve-headers')."
5085   ;; Get the Xref when the users reads the articles since most/some
5086   ;; NNTP servers do not include Xrefs when using XOVER.
5087   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5088   (let ((mail-parse-charset gnus-newsgroup-charset)
5089         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5090         (cur nntp-server-buffer)
5091         (dependencies (or dependencies gnus-newsgroup-dependencies))
5092         number headers header)
5093     (save-excursion
5094       (set-buffer nntp-server-buffer)
5095       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5096       ;; Allow the user to mangle the headers before parsing them.
5097       (gnus-run-hooks 'gnus-parse-headers-hook)
5098       (goto-char (point-min))
5099       (while (not (eobp))
5100         (condition-case ()
5101             (while (and sequence (not (eobp)))
5102               (setq number (read cur))
5103               (while (and sequence
5104                           (< (car sequence) number))
5105                 (setq sequence (cdr sequence)))
5106               (and sequence
5107                    (eq number (car sequence))
5108                    (progn
5109                      (setq sequence (cdr sequence))
5110                      (setq header (inline
5111                                     (gnus-nov-parse-line
5112                                      number dependencies force-new))))
5113                    (push header headers))
5114               (forward-line 1))
5115           (error
5116            (gnus-error 4 "Strange nov line (%d)"
5117                        (count-lines (point-min) (point)))))
5118         (forward-line 1))
5119       ;; A common bug in inn is that if you have posted an article and
5120       ;; then retrieves the active file, it will answer correctly --
5121       ;; the new article is included.  However, a NOV entry for the
5122       ;; article may not have been generated yet, so this may fail.
5123       ;; We work around this problem by retrieving the last few
5124       ;; headers using HEAD.
5125       (if (or (not also-fetch-heads)
5126               (not sequence))
5127           ;; We (probably) got all the headers.
5128           (nreverse headers)
5129         (let ((gnus-nov-is-evil t))
5130           (nconc
5131            (nreverse headers)
5132            (gnus-retrieve-parsed-headers sequence group)
5133            ))))))
5134
5135 (defun gnus-article-get-xrefs ()
5136   "Fill in the Xref value in `gnus-current-headers', if necessary.
5137 This is meant to be called in `gnus-article-internal-prepare-hook'."
5138   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5139                                  gnus-current-headers)))
5140     (or (not gnus-use-cross-reference)
5141         (not headers)
5142         (and (mail-header-xref headers)
5143              (not (string= (mail-header-xref headers) "")))
5144         (let ((case-fold-search t)
5145               xref)
5146           (save-restriction
5147             (nnheader-narrow-to-headers)
5148             (goto-char (point-min))
5149             (when (or (and (not (eobp))
5150                            (eq (downcase (char-after)) ?x)
5151                            (looking-at "Xref:"))
5152                       (search-forward "\nXref:" nil t))
5153               (goto-char (1+ (match-end 0)))
5154               (setq xref (buffer-substring (point)
5155                                            (progn (end-of-line) (point))))
5156               (mail-header-set-xref headers xref)))))))
5157
5158 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5159   "Find article ID and insert the summary line for that article.
5160 OLD-HEADER can either be a header or a line number to insert
5161 the subject line on."
5162   (let* ((line (and (numberp old-header) old-header))
5163          (old-header (and (vectorp old-header) old-header))
5164          (header (cond ((and old-header use-old-header)
5165                         old-header)
5166                        ((and (numberp id)
5167                              (gnus-number-to-header id))
5168                         (gnus-number-to-header id))
5169                        (t
5170                         (gnus-read-header id))))
5171          (number (and (numberp id) id))
5172          d)
5173     (when header
5174       ;; Rebuild the thread that this article is part of and go to the
5175       ;; article we have fetched.
5176       (when (and (not gnus-show-threads)
5177                  old-header)
5178         (when (and number
5179                    (setq d (gnus-data-find (mail-header-number old-header))))
5180           (goto-char (gnus-data-pos d))
5181           (gnus-data-remove
5182            number
5183            (- (gnus-point-at-bol)
5184               (prog1
5185                   (1+ (gnus-point-at-eol))
5186                 (gnus-delete-line))))))
5187       (when old-header
5188         (mail-header-set-number header (mail-header-number old-header)))
5189       (setq gnus-newsgroup-sparse
5190             (delq (setq number (mail-header-number header))
5191                   gnus-newsgroup-sparse))
5192       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5193       (push number gnus-newsgroup-limit)
5194       (gnus-rebuild-thread (mail-header-id header) line)
5195       (gnus-summary-goto-subject number nil t))
5196     (when (and (numberp number)
5197                (> number 0))
5198       ;; We have to update the boundaries even if we can't fetch the
5199       ;; article if ID is a number -- so that the next `P' or `N'
5200       ;; command will fetch the previous (or next) article even
5201       ;; if the one we tried to fetch this time has been canceled.
5202       (when (> number gnus-newsgroup-end)
5203         (setq gnus-newsgroup-end number))
5204       (when (< number gnus-newsgroup-begin)
5205         (setq gnus-newsgroup-begin number))
5206       (setq gnus-newsgroup-unselected
5207             (delq number gnus-newsgroup-unselected)))
5208     ;; Report back a success?
5209     (and header (mail-header-number header))))
5210
5211 ;;; Process/prefix in the summary buffer
5212
5213 (defun gnus-summary-work-articles (n)
5214   "Return a list of articles to be worked upon.
5215 The prefix argument, the list of process marked articles, and the
5216 current article will be taken into consideration."
5217   (save-excursion
5218     (set-buffer gnus-summary-buffer)
5219     (cond
5220      (n
5221       ;; A numerical prefix has been given.
5222       (setq n (prefix-numeric-value n))
5223       (let ((backward (< n 0))
5224             (n (abs (prefix-numeric-value n)))
5225             articles article)
5226         (save-excursion
5227           (while
5228               (and (> n 0)
5229                    (push (setq article (gnus-summary-article-number))
5230                          articles)
5231                    (if backward
5232                        (gnus-summary-find-prev nil article)
5233                      (gnus-summary-find-next nil article)))
5234             (decf n)))
5235         (nreverse articles)))
5236      ((and (gnus-region-active-p) (mark))
5237       (message "region active")
5238       ;; Work on the region between point and mark.
5239       (let ((max (max (point) (mark)))
5240             articles article)
5241         (save-excursion
5242           (goto-char (min (point) (mark)))
5243           (while
5244               (and
5245                (push (setq article (gnus-summary-article-number)) articles)
5246                (gnus-summary-find-next nil article)
5247                (< (point) max)))
5248           (nreverse articles))))
5249      (gnus-newsgroup-processable
5250       ;; There are process-marked articles present.
5251       ;; Save current state.
5252       (gnus-summary-save-process-mark)
5253       ;; Return the list.
5254       (reverse gnus-newsgroup-processable))
5255      (t
5256       ;; Just return the current article.
5257       (list (gnus-summary-article-number))))))
5258
5259 (defmacro gnus-summary-iterate (arg &rest forms)
5260   "Iterate over the process/prefixed articles and do FORMS.
5261 ARG is the interactive prefix given to the command.  FORMS will be
5262 executed with point over the summary line of the articles."
5263   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5264     `(let ((,articles (gnus-summary-work-articles ,arg)))
5265        (while ,articles
5266          (gnus-summary-goto-subject (car ,articles))
5267          ,@forms
5268          (pop ,articles)))))
5269
5270 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5271 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5272
5273 (defun gnus-summary-save-process-mark ()
5274   "Push the current set of process marked articles on the stack."
5275   (interactive)
5276   (push (copy-sequence gnus-newsgroup-processable)
5277         gnus-newsgroup-process-stack))
5278
5279 (defun gnus-summary-kill-process-mark ()
5280   "Push the current set of process marked articles on the stack and unmark."
5281   (interactive)
5282   (gnus-summary-save-process-mark)
5283   (gnus-summary-unmark-all-processable))
5284
5285 (defun gnus-summary-yank-process-mark ()
5286   "Pop the last process mark state off the stack and restore it."
5287   (interactive)
5288   (unless gnus-newsgroup-process-stack
5289     (error "Empty mark stack"))
5290   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5291
5292 (defun gnus-summary-process-mark-set (set)
5293   "Make SET into the current process marked articles."
5294   (gnus-summary-unmark-all-processable)
5295   (while set
5296     (gnus-summary-set-process-mark (pop set))))
5297
5298 ;;; Searching and stuff
5299
5300 (defun gnus-summary-search-group (&optional backward use-level)
5301   "Search for next unread newsgroup.
5302 If optional argument BACKWARD is non-nil, search backward instead."
5303   (save-excursion
5304     (set-buffer gnus-group-buffer)
5305     (when (gnus-group-search-forward
5306            backward nil (if use-level (gnus-group-group-level) nil))
5307       (gnus-group-group-name))))
5308
5309 (defun gnus-summary-best-group (&optional exclude-group)
5310   "Find the name of the best unread group.
5311 If EXCLUDE-GROUP, do not go to this group."
5312   (save-excursion
5313     (set-buffer gnus-group-buffer)
5314     (save-excursion
5315       (gnus-group-best-unread-group exclude-group))))
5316
5317 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5318   (if backward (gnus-summary-find-prev)
5319     (let* ((dummy (gnus-summary-article-intangible-p))
5320            (article (or article (gnus-summary-article-number)))
5321            (arts (gnus-data-find-list article))
5322            result)
5323       (when (and (not dummy)
5324                  (or (not gnus-summary-check-current)
5325                      (not unread)
5326                      (not (gnus-data-unread-p (car arts)))))
5327         (setq arts (cdr arts)))
5328       (when (setq result
5329                   (if unread
5330                       (progn
5331                         (while arts
5332                           (when (or (and undownloaded
5333                                          (eq gnus-undownloaded-mark
5334                                              (gnus-data-mark (car arts))))
5335                                     (gnus-data-unread-p (car arts)))
5336                             (setq result (car arts)
5337                                   arts nil))
5338                           (setq arts (cdr arts)))
5339                         result)
5340                     (car arts)))
5341         (goto-char (gnus-data-pos result))
5342         (gnus-data-number result)))))
5343
5344 (defun gnus-summary-find-prev (&optional unread article)
5345   (let* ((eobp (eobp))
5346          (article (or article (gnus-summary-article-number)))
5347          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5348          result)
5349     (when (and (not eobp)
5350                (or (not gnus-summary-check-current)
5351                    (not unread)
5352                    (not (gnus-data-unread-p (car arts)))))
5353       (setq arts (cdr arts)))
5354     (when (setq result
5355                 (if unread
5356                     (progn
5357                       (while arts
5358                         (when (gnus-data-unread-p (car arts))
5359                           (setq result (car arts)
5360                                 arts nil))
5361                         (setq arts (cdr arts)))
5362                       result)
5363                   (car arts)))
5364       (goto-char (gnus-data-pos result))
5365       (gnus-data-number result))))
5366
5367 (defun gnus-summary-find-subject (subject &optional unread backward article)
5368   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5369          (article (or article (gnus-summary-article-number)))
5370          (articles (gnus-data-list backward))
5371          (arts (gnus-data-find-list article articles))
5372          result)
5373     (when (or (not gnus-summary-check-current)
5374               (not unread)
5375               (not (gnus-data-unread-p (car arts))))
5376       (setq arts (cdr arts)))
5377     (while arts
5378       (and (or (not unread)
5379                (gnus-data-unread-p (car arts)))
5380            (vectorp (gnus-data-header (car arts)))
5381            (gnus-subject-equal
5382             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5383            (setq result (car arts)
5384                  arts nil))
5385       (setq arts (cdr arts)))
5386     (and result
5387          (goto-char (gnus-data-pos result))
5388          (gnus-data-number result))))
5389
5390 (defun gnus-summary-search-forward (&optional unread subject backward)
5391   "Search forward for an article.
5392 If UNREAD, look for unread articles.  If SUBJECT, look for
5393 articles with that subject.  If BACKWARD, search backward instead."
5394   (cond (subject (gnus-summary-find-subject subject unread backward))
5395         (backward (gnus-summary-find-prev unread))
5396         (t (gnus-summary-find-next unread))))
5397
5398 (defun gnus-recenter (&optional n)
5399   "Center point in window and redisplay frame.
5400 Also do horizontal recentering."
5401   (interactive "P")
5402   (when (and gnus-auto-center-summary
5403              (not (eq gnus-auto-center-summary 'vertical)))
5404     (gnus-horizontal-recenter))
5405   (recenter n))
5406
5407 (defun gnus-summary-recenter ()
5408   "Center point in the summary window.
5409 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5410 displayed, no centering will be performed."
5411   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5412   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5413   (interactive)
5414   (let* ((top (cond ((< (window-height) 4) 0)
5415                     ((< (window-height) 7) 1)
5416                     (t (if (numberp gnus-auto-center-summary)
5417                            gnus-auto-center-summary
5418                          2))))
5419          (height (1- (window-height)))
5420          (bottom (save-excursion (goto-char (point-max))
5421                                  (forward-line (- height))
5422                                  (point)))
5423          (window (get-buffer-window (current-buffer))))
5424     ;; The user has to want it.
5425     (when gnus-auto-center-summary
5426       (when (get-buffer-window gnus-article-buffer)
5427         ;; Only do recentering when the article buffer is displayed,
5428         ;; Set the window start to either `bottom', which is the biggest
5429         ;; possible valid number, or the second line from the top,
5430         ;; whichever is the least.
5431         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5432           (if (> bottom top-pos)
5433               ;; Keep the second line from the top visible
5434               (set-window-start window top-pos t)
5435             ;; Try to keep the bottom line visible; if it's partially
5436             ;; obscured, either scroll one more line to make it fully
5437             ;; visible, or revert to using TOP-POS.
5438             (save-excursion
5439               (goto-char (point-max))
5440               (forward-line -1)
5441               (let ((last-line-start (point)))
5442                 (goto-char bottom)
5443                 (set-window-start window (point) t)
5444                 (when (not (pos-visible-in-window-p last-line-start window))
5445                   (forward-line 1)
5446                   (set-window-start window (min (point) top-pos) t)))))))
5447       ;; Do horizontal recentering while we're at it.
5448       (when (and (get-buffer-window (current-buffer) t)
5449                  (not (eq gnus-auto-center-summary 'vertical)))
5450         (let ((selected (selected-window)))
5451           (select-window (get-buffer-window (current-buffer) t))
5452           (gnus-summary-position-point)
5453           (gnus-horizontal-recenter)
5454           (select-window selected))))))
5455
5456 (defun gnus-summary-jump-to-group (newsgroup)
5457   "Move point to NEWSGROUP in group mode buffer."
5458   ;; Keep update point of group mode buffer if visible.
5459   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5460       (save-window-excursion
5461         ;; Take care of tree window mode.
5462         (when (get-buffer-window gnus-group-buffer)
5463           (pop-to-buffer gnus-group-buffer))
5464         (gnus-group-jump-to-group newsgroup))
5465     (save-excursion
5466       ;; Take care of tree window mode.
5467       (if (get-buffer-window gnus-group-buffer)
5468           (pop-to-buffer gnus-group-buffer)
5469         (set-buffer gnus-group-buffer))
5470       (gnus-group-jump-to-group newsgroup))))
5471
5472 ;; This function returns a list of article numbers based on the
5473 ;; difference between the ranges of read articles in this group and
5474 ;; the range of active articles.
5475 (defun gnus-list-of-unread-articles (group)
5476   (let* ((read (gnus-info-read (gnus-get-info group)))
5477          (active (or (gnus-active group) (gnus-activate-group group)))
5478          (last (cdr active))
5479          first nlast unread)
5480     ;; If none are read, then all are unread.
5481     (if (not read)
5482         (setq first (car active))
5483       ;; If the range of read articles is a single range, then the
5484       ;; first unread article is the article after the last read
5485       ;; article.  Sounds logical, doesn't it?
5486       (if (and (not (listp (cdr read)))
5487                (or (< (car read) (car active))
5488                    (progn (setq read (list read))
5489                           nil)))
5490           (setq first (max (car active) (1+ (cdr read))))
5491         ;; `read' is a list of ranges.
5492         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5493                                   (caar read)))
5494                   1)
5495           (setq first (car active)))
5496         (while read
5497           (when first
5498             (while (< first nlast)
5499               (push first unread)
5500               (setq first (1+ first))))
5501           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5502           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5503           (setq read (cdr read)))))
5504     ;; And add the last unread articles.
5505     (while (<= first last)
5506       (push first unread)
5507       (setq first (1+ first)))
5508     ;; Return the list of unread articles.
5509     (delq 0 (nreverse unread))))
5510
5511 (defun gnus-list-of-read-articles (group)
5512   "Return a list of unread, unticked and non-dormant articles."
5513   (let* ((info (gnus-get-info group))
5514          (marked (gnus-info-marks info))
5515          (active (gnus-active group)))
5516     (and info active
5517          (gnus-set-difference
5518           (gnus-sorted-complement
5519            (gnus-uncompress-range active)
5520            (gnus-list-of-unread-articles group))
5521           (append
5522            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5523            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5524
5525 ;; Various summary commands
5526
5527 (defun gnus-summary-select-article-buffer ()
5528   "Reconfigure windows to show article buffer."
5529   (interactive)
5530   (if (not (gnus-buffer-live-p gnus-article-buffer))
5531       (error "There is no article buffer for this summary buffer")
5532     (gnus-configure-windows 'article)
5533     (select-window (get-buffer-window gnus-article-buffer))))
5534
5535 (defun gnus-summary-universal-argument (arg)
5536   "Perform any operation on all articles that are process/prefixed."
5537   (interactive "P")
5538   (let ((articles (gnus-summary-work-articles arg))
5539         func article)
5540     (if (eq
5541          (setq
5542           func
5543           (key-binding
5544            (read-key-sequence
5545             (substitute-command-keys
5546              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5547          'undefined)
5548         (gnus-error 1 "Undefined key")
5549       (save-excursion
5550         (while articles
5551           (gnus-summary-goto-subject (setq article (pop articles)))
5552           (let (gnus-newsgroup-processable)
5553             (command-execute func))
5554           (gnus-summary-remove-process-mark article)))))
5555   (gnus-summary-position-point))
5556
5557 (defun gnus-summary-toggle-truncation (&optional arg)
5558   "Toggle truncation of summary lines.
5559 With arg, turn line truncation on iff arg is positive."
5560   (interactive "P")
5561   (setq truncate-lines
5562         (if (null arg) (not truncate-lines)
5563           (> (prefix-numeric-value arg) 0)))
5564   (redraw-display))
5565
5566 (defun gnus-summary-reselect-current-group (&optional all rescan)
5567   "Exit and then reselect the current newsgroup.
5568 The prefix argument ALL means to select all articles."
5569   (interactive "P")
5570   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5571     (error "Ephemeral groups can't be reselected"))
5572   (let ((current-subject (gnus-summary-article-number))
5573         (group gnus-newsgroup-name))
5574     (setq gnus-newsgroup-begin nil)
5575     (gnus-summary-exit)
5576     ;; We have to adjust the point of group mode buffer because
5577     ;; point was moved to the next unread newsgroup by exiting.
5578     (gnus-summary-jump-to-group group)
5579     (when rescan
5580       (save-excursion
5581         (save-window-excursion
5582           ;; Don't show group contents.
5583           (set-window-start (selected-window) (point-max))
5584           (gnus-group-get-new-news-this-group 1))))
5585     (gnus-group-read-group all t)
5586     (gnus-summary-goto-subject current-subject nil t)))
5587
5588 (defun gnus-summary-rescan-group (&optional all)
5589   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5590   (interactive "P")
5591   (gnus-summary-reselect-current-group all t))
5592
5593 (defun gnus-summary-update-info (&optional non-destructive)
5594   (save-excursion
5595     (let ((group gnus-newsgroup-name))
5596       (when group
5597         (when gnus-newsgroup-kill-headers
5598           (setq gnus-newsgroup-killed
5599                 (gnus-compress-sequence
5600                  (nconc
5601                   (gnus-set-sorted-intersection
5602                    (gnus-uncompress-range gnus-newsgroup-killed)
5603                    (setq gnus-newsgroup-unselected
5604                          (sort gnus-newsgroup-unselected '<)))
5605                   (setq gnus-newsgroup-unreads
5606                         (sort gnus-newsgroup-unreads '<)))
5607                  t)))
5608         (unless (listp (cdr gnus-newsgroup-killed))
5609           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5610         (let ((headers gnus-newsgroup-headers))
5611           ;; Set the new ranges of read articles.
5612           (save-excursion
5613             (set-buffer gnus-group-buffer)
5614             (gnus-undo-force-boundary))
5615           (gnus-update-read-articles
5616            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5617           ;; Set the current article marks.
5618           (let ((gnus-newsgroup-scored
5619                  (if (and (not gnus-save-score)
5620                           (not non-destructive))
5621                      nil
5622                    gnus-newsgroup-scored)))
5623             (save-excursion
5624               (gnus-update-marks)))
5625           ;; Do the cross-ref thing.
5626           (when gnus-use-cross-reference
5627             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5628           ;; Do not switch windows but change the buffer to work.
5629           (set-buffer gnus-group-buffer)
5630           (unless (gnus-ephemeral-group-p group)
5631             (gnus-group-update-group group)))))))
5632
5633 (defun gnus-summary-save-newsrc (&optional force)
5634   "Save the current number of read/marked articles in the dribble buffer.
5635 The dribble buffer will then be saved.
5636 If FORCE (the prefix), also save the .newsrc file(s)."
5637   (interactive "P")
5638   (gnus-summary-update-info t)
5639   (if force
5640       (gnus-save-newsrc-file)
5641     (gnus-dribble-save)))
5642
5643 (defun gnus-summary-exit (&optional temporary)
5644   "Exit reading current newsgroup, and then return to group selection mode.
5645 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5646   (interactive)
5647   (gnus-set-global-variables)
5648   (gnus-kill-save-kill-buffer)
5649   (gnus-async-halt-prefetch)
5650   (let* ((group gnus-newsgroup-name)
5651          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5652          (mode major-mode)
5653          (group-point nil)
5654          (buf (current-buffer)))
5655     (unless quit-config
5656       ;; Do adaptive scoring, and possibly save score files.
5657       (when gnus-newsgroup-adaptive
5658         (gnus-score-adaptive))
5659       (when gnus-use-scoring
5660         (gnus-score-save)))
5661     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5662     ;; If we have several article buffers, we kill them at exit.
5663     (unless gnus-single-article-buffer
5664       (gnus-kill-buffer gnus-original-article-buffer)
5665       (setq gnus-article-current nil))
5666     (when gnus-use-cache
5667       (gnus-cache-possibly-remove-articles)
5668       (gnus-cache-save-buffers))
5669     (gnus-async-prefetch-remove-group group)
5670     (when gnus-suppress-duplicates
5671       (gnus-dup-enter-articles))
5672     (when gnus-use-trees
5673       (gnus-tree-close group))
5674     (when gnus-use-cache
5675       (gnus-cache-write-active))
5676     ;; Remove entries for this group.
5677     (nnmail-purge-split-history (gnus-group-real-name group))
5678     ;; Make all changes in this group permanent.
5679     (unless quit-config
5680       (gnus-run-hooks 'gnus-exit-group-hook)
5681       (gnus-summary-update-info))
5682     (gnus-close-group group)
5683     ;; Make sure where we were, and go to next newsgroup.
5684     (set-buffer gnus-group-buffer)
5685     (unless quit-config
5686       (gnus-group-jump-to-group group))
5687     (gnus-run-hooks 'gnus-summary-exit-hook)
5688     (unless (or quit-config
5689                 ;; If this group has disappeared from the summary
5690                 ;; buffer, don't skip forwards.
5691                 (not (string= group (gnus-group-group-name))))
5692       (gnus-group-next-unread-group 1))
5693     (setq group-point (point))
5694     (if temporary
5695         nil                             ;Nothing to do.
5696       ;; If we have several article buffers, we kill them at exit.
5697       (unless gnus-single-article-buffer
5698         (gnus-kill-buffer gnus-article-buffer)
5699         (gnus-kill-buffer gnus-original-article-buffer)
5700         (setq gnus-article-current nil))
5701       (set-buffer buf)
5702       (if (not gnus-kill-summary-on-exit)
5703           (gnus-deaden-summary)
5704         ;; We set all buffer-local variables to nil.  It is unclear why
5705         ;; this is needed, but if we don't, buffer-local variables are
5706         ;; not garbage-collected, it seems.  This would the lead to en
5707         ;; ever-growing Emacs.
5708         (gnus-summary-clear-local-variables)
5709         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5710           (gnus-summary-clear-local-variables))
5711         (when (get-buffer gnus-article-buffer)
5712           (bury-buffer gnus-article-buffer))
5713         ;; We clear the global counterparts of the buffer-local
5714         ;; variables as well, just to be on the safe side.
5715         (set-buffer gnus-group-buffer)
5716         (gnus-summary-clear-local-variables)
5717         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5718           (gnus-summary-clear-local-variables))
5719         ;; Return to group mode buffer.
5720         (when (eq mode 'gnus-summary-mode)
5721           (gnus-kill-buffer buf)))
5722       (setq gnus-current-select-method gnus-select-method)
5723       (pop-to-buffer gnus-group-buffer)
5724       (if (not quit-config)
5725           (progn
5726             (goto-char group-point)
5727             (gnus-configure-windows 'group 'force)
5728             (unless (pos-visible-in-window-p)
5729               (forward-line (/ (static-if (featurep 'xemacs)
5730                                    (window-displayed-height)
5731                                  (1- (window-height)))
5732                                -2))
5733               (set-window-start (selected-window) (point))
5734               (goto-char group-point)))
5735         (gnus-handle-ephemeral-exit quit-config))
5736       ;; Clear the current group name.
5737       (unless quit-config
5738         (setq gnus-newsgroup-name nil)))))
5739
5740 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5741 (defun gnus-summary-exit-no-update (&optional no-questions)
5742   "Quit reading current newsgroup without updating read article info."
5743   (interactive)
5744   (let* ((group gnus-newsgroup-name)
5745          (quit-config (gnus-group-quit-config group)))
5746     (when (or no-questions
5747               gnus-expert-user
5748               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5749       (gnus-async-halt-prefetch)
5750       (mapcar 'funcall
5751               (delq 'gnus-summary-expire-articles
5752                     (copy-sequence gnus-summary-prepare-exit-hook)))
5753       ;; If we have several article buffers, we kill them at exit.
5754       (unless gnus-single-article-buffer
5755         (gnus-kill-buffer gnus-article-buffer)
5756         (gnus-kill-buffer gnus-original-article-buffer)
5757         (setq gnus-article-current nil))
5758       (if (not gnus-kill-summary-on-exit)
5759           (gnus-deaden-summary)
5760         (gnus-close-group group)
5761         (gnus-summary-clear-local-variables)
5762         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5763           (gnus-summary-clear-local-variables))
5764         (set-buffer gnus-group-buffer)
5765         (gnus-summary-clear-local-variables)
5766         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5767           (gnus-summary-clear-local-variables))
5768         (when (get-buffer gnus-summary-buffer)
5769           (kill-buffer gnus-summary-buffer)))
5770       (unless gnus-single-article-buffer
5771         (setq gnus-article-current nil))
5772       (when gnus-use-trees
5773         (gnus-tree-close group))
5774       (gnus-async-prefetch-remove-group group)
5775       (when (get-buffer gnus-article-buffer)
5776         (bury-buffer gnus-article-buffer))
5777       ;; Return to the group buffer.
5778       (gnus-configure-windows 'group 'force)
5779       ;; Clear the current group name.
5780       (setq gnus-newsgroup-name nil)
5781       (when (equal (gnus-group-group-name) group)
5782         (gnus-group-next-unread-group 1))
5783       (when quit-config
5784         (gnus-handle-ephemeral-exit quit-config)))))
5785
5786 (defun gnus-handle-ephemeral-exit (quit-config)
5787   "Handle movement when leaving an ephemeral group.
5788 The state which existed when entering the ephemeral is reset."
5789   (if (not (buffer-name (car quit-config)))
5790       (gnus-configure-windows 'group 'force)
5791     (set-buffer (car quit-config))
5792     (cond ((eq major-mode 'gnus-summary-mode)
5793            (gnus-set-global-variables))
5794           ((eq major-mode 'gnus-article-mode)
5795            (save-excursion
5796              ;; The `gnus-summary-buffer' variable may point
5797              ;; to the old summary buffer when using a single
5798              ;; article buffer.
5799              (unless (gnus-buffer-live-p gnus-summary-buffer)
5800                (set-buffer gnus-group-buffer))
5801              (set-buffer gnus-summary-buffer)
5802              (gnus-set-global-variables))))
5803     (if (or (eq (cdr quit-config) 'article)
5804             (eq (cdr quit-config) 'pick))
5805         (progn
5806           ;; The current article may be from the ephemeral group
5807           ;; thus it is best that we reload this article
5808           (gnus-summary-show-article)
5809           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5810               (gnus-configure-windows 'pick 'force)
5811             (gnus-configure-windows (cdr quit-config) 'force)))
5812       (gnus-configure-windows (cdr quit-config) 'force))
5813     (when (eq major-mode 'gnus-summary-mode)
5814       (gnus-summary-next-subject 1 nil t)
5815       (gnus-summary-recenter)
5816       (gnus-summary-position-point))))
5817
5818 (defun gnus-summary-preview-mime-message ()
5819   "MIME decode and play this message."
5820   (interactive)
5821   (let ((gnus-break-pages nil)
5822         (gnus-show-mime t))
5823     (gnus-summary-select-article gnus-show-all-headers t))
5824   (select-window (get-buffer-window gnus-article-buffer)))
5825
5826 ;;; Dead summaries.
5827
5828 (defvar gnus-dead-summary-mode-map nil)
5829
5830 (unless gnus-dead-summary-mode-map
5831   (setq gnus-dead-summary-mode-map (make-keymap))
5832   (suppress-keymap gnus-dead-summary-mode-map)
5833   (substitute-key-definition
5834    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5835   (let ((keys '("\C-d" "\r" "\177" [delete])))
5836     (while keys
5837       (define-key gnus-dead-summary-mode-map
5838         (pop keys) 'gnus-summary-wake-up-the-dead))))
5839
5840 (defvar gnus-dead-summary-mode nil
5841   "Minor mode for Gnus summary buffers.")
5842
5843 (defun gnus-dead-summary-mode (&optional arg)
5844   "Minor mode for Gnus summary buffers."
5845   (interactive "P")
5846   (when (eq major-mode 'gnus-summary-mode)
5847     (make-local-variable 'gnus-dead-summary-mode)
5848     (setq gnus-dead-summary-mode
5849           (if (null arg) (not gnus-dead-summary-mode)
5850             (> (prefix-numeric-value arg) 0)))
5851     (when gnus-dead-summary-mode
5852       (gnus-add-minor-mode
5853        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5854
5855 (defun gnus-deaden-summary ()
5856   "Make the current summary buffer into a dead summary buffer."
5857   ;; Kill any previous dead summary buffer.
5858   (when (and gnus-dead-summary
5859              (buffer-name gnus-dead-summary))
5860     (save-excursion
5861       (set-buffer gnus-dead-summary)
5862       (when gnus-dead-summary-mode
5863         (kill-buffer (current-buffer)))))
5864   ;; Make this the current dead summary.
5865   (setq gnus-dead-summary (current-buffer))
5866   (gnus-dead-summary-mode 1)
5867   (let ((name (buffer-name)))
5868     (when (string-match "Summary" name)
5869       (rename-buffer
5870        (concat (substring name 0 (match-beginning 0)) "Dead "
5871                (substring name (match-beginning 0)))
5872        t)
5873       (bury-buffer))))
5874
5875 (defun gnus-kill-or-deaden-summary (buffer)
5876   "Kill or deaden the summary BUFFER."
5877   (save-excursion
5878     (when (and (buffer-name buffer)
5879                (not gnus-single-article-buffer))
5880       (save-excursion
5881         (set-buffer buffer)
5882         (gnus-kill-buffer gnus-article-buffer)
5883         (gnus-kill-buffer gnus-original-article-buffer)))
5884     (cond (gnus-kill-summary-on-exit
5885            (when (and gnus-use-trees
5886                       (gnus-buffer-exists-p buffer))
5887              (save-excursion
5888                (set-buffer buffer)
5889                (gnus-tree-close gnus-newsgroup-name)))
5890            (gnus-kill-buffer buffer))
5891           ((gnus-buffer-exists-p buffer)
5892            (save-excursion
5893              (set-buffer buffer)
5894              (gnus-deaden-summary))))))
5895
5896 (defun gnus-summary-wake-up-the-dead (&rest args)
5897   "Wake up the dead summary buffer."
5898   (interactive)
5899   (gnus-dead-summary-mode -1)
5900   (let ((name (buffer-name)))
5901     (when (string-match "Dead " name)
5902       (rename-buffer
5903        (concat (substring name 0 (match-beginning 0))
5904                (substring name (match-end 0)))
5905        t)))
5906   (gnus-message 3 "This dead summary is now alive again"))
5907
5908 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5909 (defun gnus-summary-fetch-faq (&optional faq-dir)
5910   "Fetch the FAQ for the current group.
5911 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5912 in."
5913   (interactive
5914    (list
5915     (when current-prefix-arg
5916       (completing-read
5917        "Faq dir: " (and (listp gnus-group-faq-directory)
5918                         (mapcar (lambda (file) (list file))
5919                                 gnus-group-faq-directory))))))
5920   (let (gnus-faq-buffer)
5921     (when (setq gnus-faq-buffer
5922                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5923       (gnus-configure-windows 'summary-faq))))
5924
5925 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5926 (defun gnus-summary-describe-group (&optional force)
5927   "Describe the current newsgroup."
5928   (interactive "P")
5929   (gnus-group-describe-group force gnus-newsgroup-name))
5930
5931 (defun gnus-summary-describe-briefly ()
5932   "Describe summary mode commands briefly."
5933   (interactive)
5934   (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")))
5935
5936 ;; Walking around group mode buffer from summary mode.
5937
5938 (defun gnus-summary-next-group (&optional no-article target-group backward)
5939   "Exit current newsgroup and then select next unread newsgroup.
5940 If prefix argument NO-ARTICLE is non-nil, no article is selected
5941 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5942 previous group instead."
5943   (interactive "P")
5944   ;; Stop pre-fetching.
5945   (gnus-async-halt-prefetch)
5946   (let ((current-group gnus-newsgroup-name)
5947         (current-buffer (current-buffer))
5948         entered)
5949     ;; First we semi-exit this group to update Xrefs and all variables.
5950     ;; We can't do a real exit, because the window conf must remain
5951     ;; the same in case the user is prompted for info, and we don't
5952     ;; want the window conf to change before that...
5953     (gnus-summary-exit t)
5954     (while (not entered)
5955       ;; Then we find what group we are supposed to enter.
5956       (set-buffer gnus-group-buffer)
5957       (gnus-group-jump-to-group current-group)
5958       (setq target-group
5959             (or target-group
5960                 (if (eq gnus-keep-same-level 'best)
5961                     (gnus-summary-best-group gnus-newsgroup-name)
5962                   (gnus-summary-search-group backward gnus-keep-same-level))))
5963       (if (not target-group)
5964           ;; There are no further groups, so we return to the group
5965           ;; buffer.
5966           (progn
5967             (gnus-message 5 "Returning to the group buffer")
5968             (setq entered t)
5969             (when (gnus-buffer-live-p current-buffer)
5970               (set-buffer current-buffer)
5971               (gnus-summary-exit))
5972             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5973         ;; We try to enter the target group.
5974         (gnus-group-jump-to-group target-group)
5975         (let ((unreads (gnus-group-group-unread)))
5976           (if (and (or (eq t unreads)
5977                        (and unreads (not (zerop unreads))))
5978                    (gnus-summary-read-group
5979                     target-group nil no-article
5980                     (and (buffer-name current-buffer) current-buffer)
5981                     nil backward))
5982               (setq entered t)
5983             (setq current-group target-group
5984                   target-group nil)))))))
5985
5986 (defun gnus-summary-prev-group (&optional no-article)
5987   "Exit current newsgroup and then select previous unread newsgroup.
5988 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5989   (interactive "P")
5990   (gnus-summary-next-group no-article nil t))
5991
5992 ;; Walking around summary lines.
5993
5994 (defun gnus-summary-first-subject (&optional unread undownloaded)
5995   "Go to the first unread subject.
5996 If UNREAD is non-nil, go to the first unread article.
5997 Returns the article selected or nil if there are no unread articles."
5998   (interactive "P")
5999   (prog1
6000       (cond
6001        ;; Empty summary.
6002        ((null gnus-newsgroup-data)
6003         (gnus-message 3 "No articles in the group")
6004         nil)
6005        ;; Pick the first article.
6006        ((not unread)
6007         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6008         (gnus-data-number (car gnus-newsgroup-data)))
6009        ;; No unread articles.
6010        ((null gnus-newsgroup-unreads)
6011         (gnus-message 3 "No more unread articles")
6012         nil)
6013        ;; Find the first unread article.
6014        (t
6015         (let ((data gnus-newsgroup-data))
6016           (while (and data
6017                       (and (not (and undownloaded
6018                                      (eq gnus-undownloaded-mark
6019                                          (gnus-data-mark (car data)))))
6020                            (not (gnus-data-unread-p (car data)))))
6021             (setq data (cdr data)))
6022           (when data
6023             (goto-char (gnus-data-pos (car data)))
6024             (gnus-data-number (car data))))))
6025     (gnus-summary-position-point)))
6026
6027 (defun gnus-summary-next-subject (n &optional unread dont-display)
6028   "Go to next N'th summary line.
6029 If N is negative, go to the previous N'th subject line.
6030 If UNREAD is non-nil, only unread articles are selected.
6031 The difference between N and the actual number of steps taken is
6032 returned."
6033   (interactive "p")
6034   (let ((backward (< n 0))
6035         (n (abs n)))
6036     (while (and (> n 0)
6037                 (if backward
6038                     (gnus-summary-find-prev unread)
6039                   (gnus-summary-find-next unread)))
6040       (unless (zerop (setq n (1- n)))
6041         (gnus-summary-show-thread)))
6042     (when (/= 0 n)
6043       (gnus-message 7 "No more%s articles"
6044                     (if unread " unread" "")))
6045     (unless dont-display
6046       (gnus-summary-recenter)
6047       (gnus-summary-position-point))
6048     n))
6049
6050 (defun gnus-summary-next-unread-subject (n)
6051   "Go to next N'th unread summary line."
6052   (interactive "p")
6053   (gnus-summary-next-subject n t))
6054
6055 (defun gnus-summary-prev-subject (n &optional unread)
6056   "Go to previous N'th summary line.
6057 If optional argument UNREAD is non-nil, only unread article is selected."
6058   (interactive "p")
6059   (gnus-summary-next-subject (- n) unread))
6060
6061 (defun gnus-summary-prev-unread-subject (n)
6062   "Go to previous N'th unread summary line."
6063   (interactive "p")
6064   (gnus-summary-next-subject (- n) t))
6065
6066 (defun gnus-summary-goto-subject (article &optional force silent)
6067   "Go the subject line of ARTICLE.
6068 If FORCE, also allow jumping to articles not currently shown."
6069   (interactive "nArticle number: ")
6070   (let ((b (point))
6071         (data (gnus-data-find article)))
6072     ;; We read in the article if we have to.
6073     (and (not data)
6074          force
6075          (gnus-summary-insert-subject
6076           article
6077           (if (or (numberp force) (vectorp force)) force)
6078           t)
6079          (setq data (gnus-data-find article)))
6080     (goto-char b)
6081     (if (not data)
6082         (progn
6083           (unless silent
6084             (gnus-message 3 "Can't find article %d" article))
6085           nil)
6086       (goto-char (gnus-data-pos data))
6087       (gnus-summary-position-point)
6088       article)))
6089
6090 ;; Walking around summary lines with displaying articles.
6091
6092 (defun gnus-summary-expand-window (&optional arg)
6093   "Make the summary buffer take up the entire Emacs frame.
6094 Given a prefix, will force an `article' buffer configuration."
6095   (interactive "P")
6096   (if arg
6097       (gnus-configure-windows 'article 'force)
6098     (gnus-configure-windows 'summary 'force)))
6099
6100 (defun gnus-summary-display-article (article &optional all-header)
6101   "Display ARTICLE in article buffer."
6102   (when (gnus-buffer-live-p gnus-article-buffer)
6103     (with-current-buffer gnus-article-buffer
6104       (set-buffer-multibyte t)))
6105   (gnus-set-global-variables)
6106   (when (gnus-buffer-live-p gnus-article-buffer)
6107     (with-current-buffer gnus-article-buffer
6108       (setq gnus-article-charset gnus-newsgroup-charset)
6109       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)))
6110   (if (null article)
6111       nil
6112     (prog1
6113         (if gnus-summary-display-article-function
6114             (funcall gnus-summary-display-article-function article all-header)
6115           (gnus-article-prepare article all-header))
6116       (with-current-buffer gnus-article-buffer
6117         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6118              nil))
6119       (gnus-run-hooks 'gnus-select-article-hook)
6120       (when (and gnus-current-article
6121                  (not (zerop gnus-current-article)))
6122         (gnus-summary-goto-subject gnus-current-article))
6123       (gnus-summary-recenter)
6124       (when (and gnus-use-trees gnus-show-threads)
6125         (gnus-possibly-generate-tree article)
6126         (gnus-highlight-selected-tree article))
6127       ;; Successfully display article.
6128       (gnus-article-set-window-start
6129        (cdr (assq article gnus-newsgroup-bookmarks))))))
6130
6131 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6132   "Select the current article.
6133 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6134 non-nil, the article will be re-fetched even if it already present in
6135 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6136 be displayed."
6137   ;; Make sure we are in the summary buffer to work around bbdb bug.
6138   (unless (eq major-mode 'gnus-summary-mode)
6139     (set-buffer gnus-summary-buffer))
6140   (let ((article (or article (gnus-summary-article-number)))
6141         (all-headers (not (not all-headers))) ;Must be T or NIL.
6142         gnus-summary-display-article-function)
6143     (and (not pseudo)
6144          (gnus-summary-article-pseudo-p article)
6145          (error "This is a pseudo-article"))
6146     (save-excursion
6147       (set-buffer gnus-summary-buffer)
6148       (if (or (and gnus-single-article-buffer
6149                    (or (null gnus-current-article)
6150                        (null gnus-article-current)
6151                        (null (get-buffer gnus-article-buffer))
6152                        (not (eq article (cdr gnus-article-current)))
6153                        (not (equal (car gnus-article-current)
6154                                    gnus-newsgroup-name))))
6155               (and (not gnus-single-article-buffer)
6156                    (or (null gnus-current-article)
6157                        (not (eq gnus-current-article article))))
6158               force)
6159           ;; The requested article is different from the current article.
6160           (progn
6161             (gnus-summary-display-article article all-headers)
6162             (when (or all-headers gnus-show-all-headers)
6163               (gnus-article-show-all-headers))
6164             (gnus-article-set-window-start
6165              (cdr (assq article gnus-newsgroup-bookmarks)))
6166             article)
6167         (when (or all-headers gnus-show-all-headers)
6168           (gnus-article-show-all-headers))
6169         'old))))
6170
6171 (defun gnus-summary-force-verify-and-decrypt ()
6172   (interactive)
6173   (let ((mm-verify-option 'known)
6174         (mm-decrypt-option 'known))
6175     (gnus-summary-select-article nil 'force)))
6176
6177 (defun gnus-summary-set-current-mark (&optional current-mark)
6178   "Obsolete function."
6179   nil)
6180
6181 (defun gnus-summary-next-article (&optional unread subject backward push)
6182   "Select the next article.
6183 If UNREAD, only unread articles are selected.
6184 If SUBJECT, only articles with SUBJECT are selected.
6185 If BACKWARD, the previous article is selected instead of the next."
6186   (interactive "P")
6187   (cond
6188    ;; Is there such an article?
6189    ((and (gnus-summary-search-forward unread subject backward)
6190          (or (gnus-summary-display-article (gnus-summary-article-number))
6191              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6192     (gnus-summary-position-point))
6193    ;; If not, we try the first unread, if that is wanted.
6194    ((and subject
6195          gnus-auto-select-same
6196          (gnus-summary-first-unread-article))
6197     (gnus-summary-position-point)
6198     (gnus-message 6 "Wrapped"))
6199    ;; Try to get next/previous article not displayed in this group.
6200    ((and gnus-auto-extend-newsgroup
6201          (not unread) (not subject))
6202     (gnus-summary-goto-article
6203      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6204      nil (count-lines (point-min) (point))))
6205    ;; Go to next/previous group.
6206    (t
6207     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6208       (gnus-summary-jump-to-group gnus-newsgroup-name))
6209     (let ((cmd last-command-char)
6210           (point
6211            (save-excursion
6212              (set-buffer gnus-group-buffer)
6213              (point)))
6214           (group
6215            (if (eq gnus-keep-same-level 'best)
6216                (gnus-summary-best-group gnus-newsgroup-name)
6217              (gnus-summary-search-group backward gnus-keep-same-level))))
6218       ;; For some reason, the group window gets selected.  We change
6219       ;; it back.
6220       (select-window (get-buffer-window (current-buffer)))
6221       ;; Select next unread newsgroup automagically.
6222       (cond
6223        ((or (not gnus-auto-select-next)
6224             (not cmd))
6225         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6226        ((or (eq gnus-auto-select-next 'quietly)
6227             (and (eq gnus-auto-select-next 'slightly-quietly)
6228                  push)
6229             (and (eq gnus-auto-select-next 'almost-quietly)
6230                  (gnus-summary-last-article-p)))
6231         ;; Select quietly.
6232         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6233             (gnus-summary-exit)
6234           (gnus-message 7 "No more%s articles (%s)..."
6235                         (if unread " unread" "")
6236                         (if group (concat "selecting " group)
6237                           "exiting"))
6238           (gnus-summary-next-group nil group backward)))
6239        (t
6240         (when (gnus-key-press-event-p last-input-event)
6241           (gnus-summary-walk-group-buffer
6242            gnus-newsgroup-name cmd unread backward point))))))))
6243
6244 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6245   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6246                       (?\C-p (gnus-group-prev-unread-group 1))))
6247         (cursor-in-echo-area t)
6248         keve key group ended)
6249     (save-excursion
6250       (set-buffer gnus-group-buffer)
6251       (goto-char start)
6252       (setq group
6253             (if (eq gnus-keep-same-level 'best)
6254                 (gnus-summary-best-group gnus-newsgroup-name)
6255               (gnus-summary-search-group backward gnus-keep-same-level))))
6256     (while (not ended)
6257       (gnus-message
6258        5 "No more%s articles%s" (if unread " unread" "")
6259        (if (and group
6260                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6261            (format " (Type %s for %s [%s])"
6262                    (single-key-description cmd) group
6263                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6264          (format " (Type %s to exit %s)"
6265                  (single-key-description cmd)
6266                  gnus-newsgroup-name)))
6267       ;; Confirm auto selection.
6268       (setq key (car (setq keve (gnus-read-event-char))))
6269       (setq ended t)
6270       (cond
6271        ((assq key keystrokes)
6272         (let ((obuf (current-buffer)))
6273           (switch-to-buffer gnus-group-buffer)
6274           (when group
6275             (gnus-group-jump-to-group group))
6276           (eval (cadr (assq key keystrokes)))
6277           (setq group (gnus-group-group-name))
6278           (switch-to-buffer obuf))
6279         (setq ended nil))
6280        ((equal key cmd)
6281         (if (or (not group)
6282                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6283             (gnus-summary-exit)
6284           (gnus-summary-next-group nil group backward)))
6285        (t
6286         (push (cdr keve) unread-command-events))))))
6287
6288 (defun gnus-summary-next-unread-article ()
6289   "Select unread article after current one."
6290   (interactive)
6291   (gnus-summary-next-article
6292    (or (not (eq gnus-summary-goto-unread 'never))
6293        (gnus-summary-last-article-p (gnus-summary-article-number)))
6294    (and gnus-auto-select-same
6295         (gnus-summary-article-subject))))
6296
6297 (defun gnus-summary-prev-article (&optional unread subject)
6298   "Select the article after the current one.
6299 If UNREAD is non-nil, only unread articles are selected."
6300   (interactive "P")
6301   (gnus-summary-next-article unread subject t))
6302
6303 (defun gnus-summary-prev-unread-article ()
6304   "Select unread article before current one."
6305   (interactive)
6306   (gnus-summary-prev-article
6307    (or (not (eq gnus-summary-goto-unread 'never))
6308        (gnus-summary-first-article-p (gnus-summary-article-number)))
6309    (and gnus-auto-select-same
6310         (gnus-summary-article-subject))))
6311
6312 (defun gnus-summary-next-page (&optional lines circular)
6313   "Show next page of the selected article.
6314 If at the end of the current article, select the next article.
6315 LINES says how many lines should be scrolled up.
6316
6317 If CIRCULAR is non-nil, go to the start of the article instead of
6318 selecting the next article when reaching the end of the current
6319 article."
6320   (interactive "P")
6321   (setq gnus-summary-buffer (current-buffer))
6322   (gnus-set-global-variables)
6323   (let ((article (gnus-summary-article-number))
6324         (article-window (get-buffer-window gnus-article-buffer t))
6325         endp)
6326     ;; If the buffer is empty, we have no article.
6327     (unless article
6328       (error "No article to select"))
6329     (gnus-configure-windows 'article)
6330     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6331         (if (and (eq gnus-summary-goto-unread 'never)
6332                  (not (gnus-summary-last-article-p article)))
6333             (gnus-summary-next-article)
6334           (gnus-summary-next-unread-article))
6335       (if (or (null gnus-current-article)
6336               (null gnus-article-current)
6337               (/= article (cdr gnus-article-current))
6338               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6339           ;; Selected subject is different from current article's.
6340           (gnus-summary-display-article article)
6341         (when article-window
6342           (gnus-eval-in-buffer-window gnus-article-buffer
6343             (setq endp (gnus-article-next-page lines)))
6344           (when endp
6345             (cond (circular
6346                    (gnus-summary-beginning-of-article))
6347                   (lines
6348                    (gnus-message 3 "End of message"))
6349                   ((null lines)
6350                    (if (and (eq gnus-summary-goto-unread 'never)
6351                             (not (gnus-summary-last-article-p article)))
6352                        (gnus-summary-next-article)
6353                      (gnus-summary-next-unread-article))))))))
6354     (gnus-summary-recenter)
6355     (gnus-summary-position-point)))
6356
6357 (defun gnus-summary-prev-page (&optional lines move)
6358   "Show previous page of selected article.
6359 Argument LINES specifies lines to be scrolled down.
6360 If MOVE, move to the previous unread article if point is at
6361 the beginning of the buffer."
6362   (interactive "P")
6363   (let ((article (gnus-summary-article-number))
6364         (article-window (get-buffer-window gnus-article-buffer t))
6365         endp)
6366     (gnus-configure-windows 'article)
6367     (if (or (null gnus-current-article)
6368             (null gnus-article-current)
6369             (/= article (cdr gnus-article-current))
6370             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6371         ;; Selected subject is different from current article's.
6372         (gnus-summary-display-article article)
6373       (gnus-summary-recenter)
6374       (when article-window
6375         (gnus-eval-in-buffer-window gnus-article-buffer
6376           (setq endp (gnus-article-prev-page lines)))
6377         (when (and move endp)
6378           (cond (lines
6379                  (gnus-message 3 "Beginning of message"))
6380                 ((null lines)
6381                  (if (and (eq gnus-summary-goto-unread 'never)
6382                           (not (gnus-summary-first-article-p article)))
6383                      (gnus-summary-prev-article)
6384                    (gnus-summary-prev-unread-article))))))))
6385   (gnus-summary-position-point))
6386
6387 (defun gnus-summary-prev-page-or-article (&optional lines)
6388   "Show previous page of selected article.
6389 Argument LINES specifies lines to be scrolled down.
6390 If at the beginning of the article, go to the next article."
6391   (interactive "P")
6392   (gnus-summary-prev-page lines t))
6393
6394 (defun gnus-summary-scroll-up (lines)
6395   "Scroll up (or down) one line current article.
6396 Argument LINES specifies lines to be scrolled up (or down if negative)."
6397   (interactive "p")
6398   (gnus-configure-windows 'article)
6399   (gnus-summary-show-thread)
6400   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6401     (gnus-eval-in-buffer-window gnus-article-buffer
6402       (cond ((> lines 0)
6403              (when (gnus-article-next-page lines)
6404                (gnus-message 3 "End of message")))
6405             ((< lines 0)
6406              (gnus-article-prev-page (- lines))))))
6407   (gnus-summary-recenter)
6408   (gnus-summary-position-point))
6409
6410 (defun gnus-summary-scroll-down (lines)
6411   "Scroll down (or up) one line current article.
6412 Argument LINES specifies lines to be scrolled down (or up if negative)."
6413   (interactive "p")
6414   (gnus-summary-scroll-up (- lines)))
6415
6416 (defun gnus-summary-next-same-subject ()
6417   "Select next article which has the same subject as current one."
6418   (interactive)
6419   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6420
6421 (defun gnus-summary-prev-same-subject ()
6422   "Select previous article which has the same subject as current one."
6423   (interactive)
6424   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6425
6426 (defun gnus-summary-next-unread-same-subject ()
6427   "Select next unread article which has the same subject as current one."
6428   (interactive)
6429   (gnus-summary-next-article t (gnus-summary-article-subject)))
6430
6431 (defun gnus-summary-prev-unread-same-subject ()
6432   "Select previous unread article which has the same subject as current one."
6433   (interactive)
6434   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6435
6436 (defun gnus-summary-first-unread-article ()
6437   "Select the first unread article.
6438 Return nil if there are no unread articles."
6439   (interactive)
6440   (prog1
6441       (when (gnus-summary-first-subject t)
6442         (gnus-summary-show-thread)
6443         (gnus-summary-first-subject t)
6444         (gnus-summary-display-article (gnus-summary-article-number)))
6445     (gnus-summary-position-point)))
6446
6447 (defun gnus-summary-first-unread-subject ()
6448   "Place the point on the subject line of the first unread article.
6449 Return nil if there are no unread articles."
6450   (interactive)
6451   (prog1
6452       (when (gnus-summary-first-subject t)
6453         (gnus-summary-show-thread)
6454         (gnus-summary-first-subject t))
6455     (gnus-summary-position-point)))
6456
6457 (defun gnus-summary-first-article ()
6458   "Select the first article.
6459 Return nil if there are no articles."
6460   (interactive)
6461   (prog1
6462       (when (gnus-summary-first-subject)
6463         (gnus-summary-show-thread)
6464         (gnus-summary-first-subject)
6465         (gnus-summary-display-article (gnus-summary-article-number)))
6466     (gnus-summary-position-point)))
6467
6468 (defun gnus-summary-best-unread-article ()
6469   "Select the unread article with the highest score."
6470   (interactive)
6471   (let ((best -1000000)
6472         (data gnus-newsgroup-data)
6473         article score)
6474     (while data
6475       (and (gnus-data-unread-p (car data))
6476            (> (setq score
6477                     (gnus-summary-article-score (gnus-data-number (car data))))
6478               best)
6479            (setq best score
6480                  article (gnus-data-number (car data))))
6481       (setq data (cdr data)))
6482     (prog1
6483         (if article
6484             (gnus-summary-goto-article article)
6485           (error "No unread articles"))
6486       (gnus-summary-position-point))))
6487
6488 (defun gnus-summary-last-subject ()
6489   "Go to the last displayed subject line in the group."
6490   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6491     (when article
6492       (gnus-summary-goto-subject article))))
6493
6494 (defun gnus-summary-goto-article (article &optional all-headers force)
6495   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6496 If ALL-HEADERS is non-nil, no header lines are hidden.
6497 If FORCE, go to the article even if it isn't displayed.  If FORCE
6498 is a number, it is the line the article is to be displayed on."
6499   (interactive
6500    (list
6501     (completing-read
6502      "Article number or Message-ID: "
6503      (mapcar (lambda (number) (list (int-to-string number)))
6504              gnus-newsgroup-limit))
6505     current-prefix-arg
6506     t))
6507   (prog1
6508       (if (and (stringp article)
6509                (string-match "@" article))
6510           (gnus-summary-refer-article article)
6511         (when (stringp article)
6512           (setq article (string-to-number article)))
6513         (if (gnus-summary-goto-subject article force)
6514             (gnus-summary-display-article article all-headers)
6515           (gnus-message 4 "Couldn't go to article %s" article) nil))
6516     (gnus-summary-position-point)))
6517
6518 (defun gnus-summary-goto-last-article ()
6519   "Go to the previously read article."
6520   (interactive)
6521   (prog1
6522       (when gnus-last-article
6523         (gnus-summary-goto-article gnus-last-article nil t))
6524     (gnus-summary-position-point)))
6525
6526 (defun gnus-summary-pop-article (number)
6527   "Pop one article off the history and go to the previous.
6528 NUMBER articles will be popped off."
6529   (interactive "p")
6530   (let (to)
6531     (setq gnus-newsgroup-history
6532           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6533     (if to
6534         (gnus-summary-goto-article (car to) nil t)
6535       (error "Article history empty")))
6536   (gnus-summary-position-point))
6537
6538 ;; Summary commands and functions for limiting the summary buffer.
6539
6540 (defun gnus-summary-limit-to-articles (n)
6541   "Limit the summary buffer to the next N articles.
6542 If not given a prefix, use the process marked articles instead."
6543   (interactive "P")
6544   (prog1
6545       (let ((articles (gnus-summary-work-articles n)))
6546         (setq gnus-newsgroup-processable nil)
6547         (gnus-summary-limit articles))
6548     (gnus-summary-position-point)))
6549
6550 (defun gnus-summary-pop-limit (&optional total)
6551   "Restore the previous limit.
6552 If given a prefix, remove all limits."
6553   (interactive "P")
6554   (when total
6555     (setq gnus-newsgroup-limits
6556           (list (mapcar (lambda (h) (mail-header-number h))
6557                         gnus-newsgroup-headers))))
6558   (unless gnus-newsgroup-limits
6559     (error "No limit to pop"))
6560   (prog1
6561       (gnus-summary-limit nil 'pop)
6562     (gnus-summary-position-point)))
6563
6564 (defun gnus-summary-limit-to-subject (subject &optional header)
6565   "Limit the summary buffer to articles that have subjects that match a regexp."
6566   (interactive "sLimit to subject (regexp): ")
6567   (unless header
6568     (setq header "subject"))
6569   (when (not (equal "" subject))
6570     (prog1
6571         (let ((articles (gnus-summary-find-matching
6572                          (or header "subject") subject 'all)))
6573           (unless articles
6574             (error "Found no matches for \"%s\"" subject))
6575           (gnus-summary-limit articles))
6576       (gnus-summary-position-point))))
6577
6578 (defun gnus-summary-limit-to-author (from)
6579   "Limit the summary buffer to articles that have authors that match a regexp."
6580   (interactive "sLimit to author (regexp): ")
6581   (gnus-summary-limit-to-subject from "from"))
6582
6583 (defun gnus-summary-limit-to-age (age &optional younger-p)
6584   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6585 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6586 articles that are younger than AGE days."
6587   (interactive
6588    (let ((younger current-prefix-arg)
6589          (days-got nil)
6590          days)
6591      (while (not days-got)
6592        (setq days (if younger
6593                       (read-string "Limit to articles within (in days): ")
6594                     (read-string "Limit to articles older than (in days): ")))
6595        (when (> (length days) 0)
6596          (setq days (read days)))
6597        (if (numberp days)
6598            (setq days-got t)
6599          (message "Please enter a number.")
6600          (sleep-for 1)))
6601      (list days younger)))
6602   (prog1
6603       (let ((data gnus-newsgroup-data)
6604             (cutoff (days-to-time age))
6605             articles d date is-younger)
6606         (while (setq d (pop data))
6607           (when (and (vectorp (gnus-data-header d))
6608                      (setq date (mail-header-date (gnus-data-header d))))
6609             (setq is-younger (time-less-p
6610                               (time-since (condition-case ()
6611                                               (date-to-time date)
6612                                             (error '(0 0))))
6613                               cutoff))
6614             (when (if younger-p
6615                       is-younger
6616                     (not is-younger))
6617               (push (gnus-data-number d) articles))))
6618         (gnus-summary-limit (nreverse articles)))
6619     (gnus-summary-position-point)))
6620
6621 (defun gnus-summary-limit-to-extra (header regexp)
6622   "Limit the summary buffer to articles that match an 'extra' header."
6623   (interactive
6624    (let ((header
6625           (intern
6626            (gnus-completing-read
6627             (symbol-name (car gnus-extra-headers))
6628             "Limit extra header:"
6629             (mapcar (lambda (x)
6630                       (cons (symbol-name x) x))
6631                     gnus-extra-headers)
6632             nil
6633             t))))
6634      (list header
6635            (read-string (format "Limit to header %s (regexp): " header)))))
6636   (when (not (equal "" regexp))
6637     (prog1
6638         (let ((articles (gnus-summary-find-matching
6639                          (cons 'extra header) regexp 'all)))
6640           (unless articles
6641             (error "Found no matches for \"%s\"" regexp))
6642           (gnus-summary-limit articles))
6643       (gnus-summary-position-point))))
6644
6645 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6646 (make-obsolete
6647  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6648
6649 (defun gnus-summary-limit-to-unread (&optional all)
6650   "Limit the summary buffer to articles that are not marked as read.
6651 If ALL is non-nil, limit strictly to unread articles."
6652   (interactive "P")
6653   (if all
6654       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6655     (gnus-summary-limit-to-marks
6656      ;; Concat all the marks that say that an article is read and have
6657      ;; those removed.
6658      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6659            gnus-killed-mark gnus-kill-file-mark
6660            gnus-low-score-mark gnus-expirable-mark
6661            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6662            gnus-duplicate-mark gnus-souped-mark)
6663      'reverse)))
6664
6665 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6666 (make-obsolete 'gnus-summary-delete-marked-with
6667                'gnus-summary-limit-exlude-marks)
6668
6669 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6670   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6671 If REVERSE, limit the summary buffer to articles that are marked
6672 with MARKS.  MARKS can either be a string of marks or a list of marks.
6673 Returns how many articles were removed."
6674   (interactive "sMarks: ")
6675   (gnus-summary-limit-to-marks marks t))
6676
6677 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6678   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6679 If REVERSE (the prefix), limit the summary buffer to articles that are
6680 not marked with MARKS.  MARKS can either be a string of marks or a
6681 list of marks.
6682 Returns how many articles were removed."
6683   (interactive "sMarks: \nP")
6684   (prog1
6685       (let ((data gnus-newsgroup-data)
6686             (marks (if (listp marks) marks
6687                      (append marks nil))) ; Transform to list.
6688             articles)
6689         (while data
6690           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6691                   (memq (gnus-data-mark (car data)) marks))
6692             (push (gnus-data-number (car data)) articles))
6693           (setq data (cdr data)))
6694         (gnus-summary-limit articles))
6695     (gnus-summary-position-point)))
6696
6697 (defun gnus-summary-limit-to-score (&optional score)
6698   "Limit to articles with score at or above SCORE."
6699   (interactive "P")
6700   (setq score (if score
6701                   (prefix-numeric-value score)
6702                 (or gnus-summary-default-score 0)))
6703   (let ((data gnus-newsgroup-data)
6704         articles)
6705     (while data
6706       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6707                 score)
6708         (push (gnus-data-number (car data)) articles))
6709       (setq data (cdr data)))
6710     (prog1
6711         (gnus-summary-limit articles)
6712       (gnus-summary-position-point))))
6713
6714 (defun gnus-summary-limit-include-thread (id)
6715   "Display all the hidden articles that in the current thread."
6716   (interactive (list (mail-header-id (gnus-summary-article-header))))
6717   (let ((articles (gnus-articles-in-thread
6718                    (gnus-id-to-thread (gnus-root-id id)))))
6719     (prog1
6720         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6721       (gnus-summary-position-point))))
6722
6723 (defun gnus-summary-limit-include-dormant ()
6724   "Display all the hidden articles that are marked as dormant.
6725 Note that this command only works on a subset of the articles currently
6726 fetched for this group."
6727   (interactive)
6728   (unless gnus-newsgroup-dormant
6729     (error "There are no dormant articles in this group"))
6730   (prog1
6731       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6732     (gnus-summary-position-point)))
6733
6734 (defun gnus-summary-limit-exclude-dormant ()
6735   "Hide all dormant articles."
6736   (interactive)
6737   (prog1
6738       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6739     (gnus-summary-position-point)))
6740
6741 (defun gnus-summary-limit-exclude-childless-dormant ()
6742   "Hide all dormant articles that have no children."
6743   (interactive)
6744   (let ((data (gnus-data-list t))
6745         articles d children)
6746     ;; Find all articles that are either not dormant or have
6747     ;; children.
6748     (while (setq d (pop data))
6749       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6750                 (and (setq children
6751                            (gnus-article-children (gnus-data-number d)))
6752                      (let (found)
6753                        (while children
6754                          (when (memq (car children) articles)
6755                            (setq children nil
6756                                  found t))
6757                          (pop children))
6758                        found)))
6759         (push (gnus-data-number d) articles)))
6760     ;; Do the limiting.
6761     (prog1
6762         (gnus-summary-limit articles)
6763       (gnus-summary-position-point))))
6764
6765 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6766   "Mark all unread excluded articles as read.
6767 If ALL, mark even excluded ticked and dormants as read."
6768   (interactive "P")
6769   (let ((articles (gnus-sorted-complement
6770                    (sort
6771                     (mapcar (lambda (h) (mail-header-number h))
6772                             gnus-newsgroup-headers)
6773                     '<)
6774                    (sort gnus-newsgroup-limit '<)))
6775         article)
6776     (setq gnus-newsgroup-unreads
6777           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6778     (if all
6779         (setq gnus-newsgroup-dormant nil
6780               gnus-newsgroup-marked nil
6781               gnus-newsgroup-reads
6782               (nconc
6783                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6784                gnus-newsgroup-reads))
6785       (while (setq article (pop articles))
6786         (unless (or (memq article gnus-newsgroup-dormant)
6787                     (memq article gnus-newsgroup-marked))
6788           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6789
6790 (defun gnus-summary-limit (articles &optional pop)
6791   (if pop
6792       ;; We pop the previous limit off the stack and use that.
6793       (setq articles (car gnus-newsgroup-limits)
6794             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6795     ;; We use the new limit, so we push the old limit on the stack.
6796     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6797   ;; Set the limit.
6798   (setq gnus-newsgroup-limit articles)
6799   (let ((total (length gnus-newsgroup-data))
6800         (data (gnus-data-find-list (gnus-summary-article-number)))
6801         (gnus-summary-mark-below nil)   ; Inhibit this.
6802         found)
6803     ;; This will do all the work of generating the new summary buffer
6804     ;; according to the new limit.
6805     (gnus-summary-prepare)
6806     ;; Hide any threads, possibly.
6807     (and gnus-show-threads
6808          gnus-thread-hide-subtree
6809          (gnus-summary-hide-all-threads))
6810     ;; Try to return to the article you were at, or one in the
6811     ;; neighborhood.
6812     (when data
6813       ;; We try to find some article after the current one.
6814       (while data
6815         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6816           (setq data nil
6817                 found t))
6818         (setq data (cdr data))))
6819     (unless found
6820       ;; If there is no data, that means that we were after the last
6821       ;; article.  The same goes when we can't find any articles
6822       ;; after the current one.
6823       (goto-char (point-max))
6824       (gnus-summary-find-prev))
6825     (gnus-set-mode-line 'summary)
6826     ;; We return how many articles were removed from the summary
6827     ;; buffer as a result of the new limit.
6828     (- total (length gnus-newsgroup-data))))
6829
6830 (defsubst gnus-invisible-cut-children (threads)
6831   (let ((num 0))
6832     (while threads
6833       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6834         (incf num))
6835       (pop threads))
6836     (< num 2)))
6837
6838 (defsubst gnus-cut-thread (thread)
6839   "Go forwards in the thread until we find an article that we want to display."
6840   (when (or (eq gnus-fetch-old-headers 'some)
6841             (eq gnus-fetch-old-headers 'invisible)
6842             (numberp gnus-fetch-old-headers)
6843             (eq gnus-build-sparse-threads 'some)
6844             (eq gnus-build-sparse-threads 'more))
6845     ;; Deal with old-fetched headers and sparse threads.
6846     (while (and
6847             thread
6848             (or
6849              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6850              (gnus-summary-article-ancient-p
6851               (mail-header-number (car thread))))
6852             (if (or (<= (length (cdr thread)) 1)
6853                     (eq gnus-fetch-old-headers 'invisible))
6854                 (setq gnus-newsgroup-limit
6855                       (delq (mail-header-number (car thread))
6856                             gnus-newsgroup-limit)
6857                       thread (cadr thread))
6858               (when (gnus-invisible-cut-children (cdr thread))
6859                 (let ((th (cdr thread)))
6860                   (while th
6861                     (if (memq (mail-header-number (caar th))
6862                               gnus-newsgroup-limit)
6863                         (setq thread (car th)
6864                               th nil)
6865                       (setq th (cdr th))))))))))
6866   thread)
6867
6868 (defun gnus-cut-threads (threads)
6869   "Cut off all uninteresting articles from the beginning of threads."
6870   (when (or (eq gnus-fetch-old-headers 'some)
6871             (eq gnus-fetch-old-headers 'invisible)
6872             (numberp gnus-fetch-old-headers)
6873             (eq gnus-build-sparse-threads 'some)
6874             (eq gnus-build-sparse-threads 'more))
6875     (let ((th threads))
6876       (while th
6877         (setcar th (gnus-cut-thread (car th)))
6878         (setq th (cdr th)))))
6879   ;; Remove nixed out threads.
6880   (delq nil threads))
6881
6882 (defun gnus-summary-initial-limit (&optional show-if-empty)
6883   "Figure out what the initial limit is supposed to be on group entry.
6884 This entails weeding out unwanted dormants, low-scored articles,
6885 fetch-old-headers verbiage, and so on."
6886   ;; Most groups have nothing to remove.
6887   (if (or gnus-inhibit-limiting
6888           (and (null gnus-newsgroup-dormant)
6889                (not (eq gnus-fetch-old-headers 'some))
6890                (not (numberp gnus-fetch-old-headers))
6891                (not (eq gnus-fetch-old-headers 'invisible))
6892                (null gnus-summary-expunge-below)
6893                (not (eq gnus-build-sparse-threads 'some))
6894                (not (eq gnus-build-sparse-threads 'more))
6895                (null gnus-thread-expunge-below)
6896                (not gnus-use-nocem)))
6897       ()                                ; Do nothing.
6898     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6899     (setq gnus-newsgroup-limit nil)
6900     (mapatoms
6901      (lambda (node)
6902        (unless (car (symbol-value node))
6903          ;; These threads have no parents -- they are roots.
6904          (let ((nodes (cdr (symbol-value node)))
6905                thread)
6906            (while nodes
6907              (if (and gnus-thread-expunge-below
6908                       (< (gnus-thread-total-score (car nodes))
6909                          gnus-thread-expunge-below))
6910                  (gnus-expunge-thread (pop nodes))
6911                (setq thread (pop nodes))
6912                (gnus-summary-limit-children thread))))))
6913      gnus-newsgroup-dependencies)
6914     ;; If this limitation resulted in an empty group, we might
6915     ;; pop the previous limit and use it instead.
6916     (when (and (not gnus-newsgroup-limit)
6917                show-if-empty)
6918       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6919     gnus-newsgroup-limit))
6920
6921 (defun gnus-summary-limit-children (thread)
6922   "Return 1 if this subthread is visible and 0 if it is not."
6923   ;; First we get the number of visible children to this thread.  This
6924   ;; is done by recursing down the thread using this function, so this
6925   ;; will really go down to a leaf article first, before slowly
6926   ;; working its way up towards the root.
6927   (when thread
6928     (let ((children
6929            (if (cdr thread)
6930                (apply '+ (mapcar 'gnus-summary-limit-children
6931                                  (cdr thread)))
6932              0))
6933           (number (mail-header-number (car thread)))
6934           score)
6935       (if (and
6936            (not (memq number gnus-newsgroup-marked))
6937            (or
6938             ;; If this article is dormant and has absolutely no visible
6939             ;; children, then this article isn't visible.
6940             (and (memq number gnus-newsgroup-dormant)
6941                  (zerop children))
6942             ;; If this is "fetch-old-headered" and there is no
6943             ;; visible children, then we don't want this article.
6944             (and (or (eq gnus-fetch-old-headers 'some)
6945                      (numberp gnus-fetch-old-headers))
6946                  (gnus-summary-article-ancient-p number)
6947                  (zerop children))
6948             ;; If this is "fetch-old-headered" and `invisible', then
6949             ;; we don't want this article.
6950             (and (eq gnus-fetch-old-headers 'invisible)
6951                  (gnus-summary-article-ancient-p number))
6952             ;; If this is a sparsely inserted article with no children,
6953             ;; we don't want it.
6954             (and (eq gnus-build-sparse-threads 'some)
6955                  (gnus-summary-article-sparse-p number)
6956                  (zerop children))
6957             ;; If we use expunging, and this article is really
6958             ;; low-scored, then we don't want this article.
6959             (when (and gnus-summary-expunge-below
6960                        (< (setq score
6961                                 (or (cdr (assq number gnus-newsgroup-scored))
6962                                     gnus-summary-default-score))
6963                           gnus-summary-expunge-below))
6964               ;; We increase the expunge-tally here, but that has
6965               ;; nothing to do with the limits, really.
6966               (incf gnus-newsgroup-expunged-tally)
6967               ;; We also mark as read here, if that's wanted.
6968               (when (and gnus-summary-mark-below
6969                          (< score gnus-summary-mark-below))
6970                 (setq gnus-newsgroup-unreads
6971                       (delq number gnus-newsgroup-unreads))
6972                 (if gnus-newsgroup-auto-expire
6973                     (push number gnus-newsgroup-expirable)
6974                   (push (cons number gnus-low-score-mark)
6975                         gnus-newsgroup-reads)))
6976               t)
6977             ;; Check NoCeM things.
6978             (if (and gnus-use-nocem
6979                      (gnus-nocem-unwanted-article-p
6980                       (mail-header-id (car thread))))
6981                 (progn
6982                   (setq gnus-newsgroup-unreads
6983                         (delq number gnus-newsgroup-unreads))
6984                   t))))
6985           ;; Nope, invisible article.
6986           0
6987         ;; Ok, this article is to be visible, so we add it to the limit
6988         ;; and return 1.
6989         (push number gnus-newsgroup-limit)
6990         1))))
6991
6992 (defun gnus-expunge-thread (thread)
6993   "Mark all articles in THREAD as read."
6994   (let* ((number (mail-header-number (car thread))))
6995     (incf gnus-newsgroup-expunged-tally)
6996     ;; We also mark as read here, if that's wanted.
6997     (setq gnus-newsgroup-unreads
6998           (delq number gnus-newsgroup-unreads))
6999     (if gnus-newsgroup-auto-expire
7000         (push number gnus-newsgroup-expirable)
7001       (push (cons number gnus-low-score-mark)
7002             gnus-newsgroup-reads)))
7003   ;; Go recursively through all subthreads.
7004   (mapcar 'gnus-expunge-thread (cdr thread)))
7005
7006 ;; Summary article oriented commands
7007
7008 (defun gnus-summary-refer-parent-article (n)
7009   "Refer parent article N times.
7010 If N is negative, go to ancestor -N instead.
7011 The difference between N and the number of articles fetched is returned."
7012   (interactive "p")
7013   (let ((skip 1)
7014         error header ref)
7015     (when (not (natnump n))
7016       (setq skip (abs n)
7017             n 1))
7018     (while (and (> n 0)
7019                 (not error))
7020       (setq header (gnus-summary-article-header))
7021       (if (and (eq (mail-header-number header)
7022                    (cdr gnus-article-current))
7023                (equal gnus-newsgroup-name
7024                       (car gnus-article-current)))
7025           ;; If we try to find the parent of the currently
7026           ;; displayed article, then we take a look at the actual
7027           ;; References header, since this is slightly more
7028           ;; reliable than the References field we got from the
7029           ;; server.
7030           (save-excursion
7031             (set-buffer gnus-original-article-buffer)
7032             (nnheader-narrow-to-headers)
7033             (unless (setq ref (message-fetch-field "references"))
7034               (setq ref (message-fetch-field "in-reply-to")))
7035             (widen))
7036         (setq ref
7037               ;; It's not the current article, so we take a bet on
7038               ;; the value we got from the server.
7039               (mail-header-references header)))
7040       (if (and ref
7041                (not (equal ref "")))
7042           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7043             (gnus-message 1 "Couldn't find parent"))
7044         (gnus-message 1 "No references in article %d"
7045                       (gnus-summary-article-number))
7046         (setq error t))
7047       (decf n))
7048     (gnus-summary-position-point)
7049     n))
7050
7051 (defun gnus-summary-refer-references ()
7052   "Fetch all articles mentioned in the References header.
7053 Return the number of articles fetched."
7054   (interactive)
7055   (let ((ref (mail-header-references (gnus-summary-article-header)))
7056         (current (gnus-summary-article-number))
7057         (n 0))
7058     (if (or (not ref)
7059             (equal ref ""))
7060         (error "No References in the current article")
7061       ;; For each Message-ID in the References header...
7062       (while (string-match "<[^>]*>" ref)
7063         (incf n)
7064         ;; ... fetch that article.
7065         (gnus-summary-refer-article
7066          (prog1 (match-string 0 ref)
7067            (setq ref (substring ref (match-end 0))))))
7068       (gnus-summary-goto-subject current)
7069       (gnus-summary-position-point)
7070       n)))
7071
7072 (defun gnus-summary-refer-thread (&optional limit)
7073   "Fetch all articles in the current thread.
7074 If LIMIT (the numerical prefix), fetch that many old headers instead
7075 of what's specified by the `gnus-refer-thread-limit' variable."
7076   (interactive "P")
7077   (let ((id (mail-header-id (gnus-summary-article-header)))
7078         (limit (if limit (prefix-numeric-value limit)
7079                  gnus-refer-thread-limit)))
7080     ;; We want to fetch LIMIT *old* headers, but we also have to
7081     ;; re-fetch all the headers in the current buffer, because many of
7082     ;; them may be undisplayed.  So we adjust LIMIT.
7083     (when (numberp limit)
7084       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7085     (unless (eq gnus-fetch-old-headers 'invisible)
7086       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7087       ;; Retrieve the headers and read them in.
7088       (if (eq (gnus-retrieve-headers
7089                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7090               'nov)
7091           (gnus-build-all-threads)
7092         (error "Can't fetch thread from backends that don't support NOV"))
7093       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7094     (gnus-summary-limit-include-thread id)))
7095
7096 (defun gnus-summary-refer-article (message-id)
7097   "Fetch an article specified by MESSAGE-ID."
7098   (interactive "sMessage-ID: ")
7099   (when (and (stringp message-id)
7100              (not (zerop (length message-id))))
7101     ;; Construct the correct Message-ID if necessary.
7102     ;; Suggested by tale@pawl.rpi.edu.
7103     (unless (string-match "^<" message-id)
7104       (setq message-id (concat "<" message-id)))
7105     (unless (string-match ">$" message-id)
7106       (setq message-id (concat message-id ">")))
7107     (let* ((header (gnus-id-to-header message-id))
7108            (sparse (and header
7109                         (gnus-summary-article-sparse-p
7110                          (mail-header-number header))
7111                         (memq (mail-header-number header)
7112                               gnus-newsgroup-limit)))
7113            number)
7114       (cond
7115        ;; If the article is present in the buffer we just go to it.
7116        ((and header
7117              (or (not (gnus-summary-article-sparse-p
7118                        (mail-header-number header)))
7119                  sparse))
7120         (prog1
7121             (gnus-summary-goto-article
7122              (mail-header-number header) nil t)
7123           (when sparse
7124             (gnus-summary-update-article (mail-header-number header)))))
7125        (t
7126         ;; We fetch the article.
7127         (catch 'found
7128           (dolist (gnus-override-method (gnus-refer-article-methods))
7129             (gnus-check-server gnus-override-method)
7130             ;; Fetch the header, and display the article.
7131             (when (setq number (gnus-summary-insert-subject message-id))
7132               (gnus-summary-select-article nil nil nil number)
7133               (throw 'found t)))
7134           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7135
7136 (defun gnus-refer-article-methods ()
7137   "Return a list of referrable methods."
7138   (cond
7139    ;; No method, so we default to current and native.
7140    ((null gnus-refer-article-method)
7141     (list gnus-current-select-method gnus-select-method))
7142    ;; Current.
7143    ((eq 'current gnus-refer-article-method)
7144     (list gnus-current-select-method))
7145    ;; List of select methods.
7146    ((not (and (symbolp (car gnus-refer-article-method))
7147               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7148     (let (out)
7149       (dolist (method gnus-refer-article-method)
7150         (push (if (eq 'current method)
7151                   gnus-current-select-method
7152                 method)
7153               out))
7154       (nreverse out)))
7155    ;; One single select method.
7156    (t
7157     (list gnus-refer-article-method))))
7158
7159 (defun gnus-summary-edit-parameters ()
7160   "Edit the group parameters of the current group."
7161   (interactive)
7162   (gnus-group-edit-group gnus-newsgroup-name 'params))
7163
7164 (defun gnus-summary-customize-parameters ()
7165   "Customize the group parameters of the current group."
7166   (interactive)
7167   (gnus-group-customize gnus-newsgroup-name))
7168
7169 (defun gnus-summary-enter-digest-group (&optional force)
7170   "Enter an nndoc group based on the current article.
7171 If FORCE, force a digest interpretation.  If not, try
7172 to guess what the document format is."
7173   (interactive "P")
7174   (let ((conf gnus-current-window-configuration))
7175     (save-excursion
7176       (gnus-summary-select-article))
7177     (setq gnus-current-window-configuration conf)
7178     (let* ((name (format "%s-%d"
7179                          (gnus-group-prefixed-name
7180                           gnus-newsgroup-name (list 'nndoc ""))
7181                          (save-excursion
7182                            (set-buffer gnus-summary-buffer)
7183                            gnus-current-article)))
7184            (ogroup gnus-newsgroup-name)
7185            (params (append (gnus-info-params (gnus-get-info ogroup))
7186                            (list (cons 'to-group ogroup))
7187                            (list (cons 'save-article-group ogroup))))
7188            (case-fold-search t)
7189            (buf (current-buffer))
7190            dig to-address)
7191       (save-excursion
7192         (set-buffer gnus-original-article-buffer)
7193         ;; Have the digest group inherit the main mail address of
7194         ;; the parent article.
7195         (when (setq to-address (or (message-fetch-field "reply-to")
7196                                    (message-fetch-field "from")))
7197           (setq params (append
7198                         (list (cons 'to-address
7199                                     (funcall gnus-decode-encoded-word-function
7200                                              to-address))))))
7201         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7202         (insert-buffer-substring gnus-original-article-buffer)
7203         ;; Remove lines that may lead nndoc to misinterpret the
7204         ;; document type.
7205         (narrow-to-region
7206          (goto-char (point-min))
7207          (or (search-forward "\n\n" nil t) (point)))
7208         (goto-char (point-min))
7209         (delete-matching-lines "^Path:\\|^From ")
7210         (widen))
7211       (unwind-protect
7212           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7213                     (gnus-newsgroup-ephemeral-ignored-charsets
7214                      gnus-newsgroup-ignored-charsets))
7215                 (gnus-group-read-ephemeral-group
7216                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7217                               (nndoc-article-type
7218                                ,(if force 'mbox 'guess))) t))
7219               ;; Make all postings to this group go to the parent group.
7220               (nconc (gnus-info-params (gnus-get-info name))
7221                      params)
7222             ;; Couldn't select this doc group.
7223             (switch-to-buffer buf)
7224             (gnus-set-global-variables)
7225             (gnus-configure-windows 'summary)
7226             (gnus-message 3 "Article couldn't be entered?"))
7227         (kill-buffer dig)))))
7228
7229 (defun gnus-summary-read-document (n)
7230   "Open a new group based on the current article(s).
7231 This will allow you to read digests and other similar
7232 documents as newsgroups.
7233 Obeys the standard process/prefix convention."
7234   (interactive "P")
7235   (let* ((articles (gnus-summary-work-articles n))
7236          (ogroup gnus-newsgroup-name)
7237          (params (append (gnus-info-params (gnus-get-info ogroup))
7238                          (list (cons 'to-group ogroup))))
7239          article group egroup groups vgroup)
7240     (while (setq article (pop articles))
7241       (setq group (format "%s-%d" gnus-newsgroup-name article))
7242       (gnus-summary-remove-process-mark article)
7243       (when (gnus-summary-display-article article)
7244         (save-excursion
7245           (with-temp-buffer
7246             (insert-buffer-substring gnus-original-article-buffer)
7247             ;; Remove some headers that may lead nndoc to make
7248             ;; the wrong guess.
7249             (message-narrow-to-head)
7250             (goto-char (point-min))
7251             (delete-matching-lines "^\\(Path\\):\\|^From ")
7252             (widen)
7253             (if (setq egroup
7254                       (gnus-group-read-ephemeral-group
7255                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7256                                      (nndoc-article-type guess))
7257                        t nil t))
7258                 (progn
7259                   ;; Make all postings to this group go to the parent group.
7260                   (nconc (gnus-info-params (gnus-get-info egroup))
7261                          params)
7262                   (push egroup groups))
7263               ;; Couldn't select this doc group.
7264               (gnus-error 3 "Article couldn't be entered"))))))
7265     ;; Now we have selected all the documents.
7266     (cond
7267      ((not groups)
7268       (error "None of the articles could be interpreted as documents"))
7269      ((gnus-group-read-ephemeral-group
7270        (setq vgroup (format
7271                      "nnvirtual:%s-%s" gnus-newsgroup-name
7272                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7273        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7274        t
7275        (cons (current-buffer) 'summary)))
7276      (t
7277       (error "Couldn't select virtual nndoc group")))))
7278
7279 (defun gnus-summary-isearch-article (&optional regexp-p)
7280   "Do incremental search forward on the current article.
7281 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7282   (interactive "P")
7283   (let* ((gnus-inhibit-treatment t)
7284          (old (gnus-summary-select-article)))
7285     (gnus-configure-windows 'article)
7286     (gnus-eval-in-buffer-window gnus-article-buffer
7287       (save-restriction
7288         (widen)
7289         (when (eq 'old old)
7290           (gnus-article-show-all-headers))
7291         (goto-char (point-min))
7292         (isearch-forward regexp-p)))))
7293
7294 (defun gnus-summary-search-article-forward (regexp &optional backward)
7295   "Search for an article containing REGEXP forward.
7296 If BACKWARD, search backward instead."
7297   (interactive
7298    (list (read-string
7299           (format "Search article %s (regexp%s): "
7300                   (if current-prefix-arg "backward" "forward")
7301                   (if gnus-last-search-regexp
7302                       (concat ", default " gnus-last-search-regexp)
7303                     "")))
7304          current-prefix-arg))
7305   (if (string-equal regexp "")
7306       (setq regexp (or gnus-last-search-regexp ""))
7307     (setq gnus-last-search-regexp regexp)
7308     (setq gnus-article-before-search gnus-current-article))
7309   ;; Intentionally set gnus-last-article.
7310   (setq gnus-last-article gnus-article-before-search)
7311   (let ((gnus-last-article gnus-last-article))
7312     (if (gnus-summary-search-article regexp backward)
7313         (gnus-summary-show-thread)
7314       (error "Search failed: \"%s\"" regexp))))
7315
7316 (defun gnus-summary-search-article-backward (regexp)
7317   "Search for an article containing REGEXP backward."
7318   (interactive
7319    (list (read-string
7320           (format "Search article backward (regexp%s): "
7321                   (if gnus-last-search-regexp
7322                       (concat ", default " gnus-last-search-regexp)
7323                     "")))))
7324   (gnus-summary-search-article-forward regexp 'backward))
7325
7326 (eval-when-compile
7327   (defmacro gnus-summary-search-article-position-point (regexp backward)
7328     "Dehighlight the last matched text and goto the beginning position."
7329     (` (if (and gnus-summary-search-article-matched-data
7330                 (let ((text (caddr gnus-summary-search-article-matched-data))
7331                       (inhibit-read-only t)
7332                       buffer-read-only)
7333                   (delete-region
7334                    (goto-char (car gnus-summary-search-article-matched-data))
7335                    (cadr gnus-summary-search-article-matched-data))
7336                   (insert text)
7337                   (string-match (, regexp) text)))
7338            (if (, backward) (beginning-of-line) (end-of-line))
7339          (goto-char (if (, backward) (point-max) (point-min))))))
7340
7341   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7342     "Place point where X-Face image is displayed."
7343     (if (featurep 'xemacs)
7344         (` (let ((end (if (search-forward "\n\n" nil t)
7345                           (goto-char (1- (point)))
7346                         (point-min)))
7347                  extent)
7348              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7349              (unless (and (re-search-forward "^From:" end t)
7350                           (setq extent (extent-at (point)))
7351                           (extent-begin-glyph extent))
7352                (goto-char (, opoint)))))
7353       (` (let ((end (if (search-forward "\n\n" nil t)
7354                         (goto-char (1- (point)))
7355                       (point-min)))
7356                (start (or (search-backward "\n\n" nil t) (point-min))))
7357            (goto-char
7358             (or (text-property-any start end 'x-face-image t);; x-face-e21
7359                 (text-property-any start end 'x-face-mule-bitmap-image t)
7360                 (, opoint)))))))
7361
7362   (defmacro gnus-summary-search-article-highlight-matched-text
7363     (backward treated x-face)
7364     "Highlight matched text in the function `gnus-summary-search-article'."
7365     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7366              (end (set-marker (make-marker) (match-end 0)))
7367              (inhibit-read-only t)
7368              buffer-read-only)
7369          (unless treated
7370            (let ((,@
7371                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7372                     (mapcar
7373                      (lambda (item) (setq items (delq item items)))
7374                      '(gnus-treat-buttonize
7375                        gnus-treat-fill-article
7376                        gnus-treat-fill-long-lines
7377                        gnus-treat-emphasize
7378                        gnus-treat-highlight-headers
7379                        gnus-treat-highlight-citation
7380                        gnus-treat-highlight-signature
7381                        gnus-treat-overstrike
7382                        gnus-treat-display-xface
7383                        gnus-treat-buttonize-head
7384                        gnus-treat-decode-article-as-default-mime-charset))
7385                     (static-if (featurep 'xemacs)
7386                         items
7387                       (cons '(x-face-mule-delete-x-face-field
7388                               (quote never))
7389                             items))))
7390                  (gnus-treat-display-xface
7391                   (when (, x-face) gnus-treat-display-xface)))
7392              (gnus-article-prepare-mime-display)))
7393          (goto-char (if (, backward) start end))
7394          (when (, x-face)
7395            (gnus-summary-search-article-highlight-goto-x-face (point)))
7396          (setq gnus-summary-search-article-matched-data
7397                (list start end (buffer-substring start end)))
7398          (unless (eq start end);; matched text has been deleted. :-<
7399            (put-text-property start end 'face
7400                               (or (find-face 'isearch)
7401                                   'secondary-selection))))))
7402   )
7403
7404 (defun gnus-summary-search-article (regexp &optional backward)
7405   "Search for an article containing REGEXP.
7406 Optional argument BACKWARD means do search for backward.
7407 `gnus-select-article-hook' is not called during the search."
7408   ;; We have to require this here to make sure that the following
7409   ;; dynamic binding isn't shadowed by autoloading.
7410   (require 'gnus-async)
7411   (require 'gnus-art)
7412   (let ((gnus-select-article-hook nil)  ;Disable hook.
7413         (gnus-article-display-hook nil)
7414         (gnus-article-prepare-hook nil)
7415         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7416         (gnus-use-article-prefetch nil)
7417         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7418         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7419         (sum (current-buffer))
7420         (found nil)
7421         point treated)
7422     (gnus-save-hidden-threads
7423       (static-if (featurep 'xemacs)
7424           (let ((gnus-inhibit-treatment t))
7425             (setq treated (eq 'old (gnus-summary-select-article)))
7426             (when (and treated
7427                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7428                                  (window-live-p (get-buffer-window
7429                                                  gnus-article-buffer t)))))
7430               (gnus-summary-select-article nil t)
7431               (setq treated nil)))
7432         (let ((gnus-inhibit-treatment t)
7433               (x-face-mule-delete-x-face-field 'never))
7434           (setq treated (eq 'old (gnus-summary-select-article)))
7435           (when (and treated
7436                      (not
7437                       (and (gnus-buffer-live-p gnus-article-buffer)
7438                            (window-live-p (get-buffer-window
7439                                            gnus-article-buffer t))
7440                            (or (not (string-match "^\\^X-Face:" regexp))
7441                                (with-current-buffer gnus-article-buffer
7442                                  gnus-summary-search-article-matched-data)))))
7443             (gnus-summary-select-article nil t)
7444             (setq treated nil))))
7445       (set-buffer gnus-article-buffer)
7446       (widen)
7447       (if treated
7448           (progn
7449             (gnus-article-show-all-headers)
7450             (gnus-summary-search-article-position-point regexp backward))
7451         (goto-char (if backward (point-max) (point-min))))
7452       (while (not found)
7453         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7454         (if (if backward
7455                 (re-search-backward regexp nil t)
7456               (re-search-forward regexp nil t))
7457             ;; We found the regexp.
7458             (progn
7459               (gnus-summary-search-article-highlight-matched-text
7460                backward treated (string-match "^\\^X-Face:" regexp))
7461               (setq found 'found)
7462               (forward-line
7463                (/ (- 2 (window-height
7464                         (get-buffer-window gnus-article-buffer t)))
7465                   2))
7466               (set-window-start
7467                (get-buffer-window (current-buffer))
7468                (point))
7469               (set-buffer sum)
7470               (setq point (point)))
7471           ;; We didn't find it, so we go to the next article.
7472           (set-buffer sum)
7473           (setq found 'not)
7474           (while (eq found 'not)
7475             (if (not (if backward (gnus-summary-find-prev)
7476                        (gnus-summary-find-next)))
7477                 ;; No more articles.
7478                 (setq found t)
7479               ;; Select the next article and adjust point.
7480               (unless (gnus-summary-article-sparse-p
7481                        (gnus-summary-article-number))
7482                 (setq found nil)
7483                 (let ((gnus-inhibit-treatment t))
7484                   (gnus-summary-select-article))
7485                 (setq treated nil)
7486                 (set-buffer gnus-article-buffer)
7487                 (widen)
7488                 (goto-char (if backward (point-max) (point-min))))))))
7489       (gnus-message 7 ""))
7490     ;; Return whether we found the regexp.
7491     (when (eq found 'found)
7492       (goto-char point)
7493       (gnus-summary-show-thread)
7494       (gnus-summary-goto-subject gnus-current-article)
7495       (gnus-summary-position-point)
7496       t)))
7497
7498 (defun gnus-summary-find-matching (header regexp &optional backward unread
7499                                           not-case-fold)
7500   "Return a list of all articles that match REGEXP on HEADER.
7501 The search stars on the current article and goes forwards unless
7502 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7503 If UNREAD is non-nil, only unread articles will
7504 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7505 in the comparisons."
7506   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7507                 (gnus-data-find-list
7508                  (gnus-summary-article-number) (gnus-data-list backward))))
7509         (case-fold-search (not not-case-fold))
7510         articles d func)
7511     (if (consp header)
7512         (if (eq (car header) 'extra)
7513             (setq func
7514                   `(lambda (h)
7515                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7516                          "")))
7517           (error "%s is an invalid header" header))
7518       (unless (fboundp (intern (concat "mail-header-" header)))
7519         (error "%s is not a valid header" header))
7520       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7521     (while data
7522       (setq d (car data))
7523       (and (or (not unread)             ; We want all articles...
7524                (gnus-data-unread-p d))  ; Or just unreads.
7525            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7526            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7527            (push (gnus-data-number d) articles)) ; Success!
7528       (setq data (cdr data)))
7529     (nreverse articles)))
7530
7531 (defun gnus-summary-execute-command (header regexp command &optional backward)
7532   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7533 If HEADER is an empty string (or nil), the match is done on the entire
7534 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7535   (interactive
7536    (list (let ((completion-ignore-case t))
7537            (completing-read
7538             "Header name: "
7539             (mapcar (lambda (string) (list string))
7540                     '("Number" "Subject" "From" "Lines" "Date"
7541                       "Message-ID" "Xref" "References" "Body"))
7542             nil 'require-match))
7543          (read-string "Regexp: ")
7544          (read-key-sequence "Command: ")
7545          current-prefix-arg))
7546   (when (equal header "Body")
7547     (setq header ""))
7548   ;; Hidden thread subtrees must be searched as well.
7549   (gnus-summary-show-all-threads)
7550   ;; We don't want to change current point nor window configuration.
7551   (save-excursion
7552     (save-window-excursion
7553       (gnus-message 6 "Executing %s..." (key-description command))
7554       ;; We'd like to execute COMMAND interactively so as to give arguments.
7555       (gnus-execute header regexp
7556                     `(call-interactively ',(key-binding command))
7557                     backward)
7558       (gnus-message 6 "Executing %s...done" (key-description command)))))
7559
7560 (defun gnus-summary-beginning-of-article ()
7561   "Scroll the article back to the beginning."
7562   (interactive)
7563   (gnus-summary-select-article)
7564   (gnus-configure-windows 'article)
7565   (gnus-eval-in-buffer-window gnus-article-buffer
7566     (widen)
7567     (goto-char (point-min))
7568     (when gnus-page-broken
7569       (gnus-narrow-to-page))))
7570
7571 (defun gnus-summary-end-of-article ()
7572   "Scroll to the end of the article."
7573   (interactive)
7574   (gnus-summary-select-article)
7575   (gnus-configure-windows 'article)
7576   (gnus-eval-in-buffer-window gnus-article-buffer
7577     (widen)
7578     (goto-char (point-max))
7579     (recenter -3)
7580     (when gnus-page-broken
7581       (gnus-narrow-to-page))))
7582
7583 (defun gnus-summary-print-article (&optional filename n)
7584   "Generate and print a PostScript image of the N next (mail) articles.
7585
7586 If N is negative, print the N previous articles.  If N is nil and articles
7587 have been marked with the process mark, print these instead.
7588
7589 If the optional first argument FILENAME is nil, send the image to the
7590 printer.  If FILENAME is a string, save the PostScript image in a file with
7591 that name.  If FILENAME is a number, prompt the user for the name of the file
7592 to save in."
7593   (interactive (list (ps-print-preprint current-prefix-arg)))
7594   (dolist (article (gnus-summary-work-articles n))
7595     (gnus-summary-select-article nil nil 'pseudo article)
7596     (gnus-eval-in-buffer-window gnus-article-buffer
7597       (let ((buffer (generate-new-buffer " *print*")))
7598         (unwind-protect
7599             (progn
7600               (copy-to-buffer buffer (point-min) (point-max))
7601               (set-buffer buffer)
7602               (gnus-article-delete-invisible-text)
7603               (when (gnus-visual-p 'article-highlight 'highlight)
7604                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7605                 ;; highlight.
7606                 (let ((gnus-article-buffer buffer))
7607                   (gnus-article-highlight-citation t)
7608                   (gnus-article-highlight-signature)))
7609               (let ((ps-left-header
7610                      (list
7611                       (concat "("
7612                               (mail-header-subject gnus-current-headers) ")")
7613                       (concat "("
7614                               (mail-header-from gnus-current-headers) ")")))
7615                     (ps-right-header
7616                      (list
7617                       "/pagenumberstring load"
7618                       (concat "("
7619                               (mail-header-date gnus-current-headers) ")"))))
7620                 (gnus-run-hooks 'gnus-ps-print-hook)
7621                 (save-excursion
7622                   (ps-spool-buffer-with-faces))))
7623           (kill-buffer buffer))))
7624     (gnus-summary-remove-process-mark article))
7625   (ps-despool filename))
7626
7627 (defun gnus-summary-show-article (&optional arg)
7628   "Force re-fetching of the current article.
7629 If ARG (the prefix) is a number, show the article with the charset
7630 defined in `gnus-summary-show-article-charset-alist', or the charset
7631 inputed.
7632 If ARG (the prefix) is non-nil and not a number, show the raw article
7633 without any article massaging functions being run."
7634   (interactive "P")
7635   (cond
7636    ((numberp arg)
7637     (let ((gnus-newsgroup-charset
7638            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7639                (read-coding-system "Charset: ")))
7640           (gnus-newsgroup-ignored-charsets 'gnus-all))
7641       (gnus-summary-select-article nil 'force)
7642       (let ((deps gnus-newsgroup-dependencies)
7643             head header)
7644         (save-excursion
7645           (set-buffer gnus-original-article-buffer)
7646           (save-restriction
7647             (message-narrow-to-head)
7648             (setq head (buffer-string)))
7649           (with-temp-buffer
7650             (insert (format "211 %d Article retrieved.\n"
7651                             (cdr gnus-article-current)))
7652             (insert head)
7653             (insert ".\n")
7654             (let ((nntp-server-buffer (current-buffer)))
7655               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7656         (gnus-data-set-header
7657          (gnus-data-find (cdr gnus-article-current))
7658          header)
7659         (gnus-summary-update-article-line
7660          (cdr gnus-article-current) header))))
7661    ((not arg)
7662     ;; Select the article the normal way.
7663     (gnus-summary-select-article nil 'force))
7664    (t
7665     ;; We have to require this here to make sure that the following
7666     ;; dynamic binding isn't shadowed by autoloading.
7667     (require 'gnus-async)
7668     (require 'gnus-art)
7669     ;; Bind the article treatment functions to nil.
7670     (let ((gnus-have-all-headers t)
7671           gnus-article-display-hook
7672           gnus-article-prepare-hook
7673           gnus-article-decode-hook
7674           gnus-break-pages
7675           gnus-show-mime)
7676       (gnus-summary-select-article nil 'force))))
7677   (gnus-summary-goto-subject gnus-current-article)
7678   (gnus-summary-position-point))
7679
7680 (defun gnus-summary-verbose-headers (&optional arg)
7681   "Toggle permanent full header display.
7682 If ARG is a positive number, turn header display on.
7683 If ARG is a negative number, turn header display off."
7684   (interactive "P")
7685   (setq gnus-show-all-headers
7686         (cond ((or (not (numberp arg))
7687                    (zerop arg))
7688                (not gnus-show-all-headers))
7689               ((natnump arg)
7690                t)))
7691   (gnus-summary-show-article))
7692
7693 (defun gnus-summary-toggle-header (&optional arg)
7694   "Show the headers if they are hidden, or hide them if they are shown.
7695 If ARG is a positive number, show the entire header.
7696 If ARG is a negative number, hide the unwanted header lines."
7697   (interactive "P")
7698   (save-excursion
7699     (set-buffer gnus-article-buffer)
7700     (save-restriction
7701       (let* ((buffer-read-only nil)
7702              (inhibit-point-motion-hooks t)
7703              hidden e)
7704         (setq hidden
7705               (if (numberp arg)
7706                   (>= arg 0)
7707                 (save-restriction
7708                   (article-narrow-to-head)
7709                   (gnus-article-hidden-text-p 'headers))))
7710         (goto-char (point-min))
7711         (when (search-forward "\n\n" nil t)
7712           (delete-region (point-min) (1- (point))))
7713         (goto-char (point-min))
7714         (save-excursion
7715           (set-buffer gnus-original-article-buffer)
7716           (goto-char (point-min))
7717           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7718         (insert-buffer-substring gnus-original-article-buffer 1 e)
7719         (save-restriction
7720           (narrow-to-region (point-min) (point))
7721           (article-decode-encoded-words)
7722           (if  hidden
7723               (let ((gnus-treat-hide-headers nil)
7724                     (gnus-treat-hide-boring-headers nil))
7725                 (setq gnus-article-wash-types
7726                       (delq 'headers gnus-article-wash-types))
7727                 (gnus-treat-article 'head))
7728             (gnus-treat-article 'head)))
7729         (gnus-set-mode-line 'article)))))
7730
7731 (defun gnus-summary-show-all-headers ()
7732   "Make all header lines visible."
7733   (interactive)
7734   (gnus-article-show-all-headers))
7735
7736 (defun gnus-summary-toggle-mime (&optional arg)
7737   "Toggle MIME processing.
7738 If ARG is a positive number, turn MIME processing on."
7739   (interactive "P")
7740   (setq gnus-show-mime
7741         (if (null arg)
7742             (not gnus-show-mime)
7743           (> (prefix-numeric-value arg) 0)))
7744   (gnus-summary-select-article t 'force))
7745
7746 (defun gnus-summary-caesar-message (&optional arg)
7747   "Caesar rotate the current article by 13.
7748 The numerical prefix specifies how many places to rotate each letter
7749 forward."
7750   (interactive "P")
7751   (gnus-summary-select-article)
7752   (let ((mail-header-separator ""))
7753     (gnus-eval-in-buffer-window gnus-article-buffer
7754       (save-restriction
7755         (widen)
7756         (let ((start (window-start))
7757               buffer-read-only)
7758           (message-caesar-buffer-body arg)
7759           (set-window-start (get-buffer-window (current-buffer)) start))))))
7760
7761 (defun gnus-summary-stop-page-breaking ()
7762   "Stop page breaking in the current article."
7763   (interactive)
7764   (gnus-summary-select-article)
7765   (gnus-eval-in-buffer-window gnus-article-buffer
7766     (widen)
7767     (when (gnus-visual-p 'page-marker)
7768       (let ((buffer-read-only nil))
7769         (gnus-remove-text-with-property 'gnus-prev)
7770         (gnus-remove-text-with-property 'gnus-next))
7771       (setq gnus-page-broken nil))))
7772
7773 (defun gnus-summary-move-article (&optional n to-newsgroup
7774                                             select-method action)
7775   "Move the current article to a different newsgroup.
7776 If N is a positive number, move the N next articles.
7777 If N is a negative number, move the N previous articles.
7778 If N is nil and any articles have been marked with the process mark,
7779 move those articles instead.
7780 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7781 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7782 re-spool using this method.
7783
7784 For this function to work, both the current newsgroup and the
7785 newsgroup that you want to move to have to support the `request-move'
7786 and `request-accept' functions.
7787
7788 ACTION can be either `move' (the default), `crosspost' or `copy'."
7789   (interactive "P")
7790   (unless action
7791     (setq action 'move))
7792   ;; Disable marking as read.
7793   (let (gnus-mark-article-hook)
7794     (save-window-excursion
7795       (gnus-summary-select-article)))
7796   ;; Check whether the source group supports the required functions.
7797   (cond ((and (eq action 'move)
7798               (not (gnus-check-backend-function
7799                     'request-move-article gnus-newsgroup-name)))
7800          (error "The current group does not support article moving"))
7801         ((and (eq action 'crosspost)
7802               (not (gnus-check-backend-function
7803                     'request-replace-article gnus-newsgroup-name)))
7804          (error "The current group does not support article editing")))
7805   (let ((articles (gnus-summary-work-articles n))
7806         (prefix (if (gnus-check-backend-function
7807                     'request-move-article gnus-newsgroup-name)
7808                     (gnus-group-real-prefix gnus-newsgroup-name)
7809                   ""))
7810         (names '((move "Move" "Moving")
7811                  (copy "Copy" "Copying")
7812                  (crosspost "Crosspost" "Crossposting")))
7813         (copy-buf (save-excursion
7814                     (nnheader-set-temp-buffer " *copy article*")))
7815         (default-marks gnus-article-mark-lists)
7816         (no-expire-marks (delete '(expirable . expire)
7817                                  (copy-sequence gnus-article-mark-lists)))
7818         art-group to-method new-xref article to-groups)
7819     (unless (assq action names)
7820       (error "Unknown action %s" action))
7821     ;; Read the newsgroup name.
7822     (when (and (not to-newsgroup)
7823                (not select-method))
7824       (setq to-newsgroup
7825             (gnus-read-move-group-name
7826              (cadr (assq action names))
7827              (symbol-value (intern (format "gnus-current-%s-group" action)))
7828              articles prefix))
7829       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7830     (setq to-method (or select-method
7831                         (gnus-server-to-method
7832                          (gnus-group-method to-newsgroup))))
7833     ;; Check the method we are to move this article to...
7834     (unless (gnus-check-backend-function
7835              'request-accept-article (car to-method))
7836       (error "%s does not support article copying" (car to-method)))
7837     (unless (gnus-check-server to-method)
7838       (error "Can't open server %s" (car to-method)))
7839     (gnus-message 6 "%s to %s: %s..."
7840                   (caddr (assq action names))
7841                   (or (car select-method) to-newsgroup) articles)
7842     (while articles
7843       (setq article (pop articles))
7844       (setq
7845        art-group
7846        (cond
7847         ;; Move the article.
7848         ((eq action 'move)
7849          ;; Remove this article from future suppression.
7850          (gnus-dup-unsuppress-article article)
7851          (gnus-request-move-article
7852           article                       ; Article to move
7853           gnus-newsgroup-name           ; From newsgroup
7854           (nth 1 (gnus-find-method-for-group
7855                   gnus-newsgroup-name)) ; Server
7856           (list 'gnus-request-accept-article
7857                 to-newsgroup (list 'quote select-method)
7858                 (not articles) t)       ; Accept form
7859           (not articles)))              ; Only save nov last time
7860         ;; Copy the article.
7861         ((eq action 'copy)
7862          (save-excursion
7863            (set-buffer copy-buf)
7864            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7865              (gnus-request-accept-article
7866               to-newsgroup select-method (not articles) t))))
7867         ;; Crosspost the article.
7868         ((eq action 'crosspost)
7869          (let ((xref (message-tokenize-header
7870                       (mail-header-xref (gnus-summary-article-header article))
7871                       " ")))
7872            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7873                                   ":" article))
7874            (unless xref
7875              (setq xref (list (system-name))))
7876            (setq new-xref
7877                  (concat
7878                   (mapconcat 'identity
7879                              (delete "Xref:" (delete new-xref xref))
7880                              " ")
7881                   " " new-xref))
7882            (save-excursion
7883              (set-buffer copy-buf)
7884              ;; First put the article in the destination group.
7885              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7886              (when (consp (setq art-group
7887                                 (gnus-request-accept-article
7888                                  to-newsgroup select-method (not articles))))
7889                (setq new-xref (concat new-xref " " (car art-group)
7890                                       ":" (cdr art-group)))
7891                ;; Now we have the new Xrefs header, so we insert
7892                ;; it and replace the new article.
7893                (nnheader-replace-header "Xref" new-xref)
7894                (gnus-request-replace-article
7895                 (cdr art-group) to-newsgroup (current-buffer))
7896                art-group))))))
7897       (cond
7898        ((not art-group)
7899         (gnus-message 1 "Couldn't %s article %s: %s"
7900                       (cadr (assq action names)) article
7901                       (nnheader-get-report (car to-method))))
7902        ((eq art-group 'junk)
7903         (when (eq action 'move)
7904           (gnus-summary-mark-article article gnus-canceled-mark)
7905           (gnus-message 4 "Deleted article %s" article)))
7906        (t
7907         (let* ((pto-group (gnus-group-prefixed-name
7908                            (car art-group) to-method))
7909                (entry
7910                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7911                (info (nth 2 entry))
7912                (to-group (gnus-info-group info))
7913                to-marks)
7914           ;; Update the group that has been moved to.
7915           (when (and info
7916                      (memq action '(move copy)))
7917             (unless (member to-group to-groups)
7918               (push to-group to-groups))
7919
7920             (unless (memq article gnus-newsgroup-unreads)
7921               (push 'read to-marks)
7922               (gnus-info-set-read
7923                info (gnus-add-to-range (gnus-info-read info)
7924                                        (list (cdr art-group)))))
7925
7926             ;; See whether the article is to be put in the cache.
7927             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7928                              default-marks
7929                            no-expire-marks))
7930                   (to-article (cdr art-group)))
7931
7932               ;; Enter the article into the cache in the new group,
7933               ;; if that is required.
7934               (when gnus-use-cache
7935                 (gnus-cache-possibly-enter-article
7936                  to-group to-article
7937                  (let ((header (copy-sequence
7938                                 (gnus-summary-article-header article))))
7939                    (mail-header-set-number header to-article)
7940                    header)
7941                  (memq article gnus-newsgroup-marked)
7942                  (memq article gnus-newsgroup-dormant)
7943                  (memq article gnus-newsgroup-unreads)))
7944
7945               (when gnus-preserve-marks
7946                 ;; Copy any marks over to the new group.
7947                 (when (and (equal to-group gnus-newsgroup-name)
7948                            (not (memq article gnus-newsgroup-unreads)))
7949                   ;; Mark this article as read in this group.
7950                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7951                   (setcdr (gnus-active to-group) to-article)
7952                   (setcdr gnus-newsgroup-active to-article))
7953
7954                 (while marks
7955                   (when (memq article (symbol-value
7956                                        (intern (format "gnus-newsgroup-%s"
7957                                                        (caar marks)))))
7958                     (push (cdar marks) to-marks)
7959                     ;; If the other group is the same as this group,
7960                     ;; then we have to add the mark to the list.
7961                     (when (equal to-group gnus-newsgroup-name)
7962                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7963                            (cons to-article
7964                                  (symbol-value
7965                                   (intern (format "gnus-newsgroup-%s"
7966                                                   (caar marks)))))))
7967                     ;; Copy the marks to other group.
7968                     (gnus-add-marked-articles
7969                      to-group (cdar marks) (list to-article) info))
7970                   (setq marks (cdr marks)))
7971
7972                 (gnus-request-set-mark to-group (list (list (list to-article)
7973                                                             'set
7974                                                             to-marks))))
7975
7976               (gnus-dribble-enter
7977                (concat "(gnus-group-set-info '"
7978                        (gnus-prin1-to-string (gnus-get-info to-group))
7979                        ")"))))
7980
7981           ;; Update the Xref header in this article to point to
7982           ;; the new crossposted article we have just created.
7983           (when (eq action 'crosspost)
7984             (save-excursion
7985               (set-buffer copy-buf)
7986               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7987               (nnheader-replace-header "Xref" new-xref)
7988               (gnus-request-replace-article
7989                article gnus-newsgroup-name (current-buffer)))))
7990
7991         ;;;!!!Why is this necessary?
7992         (set-buffer gnus-summary-buffer)
7993
7994         (gnus-summary-goto-subject article)
7995         (when (eq action 'move)
7996           (gnus-summary-mark-article article gnus-canceled-mark))))
7997       (gnus-summary-remove-process-mark article))
7998     ;; Re-activate all groups that have been moved to.
7999     (while to-groups
8000       (save-excursion
8001         (set-buffer gnus-group-buffer)
8002         (when (gnus-group-goto-group (car to-groups) t)
8003           (gnus-group-get-new-news-this-group 1 t))
8004         (pop to-groups)))
8005
8006     (gnus-kill-buffer copy-buf)
8007     (gnus-summary-position-point)
8008     (gnus-set-mode-line 'summary)))
8009
8010 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8011   "Move the current article to a different newsgroup.
8012 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8013 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8014 re-spool using this method."
8015   (interactive "P")
8016   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8017
8018 (defun gnus-summary-crosspost-article (&optional n)
8019   "Crosspost the current article to some other group."
8020   (interactive "P")
8021   (gnus-summary-move-article n nil nil 'crosspost))
8022
8023 (defcustom gnus-summary-respool-default-method nil
8024   "Default method for respooling an article.
8025 If nil, use to the current newsgroup method."
8026   :type '(choice (gnus-select-method :value (nnml ""))
8027                  (const nil))
8028   :group 'gnus-summary-mail)
8029
8030 (defun gnus-summary-respool-article (&optional n method)
8031   "Respool the current article.
8032 The article will be squeezed through the mail spooling process again,
8033 which means that it will be put in some mail newsgroup or other
8034 depending on `nnmail-split-methods'.
8035 If N is a positive number, respool the N next articles.
8036 If N is a negative number, respool the N previous articles.
8037 If N is nil and any articles have been marked with the process mark,
8038 respool those articles instead.
8039
8040 Respooling can be done both from mail groups and \"real\" newsgroups.
8041 In the former case, the articles in question will be moved from the
8042 current group into whatever groups they are destined to.  In the
8043 latter case, they will be copied into the relevant groups."
8044   (interactive
8045    (list current-prefix-arg
8046          (let* ((methods (gnus-methods-using 'respool))
8047                 (methname
8048                  (symbol-name (or gnus-summary-respool-default-method
8049                                   (car (gnus-find-method-for-group
8050                                         gnus-newsgroup-name)))))
8051                 (method
8052                  (gnus-completing-read
8053                   methname "What backend do you want to use when respooling?"
8054                   methods nil t nil 'gnus-mail-method-history))
8055                 ms)
8056            (cond
8057             ((zerop (length (setq ms (gnus-servers-using-backend
8058                                       (intern method)))))
8059              (list (intern method) ""))
8060             ((= 1 (length ms))
8061              (car ms))
8062             (t
8063              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8064                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8065                            ms-alist))))))))
8066   (unless method
8067     (error "No method given for respooling"))
8068   (if (assoc (symbol-name
8069               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8070              (gnus-methods-using 'respool))
8071       (gnus-summary-move-article n nil method)
8072     (gnus-summary-copy-article n nil method)))
8073
8074 (defun gnus-summary-import-article (file)
8075   "Import an arbitrary file into a mail newsgroup."
8076   (interactive "fImport file: ")
8077   (let ((group gnus-newsgroup-name)
8078         (now (current-time))
8079         atts lines)
8080     (unless (gnus-check-backend-function 'request-accept-article group)
8081       (error "%s does not support article importing" group))
8082     (or (file-readable-p file)
8083         (not (file-regular-p file))
8084         (error "Can't read %s" file))
8085     (save-excursion
8086       (set-buffer (gnus-get-buffer-create " *import file*"))
8087       (erase-buffer)
8088       (nnheader-insert-file-contents file)
8089       (goto-char (point-min))
8090       (unless (nnheader-article-p)
8091         ;; This doesn't look like an article, so we fudge some headers.
8092         (setq atts (file-attributes file)
8093               lines (count-lines (point-min) (point-max)))
8094         (insert "From: " (read-string "From: ") "\n"
8095                 "Subject: " (read-string "Subject: ") "\n"
8096                 "Date: " (message-make-date (nth 5 atts))
8097                 "\n"
8098                 "Message-ID: " (message-make-message-id) "\n"
8099                 "Lines: " (int-to-string lines) "\n"
8100                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8101       (gnus-request-accept-article group nil t)
8102       (kill-buffer (current-buffer)))))
8103
8104 (defun gnus-summary-article-posted-p ()
8105   "Say whether the current (mail) article is available from news as well.
8106 This will be the case if the article has both been mailed and posted."
8107   (interactive)
8108   (let ((id (mail-header-references (gnus-summary-article-header)))
8109         (gnus-override-method (car (gnus-refer-article-methods))))
8110     (if (gnus-request-head id "")
8111         (gnus-message 2 "The current message was found on %s"
8112                       gnus-override-method)
8113       (gnus-message 2 "The current message couldn't be found on %s"
8114                     gnus-override-method)
8115       nil)))
8116
8117 (defun gnus-summary-expire-articles (&optional now)
8118   "Expire all articles that are marked as expirable in the current group."
8119   (interactive)
8120   (when (gnus-check-backend-function
8121          'request-expire-articles gnus-newsgroup-name)
8122     ;; This backend supports expiry.
8123     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8124            (expirable (if total
8125                           (progn
8126                             ;; We need to update the info for
8127                             ;; this group for `gnus-list-of-read-articles'
8128                             ;; to give us the right answer.
8129                             (gnus-run-hooks 'gnus-exit-group-hook)
8130                             (gnus-summary-update-info)
8131                             (gnus-list-of-read-articles gnus-newsgroup-name))
8132                         (setq gnus-newsgroup-expirable
8133                               (sort gnus-newsgroup-expirable '<))))
8134            (expiry-wait (if now 'immediate
8135                           (gnus-group-find-parameter
8136                            gnus-newsgroup-name 'expiry-wait)))
8137            (nnmail-expiry-target
8138             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8139                 nnmail-expiry-target))
8140            es)
8141       (when expirable
8142         ;; There are expirable articles in this group, so we run them
8143         ;; through the expiry process.
8144         (gnus-message 6 "Expiring articles...")
8145         (unless (gnus-check-group gnus-newsgroup-name)
8146           (error "Can't open server for %s" gnus-newsgroup-name))
8147         ;; The list of articles that weren't expired is returned.
8148         (save-excursion
8149           (if expiry-wait
8150               (let ((nnmail-expiry-wait-function nil)
8151                     (nnmail-expiry-wait expiry-wait))
8152                 (setq es (gnus-request-expire-articles
8153                           expirable gnus-newsgroup-name)))
8154             (setq es (gnus-request-expire-articles
8155                       expirable gnus-newsgroup-name)))
8156           (unless total
8157             (setq gnus-newsgroup-expirable es))
8158           ;; We go through the old list of expirable, and mark all
8159           ;; really expired articles as nonexistent.
8160           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8161             (let ((gnus-use-cache nil))
8162               (while expirable
8163                 (unless (memq (car expirable) es)
8164                   (when (gnus-data-find (car expirable))
8165                     (gnus-summary-mark-article
8166                      (car expirable) gnus-canceled-mark)))
8167                 (setq expirable (cdr expirable))))))
8168         (gnus-message 6 "Expiring articles...done")))))
8169
8170 (defun gnus-summary-expire-articles-now ()
8171   "Expunge all expirable articles in the current group.
8172 This means that *all* articles that are marked as expirable will be
8173 deleted forever, right now."
8174   (interactive)
8175   (or gnus-expert-user
8176       (gnus-yes-or-no-p
8177        "Are you really, really, really sure you want to delete all these messages? ")
8178       (error "Phew!"))
8179   (gnus-summary-expire-articles t))
8180
8181 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8182 (defun gnus-summary-delete-article (&optional n)
8183   "Delete the N next (mail) articles.
8184 This command actually deletes articles.  This is not a marking
8185 command.  The article will disappear forever from your life, never to
8186 return.
8187 If N is negative, delete backwards.
8188 If N is nil and articles have been marked with the process mark,
8189 delete these instead."
8190   (interactive "P")
8191   (unless (gnus-check-backend-function 'request-expire-articles
8192                                        gnus-newsgroup-name)
8193     (error "The current newsgroup does not support article deletion"))
8194   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8195     (error "Couldn't open server"))
8196   ;; Compute the list of articles to delete.
8197   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8198         not-deleted)
8199     (if (and gnus-novice-user
8200              (not (gnus-yes-or-no-p
8201                    (format "Do you really want to delete %s forever? "
8202                            (if (> (length articles) 1)
8203                                (format "these %s articles" (length articles))
8204                              "this article")))))
8205         ()
8206       ;; Delete the articles.
8207       (setq not-deleted (gnus-request-expire-articles
8208                          articles gnus-newsgroup-name 'force))
8209       (while articles
8210         (gnus-summary-remove-process-mark (car articles))
8211         ;; The backend might not have been able to delete the article
8212         ;; after all.
8213         (unless (memq (car articles) not-deleted)
8214           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8215         (setq articles (cdr articles)))
8216       (when not-deleted
8217         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8218     (gnus-summary-position-point)
8219     (gnus-set-mode-line 'summary)
8220     not-deleted))
8221
8222 (defun gnus-summary-edit-article (&optional force)
8223   "Edit the current article.
8224 This will have permanent effect only in mail groups.
8225 If FORCE is non-nil, allow editing of articles even in read-only
8226 groups."
8227   (interactive "P")
8228   (save-excursion
8229     (set-buffer gnus-summary-buffer)
8230     (let ((mail-parse-charset gnus-newsgroup-charset)
8231           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8232       (gnus-set-global-variables)
8233       (when (and (not force)
8234                  (gnus-group-read-only-p))
8235         (error "The current newsgroup does not support article editing"))
8236       (gnus-summary-show-article t)
8237       (gnus-article-edit-article
8238        'ignore
8239        `(lambda (no-highlight)
8240           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8241                 (message-options message-options)
8242                 (message-options-set-recipient)
8243                 (mail-parse-ignored-charsets
8244                  ',gnus-newsgroup-ignored-charsets))
8245             (gnus-summary-edit-article-done
8246              ,(or (mail-header-references gnus-current-headers) "")
8247              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8248
8249 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8250
8251 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8252                                                  no-highlight)
8253   "Make edits to the current article permanent."
8254   (interactive)
8255   (save-excursion
8256     ;; The buffer restriction contains the entire article if it exists.
8257     (when (article-goto-body)
8258       (let ((lines (count-lines (point) (point-max)))
8259             (length (- (point-max) (point)))
8260             (case-fold-search t)
8261             (body (copy-marker (point))))
8262         (goto-char (point-min))
8263         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8264           (delete-region (match-beginning 1) (match-end 1))
8265           (insert (number-to-string length)))
8266         (goto-char (point-min))
8267         (when (re-search-forward
8268                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8269           (delete-region (match-beginning 1) (match-end 1))
8270           (insert (number-to-string length)))
8271         (goto-char (point-min))
8272         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8273           (delete-region (match-beginning 1) (match-end 1))
8274           (insert (number-to-string lines))))))
8275   ;; Replace the article.
8276   (let ((buf (current-buffer)))
8277     (with-temp-buffer
8278       (insert-buffer-substring buf)
8279
8280       (if (and (not read-only)
8281                (not (gnus-request-replace-article
8282                      (cdr gnus-article-current) (car gnus-article-current)
8283                      (current-buffer) t)))
8284           (error "Couldn't replace article")
8285         ;; Update the summary buffer.
8286         (if (and references
8287                  (equal (message-tokenize-header references " ")
8288                         (message-tokenize-header
8289                          (or (message-fetch-field "references") "") " ")))
8290             ;; We only have to update this line.
8291             (save-excursion
8292               (save-restriction
8293                 (message-narrow-to-head)
8294                 (let ((head (buffer-string))
8295                       header)
8296                   (with-temp-buffer
8297                     (insert (format "211 %d Article retrieved.\n"
8298                                     (cdr gnus-article-current)))
8299                     (insert head)
8300                     (insert ".\n")
8301                     (let ((nntp-server-buffer (current-buffer)))
8302                       (setq header (car (gnus-get-newsgroup-headers
8303                                          (save-excursion
8304                                            (set-buffer gnus-summary-buffer)
8305                                            gnus-newsgroup-dependencies)
8306                                          t))))
8307                     (save-excursion
8308                       (set-buffer gnus-summary-buffer)
8309                       (gnus-data-set-header
8310                        (gnus-data-find (cdr gnus-article-current))
8311                        header)
8312                       (gnus-summary-update-article-line
8313                        (cdr gnus-article-current) header))))))
8314           ;; Update threads.
8315           (set-buffer (or buffer gnus-summary-buffer))
8316           (gnus-summary-update-article (cdr gnus-article-current)))
8317         ;; Prettify the article buffer again.
8318         (unless no-highlight
8319           (save-excursion
8320             (set-buffer gnus-article-buffer)
8321             ;;;!!! Fix this -- article should be rehighlighted.
8322             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8323             (set-buffer gnus-original-article-buffer)
8324             (gnus-request-article
8325              (cdr gnus-article-current)
8326              (car gnus-article-current) (current-buffer))))
8327         ;; Prettify the summary buffer line.
8328         (when (gnus-visual-p 'summary-highlight 'highlight)
8329           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8330
8331 (defun gnus-summary-edit-wash (key)
8332   "Perform editing command KEY in the article buffer."
8333   (interactive
8334    (list
8335     (progn
8336       (message "%s" (concat (this-command-keys) "- "))
8337       (read-char))))
8338   (message "")
8339   (gnus-summary-edit-article)
8340   (execute-kbd-macro (concat (this-command-keys) key))
8341   (gnus-article-edit-done))
8342
8343 ;;; Respooling
8344
8345 (defun gnus-summary-respool-query (&optional silent trace)
8346   "Query where the respool algorithm would put this article."
8347   (interactive)
8348   (let (gnus-mark-article-hook)
8349     (gnus-summary-select-article)
8350     (save-excursion
8351       (set-buffer gnus-original-article-buffer)
8352       (save-restriction
8353         (message-narrow-to-head)
8354         (let ((groups (nnmail-article-group 'identity trace)))
8355           (unless silent
8356             (if groups
8357                 (message "This message would go to %s"
8358                          (mapconcat 'car groups ", "))
8359               (message "This message would go to no groups"))
8360             groups))))))
8361
8362 (defun gnus-summary-respool-trace ()
8363   "Trace where the respool algorithm would put this article.
8364 Display a buffer showing all fancy splitting patterns which matched."
8365   (interactive)
8366   (gnus-summary-respool-query nil t))
8367
8368 ;; Summary marking commands.
8369
8370 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8371   "Mark articles which has the same subject as read, and then select the next.
8372 If UNMARK is positive, remove any kind of mark.
8373 If UNMARK is negative, tick articles."
8374   (interactive "P")
8375   (when unmark
8376     (setq unmark (prefix-numeric-value unmark)))
8377   (let ((count
8378          (gnus-summary-mark-same-subject
8379           (gnus-summary-article-subject) unmark)))
8380     ;; Select next unread article.  If auto-select-same mode, should
8381     ;; select the first unread article.
8382     (gnus-summary-next-article t (and gnus-auto-select-same
8383                                       (gnus-summary-article-subject)))
8384     (gnus-message 7 "%d article%s marked as %s"
8385                   count (if (= count 1) " is" "s are")
8386                   (if unmark "unread" "read"))))
8387
8388 (defun gnus-summary-kill-same-subject (&optional unmark)
8389   "Mark articles which has the same subject as read.
8390 If UNMARK is positive, remove any kind of mark.
8391 If UNMARK is negative, tick articles."
8392   (interactive "P")
8393   (when unmark
8394     (setq unmark (prefix-numeric-value unmark)))
8395   (let ((count
8396          (gnus-summary-mark-same-subject
8397           (gnus-summary-article-subject) unmark)))
8398     ;; If marked as read, go to next unread subject.
8399     (when (null unmark)
8400       ;; Go to next unread subject.
8401       (gnus-summary-next-subject 1 t))
8402     (gnus-message 7 "%d articles are marked as %s"
8403                   count (if unmark "unread" "read"))))
8404
8405 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8406   "Mark articles with same SUBJECT as read, and return marked number.
8407 If optional argument UNMARK is positive, remove any kinds of marks.
8408 If optional argument UNMARK is negative, mark articles as unread instead."
8409   (let ((count 1))
8410     (save-excursion
8411       (cond
8412        ((null unmark)                   ; Mark as read.
8413         (while (and
8414                 (progn
8415                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8416                   (gnus-summary-show-thread) t)
8417                 (gnus-summary-find-subject subject))
8418           (setq count (1+ count))))
8419        ((> unmark 0)                    ; Tick.
8420         (while (and
8421                 (progn
8422                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8423                   (gnus-summary-show-thread) t)
8424                 (gnus-summary-find-subject subject))
8425           (setq count (1+ count))))
8426        (t                               ; Mark as unread.
8427         (while (and
8428                 (progn
8429                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8430                   (gnus-summary-show-thread) t)
8431                 (gnus-summary-find-subject subject))
8432           (setq count (1+ count)))))
8433       (gnus-set-mode-line 'summary)
8434       ;; Return the number of marked articles.
8435       count)))
8436
8437 (defun gnus-summary-mark-as-processable (n &optional unmark)
8438   "Set the process mark on the next N articles.
8439 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8440 the process mark instead.  The difference between N and the actual
8441 number of articles marked is returned."
8442   (interactive "P")
8443   (if (and (null n) (gnus-region-active-p))
8444       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8445     (setq n (prefix-numeric-value n))
8446     (let ((backward (< n 0))
8447           (n (abs n)))
8448       (while (and
8449               (> n 0)
8450               (if unmark
8451                 (gnus-summary-remove-process-mark
8452                  (gnus-summary-article-number))
8453                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8454               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8455         (setq n (1- n)))
8456       (when (/= 0 n)
8457         (gnus-message 7 "No more articles"))
8458       (gnus-summary-recenter)
8459       (gnus-summary-position-point)
8460       n)))
8461
8462 (defun gnus-summary-unmark-as-processable (n)
8463   "Remove the process mark from the next N articles.
8464 If N is negative, unmark backward instead.  The difference between N and
8465 the actual number of articles unmarked is returned."
8466   (interactive "P")
8467   (gnus-summary-mark-as-processable n t))
8468
8469 (defun gnus-summary-unmark-all-processable ()
8470   "Remove the process mark from all articles."
8471   (interactive)
8472   (save-excursion
8473     (while gnus-newsgroup-processable
8474       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8475   (gnus-summary-position-point))
8476
8477 (defun gnus-summary-add-mark (article type)
8478   "Mark ARTICLE with a mark of TYPE."
8479   (let ((vtype (car (assq type gnus-article-mark-lists)))
8480         var)
8481     (if (not vtype)
8482         (error "No such mark type: %s" type)
8483       (setq var (intern (format "gnus-newsgroup-%s" type)))
8484       (set var (cons article (symbol-value var)))
8485       (if (memq type '(processable cached replied saved))
8486           (gnus-summary-update-secondary-mark article)
8487         ;;; !!! This is bobus.  We should find out what primary
8488         ;;; !!! mark we want to set.
8489         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8490
8491 (defun gnus-summary-mark-as-expirable (n)
8492   "Mark N articles forward as expirable.
8493 If N is negative, mark backward instead.  The difference between N and
8494 the actual number of articles marked is returned."
8495   (interactive "p")
8496   (gnus-summary-mark-forward n gnus-expirable-mark))
8497
8498 (defun gnus-summary-mark-article-as-replied (article)
8499   "Mark ARTICLE replied and update the summary line."
8500   (push article gnus-newsgroup-replied)
8501   (let ((buffer-read-only nil))
8502     (when (gnus-summary-goto-subject article nil t)
8503       (gnus-summary-update-secondary-mark article))))
8504
8505 (defun gnus-summary-set-bookmark (article)
8506   "Set a bookmark in current article."
8507   (interactive (list (gnus-summary-article-number)))
8508   (when (or (not (get-buffer gnus-article-buffer))
8509             (not gnus-current-article)
8510             (not gnus-article-current)
8511             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8512     (error "No current article selected"))
8513   ;; Remove old bookmark, if one exists.
8514   (let ((old (assq article gnus-newsgroup-bookmarks)))
8515     (when old
8516       (setq gnus-newsgroup-bookmarks
8517             (delq old gnus-newsgroup-bookmarks))))
8518   ;; Set the new bookmark, which is on the form
8519   ;; (article-number . line-number-in-body).
8520   (push
8521    (cons article
8522          (save-excursion
8523            (set-buffer gnus-article-buffer)
8524            (count-lines
8525             (min (point)
8526                  (save-excursion
8527                    (goto-char (point-min))
8528                    (search-forward "\n\n" nil t)
8529                    (point)))
8530             (point))))
8531    gnus-newsgroup-bookmarks)
8532   (gnus-message 6 "A bookmark has been added to the current article."))
8533
8534 (defun gnus-summary-remove-bookmark (article)
8535   "Remove the bookmark from the current article."
8536   (interactive (list (gnus-summary-article-number)))
8537   ;; Remove old bookmark, if one exists.
8538   (let ((old (assq article gnus-newsgroup-bookmarks)))
8539     (if old
8540         (progn
8541           (setq gnus-newsgroup-bookmarks
8542                 (delq old gnus-newsgroup-bookmarks))
8543           (gnus-message 6 "Removed bookmark."))
8544       (gnus-message 6 "No bookmark in current article."))))
8545
8546 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8547 (defun gnus-summary-mark-as-dormant (n)
8548   "Mark N articles forward as dormant.
8549 If N is negative, mark backward instead.  The difference between N and
8550 the actual number of articles marked is returned."
8551   (interactive "p")
8552   (gnus-summary-mark-forward n gnus-dormant-mark))
8553
8554 (defun gnus-summary-set-process-mark (article)
8555   "Set the process mark on ARTICLE and update the summary line."
8556   (setq gnus-newsgroup-processable
8557         (cons article
8558               (delq article gnus-newsgroup-processable)))
8559   (when (gnus-summary-goto-subject article)
8560     (gnus-summary-show-thread)
8561     (gnus-summary-goto-subject article)
8562     (gnus-summary-update-secondary-mark article)))
8563
8564 (defun gnus-summary-remove-process-mark (article)
8565   "Remove the process mark from ARTICLE and update the summary line."
8566   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8567   (when (gnus-summary-goto-subject article)
8568     (gnus-summary-show-thread)
8569     (gnus-summary-goto-subject article)
8570     (gnus-summary-update-secondary-mark article)))
8571
8572 (defun gnus-summary-set-saved-mark (article)
8573   "Set the process mark on ARTICLE and update the summary line."
8574   (push article gnus-newsgroup-saved)
8575   (when (gnus-summary-goto-subject article)
8576     (gnus-summary-update-secondary-mark article)))
8577
8578 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8579   "Mark N articles as read forwards.
8580 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8581 The difference between N and the actual number of articles marked is
8582 returned.
8583 Iff NO-EXPIRE, auto-expiry will be inhibited."
8584   (interactive "p")
8585   (gnus-summary-show-thread)
8586   (let ((backward (< n 0))
8587         (gnus-summary-goto-unread
8588          (and gnus-summary-goto-unread
8589               (not (eq gnus-summary-goto-unread 'never))
8590               (not (memq mark (list gnus-unread-mark
8591                                     gnus-ticked-mark gnus-dormant-mark)))))
8592         (n (abs n))
8593         (mark (or mark gnus-del-mark)))
8594     (while (and (> n 0)
8595                 (gnus-summary-mark-article nil mark no-expire)
8596                 (zerop (gnus-summary-next-subject
8597                         (if backward -1 1)
8598                         (and gnus-summary-goto-unread
8599                              (not (eq gnus-summary-goto-unread 'never)))
8600                         t)))
8601       (setq n (1- n)))
8602     (when (/= 0 n)
8603       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8604     (gnus-summary-recenter)
8605     (gnus-summary-position-point)
8606     (gnus-set-mode-line 'summary)
8607     n))
8608
8609 (defun gnus-summary-mark-article-as-read (mark)
8610   "Mark the current article quickly as read with MARK."
8611   (let ((article (gnus-summary-article-number)))
8612     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8613     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8614     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8615     (push (cons article mark) gnus-newsgroup-reads)
8616     ;; Possibly remove from cache, if that is used.
8617     (when gnus-use-cache
8618       (gnus-cache-enter-remove-article article))
8619     ;; Allow the backend to change the mark.
8620     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8621     ;; Check for auto-expiry.
8622     (when (and gnus-newsgroup-auto-expire
8623                (memq mark gnus-auto-expirable-marks))
8624       (setq mark gnus-expirable-mark)
8625       ;; Let the backend know about the mark change.
8626       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8627       (push article gnus-newsgroup-expirable))
8628     ;; Set the mark in the buffer.
8629     (gnus-summary-update-mark mark 'unread)
8630     t))
8631
8632 (defun gnus-summary-mark-article-as-unread (mark)
8633   "Mark the current article quickly as unread with MARK."
8634   (let* ((article (gnus-summary-article-number))
8635          (old-mark (gnus-summary-article-mark article)))
8636     ;; Allow the backend to change the mark.
8637     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8638     (if (eq mark old-mark)
8639         t
8640       (if (<= article 0)
8641           (progn
8642             (gnus-error 1 "Can't mark negative article numbers")
8643             nil)
8644         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8645         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8646         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8647         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8648         (cond ((= mark gnus-ticked-mark)
8649                (push article gnus-newsgroup-marked))
8650               ((= mark gnus-dormant-mark)
8651                (push article gnus-newsgroup-dormant))
8652               (t
8653                (push article gnus-newsgroup-unreads)))
8654         (gnus-pull article gnus-newsgroup-reads)
8655
8656         ;; See whether the article is to be put in the cache.
8657         (and gnus-use-cache
8658              (vectorp (gnus-summary-article-header article))
8659              (save-excursion
8660                (gnus-cache-possibly-enter-article
8661                 gnus-newsgroup-name article
8662                 (gnus-summary-article-header article)
8663                 (= mark gnus-ticked-mark)
8664                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8665
8666         ;; Fix the mark.
8667         (gnus-summary-update-mark mark 'unread)
8668         t))))
8669
8670 (defun gnus-summary-mark-article (&optional article mark no-expire)
8671   "Mark ARTICLE with MARK.  MARK can be any character.
8672 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8673 `??' (dormant) and `?E' (expirable).
8674 If MARK is nil, then the default character `?r' is used.
8675 If ARTICLE is nil, then the article on the current line will be
8676 marked.
8677 Iff NO-EXPIRE, auto-expiry will be inhibited."
8678   ;; The mark might be a string.
8679   (when (stringp mark)
8680     (setq mark (aref mark 0)))
8681   ;; If no mark is given, then we check auto-expiring.
8682   (when (null mark)
8683     (setq mark gnus-del-mark))
8684   (when (and (not no-expire)
8685              gnus-newsgroup-auto-expire
8686              (memq mark gnus-auto-expirable-marks))
8687     (setq mark gnus-expirable-mark))
8688   (let ((article (or article (gnus-summary-article-number)))
8689         (old-mark (gnus-summary-article-mark article)))
8690     ;; Allow the backend to change the mark.
8691     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8692     (if (eq mark old-mark)
8693         t
8694       (unless article
8695         (error "No article on current line"))
8696       (if (not (if (or (= mark gnus-unread-mark)
8697                        (= mark gnus-ticked-mark)
8698                        (= mark gnus-dormant-mark))
8699                    (gnus-mark-article-as-unread article mark)
8700                  (gnus-mark-article-as-read article mark)))
8701           t
8702         ;; See whether the article is to be put in the cache.
8703         (and gnus-use-cache
8704              (not (= mark gnus-canceled-mark))
8705              (vectorp (gnus-summary-article-header article))
8706              (save-excursion
8707                (gnus-cache-possibly-enter-article
8708                 gnus-newsgroup-name article
8709                 (gnus-summary-article-header article)
8710                 (= mark gnus-ticked-mark)
8711                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8712
8713         (when (gnus-summary-goto-subject article nil t)
8714           (let ((buffer-read-only nil))
8715             (gnus-summary-show-thread)
8716             ;; Fix the mark.
8717             (gnus-summary-update-mark mark 'unread)
8718             t))))))
8719
8720 (defun gnus-summary-update-secondary-mark (article)
8721   "Update the secondary (read, process, cache) mark."
8722   (gnus-summary-update-mark
8723    (cond ((memq article gnus-newsgroup-processable)
8724           gnus-process-mark)
8725          ((memq article gnus-newsgroup-cached)
8726           gnus-cached-mark)
8727          ((memq article gnus-newsgroup-replied)
8728           gnus-replied-mark)
8729          ((memq article gnus-newsgroup-saved)
8730           gnus-saved-mark)
8731          (t gnus-no-mark))
8732    'replied)
8733   (when (gnus-visual-p 'summary-highlight 'highlight)
8734     (gnus-run-hooks 'gnus-summary-update-hook))
8735   t)
8736
8737 (defun gnus-summary-update-mark (mark type)
8738   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8739         (buffer-read-only nil))
8740     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8741     (when forward
8742       (when (looking-at "\r")
8743         (incf forward))
8744       (when (<= (+ forward (point)) (point-max))
8745         ;; Go to the right position on the line.
8746         (goto-char (+ forward (point)))
8747         ;; Replace the old mark with the new mark.
8748         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8749         ;; Optionally update the marks by some user rule.
8750         (when (eq type 'unread)
8751           (gnus-data-set-mark
8752            (gnus-data-find (gnus-summary-article-number)) mark)
8753           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8754
8755 (defun gnus-mark-article-as-read (article &optional mark)
8756   "Enter ARTICLE in the pertinent lists and remove it from others."
8757   ;; Make the article expirable.
8758   (let ((mark (or mark gnus-del-mark)))
8759     (if (= mark gnus-expirable-mark)
8760         (push article gnus-newsgroup-expirable)
8761       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8762     ;; Remove from unread and marked lists.
8763     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8764     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8765     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8766     (push (cons article mark) gnus-newsgroup-reads)
8767     ;; Possibly remove from cache, if that is used.
8768     (when gnus-use-cache
8769       (gnus-cache-enter-remove-article article))
8770     t))
8771
8772 (defun gnus-mark-article-as-unread (article &optional mark)
8773   "Enter ARTICLE in the pertinent lists and remove it from others."
8774   (let ((mark (or mark gnus-ticked-mark)))
8775     (if (<= article 0)
8776         (progn
8777           (gnus-error 1 "Can't mark negative article numbers")
8778           nil)
8779       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8780             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8781             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8782             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8783
8784       ;; Unsuppress duplicates?
8785       (when gnus-suppress-duplicates
8786         (gnus-dup-unsuppress-article article))
8787
8788       (cond ((= mark gnus-ticked-mark)
8789              (push article gnus-newsgroup-marked))
8790             ((= mark gnus-dormant-mark)
8791              (push article gnus-newsgroup-dormant))
8792             (t
8793              (push article gnus-newsgroup-unreads)))
8794       (gnus-pull article gnus-newsgroup-reads)
8795       t)))
8796
8797 (defalias 'gnus-summary-mark-as-unread-forward
8798   'gnus-summary-tick-article-forward)
8799 (make-obsolete 'gnus-summary-mark-as-unread-forward
8800                'gnus-summary-tick-article-forward)
8801 (defun gnus-summary-tick-article-forward (n)
8802   "Tick N articles forwards.
8803 If N is negative, tick backwards instead.
8804 The difference between N and the number of articles ticked is returned."
8805   (interactive "p")
8806   (gnus-summary-mark-forward n gnus-ticked-mark))
8807
8808 (defalias 'gnus-summary-mark-as-unread-backward
8809   'gnus-summary-tick-article-backward)
8810 (make-obsolete 'gnus-summary-mark-as-unread-backward
8811                'gnus-summary-tick-article-backward)
8812 (defun gnus-summary-tick-article-backward (n)
8813   "Tick N articles backwards.
8814 The difference between N and the number of articles ticked is returned."
8815   (interactive "p")
8816   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8817
8818 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8819 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8820 (defun gnus-summary-tick-article (&optional article clear-mark)
8821   "Mark current article as unread.
8822 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8823 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8824   (interactive)
8825   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8826                                        gnus-ticked-mark)))
8827
8828 (defun gnus-summary-mark-as-read-forward (n)
8829   "Mark N articles as read forwards.
8830 If N is negative, mark backwards instead.
8831 The difference between N and the actual number of articles marked is
8832 returned."
8833   (interactive "p")
8834   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8835
8836 (defun gnus-summary-mark-as-read-backward (n)
8837   "Mark the N articles as read backwards.
8838 The difference between N and the actual number of articles marked is
8839 returned."
8840   (interactive "p")
8841   (gnus-summary-mark-forward
8842    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8843
8844 (defun gnus-summary-mark-as-read (&optional article mark)
8845   "Mark current article as read.
8846 ARTICLE specifies the article to be marked as read.
8847 MARK specifies a string to be inserted at the beginning of the line."
8848   (gnus-summary-mark-article article mark))
8849
8850 (defun gnus-summary-clear-mark-forward (n)
8851   "Clear marks from N articles forward.
8852 If N is negative, clear backward instead.
8853 The difference between N and the number of marks cleared is returned."
8854   (interactive "p")
8855   (gnus-summary-mark-forward n gnus-unread-mark))
8856
8857 (defun gnus-summary-clear-mark-backward (n)
8858   "Clear marks from N articles backward.
8859 The difference between N and the number of marks cleared is returned."
8860   (interactive "p")
8861   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8862
8863 (defun gnus-summary-mark-unread-as-read ()
8864   "Intended to be used by `gnus-summary-mark-article-hook'."
8865   (when (memq gnus-current-article gnus-newsgroup-unreads)
8866     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8867
8868 (defun gnus-summary-mark-read-and-unread-as-read ()
8869   "Intended to be used by `gnus-summary-mark-article-hook'."
8870   (let ((mark (gnus-summary-article-mark)))
8871     (when (or (gnus-unread-mark-p mark)
8872               (gnus-read-mark-p mark))
8873       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8874
8875 (defun gnus-summary-mark-unread-as-ticked ()
8876    "Intended to be used by `gnus-summary-mark-article-hook'."
8877   (when (memq gnus-current-article gnus-newsgroup-unreads)
8878     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
8879
8880 (defun gnus-summary-mark-region-as-read (point mark all)
8881   "Mark all unread articles between point and mark as read.
8882 If given a prefix, mark all articles between point and mark as read,
8883 even ticked and dormant ones."
8884   (interactive "r\nP")
8885   (save-excursion
8886     (let (article)
8887       (goto-char point)
8888       (beginning-of-line)
8889       (while (and
8890               (< (point) mark)
8891               (progn
8892                 (when (or all
8893                           (memq (setq article (gnus-summary-article-number))
8894                                 gnus-newsgroup-unreads))
8895                   (gnus-summary-mark-article article gnus-del-mark))
8896                 t)
8897               (gnus-summary-find-next))))))
8898
8899 (defun gnus-summary-mark-below (score mark)
8900   "Mark articles with score less than SCORE with MARK."
8901   (interactive "P\ncMark: ")
8902   (setq score (if score
8903                   (prefix-numeric-value score)
8904                 (or gnus-summary-default-score 0)))
8905   (save-excursion
8906     (set-buffer gnus-summary-buffer)
8907     (goto-char (point-min))
8908     (while
8909         (progn
8910           (and (< (gnus-summary-article-score) score)
8911                (gnus-summary-mark-article nil mark))
8912           (gnus-summary-find-next)))))
8913
8914 (defun gnus-summary-kill-below (&optional score)
8915   "Mark articles with score below SCORE as read."
8916   (interactive "P")
8917   (gnus-summary-mark-below score gnus-killed-mark))
8918
8919 (defun gnus-summary-clear-above (&optional score)
8920   "Clear all marks from articles with score above SCORE."
8921   (interactive "P")
8922   (gnus-summary-mark-above score gnus-unread-mark))
8923
8924 (defun gnus-summary-tick-above (&optional score)
8925   "Tick all articles with score above SCORE."
8926   (interactive "P")
8927   (gnus-summary-mark-above score gnus-ticked-mark))
8928
8929 (defun gnus-summary-mark-above (score mark)
8930   "Mark articles with score over SCORE with MARK."
8931   (interactive "P\ncMark: ")
8932   (setq score (if score
8933                   (prefix-numeric-value score)
8934                 (or gnus-summary-default-score 0)))
8935   (save-excursion
8936     (set-buffer gnus-summary-buffer)
8937     (goto-char (point-min))
8938     (while (and (progn
8939                   (when (> (gnus-summary-article-score) score)
8940                     (gnus-summary-mark-article nil mark))
8941                   t)
8942                 (gnus-summary-find-next)))))
8943
8944 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8945 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8946 (defun gnus-summary-limit-include-expunged (&optional no-error)
8947   "Display all the hidden articles that were expunged for low scores."
8948   (interactive)
8949   (let ((buffer-read-only nil))
8950     (let ((scored gnus-newsgroup-scored)
8951           headers h)
8952       (while scored
8953         (unless (gnus-number-to-header (caar scored))
8954           (and (setq h (gnus-summary-article-header (caar scored)))
8955                (< (cdar scored) gnus-summary-expunge-below)
8956                (push h headers)))
8957         (setq scored (cdr scored)))
8958       (if (not headers)
8959           (when (not no-error)
8960             (error "No expunged articles hidden"))
8961         (goto-char (point-min))
8962         (gnus-summary-prepare-unthreaded (nreverse headers))
8963         (goto-char (point-min))
8964         (gnus-summary-position-point)
8965         t))))
8966
8967 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8968   "Mark all unread articles in this newsgroup as read.
8969 If prefix argument ALL is non-nil, ticked and dormant articles will
8970 also be marked as read.
8971 If QUIETLY is non-nil, no questions will be asked.
8972 If TO-HERE is non-nil, it should be a point in the buffer.  All
8973 articles before this point will be marked as read.
8974 Note that this function will only catch up the unread article
8975 in the current summary buffer limitation.
8976 The number of articles marked as read is returned."
8977   (interactive "P")
8978   (prog1
8979       (save-excursion
8980         (when (or quietly
8981                   (not gnus-interactive-catchup) ;Without confirmation?
8982                   gnus-expert-user
8983                   (gnus-y-or-n-p
8984                    (if all
8985                        "Mark absolutely all articles as read? "
8986                      "Mark all unread articles as read? ")))
8987           (if (and not-mark
8988                    (not gnus-newsgroup-adaptive)
8989                    (not gnus-newsgroup-auto-expire)
8990                    (not gnus-suppress-duplicates)
8991                    (or (not gnus-use-cache)
8992                        (eq gnus-use-cache 'passive)))
8993               (progn
8994                 (when all
8995                   (setq gnus-newsgroup-marked nil
8996                         gnus-newsgroup-dormant nil))
8997                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8998             ;; We actually mark all articles as canceled, which we
8999             ;; have to do when using auto-expiry or adaptive scoring.
9000             (gnus-summary-show-all-threads)
9001             (when (gnus-summary-first-subject (not all) t)
9002               (while (and
9003                       (if to-here (< (point) to-here) t)
9004                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
9005                       (gnus-summary-find-next (not all) nil nil t))))
9006             (gnus-set-mode-line 'summary))
9007           t))
9008     (gnus-summary-position-point)))
9009
9010 (defun gnus-summary-catchup-to-here (&optional all)
9011   "Mark all unticked articles before the current one as read.
9012 If ALL is non-nil, also mark ticked and dormant articles as read."
9013   (interactive "P")
9014   (save-excursion
9015     (gnus-save-hidden-threads
9016       (let ((beg (point)))
9017         ;; We check that there are unread articles.
9018         (when (or all (gnus-summary-find-prev))
9019           (gnus-summary-catchup all t beg)))))
9020   (gnus-summary-position-point))
9021
9022 (defun gnus-summary-catchup-all (&optional quietly)
9023   "Mark all articles in this newsgroup as read."
9024   (interactive "P")
9025   (gnus-summary-catchup t quietly))
9026
9027 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9028   "Mark all unread articles in this group as read, then exit.
9029 If prefix argument ALL is non-nil, all articles are marked as read."
9030   (interactive "P")
9031   (when (gnus-summary-catchup all quietly nil 'fast)
9032     ;; Select next newsgroup or exit.
9033     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9034              (eq gnus-auto-select-next 'quietly))
9035         (gnus-summary-next-group nil)
9036       (gnus-summary-exit))))
9037
9038 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9039   "Mark all articles in this newsgroup as read, and then exit."
9040   (interactive "P")
9041   (gnus-summary-catchup-and-exit t quietly))
9042
9043 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9044   "Mark all articles in this group as read and select the next group.
9045 If given a prefix, mark all articles, unread as well as ticked, as
9046 read."
9047   (interactive "P")
9048   (save-excursion
9049     (gnus-summary-catchup all))
9050   (gnus-summary-next-group))
9051
9052 ;;;
9053 ;;; with article
9054 ;;;
9055
9056 (defmacro gnus-with-article (article &rest forms)
9057   "Select ARTICLE and perform FORMS in the original article buffer.
9058 Then replace the article with the result."
9059   `(progn
9060      ;; We don't want the article to be marked as read.
9061      (let (gnus-mark-article-hook)
9062        (gnus-summary-select-article t t nil ,article))
9063      (set-buffer gnus-original-article-buffer)
9064      ,@forms
9065      (if (not (gnus-check-backend-function
9066                'request-replace-article (car gnus-article-current)))
9067          (gnus-message 5 "Read-only group; not replacing")
9068        (unless (gnus-request-replace-article
9069                 ,article (car gnus-article-current)
9070                 (current-buffer) t)
9071          (error "Couldn't replace article")))
9072      ;; The cache and backlog have to be flushed somewhat.
9073      (when gnus-keep-backlog
9074        (gnus-backlog-remove-article
9075         (car gnus-article-current) (cdr gnus-article-current)))
9076      (when gnus-use-cache
9077        (gnus-cache-update-article
9078         (car gnus-article-current) (cdr gnus-article-current)))))
9079
9080 (put 'gnus-with-article 'lisp-indent-function 1)
9081 (put 'gnus-with-article 'edebug-form-spec '(form body))
9082
9083 ;; Thread-based commands.
9084
9085 (defun gnus-summary-articles-in-thread (&optional article)
9086   "Return a list of all articles in the current thread.
9087 If ARTICLE is non-nil, return all articles in the thread that starts
9088 with that article."
9089   (let* ((article (or article (gnus-summary-article-number)))
9090          (data (gnus-data-find-list article))
9091          (top-level (gnus-data-level (car data)))
9092          (top-subject
9093           (cond ((null gnus-thread-operation-ignore-subject)
9094                  (gnus-simplify-subject-re
9095                   (mail-header-subject (gnus-data-header (car data)))))
9096                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9097                  (gnus-simplify-subject-fuzzy
9098                   (mail-header-subject (gnus-data-header (car data)))))
9099                 (t nil)))
9100          (end-point (save-excursion
9101                       (if (gnus-summary-go-to-next-thread)
9102                           (point) (point-max))))
9103          articles)
9104     (while (and data
9105                 (< (gnus-data-pos (car data)) end-point))
9106       (when (or (not top-subject)
9107                 (string= top-subject
9108                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9109                              (gnus-simplify-subject-fuzzy
9110                               (mail-header-subject
9111                                (gnus-data-header (car data))))
9112                            (gnus-simplify-subject-re
9113                             (mail-header-subject
9114                              (gnus-data-header (car data)))))))
9115         (push (gnus-data-number (car data)) articles))
9116       (unless (and (setq data (cdr data))
9117                    (> (gnus-data-level (car data)) top-level))
9118         (setq data nil)))
9119     ;; Return the list of articles.
9120     (nreverse articles)))
9121
9122 (defun gnus-summary-rethread-current ()
9123   "Rethread the thread the current article is part of."
9124   (interactive)
9125   (let* ((gnus-show-threads t)
9126          (article (gnus-summary-article-number))
9127          (id (mail-header-id (gnus-summary-article-header)))
9128          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9129     (unless id
9130       (error "No article on the current line"))
9131     (gnus-rebuild-thread id)
9132     (gnus-summary-goto-subject article)))
9133
9134 (defun gnus-summary-reparent-thread ()
9135   "Make the current article child of the marked (or previous) article.
9136
9137 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9138 is non-nil or the Subject: of both articles are the same."
9139   (interactive)
9140   (unless (not (gnus-group-read-only-p))
9141     (error "The current newsgroup does not support article editing"))
9142   (unless (<= (length gnus-newsgroup-processable) 1)
9143     (error "No more than one article may be marked"))
9144   (save-window-excursion
9145     (let ((gnus-article-buffer " *reparent*")
9146           (current-article (gnus-summary-article-number))
9147           ;; First grab the marked article, otherwise one line up.
9148           (parent-article (if (not (null gnus-newsgroup-processable))
9149                               (car gnus-newsgroup-processable)
9150                             (save-excursion
9151                               (if (eq (forward-line -1) 0)
9152                                   (gnus-summary-article-number)
9153                                 (error "Beginning of summary buffer"))))))
9154       (unless (not (eq current-article parent-article))
9155         (error "An article may not be self-referential"))
9156       (let ((message-id (mail-header-id
9157                          (gnus-summary-article-header parent-article))))
9158         (unless (and message-id (not (equal message-id "")))
9159           (error "No message-id in desired parent"))
9160         (gnus-with-article current-article
9161           (save-restriction
9162             (goto-char (point-min))
9163             (message-narrow-to-head)
9164             (if (re-search-forward "^References: " nil t)
9165                 (progn
9166                   (re-search-forward "^[^ \t]" nil t)
9167                   (forward-line -1)
9168                   (end-of-line)
9169                   (insert " " message-id))
9170               (insert "References: " message-id "\n"))))
9171         (set-buffer gnus-summary-buffer)
9172         (gnus-summary-unmark-all-processable)
9173         (gnus-summary-update-article current-article)
9174         (gnus-summary-rethread-current)
9175         (gnus-message 3 "Article %d is now the child of article %d"
9176                       current-article parent-article)))))
9177
9178 (defun gnus-summary-toggle-threads (&optional arg)
9179   "Toggle showing conversation threads.
9180 If ARG is positive number, turn showing conversation threads on."
9181   (interactive "P")
9182   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9183     (setq gnus-show-threads
9184           (if (null arg) (not gnus-show-threads)
9185             (> (prefix-numeric-value arg) 0)))
9186     (gnus-summary-prepare)
9187     (gnus-summary-goto-subject current)
9188     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9189     (gnus-summary-position-point)))
9190
9191 (defun gnus-summary-show-all-threads ()
9192   "Show all threads."
9193   (interactive)
9194   (save-excursion
9195     (let ((buffer-read-only nil))
9196       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9197   (gnus-summary-position-point))
9198
9199 (defun gnus-summary-show-thread ()
9200   "Show thread subtrees.
9201 Returns nil if no thread was there to be shown."
9202   (interactive)
9203   (let ((buffer-read-only nil)
9204         (orig (point))
9205         ;; first goto end then to beg, to have point at beg after let
9206         (end (progn (end-of-line) (point)))
9207         (beg (progn (beginning-of-line) (point))))
9208     (prog1
9209         ;; Any hidden lines here?
9210         (search-forward "\r" end t)
9211       (subst-char-in-region beg end ?\^M ?\n t)
9212       (goto-char orig)
9213       (gnus-summary-position-point))))
9214
9215 (defun gnus-summary-hide-all-threads ()
9216   "Hide all thread subtrees."
9217   (interactive)
9218   (save-excursion
9219     (goto-char (point-min))
9220     (gnus-summary-hide-thread)
9221     (while (zerop (gnus-summary-next-thread 1 t))
9222       (gnus-summary-hide-thread)))
9223   (gnus-summary-position-point))
9224
9225 (defun gnus-summary-hide-thread ()
9226   "Hide thread subtrees.
9227 Returns nil if no threads were there to be hidden."
9228   (interactive)
9229   (let ((buffer-read-only nil)
9230         (start (point))
9231         (article (gnus-summary-article-number)))
9232     (goto-char start)
9233     ;; Go forward until either the buffer ends or the subthread
9234     ;; ends.
9235     (when (and (not (eobp))
9236                (or (zerop (gnus-summary-next-thread 1 t))
9237                    (goto-char (point-max))))
9238       (prog1
9239           (if (and (> (point) start)
9240                    (search-backward "\n" start t))
9241               (progn
9242                 (subst-char-in-region start (point) ?\n ?\^M)
9243                 (gnus-summary-goto-subject article))
9244             (goto-char start)
9245             nil)))))
9246
9247 (defun gnus-summary-go-to-next-thread (&optional previous)
9248   "Go to the same level (or less) next thread.
9249 If PREVIOUS is non-nil, go to previous thread instead.
9250 Return the article number moved to, or nil if moving was impossible."
9251   (let ((level (gnus-summary-thread-level))
9252         (way (if previous -1 1))
9253         (beg (point)))
9254     (forward-line way)
9255     (while (and (not (eobp))
9256                 (< level (gnus-summary-thread-level)))
9257       (forward-line way))
9258     (if (eobp)
9259         (progn
9260           (goto-char beg)
9261           nil)
9262       (setq beg (point))
9263       (prog1
9264           (gnus-summary-article-number)
9265         (goto-char beg)))))
9266
9267 (defun gnus-summary-next-thread (n &optional silent)
9268   "Go to the same level next N'th thread.
9269 If N is negative, search backward instead.
9270 Returns the difference between N and the number of skips actually
9271 done.
9272
9273 If SILENT, don't output messages."
9274   (interactive "p")
9275   (let ((backward (< n 0))
9276         (n (abs n)))
9277     (while (and (> n 0)
9278                 (gnus-summary-go-to-next-thread backward))
9279       (decf n))
9280     (unless silent
9281       (gnus-summary-position-point))
9282     (when (and (not silent) (/= 0 n))
9283       (gnus-message 7 "No more threads"))
9284     n))
9285
9286 (defun gnus-summary-prev-thread (n)
9287   "Go to the same level previous N'th thread.
9288 Returns the difference between N and the number of skips actually
9289 done."
9290   (interactive "p")
9291   (gnus-summary-next-thread (- n)))
9292
9293 (defun gnus-summary-go-down-thread ()
9294   "Go down one level in the current thread."
9295   (let ((children (gnus-summary-article-children)))
9296     (when children
9297       (gnus-summary-goto-subject (car children)))))
9298
9299 (defun gnus-summary-go-up-thread ()
9300   "Go up one level in the current thread."
9301   (let ((parent (gnus-summary-article-parent)))
9302     (when parent
9303       (gnus-summary-goto-subject parent))))
9304
9305 (defun gnus-summary-down-thread (n)
9306   "Go down thread N steps.
9307 If N is negative, go up instead.
9308 Returns the difference between N and how many steps down that were
9309 taken."
9310   (interactive "p")
9311   (let ((up (< n 0))
9312         (n (abs n)))
9313     (while (and (> n 0)
9314                 (if up (gnus-summary-go-up-thread)
9315                   (gnus-summary-go-down-thread)))
9316       (setq n (1- n)))
9317     (gnus-summary-position-point)
9318     (when (/= 0 n)
9319       (gnus-message 7 "Can't go further"))
9320     n))
9321
9322 (defun gnus-summary-up-thread (n)
9323   "Go up thread N steps.
9324 If N is negative, go up instead.
9325 Returns the difference between N and how many steps down that were
9326 taken."
9327   (interactive "p")
9328   (gnus-summary-down-thread (- n)))
9329
9330 (defun gnus-summary-top-thread ()
9331   "Go to the top of the thread."
9332   (interactive)
9333   (while (gnus-summary-go-up-thread))
9334   (gnus-summary-article-number))
9335
9336 (defun gnus-summary-kill-thread (&optional unmark)
9337   "Mark articles under current thread as read.
9338 If the prefix argument is positive, remove any kinds of marks.
9339 If the prefix argument is negative, tick articles instead."
9340   (interactive "P")
9341   (when unmark
9342     (setq unmark (prefix-numeric-value unmark)))
9343   (let ((articles (gnus-summary-articles-in-thread)))
9344     (save-excursion
9345       ;; Expand the thread.
9346       (gnus-summary-show-thread)
9347       ;; Mark all the articles.
9348       (while articles
9349         (gnus-summary-goto-subject (car articles))
9350         (cond ((null unmark)
9351                (gnus-summary-mark-article-as-read gnus-killed-mark))
9352               ((> unmark 0)
9353                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9354               (t
9355                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9356         (setq articles (cdr articles))))
9357     ;; Hide killed subtrees.
9358     (and (null unmark)
9359          gnus-thread-hide-killed
9360          (gnus-summary-hide-thread))
9361     ;; If marked as read, go to next unread subject.
9362     (when (null unmark)
9363       ;; Go to next unread subject.
9364       (gnus-summary-next-subject 1 t)))
9365   (gnus-set-mode-line 'summary))
9366
9367 ;; Summary sorting commands
9368
9369 (defun gnus-summary-sort-by-number (&optional reverse)
9370   "Sort the summary buffer by article number.
9371 Argument REVERSE means reverse order."
9372   (interactive "P")
9373   (gnus-summary-sort 'number reverse))
9374
9375 (defun gnus-summary-sort-by-author (&optional reverse)
9376   "Sort the summary buffer by author name alphabetically.
9377 If `case-fold-search' is non-nil, case of letters is ignored.
9378 Argument REVERSE means reverse order."
9379   (interactive "P")
9380   (gnus-summary-sort 'author reverse))
9381
9382 (defun gnus-summary-sort-by-subject (&optional reverse)
9383   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9384 If `case-fold-search' is non-nil, case of letters is ignored.
9385 Argument REVERSE means reverse order."
9386   (interactive "P")
9387   (gnus-summary-sort 'subject reverse))
9388
9389 (defun gnus-summary-sort-by-date (&optional reverse)
9390   "Sort the summary buffer by date.
9391 Argument REVERSE means reverse order."
9392   (interactive "P")
9393   (gnus-summary-sort 'date reverse))
9394
9395 (defun gnus-summary-sort-by-score (&optional reverse)
9396   "Sort the summary buffer by score.
9397 Argument REVERSE means reverse order."
9398   (interactive "P")
9399   (gnus-summary-sort 'score reverse))
9400
9401 (defun gnus-summary-sort-by-lines (&optional reverse)
9402   "Sort the summary buffer by the number of lines.
9403 Argument REVERSE means reverse order."
9404   (interactive "P")
9405   (gnus-summary-sort 'lines reverse))
9406
9407 (defun gnus-summary-sort-by-chars (&optional reverse)
9408   "Sort the summary buffer by article length.
9409 Argument REVERSE means reverse order."
9410   (interactive "P")
9411   (gnus-summary-sort 'chars reverse))
9412
9413 (defun gnus-summary-sort (predicate reverse)
9414   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9415   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9416          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9417          (gnus-thread-sort-functions
9418           (if (not reverse)
9419               thread
9420             `(lambda (t1 t2)
9421                (,thread t2 t1))))
9422          (gnus-sort-gathered-threads-function
9423           gnus-thread-sort-functions)
9424          (gnus-article-sort-functions
9425           (if (not reverse)
9426               article
9427             `(lambda (t1 t2)
9428                (,article t2 t1))))
9429          (buffer-read-only)
9430          (gnus-summary-prepare-hook nil))
9431     ;; We do the sorting by regenerating the threads.
9432     (gnus-summary-prepare)
9433     ;; Hide subthreads if needed.
9434     (when (and gnus-show-threads gnus-thread-hide-subtree)
9435       (gnus-summary-hide-all-threads))))
9436
9437 ;; Summary saving commands.
9438
9439 (defun gnus-summary-save-article (&optional n not-saved)
9440   "Save the current article using the default saver function.
9441 If N is a positive number, save the N next articles.
9442 If N is a negative number, save the N previous articles.
9443 If N is nil and any articles have been marked with the process mark,
9444 save those articles instead.
9445 The variable `gnus-default-article-saver' specifies the saver function."
9446   (interactive "P")
9447   (let* ((articles (gnus-summary-work-articles n))
9448          (save-buffer (save-excursion
9449                         (nnheader-set-temp-buffer " *Gnus Save*")))
9450          (num (length articles))
9451          header file)
9452     (dolist (article articles)
9453       (setq header (gnus-summary-article-header article))
9454       (if (not (vectorp header))
9455           ;; This is a pseudo-article.
9456           (if (assq 'name header)
9457               (gnus-copy-file (cdr (assq 'name header)))
9458             (gnus-message 1 "Article %d is unsaveable" article))
9459         ;; This is a real article.
9460         (save-window-excursion
9461           (gnus-summary-select-article t nil nil article))
9462         (save-excursion
9463           (set-buffer save-buffer)
9464           (erase-buffer)
9465           (insert-buffer-substring gnus-original-article-buffer))
9466         (setq file (gnus-article-save save-buffer file num))
9467         (gnus-summary-remove-process-mark article)
9468         (unless not-saved
9469           (gnus-summary-set-saved-mark article))))
9470     (gnus-kill-buffer save-buffer)
9471     (gnus-summary-position-point)
9472     (gnus-set-mode-line 'summary)
9473     n))
9474
9475 (defun gnus-summary-pipe-output (&optional arg)
9476   "Pipe the current article to a subprocess.
9477 If N is a positive number, pipe the N next articles.
9478 If N is a negative number, pipe the N previous articles.
9479 If N is nil and any articles have been marked with the process mark,
9480 pipe those articles instead."
9481   (interactive "P")
9482   (require 'gnus-art)
9483   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9484     (gnus-summary-save-article arg t))
9485   (gnus-configure-windows 'pipe))
9486
9487 (defun gnus-summary-save-article-mail (&optional arg)
9488   "Append the current article to an mail file.
9489 If N is a positive number, save the N next articles.
9490 If N is a negative number, save the N previous articles.
9491 If N is nil and any articles have been marked with the process mark,
9492 save those articles instead."
9493   (interactive "P")
9494   (require 'gnus-art)
9495   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9496     (gnus-summary-save-article arg)))
9497
9498 (defun gnus-summary-save-article-rmail (&optional arg)
9499   "Append the current article to an rmail file.
9500 If N is a positive number, save the N next articles.
9501 If N is a negative number, save the N previous articles.
9502 If N is nil and any articles have been marked with the process mark,
9503 save those articles instead."
9504   (interactive "P")
9505   (require 'gnus-art)
9506   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9507     (gnus-summary-save-article arg)))
9508
9509 (defun gnus-summary-save-article-file (&optional arg)
9510   "Append the current article to a file.
9511 If N is a positive number, save the N next articles.
9512 If N is a negative number, save the N previous articles.
9513 If N is nil and any articles have been marked with the process mark,
9514 save those articles instead."
9515   (interactive "P")
9516   (require 'gnus-art)
9517   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9518     (gnus-summary-save-article arg)))
9519
9520 (defun gnus-summary-write-article-file (&optional arg)
9521   "Write the current article to a file, deleting the previous file.
9522 If N is a positive number, save the N next articles.
9523 If N is a negative number, save the N previous articles.
9524 If N is nil and any articles have been marked with the process mark,
9525 save those articles instead."
9526   (interactive "P")
9527   (require 'gnus-art)
9528   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9529     (gnus-summary-save-article arg)))
9530
9531 (defun gnus-summary-save-article-body-file (&optional arg)
9532   "Append the current article body to a file.
9533 If N is a positive number, save the N next articles.
9534 If N is a negative number, save the N previous articles.
9535 If N is nil and any articles have been marked with the process mark,
9536 save those articles instead."
9537   (interactive "P")
9538   (require 'gnus-art)
9539   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9540     (gnus-summary-save-article arg)))
9541
9542 (defun gnus-summary-pipe-message (program)
9543   "Pipe the current article through PROGRAM."
9544   (interactive "sProgram: ")
9545   (gnus-summary-select-article)
9546   (let ((mail-header-separator ""))
9547     (gnus-eval-in-buffer-window gnus-article-buffer
9548       (save-restriction
9549         (widen)
9550         (let ((start (window-start))
9551               buffer-read-only)
9552           (message-pipe-buffer-body program)
9553           (set-window-start (get-buffer-window (current-buffer)) start))))))
9554
9555 (defun gnus-get-split-value (methods)
9556   "Return a value based on the split METHODS."
9557   (let (split-name method result match)
9558     (when methods
9559       (save-excursion
9560         (set-buffer gnus-original-article-buffer)
9561         (save-restriction
9562           (nnheader-narrow-to-headers)
9563           (while (and methods (not split-name))
9564             (goto-char (point-min))
9565             (setq method (pop methods))
9566             (setq match (car method))
9567             (when (cond
9568                    ((stringp match)
9569                     ;; Regular expression.
9570                     (ignore-errors
9571                       (re-search-forward match nil t)))
9572                    ((gnus-functionp match)
9573                     ;; Function.
9574                     (save-restriction
9575                       (widen)
9576                       (setq result (funcall match gnus-newsgroup-name))))
9577                    ((consp match)
9578                     ;; Form.
9579                     (save-restriction
9580                       (widen)
9581                       (setq result (eval match)))))
9582               (setq split-name (cdr method))
9583               (cond ((stringp result)
9584                      (push (expand-file-name
9585                             result gnus-article-save-directory)
9586                            split-name))
9587                     ((consp result)
9588                      (setq split-name (append result split-name)))))))))
9589     (nreverse split-name)))
9590
9591 (defun gnus-valid-move-group-p (group)
9592   (and (boundp group)
9593        (symbol-name group)
9594        (symbol-value group)
9595        (gnus-get-function (gnus-find-method-for-group
9596                            (symbol-name group)) 'request-accept-article t)))
9597
9598 (defun gnus-read-move-group-name (prompt default articles prefix)
9599   "Read a group name."
9600   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9601          (minibuffer-confirm-incomplete nil) ; XEmacs
9602          (prom
9603           (format "%s %s to:"
9604                   prompt
9605                   (if (> (length articles) 1)
9606                       (format "these %d articles" (length articles))
9607                     "this article")))
9608          (to-newsgroup
9609           (cond
9610            ((null split-name)
9611             (gnus-completing-read default prom
9612                                   gnus-active-hashtb
9613                                   'gnus-valid-move-group-p
9614                                   nil prefix
9615                                   'gnus-group-history))
9616            ((= 1 (length split-name))
9617             (gnus-completing-read (car split-name) prom
9618                                   gnus-active-hashtb
9619                                   'gnus-valid-move-group-p
9620                                   nil nil
9621                                   'gnus-group-history))
9622            (t
9623             (gnus-completing-read nil prom
9624                                   (mapcar (lambda (el) (list el))
9625                                           (nreverse split-name))
9626                                   nil nil nil
9627                                   'gnus-group-history))))
9628          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9629     (when to-newsgroup
9630       (if (or (string= to-newsgroup "")
9631               (string= to-newsgroup prefix))
9632           (setq to-newsgroup default))
9633       (unless to-newsgroup
9634         (error "No group name entered"))
9635       (or (gnus-active to-newsgroup)
9636           (gnus-activate-group to-newsgroup nil nil to-method)
9637           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9638                                      to-newsgroup))
9639               (or (and (gnus-request-create-group to-newsgroup to-method)
9640                        (gnus-activate-group
9641                         to-newsgroup nil nil to-method)
9642                        (gnus-subscribe-group to-newsgroup))
9643                   (error "Couldn't create group %s" to-newsgroup)))
9644           (error "No such group: %s" to-newsgroup)))
9645     to-newsgroup))
9646
9647 (defun gnus-summary-save-parts (type dir n &optional reverse)
9648   "Save parts matching TYPE to DIR.
9649 If REVERSE, save parts that do not match TYPE."
9650   (interactive
9651    (list (read-string "Save parts of type: "
9652                       (or (car gnus-summary-save-parts-type-history)
9653                           gnus-summary-save-parts-default-mime)
9654                       'gnus-summary-save-parts-type-history)
9655          (setq gnus-summary-save-parts-last-directory
9656                (read-file-name "Save to directory: "
9657                                gnus-summary-save-parts-last-directory
9658                                nil t))
9659          current-prefix-arg))
9660   (gnus-summary-iterate n
9661     (let ((gnus-display-mime-function nil)
9662           (gnus-inhibit-treatment t))
9663       (gnus-summary-select-article))
9664     (save-excursion
9665       (set-buffer gnus-article-buffer)
9666       (let ((handles (or gnus-article-mime-handles
9667                          (mm-dissect-buffer) (mm-uu-dissect))))
9668         (when handles
9669           (gnus-summary-save-parts-1 type dir handles reverse)
9670           (unless gnus-article-mime-handles ;; Don't destroy this case.
9671             (mm-destroy-parts handles)))))))
9672
9673 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9674   (if (stringp (car handle))
9675       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9676               (cdr handle))
9677     (when (if reverse
9678               (not (string-match type (mm-handle-media-type handle)))
9679             (string-match type (mm-handle-media-type handle)))
9680       (let ((file (expand-file-name
9681                    (file-name-nondirectory
9682                     (or
9683                      (mail-content-type-get
9684                       (mm-handle-disposition handle) 'filename)
9685                      (concat gnus-newsgroup-name
9686                              "." (number-to-string
9687                                   (cdr gnus-article-current)))))
9688                    dir)))
9689         (unless (file-exists-p file)
9690           (mm-save-part-to-file handle file))))))
9691
9692 ;; Summary extract commands
9693
9694 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9695   (let ((buffer-read-only nil)
9696         (article (gnus-summary-article-number))
9697         after-article b e)
9698     (unless (gnus-summary-goto-subject article)
9699       (error "No such article: %d" article))
9700     (gnus-summary-position-point)
9701     ;; If all commands are to be bunched up on one line, we collect
9702     ;; them here.
9703     (unless gnus-view-pseudos-separately
9704       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9705             files action)
9706         (while ps
9707           (setq action (cdr (assq 'action (car ps))))
9708           (setq files (list (cdr (assq 'name (car ps)))))
9709           (while (and ps (cdr ps)
9710                       (string= (or action "1")
9711                                (or (cdr (assq 'action (cadr ps))) "2")))
9712             (push (cdr (assq 'name (cadr ps))) files)
9713             (setcdr ps (cddr ps)))
9714           (when files
9715             (when (not (string-match "%s" action))
9716               (push " " files))
9717             (push " " files)
9718             (when (assq 'execute (car ps))
9719               (setcdr (assq 'execute (car ps))
9720                       (funcall (if (string-match "%s" action)
9721                                    'format 'concat)
9722                                action
9723                                (mapconcat
9724                                 (lambda (f)
9725                                   (if (equal f " ")
9726                                       f
9727                                     (gnus-quote-arg-for-sh-or-csh f)))
9728                                 files " ")))))
9729           (setq ps (cdr ps)))))
9730     (if (and gnus-view-pseudos (not not-view))
9731         (while pslist
9732           (when (assq 'execute (car pslist))
9733             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9734                                   (eq gnus-view-pseudos 'not-confirm)))
9735           (setq pslist (cdr pslist)))
9736       (save-excursion
9737         (while pslist
9738           (setq after-article (or (cdr (assq 'article (car pslist)))
9739                                   (gnus-summary-article-number)))
9740           (gnus-summary-goto-subject after-article)
9741           (forward-line 1)
9742           (setq b (point))
9743           (insert "    " (file-name-nondirectory
9744                           (cdr (assq 'name (car pslist))))
9745                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9746           (setq e (point))
9747           (forward-line -1)             ; back to `b'
9748           (gnus-add-text-properties
9749            b (1- e) (list 'gnus-number gnus-reffed-article-number
9750                           gnus-mouse-face-prop gnus-mouse-face))
9751           (gnus-data-enter
9752            after-article gnus-reffed-article-number
9753            gnus-unread-mark b (car pslist) 0 (- e b))
9754           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9755           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9756           (setq pslist (cdr pslist)))))))
9757
9758 (defun gnus-pseudos< (p1 p2)
9759   (let ((c1 (cdr (assq 'action p1)))
9760         (c2 (cdr (assq 'action p2))))
9761     (and c1 c2 (string< c1 c2))))
9762
9763 (defun gnus-request-pseudo-article (props)
9764   (cond ((assq 'execute props)
9765          (gnus-execute-command (cdr (assq 'execute props)))))
9766   (let ((gnus-current-article (gnus-summary-article-number)))
9767     (gnus-run-hooks 'gnus-mark-article-hook)))
9768
9769 (defun gnus-execute-command (command &optional automatic)
9770   (save-excursion
9771     (gnus-article-setup-buffer)
9772     (set-buffer gnus-article-buffer)
9773     (setq buffer-read-only nil)
9774     (let ((command (if automatic command
9775                      (read-string "Command: " (cons command 0)))))
9776       (erase-buffer)
9777       (insert "$ " command "\n\n")
9778       (if gnus-view-pseudo-asynchronously
9779           (start-process "gnus-execute" (current-buffer) shell-file-name
9780                          shell-command-switch command)
9781         (call-process shell-file-name nil t nil
9782                       shell-command-switch command)))))
9783
9784 ;; Summary kill commands.
9785
9786 (defun gnus-summary-edit-global-kill (article)
9787   "Edit the \"global\" kill file."
9788   (interactive (list (gnus-summary-article-number)))
9789   (gnus-group-edit-global-kill article))
9790
9791 (defun gnus-summary-edit-local-kill ()
9792   "Edit a local kill file applied to the current newsgroup."
9793   (interactive)
9794   (setq gnus-current-headers (gnus-summary-article-header))
9795   (gnus-group-edit-local-kill
9796    (gnus-summary-article-number) gnus-newsgroup-name))
9797
9798 ;;; Header reading.
9799
9800 (defun gnus-read-header (id &optional header)
9801   "Read the headers of article ID and enter them into the Gnus system."
9802   (let ((group gnus-newsgroup-name)
9803         (gnus-override-method
9804          (or
9805           gnus-override-method
9806           (and (gnus-news-group-p gnus-newsgroup-name)
9807                (car (gnus-refer-article-methods)))))
9808         where)
9809     ;; First we check to see whether the header in question is already
9810     ;; fetched.
9811     (if (stringp id)
9812         ;; This is a Message-ID.
9813         (setq header (or header (gnus-id-to-header id)))
9814       ;; This is an article number.
9815       (setq header (or header (gnus-summary-article-header id))))
9816     (if (and header
9817              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9818         ;; We have found the header.
9819         header
9820       ;; If this is a sparse article, we have to nix out its
9821       ;; previous entry in the thread hashtb.
9822       (when (and header
9823                  (gnus-summary-article-sparse-p (mail-header-number header)))
9824         (let* ((parent (gnus-parent-id (mail-header-references header)))
9825                (thread (and parent (gnus-id-to-thread parent))))
9826           (when thread
9827             (delq (assq header thread) thread))))
9828       ;; We have to really fetch the header to this article.
9829       (save-excursion
9830         (set-buffer nntp-server-buffer)
9831         (when (setq where (gnus-request-head id group))
9832           (nnheader-fold-continuation-lines)
9833           (goto-char (point-max))
9834           (insert ".\n")
9835           (goto-char (point-min))
9836           (insert "211 ")
9837           (princ (cond
9838                   ((numberp id) id)
9839                   ((cdr where) (cdr where))
9840                   (header (mail-header-number header))
9841                   (t gnus-reffed-article-number))
9842                  (current-buffer))
9843           (insert " Article retrieved.\n"))
9844         (if (or (not where)
9845                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9846             ()                          ; Malformed head.
9847           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9848             (when (and (stringp id)
9849                        (not (string= (gnus-group-real-name group)
9850                                      (car where))))
9851               ;; If we fetched by Message-ID and the article came
9852               ;; from a different group, we fudge some bogus article
9853               ;; numbers for this article.
9854               (mail-header-set-number header gnus-reffed-article-number))
9855             (save-excursion
9856               (set-buffer gnus-summary-buffer)
9857               (decf gnus-reffed-article-number)
9858               (gnus-remove-header (mail-header-number header))
9859               (push header gnus-newsgroup-headers)
9860               (setq gnus-current-headers header)
9861               (push (mail-header-number header) gnus-newsgroup-limit)))
9862           header)))))
9863
9864 (defun gnus-remove-header (number)
9865   "Remove header NUMBER from `gnus-newsgroup-headers'."
9866   (if (and gnus-newsgroup-headers
9867            (= number (mail-header-number (car gnus-newsgroup-headers))))
9868       (pop gnus-newsgroup-headers)
9869     (let ((headers gnus-newsgroup-headers))
9870       (while (and (cdr headers)
9871                   (not (= number (mail-header-number (cadr headers)))))
9872         (pop headers))
9873       (when (cdr headers)
9874         (setcdr headers (cddr headers))))))
9875
9876 ;;;
9877 ;;; summary highlights
9878 ;;;
9879
9880 (defun gnus-highlight-selected-summary ()
9881   "Highlight selected article in summary buffer."
9882   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9883   (when gnus-summary-selected-face
9884     (save-excursion
9885       (let* ((beg (progn (beginning-of-line) (point)))
9886              (end (progn (end-of-line) (point)))
9887              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9888              (from (if (get-text-property beg gnus-mouse-face-prop)
9889                        beg
9890                      (or (next-single-property-change
9891                           beg gnus-mouse-face-prop nil end)
9892                          beg)))
9893              (to
9894               (if (= from end)
9895                   (- from 2)
9896                 (or (next-single-property-change
9897                      from gnus-mouse-face-prop nil end)
9898                     end))))
9899         ;; If no mouse-face prop on line we will have to = from = end,
9900         ;; so we highlight the entire line instead.
9901         (when (= (+ to 2) from)
9902           (setq from beg)
9903           (setq to end))
9904         (if gnus-newsgroup-selected-overlay
9905             ;; Move old overlay.
9906             (gnus-move-overlay
9907              gnus-newsgroup-selected-overlay from to (current-buffer))
9908           ;; Create new overlay.
9909           (gnus-overlay-put
9910            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9911            'face gnus-summary-selected-face))))))
9912
9913 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9914 (defun gnus-summary-highlight-line ()
9915   "Highlight current line according to `gnus-summary-highlight'."
9916   (let* ((list gnus-summary-highlight)
9917          (p (point))
9918          (end (progn (end-of-line) (point)))
9919          ;; now find out where the line starts and leave point there.
9920          (beg (progn (beginning-of-line) (point)))
9921          (article (gnus-summary-article-number))
9922          (score (or (cdr (assq (or article gnus-current-article)
9923                                gnus-newsgroup-scored))
9924                     gnus-summary-default-score 0))
9925          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9926          (inhibit-read-only t))
9927     ;; Eval the cars of the lists until we find a match.
9928     (let ((default gnus-summary-default-score))
9929       (while (and list
9930                   (not (eval (caar list))))
9931         (setq list (cdr list))))
9932     (let ((face (cdar list)))
9933       (unless (eq face (get-text-property beg 'face))
9934         (gnus-put-text-property-excluding-characters-with-faces
9935          beg end 'face
9936          (setq face (if (boundp face) (symbol-value face) face)))
9937         (when gnus-summary-highlight-line-function
9938           (funcall gnus-summary-highlight-line-function article face))))
9939     (goto-char p)))
9940
9941 (defun gnus-update-read-articles (group unread &optional compute)
9942   "Update the list of read articles in GROUP."
9943   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9944          (entry (gnus-gethash group gnus-newsrc-hashtb))
9945          (info (nth 2 entry))
9946          (prev 1)
9947          (unread (sort (copy-sequence unread) '<))
9948          read)
9949     (if (or (not info) (not active))
9950         ;; There is no info on this group if it was, in fact,
9951         ;; killed.  Gnus stores no information on killed groups, so
9952         ;; there's nothing to be done.
9953         ;; One could store the information somewhere temporarily,
9954         ;; perhaps...  Hmmm...
9955         ()
9956       ;; Remove any negative articles numbers.
9957       (while (and unread (< (car unread) 0))
9958         (setq unread (cdr unread)))
9959       ;; Remove any expired article numbers
9960       (while (and unread (< (car unread) (car active)))
9961         (setq unread (cdr unread)))
9962       ;; Compute the ranges of read articles by looking at the list of
9963       ;; unread articles.
9964       (while unread
9965         (when (/= (car unread) prev)
9966           (push (if (= prev (1- (car unread))) prev
9967                   (cons prev (1- (car unread))))
9968                 read))
9969         (setq prev (1+ (car unread)))
9970         (setq unread (cdr unread)))
9971       (when (<= prev (cdr active))
9972         (push (cons prev (cdr active)) read))
9973       (setq read (if (> (length read) 1) (nreverse read) read))
9974       (if compute
9975           read
9976         (save-excursion
9977           (let (setmarkundo)
9978             ;; Propagate the read marks to the backend.
9979             (when (gnus-check-backend-function 'request-set-mark group)
9980               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9981                     (add (gnus-remove-from-range read (gnus-info-read info))))
9982                 (when (or add del)
9983                   (unless (gnus-check-group group)
9984                     (error "Can't open server for %s" group))
9985                   (gnus-request-set-mark
9986                    group (delq nil (list (if add (list add 'add '(read)))
9987                                          (if del (list del 'del '(read))))))
9988                   (setq setmarkundo
9989                         `(gnus-request-set-mark
9990                           ,group
9991                           ',(delq nil (list
9992                                        (if del (list del 'add '(read)))
9993                                        (if add (list add 'del '(read))))))))))
9994             (set-buffer gnus-group-buffer)
9995             (gnus-undo-register
9996               `(progn
9997                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9998                  (gnus-info-set-read ',info ',(gnus-info-read info))
9999                  (gnus-get-unread-articles-in-group ',info
10000                                                     (gnus-active ,group))
10001                  (gnus-group-update-group ,group t)
10002                  ,setmarkundo))))
10003         ;; Enter this list into the group info.
10004         (gnus-info-set-read info read)
10005         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10006         (gnus-get-unread-articles-in-group info (gnus-active group))
10007         t))))
10008
10009 (defun gnus-offer-save-summaries ()
10010   "Offer to save all active summary buffers."
10011   (save-excursion
10012     (let ((buflist (buffer-list))
10013           buffers bufname)
10014       ;; Go through all buffers and find all summaries.
10015       (while buflist
10016         (and (setq bufname (buffer-name (car buflist)))
10017              (string-match "Summary" bufname)
10018              (save-excursion
10019                (set-buffer bufname)
10020                ;; We check that this is, indeed, a summary buffer.
10021                (and (eq major-mode 'gnus-summary-mode)
10022                     ;; Also make sure this isn't bogus.
10023                     gnus-newsgroup-prepared
10024                     ;; Also make sure that this isn't a dead summary buffer.
10025                     (not gnus-dead-summary-mode)))
10026              (push bufname buffers))
10027         (setq buflist (cdr buflist)))
10028       ;; Go through all these summary buffers and offer to save them.
10029       (when buffers
10030         (map-y-or-n-p
10031          "Update summary buffer %s? "
10032          (lambda (buf)
10033            (switch-to-buffer buf)
10034            (gnus-summary-exit))
10035          buffers)))))
10036
10037
10038 ;;; @ for mime-partial
10039 ;;;
10040
10041 (defun gnus-request-partial-message ()
10042   (save-excursion
10043     (let ((number (gnus-summary-article-number))
10044           (group gnus-newsgroup-name)
10045           (mother gnus-article-buffer))
10046       (set-buffer (get-buffer-create " *Partial Article*"))
10047       (erase-buffer)
10048       (setq mime-preview-buffer mother)
10049       (gnus-request-article-this-buffer number group)
10050       (mime-parse-buffer)
10051       )))
10052
10053 (autoload 'mime-combine-message/partial-pieces-automatically
10054   "mime-partial"
10055   "Internal method to combine message/partial messages automatically.")
10056
10057 (mime-add-condition
10058  'action '((type . message)(subtype . partial)
10059            (major-mode . gnus-original-article-mode)
10060            (method . mime-combine-message/partial-pieces-automatically)
10061            (summary-buffer-exp . gnus-summary-buffer)
10062            (request-partial-message-method . gnus-request-partial-message)
10063            ))
10064
10065
10066 ;;; @ for message/rfc822
10067 ;;;
10068
10069 (defun gnus-mime-extract-message/rfc822 (entity situation)
10070   (let (group article num cwin swin cur)
10071     (with-temp-buffer
10072       (mime-insert-entity-content entity)
10073       (setq group (or (cdr (assq 'group situation))
10074                       (completing-read "Group: "
10075                                        gnus-active-hashtb
10076                                        nil
10077                                        (gnus-read-active-file-p)
10078                                        gnus-newsgroup-name))
10079             article (gnus-request-accept-article group)))
10080     (when (and (consp article)
10081                (numberp (setq article (cdr article))))
10082       (setq num (1+ (or (cdr (assq 'number situation)) 0))
10083             cwin (get-buffer-window (current-buffer) t))
10084       (save-window-excursion
10085         (if (setq swin (get-buffer-window gnus-summary-buffer t))
10086             (select-window swin)
10087           (set-buffer gnus-summary-buffer))
10088         (setq cur gnus-current-article)
10089         (forward-line num)
10090         (let (gnus-show-threads)
10091           (gnus-summary-goto-subject article t))
10092         (gnus-summary-clear-mark-forward 1)
10093         (gnus-summary-goto-subject cur))
10094       (when (and cwin (window-frame cwin))
10095         (select-frame (window-frame cwin)))
10096       (when (boundp 'mime-acting-situation-to-override)
10097         (set-alist 'mime-acting-situation-to-override
10098                    'group
10099                    group)
10100         (set-alist 'mime-acting-situation-to-override
10101                    'after-method
10102                    `(progn
10103                       (save-current-buffer
10104                         (set-buffer gnus-group-buffer)
10105                         (gnus-activate-group ,group))
10106                       (gnus-summary-goto-article ,cur
10107                                                  gnus-show-all-headers)))
10108         (set-alist 'mime-acting-situation-to-override
10109                    'number num)))))
10110
10111 (mime-add-condition
10112  'action '((type . message)(subtype . rfc822)
10113            (major-mode . gnus-original-article-mode)
10114            (method . gnus-mime-extract-message/rfc822)
10115            (mode . "extract")
10116            ))
10117
10118 (mime-add-condition
10119  'action '((type . message)(subtype . news)
10120            (major-mode . gnus-original-article-mode)
10121            (method . gnus-mime-extract-message/rfc822)
10122            (mode . "extract")
10123            ))
10124
10125 (defun gnus-mime-extract-multipart (entity situation)
10126   (let ((children (mime-entity-children entity))
10127         mime-acting-situation-to-override
10128         f)
10129     (while children
10130       (mime-play-entity (car children)
10131                         (cons (assq 'mode situation)
10132                               mime-acting-situation-to-override))
10133       (setq children (cdr children)))
10134     (if (setq f (cdr (assq 'after-method
10135                            mime-acting-situation-to-override)))
10136         (eval f)
10137       )))
10138
10139 (mime-add-condition
10140  'action '((type . multipart)
10141            (method . gnus-mime-extract-multipart)
10142            (mode . "extract")
10143            )
10144  'with-default)
10145
10146
10147 ;;; @ end
10148 ;;;
10149
10150 (defun gnus-summary-setup-default-charset ()
10151   "Setup newsgroup default charset."
10152   (if (equal gnus-newsgroup-name "nndraft:drafts")
10153       (setq gnus-newsgroup-charset nil)
10154     (let* ((ignored-charsets
10155             (or gnus-newsgroup-ephemeral-ignored-charsets
10156                 (append
10157                  (and gnus-newsgroup-name
10158                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10159                  gnus-newsgroup-ignored-charsets))))
10160       (setq gnus-newsgroup-charset
10161             (or gnus-newsgroup-ephemeral-charset
10162                 (and gnus-newsgroup-name
10163                      (gnus-parameter-charset gnus-newsgroup-name))
10164                 gnus-default-charset))
10165       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10166            ignored-charsets))))
10167
10168 ;;;
10169 ;;; Mime Commands
10170 ;;;
10171
10172 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10173   "Display the current article buffer fully MIME-buttonized.
10174 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10175 treated as multipart/mixed."
10176   (interactive "P")
10177   (require 'gnus-art)
10178   (let ((gnus-unbuttonized-mime-types nil)
10179         (gnus-mime-display-multipart-as-mixed show-all-parts))
10180     (gnus-summary-show-article)))
10181
10182 (defun gnus-summary-repair-multipart (article)
10183   "Add a Content-Type header to a multipart article without one."
10184   (interactive (list (gnus-summary-article-number)))
10185   (gnus-with-article article
10186     (message-narrow-to-head)
10187     (message-remove-header "Mime-Version")
10188     (goto-char (point-max))
10189     (insert "Mime-Version: 1.0\n")
10190     (widen)
10191     (when (search-forward "\n--" nil t)
10192       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10193         (message-narrow-to-head)
10194         (message-remove-header "Content-Type")
10195         (goto-char (point-max))
10196         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10197                         separator))
10198         (widen))))
10199   (let (gnus-mark-article-hook)
10200     (gnus-summary-select-article t t nil article)))
10201
10202 (defun gnus-summary-toggle-display-buttonized ()
10203   "Toggle the buttonizing of the article buffer."
10204   (interactive)
10205   (require 'gnus-art)
10206   (if (setq gnus-inhibit-mime-unbuttonizing
10207             (not gnus-inhibit-mime-unbuttonizing))
10208       (let ((gnus-unbuttonized-mime-types nil))
10209         (gnus-summary-show-article))
10210     (gnus-summary-show-article)))
10211
10212 ;;;
10213 ;;; Intelli-mouse commmands
10214 ;;;
10215
10216 (defun gnus-wheel-summary-scroll (event)
10217   (interactive "e")
10218   (let ((amount (if (memq 'shift (event-modifiers event))
10219                     (car gnus-wheel-scroll-amount)
10220                   (cdr gnus-wheel-scroll-amount)))
10221         (direction (- (* (static-if (featurep 'xemacs)
10222                              (event-button event)
10223                            (cond ((eq 'mouse-4 (event-basic-type event))
10224                                   4)
10225                                  ((eq 'mouse-5 (event-basic-type event))
10226                                   5)))
10227                          2) 9))
10228         edge)
10229     (gnus-summary-scroll-up (* amount direction))
10230     (when (gnus-eval-in-buffer-window gnus-article-buffer
10231             (save-restriction
10232               (widen)
10233               (and (if (< 0 direction)
10234                        (gnus-article-next-page 0)
10235                      (gnus-article-prev-page 0)
10236                      (bobp))
10237                    (if (setq edge (get-text-property
10238                                    (point-min) 'gnus-wheel-edge))
10239                        (setq edge (* edge direction))
10240                      (setq edge -1))
10241                    (or (plusp edge)
10242                        (let ((buffer-read-only nil)
10243                              (inhibit-read-only t))
10244                          (put-text-property (point-min) (point-max)
10245                                             'gnus-wheel-edge direction)
10246                          nil))
10247                    (or (> edge gnus-wheel-edge-resistance)
10248                        (let ((buffer-read-only nil)
10249                              (inhibit-read-only t))
10250                          (put-text-property (point-min) (point-max)
10251                                             'gnus-wheel-edge
10252                                             (* (1+ edge) direction))
10253                          nil))
10254                    (eq last-command 'gnus-wheel-summary-scroll))))
10255       (gnus-summary-next-article nil nil (minusp direction)))))
10256
10257 (defun gnus-wheel-install ()
10258   "Enable mouse wheel support on summary window."
10259   (when gnus-use-wheel
10260     (let ((keys
10261            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
10262       (dolist (key keys)
10263         (define-key gnus-summary-mode-map key
10264           'gnus-wheel-summary-scroll)))))
10265
10266 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
10267
10268 ;;;
10269 ;;; Traditional PGP commmands
10270 ;;;
10271
10272 (defun gnus-summary-decrypt-article (&optional force)
10273   "Decrypt the current article in traditional PGP way.
10274 This will have permanent effect only in mail groups.
10275 If FORCE is non-nil, allow editing of articles even in read-only
10276 groups."
10277   (interactive "P")
10278   (gnus-summary-select-article t)
10279   (gnus-eval-in-buffer-window gnus-article-buffer
10280     (save-excursion
10281       (save-restriction
10282         (widen)
10283         (goto-char (point-min))
10284         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10285           (error "Not a traditional PGP message!"))
10286         (let ((armor-start (match-beginning 0)))
10287           (if (and (pgg-decrypt-region armor-start (point-max))
10288                    (or force (not (gnus-group-read-only-p))))
10289               (let ((inhibit-read-only t)
10290                     buffer-read-only)
10291                 (delete-region armor-start
10292                                (progn
10293                                  (re-search-forward "^-+END PGP" nil t)
10294                                  (beginning-of-line 2)
10295                                  (point)))
10296                 (insert-buffer-substring pgg-output-buffer))))))))
10297
10298 (defun gnus-summary-verify-article ()
10299   "Verify the current article in traditional PGP way."
10300   (interactive)
10301   (save-excursion
10302     (set-buffer gnus-original-article-buffer)
10303     (goto-char (point-min))
10304     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10305       (error "Not a traditional PGP message!"))
10306     (re-search-forward "^-+END PGP" nil t)
10307     (beginning-of-line 2)
10308     (call-interactively (function pgg-verify-region))))
10309
10310 ;;;
10311 ;;; Generic summary marking commands
10312 ;;;
10313
10314 (defvar gnus-summary-marking-alist
10315   '((read gnus-del-mark "d")
10316     (unread gnus-unread-mark "u")
10317     (ticked gnus-ticked-mark "!")
10318     (dormant gnus-dormant-mark "?")
10319     (expirable gnus-expirable-mark "e"))
10320   "An alist of names/marks/keystrokes.")
10321
10322 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10323 (defvar gnus-summary-mark-map)
10324
10325 (defun gnus-summary-make-all-marking-commands ()
10326   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10327   (dolist (elem gnus-summary-marking-alist)
10328     (apply 'gnus-summary-make-marking-command elem)))
10329
10330 (defun gnus-summary-make-marking-command (name mark keystroke)
10331   (let ((map (make-sparse-keymap)))
10332     (define-key gnus-summary-generic-mark-map keystroke map)
10333     (dolist (lway `((next "next" next nil "n")
10334                     (next-unread "next unread" next t "N")
10335                     (prev "previous" prev nil "p")
10336                     (prev-unread "previous unread" prev t "P")
10337                     (nomove "" nil nil ,keystroke)))
10338       (let ((func (gnus-summary-make-marking-command-1
10339                    mark (car lway) lway name)))
10340         (setq func (eval func))
10341         (define-key map (nth 4 lway) func)))))
10342
10343 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10344   `(defun ,(intern
10345             (format "gnus-summary-put-mark-as-%s%s"
10346                     name (if (eq way 'nomove)
10347                              ""
10348                            (concat "-" (symbol-name way)))))
10349      (n)
10350      ,(format
10351        "Mark the current article as %s%s.
10352 If N, the prefix, then repeat N times.
10353 If N is negative, move in reverse order.
10354 The difference between N and the actual number of articles marked is
10355 returned."
10356        name (car (cdr lway)))
10357      (interactive "p")
10358      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10359
10360 (defun gnus-summary-generic-mark (n mark move unread)
10361   "Mark N articles with MARK."
10362   (unless (eq major-mode 'gnus-summary-mode)
10363     (error "This command can only be used in the summary buffer"))
10364   (gnus-summary-show-thread)
10365   (let ((nummove
10366          (cond
10367           ((eq move 'next) 1)
10368           ((eq move 'prev) -1)
10369           (t 0))))
10370     (if (zerop nummove)
10371         (setq n 1)
10372       (when (< n 0)
10373         (setq n (abs n)
10374               nummove (* -1 nummove))))
10375     (while (and (> n 0)
10376                 (gnus-summary-mark-article nil mark)
10377                 (zerop (gnus-summary-next-subject nummove unread t)))
10378       (setq n (1- n)))
10379     (when (/= 0 n)
10380       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10381     (gnus-summary-recenter)
10382     (gnus-summary-position-point)
10383     (gnus-set-mode-line 'summary)
10384     n))
10385
10386 (gnus-summary-make-all-marking-commands)
10387
10388 (gnus-ems-redefine)
10389
10390 (provide 'gnus-sum)
10391
10392 (run-hooks 'gnus-sum-load-hook)
10393
10394 ;;; gnus-sum.el ends here