(gnus-group-make-shimbun-group): Call
[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 (eval-when-compile (require 'gnus-clfns))
33
34 (require 'gnus)
35 (require 'gnus-group)
36 (require 'gnus-spec)
37 (require 'gnus-range)
38 (require 'gnus-int)
39 (require 'gnus-undo)
40 (require 'gnus-util)
41 ;; Recursive :-(.
42 ;; (require 'gnus-art)
43 (require 'nnoo)
44 (require 'mime-view)
45
46 (eval-when-compile
47   (require 'mime-play)
48   (require 'static))
49
50 (eval-and-compile
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 souped 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-forwarded-mark ?O
452   "*Mark used for articles that have been forwarded."
453   :group 'gnus-summary-marks
454   :type 'character)
455
456 (defcustom gnus-cached-mark ?*
457   "*Mark used for articles that are in the cache."
458   :group 'gnus-summary-marks
459   :type 'character)
460
461 (defcustom gnus-saved-mark ?S
462   "*Mark used for articles that have been saved to."
463   :group 'gnus-summary-marks
464   :type 'character)
465
466 (defcustom gnus-no-mark ?\ ;;;Whitespace
467   "*Mark used for articles that have no other secondary mark."
468   :group 'gnus-summary-marks
469   :type 'character)
470
471 (defcustom gnus-ancient-mark ?O
472   "*Mark used for ancient articles."
473   :group 'gnus-summary-marks
474   :type 'character)
475
476 (defcustom gnus-sparse-mark ?Q
477   "*Mark used for sparsely reffed articles."
478   :group 'gnus-summary-marks
479   :type 'character)
480
481 (defcustom gnus-canceled-mark ?G
482   "*Mark used for canceled articles."
483   :group 'gnus-summary-marks
484   :type 'character)
485
486 (defcustom gnus-duplicate-mark ?M
487   "*Mark used for duplicate articles."
488   :group 'gnus-summary-marks
489   :type 'character)
490
491 (defcustom gnus-undownloaded-mark ?@
492   "*Mark used for articles that weren't downloaded."
493   :group 'gnus-summary-marks
494   :type 'character)
495
496 (defcustom gnus-downloadable-mark ?%
497   "*Mark used for articles that are to be downloaded."
498   :group 'gnus-summary-marks
499   :type 'character)
500
501 (defcustom gnus-unsendable-mark ?=
502   "*Mark used for articles that won't be sent."
503   :group 'gnus-summary-marks
504   :type 'character)
505
506 (defcustom gnus-score-over-mark ?+
507   "*Score mark used for articles with high scores."
508   :group 'gnus-summary-marks
509   :type 'character)
510
511 (defcustom gnus-score-below-mark ?-
512   "*Score mark used for articles with low scores."
513   :group 'gnus-summary-marks
514   :type 'character)
515
516 (defcustom gnus-empty-thread-mark ?\ ;;;Whitespace
517   "*There is no thread under the article."
518   :group 'gnus-summary-marks
519   :type 'character)
520
521 (defcustom gnus-not-empty-thread-mark ?=
522   "*There is a thread under the article."
523   :group 'gnus-summary-marks
524   :type 'character)
525
526 (defcustom gnus-view-pseudo-asynchronously nil
527   "*If non-nil, Gnus will view pseudo-articles asynchronously."
528   :group 'gnus-extract-view
529   :type 'boolean)
530
531 (defcustom gnus-auto-expirable-marks
532   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
533         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
534         gnus-souped-mark gnus-duplicate-mark)
535   "*The list of marks converted into expiration if a group is auto-expirable."
536   :version "21.1"
537   :group 'gnus-summary
538   :type '(repeat character))
539
540 (defcustom gnus-inhibit-user-auto-expire t
541   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
542   :version "21.1"
543   :group 'gnus-summary
544   :type 'boolean)
545
546 (defcustom gnus-view-pseudos nil
547   "*If `automatic', pseudo-articles will be viewed automatically.
548 If `not-confirm', pseudos will be viewed automatically, and the user
549 will not be asked to confirm the command."
550   :group 'gnus-extract-view
551   :type '(choice (const :tag "off" nil)
552                  (const automatic)
553                  (const not-confirm)))
554
555 (defcustom gnus-view-pseudos-separately t
556   "*If non-nil, one pseudo-article will be created for each file to be viewed.
557 If nil, all files that use the same viewing command will be given as a
558 list of parameters to that command."
559   :group 'gnus-extract-view
560   :type 'boolean)
561
562 (defcustom gnus-insert-pseudo-articles t
563   "*If non-nil, insert pseudo-articles when decoding articles."
564   :group 'gnus-extract-view
565   :type 'boolean)
566
567 (defcustom gnus-summary-dummy-line-format
568   "  %(:                          :%) %S\n"
569   "*The format specification for the dummy roots in the summary buffer.
570 It works along the same lines as a normal formatting string,
571 with some simple extensions.
572
573 %S  The subject"
574   :group 'gnus-threading
575   :type 'string)
576
577 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
578   "*The format specification for the summary mode line.
579 It works along the same lines as a normal formatting string,
580 with some simple extensions:
581
582 %G  Group name
583 %p  Unprefixed group name
584 %A  Current article number
585 %z  Current article score
586 %V  Gnus version
587 %U  Number of unread articles in the group
588 %e  Number of unselected articles in the group
589 %Z  A string with unread/unselected article counts
590 %g  Shortish group name
591 %S  Subject of the current article
592 %u  User-defined spec
593 %s  Current score file name
594 %d  Number of dormant articles
595 %r  Number of articles that have been marked as read in this session
596 %E  Number of articles expunged by the score files"
597   :group 'gnus-summary-format
598   :type 'string)
599
600 (defcustom gnus-list-identifiers nil
601   "Regexp that matches list identifiers to be removed from subject.
602 This can also be a list of regexps."
603   :version "21.1"
604   :group 'gnus-summary-format
605   :group 'gnus-article-hiding
606   :type '(choice (const :tag "none" nil)
607                  (regexp :value ".*")
608                  (repeat :value (".*") regexp)))
609
610 (defcustom gnus-summary-mark-below 0
611   "*Mark all articles with a score below this variable as read.
612 This variable is local to each summary buffer and usually set by the
613 score file."
614   :group 'gnus-score-default
615   :type 'integer)
616
617 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
618   "*List of functions used for sorting articles in the summary buffer.
619
620 Each function takes two articles and returns non-nil if the first
621 article should be sorted before the other.  If you use more than one
622 function, the primary sort function should be the last.  You should
623 probably always include `gnus-article-sort-by-number' in the list of
624 sorting functions -- preferably first.  Also note that sorting by date
625 is often much slower than sorting by number, and the sorting order is
626 very similar.  (Sorting by date means sorting by the time the message
627 was sent, sorting by number means sorting by arrival time.)
628
629 Ready-made functions include `gnus-article-sort-by-number',
630 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
631 `gnus-article-sort-by-date' and `gnus-article-sort-by-score'.
632
633 When threading is turned on, the variable `gnus-thread-sort-functions'
634 controls how articles are sorted."
635   :group 'gnus-summary-sort
636   :type '(repeat (choice (function-item gnus-article-sort-by-number)
637                          (function-item gnus-article-sort-by-author)
638                          (function-item gnus-article-sort-by-subject)
639                          (function-item gnus-article-sort-by-date)
640                          (function-item gnus-article-sort-by-score)
641                          (function :tag "other"))))
642
643 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
644   "*List of functions used for sorting threads in the summary buffer.
645 By default, threads are sorted by article number.
646
647 Each function takes two threads and returns non-nil if the first
648 thread should be sorted before the other.  If you use more than one
649 function, the primary sort function should be the last.  You should
650 probably always include `gnus-thread-sort-by-number' in the list of
651 sorting functions -- preferably first.  Also note that sorting by date
652 is often much slower than sorting by number, and the sorting order is
653 very similar.  (Sorting by date means sorting by the time the message
654 was sent, sorting by number means sorting by arrival time.)
655
656 Ready-made functions include `gnus-thread-sort-by-number',
657 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
658 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
659 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
660
661 When threading is turned off, the variable
662 `gnus-article-sort-functions' controls how articles are sorted."
663   :group 'gnus-summary-sort
664   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
665                          (function-item gnus-thread-sort-by-author)
666                          (function-item gnus-thread-sort-by-subject)
667                          (function-item gnus-thread-sort-by-date)
668                          (function-item gnus-thread-sort-by-score)
669                          (function-item gnus-thread-sort-by-total-score)
670                          (function :tag "other"))))
671
672 (defcustom gnus-thread-score-function '+
673   "*Function used for calculating the total score of a thread.
674
675 The function is called with the scores of the article and each
676 subthread and should then return the score of the thread.
677
678 Some functions you can use are `+', `max', or `min'."
679   :group 'gnus-summary-sort
680   :type 'function)
681
682 (defcustom gnus-summary-expunge-below nil
683   "All articles that have a score less than this variable will be expunged.
684 This variable is local to the summary buffers."
685   :group 'gnus-score-default
686   :type '(choice (const :tag "off" nil)
687                  integer))
688
689 (defcustom gnus-thread-expunge-below nil
690   "All threads that have a total score less than this variable will be expunged.
691 See `gnus-thread-score-function' for en explanation of what a
692 \"thread score\" is.
693
694 This variable is local to the summary buffers."
695   :group 'gnus-threading
696   :group 'gnus-score-default
697   :type '(choice (const :tag "off" nil)
698                  integer))
699
700 (defcustom gnus-summary-mode-hook nil
701   "*A hook for Gnus summary mode.
702 This hook is run before any variables are set in the summary buffer."
703   :options '(turn-on-gnus-mailing-list-mode)
704   :group 'gnus-summary-various
705   :type 'hook)
706
707 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
708 (when (featurep 'xemacs)
709   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
710   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
711   (add-hook 'gnus-summary-mode-hook
712             'gnus-xmas-switch-horizontal-scrollbar-off))
713
714 (defcustom gnus-summary-menu-hook nil
715   "*Hook run after the creation of the summary mode menu."
716   :group 'gnus-summary-visual
717   :type 'hook)
718
719 (defcustom gnus-summary-exit-hook nil
720   "*A hook called on exit from the summary buffer.
721 It will be called with point in the group buffer."
722   :group 'gnus-summary-exit
723   :type 'hook)
724
725 (defcustom gnus-summary-prepare-hook nil
726   "*A hook called after the summary buffer has been generated.
727 If you want to modify the summary buffer, you can use this hook."
728   :group 'gnus-summary-various
729   :type 'hook)
730
731 (defcustom gnus-summary-prepared-hook nil
732   "*A hook called as the last thing after the summary buffer has been generated."
733   :group 'gnus-summary-various
734   :type 'hook)
735
736 (defcustom gnus-summary-generate-hook nil
737   "*A hook run just before generating the summary buffer.
738 This hook is commonly used to customize threading variables and the
739 like."
740   :group 'gnus-summary-various
741   :type 'hook)
742
743 (defcustom gnus-select-group-hook nil
744   "*A hook called when a newsgroup is selected.
745
746 If you'd like to simplify subjects like the
747 `gnus-summary-next-same-subject' command does, you can use the
748 following hook:
749
750  (setq gnus-select-group-hook
751       (list
752         (lambda ()
753           (mapcar (lambda (header)
754                      (mail-header-set-subject
755                       header
756                       (gnus-simplify-subject
757                        (mail-header-subject header) 're-only)))
758                   gnus-newsgroup-headers))))"
759   :group 'gnus-group-select
760   :type 'hook)
761
762 (defcustom gnus-select-article-hook nil
763   "*A hook called when an article is selected."
764   :group 'gnus-summary-choose
765   :type 'hook)
766
767 (defcustom gnus-visual-mark-article-hook
768   (list 'gnus-highlight-selected-summary)
769   "*Hook run after selecting an article in the summary buffer.
770 It is meant to be used for highlighting the article in some way.  It
771 is not run if `gnus-visual' is nil."
772   :group 'gnus-summary-visual
773   :type 'hook)
774
775 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
776   "*A hook called before parsing the headers."
777   :group 'gnus-various
778   :type 'hook)
779
780 (defcustom gnus-exit-group-hook nil
781   "*A hook called when exiting summary mode.
782 This hook is not called from the non-updating exit commands like `Q'."
783   :group 'gnus-various
784   :type 'hook)
785
786 (defcustom gnus-summary-update-hook
787   (list 'gnus-summary-highlight-line)
788   "*A hook called when a summary line is changed.
789 The hook will not be called if `gnus-visual' is nil.
790
791 The default function `gnus-summary-highlight-line' will
792 highlight the line according to the `gnus-summary-highlight'
793 variable."
794   :group 'gnus-summary-visual
795   :type 'hook)
796
797 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
798   "*A hook called when an article is selected for the first time.
799 The hook is intended to mark an article as read (or unread)
800 automatically when it is selected."
801   :group 'gnus-summary-choose
802   :type 'hook)
803
804 (defcustom gnus-group-no-more-groups-hook nil
805   "*A hook run when returning to group mode having no more (unread) groups."
806   :group 'gnus-group-select
807   :type 'hook)
808
809 (defcustom gnus-ps-print-hook nil
810   "*A hook run before ps-printing something from Gnus."
811   :group 'gnus-summary
812   :type 'hook)
813
814 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
815   "Face used for highlighting the current article in the summary buffer."
816   :group 'gnus-summary-visual
817   :type 'face)
818
819 (defcustom gnus-summary-highlight
820   '(((= mark gnus-canceled-mark)
821      . gnus-summary-cancelled-face)
822     ((and (> score default)
823           (or (= mark gnus-dormant-mark)
824               (= mark gnus-ticked-mark)))
825      . gnus-summary-high-ticked-face)
826     ((and (< score default)
827           (or (= mark gnus-dormant-mark)
828               (= mark gnus-ticked-mark)))
829      . gnus-summary-low-ticked-face)
830     ((or (= mark gnus-dormant-mark)
831          (= mark gnus-ticked-mark))
832      . gnus-summary-normal-ticked-face)
833     ((and (> score default) (= mark gnus-ancient-mark))
834      . gnus-summary-high-ancient-face)
835     ((and (< score default) (= mark gnus-ancient-mark))
836      . gnus-summary-low-ancient-face)
837     ((= mark gnus-ancient-mark)
838      . gnus-summary-normal-ancient-face)
839     ((and (> score default) (= mark gnus-unread-mark))
840      . gnus-summary-high-unread-face)
841     ((and (< score default) (= mark gnus-unread-mark))
842      . gnus-summary-low-unread-face)
843     ((and (memq article gnus-newsgroup-incorporated)
844           (= mark gnus-unread-mark))
845      . gnus-summary-incorporated-face)
846     ((= mark gnus-unread-mark)
847      . gnus-summary-normal-unread-face)
848     ((and (> score default) (memq mark (list gnus-downloadable-mark
849                                              gnus-undownloaded-mark)))
850      . gnus-summary-high-unread-face)
851     ((and (< score default) (memq mark (list gnus-downloadable-mark
852                                              gnus-undownloaded-mark)))
853      . gnus-summary-low-unread-face)
854     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
855           (memq article gnus-newsgroup-unreads))
856      . gnus-summary-normal-unread-face)
857     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
858      . gnus-summary-normal-read-face)
859     ((> score default)
860      . gnus-summary-high-read-face)
861     ((< score default)
862      . gnus-summary-low-read-face)
863     (t
864      . gnus-summary-normal-read-face))
865   "*Controls the highlighting of summary buffer lines.
866
867 A list of (FORM . FACE) pairs.  When deciding how a a particular
868 summary line should be displayed, each form is evaluated.  The content
869 of the face field after the first true form is used.  You can change
870 how those summary lines are displayed, by editing the face field.
871
872 You can use the following variables in the FORM field.
873
874 score:   The articles score
875 default: The default article score.
876 below:   The score below which articles are automatically marked as read.
877 mark:    The articles mark."
878   :group 'gnus-summary-visual
879   :type '(repeat (cons (sexp :tag "Form" nil)
880                        face)))
881
882 (defcustom gnus-alter-header-function nil
883   "Function called to allow alteration of article header structures.
884 The function is called with one parameter, the article header vector,
885 which it may alter in any way.")
886
887 (defvar gnus-decode-encoded-word-function
888   (mime-find-field-decoder 'From 'nov)
889   "Variable that says which function should be used to decode a string with encoded words.")
890
891 (defcustom gnus-extra-headers nil
892   "*Extra headers to parse."
893   :version "21.1"
894   :group 'gnus-summary
895   :type '(repeat symbol))
896
897 (defcustom gnus-ignored-from-addresses
898   (and user-mail-address (regexp-quote user-mail-address))
899   "*Regexp of From headers that may be suppressed in favor of To headers."
900   :version "21.1"
901   :group 'gnus-summary
902   :type 'regexp)
903
904 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
905   "List of charsets that should be ignored.
906 When these charsets are used in the \"charset\" parameter, the
907 default charset will be used instead."
908   :version "21.1"
909   :type '(repeat symbol)
910   :group 'gnus-charset)
911
912 (gnus-define-group-parameter
913  ignored-charsets
914  :type list
915  :function-document
916  "Return the ignored charsets of GROUP."
917  :variable gnus-group-ignored-charsets-alist
918  :variable-default 
919  '(("alt\\.chinese\\.text" iso-8859-1))
920  :variable-document
921  "Alist of regexps (to match group names) and charsets that should be ignored.
922 When these charsets are used in the \"charset\" parameter, the
923 default charset will be used instead."
924  :variable-group gnus-charset
925  :variable-type '(repeat (cons (regexp :tag "Group")
926                                (repeat symbol)))
927  :parameter-type '(choice :tag "Ignored charsets" 
928                           :value nil
929                           (repeat (symbol)))
930  :parameter-document       "\
931 List of charsets that should be ignored.
932
933 When these charsets are used in the \"charset\" parameter, the
934 default charset will be used instead.")
935
936 (defcustom gnus-group-highlight-words-alist nil
937   "Alist of group regexps and highlight regexps.
938 This variable uses the same syntax as `gnus-emphasis-alist'."
939   :version "21.1"
940   :type '(repeat (cons (regexp :tag "Group")
941                        (repeat (list (regexp :tag "Highlight regexp")
942                                      (number :tag "Group for entire word" 0)
943                                      (number :tag "Group for displayed part" 0)
944                                      (symbol :tag "Face"
945                                              gnus-emphasis-highlight-words)))))
946   :group 'gnus-summary-visual)
947
948 (defcustom gnus-use-wheel nil
949   "Use Intelli-mouse on summary movement"
950   :type 'boolean
951   :group 'gnus-summary-maneuvering)
952
953 (defcustom gnus-wheel-scroll-amount '(5 . 1)
954   "Amount to scroll messages by spinning the mouse wheel.
955 This is actually a cons cell, where the first item is the amount to scroll
956 on a normal wheel event, and the second is the amount to scroll when the
957 wheel is moved with the shift key depressed."
958   :type '(cons (integer :tag "Shift") integer)
959   :group 'gnus-summary-maneuvering)
960
961 (defcustom gnus-wheel-edge-resistance 2
962   "How hard it should be to change the current article
963 by moving the mouse over the edge of the article window."
964   :type 'integer
965   :group 'gnus-summary-maneuvering)
966
967 (defcustom gnus-summary-show-article-charset-alist
968   nil
969   "Alist of number and charset.
970 The article will be shown with the charset corresponding to the
971 numbered argument.
972 For example: ((1 . cn-gb-2312) (2 . big5))."
973   :version "21.1"
974   :type '(repeat (cons (number :tag "Argument" 1)
975                        (symbol :tag "Charset")))
976   :group 'gnus-charset)
977
978 (defcustom gnus-preserve-marks t
979   "Whether marks are preserved when moving, copying and respooling messages."
980   :version "21.1"
981   :type 'boolean
982   :group 'gnus-summary-marks)
983
984 (defcustom gnus-alter-articles-to-read-function nil
985   "Function to be called to alter the list of articles to be selected."
986   :type '(choice (const nil) function)
987   :group 'gnus-summary)
988
989 (defcustom gnus-orphan-score nil
990   "*All orphans get this score added.  Set in the score file."
991   :group 'gnus-score-default
992   :type '(choice (const nil)
993                  integer))
994
995 (defcustom gnus-summary-save-parts-default-mime "image/.*"
996   "*A regexp to match MIME parts when saving multiple parts of a message
997 with gnus-summary-save-parts (X m). This regexp will be used by default
998 when prompting the user for which type of files to save."
999   :group 'gnus-summary
1000   :type 'regexp)
1001
1002
1003 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1004   "*A regexp to match MIME parts when saving multiple parts of a message
1005 with gnus-summary-save-parts (X m). This regexp will be used by default
1006 when prompting the user for which type of files to save."
1007   :group 'gnus-summary
1008   :type 'regexp)
1009
1010 (defcustom gnus-read-all-available-headers nil
1011   "Whether Gnus should parse all headers made available to it.
1012 This is mostly relevant for slow backends where the user may
1013 wish to widen the summary buffer to include all headers
1014 that were fetched.  Say, for nnultimate groups."
1015   :group 'gnus-summary
1016   :type '(choice boolean regexp))
1017
1018 ;;; Internal variables
1019
1020 (defvar gnus-article-mime-handles nil)
1021 (defvar gnus-article-decoded-p nil)
1022 (defvar gnus-article-charset nil)
1023 (defvar gnus-article-ignored-charsets nil)
1024 (defvar gnus-scores-exclude-files nil)
1025 (defvar gnus-page-broken nil)
1026 (defvar gnus-inhibit-mime-unbuttonizing nil)
1027
1028 (defvar gnus-original-article nil)
1029 (defvar gnus-article-internal-prepare-hook nil)
1030 (defvar gnus-newsgroup-process-stack nil)
1031
1032 (defvar gnus-thread-indent-array nil)
1033 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1034 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1035   "Function called to sort the articles within a thread after it has been gathered together.")
1036
1037 (defvar gnus-summary-save-parts-type-history nil)
1038 (defvar gnus-summary-save-parts-last-directory nil)
1039
1040 (defvar gnus-summary-save-parts-type-history nil)
1041 (defvar gnus-summary-save-parts-last-directory nil)
1042
1043 ;; Avoid highlighting in kill files.
1044 (defvar gnus-summary-inhibit-highlight nil)
1045 (defvar gnus-newsgroup-selected-overlay nil)
1046 (defvar gnus-inhibit-limiting nil)
1047 (defvar gnus-newsgroup-adaptive-score-file nil)
1048 (defvar gnus-current-score-file nil)
1049 (defvar gnus-current-move-group nil)
1050 (defvar gnus-current-copy-group nil)
1051 (defvar gnus-current-crosspost-group nil)
1052
1053 (defvar gnus-newsgroup-dependencies nil)
1054 (defvar gnus-newsgroup-adaptive nil)
1055 (defvar gnus-summary-display-article-function nil)
1056 (defvar gnus-summary-highlight-line-function nil
1057   "Function called after highlighting a summary line.")
1058
1059 (defvar gnus-summary-line-format-alist
1060   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1061     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1062     (?s gnus-tmp-subject-or-nil ?s)
1063     (?n gnus-tmp-name ?s)
1064     (?A (std11-address-string
1065          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1066     (?a (or (std11-full-name-string
1067              (car (mime-entity-read-field gnus-tmp-header 'From)))
1068             gnus-tmp-from) ?s)
1069     (?F gnus-tmp-from ?s)
1070     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1071     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1072     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1073     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1074     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1075     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1076     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1077     (?L gnus-tmp-lines ?s)
1078     (?I gnus-tmp-indentation ?s)
1079     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1080     (?R gnus-tmp-replied ?c)
1081     (?\[ gnus-tmp-opening-bracket ?c)
1082     (?\] gnus-tmp-closing-bracket ?c)
1083     (?\> (make-string gnus-tmp-level ? ) ?s)
1084     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1085     (?i gnus-tmp-score ?d)
1086     (?z gnus-tmp-score-char ?c)
1087     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1088     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1089     (?U gnus-tmp-unread ?c)
1090     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1091     (?t (gnus-summary-number-of-articles-in-thread
1092          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1093         ?d)
1094     (?e (gnus-summary-number-of-articles-in-thread
1095          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1096         ?c)
1097     (?u gnus-tmp-user-defined ?s)
1098     (?P (gnus-pick-line-number) ?d))
1099   "An alist of format specifications that can appear in summary lines.
1100 These are paired with what variables they correspond with, along with
1101 the type of the variable (string, integer, character, etc).")
1102
1103 (defvar gnus-summary-dummy-line-format-alist
1104   `((?S gnus-tmp-subject ?s)
1105     (?N gnus-tmp-number ?d)
1106     (?u gnus-tmp-user-defined ?s)))
1107
1108 (defvar gnus-summary-mode-line-format-alist
1109   `((?G gnus-tmp-group-name ?s)
1110     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1111     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1112     (?A gnus-tmp-article-number ?d)
1113     (?Z gnus-tmp-unread-and-unselected ?s)
1114     (?V gnus-version ?s)
1115     (?U gnus-tmp-unread-and-unticked ?d)
1116     (?S gnus-tmp-subject ?s)
1117     (?e gnus-tmp-unselected ?d)
1118     (?u gnus-tmp-user-defined ?s)
1119     (?d (length gnus-newsgroup-dormant) ?d)
1120     (?t (length gnus-newsgroup-marked) ?d)
1121     (?r (length gnus-newsgroup-reads) ?d)
1122     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1123     (?E gnus-newsgroup-expunged-tally ?d)
1124     (?s (gnus-current-score-file-nondirectory) ?s)))
1125
1126 (defvar gnus-last-search-regexp nil
1127   "Default regexp for article search command.")
1128
1129 (defvar gnus-summary-search-article-matched-data nil
1130   "Last matched data of article search command.  It is the local variable
1131 in `gnus-article-buffer' which consists of the list of start position,
1132 end position and text.")
1133
1134 (defvar gnus-last-shell-command nil
1135   "Default shell command on article.")
1136
1137 (defvar gnus-newsgroup-begin nil)
1138 (defvar gnus-newsgroup-end nil)
1139 (defvar gnus-newsgroup-last-rmail nil)
1140 (defvar gnus-newsgroup-last-mail nil)
1141 (defvar gnus-newsgroup-last-folder nil)
1142 (defvar gnus-newsgroup-last-file nil)
1143 (defvar gnus-newsgroup-auto-expire nil)
1144 (defvar gnus-newsgroup-active nil)
1145
1146 (defvar gnus-newsgroup-data nil)
1147 (defvar gnus-newsgroup-data-reverse nil)
1148 (defvar gnus-newsgroup-limit nil)
1149 (defvar gnus-newsgroup-limits nil)
1150
1151 (defvar gnus-newsgroup-unreads nil
1152   "List of unread articles in the current newsgroup.")
1153
1154 (defvar gnus-newsgroup-unselected nil
1155   "List of unselected unread articles in the current newsgroup.")
1156
1157 (defvar gnus-newsgroup-reads nil
1158   "Alist of read articles and article marks in the current newsgroup.")
1159
1160 (defvar gnus-newsgroup-expunged-tally nil)
1161
1162 (defvar gnus-newsgroup-marked nil
1163   "List of ticked articles in the current newsgroup (a subset of unread art).")
1164
1165 (defvar gnus-newsgroup-killed nil
1166   "List of ranges of articles that have been through the scoring process.")
1167
1168 (defvar gnus-newsgroup-cached nil
1169   "List of articles that come from the article cache.")
1170
1171 (defvar gnus-newsgroup-saved nil
1172   "List of articles that have been saved.")
1173
1174 (defvar gnus-newsgroup-kill-headers nil)
1175
1176 (defvar gnus-newsgroup-replied nil
1177   "List of articles that have been replied to in the current newsgroup.")
1178
1179 (defvar gnus-newsgroup-forwarded nil
1180   "List of articles that have been forwarded in the current newsgroup.")
1181
1182 (defvar gnus-newsgroup-expirable nil
1183   "List of articles in the current newsgroup that can be expired.")
1184
1185 (defvar gnus-newsgroup-processable nil
1186   "List of articles in the current newsgroup that can be processed.")
1187
1188 (defvar gnus-newsgroup-downloadable nil
1189   "List of articles in the current newsgroup that can be processed.")
1190
1191 (defvar gnus-newsgroup-undownloaded nil
1192   "List of articles in the current newsgroup that haven't been downloaded..")
1193
1194 (defvar gnus-newsgroup-unsendable nil
1195   "List of articles in the current newsgroup that won't be sent.")
1196
1197 (defvar gnus-newsgroup-bookmarks nil
1198   "List of articles in the current newsgroup that have bookmarks.")
1199
1200 (defvar gnus-newsgroup-dormant nil
1201   "List of dormant articles in the current newsgroup.")
1202
1203 (defvar gnus-newsgroup-scored nil
1204   "List of scored articles in the current newsgroup.")
1205
1206 (defvar gnus-newsgroup-incorporated nil
1207   "List of incorporated articles in the current newsgroup.")
1208
1209 (defvar gnus-newsgroup-headers nil
1210   "List of article headers in the current newsgroup.")
1211
1212 (defvar gnus-newsgroup-threads nil)
1213
1214 (defvar gnus-newsgroup-prepared nil
1215   "Whether the current group has been prepared properly.")
1216
1217 (defvar gnus-newsgroup-ancient nil
1218   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1219
1220 (defvar gnus-newsgroup-sparse nil)
1221
1222 (defvar gnus-current-article nil)
1223 (defvar gnus-article-current nil)
1224 (defvar gnus-current-headers nil)
1225 (defvar gnus-have-all-headers nil)
1226 (defvar gnus-last-article nil)
1227 (defvar gnus-newsgroup-history nil)
1228 (defvar gnus-newsgroup-charset nil)
1229 (defvar gnus-newsgroup-ephemeral-charset nil)
1230 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1231
1232 (defvar gnus-article-before-search nil)
1233
1234 (defconst gnus-summary-local-variables
1235   '(gnus-newsgroup-name
1236     gnus-newsgroup-begin gnus-newsgroup-end
1237     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1238     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1239     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1240     gnus-newsgroup-unselected gnus-newsgroup-marked
1241     gnus-newsgroup-reads gnus-newsgroup-saved
1242     gnus-newsgroup-replied gnus-newsgroup-forwarded
1243     gnus-newsgroup-expirable
1244     gnus-newsgroup-processable gnus-newsgroup-killed
1245     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1246     gnus-newsgroup-unsendable
1247     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1248     gnus-newsgroup-headers gnus-newsgroup-threads
1249     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1250     gnus-current-article gnus-current-headers gnus-have-all-headers
1251     gnus-last-article gnus-article-internal-prepare-hook
1252     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1253     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1254     gnus-thread-expunge-below
1255     gnus-score-alist gnus-current-score-file
1256     (gnus-summary-expunge-below . global)
1257     (gnus-summary-mark-below . global)
1258     (gnus-orphan-score . global)
1259     gnus-newsgroup-active gnus-scores-exclude-files
1260     gnus-newsgroup-history gnus-newsgroup-ancient
1261     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1262     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1263     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1264     (gnus-newsgroup-expunged-tally . 0)
1265     gnus-cache-removable-articles gnus-newsgroup-cached
1266     gnus-newsgroup-data gnus-newsgroup-data-reverse
1267     gnus-newsgroup-limit gnus-newsgroup-limits
1268     gnus-newsgroup-charset
1269     gnus-newsgroup-incorporated)
1270   "Variables that are buffer-local to the summary buffers.")
1271
1272 (defvar gnus-newsgroup-variables nil
1273   "Variables that have separate values in the newsgroups.")
1274
1275 ;; Byte-compiler warning.
1276 (eval-when-compile (defvar gnus-article-mode-map))
1277
1278 ;; Subject simplification.
1279
1280 (defun gnus-simplify-whitespace (str)
1281   "Remove excessive whitespace from STR."
1282   (let ((mystr str))
1283     ;; Multiple spaces.
1284     (while (string-match "[ \t][ \t]+" mystr)
1285       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1286                           " "
1287                           (substring mystr (match-end 0)))))
1288     ;; Leading spaces.
1289     (when (string-match "^[ \t]+" mystr)
1290       (setq mystr (substring mystr (match-end 0))))
1291     ;; Trailing spaces.
1292     (when (string-match "[ \t]+$" mystr)
1293       (setq mystr (substring mystr 0 (match-beginning 0))))
1294     mystr))
1295
1296 (defsubst gnus-simplify-subject-re (subject)
1297   "Remove \"Re:\" from subject lines."
1298   (if (string-match message-subject-re-regexp subject)
1299       (substring subject (match-end 0))
1300     subject))
1301
1302 (defun gnus-simplify-subject (subject &optional re-only)
1303   "Remove `Re:' and words in parentheses.
1304 If RE-ONLY is non-nil, strip leading `Re:'s only."
1305   (let ((case-fold-search t))           ;Ignore case.
1306     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1307     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1308       (setq subject (substring subject (match-end 0))))
1309     ;; Remove uninteresting prefixes.
1310     (when (and (not re-only)
1311                gnus-simplify-ignored-prefixes
1312                (string-match gnus-simplify-ignored-prefixes subject))
1313       (setq subject (substring subject (match-end 0))))
1314     ;; Remove words in parentheses from end.
1315     (unless re-only
1316       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1317         (setq subject (substring subject 0 (match-beginning 0)))))
1318     ;; Return subject string.
1319     subject))
1320
1321 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1322 ;; all whitespace.
1323 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1324   (goto-char (point-min))
1325   (while (re-search-forward regexp nil t)
1326     (replace-match (or newtext ""))))
1327
1328 (defun gnus-simplify-buffer-fuzzy ()
1329   "Simplify string in the buffer fuzzily.
1330 The string in the accessible portion of the current buffer is simplified.
1331 It is assumed to be a single-line subject.
1332 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1333 matter is removed.  Additional things can be deleted by setting
1334 `gnus-simplify-subject-fuzzy-regexp'."
1335   (let ((case-fold-search t)
1336         (modified-tick))
1337     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1338
1339     (while (not (eq modified-tick (buffer-modified-tick)))
1340       (setq modified-tick (buffer-modified-tick))
1341       (cond
1342        ((listp gnus-simplify-subject-fuzzy-regexp)
1343         (mapcar 'gnus-simplify-buffer-fuzzy-step
1344                 gnus-simplify-subject-fuzzy-regexp))
1345        (gnus-simplify-subject-fuzzy-regexp
1346         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1347       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1348       (gnus-simplify-buffer-fuzzy-step
1349        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1350       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1351
1352     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1353     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1354     (gnus-simplify-buffer-fuzzy-step " $")
1355     (gnus-simplify-buffer-fuzzy-step "^ +")))
1356
1357 (defun gnus-simplify-subject-fuzzy (subject)
1358   "Simplify a subject string fuzzily.
1359 See `gnus-simplify-buffer-fuzzy' for details."
1360   (save-excursion
1361     (gnus-set-work-buffer)
1362     (let ((case-fold-search t))
1363       ;; Remove uninteresting prefixes.
1364       (when (and gnus-simplify-ignored-prefixes
1365                  (string-match gnus-simplify-ignored-prefixes subject))
1366         (setq subject (substring subject (match-end 0))))
1367       (insert subject)
1368       (inline (gnus-simplify-buffer-fuzzy))
1369       (buffer-string))))
1370
1371 (defsubst gnus-simplify-subject-fully (subject)
1372   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1373   (cond
1374    (gnus-simplify-subject-functions
1375     (gnus-map-function gnus-simplify-subject-functions subject))
1376    ((null gnus-summary-gather-subject-limit)
1377     (gnus-simplify-subject-re subject))
1378    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1379     (gnus-simplify-subject-fuzzy subject))
1380    ((numberp gnus-summary-gather-subject-limit)
1381     (gnus-limit-string (gnus-simplify-subject-re subject)
1382                        gnus-summary-gather-subject-limit))
1383    (t
1384     subject)))
1385
1386 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1387   "Check whether two subjects are equal.
1388 If optional argument simple-first is t, first argument is already
1389 simplified."
1390   (cond
1391    ((null simple-first)
1392     (equal (gnus-simplify-subject-fully s1)
1393            (gnus-simplify-subject-fully s2)))
1394    (t
1395     (equal s1
1396            (gnus-simplify-subject-fully s2)))))
1397
1398 (defun gnus-summary-bubble-group ()
1399   "Increase the score of the current group.
1400 This is a handy function to add to `gnus-summary-exit-hook' to
1401 increase the score of each group you read."
1402   (gnus-group-add-score gnus-newsgroup-name))
1403
1404 \f
1405 ;;;
1406 ;;; Gnus summary mode
1407 ;;;
1408
1409 (put 'gnus-summary-mode 'mode-class 'special)
1410
1411 (defvar gnus-article-commands-menu)
1412
1413 (when t
1414   ;; Non-orthogonal keys
1415
1416   (gnus-define-keys gnus-summary-mode-map
1417     " " gnus-summary-next-page
1418     "\177" gnus-summary-prev-page
1419     [delete] gnus-summary-prev-page
1420     [backspace] gnus-summary-prev-page
1421     "\r" gnus-summary-scroll-up
1422     "\M-\r" gnus-summary-scroll-down
1423     "n" gnus-summary-next-unread-article
1424     "p" gnus-summary-prev-unread-article
1425     "N" gnus-summary-next-article
1426     "P" gnus-summary-prev-article
1427     "\M-\C-n" gnus-summary-next-same-subject
1428     "\M-\C-p" gnus-summary-prev-same-subject
1429     "\M-n" gnus-summary-next-unread-subject
1430     "\M-p" gnus-summary-prev-unread-subject
1431     "." gnus-summary-first-unread-article
1432     "," gnus-summary-best-unread-article
1433     "\M-s" gnus-summary-search-article-forward
1434     "\M-r" gnus-summary-search-article-backward
1435     "<" gnus-summary-beginning-of-article
1436     ">" gnus-summary-end-of-article
1437     "j" gnus-summary-goto-article
1438     "^" gnus-summary-refer-parent-article
1439     "\M-^" gnus-summary-refer-article
1440     "u" gnus-summary-tick-article-forward
1441     "!" gnus-summary-tick-article-forward
1442     "U" gnus-summary-tick-article-backward
1443     "d" gnus-summary-mark-as-read-forward
1444     "D" gnus-summary-mark-as-read-backward
1445     "E" gnus-summary-mark-as-expirable
1446     "\M-u" gnus-summary-clear-mark-forward
1447     "\M-U" gnus-summary-clear-mark-backward
1448     "k" gnus-summary-kill-same-subject-and-select
1449     "\C-k" gnus-summary-kill-same-subject
1450     "\M-\C-k" gnus-summary-kill-thread
1451     "\M-\C-l" gnus-summary-lower-thread
1452     "e" gnus-summary-edit-article
1453     "#" gnus-summary-mark-as-processable
1454     "\M-#" gnus-summary-unmark-as-processable
1455     "\M-\C-t" gnus-summary-toggle-threads
1456     "\M-\C-s" gnus-summary-show-thread
1457     "\M-\C-h" gnus-summary-hide-thread
1458     "\M-\C-f" gnus-summary-next-thread
1459     "\M-\C-b" gnus-summary-prev-thread
1460     [(meta down)] gnus-summary-next-thread
1461     [(meta up)] gnus-summary-prev-thread
1462     "\M-\C-u" gnus-summary-up-thread
1463     "\M-\C-d" gnus-summary-down-thread
1464     "&" gnus-summary-execute-command
1465     "c" gnus-summary-catchup-and-exit
1466     "\C-w" gnus-summary-mark-region-as-read
1467     "\C-t" gnus-summary-toggle-truncation
1468     "?" gnus-summary-mark-as-dormant
1469     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1470     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1471     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1472     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1473     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1474     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1475     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1476     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1477     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1478     "=" gnus-summary-expand-window
1479     "\C-x\C-s" gnus-summary-reselect-current-group
1480     "\M-g" gnus-summary-rescan-group
1481     "w" gnus-summary-stop-page-breaking
1482     "\C-c\C-r" gnus-summary-caesar-message
1483     "\M-t" gnus-summary-toggle-mime
1484     "f" gnus-summary-followup
1485     "F" gnus-summary-followup-with-original
1486     "C" gnus-summary-cancel-article
1487     "r" gnus-summary-reply
1488     "R" gnus-summary-reply-with-original
1489     "\C-c\C-f" gnus-summary-mail-forward
1490     "o" gnus-summary-save-article
1491     "\C-o" gnus-summary-save-article-mail
1492     "|" gnus-summary-pipe-output
1493     "\M-k" gnus-summary-edit-local-kill
1494     "\M-K" gnus-summary-edit-global-kill
1495     ;; "V" gnus-version
1496     "\C-c\C-d" gnus-summary-describe-group
1497     "q" gnus-summary-exit
1498     "Q" gnus-summary-exit-no-update
1499     "\C-c\C-i" gnus-info-find-node
1500     gnus-mouse-2 gnus-mouse-pick-article
1501     "m" gnus-summary-mail-other-window
1502     "a" gnus-summary-post-news
1503     "x" gnus-summary-limit-to-unread
1504     "s" gnus-summary-isearch-article
1505     "t" gnus-article-toggle-headers
1506     "g" gnus-summary-show-article
1507     "l" gnus-summary-goto-last-article
1508     "v" gnus-summary-preview-mime-message
1509     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1510     "\C-d" gnus-summary-enter-digest-group
1511     "\M-\C-d" gnus-summary-read-document
1512     "\M-\C-e" gnus-summary-edit-parameters
1513     "\M-\C-a" gnus-summary-customize-parameters
1514     "\C-c\C-b" gnus-bug
1515     "*" gnus-cache-enter-article
1516     "\M-*" gnus-cache-remove-article
1517     "\M-&" gnus-summary-universal-argument
1518     "\C-l" gnus-recenter
1519     "I" gnus-summary-increase-score
1520     "L" gnus-summary-lower-score
1521     "\M-i" gnus-symbolic-argument
1522     "h" gnus-summary-select-article-buffer
1523
1524     "V" gnus-summary-score-map
1525     "X" gnus-uu-extract-map
1526     "S" gnus-summary-send-map)
1527
1528   ;; Sort of orthogonal keymap
1529   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1530     "t" gnus-summary-tick-article-forward
1531     "!" gnus-summary-tick-article-forward
1532     "d" gnus-summary-mark-as-read-forward
1533     "r" gnus-summary-mark-as-read-forward
1534     "c" gnus-summary-clear-mark-forward
1535     " " gnus-summary-clear-mark-forward
1536     "e" gnus-summary-mark-as-expirable
1537     "x" gnus-summary-mark-as-expirable
1538     "?" gnus-summary-mark-as-dormant
1539     "b" gnus-summary-set-bookmark
1540     "B" gnus-summary-remove-bookmark
1541     "#" gnus-summary-mark-as-processable
1542     "\M-#" gnus-summary-unmark-as-processable
1543     "S" gnus-summary-limit-include-expunged
1544     "C" gnus-summary-catchup
1545     "H" gnus-summary-catchup-to-here
1546     "\C-c" gnus-summary-catchup-all
1547     "k" gnus-summary-kill-same-subject-and-select
1548     "K" gnus-summary-kill-same-subject
1549     "P" gnus-uu-mark-map)
1550
1551   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1552     "c" gnus-summary-clear-above
1553     "u" gnus-summary-tick-above
1554     "m" gnus-summary-mark-above
1555     "k" gnus-summary-kill-below)
1556
1557   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1558     "/" gnus-summary-limit-to-subject
1559     "n" gnus-summary-limit-to-articles
1560     "w" gnus-summary-pop-limit
1561     "s" gnus-summary-limit-to-subject
1562     "a" gnus-summary-limit-to-author
1563     "u" gnus-summary-limit-to-unread
1564     "m" gnus-summary-limit-to-marks
1565     "M" gnus-summary-limit-exclude-marks
1566     "v" gnus-summary-limit-to-score
1567     "*" gnus-summary-limit-include-cached
1568     "D" gnus-summary-limit-include-dormant
1569     "T" gnus-summary-limit-include-thread
1570     "d" gnus-summary-limit-exclude-dormant
1571     "t" gnus-summary-limit-to-age
1572     "x" gnus-summary-limit-to-extra
1573     "E" gnus-summary-limit-include-expunged
1574     "c" gnus-summary-limit-exclude-childless-dormant
1575     "C" gnus-summary-limit-mark-excluded-as-read
1576     "o" gnus-summary-insert-old-articles
1577     "N" gnus-summary-insert-new-articles)
1578
1579   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1580     "n" gnus-summary-next-unread-article
1581     "p" gnus-summary-prev-unread-article
1582     "N" gnus-summary-next-article
1583     "P" gnus-summary-prev-article
1584     "\C-n" gnus-summary-next-same-subject
1585     "\C-p" gnus-summary-prev-same-subject
1586     "\M-n" gnus-summary-next-unread-subject
1587     "\M-p" gnus-summary-prev-unread-subject
1588     "f" gnus-summary-first-unread-article
1589     "b" gnus-summary-best-unread-article
1590     "j" gnus-summary-goto-article
1591     "g" gnus-summary-goto-subject
1592     "l" gnus-summary-goto-last-article
1593     "o" gnus-summary-pop-article)
1594
1595   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1596     "k" gnus-summary-kill-thread
1597     "l" gnus-summary-lower-thread
1598     "i" gnus-summary-raise-thread
1599     "T" gnus-summary-toggle-threads
1600     "t" gnus-summary-rethread-current
1601     "^" gnus-summary-reparent-thread
1602     "s" gnus-summary-show-thread
1603     "S" gnus-summary-show-all-threads
1604     "h" gnus-summary-hide-thread
1605     "H" gnus-summary-hide-all-threads
1606     "n" gnus-summary-next-thread
1607     "p" gnus-summary-prev-thread
1608     "u" gnus-summary-up-thread
1609     "o" gnus-summary-top-thread
1610     "d" gnus-summary-down-thread
1611     "#" gnus-uu-mark-thread
1612     "\M-#" gnus-uu-unmark-thread)
1613
1614   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1615     "g" gnus-summary-prepare
1616     "c" gnus-summary-insert-cached-articles)
1617
1618   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1619     "c" gnus-summary-catchup-and-exit
1620     "C" gnus-summary-catchup-all-and-exit
1621     "E" gnus-summary-exit-no-update
1622     "J" gnus-summary-jump-to-other-group
1623     "Q" gnus-summary-exit
1624     "Z" gnus-summary-exit
1625     "n" gnus-summary-catchup-and-goto-next-group
1626     "R" gnus-summary-reselect-current-group
1627     "G" gnus-summary-rescan-group
1628     "N" gnus-summary-next-group
1629     "s" gnus-summary-save-newsrc
1630     "P" gnus-summary-prev-group)
1631
1632   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1633     " " gnus-summary-next-page
1634     "n" gnus-summary-next-page
1635     "\177" gnus-summary-prev-page
1636     [delete] gnus-summary-prev-page
1637     "p" gnus-summary-prev-page
1638     "\r" gnus-summary-scroll-up
1639     "\M-\r" gnus-summary-scroll-down
1640     "<" gnus-summary-beginning-of-article
1641     ">" gnus-summary-end-of-article
1642     "b" gnus-summary-beginning-of-article
1643     "e" gnus-summary-end-of-article
1644     "^" gnus-summary-refer-parent-article
1645     "r" gnus-summary-refer-parent-article
1646     "D" gnus-summary-enter-digest-group
1647     "R" gnus-summary-refer-references
1648     "T" gnus-summary-refer-thread
1649     "g" gnus-summary-show-article
1650     "s" gnus-summary-isearch-article
1651     "P" gnus-summary-print-article
1652     "M" gnus-mailing-list-insinuate
1653     "t" gnus-article-babel)
1654
1655   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1656     "b" gnus-article-add-buttons
1657     "B" gnus-article-add-buttons-to-head
1658     "o" gnus-article-treat-overstrike
1659     "e" gnus-article-emphasize
1660     "w" gnus-article-fill-cited-article
1661     "Q" gnus-article-fill-long-lines
1662     "C" gnus-article-capitalize-sentences
1663     "c" gnus-article-remove-cr
1664     "Z" gnus-article-decode-HZ
1665     "f" gnus-article-display-x-face
1666     "l" gnus-summary-stop-page-breaking
1667     "r" gnus-summary-caesar-message
1668     "t" gnus-article-toggle-headers
1669     "v" gnus-summary-verbose-headers
1670     "m" gnus-summary-toggle-mime
1671     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1672     "p" gnus-article-verify-x-pgp-sig
1673     "d" gnus-article-treat-dumbquotes
1674     "s" gnus-smiley-display)
1675
1676   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1677     "a" gnus-article-hide
1678     "h" gnus-article-toggle-headers
1679     "b" gnus-article-hide-boring-headers
1680     "s" gnus-article-hide-signature
1681     "c" gnus-article-hide-citation
1682     "C" gnus-article-hide-citation-in-followups
1683     "l" gnus-article-hide-list-identifiers
1684     "p" gnus-article-hide-pgp
1685     "B" gnus-article-strip-banner
1686     "P" gnus-article-hide-pem
1687     "\C-c" gnus-article-hide-citation-maybe)
1688
1689   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1690     "a" gnus-article-highlight
1691     "h" gnus-article-highlight-headers
1692     "c" gnus-article-highlight-citation
1693     "s" gnus-article-highlight-signature)
1694
1695   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1696     "z" gnus-article-date-ut
1697     "u" gnus-article-date-ut
1698     "l" gnus-article-date-local
1699     "p" gnus-article-date-english
1700     "e" gnus-article-date-lapsed
1701     "o" gnus-article-date-original
1702     "i" gnus-article-date-iso8601
1703     "s" gnus-article-date-user)
1704
1705   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1706     "t" gnus-article-remove-trailing-blank-lines
1707     "l" gnus-article-strip-leading-blank-lines
1708     "m" gnus-article-strip-multiple-blank-lines
1709     "a" gnus-article-strip-blank-lines
1710     "A" gnus-article-strip-all-blank-lines
1711     "s" gnus-article-strip-leading-space
1712     "e" gnus-article-strip-trailing-space
1713     "w" gnus-article-remove-leading-whitespace)
1714
1715   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1716     "v" gnus-version
1717     "f" gnus-summary-fetch-faq
1718     "d" gnus-summary-describe-group
1719     "h" gnus-summary-describe-briefly
1720     "i" gnus-info-find-node)
1721
1722   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1723     "e" gnus-summary-expire-articles
1724     "\M-\C-e" gnus-summary-expire-articles-now
1725     "\177" gnus-summary-delete-article
1726     [delete] gnus-summary-delete-article
1727     [backspace] gnus-summary-delete-article
1728     "m" gnus-summary-move-article
1729     "r" gnus-summary-respool-article
1730     "w" gnus-summary-edit-article
1731     "c" gnus-summary-copy-article
1732     "B" gnus-summary-crosspost-article
1733     "q" gnus-summary-respool-query
1734     "t" gnus-summary-respool-trace
1735     "i" gnus-summary-import-article
1736     "I" gnus-summary-create-article
1737     "p" gnus-summary-article-posted-p)
1738
1739   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1740     "o" gnus-summary-save-article
1741     "m" gnus-summary-save-article-mail
1742     "F" gnus-summary-write-article-file
1743     "r" gnus-summary-save-article-rmail
1744     "f" gnus-summary-save-article-file
1745     "b" gnus-summary-save-article-body-file
1746     "h" gnus-summary-save-article-folder
1747     "v" gnus-summary-save-article-vm
1748     "p" gnus-summary-pipe-output
1749     "s" gnus-soup-add-article)
1750
1751   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1752     "b" gnus-summary-display-buttonized
1753     "m" gnus-summary-repair-multipart
1754     "v" gnus-article-view-part
1755     "o" gnus-article-save-part
1756     "c" gnus-article-copy-part
1757     "C" gnus-article-view-part-as-charset
1758     "e" gnus-article-externalize-part
1759     "E" gnus-article-encrypt-body
1760     "i" gnus-article-inline-part
1761     "|" gnus-article-pipe-part))
1762
1763 (defvar gnus-article-post-menu nil)
1764
1765 (defun gnus-summary-make-menu-bar ()
1766   (gnus-turn-off-edit-menu 'summary)
1767
1768   (unless (boundp 'gnus-summary-misc-menu)
1769
1770     (easy-menu-define
1771      gnus-summary-kill-menu gnus-summary-mode-map ""
1772      (cons
1773       "Score"
1774       (nconc
1775        (list
1776         ["Customize" gnus-score-customize t])
1777        (gnus-make-score-map 'increase)
1778        (gnus-make-score-map 'lower)
1779        '(("Mark"
1780           ["Kill below" gnus-summary-kill-below t]
1781           ["Mark above" gnus-summary-mark-above t]
1782           ["Tick above" gnus-summary-tick-above t]
1783           ["Clear above" gnus-summary-clear-above t])
1784          ["Current score" gnus-summary-current-score t]
1785          ["Set score" gnus-summary-set-score t]
1786          ["Switch current score file..." gnus-score-change-score-file t]
1787          ["Set mark below..." gnus-score-set-mark-below t]
1788          ["Set expunge below..." gnus-score-set-expunge-below t]
1789          ["Edit current score file" gnus-score-edit-current-scores t]
1790          ["Edit score file" gnus-score-edit-file t]
1791          ["Trace score" gnus-score-find-trace t]
1792          ["Find words" gnus-score-find-favourite-words t]
1793          ["Rescore buffer" gnus-summary-rescore t]
1794          ["Increase score..." gnus-summary-increase-score t]
1795          ["Lower score..." gnus-summary-lower-score t]))))
1796
1797     ;; Define both the Article menu in the summary buffer and the equivalent
1798     ;; Commands menu in the article buffer here for consistency.
1799     (let ((innards
1800            `(("Hide"
1801               ["All" gnus-article-hide t]
1802               ["Headers" gnus-article-toggle-headers t]
1803               ["Signature" gnus-article-hide-signature t]
1804               ["Citation" gnus-article-hide-citation t]
1805               ["List identifiers" gnus-article-hide-list-identifiers t]
1806               ["PGP" gnus-article-hide-pgp t]
1807               ["Banner" gnus-article-strip-banner t]
1808               ["Boring headers" gnus-article-hide-boring-headers t])
1809              ("Highlight"
1810               ["All" gnus-article-highlight t]
1811               ["Headers" gnus-article-highlight-headers t]
1812               ["Signature" gnus-article-highlight-signature t]
1813               ["Citation" gnus-article-highlight-citation t])
1814              ("Date"
1815               ["Local" gnus-article-date-local t]
1816               ["ISO8601" gnus-article-date-iso8601 t]
1817               ["UT" gnus-article-date-ut t]
1818               ["Original" gnus-article-date-original t]
1819               ["Lapsed" gnus-article-date-lapsed t]
1820               ["User-defined" gnus-article-date-user t])
1821              ("Washing"
1822               ("Remove Blanks"
1823                ["Leading" gnus-article-strip-leading-blank-lines t]
1824                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1825                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1826                ["All of the above" gnus-article-strip-blank-lines t]
1827                ["All" gnus-article-strip-all-blank-lines t]
1828                ["Leading space" gnus-article-strip-leading-space t]
1829                ["Trailing space" gnus-article-strip-trailing-space t]
1830                ["Leading space in headers" 
1831                 gnus-article-remove-leading-whitespace t])
1832               ["Overstrike" gnus-article-treat-overstrike t]
1833               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1834               ["Emphasis" gnus-article-emphasize t]
1835               ["Word wrap" gnus-article-fill-cited-article t]
1836               ["Fill long lines" gnus-article-fill-long-lines t]
1837               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1838               ["CR" gnus-article-remove-cr t]
1839               ["Show X-Face" gnus-article-display-x-face t]
1840               ["Rot 13" gnus-summary-caesar-message
1841                ,@(if (featurep 'xemacs) '(t)
1842                    '(:help "\"Caesar rotate\" article by 13"))]
1843               ["Unix pipe" gnus-summary-pipe-message t]
1844               ["Add buttons" gnus-article-add-buttons t]
1845               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1846               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1847               ["Toggle MIME" gnus-summary-toggle-mime t]
1848               ["Verbose header" gnus-summary-verbose-headers t]
1849               ["Toggle header" gnus-summary-toggle-header t]
1850               ["Toggle smileys" gnus-smiley-display t]
1851               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1852               ["HZ" gnus-article-decode-HZ t])
1853              ("Output"
1854               ["Save in default format" gnus-summary-save-article
1855                ,@(if (featurep 'xemacs) '(t)
1856                    '(:help "Save article using default method"))]
1857               ["Save in file" gnus-summary-save-article-file
1858                ,@(if (featurep 'xemacs) '(t)
1859                    '(:help "Save article in file"))]
1860               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1861               ["Save in MH folder" gnus-summary-save-article-folder t]
1862               ["Save in VM folder" gnus-summary-save-article-vm t]
1863               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1864               ["Save body in file" gnus-summary-save-article-body-file t]
1865               ["Pipe through a filter" gnus-summary-pipe-output t]
1866               ["Add to SOUP packet" gnus-soup-add-article t]
1867               ["Print" gnus-summary-print-article t])
1868              ("Backend"
1869               ["Respool article..." gnus-summary-respool-article t]
1870               ["Move article..." gnus-summary-move-article
1871                (gnus-check-backend-function
1872                 'request-move-article gnus-newsgroup-name)]
1873               ["Copy article..." gnus-summary-copy-article t]
1874               ["Crosspost article..." gnus-summary-crosspost-article
1875                (gnus-check-backend-function
1876                 'request-replace-article gnus-newsgroup-name)]
1877               ["Import file..." gnus-summary-import-article t]
1878               ["Create article..." gnus-summary-create-article t]
1879               ["Check if posted" gnus-summary-article-posted-p t]
1880               ["Edit article" gnus-summary-edit-article
1881                (not (gnus-group-read-only-p))]
1882               ["Delete article" gnus-summary-delete-article
1883                (gnus-check-backend-function
1884                 'request-expire-articles gnus-newsgroup-name)]
1885               ["Query respool" gnus-summary-respool-query t]
1886               ["Trace respool" gnus-summary-respool-trace t]
1887               ["Delete expirable articles" gnus-summary-expire-articles-now
1888                (gnus-check-backend-function
1889                 'request-expire-articles gnus-newsgroup-name)])
1890              ("Extract"
1891               ["Uudecode" gnus-uu-decode-uu
1892                ,@(if (featurep 'xemacs) '(t)
1893                    '(:help "Decode uuencoded article(s)"))]
1894               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1895               ["Unshar" gnus-uu-decode-unshar t]
1896               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1897               ["Save" gnus-uu-decode-save t]
1898               ["Binhex" gnus-uu-decode-binhex t]
1899               ["Postscript" gnus-uu-decode-postscript t])
1900              ("Cache"
1901               ["Enter article" gnus-cache-enter-article t]
1902               ["Remove article" gnus-cache-remove-article t])
1903              ["Translate" gnus-article-babel t]
1904              ["Select article buffer" gnus-summary-select-article-buffer t]
1905              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1906              ["Isearch article..." gnus-summary-isearch-article t]
1907              ["Beginning of the article" gnus-summary-beginning-of-article t]
1908              ["End of the article" gnus-summary-end-of-article t]
1909              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1910              ["Fetch referenced articles" gnus-summary-refer-references t]
1911              ["Fetch current thread" gnus-summary-refer-thread t]
1912              ["Fetch article with id..." gnus-summary-refer-article t]
1913              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1914              ["Redisplay" gnus-summary-show-article t])))
1915       (easy-menu-define
1916        gnus-summary-article-menu gnus-summary-mode-map ""
1917        (cons "Article" innards))
1918
1919       (if (not (keymapp gnus-summary-article-menu))
1920           (easy-menu-define
1921             gnus-article-commands-menu gnus-article-mode-map ""
1922             (cons "Commands" innards))
1923         ;; in Emacs, don't share menu.
1924         (setq gnus-article-commands-menu 
1925               (copy-keymap gnus-summary-article-menu))
1926         (define-key gnus-article-mode-map [menu-bar commands]
1927           (cons "Commands" gnus-article-commands-menu))))
1928
1929     (easy-menu-define
1930      gnus-summary-thread-menu gnus-summary-mode-map ""
1931      '("Threads"
1932        ["Toggle threading" gnus-summary-toggle-threads t]
1933        ["Hide threads" gnus-summary-hide-all-threads t]
1934        ["Show threads" gnus-summary-show-all-threads t]
1935        ["Hide thread" gnus-summary-hide-thread t]
1936        ["Show thread" gnus-summary-show-thread t]
1937        ["Go to next thread" gnus-summary-next-thread t]
1938        ["Go to previous thread" gnus-summary-prev-thread t]
1939        ["Go down thread" gnus-summary-down-thread t]
1940        ["Go up thread" gnus-summary-up-thread t]
1941        ["Top of thread" gnus-summary-top-thread t]
1942        ["Mark thread as read" gnus-summary-kill-thread t]
1943        ["Lower thread score" gnus-summary-lower-thread t]
1944        ["Raise thread score" gnus-summary-raise-thread t]
1945        ["Rethread current" gnus-summary-rethread-current t]))
1946
1947     (easy-menu-define
1948      gnus-summary-post-menu gnus-summary-mode-map ""
1949      `("Post"
1950        ["Post an article" gnus-summary-post-news
1951         ,@(if (featurep 'xemacs) '(t)
1952             '(:help "Post an article"))]
1953        ["Followup" gnus-summary-followup
1954         ,@(if (featurep 'xemacs) '(t)
1955             '(:help "Post followup to this article"))]
1956        ["Followup and yank" gnus-summary-followup-with-original
1957         ,@(if (featurep 'xemacs) '(t)
1958             '(:help "Post followup to this article, quoting its contents"))]
1959        ["Supersede article" gnus-summary-supersede-article t]
1960        ["Cancel article" gnus-summary-cancel-article
1961         ,@(if (featurep 'xemacs) '(t)
1962             '(:help "Cancel an article you posted"))]
1963        ["Reply" gnus-summary-reply t]
1964        ["Reply and yank" gnus-summary-reply-with-original t]
1965        ["Wide reply" gnus-summary-wide-reply t]
1966        ["Wide reply and yank" gnus-summary-wide-reply-with-original
1967         ,@(if (featurep 'xemacs) '(t)
1968             '(:help "Mail a reply, quoting this article"))]
1969        ["Mail forward" gnus-summary-mail-forward t]
1970        ["Post forward" gnus-summary-post-forward t]
1971        ["Digest and mail" gnus-summary-digest-mail-forward t]
1972        ["Digest and post" gnus-summary-digest-post-forward t]
1973        ["Resend message" gnus-summary-resend-message t]
1974        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1975        ["Send a mail" gnus-summary-mail-other-window t]
1976        ["Uuencode and post" gnus-uu-post-news
1977         ,@(if (featurep 'xemacs) '(t)
1978             '(:help "Post a uuencoded article"))]
1979        ["Followup via news" gnus-summary-followup-to-mail t]
1980        ["Followup via news and yank"
1981         gnus-summary-followup-to-mail-with-original t]
1982        ;;("Draft"
1983        ;;["Send" gnus-summary-send-draft t]
1984        ;;["Send bounced" gnus-resend-bounced-mail t])
1985        ))
1986
1987     (cond 
1988      ((not (keymapp gnus-summary-post-menu))
1989       (setq gnus-article-post-menu gnus-summary-post-menu))
1990      ((not gnus-article-post-menu)
1991       ;; Don't share post menu.
1992       (setq gnus-article-post-menu
1993             (copy-keymap gnus-summary-post-menu))))
1994     (define-key gnus-article-mode-map [menu-bar post]
1995       (cons "Post" gnus-article-post-menu))
1996
1997     (easy-menu-define
1998      gnus-summary-misc-menu gnus-summary-mode-map ""
1999      `("Misc"
2000        ("Mark Read"
2001         ["Mark as read" gnus-summary-mark-as-read-forward t]
2002         ["Mark same subject and select"
2003          gnus-summary-kill-same-subject-and-select t]
2004         ["Mark same subject" gnus-summary-kill-same-subject t]
2005         ["Catchup" gnus-summary-catchup
2006          ,@(if (featurep 'xemacs) '(t)
2007              '(:help "Mark unread articles in this group as read"))]
2008         ["Catchup all" gnus-summary-catchup-all t]
2009         ["Catchup to here" gnus-summary-catchup-to-here t]
2010         ["Catchup region" gnus-summary-mark-region-as-read t]
2011         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2012        ("Mark Various"
2013         ["Tick" gnus-summary-tick-article-forward t]
2014         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2015         ["Remove marks" gnus-summary-clear-mark-forward t]
2016         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2017         ["Set bookmark" gnus-summary-set-bookmark t]
2018         ["Remove bookmark" gnus-summary-remove-bookmark t])
2019        ("Mark Limit"
2020         ["Marks..." gnus-summary-limit-to-marks t]
2021         ["Subject..." gnus-summary-limit-to-subject t]
2022         ["Author..." gnus-summary-limit-to-author t]
2023         ["Age..." gnus-summary-limit-to-age t]
2024         ["Extra..." gnus-summary-limit-to-extra t]
2025         ["Score" gnus-summary-limit-to-score t]
2026         ["Unread" gnus-summary-limit-to-unread t]
2027         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2028         ["Articles" gnus-summary-limit-to-articles t]
2029         ["Pop limit" gnus-summary-pop-limit t]
2030         ["Show dormant" gnus-summary-limit-include-dormant t]
2031         ["Hide childless dormant"
2032          gnus-summary-limit-exclude-childless-dormant t]
2033         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2034         ["Hide marked" gnus-summary-limit-exclude-marks t]
2035         ["Show expunged" gnus-summary-limit-include-expunged t])
2036        ("Process Mark"
2037         ["Set mark" gnus-summary-mark-as-processable t]
2038         ["Remove mark" gnus-summary-unmark-as-processable t]
2039         ["Remove all marks" gnus-summary-unmark-all-processable t]
2040         ["Mark above" gnus-uu-mark-over t]
2041         ["Mark series" gnus-uu-mark-series t]
2042         ["Mark region" gnus-uu-mark-region t]
2043         ["Unmark region" gnus-uu-unmark-region t]
2044         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2045         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2046         ["Mark all" gnus-uu-mark-all t]
2047         ["Mark buffer" gnus-uu-mark-buffer t]
2048         ["Mark sparse" gnus-uu-mark-sparse t]
2049         ["Mark thread" gnus-uu-mark-thread t]
2050         ["Unmark thread" gnus-uu-unmark-thread t]
2051         ("Process Mark Sets"
2052          ["Kill" gnus-summary-kill-process-mark t]
2053          ["Yank" gnus-summary-yank-process-mark
2054           gnus-newsgroup-process-stack]
2055          ["Save" gnus-summary-save-process-mark t]))
2056        ("Scroll article"
2057         ["Page forward" gnus-summary-next-page
2058          ,@(if (featurep 'xemacs) '(t)
2059              '(:help "Show next page of article"))]
2060         ["Page backward" gnus-summary-prev-page
2061          ,@(if (featurep 'xemacs) '(t)
2062              '(:help "Show previous page of article"))]
2063         ["Line forward" gnus-summary-scroll-up t])
2064        ("Move"
2065         ["Next unread article" gnus-summary-next-unread-article t]
2066         ["Previous unread article" gnus-summary-prev-unread-article t]
2067         ["Next article" gnus-summary-next-article t]
2068         ["Previous article" gnus-summary-prev-article t]
2069         ["Next unread subject" gnus-summary-next-unread-subject t]
2070         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2071         ["Next article same subject" gnus-summary-next-same-subject t]
2072         ["Previous article same subject" gnus-summary-prev-same-subject t]
2073         ["First unread article" gnus-summary-first-unread-article t]
2074         ["Best unread article" gnus-summary-best-unread-article t]
2075         ["Go to subject number..." gnus-summary-goto-subject t]
2076         ["Go to article number..." gnus-summary-goto-article t]
2077         ["Go to the last article" gnus-summary-goto-last-article t]
2078         ["Pop article off history" gnus-summary-pop-article t])
2079        ("Sort"
2080         ["Sort by number" gnus-summary-sort-by-number t]
2081         ["Sort by author" gnus-summary-sort-by-author t]
2082         ["Sort by subject" gnus-summary-sort-by-subject t]
2083         ["Sort by date" gnus-summary-sort-by-date t]
2084         ["Sort by score" gnus-summary-sort-by-score t]
2085         ["Sort by lines" gnus-summary-sort-by-lines t]
2086         ["Sort by characters" gnus-summary-sort-by-chars t]
2087         ["Original sort" gnus-summary-sort-by-original t])
2088        ("Help"
2089         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2090         ["Describe group" gnus-summary-describe-group t]
2091         ["Read manual" gnus-info-find-node t])
2092        ("Modes"
2093         ["Pick and read" gnus-pick-mode t]
2094         ["Binary" gnus-binary-mode t])
2095        ("Regeneration"
2096         ["Regenerate" gnus-summary-prepare t]
2097         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2098         ["Toggle threading" gnus-summary-toggle-threads t])
2099        ["See old articles" gnus-summary-insert-old-articles t]
2100        ["See new articles" gnus-summary-insert-new-articles t]
2101        ["Filter articles..." gnus-summary-execute-command t]
2102        ["Run command on subjects..." gnus-summary-universal-argument t]
2103        ["Search articles forward..." gnus-summary-search-article-forward t]
2104        ["Search articles backward..." gnus-summary-search-article-backward t]
2105        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2106        ["Expand window" gnus-summary-expand-window t]
2107        ["Expire expirable articles" gnus-summary-expire-articles
2108         (gnus-check-backend-function
2109          'request-expire-articles gnus-newsgroup-name)]
2110        ["Edit local kill file" gnus-summary-edit-local-kill t]
2111        ["Edit main kill file" gnus-summary-edit-global-kill t]
2112        ["Edit group parameters" gnus-summary-edit-parameters t]
2113        ["Customize group parameters" gnus-summary-customize-parameters t]
2114        ["Send a bug report" gnus-bug t]
2115        ("Exit"
2116         ["Catchup and exit" gnus-summary-catchup-and-exit
2117          ,@(if (featurep 'xemacs) '(t)
2118              '(:help "Mark unread articles in this group as read, then exit"))]
2119         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2120         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2121         ["Exit group" gnus-summary-exit
2122          ,@(if (featurep 'xemacs) '(t)
2123              '(:help "Exit current group, return to group selection mode"))]
2124         ["Exit group without updating" gnus-summary-exit-no-update t]
2125         ["Exit and goto next group" gnus-summary-next-group t]
2126         ["Exit and goto prev group" gnus-summary-prev-group t]
2127         ["Reselect group" gnus-summary-reselect-current-group t]
2128         ["Rescan group" gnus-summary-rescan-group t]
2129         ["Update dribble" gnus-summary-save-newsrc t])))
2130
2131     (gnus-run-hooks 'gnus-summary-menu-hook)))
2132
2133 (defvar gnus-summary-tool-bar-map nil)
2134
2135 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2136 (defun gnus-summary-make-tool-bar ()
2137   (if (and (fboundp 'tool-bar-add-item-from-menu)
2138            (default-value 'tool-bar-mode)
2139            (not gnus-summary-tool-bar-map))
2140       (setq gnus-summary-tool-bar-map
2141             (let ((tool-bar-map (make-sparse-keymap))
2142                   (load-path (mm-image-load-path)))
2143               (tool-bar-add-item-from-menu
2144                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2145               (tool-bar-add-item-from-menu
2146                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2147               (tool-bar-add-item-from-menu
2148                'gnus-summary-post-news "post" gnus-summary-mode-map)
2149               (tool-bar-add-item-from-menu
2150                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2151               (tool-bar-add-item-from-menu
2152                'gnus-summary-followup "followup" gnus-summary-mode-map)
2153               (tool-bar-add-item-from-menu
2154                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2155               (tool-bar-add-item-from-menu
2156                'gnus-summary-reply "reply" gnus-summary-mode-map)
2157               (tool-bar-add-item-from-menu
2158                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2159               (tool-bar-add-item-from-menu
2160                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2161               (tool-bar-add-item-from-menu
2162                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2163               (tool-bar-add-item-from-menu
2164                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2165               (tool-bar-add-item-from-menu
2166                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2167               (tool-bar-add-item-from-menu
2168                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2169               (tool-bar-add-item-from-menu
2170                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2171               (tool-bar-add-item-from-menu
2172                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2173               tool-bar-map)))
2174   (if gnus-summary-tool-bar-map
2175       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2176
2177 (defun gnus-score-set-default (var value)
2178   "A version of set that updates the GNU Emacs menu-bar."
2179   (set var value)
2180   ;; It is the message that forces the active status to be updated.
2181   (message ""))
2182
2183 (defun gnus-make-score-map (type)
2184   "Make a summary score map of type TYPE."
2185   (if t
2186       nil
2187     (let ((headers '(("author" "from" string)
2188                      ("subject" "subject" string)
2189                      ("article body" "body" string)
2190                      ("article head" "head" string)
2191                      ("xref" "xref" string)
2192                      ("extra header" "extra" string)
2193                      ("lines" "lines" number)
2194                      ("followups to author" "followup" string)))
2195           (types '((number ("less than" <)
2196                            ("greater than" >)
2197                            ("equal" =))
2198                    (string ("substring" s)
2199                            ("exact string" e)
2200                            ("fuzzy string" f)
2201                            ("regexp" r))))
2202           (perms '(("temporary" (current-time-string))
2203                    ("permanent" nil)
2204                    ("immediate" now)))
2205           header)
2206       (list
2207        (apply
2208         'nconc
2209         (list
2210          (if (eq type 'lower)
2211              "Lower score"
2212            "Increase score"))
2213         (let (outh)
2214           (while headers
2215             (setq header (car headers))
2216             (setq outh
2217                   (cons
2218                    (apply
2219                     'nconc
2220                     (list (car header))
2221                     (let ((ts (cdr (assoc (nth 2 header) types)))
2222                           outt)
2223                       (while ts
2224                         (setq outt
2225                               (cons
2226                                (apply
2227                                 'nconc
2228                                 (list (caar ts))
2229                                 (let ((ps perms)
2230                                       outp)
2231                                   (while ps
2232                                     (setq outp
2233                                           (cons
2234                                            (vector
2235                                             (caar ps)
2236                                             (list
2237                                              'gnus-summary-score-entry
2238                                              (nth 1 header)
2239                                              (if (or (string= (nth 1 header)
2240                                                               "head")
2241                                                      (string= (nth 1 header)
2242                                                               "body"))
2243                                                  ""
2244                                                (list 'gnus-summary-header
2245                                                      (nth 1 header)))
2246                                              (list 'quote (nth 1 (car ts)))
2247                                              (list 'gnus-score-delta-default
2248                                                    nil)
2249                                              (nth 1 (car ps))
2250                                              t)
2251                                             t)
2252                                            outp))
2253                                     (setq ps (cdr ps)))
2254                                   (list (nreverse outp))))
2255                                outt))
2256                         (setq ts (cdr ts)))
2257                       (list (nreverse outt))))
2258                    outh))
2259             (setq headers (cdr headers)))
2260           (list (nreverse outh))))))))
2261
2262 \f
2263
2264 (defun gnus-summary-mode (&optional group)
2265   "Major mode for reading articles.
2266
2267 All normal editing commands are switched off.
2268 \\<gnus-summary-mode-map>
2269 Each line in this buffer represents one article.  To read an
2270 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2271 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2272 respectively.
2273
2274 You can also post articles and send mail from this buffer.  To
2275 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2276 of an article, type `\\[gnus-summary-reply]'.
2277
2278 There are approx. one gazillion commands you can execute in this
2279 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2280
2281 The following commands are available:
2282
2283 \\{gnus-summary-mode-map}"
2284   (interactive)
2285   (kill-all-local-variables)
2286   (when (gnus-visual-p 'summary-menu 'menu)
2287     (gnus-summary-make-menu-bar)
2288     (gnus-summary-make-tool-bar))
2289   (gnus-summary-make-local-variables)
2290   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2291     (gnus-summary-make-local-variables))
2292   (gnus-make-thread-indent-array)
2293   (gnus-simplify-mode-line)
2294   (setq major-mode 'gnus-summary-mode)
2295   (setq mode-name "Summary")
2296   (make-local-variable 'minor-mode-alist)
2297   (use-local-map gnus-summary-mode-map)
2298   (buffer-disable-undo)
2299   (setq buffer-read-only t)             ;Disable modification
2300   (setq truncate-lines t)
2301   (setq selective-display t)
2302   (setq selective-display-ellipses t)   ;Display `...'
2303   (gnus-summary-set-display-table)
2304   (gnus-set-default-directory)
2305   (setq gnus-newsgroup-name group)
2306   (unless (gnus-news-group-p group)
2307     (setq gnus-newsgroup-incorporated
2308           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2309   (make-local-variable 'gnus-summary-line-format)
2310   (make-local-variable 'gnus-summary-line-format-spec)
2311   (make-local-variable 'gnus-summary-dummy-line-format)
2312   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2313   (make-local-variable 'gnus-summary-mark-positions)
2314   (make-local-hook 'pre-command-hook)
2315   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2316   (gnus-run-hooks 'gnus-summary-mode-hook)
2317   (turn-on-gnus-mailing-list-mode)
2318   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2319   (gnus-update-summary-mark-positions))
2320
2321 (defun gnus-summary-make-local-variables ()
2322   "Make all the local summary buffer variables."
2323   (let (global)
2324     (dolist (local gnus-summary-local-variables)
2325       (if (consp local)
2326           (progn
2327             (if (eq (cdr local) 'global)
2328                 ;; Copy the global value of the variable.
2329                 (setq global (symbol-value (car local)))
2330               ;; Use the value from the list.
2331               (setq global (eval (cdr local))))
2332             (set (make-local-variable (car local)) global))
2333         ;; Simple nil-valued local variable.
2334         (set (make-local-variable local) nil)))))
2335
2336 (defun gnus-summary-clear-local-variables ()
2337   (let ((locals gnus-summary-local-variables))
2338     (while locals
2339       (if (consp (car locals))
2340           (and (vectorp (caar locals))
2341                (set (caar locals) nil))
2342         (and (vectorp (car locals))
2343              (set (car locals) nil)))
2344       (setq locals (cdr locals)))))
2345
2346 ;; Summary data functions.
2347
2348 (defmacro gnus-data-number (data)
2349   `(car ,data))
2350
2351 (defmacro gnus-data-set-number (data number)
2352   `(setcar ,data ,number))
2353
2354 (defmacro gnus-data-mark (data)
2355   `(nth 1 ,data))
2356
2357 (defmacro gnus-data-set-mark (data mark)
2358   `(setcar (nthcdr 1 ,data) ,mark))
2359
2360 (defmacro gnus-data-pos (data)
2361   `(nth 2 ,data))
2362
2363 (defmacro gnus-data-set-pos (data pos)
2364   `(setcar (nthcdr 2 ,data) ,pos))
2365
2366 (defmacro gnus-data-header (data)
2367   `(nth 3 ,data))
2368
2369 (defmacro gnus-data-set-header (data header)
2370   `(setcar (nthcdr 3 ,data) ,header))
2371
2372 (defmacro gnus-data-level (data)
2373   `(nth 4 ,data))
2374
2375 (defmacro gnus-data-unread-p (data)
2376   `(= (nth 1 ,data) gnus-unread-mark))
2377
2378 (defmacro gnus-data-read-p (data)
2379   `(/= (nth 1 ,data) gnus-unread-mark))
2380
2381 (defmacro gnus-data-pseudo-p (data)
2382   `(consp (nth 3 ,data)))
2383
2384 (defmacro gnus-data-find (number)
2385   `(assq ,number gnus-newsgroup-data))
2386
2387 (defmacro gnus-data-find-list (number &optional data)
2388   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2389      (memq (assq ,number bdata)
2390            bdata)))
2391
2392 (defmacro gnus-data-make (number mark pos header level)
2393   `(list ,number ,mark ,pos ,header ,level))
2394
2395 (defun gnus-data-enter (after-article number mark pos header level offset)
2396   (let ((data (gnus-data-find-list after-article)))
2397     (unless data
2398       (error "No such article: %d" after-article))
2399     (setcdr data (cons (gnus-data-make number mark pos header level)
2400                        (cdr data)))
2401     (setq gnus-newsgroup-data-reverse nil)
2402     (gnus-data-update-list (cddr data) offset)))
2403
2404 (defun gnus-data-enter-list (after-article list &optional offset)
2405   (when list
2406     (let ((data (and after-article (gnus-data-find-list after-article)))
2407           (ilist list))
2408       (if (not (or data
2409                    after-article))
2410           (let ((odata gnus-newsgroup-data))
2411             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2412             (when offset
2413               (gnus-data-update-list odata offset)))
2414         ;; Find the last element in the list to be spliced into the main
2415         ;; list.
2416         (while (cdr list)
2417           (setq list (cdr list)))
2418         (if (not data)
2419             (progn
2420               (setcdr list gnus-newsgroup-data)
2421               (setq gnus-newsgroup-data ilist)
2422               (when offset
2423                 (gnus-data-update-list (cdr list) offset)))
2424           (setcdr list (cdr data))
2425           (setcdr data ilist)
2426           (when offset
2427             (gnus-data-update-list (cdr list) offset))))
2428       (setq gnus-newsgroup-data-reverse nil))))
2429
2430 (defun gnus-data-remove (article &optional offset)
2431   (let ((data gnus-newsgroup-data))
2432     (if (= (gnus-data-number (car data)) article)
2433         (progn
2434           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2435                 gnus-newsgroup-data-reverse nil)
2436           (when offset
2437             (gnus-data-update-list gnus-newsgroup-data offset)))
2438       (while (cdr data)
2439         (when (= (gnus-data-number (cadr data)) article)
2440           (setcdr data (cddr data))
2441           (when offset
2442             (gnus-data-update-list (cdr data) offset))
2443           (setq data nil
2444                 gnus-newsgroup-data-reverse nil))
2445         (setq data (cdr data))))))
2446
2447 (defmacro gnus-data-list (backward)
2448   `(if ,backward
2449        (or gnus-newsgroup-data-reverse
2450            (setq gnus-newsgroup-data-reverse
2451                  (reverse gnus-newsgroup-data)))
2452      gnus-newsgroup-data))
2453
2454 (defun gnus-data-update-list (data offset)
2455   "Add OFFSET to the POS of all data entries in DATA."
2456   (setq gnus-newsgroup-data-reverse nil)
2457   (while data
2458     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2459     (setq data (cdr data))))
2460
2461 (defun gnus-summary-article-pseudo-p (article)
2462   "Say whether this article is a pseudo article or not."
2463   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2464
2465 (defmacro gnus-summary-article-sparse-p (article)
2466   "Say whether this article is a sparse article or not."
2467   `(memq ,article gnus-newsgroup-sparse))
2468
2469 (defmacro gnus-summary-article-ancient-p (article)
2470   "Say whether this article is a sparse article or not."
2471   `(memq ,article gnus-newsgroup-ancient))
2472
2473 (defun gnus-article-parent-p (number)
2474   "Say whether this article is a parent or not."
2475   (let ((data (gnus-data-find-list number)))
2476     (and (cdr data)                     ; There has to be an article after...
2477          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2478             (gnus-data-level (nth 1 data))))))
2479
2480 (defun gnus-article-children (number)
2481   "Return a list of all children to NUMBER."
2482   (let* ((data (gnus-data-find-list number))
2483          (level (gnus-data-level (car data)))
2484          children)
2485     (setq data (cdr data))
2486     (while (and data
2487                 (= (gnus-data-level (car data)) (1+ level)))
2488       (push (gnus-data-number (car data)) children)
2489       (setq data (cdr data)))
2490     children))
2491
2492 (defmacro gnus-summary-skip-intangible ()
2493   "If the current article is intangible, then jump to a different article."
2494   '(let ((to (get-text-property (point) 'gnus-intangible)))
2495      (and to (gnus-summary-goto-subject to))))
2496
2497 (defmacro gnus-summary-article-intangible-p ()
2498   "Say whether this article is intangible or not."
2499   '(get-text-property (point) 'gnus-intangible))
2500
2501 (defun gnus-article-read-p (article)
2502   "Say whether ARTICLE is read or not."
2503   (not (or (memq article gnus-newsgroup-marked)
2504            (memq article gnus-newsgroup-unreads)
2505            (memq article gnus-newsgroup-unselected)
2506            (memq article gnus-newsgroup-dormant))))
2507
2508 ;; Some summary mode macros.
2509
2510 (defmacro gnus-summary-article-number ()
2511   "The article number of the article on the current line.
2512 If there isn's an article number here, then we return the current
2513 article number."
2514   '(progn
2515      (gnus-summary-skip-intangible)
2516      (or (get-text-property (point) 'gnus-number)
2517          (gnus-summary-last-subject))))
2518
2519 (defmacro gnus-summary-article-header (&optional number)
2520   "Return the header of article NUMBER."
2521   `(gnus-data-header (gnus-data-find
2522                       ,(or number '(gnus-summary-article-number)))))
2523
2524 (defmacro gnus-summary-thread-level (&optional number)
2525   "Return the level of thread that starts with article NUMBER."
2526   `(if (and (eq gnus-summary-make-false-root 'dummy)
2527             (get-text-property (point) 'gnus-intangible))
2528        0
2529      (gnus-data-level (gnus-data-find
2530                        ,(or number '(gnus-summary-article-number))))))
2531
2532 (defmacro gnus-summary-article-mark (&optional number)
2533   "Return the mark of article NUMBER."
2534   `(gnus-data-mark (gnus-data-find
2535                     ,(or number '(gnus-summary-article-number)))))
2536
2537 (defmacro gnus-summary-article-pos (&optional number)
2538   "Return the position of the line of article NUMBER."
2539   `(gnus-data-pos (gnus-data-find
2540                    ,(or number '(gnus-summary-article-number)))))
2541
2542 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2543 (defmacro gnus-summary-article-subject (&optional number)
2544   "Return current subject string or nil if nothing."
2545   `(let ((headers
2546           ,(if number
2547                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2548              '(gnus-data-header (assq (gnus-summary-article-number)
2549                                       gnus-newsgroup-data)))))
2550      (and headers
2551           (vectorp headers)
2552           (mail-header-subject headers))))
2553
2554 (defmacro gnus-summary-article-score (&optional number)
2555   "Return current article score."
2556   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2557                   gnus-newsgroup-scored))
2558        gnus-summary-default-score 0))
2559
2560 (defun gnus-summary-article-children (&optional number)
2561   "Return a list of article numbers that are children of article NUMBER."
2562   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2563          (level (gnus-data-level (car data)))
2564          l children)
2565     (while (and (setq data (cdr data))
2566                 (> (setq l (gnus-data-level (car data))) level))
2567       (and (= (1+ level) l)
2568            (push (gnus-data-number (car data))
2569                  children)))
2570     (nreverse children)))
2571
2572 (defun gnus-summary-article-parent (&optional number)
2573   "Return the article number of the parent of article NUMBER."
2574   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2575                                     (gnus-data-list t)))
2576          (level (gnus-data-level (car data))))
2577     (if (zerop level)
2578         ()                              ; This is a root.
2579       ;; We search until we find an article with a level less than
2580       ;; this one.  That function has to be the parent.
2581       (while (and (setq data (cdr data))
2582                   (not (< (gnus-data-level (car data)) level))))
2583       (and data (gnus-data-number (car data))))))
2584
2585 (defun gnus-unread-mark-p (mark)
2586   "Say whether MARK is the unread mark."
2587   (= mark gnus-unread-mark))
2588
2589 (defun gnus-read-mark-p (mark)
2590   "Say whether MARK is one of the marks that mark as read.
2591 This is all marks except unread, ticked, dormant, and expirable."
2592   (not (or (= mark gnus-unread-mark)
2593            (= mark gnus-ticked-mark)
2594            (= mark gnus-dormant-mark)
2595            (= mark gnus-expirable-mark))))
2596
2597 (defmacro gnus-article-mark (number)
2598   "Return the MARK of article NUMBER.
2599 This macro should only be used when computing the mark the \"first\"
2600 time; i.e., when generating the summary lines.  After that,
2601 `gnus-summary-article-mark' should be used to examine the
2602 marks of articles."
2603   `(cond
2604     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2605     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2606     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2607     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2608     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2609     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2610     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2611     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2612            gnus-ancient-mark))))
2613
2614 ;; Saving hidden threads.
2615
2616 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2617 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2618
2619 (defmacro gnus-save-hidden-threads (&rest forms)
2620   "Save hidden threads, eval FORMS, and restore the hidden threads."
2621   (let ((config (make-symbol "config")))
2622     `(let ((,config (gnus-hidden-threads-configuration)))
2623        (unwind-protect
2624            (save-excursion
2625              ,@forms)
2626          (gnus-restore-hidden-threads-configuration ,config)))))
2627
2628 (defun gnus-data-compute-positions ()
2629   "Compute the positions of all articles."
2630   (setq gnus-newsgroup-data-reverse nil)
2631   (let ((data gnus-newsgroup-data))
2632     (save-excursion
2633       (gnus-save-hidden-threads
2634         (gnus-summary-show-all-threads)
2635         (goto-char (point-min))
2636         (while data
2637           (while (get-text-property (point) 'gnus-intangible)
2638             (forward-line 1))
2639           (gnus-data-set-pos (car data) (+ (point) 3))
2640           (setq data (cdr data))
2641           (forward-line 1))))))
2642
2643 (defun gnus-hidden-threads-configuration ()
2644   "Return the current hidden threads configuration."
2645   (save-excursion
2646     (let (config)
2647       (goto-char (point-min))
2648       (while (search-forward "\r" nil t)
2649         (push (1- (point)) config))
2650       config)))
2651
2652 (defun gnus-restore-hidden-threads-configuration (config)
2653   "Restore hidden threads configuration from CONFIG."
2654   (save-excursion
2655     (let (point buffer-read-only)
2656       (while (setq point (pop config))
2657         (when (and (< point (point-max))
2658                    (goto-char point)
2659                    (eq (char-after) ?\n))
2660           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2661
2662 ;; Various summary mode internalish functions.
2663
2664 (defun gnus-mouse-pick-article (e)
2665   (interactive "e")
2666   (mouse-set-point e)
2667   (gnus-summary-next-page nil t))
2668
2669 (defun gnus-summary-set-display-table ()
2670   "Change the display table.
2671 Odd characters have a tendency to mess
2672 up nicely formatted displays - we make all possible glyphs
2673 display only a single character."
2674
2675   ;; We start from the standard display table, if any.
2676   (let ((table (or (copy-sequence standard-display-table)
2677                    (make-display-table)))
2678         (i 32))
2679     ;; Nix out all the control chars...
2680     (while (>= (setq i (1- i)) 0)
2681       (aset table i [??]))
2682     ;; ... but not newline and cr, of course.  (cr is necessary for the
2683     ;; selective display).
2684     (aset table ?\n nil)
2685     (aset table ?\r nil)
2686     ;; We keep TAB as well.
2687     (aset table ?\t nil)
2688     ;; We nix out any glyphs over 126 that are not set already.
2689     (let ((i 256))
2690       (while (>= (setq i (1- i)) 127)
2691         ;; Only modify if the entry is nil.
2692         (unless (aref table i)
2693           (aset table i [??]))))
2694     (setq buffer-display-table table)))
2695
2696 (defun gnus-summary-buffer-name (group)
2697   "Return the summary buffer name of GROUP."
2698   (concat "*Summary " group "*"))
2699
2700 (defun gnus-summary-setup-buffer (group)
2701   "Initialize summary buffer."
2702   (let ((buffer (gnus-summary-buffer-name group)))
2703     (if (get-buffer buffer)
2704         (progn
2705           (set-buffer buffer)
2706           (setq gnus-summary-buffer (current-buffer))
2707           (not gnus-newsgroup-prepared))
2708       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2709       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2710       (gnus-summary-mode group)
2711       (when gnus-carpal
2712         (gnus-carpal-setup-buffer 'summary))
2713       (unless gnus-single-article-buffer
2714         (make-local-variable 'gnus-article-buffer)
2715         (make-local-variable 'gnus-article-current)
2716         (make-local-variable 'gnus-original-article-buffer))
2717       (setq gnus-newsgroup-name group)
2718       ;; Set any local variables in the group parameters.
2719       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2720       t)))
2721
2722 (defun gnus-set-global-variables ()
2723   "Set the global equivalents of the buffer-local variables.
2724 They are set to the latest values they had.  These reflect the summary
2725 buffer that was in action when the last article was fetched."
2726   (when (eq major-mode 'gnus-summary-mode)
2727     (setq gnus-summary-buffer (current-buffer))
2728     (let ((name gnus-newsgroup-name)
2729           (marked gnus-newsgroup-marked)
2730           (unread gnus-newsgroup-unreads)
2731           (headers gnus-current-headers)
2732           (data gnus-newsgroup-data)
2733           (summary gnus-summary-buffer)
2734           (article-buffer gnus-article-buffer)
2735           (original gnus-original-article-buffer)
2736           (gac gnus-article-current)
2737           (reffed gnus-reffed-article-number)
2738           (score-file gnus-current-score-file)
2739           (default-charset gnus-newsgroup-charset)
2740           vlist)
2741       (let ((locals gnus-newsgroup-variables))
2742         (while locals
2743           (if (consp (car locals))
2744               (push (eval (caar locals)) vlist)
2745             (push (eval (car locals)) vlist))
2746           (setq locals (cdr locals)))
2747         (setq vlist (nreverse vlist)))
2748       (save-excursion
2749         (set-buffer gnus-group-buffer)
2750         (setq gnus-newsgroup-name name
2751               gnus-newsgroup-marked marked
2752               gnus-newsgroup-unreads unread
2753               gnus-current-headers headers
2754               gnus-newsgroup-data data
2755               gnus-article-current gac
2756               gnus-summary-buffer summary
2757               gnus-article-buffer article-buffer
2758               gnus-original-article-buffer original
2759               gnus-reffed-article-number reffed
2760               gnus-current-score-file score-file
2761               gnus-newsgroup-charset default-charset)
2762         (let ((locals gnus-newsgroup-variables))
2763           (while locals
2764             (if (consp (car locals))
2765                 (set (caar locals) (pop vlist))
2766               (set (car locals) (pop vlist)))
2767             (setq locals (cdr locals))))
2768         ;; The article buffer also has local variables.
2769         (when (gnus-buffer-live-p gnus-article-buffer)
2770           (set-buffer gnus-article-buffer)
2771           (setq gnus-summary-buffer summary))))))
2772
2773 (defun gnus-summary-article-unread-p (article)
2774   "Say whether ARTICLE is unread or not."
2775   (memq article gnus-newsgroup-unreads))
2776
2777 (defun gnus-summary-first-article-p (&optional article)
2778   "Return whether ARTICLE is the first article in the buffer."
2779   (if (not (setq article (or article (gnus-summary-article-number))))
2780       nil
2781     (eq article (caar gnus-newsgroup-data))))
2782
2783 (defun gnus-summary-last-article-p (&optional article)
2784   "Return whether ARTICLE is the last article in the buffer."
2785   (if (not (setq article (or article (gnus-summary-article-number))))
2786       ;; All non-existent numbers are the last article.  :-)
2787       t
2788     (not (cdr (gnus-data-find-list article)))))
2789
2790 (defun gnus-make-thread-indent-array ()
2791   (let ((n 200))
2792     (unless (and gnus-thread-indent-array
2793                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2794       (setq gnus-thread-indent-array (make-vector 201 "")
2795             gnus-thread-indent-array-level gnus-thread-indent-level)
2796       (while (>= n 0)
2797         (aset gnus-thread-indent-array n
2798               (make-string (* n gnus-thread-indent-level) ? ))
2799         (setq n (1- n))))))
2800
2801 (defun gnus-update-summary-mark-positions ()
2802   "Compute where the summary marks are to go."
2803   (save-excursion
2804     (when (gnus-buffer-exists-p gnus-summary-buffer)
2805       (set-buffer gnus-summary-buffer))
2806     (let ((gnus-replied-mark 129)
2807           (gnus-score-below-mark 130)
2808           (gnus-score-over-mark 130)
2809           (gnus-download-mark 131)
2810           (spec gnus-summary-line-format-spec)
2811           gnus-visual pos)
2812       (save-excursion
2813         (gnus-set-work-buffer)
2814         (let ((gnus-summary-line-format-spec spec)
2815               (gnus-newsgroup-downloadable '((0 . t))))
2816           (gnus-summary-insert-line
2817            (make-full-mail-header 0 "" "nobody"
2818                                   "05 Apr 2001 23:33:09 +0400"
2819                                   "" "" 0 0 "" nil)
2820            0 nil 128 t nil "" nil 1)
2821           (goto-char (point-min))
2822           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2823                                              (- (point) 2)))))
2824           (goto-char (point-min))
2825           (push (cons 'replied (and (search-forward "\201" nil t)
2826                                     (- (point) 2)))
2827                 pos)
2828           (goto-char (point-min))
2829           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2830                 pos)
2831           (goto-char (point-min))
2832           (push (cons 'download
2833                       (and (search-forward "\203" nil t) (- (point) 2)))
2834                 pos)))
2835       (setq gnus-summary-mark-positions pos))))
2836
2837 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2838   "Insert a dummy root in the summary buffer."
2839   (beginning-of-line)
2840   (gnus-add-text-properties
2841    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2842    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2843
2844 (defun gnus-summary-from-or-to-or-newsgroups (header)
2845   (let ((to (cdr (assq 'To (mail-header-extra header))))
2846         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2847         (default-mime-charset (with-current-buffer gnus-summary-buffer
2848                                 default-mime-charset)))
2849     (cond
2850      ((and to
2851            gnus-ignored-from-addresses
2852            (string-match gnus-ignored-from-addresses
2853                          (mail-header-from header)))
2854       (concat "-> "
2855               (or (car (funcall gnus-extract-address-components
2856                                 (funcall
2857                                  gnus-decode-encoded-word-function to)))
2858                   (funcall gnus-decode-encoded-word-function to))))
2859      ((and newsgroups
2860            gnus-ignored-from-addresses
2861            (string-match gnus-ignored-from-addresses
2862                          (mail-header-from header)))
2863       (concat "=> " newsgroups))
2864      (t
2865       (or (car (funcall gnus-extract-address-components
2866                         (mail-header-from header)))
2867           (mail-header-from header))))))
2868
2869 (defun gnus-summary-insert-line (gnus-tmp-header
2870                                  gnus-tmp-level gnus-tmp-current
2871                                  gnus-tmp-unread gnus-tmp-replied
2872                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2873                                  &optional gnus-tmp-dummy gnus-tmp-score
2874                                  gnus-tmp-process)
2875   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2876          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2877          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2878          (gnus-tmp-score-char
2879           (if (or (null gnus-summary-default-score)
2880                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2881                       gnus-summary-zcore-fuzz))
2882               ?\ ;;;Whitespace
2883             (if (< gnus-tmp-score gnus-summary-default-score)
2884                 gnus-score-below-mark gnus-score-over-mark)))
2885          (gnus-tmp-replied
2886           (cond (gnus-tmp-process gnus-process-mark)
2887                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2888                  gnus-cached-mark)
2889                 (gnus-tmp-replied gnus-replied-mark)
2890                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2891                  gnus-saved-mark)
2892                 (t gnus-no-mark)))
2893          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2894          (gnus-tmp-name
2895           (cond
2896            ((string-match "<[^>]+> *$" gnus-tmp-from)
2897             (let ((beg (match-beginning 0)))
2898               (or (and (string-match "^\".+\"" gnus-tmp-from)
2899                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2900                   (substring gnus-tmp-from 0 beg))))
2901            ((string-match "(.+)" gnus-tmp-from)
2902             (substring gnus-tmp-from
2903                        (1+ (match-beginning 0)) (1- (match-end 0))))
2904            (t gnus-tmp-from)))
2905          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2906          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2907          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2908          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2909          (buffer-read-only nil))
2910     (when (string= gnus-tmp-name "")
2911       (setq gnus-tmp-name gnus-tmp-from))
2912     (unless (numberp gnus-tmp-lines)
2913       (setq gnus-tmp-lines -1))
2914     (when (= gnus-tmp-lines -1)
2915       (setq gnus-tmp-lines "?"))
2916     (gnus-put-text-property-excluding-characters-with-faces
2917      (point)
2918      (progn (eval gnus-summary-line-format-spec) (point))
2919      'gnus-number gnus-tmp-number)
2920     (when (gnus-visual-p 'summary-highlight 'highlight)
2921       (forward-line -1)
2922       (gnus-run-hooks 'gnus-summary-update-hook)
2923       (forward-line 1))))
2924
2925 (defun gnus-summary-update-line (&optional dont-update)
2926   "Update summary line after change."
2927   (when (and gnus-summary-default-score
2928              (not gnus-summary-inhibit-highlight))
2929     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2930            (article (gnus-summary-article-number))
2931            (score (gnus-summary-article-score article)))
2932       (unless dont-update
2933         (if (and gnus-summary-mark-below
2934                  (< (gnus-summary-article-score)
2935                     gnus-summary-mark-below))
2936             ;; This article has a low score, so we mark it as read.
2937             (when (memq article gnus-newsgroup-unreads)
2938               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2939           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2940             ;; This article was previously marked as read on account
2941             ;; of a low score, but now it has risen, so we mark it as
2942             ;; unread.
2943             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2944         (gnus-summary-update-mark
2945          (if (or (null gnus-summary-default-score)
2946                  (<= (abs (- score gnus-summary-default-score))
2947                      gnus-summary-zcore-fuzz))
2948              ?\ ;;;Whitespace
2949            (if (< score gnus-summary-default-score)
2950                gnus-score-below-mark gnus-score-over-mark))
2951          'score))
2952       ;; Do visual highlighting.
2953       (when (gnus-visual-p 'summary-highlight 'highlight)
2954         (gnus-run-hooks 'gnus-summary-update-hook)))))
2955
2956 (defvar gnus-tmp-new-adopts nil)
2957
2958 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2959   "Return the number of articles in THREAD.
2960 This may be 0 in some cases -- if none of the articles in
2961 the thread are to be displayed."
2962   (let* ((number
2963           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2964           (cond
2965            ((not (listp thread))
2966             1)
2967            ((and (consp thread) (cdr thread))
2968             (apply
2969              '+ 1 (mapcar
2970                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2971            ((null thread)
2972             1)
2973            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2974             1)
2975            (t 0))))
2976     (when (and level (zerop level) gnus-tmp-new-adopts)
2977       (incf number
2978             (apply '+ (mapcar
2979                        'gnus-summary-number-of-articles-in-thread
2980                        gnus-tmp-new-adopts))))
2981     (if char
2982         (if (> number 1) gnus-not-empty-thread-mark
2983           gnus-empty-thread-mark)
2984       number)))
2985
2986 (defun gnus-summary-set-local-parameters (group)
2987   "Go through the local params of GROUP and set all variable specs in that list."
2988   (let ((params (gnus-group-find-parameter group))
2989         elem)
2990     (while params
2991       (setq elem (car params)
2992             params (cdr params))
2993       (and (consp elem)                 ; Has to be a cons.
2994            (consp (cdr elem))           ; The cdr has to be a list.
2995            (symbolp (car elem))         ; Has to be a symbol in there.
2996            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2997            (ignore-errors               ; So we set it.
2998              (make-local-variable (car elem))
2999              (set (car elem) (eval (nth 1 elem))))))))
3000
3001 (defun gnus-summary-read-group (group &optional show-all no-article
3002                                       kill-buffer no-display backward
3003                                       select-articles)
3004   "Start reading news in newsgroup GROUP.
3005 If SHOW-ALL is non-nil, already read articles are also listed.
3006 If NO-ARTICLE is non-nil, no article is selected initially.
3007 If NO-DISPLAY, don't generate a summary buffer."
3008   (let (result)
3009     (while (and group
3010                 (null (setq result
3011                             (let ((gnus-auto-select-next nil))
3012                               (or (gnus-summary-read-group-1
3013                                    group show-all no-article
3014                                    kill-buffer no-display
3015                                    select-articles)
3016                                   (setq show-all nil
3017                                         select-articles nil)))))
3018                 (eq gnus-auto-select-next 'quietly))
3019       (set-buffer gnus-group-buffer)
3020       ;; The entry function called above goes to the next
3021       ;; group automatically, so we go two groups back
3022       ;; if we are searching for the previous group.
3023       (when backward
3024         (gnus-group-prev-unread-group 2))
3025       (if (not (equal group (gnus-group-group-name)))
3026           (setq group (gnus-group-group-name))
3027         (setq group nil)))
3028     result))
3029
3030 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3031   "Directly jump to the other GROUP from summary buffer.
3032 If SHOW-ALL is non-nil, already read articles are also listed."
3033   (interactive
3034    (if (eq gnus-summary-buffer (current-buffer))
3035        (list (completing-read
3036               "Group: " gnus-active-hashtb nil t
3037               (when (and gnus-newsgroup-name
3038                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3039                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3040               'gnus-group-history)
3041              current-prefix-arg)
3042      (error "%s must be invoked from a gnus summary buffer." this-command)))
3043   (unless (or (zerop (length group))
3044               (and gnus-newsgroup-name
3045                    (string-equal gnus-newsgroup-name group)))
3046     (gnus-summary-exit)
3047     (gnus-summary-read-group group show-all
3048                              gnus-dont-select-after-jump-to-other-group)))
3049
3050 (defun gnus-summary-read-group-1 (group show-all no-article
3051                                         kill-buffer no-display
3052                                         &optional select-articles)
3053   ;; Killed foreign groups can't be entered.
3054   ;;  (when (and (not (gnus-group-native-p group))
3055   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3056   ;;    (error "Dead non-native groups can't be entered"))
3057   (gnus-message 5 "Retrieving newsgroup: %s..."
3058                 (gnus-group-decoded-name group))
3059   (let* ((new-group (gnus-summary-setup-buffer group))
3060          (quit-config (gnus-group-quit-config group))
3061          (did-select (and new-group (gnus-select-newsgroup
3062                                      group show-all select-articles))))
3063     (cond
3064      ;; This summary buffer exists already, so we just select it.
3065      ((not new-group)
3066       (gnus-set-global-variables)
3067       (when kill-buffer
3068         (gnus-kill-or-deaden-summary kill-buffer))
3069       (gnus-configure-windows 'summary 'force)
3070       (gnus-set-mode-line 'summary)
3071       (gnus-summary-position-point)
3072       (message "")
3073       t)
3074      ;; We couldn't select this group.
3075      ((null did-select)
3076       (when (and (eq major-mode 'gnus-summary-mode)
3077                  (not (equal (current-buffer) kill-buffer)))
3078         (kill-buffer (current-buffer))
3079         (if (not quit-config)
3080             (progn
3081               ;; Update the info -- marks might need to be removed,
3082               ;; for instance.
3083               (gnus-summary-update-info)
3084               (set-buffer gnus-group-buffer)
3085               (gnus-group-jump-to-group group)
3086               (gnus-group-next-unread-group 1))
3087           (gnus-handle-ephemeral-exit quit-config)))
3088       (let ((grpinfo (gnus-get-info group)))
3089         (if (null (gnus-info-read grpinfo))
3090             (gnus-message 3 "Group %s contains no messages" 
3091                           (gnus-group-decoded-name group))
3092           (gnus-message 3 "Can't select group")))
3093       nil)
3094      ;; The user did a `C-g' while prompting for number of articles,
3095      ;; so we exit this group.
3096      ((eq did-select 'quit)
3097       (and (eq major-mode 'gnus-summary-mode)
3098            (not (equal (current-buffer) kill-buffer))
3099            (kill-buffer (current-buffer)))
3100       (when kill-buffer
3101         (gnus-kill-or-deaden-summary kill-buffer))
3102       (if (not quit-config)
3103           (progn
3104             (set-buffer gnus-group-buffer)
3105             (gnus-group-jump-to-group group)
3106             (gnus-group-next-unread-group 1)
3107             (gnus-configure-windows 'group 'force))
3108         (gnus-handle-ephemeral-exit quit-config))
3109       ;; Finally signal the quit.
3110       (signal 'quit nil))
3111      ;; The group was successfully selected.
3112      (t
3113       (gnus-set-global-variables)
3114       ;; Save the active value in effect when the group was entered.
3115       (setq gnus-newsgroup-active
3116             (gnus-copy-sequence
3117              (gnus-active gnus-newsgroup-name)))
3118       ;; You can change the summary buffer in some way with this hook.
3119       (gnus-run-hooks 'gnus-select-group-hook)
3120       (gnus-update-format-specifications
3121        nil 'summary 'summary-mode 'summary-dummy)
3122       (gnus-update-summary-mark-positions)
3123       ;; Do score processing.
3124       (when gnus-use-scoring
3125         (gnus-possibly-score-headers))
3126       ;; Check whether to fill in the gaps in the threads.
3127       (when gnus-build-sparse-threads
3128         (gnus-build-sparse-threads))
3129       ;; Find the initial limit.
3130       (if gnus-show-threads
3131           (if show-all
3132               (let ((gnus-newsgroup-dormant nil))
3133                 (gnus-summary-initial-limit show-all))
3134             (gnus-summary-initial-limit show-all))
3135         ;; When untreaded, all articles are always shown.
3136         (setq gnus-newsgroup-limit
3137               (mapcar
3138                (lambda (header) (mail-header-number header))
3139                gnus-newsgroup-headers)))
3140       ;; Generate the summary buffer.
3141       (unless no-display
3142         (gnus-summary-prepare))
3143       (when gnus-use-trees
3144         (gnus-tree-open group)
3145         (setq gnus-summary-highlight-line-function
3146               'gnus-tree-highlight-article))
3147       ;; If the summary buffer is empty, but there are some low-scored
3148       ;; articles or some excluded dormants, we include these in the
3149       ;; buffer.
3150       (when (and (zerop (buffer-size))
3151                  (not no-display))
3152         (cond (gnus-newsgroup-dormant
3153                (gnus-summary-limit-include-dormant))
3154               ((and gnus-newsgroup-scored show-all)
3155                (gnus-summary-limit-include-expunged t))))
3156       ;; Function `gnus-apply-kill-file' must be called in this hook.
3157       (gnus-run-hooks 'gnus-apply-kill-hook)
3158       (if (and (zerop (buffer-size))
3159                (not no-display))
3160           (progn
3161             ;; This newsgroup is empty.
3162             (gnus-summary-catchup-and-exit nil t)
3163             (gnus-message 6 "No unread news")
3164             (when kill-buffer
3165               (gnus-kill-or-deaden-summary kill-buffer))
3166             ;; Return nil from this function.
3167             nil)
3168         ;; Hide conversation thread subtrees.  We cannot do this in
3169         ;; gnus-summary-prepare-hook since kill processing may not
3170         ;; work with hidden articles.
3171         (and gnus-show-threads
3172              gnus-thread-hide-subtree
3173              (gnus-summary-hide-all-threads))
3174         (when kill-buffer
3175           (gnus-kill-or-deaden-summary kill-buffer))
3176         ;; Show first unread article if requested.
3177         (if (and (not no-article)
3178                  (not no-display)
3179                  gnus-newsgroup-unreads
3180                  gnus-auto-select-first)
3181             (progn
3182               (gnus-configure-windows 'summary)
3183               (cond
3184                ((eq gnus-auto-select-first 'best)
3185                 (gnus-summary-best-unread-article))
3186                ((eq gnus-auto-select-first t)
3187                 (gnus-summary-first-unread-article))
3188                ((gnus-functionp gnus-auto-select-first)
3189                 (funcall gnus-auto-select-first))))
3190           ;; Don't select any articles, just move point to the first
3191           ;; article in the group.
3192           (goto-char (point-min))
3193           (gnus-summary-position-point)
3194           (gnus-configure-windows 'summary 'force)
3195           (gnus-set-mode-line 'summary))
3196         (when (get-buffer-window gnus-group-buffer t)
3197           ;; Gotta use windows, because recenter does weird stuff if
3198           ;; the current buffer ain't the displayed window.
3199           (let ((owin (selected-window)))
3200             (select-window (get-buffer-window gnus-group-buffer t))
3201             (when (gnus-group-goto-group group)
3202               (recenter))
3203             (select-window owin)))
3204         ;; Mark this buffer as "prepared".
3205         (setq gnus-newsgroup-prepared t)
3206         (gnus-run-hooks 'gnus-summary-prepared-hook)
3207         t)))))
3208
3209 (defun gnus-summary-prepare ()
3210   "Generate the summary buffer."
3211   (interactive)
3212   (let ((buffer-read-only nil))
3213     (erase-buffer)
3214     (setq gnus-newsgroup-data nil
3215           gnus-newsgroup-data-reverse nil)
3216     (gnus-run-hooks 'gnus-summary-generate-hook)
3217     ;; Generate the buffer, either with threads or without.
3218     (when gnus-newsgroup-headers
3219       (gnus-summary-prepare-threads
3220        (if gnus-show-threads
3221            (gnus-sort-gathered-threads
3222             (funcall gnus-summary-thread-gathering-function
3223                      (gnus-sort-threads
3224                       (gnus-cut-threads (gnus-make-threads)))))
3225          ;; Unthreaded display.
3226          (gnus-sort-articles gnus-newsgroup-headers))))
3227     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3228     ;; Call hooks for modifying summary buffer.
3229     (goto-char (point-min))
3230     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3231
3232 (defsubst gnus-general-simplify-subject (subject)
3233   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3234   (setq subject
3235         (cond
3236          ;; Truncate the subject.
3237          (gnus-simplify-subject-functions
3238           (gnus-map-function gnus-simplify-subject-functions subject))
3239          ((numberp gnus-summary-gather-subject-limit)
3240           (setq subject (gnus-simplify-subject-re subject))
3241           (if (> (length subject) gnus-summary-gather-subject-limit)
3242               (substring subject 0 gnus-summary-gather-subject-limit)
3243             subject))
3244          ;; Fuzzily simplify it.
3245          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3246           (gnus-simplify-subject-fuzzy subject))
3247          ;; Just remove the leading "Re:".
3248          (t
3249           (gnus-simplify-subject-re subject))))
3250
3251   (if (and gnus-summary-gather-exclude-subject
3252            (string-match gnus-summary-gather-exclude-subject subject))
3253       nil                               ; This article shouldn't be gathered
3254     subject))
3255
3256 (defun gnus-summary-simplify-subject-query ()
3257   "Query where the respool algorithm would put this article."
3258   (interactive)
3259   (gnus-summary-select-article)
3260   (message "%s"
3261            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3262
3263 (defun gnus-gather-threads-by-subject (threads)
3264   "Gather threads by looking at Subject headers."
3265   (if (not gnus-summary-make-false-root)
3266       threads
3267     (let ((hashtb (gnus-make-hashtable 1024))
3268           (prev threads)
3269           (result threads)
3270           subject hthread whole-subject)
3271       (while threads
3272         (setq subject (gnus-general-simplify-subject
3273                        (setq whole-subject (mail-header-subject
3274                                             (caar threads)))))
3275         (when subject
3276           (if (setq hthread (gnus-gethash subject hashtb))
3277               (progn
3278                 ;; We enter a dummy root into the thread, if we
3279                 ;; haven't done that already.
3280                 (unless (stringp (caar hthread))
3281                   (setcar hthread (list whole-subject (car hthread))))
3282                 ;; We add this new gathered thread to this gathered
3283                 ;; thread.
3284                 (setcdr (car hthread)
3285                         (nconc (cdar hthread) (list (car threads))))
3286                 ;; Remove it from the list of threads.
3287                 (setcdr prev (cdr threads))
3288                 (setq threads prev))
3289             ;; Enter this thread into the hash table.
3290             (gnus-sethash subject threads hashtb)))
3291         (setq prev threads)
3292         (setq threads (cdr threads)))
3293       result)))
3294
3295 (defun gnus-gather-threads-by-references (threads)
3296   "Gather threads by looking at References headers."
3297   (let ((idhashtb (gnus-make-hashtable 1024))
3298         (thhashtb (gnus-make-hashtable 1024))
3299         (prev threads)
3300         (result threads)
3301         ids references id gthread gid entered ref)
3302     (while threads
3303       (when (setq references (mail-header-references (caar threads)))
3304         (setq id (mail-header-id (caar threads))
3305               ids (gnus-split-references references)
3306               entered nil)
3307         (while (setq ref (pop ids))
3308           (setq ids (delete ref ids))
3309           (if (not (setq gid (gnus-gethash ref idhashtb)))
3310               (progn
3311                 (gnus-sethash ref id idhashtb)
3312                 (gnus-sethash id threads thhashtb))
3313             (setq gthread (gnus-gethash gid thhashtb))
3314             (unless entered
3315               ;; We enter a dummy root into the thread, if we
3316               ;; haven't done that already.
3317               (unless (stringp (caar gthread))
3318                 (setcar gthread (list (mail-header-subject (caar gthread))
3319                                       (car gthread))))
3320               ;; We add this new gathered thread to this gathered
3321               ;; thread.
3322               (setcdr (car gthread)
3323                       (nconc (cdar gthread) (list (car threads)))))
3324             ;; Add it into the thread hash table.
3325             (gnus-sethash id gthread thhashtb)
3326             (setq entered t)
3327             ;; Remove it from the list of threads.
3328             (setcdr prev (cdr threads))
3329             (setq threads prev))))
3330       (setq prev threads)
3331       (setq threads (cdr threads)))
3332     result))
3333
3334 (defun gnus-sort-gathered-threads (threads)
3335   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3336   (let ((result threads))
3337     (while threads
3338       (when (stringp (caar threads))
3339         (setcdr (car threads)
3340                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3341       (setq threads (cdr threads)))
3342     result))
3343
3344 (defun gnus-thread-loop-p (root thread)
3345   "Say whether ROOT is in THREAD."
3346   (let ((stack (list thread))
3347         (infloop 0)
3348         th)
3349     (while (setq thread (pop stack))
3350       (setq th (cdr thread))
3351       (while (and th
3352                   (not (eq (caar th) root)))
3353         (pop th))
3354       (if th
3355           ;; We have found a loop.
3356           (let (ref-dep)
3357             (setcdr thread (delq (car th) (cdr thread)))
3358             (if (boundp (setq ref-dep (intern "none"
3359                                               gnus-newsgroup-dependencies)))
3360                 (setcdr (symbol-value ref-dep)
3361                         (nconc (cdr (symbol-value ref-dep))
3362                                (list (car th))))
3363               (set ref-dep (list nil (car th))))
3364             (setq infloop 1
3365                   stack nil))
3366         ;; Push all the subthreads onto the stack.
3367         (push (cdr thread) stack)))
3368     infloop))
3369
3370 (defun gnus-make-threads ()
3371   "Go through the dependency hashtb and find the roots.  Return all threads."
3372   (let (threads)
3373     (while (catch 'infloop
3374              (mapatoms
3375               (lambda (refs)
3376                 ;; Deal with self-referencing References loops.
3377                 (when (and (car (symbol-value refs))
3378                            (not (zerop
3379                                  (apply
3380                                   '+
3381                                   (mapcar
3382                                    (lambda (thread)
3383                                      (gnus-thread-loop-p
3384                                       (car (symbol-value refs)) thread))
3385                                    (cdr (symbol-value refs)))))))
3386                   (setq threads nil)
3387                   (throw 'infloop t))
3388                 (unless (car (symbol-value refs))
3389                   ;; These threads do not refer back to any other articles,
3390                   ;; so they're roots.
3391                   (setq threads (append (cdr (symbol-value refs)) threads))))
3392               gnus-newsgroup-dependencies)))
3393     threads))
3394
3395 ;; Build the thread tree.
3396 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3397   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3398
3399 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3400 if it was already present.
3401
3402 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3403 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3404 Message-IDs will be renamed be renamed to a unique Message-ID before
3405 being entered.
3406
3407 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3408   (let* ((id (mail-header-id header))
3409          (id-dep (and id (intern id dependencies)))
3410          ref ref-dep ref-header)
3411     ;; Enter this `header' in the `dependencies' table.
3412     (cond
3413      ((not id-dep)
3414       (setq header nil))
3415      ;; The first two cases do the normal part: enter a new `header'
3416      ;; in the `dependencies' table.
3417      ((not (boundp id-dep))
3418       (set id-dep (list header)))
3419      ((null (car (symbol-value id-dep)))
3420       (setcar (symbol-value id-dep) header))
3421
3422      ;; From here the `header' was already present in the
3423      ;; `dependencies' table.
3424      (force-new
3425       ;; Overrides an existing entry;
3426       ;; just set the header part of the entry.
3427       (setcar (symbol-value id-dep) header))
3428
3429      ;; Renames the existing `header' to a unique Message-ID.
3430      ((not gnus-summary-ignore-duplicates)
3431       ;; An article with this Message-ID has already been seen.
3432       ;; We rename the Message-ID.
3433       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3434            (list header))
3435       (mail-header-set-id header id))
3436
3437      ;; The last case ignores an existing entry, except it adds any
3438      ;; additional Xrefs (in case the two articles came from different
3439      ;; servers.
3440      ;; Also sets `header' to `nil' meaning that the `dependencies'
3441      ;; table was *not* modified.
3442      (t
3443       (mail-header-set-xref
3444        (car (symbol-value id-dep))
3445        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3446                    "")
3447                (or (mail-header-xref header) "")))
3448       (setq header nil)))
3449
3450     (when header
3451       ;; First check if that we are not creating a References loop.
3452       (setq ref (gnus-parent-id (mail-header-references header)))
3453       (while (and ref
3454                   (setq ref-dep (intern-soft ref dependencies))
3455                   (boundp ref-dep)
3456                   (setq ref-header (car (symbol-value ref-dep))))
3457         (if (string= id ref)
3458             ;; Yuk!  This is a reference loop.  Make the article be a
3459             ;; root article.
3460             (progn
3461               (mail-header-set-references (car (symbol-value id-dep)) "none")
3462               (setq ref nil))
3463           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3464       (setq ref (gnus-parent-id (mail-header-references header)))
3465       (setq ref-dep (intern (or ref "none") dependencies))
3466       (if (boundp ref-dep)
3467           (setcdr (symbol-value ref-dep)
3468                   (nconc (cdr (symbol-value ref-dep))
3469                          (list (symbol-value id-dep))))
3470         (set ref-dep (list nil (symbol-value id-dep)))))
3471     header))
3472
3473 (defun gnus-build-sparse-threads ()
3474   (let ((headers gnus-newsgroup-headers)
3475         (mail-parse-charset gnus-newsgroup-charset)
3476         (gnus-summary-ignore-duplicates t)
3477         header references generation relations
3478         subject child end new-child date)
3479     ;; First we create an alist of generations/relations, where
3480     ;; generations is how much we trust the relation, and the relation
3481     ;; is parent/child.
3482     (gnus-message 7 "Making sparse threads...")
3483     (save-excursion
3484       (nnheader-set-temp-buffer " *gnus sparse threads*")
3485       (while (setq header (pop headers))
3486         (when (and (setq references (mail-header-references header))
3487                    (not (string= references "")))
3488           (insert references)
3489           (setq child (mail-header-id header)
3490                 subject (mail-header-subject header)
3491                 date (mail-header-date header)
3492                 generation 0)
3493           (while (search-backward ">" nil t)
3494             (setq end (1+ (point)))
3495             (when (search-backward "<" nil t)
3496               (setq new-child (buffer-substring (point) end))
3497               (push (list (incf generation)
3498                           child (setq child new-child)
3499                           subject date)
3500                     relations)))
3501           (when child
3502             (push (list (1+ generation) child nil subject) relations))
3503           (erase-buffer)))
3504       (kill-buffer (current-buffer)))
3505     ;; Sort over trustworthiness.
3506     (mapcar
3507      (lambda (relation)
3508        (when (gnus-dependencies-add-header
3509               (make-full-mail-header-from-decoded-header
3510                gnus-reffed-article-number
3511                (nth 3 relation) "" (or (nth 4 relation) "")
3512                (nth 1 relation)
3513                (or (nth 2 relation) "") 0 0 "")
3514               gnus-newsgroup-dependencies nil)
3515          (push gnus-reffed-article-number gnus-newsgroup-limit)
3516          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3517          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3518                gnus-newsgroup-reads)
3519          (decf gnus-reffed-article-number)))
3520      (sort relations 'car-less-than-car))
3521     (gnus-message 7 "Making sparse threads...done")))
3522
3523 (defun gnus-build-old-threads ()
3524   ;; Look at all the articles that refer back to old articles, and
3525   ;; fetch the headers for the articles that aren't there.  This will
3526   ;; build complete threads - if the roots haven't been expired by the
3527   ;; server, that is.
3528   (let ((mail-parse-charset gnus-newsgroup-charset)
3529         id heads)
3530     (mapatoms
3531      (lambda (refs)
3532        (when (not (car (symbol-value refs)))
3533          (setq heads (cdr (symbol-value refs)))
3534          (while heads
3535            (if (memq (mail-header-number (caar heads))
3536                      gnus-newsgroup-dormant)
3537                (setq heads (cdr heads))
3538              (setq id (symbol-name refs))
3539              (while (and (setq id (gnus-build-get-header id))
3540                          (not (car (gnus-id-to-thread id)))))
3541              (setq heads nil)))))
3542      gnus-newsgroup-dependencies)))
3543
3544 ;; This function has to be called with point after the article number
3545 ;; on the beginning of the line.
3546 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3547   (let ((eol (gnus-point-at-eol))
3548         (buffer (current-buffer))
3549         header)
3550
3551     ;; overview: [num subject from date id refs chars lines misc]
3552     (unwind-protect
3553         (progn
3554           (narrow-to-region (point) eol)
3555           (unless (eobp)
3556             (forward-char))
3557
3558           (setq header
3559                 (make-full-mail-header
3560                  number                         ; number
3561                  (nnheader-nov-field)           ; subject
3562                  (nnheader-nov-field)           ; from
3563                  (nnheader-nov-field)           ; date
3564                  (nnheader-nov-read-message-id) ; id
3565                  (nnheader-nov-field)           ; refs
3566                  (nnheader-nov-read-integer)    ; chars
3567                  (nnheader-nov-read-integer)    ; lines
3568                  (unless (eobp)
3569                    (if (looking-at "Xref: ")
3570                        (goto-char (match-end 0)))
3571                    (nnheader-nov-field))        ; Xref
3572                  (nnheader-nov-parse-extra))))  ; extra
3573
3574       (widen))
3575
3576     (when gnus-alter-header-function
3577       (funcall gnus-alter-header-function header))
3578     (gnus-dependencies-add-header header dependencies force-new)))
3579
3580 (defun gnus-build-get-header (id)
3581   "Look through the buffer of NOV lines and find the header to ID.
3582 Enter this line into the dependencies hash table, and return
3583 the id of the parent article (if any)."
3584   (let ((deps gnus-newsgroup-dependencies)
3585         found header)
3586     (prog1
3587         (save-excursion
3588           (set-buffer nntp-server-buffer)
3589           (let ((case-fold-search nil))
3590             (goto-char (point-min))
3591             (while (and (not found)
3592                         (search-forward id nil t))
3593               (beginning-of-line)
3594               (setq found (looking-at
3595                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3596                                    (regexp-quote id))))
3597               (or found (beginning-of-line 2)))
3598             (when found
3599               (beginning-of-line)
3600               (and
3601                (setq header (gnus-nov-parse-line
3602                              (read (current-buffer)) deps))
3603                (gnus-parent-id (mail-header-references header))))))
3604       (when header
3605         (let ((number (mail-header-number header)))
3606           (push number gnus-newsgroup-limit)
3607           (push header gnus-newsgroup-headers)
3608           (if (memq number gnus-newsgroup-unselected)
3609               (progn
3610                 (push number gnus-newsgroup-unreads)
3611                 (setq gnus-newsgroup-unselected
3612                       (delq number gnus-newsgroup-unselected)))
3613             (push number gnus-newsgroup-ancient)))))))
3614
3615 (defun gnus-build-all-threads ()
3616   "Read all the headers."
3617   (let ((gnus-summary-ignore-duplicates t)
3618         (mail-parse-charset gnus-newsgroup-charset)
3619         (dependencies gnus-newsgroup-dependencies)
3620         header article)
3621     (save-excursion
3622       (set-buffer nntp-server-buffer)
3623       (let ((case-fold-search nil))
3624         (goto-char (point-min))
3625         (while (not (eobp))
3626           (ignore-errors
3627             (setq article (read (current-buffer))
3628                   header (gnus-nov-parse-line article dependencies)))
3629           (when header
3630             (save-excursion
3631               (set-buffer gnus-summary-buffer)
3632               (push header gnus-newsgroup-headers)
3633               (if (memq (setq article (mail-header-number header))
3634                         gnus-newsgroup-unselected)
3635                   (progn
3636                     (push article gnus-newsgroup-unreads)
3637                     (setq gnus-newsgroup-unselected
3638                           (delq article gnus-newsgroup-unselected)))
3639                 (push article gnus-newsgroup-ancient)))
3640             (forward-line 1)))))))
3641
3642 (defun gnus-summary-update-article-line (article header)
3643   "Update the line for ARTICLE using HEADERS."
3644   (let* ((id (mail-header-id header))
3645          (thread (gnus-id-to-thread id)))
3646     (unless thread
3647       (error "Article in no thread"))
3648     ;; Update the thread.
3649     (setcar thread header)
3650     (gnus-summary-goto-subject article)
3651     (let* ((datal (gnus-data-find-list article))
3652            (data (car datal))
3653            (length (when (cdr datal)
3654                      (- (gnus-data-pos data)
3655                         (gnus-data-pos (cadr datal)))))
3656            (buffer-read-only nil)
3657            (level (gnus-summary-thread-level)))
3658       (gnus-delete-line)
3659       (gnus-summary-insert-line
3660        header level nil (gnus-article-mark article)
3661        (memq article gnus-newsgroup-replied)
3662        (memq article gnus-newsgroup-expirable)
3663        ;; Only insert the Subject string when it's different
3664        ;; from the previous Subject string.
3665        (if (and
3666             gnus-show-threads
3667             (gnus-subject-equal
3668              (condition-case ()
3669                  (mail-header-subject
3670                   (gnus-data-header
3671                    (cadr
3672                     (gnus-data-find-list
3673                      article
3674                      (gnus-data-list t)))))
3675                ;; Error on the side of excessive subjects.
3676                (error ""))
3677              (mail-header-subject header)))
3678            ""
3679          (mail-header-subject header))
3680        nil (cdr (assq article gnus-newsgroup-scored))
3681        (memq article gnus-newsgroup-processable))
3682       (when length
3683         (gnus-data-update-list
3684          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3685
3686 (defun gnus-summary-update-article (article &optional iheader)
3687   "Update ARTICLE in the summary buffer."
3688   (set-buffer gnus-summary-buffer)
3689   (let* ((header (gnus-summary-article-header article))
3690          (id (mail-header-id header))
3691          (data (gnus-data-find article))
3692          (thread (gnus-id-to-thread id))
3693          (references (mail-header-references header))
3694          (parent
3695           (gnus-id-to-thread
3696            (or (gnus-parent-id
3697                 (when (and references
3698                            (not (equal "" references)))
3699                   references))
3700                "none")))
3701          (buffer-read-only nil)
3702          (old (car thread)))
3703     (when thread
3704       (unless iheader
3705         (setcar thread nil)
3706         (when parent
3707           (delq thread parent)))
3708       (if (gnus-summary-insert-subject id header)
3709           ;; Set the (possibly) new article number in the data structure.
3710           (gnus-data-set-number data (gnus-id-to-article id))
3711         (setcar thread old)
3712         nil))))
3713
3714 (defun gnus-rebuild-thread (id &optional line)
3715   "Rebuild the thread containing ID.
3716 If LINE, insert the rebuilt thread starting on line LINE."
3717   (let ((buffer-read-only nil)
3718         old-pos current thread data)
3719     (if (not gnus-show-threads)
3720         (setq thread (list (car (gnus-id-to-thread id))))
3721       ;; Get the thread this article is part of.
3722       (setq thread (gnus-remove-thread id)))
3723     (setq old-pos (gnus-point-at-bol))
3724     (setq current (save-excursion
3725                     (and (zerop (forward-line -1))
3726                          (gnus-summary-article-number))))
3727     ;; If this is a gathered thread, we have to go some re-gathering.
3728     (when (stringp (car thread))
3729       (let ((subject (car thread))
3730             roots thr)
3731         (setq thread (cdr thread))
3732         (while thread
3733           (unless (memq (setq thr (gnus-id-to-thread
3734                                    (gnus-root-id
3735                                     (mail-header-id (caar thread)))))
3736                         roots)
3737             (push thr roots))
3738           (setq thread (cdr thread)))
3739         ;; We now have all (unique) roots.
3740         (if (= (length roots) 1)
3741             ;; All the loose roots are now one solid root.
3742             (setq thread (car roots))
3743           (setq thread (cons subject (gnus-sort-threads roots))))))
3744     (let (threads)
3745       ;; We then insert this thread into the summary buffer.
3746       (when line
3747         (goto-char (point-min))
3748         (forward-line (1- line)))
3749       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3750         (if gnus-show-threads
3751             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3752           (gnus-summary-prepare-unthreaded thread))
3753         (setq data (nreverse gnus-newsgroup-data))
3754         (setq threads gnus-newsgroup-threads))
3755       ;; We splice the new data into the data structure.
3756       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3757       ;;!!! then we want to insert at the beginning of the buffer.
3758       ;;!!! That happens to be true with Gnus now, but that may
3759       ;;!!! change in the future.  Perhaps.
3760       (gnus-data-enter-list
3761        (if line nil current) data (- (point) old-pos))
3762       (setq gnus-newsgroup-threads
3763             (nconc threads gnus-newsgroup-threads))
3764       (gnus-data-compute-positions))))
3765
3766 (defun gnus-number-to-header (number)
3767   "Return the header for article NUMBER."
3768   (let ((headers gnus-newsgroup-headers))
3769     (while (and headers
3770                 (not (= number (mail-header-number (car headers)))))
3771       (pop headers))
3772     (when headers
3773       (car headers))))
3774
3775 (defun gnus-parent-headers (in-headers &optional generation)
3776   "Return the headers of the GENERATIONeth parent of HEADERS."
3777   (unless generation
3778     (setq generation 1))
3779   (let ((parent t)
3780         (headers in-headers)
3781         references)
3782     (while (and parent
3783                 (not (zerop generation))
3784                 (setq references (mail-header-references headers)))
3785       (setq headers (if (and references
3786                              (setq parent (gnus-parent-id references)))
3787                         (car (gnus-id-to-thread parent))
3788                       nil))
3789       (decf generation))
3790     (and (not (eq headers in-headers))
3791          headers)))
3792
3793 (defun gnus-id-to-thread (id)
3794   "Return the (sub-)thread where ID appears."
3795   (gnus-gethash id gnus-newsgroup-dependencies))
3796
3797 (defun gnus-id-to-article (id)
3798   "Return the article number of ID."
3799   (let ((thread (gnus-id-to-thread id)))
3800     (when (and thread
3801                (car thread))
3802       (mail-header-number (car thread)))))
3803
3804 (defun gnus-id-to-header (id)
3805   "Return the article headers of ID."
3806   (car (gnus-id-to-thread id)))
3807
3808 (defun gnus-article-displayed-root-p (article)
3809   "Say whether ARTICLE is a root(ish) article."
3810   (let ((level (gnus-summary-thread-level article))
3811         (refs (mail-header-references  (gnus-summary-article-header article)))
3812         particle)
3813     (cond
3814      ((null level) nil)
3815      ((zerop level) t)
3816      ((null refs) t)
3817      ((null (gnus-parent-id refs)) t)
3818      ((and (= 1 level)
3819            (null (setq particle (gnus-id-to-article
3820                                  (gnus-parent-id refs))))
3821            (null (gnus-summary-thread-level particle)))))))
3822
3823 (defun gnus-root-id (id)
3824   "Return the id of the root of the thread where ID appears."
3825   (let (last-id prev)
3826     (while (and id (setq prev (car (gnus-id-to-thread id))))
3827       (setq last-id id
3828             id (gnus-parent-id (mail-header-references prev))))
3829     last-id))
3830
3831 (defun gnus-articles-in-thread (thread)
3832   "Return the list of articles in THREAD."
3833   (cons (mail-header-number (car thread))
3834         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3835
3836 (defun gnus-remove-thread (id &optional dont-remove)
3837   "Remove the thread that has ID in it."
3838   (let (headers thread last-id)
3839     ;; First go up in this thread until we find the root.
3840     (setq last-id (gnus-root-id id)
3841           headers (message-flatten-list (gnus-id-to-thread last-id)))
3842     ;; We have now found the real root of this thread.  It might have
3843     ;; been gathered into some loose thread, so we have to search
3844     ;; through the threads to find the thread we wanted.
3845     (let ((threads gnus-newsgroup-threads)
3846           sub)
3847       (while threads
3848         (setq sub (car threads))
3849         (if (stringp (car sub))
3850             ;; This is a gathered thread, so we look at the roots
3851             ;; below it to find whether this article is in this
3852             ;; gathered root.
3853             (progn
3854               (setq sub (cdr sub))
3855               (while sub
3856                 (when (member (caar sub) headers)
3857                   (setq thread (car threads)
3858                         threads nil
3859                         sub nil))
3860                 (setq sub (cdr sub))))
3861           ;; It's an ordinary thread, so we check it.
3862           (when (eq (car sub) (car headers))
3863             (setq thread sub
3864                   threads nil)))
3865         (setq threads (cdr threads)))
3866       ;; If this article is in no thread, then it's a root.
3867       (if thread
3868           (unless dont-remove
3869             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3870         (setq thread (gnus-id-to-thread last-id)))
3871       (when thread
3872         (prog1
3873             thread                      ; We return this thread.
3874           (unless dont-remove
3875             (if (stringp (car thread))
3876                 (progn
3877                   ;; If we use dummy roots, then we have to remove the
3878                   ;; dummy root as well.
3879                   (when (eq gnus-summary-make-false-root 'dummy)
3880                     ;; We go to the dummy root by going to
3881                     ;; the first sub-"thread", and then one line up.
3882                     (gnus-summary-goto-article
3883                      (mail-header-number (caadr thread)))
3884                     (forward-line -1)
3885                     (gnus-delete-line)
3886                     (gnus-data-compute-positions))
3887                   (setq thread (cdr thread))
3888                   (while thread
3889                     (gnus-remove-thread-1 (car thread))
3890                     (setq thread (cdr thread))))
3891               (gnus-remove-thread-1 thread))))))))
3892
3893 (defun gnus-remove-thread-1 (thread)
3894   "Remove the thread THREAD recursively."
3895   (let ((number (mail-header-number (pop thread)))
3896         d)
3897     (setq thread (reverse thread))
3898     (while thread
3899       (gnus-remove-thread-1 (pop thread)))
3900     (when (setq d (gnus-data-find number))
3901       (goto-char (gnus-data-pos d))
3902       (gnus-summary-show-thread)
3903       (gnus-data-remove
3904        number
3905        (- (gnus-point-at-bol)
3906           (prog1
3907               (1+ (gnus-point-at-eol))
3908             (gnus-delete-line)))))))
3909
3910 (defun gnus-sort-threads-1 (threads func)
3911   (sort (mapcar (lambda (thread)
3912                   (cons (car thread)
3913                         (and (cdr thread)
3914                              (gnus-sort-threads-1 (cdr thread) func))))
3915                 threads) func))
3916
3917 (defun gnus-sort-threads (threads)
3918   "Sort THREADS."
3919   (if (not gnus-thread-sort-functions)
3920       threads
3921     (gnus-message 8 "Sorting threads...")
3922     (prog1
3923         (gnus-sort-threads-1
3924          threads
3925          (gnus-make-sort-function gnus-thread-sort-functions))
3926       (gnus-message 8 "Sorting threads...done"))))
3927
3928 (defun gnus-sort-articles (articles)
3929   "Sort ARTICLES."
3930   (when gnus-article-sort-functions
3931     (gnus-message 7 "Sorting articles...")
3932     (prog1
3933         (setq gnus-newsgroup-headers
3934               (sort articles (gnus-make-sort-function
3935                               gnus-article-sort-functions)))
3936       (gnus-message 7 "Sorting articles...done"))))
3937
3938 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3939 (defmacro gnus-thread-header (thread)
3940   "Return header of first article in THREAD.
3941 Note that THREAD must never, ever be anything else than a variable -
3942 using some other form will lead to serious barfage."
3943   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3944   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3945   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3946         (vector thread) 2))
3947
3948 (defsubst gnus-article-sort-by-number (h1 h2)
3949   "Sort articles by article number."
3950   (< (mail-header-number h1)
3951      (mail-header-number h2)))
3952
3953 (defun gnus-thread-sort-by-number (h1 h2)
3954   "Sort threads by root article number."
3955   (gnus-article-sort-by-number
3956    (gnus-thread-header h1) (gnus-thread-header h2)))
3957
3958 (defsubst gnus-article-sort-by-lines (h1 h2)
3959   "Sort articles by article Lines header."
3960   (< (mail-header-lines h1)
3961      (mail-header-lines h2)))
3962
3963 (defun gnus-thread-sort-by-lines (h1 h2)
3964   "Sort threads by root article Lines header."
3965   (gnus-article-sort-by-lines
3966    (gnus-thread-header h1) (gnus-thread-header h2)))
3967
3968 (defsubst gnus-article-sort-by-chars (h1 h2)
3969   "Sort articles by octet length."
3970   (< (mail-header-chars h1)
3971      (mail-header-chars h2)))
3972
3973 (defun gnus-thread-sort-by-chars (h1 h2)
3974   "Sort threads by root article octet length."
3975   (gnus-article-sort-by-chars
3976    (gnus-thread-header h1) (gnus-thread-header h2)))
3977
3978 (defsubst gnus-article-sort-by-author (h1 h2)
3979   "Sort articles by root author."
3980   (string-lessp
3981    (let ((addr (car (mime-entity-read-field h1 'From))))
3982      (or (std11-full-name-string addr)
3983          (std11-address-string addr)
3984          ""))
3985    (let ((addr (car (mime-entity-read-field h2 'From))))
3986      (or (std11-full-name-string addr)
3987          (std11-address-string addr)
3988          ""))
3989    ))
3990
3991 (defun gnus-thread-sort-by-author (h1 h2)
3992   "Sort threads by root author."
3993   (gnus-article-sort-by-author
3994    (gnus-thread-header h1)  (gnus-thread-header h2)))
3995
3996 (defsubst gnus-article-sort-by-subject (h1 h2)
3997   "Sort articles by root subject."
3998   (string-lessp
3999    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4000    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4001
4002 (defun gnus-thread-sort-by-subject (h1 h2)
4003   "Sort threads by root subject."
4004   (gnus-article-sort-by-subject
4005    (gnus-thread-header h1) (gnus-thread-header h2)))
4006
4007 (defsubst gnus-article-sort-by-date (h1 h2)
4008   "Sort articles by root article date."
4009   (time-less-p
4010    (gnus-date-get-time (mail-header-date h1))
4011    (gnus-date-get-time (mail-header-date h2))))
4012
4013 (defun gnus-thread-sort-by-date (h1 h2)
4014   "Sort threads by root article date."
4015   (gnus-article-sort-by-date
4016    (gnus-thread-header h1) (gnus-thread-header h2)))
4017
4018 (defsubst gnus-article-sort-by-score (h1 h2)
4019   "Sort articles by root article score.
4020 Unscored articles will be counted as having a score of zero."
4021   (> (or (cdr (assq (mail-header-number h1)
4022                     gnus-newsgroup-scored))
4023          gnus-summary-default-score 0)
4024      (or (cdr (assq (mail-header-number h2)
4025                     gnus-newsgroup-scored))
4026          gnus-summary-default-score 0)))
4027
4028 (defun gnus-thread-sort-by-score (h1 h2)
4029   "Sort threads by root article score."
4030   (gnus-article-sort-by-score
4031    (gnus-thread-header h1) (gnus-thread-header h2)))
4032
4033 (defun gnus-thread-sort-by-total-score (h1 h2)
4034   "Sort threads by the sum of all scores in the thread.
4035 Unscored articles will be counted as having a score of zero."
4036   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4037
4038 (defun gnus-thread-total-score (thread)
4039   ;; This function find the total score of THREAD.
4040   (cond ((null thread)
4041          0)
4042         ((consp thread)
4043          (if (stringp (car thread))
4044              (apply gnus-thread-score-function 0
4045                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4046            (gnus-thread-total-score-1 thread)))
4047         (t
4048          (gnus-thread-total-score-1 (list thread)))))
4049
4050 (defun gnus-thread-total-score-1 (root)
4051   ;; This function find the total score of the thread below ROOT.
4052   (setq root (car root))
4053   (apply gnus-thread-score-function
4054          (or (append
4055               (mapcar 'gnus-thread-total-score
4056                       (cdr (gnus-id-to-thread (mail-header-id root))))
4057               (when (> (mail-header-number root) 0)
4058                 (list (or (cdr (assq (mail-header-number root)
4059                                      gnus-newsgroup-scored))
4060                           gnus-summary-default-score 0))))
4061              (list gnus-summary-default-score)
4062              '(0))))
4063
4064 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4065 (defvar gnus-tmp-prev-subject nil)
4066 (defvar gnus-tmp-false-parent nil)
4067 (defvar gnus-tmp-root-expunged nil)
4068 (defvar gnus-tmp-dummy-line nil)
4069
4070 (eval-when-compile (defvar gnus-tmp-header))
4071 (defun gnus-extra-header (type &optional header)
4072   "Return the extra header of TYPE."
4073   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4074       ""))
4075
4076 (defun gnus-summary-prepare-threads (threads)
4077   "Prepare summary buffer from THREADS and indentation LEVEL.
4078 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4079 or a straight list of headers."
4080   (gnus-message 7 "Generating summary...")
4081
4082   (setq gnus-newsgroup-threads threads)
4083   (beginning-of-line)
4084
4085   (let ((gnus-tmp-level 0)
4086         (default-score (or gnus-summary-default-score 0))
4087         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4088         thread number subject stack state gnus-tmp-gathered beg-match
4089         new-roots gnus-tmp-new-adopts thread-end
4090         gnus-tmp-header gnus-tmp-unread
4091         gnus-tmp-replied gnus-tmp-subject-or-nil
4092         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4093         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4094         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
4095
4096     (setq gnus-tmp-prev-subject nil)
4097
4098     (if (vectorp (car threads))
4099         ;; If this is a straight (sic) list of headers, then a
4100         ;; threaded summary display isn't required, so we just create
4101         ;; an unthreaded one.
4102         (gnus-summary-prepare-unthreaded threads)
4103
4104       ;; Do the threaded display.
4105
4106       (while (or threads stack gnus-tmp-new-adopts new-roots)
4107
4108         (if (and (= gnus-tmp-level 0)
4109                  (or (not stack)
4110                      (= (caar stack) 0))
4111                  (not gnus-tmp-false-parent)
4112                  (or gnus-tmp-new-adopts new-roots))
4113             (if gnus-tmp-new-adopts
4114                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4115                       thread (list (car gnus-tmp-new-adopts))
4116                       gnus-tmp-header (caar thread)
4117                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4118               (when new-roots
4119                 (setq thread (list (car new-roots))
4120                       gnus-tmp-header (caar thread)
4121                       new-roots (cdr new-roots))))
4122
4123           (if threads
4124               ;; If there are some threads, we do them before the
4125               ;; threads on the stack.
4126               (setq thread threads
4127                     gnus-tmp-header (caar thread))
4128             ;; There were no current threads, so we pop something off
4129             ;; the stack.
4130             (setq state (car stack)
4131                   gnus-tmp-level (car state)
4132                   thread (cdr state)
4133                   stack (cdr stack)
4134                   gnus-tmp-header (caar thread))))
4135
4136         (setq gnus-tmp-false-parent nil)
4137         (setq gnus-tmp-root-expunged nil)
4138         (setq thread-end nil)
4139
4140         (if (stringp gnus-tmp-header)
4141             ;; The header is a dummy root.
4142             (cond
4143              ((eq gnus-summary-make-false-root 'adopt)
4144               ;; We let the first article adopt the rest.
4145               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4146                                                (cddar thread)))
4147               (setq gnus-tmp-gathered
4148                     (nconc (mapcar
4149                             (lambda (h) (mail-header-number (car h)))
4150                             (cddar thread))
4151                            gnus-tmp-gathered))
4152               (setq thread (cons (list (caar thread)
4153                                        (cadar thread))
4154                                  (cdr thread)))
4155               (setq gnus-tmp-level -1
4156                     gnus-tmp-false-parent t))
4157              ((eq gnus-summary-make-false-root 'empty)
4158               ;; We print adopted articles with empty subject fields.
4159               (setq gnus-tmp-gathered
4160                     (nconc (mapcar
4161                             (lambda (h) (mail-header-number (car h)))
4162                             (cddar thread))
4163                            gnus-tmp-gathered))
4164               (setq gnus-tmp-level -1))
4165              ((eq gnus-summary-make-false-root 'dummy)
4166               ;; We remember that we probably want to output a dummy
4167               ;; root.
4168               (setq gnus-tmp-dummy-line gnus-tmp-header)
4169               (setq gnus-tmp-prev-subject gnus-tmp-header))
4170              (t
4171               ;; We do not make a root for the gathered
4172               ;; sub-threads at all.
4173               (setq gnus-tmp-level -1)))
4174
4175           (setq number (mail-header-number gnus-tmp-header)
4176                 subject (mail-header-subject gnus-tmp-header))
4177
4178           (cond
4179            ;; If the thread has changed subject, we might want to make
4180            ;; this subthread into a root.
4181            ((and (null gnus-thread-ignore-subject)
4182                  (not (zerop gnus-tmp-level))
4183                  gnus-tmp-prev-subject
4184                  (not (inline
4185                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4186             (setq new-roots (nconc new-roots (list (car thread)))
4187                   thread-end t
4188                   gnus-tmp-header nil))
4189            ;; If the article lies outside the current limit,
4190            ;; then we do not display it.
4191            ((not (memq number gnus-newsgroup-limit))
4192             (setq gnus-tmp-gathered
4193                   (nconc (mapcar
4194                           (lambda (h) (mail-header-number (car h)))
4195                           (cdar thread))
4196                          gnus-tmp-gathered))
4197             (setq gnus-tmp-new-adopts (if (cdar thread)
4198                                           (append gnus-tmp-new-adopts
4199                                                   (cdar thread))
4200                                         gnus-tmp-new-adopts)
4201                   thread-end t
4202                   gnus-tmp-header nil)
4203             (when (zerop gnus-tmp-level)
4204               (setq gnus-tmp-root-expunged t)))
4205            ;; Perhaps this article is to be marked as read?
4206            ((and gnus-summary-mark-below
4207                  (< (or (cdr (assq number gnus-newsgroup-scored))
4208                         default-score)
4209                     gnus-summary-mark-below)
4210                  ;; Don't touch sparse articles.
4211                  (not (gnus-summary-article-sparse-p number))
4212                  (not (gnus-summary-article-ancient-p number)))
4213             (setq gnus-newsgroup-unreads
4214                   (delq number gnus-newsgroup-unreads))
4215             (if gnus-newsgroup-auto-expire
4216                 (push number gnus-newsgroup-expirable)
4217               (push (cons number gnus-low-score-mark)
4218                     gnus-newsgroup-reads))))
4219
4220           (when gnus-tmp-header
4221             ;; We may have an old dummy line to output before this
4222             ;; article.
4223             (when (and gnus-tmp-dummy-line
4224                        (gnus-subject-equal
4225                         gnus-tmp-dummy-line
4226                         (mail-header-subject gnus-tmp-header)))
4227               (gnus-summary-insert-dummy-line
4228                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4229               (setq gnus-tmp-dummy-line nil))
4230
4231             ;; Compute the mark.
4232             (setq gnus-tmp-unread (gnus-article-mark number))
4233
4234             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4235                                   gnus-tmp-header gnus-tmp-level)
4236                   gnus-newsgroup-data)
4237
4238             ;; Actually insert the line.
4239             (setq
4240              gnus-tmp-subject-or-nil
4241              (cond
4242               ((and gnus-thread-ignore-subject
4243                     gnus-tmp-prev-subject
4244                     (not (inline (gnus-subject-equal
4245                                   gnus-tmp-prev-subject subject))))
4246                subject)
4247               ((zerop gnus-tmp-level)
4248                (if (and (eq gnus-summary-make-false-root 'empty)
4249                         (memq number gnus-tmp-gathered)
4250                         gnus-tmp-prev-subject
4251                         (inline (gnus-subject-equal
4252                                  gnus-tmp-prev-subject subject)))
4253                    gnus-summary-same-subject
4254                  subject))
4255               (t gnus-summary-same-subject)))
4256             (if (and (eq gnus-summary-make-false-root 'adopt)
4257                      (= gnus-tmp-level 1)
4258                      (memq number gnus-tmp-gathered))
4259                 (setq gnus-tmp-opening-bracket ?\<
4260                       gnus-tmp-closing-bracket ?\>)
4261               (setq gnus-tmp-opening-bracket ?\[
4262                     gnus-tmp-closing-bracket ?\]))
4263             (setq
4264              gnus-tmp-indentation
4265              (aref gnus-thread-indent-array gnus-tmp-level)
4266              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4267              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4268                                 gnus-summary-default-score 0)
4269              gnus-tmp-score-char
4270              (if (or (null gnus-summary-default-score)
4271                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4272                          gnus-summary-zcore-fuzz))
4273                  ?\ ;;;Whitespace
4274                (if (< gnus-tmp-score gnus-summary-default-score)
4275                    gnus-score-below-mark gnus-score-over-mark))
4276              gnus-tmp-replied
4277              (cond ((memq number gnus-newsgroup-processable)
4278                     gnus-process-mark)
4279                    ((memq number gnus-newsgroup-cached)
4280                     gnus-cached-mark)
4281                    ((memq number gnus-newsgroup-replied)
4282                     gnus-replied-mark)
4283                    ((memq number gnus-newsgroup-forwarded)
4284                     gnus-forwarded-mark)
4285                    ((memq number gnus-newsgroup-saved)
4286                     gnus-saved-mark)
4287                    (t gnus-no-mark))
4288              gnus-tmp-from (mail-header-from gnus-tmp-header)
4289              gnus-tmp-name
4290              (cond
4291               ((string-match "<[^>]+> *$" gnus-tmp-from)
4292                (setq beg-match (match-beginning 0))
4293                (or (and (string-match "^\".+\"" gnus-tmp-from)
4294                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4295                    (substring gnus-tmp-from 0 beg-match)))
4296               ((string-match "(.+)" gnus-tmp-from)
4297                (substring gnus-tmp-from
4298                           (1+ (match-beginning 0)) (1- (match-end 0))))
4299               (t gnus-tmp-from)))
4300             (when (string= gnus-tmp-name "")
4301               (setq gnus-tmp-name gnus-tmp-from))
4302             (unless (numberp gnus-tmp-lines)
4303               (setq gnus-tmp-lines -1))
4304             (when (= gnus-tmp-lines -1)
4305               (setq gnus-tmp-lines "?"))
4306             (gnus-put-text-property
4307              (point)
4308              (progn (eval gnus-summary-line-format-spec) (point))
4309              'gnus-number number)
4310             (when gnus-visual-p
4311               (forward-line -1)
4312               (gnus-run-hooks 'gnus-summary-update-hook)
4313               (forward-line 1))
4314
4315             (setq gnus-tmp-prev-subject subject)))
4316
4317         (when (nth 1 thread)
4318           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4319         (incf gnus-tmp-level)
4320         (setq threads (if thread-end nil (cdar thread)))
4321         (unless threads
4322           (setq gnus-tmp-level 0)))))
4323   (gnus-message 7 "Generating summary...done"))
4324
4325 (defun gnus-summary-prepare-unthreaded (headers)
4326   "Generate an unthreaded summary buffer based on HEADERS."
4327   (let (header number mark)
4328
4329     (beginning-of-line)
4330
4331     (while headers
4332       ;; We may have to root out some bad articles...
4333       (when (memq (setq number (mail-header-number
4334                                 (setq header (pop headers))))
4335                   gnus-newsgroup-limit)
4336         ;; Mark article as read when it has a low score.
4337         (when (and gnus-summary-mark-below
4338                    (< (or (cdr (assq number gnus-newsgroup-scored))
4339                           gnus-summary-default-score 0)
4340                       gnus-summary-mark-below)
4341                    (not (gnus-summary-article-ancient-p number)))
4342           (setq gnus-newsgroup-unreads
4343                 (delq number gnus-newsgroup-unreads))
4344           (if gnus-newsgroup-auto-expire
4345               (push number gnus-newsgroup-expirable)
4346             (push (cons number gnus-low-score-mark)
4347                   gnus-newsgroup-reads)))
4348
4349         (setq mark (gnus-article-mark number))
4350         (push (gnus-data-make number mark (1+ (point)) header 0)
4351               gnus-newsgroup-data)
4352         (gnus-summary-insert-line
4353          header 0 number
4354          mark (memq number gnus-newsgroup-replied)
4355          (memq number gnus-newsgroup-expirable)
4356          (mail-header-subject header) nil
4357          (cdr (assq number gnus-newsgroup-scored))
4358          (memq number gnus-newsgroup-processable))))))
4359
4360 (defun gnus-summary-remove-list-identifiers ()
4361   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4362   (let ((regexp (if (consp gnus-list-identifiers)
4363                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4364                   gnus-list-identifiers))
4365         changed subject)
4366     (when regexp
4367       (dolist (header gnus-newsgroup-headers)
4368         (setq subject (mail-header-subject header)
4369               changed nil)
4370         (while (string-match
4371                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4372                 subject)
4373           (setq subject
4374                 (concat (substring subject 0 (match-beginning 2))
4375                         (substring subject (match-end 0)))
4376                 changed t))
4377         (when (and changed
4378                    (string-match
4379                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4380           (setq subject
4381                 (concat (substring subject 0 (match-beginning 1))
4382                         (substring subject (match-end 1)))))
4383         (when changed
4384           (mail-header-set-subject header subject))))))
4385
4386 (defun gnus-fetch-headers (articles)
4387   "Fetch headers of ARTICLES."
4388   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4389     (gnus-message 5 "Fetching headers for %s..." name)
4390     (prog1
4391         (if (eq 'nov
4392                 (setq gnus-headers-retrieved-by
4393                       (gnus-retrieve-headers
4394                        articles gnus-newsgroup-name
4395                        ;; We might want to fetch old headers, but
4396                        ;; not if there is only 1 article.
4397                        (and (or (and
4398                                  (not (eq gnus-fetch-old-headers 'some))
4399                                  (not (numberp gnus-fetch-old-headers)))
4400                                 (> (length articles) 1))
4401                             gnus-fetch-old-headers))))
4402             (gnus-get-newsgroup-headers-xover
4403              articles nil nil gnus-newsgroup-name t)
4404           (gnus-get-newsgroup-headers))
4405       (gnus-message 5 "Fetching headers for %s...done" name))))
4406
4407 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4408   "Select newsgroup GROUP.
4409 If READ-ALL is non-nil, all articles in the group are selected.
4410 If SELECT-ARTICLES, only select those articles from GROUP."
4411   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4412          ;;!!! Dirty hack; should be removed.
4413          (gnus-summary-ignore-duplicates
4414           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4415               t
4416             gnus-summary-ignore-duplicates))
4417          (info (nth 2 entry))
4418          articles fetched-articles cached)
4419
4420     (unless (gnus-check-server
4421              (setq gnus-current-select-method
4422                    (gnus-find-method-for-group group)))
4423       (error "Couldn't open server"))
4424
4425     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4426         (gnus-activate-group group)     ; Or we can activate it...
4427         (progn                          ; Or we bug out.
4428           (when (equal major-mode 'gnus-summary-mode)
4429             (kill-buffer (current-buffer)))
4430           (error "Couldn't activate group %s: %s"
4431                  group (gnus-status-message group))))
4432
4433     (unless (gnus-request-group group t)
4434       (when (equal major-mode 'gnus-summary-mode)
4435         (kill-buffer (current-buffer)))
4436       (error "Couldn't request group %s: %s"
4437              group (gnus-status-message group)))
4438
4439     (setq gnus-newsgroup-name group)
4440     (setq gnus-newsgroup-unselected nil)
4441     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4442     (gnus-summary-setup-default-charset)
4443
4444     ;; Adjust and set lists of article marks.
4445     (when info
4446       (gnus-adjust-marked-articles info))
4447
4448     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4449     (when (gnus-virtual-group-p group)
4450       (setq cached gnus-newsgroup-cached))
4451
4452     (setq gnus-newsgroup-unreads
4453           (gnus-set-difference
4454            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4455            gnus-newsgroup-dormant))
4456
4457     (setq gnus-newsgroup-processable nil)
4458
4459     (gnus-update-read-articles group gnus-newsgroup-unreads)
4460
4461     (if (setq articles select-articles)
4462         (setq gnus-newsgroup-unselected
4463               (gnus-sorted-intersection
4464                gnus-newsgroup-unreads
4465                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4466       (setq articles (gnus-articles-to-read group read-all)))
4467
4468     (cond
4469      ((null articles)
4470       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4471       'quit)
4472      ((eq articles 0) nil)
4473      (t
4474       ;; Init the dependencies hash table.
4475       (setq gnus-newsgroup-dependencies
4476             (gnus-make-hashtable (length articles)))
4477       (gnus-set-global-variables)
4478       ;; Retrieve the headers and read them in.
4479       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4480
4481       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4482       (when cached
4483         (setq gnus-newsgroup-cached cached))
4484
4485       ;; Suppress duplicates?
4486       (when gnus-suppress-duplicates
4487         (gnus-dup-suppress-articles))
4488
4489       ;; Set the initial limit.
4490       (setq gnus-newsgroup-limit (copy-sequence articles))
4491       ;; Remove canceled articles from the list of unread articles.
4492       (setq gnus-newsgroup-unreads
4493             (gnus-set-sorted-intersection
4494              gnus-newsgroup-unreads
4495              (setq fetched-articles
4496                    (mapcar (lambda (headers) (mail-header-number headers))
4497                            gnus-newsgroup-headers))))
4498       ;; Removed marked articles that do not exist.
4499       (gnus-update-missing-marks
4500        (gnus-sorted-complement fetched-articles articles))
4501       ;; We might want to build some more threads first.
4502       (when (and gnus-fetch-old-headers
4503                  (eq gnus-headers-retrieved-by 'nov))
4504         (if (eq gnus-fetch-old-headers 'invisible)
4505             (gnus-build-all-threads)
4506           (gnus-build-old-threads)))
4507       ;; Let the Gnus agent mark articles as read.
4508       (when gnus-agent
4509         (gnus-agent-get-undownloaded-list))
4510       ;; Remove list identifiers from subject
4511       (when gnus-list-identifiers
4512         (gnus-summary-remove-list-identifiers))
4513       ;; Check whether auto-expire is to be done in this group.
4514       (setq gnus-newsgroup-auto-expire
4515             (gnus-group-auto-expirable-p group))
4516       ;; Set up the article buffer now, if necessary.
4517       (unless gnus-single-article-buffer
4518         (gnus-article-setup-buffer))
4519       ;; First and last article in this newsgroup.
4520       (when gnus-newsgroup-headers
4521         (setq gnus-newsgroup-begin
4522               (mail-header-number (car gnus-newsgroup-headers))
4523               gnus-newsgroup-end
4524               (mail-header-number
4525                (gnus-last-element gnus-newsgroup-headers))))
4526       ;; GROUP is successfully selected.
4527       (or gnus-newsgroup-headers t)))))
4528
4529 (defun gnus-articles-to-read (group &optional read-all)
4530   "Find out what articles the user wants to read."
4531   (let* ((articles
4532           ;; Select all articles if `read-all' is non-nil, or if there
4533           ;; are no unread articles.
4534           (if (or read-all
4535                   (and (zerop (length gnus-newsgroup-marked))
4536                        (zerop (length gnus-newsgroup-unreads)))
4537                   (eq (gnus-group-find-parameter group 'display)
4538                       'all))
4539               (or
4540                (gnus-uncompress-range (gnus-active group))
4541                (gnus-cache-articles-in-group group))
4542             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4543                           (copy-sequence gnus-newsgroup-unreads))
4544                   '<)))
4545          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4546          (scored (length scored-list))
4547          (number (length articles))
4548          (marked (+ (length gnus-newsgroup-marked)
4549                     (length gnus-newsgroup-dormant)))
4550          (select
4551           (cond
4552            ((numberp read-all)
4553             read-all)
4554            (t
4555             (condition-case ()
4556                 (cond
4557                  ((and (or (<= scored marked) (= scored number))
4558                        (natnump gnus-large-newsgroup)
4559                        (> number gnus-large-newsgroup))
4560                   (let* ((cursor-in-echo-area nil)
4561                          (input (read-from-minibuffer
4562                                  (format
4563                                   "How many articles from %s (max %d): "
4564                                   (gnus-limit-string gnus-newsgroup-name 35)
4565                                   number)
4566                                  (cons (number-to-string gnus-large-newsgroup)
4567                                        0))))
4568                     (if (string-match "^[ \t]*$" input)
4569                         number
4570                       input)))
4571                  ((and (> scored marked) (< scored number)
4572                        (> (- scored number) 20))
4573                   (let ((input
4574                          (read-string
4575                           (format "%s %s (%d scored, %d total): "
4576                                   "How many articles from"
4577                                   group scored number))))
4578                     (if (string-match "^[ \t]*$" input)
4579                         number input)))
4580                  (t number))
4581               (quit
4582                (message "Quit getting the articles to read")
4583                nil))))))
4584     (setq select (if (stringp select) (string-to-number select) select))
4585     (if (or (null select) (zerop select))
4586         select
4587       (if (and (not (zerop scored)) (<= (abs select) scored))
4588           (progn
4589             (setq articles (sort scored-list '<))
4590             (setq number (length articles)))
4591         (setq articles (copy-sequence articles)))
4592
4593       (when (< (abs select) number)
4594         (if (< select 0)
4595             ;; Select the N oldest articles.
4596             (setcdr (nthcdr (1- (abs select)) articles) nil)
4597           ;; Select the N most recent articles.
4598           (setq articles (nthcdr (- number select) articles))))
4599       (setq gnus-newsgroup-unselected
4600             (gnus-sorted-intersection
4601              gnus-newsgroup-unreads
4602              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4603       (when gnus-alter-articles-to-read-function
4604         (setq gnus-newsgroup-unreads
4605               (sort
4606                (funcall gnus-alter-articles-to-read-function
4607                         gnus-newsgroup-name gnus-newsgroup-unreads)
4608                '<)))
4609       articles)))
4610
4611 (defun gnus-killed-articles (killed articles)
4612   (let (out)
4613     (while articles
4614       (when (inline (gnus-member-of-range (car articles) killed))
4615         (push (car articles) out))
4616       (setq articles (cdr articles)))
4617     out))
4618
4619 (defun gnus-uncompress-marks (marks)
4620   "Uncompress the mark ranges in MARKS."
4621   (let ((uncompressed '(score bookmark))
4622         out)
4623     (while marks
4624       (if (memq (caar marks) uncompressed)
4625           (push (car marks) out)
4626         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4627       (setq marks (cdr marks)))
4628     out))
4629
4630 (defun gnus-adjust-marked-articles (info)
4631   "Set all article lists and remove all marks that are no longer valid."
4632   (let* ((marked-lists (gnus-info-marks info))
4633          (active (gnus-active (gnus-info-group info)))
4634          (min (car active))
4635          (max (cdr active))
4636          (types gnus-article-mark-lists)
4637          (uncompressed '(score bookmark killed))
4638          marks var articles article mark)
4639
4640     (while marked-lists
4641       (setq marks (pop marked-lists))
4642       (set (setq var (intern (format "gnus-newsgroup-%s"
4643                                      (car (rassq (setq mark (car marks))
4644                                                  types)))))
4645            (if (memq (car marks) uncompressed) (cdr marks)
4646              (gnus-uncompress-range (cdr marks))))
4647
4648       (setq articles (symbol-value var))
4649
4650       ;; All articles have to be subsets of the active articles.
4651       (cond
4652        ;; Adjust "simple" lists.
4653        ((memq mark '(tick dormant expire reply save))
4654         (while articles
4655           (when (or (< (setq article (pop articles)) min) (> article max))
4656             (set var (delq article (symbol-value var))))))
4657        ;; Adjust assocs.
4658        ((memq mark uncompressed)
4659         (when (not (listp (cdr (symbol-value var))))
4660           (set var (list (symbol-value var))))
4661         (when (not (listp (cdr articles)))
4662           (setq articles (list articles)))
4663         (while articles
4664           (when (or (not (consp (setq article (pop articles))))
4665                     (< (car article) min)
4666                     (> (car article) max))
4667             (set var (delq article (symbol-value var))))))))))
4668
4669 (defun gnus-update-missing-marks (missing)
4670   "Go through the list of MISSING articles and remove them from the mark lists."
4671   (when missing
4672     (let ((types gnus-article-mark-lists)
4673           var m)
4674       ;; Go through all types.
4675       (while types
4676         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4677         (when (symbol-value var)
4678           ;; This list has articles.  So we delete all missing articles
4679           ;; from it.
4680           (setq m missing)
4681           (while m
4682             (set var (delq (pop m) (symbol-value var)))))))))
4683
4684 (defun gnus-update-marks ()
4685   "Enter the various lists of marked articles into the newsgroup info list."
4686   (let ((types gnus-article-mark-lists)
4687         (info (gnus-get-info gnus-newsgroup-name))
4688         (uncompressed '(score bookmark killed))
4689         type list newmarked symbol delta-marks)
4690     (when info
4691       ;; Add all marks lists to the list of marks lists.
4692       (while (setq type (pop types))
4693         (setq list (symbol-value
4694                     (setq symbol
4695                           (intern (format "gnus-newsgroup-%s"
4696                                           (car type))))))
4697
4698         (when list
4699           ;; Get rid of the entries of the articles that have the
4700           ;; default score.
4701           (when (and (eq (cdr type) 'score)
4702                      gnus-save-score
4703                      list)
4704             (let* ((arts list)
4705                    (prev (cons nil list))
4706                    (all prev))
4707               (while arts
4708                 (if (or (not (consp (car arts)))
4709                         (= (cdar arts) gnus-summary-default-score))
4710                     (setcdr prev (cdr arts))
4711                   (setq prev arts))
4712                 (setq arts (cdr arts)))
4713               (setq list (cdr all)))))
4714
4715         (unless (memq (cdr type) uncompressed)
4716           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4717
4718         (when (gnus-check-backend-function
4719                'request-set-mark gnus-newsgroup-name)
4720           ;; propagate flags to server, with the following exceptions:
4721           ;; uncompressed:s are not proper flags (they are cons cells)
4722           ;; cache is a internal gnus flag
4723           ;; download are local to one gnus installation (well)
4724           ;; unsend are for nndraft groups only
4725           ;; xxx: generality of this?  this suits nnimap anyway
4726           (unless (memq (cdr type) (append '(cache download unsend)
4727                                            uncompressed))
4728             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4729                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4730                    (add (gnus-remove-from-range
4731                          (gnus-copy-sequence list) old)))
4732               (when add
4733                 (push (list add 'add (list (cdr type))) delta-marks))
4734               (when del
4735                 (push (list del 'del (list (cdr type))) delta-marks)))))
4736
4737         (when list
4738           (push (cons (cdr type) list) newmarked)))
4739
4740       (when delta-marks
4741         (unless (gnus-check-group gnus-newsgroup-name)
4742           (error "Can't open server for %s" gnus-newsgroup-name))
4743         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4744
4745       ;; Enter these new marks into the info of the group.
4746       (if (nthcdr 3 info)
4747           (setcar (nthcdr 3 info) newmarked)
4748         ;; Add the marks lists to the end of the info.
4749         (when newmarked
4750           (setcdr (nthcdr 2 info) (list newmarked))))
4751
4752       ;; Cut off the end of the info if there's nothing else there.
4753       (let ((i 5))
4754         (while (and (> i 2)
4755                     (not (nth i info)))
4756           (when (nthcdr (decf i) info)
4757             (setcdr (nthcdr i info) nil)))))))
4758
4759 (defun gnus-set-mode-line (where)
4760   "Set the mode line of the article or summary buffers.
4761 If WHERE is `summary', the summary mode line format will be used."
4762   ;; Is this mode line one we keep updated?
4763   (when (and (memq where gnus-updated-mode-lines)
4764              (symbol-value
4765               (intern (format "gnus-%s-mode-line-format-spec" where))))
4766     (let (mode-string)
4767       (save-excursion
4768         ;; We evaluate this in the summary buffer since these
4769         ;; variables are buffer-local to that buffer.
4770         (set-buffer gnus-summary-buffer)
4771         ;; We bind all these variables that are used in the `eval' form
4772         ;; below.
4773         (let* ((mformat (symbol-value
4774                          (intern
4775                           (format "gnus-%s-mode-line-format-spec" where))))
4776                (gnus-tmp-group-name (gnus-group-decoded-name 
4777                                      gnus-newsgroup-name))
4778                (gnus-tmp-article-number (or gnus-current-article 0))
4779                (gnus-tmp-unread gnus-newsgroup-unreads)
4780                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4781                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4782                (gnus-tmp-unread-and-unselected
4783                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4784                             (zerop gnus-tmp-unselected))
4785                        "")
4786                       ((zerop gnus-tmp-unselected)
4787                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4788                       (t (format "{%d(+%d) more}"
4789                                  gnus-tmp-unread-and-unticked
4790                                  gnus-tmp-unselected))))
4791                (gnus-tmp-subject
4792                 (if (and gnus-current-headers
4793                          (vectorp gnus-current-headers))
4794                     (gnus-mode-string-quote
4795                      (mail-header-subject gnus-current-headers))
4796                   ""))
4797                bufname-length max-len
4798                gnus-tmp-header);; passed as argument to any user-format-funcs
4799           (setq mode-string (eval mformat))
4800           (setq bufname-length (if (string-match "%b" mode-string)
4801                                    (- (length
4802                                        (buffer-name
4803                                         (if (eq where 'summary)
4804                                             nil
4805                                           (get-buffer gnus-article-buffer))))
4806                                       2)
4807                                  0))
4808           (setq max-len (max 4 (if gnus-mode-non-string-length
4809                                    (- (window-width)
4810                                       gnus-mode-non-string-length
4811                                       bufname-length)
4812                                  (length mode-string))))
4813           ;; We might have to chop a bit of the string off...
4814           (when (> (length mode-string) max-len)
4815             (setq mode-string
4816                   (concat (gnus-truncate-string mode-string (- max-len 3))
4817                           "...")))
4818           ;; Pad the mode string a bit.
4819           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4820       ;; Update the mode line.
4821       (setq mode-line-buffer-identification
4822             (gnus-mode-line-buffer-identification (list mode-string)))
4823       (set-buffer-modified-p t))))
4824
4825 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4826   "Go through the HEADERS list and add all Xrefs to a hash table.
4827 The resulting hash table is returned, or nil if no Xrefs were found."
4828   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4829          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4830          (xref-hashtb (gnus-make-hashtable))
4831          start group entry number xrefs header)
4832     (while headers
4833       (setq header (pop headers))
4834       (when (and (setq xrefs (mail-header-xref header))
4835                  (not (memq (setq number (mail-header-number header))
4836                             unreads)))
4837         (setq start 0)
4838         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4839           (setq start (match-end 0))
4840           (setq group (if prefix
4841                           (concat prefix (substring xrefs (match-beginning 1)
4842                                                     (match-end 1)))
4843                         (substring xrefs (match-beginning 1) (match-end 1))))
4844           (setq number
4845                 (string-to-int (substring xrefs (match-beginning 2)
4846                                           (match-end 2))))
4847           (if (setq entry (gnus-gethash group xref-hashtb))
4848               (setcdr entry (cons number (cdr entry)))
4849             (gnus-sethash group (cons number nil) xref-hashtb)))))
4850     (and start xref-hashtb)))
4851
4852 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4853   "Look through all the headers and mark the Xrefs as read."
4854   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4855         name entry info xref-hashtb idlist method nth4)
4856     (save-excursion
4857       (set-buffer gnus-group-buffer)
4858       (when (setq xref-hashtb
4859                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4860         (mapatoms
4861          (lambda (group)
4862            (unless (string= from-newsgroup (setq name (symbol-name group)))
4863              (setq idlist (symbol-value group))
4864              ;; Dead groups are not updated.
4865              (and (prog1
4866                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4867                             info (nth 2 entry))
4868                     (when (stringp (setq nth4 (gnus-info-method info)))
4869                       (setq nth4 (gnus-server-to-method nth4))))
4870                   ;; Only do the xrefs if the group has the same
4871                   ;; select method as the group we have just read.
4872                   (or (gnus-methods-equal-p
4873                        nth4 (gnus-find-method-for-group from-newsgroup))
4874                       virtual
4875                       (equal nth4 (setq method (gnus-find-method-for-group
4876                                                 from-newsgroup)))
4877                       (and (equal (car nth4) (car method))
4878                            (equal (nth 1 nth4) (nth 1 method))))
4879                   gnus-use-cross-reference
4880                   (or (not (eq gnus-use-cross-reference t))
4881                       virtual
4882                       ;; Only do cross-references on subscribed
4883                       ;; groups, if that is what is wanted.
4884                       (<= (gnus-info-level info) gnus-level-subscribed))
4885                   (gnus-group-make-articles-read name idlist))))
4886          xref-hashtb)))))
4887
4888 (defun gnus-compute-read-articles (group articles)
4889   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4890          (info (nth 2 entry))
4891          (active (gnus-active group))
4892          ninfo)
4893     (when entry
4894       ;; First peel off all invalid article numbers.
4895       (when active
4896         (let ((ids articles)
4897               id first)
4898           (while (setq id (pop ids))
4899             (when (and first (> id (cdr active)))
4900               ;; We'll end up in this situation in one particular
4901               ;; obscure situation.  If you re-scan a group and get
4902               ;; a new article that is cross-posted to a different
4903               ;; group that has not been re-scanned, you might get
4904               ;; crossposted article that has a higher number than
4905               ;; Gnus believes possible.  So we re-activate this
4906               ;; group as well.  This might mean doing the
4907               ;; crossposting thingy will *increase* the number
4908               ;; of articles in some groups.  Tsk, tsk.
4909               (setq active (or (gnus-activate-group group) active)))
4910             (when (or (> id (cdr active))
4911                       (< id (car active)))
4912               (setq articles (delq id articles))))))
4913       ;; If the read list is nil, we init it.
4914       (if (and active
4915                (null (gnus-info-read info))
4916                (> (car active) 1))
4917           (setq ninfo (cons 1 (1- (car active))))
4918         (setq ninfo (gnus-info-read info)))
4919       ;; Then we add the read articles to the range.
4920       (gnus-add-to-range
4921        ninfo (setq articles (sort articles '<))))))
4922
4923 (defun gnus-group-make-articles-read (group articles)
4924   "Update the info of GROUP to say that ARTICLES are read."
4925   (let* ((num 0)
4926          (entry (gnus-gethash group gnus-newsrc-hashtb))
4927          (info (nth 2 entry))
4928          (active (gnus-active group))
4929          range)
4930     (when entry
4931       (setq range (gnus-compute-read-articles group articles))
4932       (save-excursion
4933         (set-buffer gnus-group-buffer)
4934         (gnus-undo-register
4935           `(progn
4936              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4937              (gnus-info-set-read ',info ',(gnus-info-read info))
4938              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4939              (gnus-group-update-group ,group t))))
4940       ;; Add the read articles to the range.
4941       (gnus-info-set-read info range)
4942       ;; Then we have to re-compute how many unread
4943       ;; articles there are in this group.
4944       (when active
4945         (cond
4946          ((not range)
4947           (setq num (- (1+ (cdr active)) (car active))))
4948          ((not (listp (cdr range)))
4949           (setq num (- (cdr active) (- (1+ (cdr range))
4950                                        (car range)))))
4951          (t
4952           (while range
4953             (if (numberp (car range))
4954                 (setq num (1+ num))
4955               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4956             (setq range (cdr range)))
4957           (setq num (- (cdr active) num))))
4958         ;; Update the number of unread articles.
4959         (setcar entry num)
4960         ;; Update the group buffer.
4961         (gnus-group-update-group group t)))))
4962
4963 (defvar gnus-newsgroup-none-id 0)
4964
4965 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4966   (let ((cur nntp-server-buffer)
4967         (dependencies
4968          (or dependencies
4969              (save-excursion (set-buffer gnus-summary-buffer)
4970                              gnus-newsgroup-dependencies)))
4971         headers id end ref
4972         (mail-parse-charset gnus-newsgroup-charset)
4973         (mail-parse-ignored-charsets
4974          (save-excursion (condition-case nil
4975                              (set-buffer gnus-summary-buffer)
4976                            (error))
4977                          gnus-newsgroup-ignored-charsets)))
4978     (save-excursion
4979       (set-buffer nntp-server-buffer)
4980       ;; Translate all TAB characters into SPACE characters.
4981       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4982       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4983       (gnus-run-hooks 'gnus-parse-headers-hook)
4984       (let ((case-fold-search t)
4985             in-reply-to header p lines chars ctype)
4986         (goto-char (point-min))
4987         ;; Search to the beginning of the next header.  Error messages
4988         ;; do not begin with 2 or 3.
4989         (while (re-search-forward "^[23][0-9]+ " nil t)
4990           (setq id nil
4991                 ref nil)
4992           ;; This implementation of this function, with nine
4993           ;; search-forwards instead of the one re-search-forward and
4994           ;; a case (which basically was the old function) is actually
4995           ;; about twice as fast, even though it looks messier.  You
4996           ;; can't have everything, I guess.  Speed and elegance
4997           ;; doesn't always go hand in hand.
4998           (setq
4999            header
5000            (make-full-mail-header
5001             ;; Number.
5002             (prog1
5003                 (read cur)
5004               (end-of-line)
5005               (setq p (point))
5006               (narrow-to-region (point)
5007                                 (or (and (search-forward "\n.\n" nil t)
5008                                          (- (point) 2))
5009                                     (point))))
5010             ;; Subject.
5011             (progn
5012               (goto-char p)
5013               (if (search-forward "\nsubject: " nil t)
5014                   (nnheader-header-value)
5015                 "(none)"))
5016             ;; From.
5017             (progn
5018               (goto-char p)
5019               (if (or (search-forward "\nfrom: " nil t)
5020                       (search-forward "\nfrom:" nil t))
5021                   (nnheader-header-value)
5022                 "(nobody)"))
5023             ;; Date.
5024             (progn
5025               (goto-char p)
5026               (if (search-forward "\ndate: " nil t)
5027                   (nnheader-header-value)
5028                 ""))
5029             ;; Message-ID.
5030             (progn
5031               (goto-char p)
5032               (setq id (if (re-search-forward
5033                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5034                            ;; We do it this way to make sure the Message-ID
5035                            ;; is (somewhat) syntactically valid.
5036                            (buffer-substring (match-beginning 1)
5037                                              (match-end 1))
5038                          ;; If there was no message-id, we just fake one
5039                          ;; to make subsequent routines simpler.
5040                          (nnheader-generate-fake-message-id))))
5041             ;; References.
5042             (progn
5043               (goto-char p)
5044               (if (search-forward "\nreferences: " nil t)
5045                   (progn
5046                     (setq end (point))
5047                     (prog1
5048                         (nnheader-header-value)
5049                       (setq ref
5050                             (buffer-substring
5051                              (progn
5052                                ;; (end-of-line)
5053                                (search-backward ">" end t)
5054                                (1+ (point)))
5055                              (progn
5056                                (search-backward "<" end t)
5057                                (point))))))
5058                 ;; Get the references from the in-reply-to header if there
5059                 ;; were no references and the in-reply-to header looks
5060                 ;; promising.
5061                 (if (and (search-forward "\nin-reply-to: " nil t)
5062                          (setq in-reply-to (nnheader-header-value))
5063                          (string-match "<[^>]+>" in-reply-to))
5064                     (let (ref2)
5065                       (setq ref (substring in-reply-to (match-beginning 0)
5066                                            (match-end 0)))
5067                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5068                         (setq ref2 (substring in-reply-to (match-beginning 0)
5069                                               (match-end 0)))
5070                         (when (> (length ref2) (length ref))
5071                           (setq ref ref2)))
5072                       ref)
5073                   (setq ref nil))))
5074             ;; Chars.
5075             (progn
5076               (goto-char p)
5077               (if (search-forward "\nchars: " nil t)
5078                   (if (numberp (setq chars (ignore-errors (read cur))))
5079                       chars -1)
5080                 -1))
5081             ;; Lines.
5082             (progn
5083               (goto-char p)
5084               (if (search-forward "\nlines: " nil t)
5085                   (if (numberp (setq lines (ignore-errors (read cur))))
5086                       lines -1)
5087                 -1))
5088             ;; Xref.
5089             (progn
5090               (goto-char p)
5091               (and (search-forward "\nxref: " nil t)
5092                    (nnheader-header-value)))
5093             ;; Extra.
5094             (when gnus-extra-headers
5095               (let ((extra gnus-extra-headers)
5096                     out)
5097                 (while extra
5098                   (goto-char p)
5099                   (when (search-forward
5100                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
5101                     (push (cons (car extra) (nnheader-header-value)) out))
5102                   (pop extra))
5103                 out))))
5104           (goto-char p)
5105           (if (and (search-forward "\ncontent-type: " nil t)
5106                    (setq ctype (nnheader-header-value)))
5107               (mime-entity-set-content-type-internal
5108                header (mime-parse-Content-Type ctype)))
5109           (when (equal id ref)
5110             (setq ref nil))
5111
5112           (when gnus-alter-header-function
5113             (funcall gnus-alter-header-function header)
5114             (setq id (mail-header-id header)
5115                   ref (gnus-parent-id (mail-header-references header))))
5116
5117           (when (setq header
5118                       (gnus-dependencies-add-header
5119                        header dependencies force-new))
5120             (push header headers))
5121           (goto-char (point-max))
5122           (widen))
5123         (nreverse headers)))))
5124
5125 ;; Goes through the xover lines and returns a list of vectors
5126 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5127                                                   force-new dependencies
5128                                                   group also-fetch-heads)
5129   "Parse the news overview data in the server buffer.
5130 Return a list of headers that match SEQUENCE (see
5131 `nntp-retrieve-headers')."
5132   ;; Get the Xref when the users reads the articles since most/some
5133   ;; NNTP servers do not include Xrefs when using XOVER.
5134   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5135   (let ((mail-parse-charset gnus-newsgroup-charset)
5136         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5137         (cur nntp-server-buffer)
5138         (dependencies (or dependencies gnus-newsgroup-dependencies))
5139         (allp (cond
5140                ((eq gnus-read-all-available-headers t)
5141                 t)
5142                ((stringp gnus-read-all-available-headers)
5143                 (string-match gnus-read-all-available-headers group))
5144                (t
5145                 nil)))
5146         number headers header)
5147     (save-excursion
5148       (set-buffer nntp-server-buffer)
5149       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5150       ;; Allow the user to mangle the headers before parsing them.
5151       (gnus-run-hooks 'gnus-parse-headers-hook)
5152       (goto-char (point-min))
5153       (while (not (eobp))
5154         (condition-case ()
5155             (while (and (or sequence allp)
5156                         (not (eobp)))
5157               (setq number (read cur))
5158               (when (not allp)
5159                 (while (and sequence
5160                             (< (car sequence) number))
5161                   (setq sequence (cdr sequence))))
5162               (when (and (or allp
5163                              (and sequence
5164                                   (eq number (car sequence))))
5165                          (progn
5166                            (setq sequence (cdr sequence))
5167                            (setq header (inline
5168                                           (gnus-nov-parse-line
5169                                            number dependencies force-new)))))
5170                 (push header headers))
5171               (forward-line 1))
5172           (error
5173            (gnus-error 4 "Strange nov line (%d)"
5174                        (count-lines (point-min) (point)))))
5175         (forward-line 1))
5176       ;; A common bug in inn is that if you have posted an article and
5177       ;; then retrieves the active file, it will answer correctly --
5178       ;; the new article is included.  However, a NOV entry for the
5179       ;; article may not have been generated yet, so this may fail.
5180       ;; We work around this problem by retrieving the last few
5181       ;; headers using HEAD.
5182       (if (or (not also-fetch-heads)
5183               (not sequence))
5184           ;; We (probably) got all the headers.
5185           (nreverse headers)
5186         (let ((gnus-nov-is-evil t))
5187           (nconc
5188            (nreverse headers)
5189            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5190              (gnus-get-newsgroup-headers))))))))
5191
5192 (defun gnus-article-get-xrefs ()
5193   "Fill in the Xref value in `gnus-current-headers', if necessary.
5194 This is meant to be called in `gnus-article-internal-prepare-hook'."
5195   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5196                                  gnus-current-headers)))
5197     (or (not gnus-use-cross-reference)
5198         (not headers)
5199         (and (mail-header-xref headers)
5200              (not (string= (mail-header-xref headers) "")))
5201         (let ((case-fold-search t)
5202               xref)
5203           (save-restriction
5204             (nnheader-narrow-to-headers)
5205             (goto-char (point-min))
5206             (when (or (and (not (eobp))
5207                            (eq (downcase (char-after)) ?x)
5208                            (looking-at "Xref:"))
5209                       (search-forward "\nXref:" nil t))
5210               (goto-char (1+ (match-end 0)))
5211               (setq xref (buffer-substring (point)
5212                                            (progn (end-of-line) (point))))
5213               (mail-header-set-xref headers xref)))))))
5214
5215 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5216   "Find article ID and insert the summary line for that article.
5217 OLD-HEADER can either be a header or a line number to insert
5218 the subject line on."
5219   (let* ((line (and (numberp old-header) old-header))
5220          (old-header (and (vectorp old-header) old-header))
5221          (header (cond ((and old-header use-old-header)
5222                         old-header)
5223                        ((and (numberp id)
5224                              (gnus-number-to-header id))
5225                         (gnus-number-to-header id))
5226                        (t
5227                         (gnus-read-header id))))
5228          (number (and (numberp id) id))
5229          d)
5230     (when header
5231       ;; Rebuild the thread that this article is part of and go to the
5232       ;; article we have fetched.
5233       (when (and (not gnus-show-threads)
5234                  old-header)
5235         (when (and number
5236                    (setq d (gnus-data-find (mail-header-number old-header))))
5237           (goto-char (gnus-data-pos d))
5238           (gnus-data-remove
5239            number
5240            (- (gnus-point-at-bol)
5241               (prog1
5242                   (1+ (gnus-point-at-eol))
5243                 (gnus-delete-line))))))
5244       (when old-header
5245         (mail-header-set-number header (mail-header-number old-header)))
5246       (setq gnus-newsgroup-sparse
5247             (delq (setq number (mail-header-number header))
5248                   gnus-newsgroup-sparse))
5249       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5250       (push number gnus-newsgroup-limit)
5251       (gnus-rebuild-thread (mail-header-id header) line)
5252       (gnus-summary-goto-subject number nil t))
5253     (when (and (numberp number)
5254                (> number 0))
5255       ;; We have to update the boundaries even if we can't fetch the
5256       ;; article if ID is a number -- so that the next `P' or `N'
5257       ;; command will fetch the previous (or next) article even
5258       ;; if the one we tried to fetch this time has been canceled.
5259       (when (> number gnus-newsgroup-end)
5260         (setq gnus-newsgroup-end number))
5261       (when (< number gnus-newsgroup-begin)
5262         (setq gnus-newsgroup-begin number))
5263       (setq gnus-newsgroup-unselected
5264             (delq number gnus-newsgroup-unselected)))
5265     ;; Report back a success?
5266     (and header (mail-header-number header))))
5267
5268 ;;; Process/prefix in the summary buffer
5269
5270 (defun gnus-summary-work-articles (n)
5271   "Return a list of articles to be worked upon.
5272 The prefix argument, the list of process marked articles, and the
5273 current article will be taken into consideration."
5274   (save-excursion
5275     (set-buffer gnus-summary-buffer)
5276     (cond
5277      (n
5278       ;; A numerical prefix has been given.
5279       (setq n (prefix-numeric-value n))
5280       (let ((backward (< n 0))
5281             (n (abs (prefix-numeric-value n)))
5282             articles article)
5283         (save-excursion
5284           (while
5285               (and (> n 0)
5286                    (push (setq article (gnus-summary-article-number))
5287                          articles)
5288                    (if backward
5289                        (gnus-summary-find-prev nil article)
5290                      (gnus-summary-find-next nil article)))
5291             (decf n)))
5292         (nreverse articles)))
5293      ((and (gnus-region-active-p) (mark))
5294       (message "region active")
5295       ;; Work on the region between point and mark.
5296       (let ((max (max (point) (mark)))
5297             articles article)
5298         (save-excursion
5299           (goto-char (min (point) (mark)))
5300           (while
5301               (and
5302                (push (setq article (gnus-summary-article-number)) articles)
5303                (gnus-summary-find-next nil article)
5304                (< (point) max)))
5305           (nreverse articles))))
5306      (gnus-newsgroup-processable
5307       ;; There are process-marked articles present.
5308       ;; Save current state.
5309       (gnus-summary-save-process-mark)
5310       ;; Return the list.
5311       (reverse gnus-newsgroup-processable))
5312      (t
5313       ;; Just return the current article.
5314       (list (gnus-summary-article-number))))))
5315
5316 (defmacro gnus-summary-iterate (arg &rest forms)
5317   "Iterate over the process/prefixed articles and do FORMS.
5318 ARG is the interactive prefix given to the command.  FORMS will be
5319 executed with point over the summary line of the articles."
5320   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5321     `(let ((,articles (gnus-summary-work-articles ,arg)))
5322        (while ,articles
5323          (gnus-summary-goto-subject (car ,articles))
5324          ,@forms
5325          (pop ,articles)))))
5326
5327 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5328 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5329
5330 (defun gnus-summary-save-process-mark ()
5331   "Push the current set of process marked articles on the stack."
5332   (interactive)
5333   (push (copy-sequence gnus-newsgroup-processable)
5334         gnus-newsgroup-process-stack))
5335
5336 (defun gnus-summary-kill-process-mark ()
5337   "Push the current set of process marked articles on the stack and unmark."
5338   (interactive)
5339   (gnus-summary-save-process-mark)
5340   (gnus-summary-unmark-all-processable))
5341
5342 (defun gnus-summary-yank-process-mark ()
5343   "Pop the last process mark state off the stack and restore it."
5344   (interactive)
5345   (unless gnus-newsgroup-process-stack
5346     (error "Empty mark stack"))
5347   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5348
5349 (defun gnus-summary-process-mark-set (set)
5350   "Make SET into the current process marked articles."
5351   (gnus-summary-unmark-all-processable)
5352   (while set
5353     (gnus-summary-set-process-mark (pop set))))
5354
5355 ;;; Searching and stuff
5356
5357 (defun gnus-summary-search-group (&optional backward use-level)
5358   "Search for next unread newsgroup.
5359 If optional argument BACKWARD is non-nil, search backward instead."
5360   (save-excursion
5361     (set-buffer gnus-group-buffer)
5362     (when (gnus-group-search-forward
5363            backward nil (if use-level (gnus-group-group-level) nil))
5364       (gnus-group-group-name))))
5365
5366 (defun gnus-summary-best-group (&optional exclude-group)
5367   "Find the name of the best unread group.
5368 If EXCLUDE-GROUP, do not go to this group."
5369   (save-excursion
5370     (set-buffer gnus-group-buffer)
5371     (save-excursion
5372       (gnus-group-best-unread-group exclude-group))))
5373
5374 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5375   (if backward (gnus-summary-find-prev)
5376     (let* ((dummy (gnus-summary-article-intangible-p))
5377            (article (or article (gnus-summary-article-number)))
5378            (arts (gnus-data-find-list article))
5379            result)
5380       (when (and (not dummy)
5381                  (or (not gnus-summary-check-current)
5382                      (not unread)
5383                      (not (gnus-data-unread-p (car arts)))))
5384         (setq arts (cdr arts)))
5385       (when (setq result
5386                   (if unread
5387                       (progn
5388                         (while arts
5389                           (when (or (and undownloaded
5390                                          (eq gnus-undownloaded-mark
5391                                              (gnus-data-mark (car arts))))
5392                                     (gnus-data-unread-p (car arts)))
5393                             (setq result (car arts)
5394                                   arts nil))
5395                           (setq arts (cdr arts)))
5396                         result)
5397                     (car arts)))
5398         (goto-char (gnus-data-pos result))
5399         (gnus-data-number result)))))
5400
5401 (defun gnus-summary-find-prev (&optional unread article)
5402   (let* ((eobp (eobp))
5403          (article (or article (gnus-summary-article-number)))
5404          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5405          result)
5406     (when (and (not eobp)
5407                (or (not gnus-summary-check-current)
5408                    (not unread)
5409                    (not (gnus-data-unread-p (car arts)))))
5410       (setq arts (cdr arts)))
5411     (when (setq result
5412                 (if unread
5413                     (progn
5414                       (while arts
5415                         (when (gnus-data-unread-p (car arts))
5416                           (setq result (car arts)
5417                                 arts nil))
5418                         (setq arts (cdr arts)))
5419                       result)
5420                   (car arts)))
5421       (goto-char (gnus-data-pos result))
5422       (gnus-data-number result))))
5423
5424 (defun gnus-summary-find-subject (subject &optional unread backward article)
5425   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5426          (article (or article (gnus-summary-article-number)))
5427          (articles (gnus-data-list backward))
5428          (arts (gnus-data-find-list article articles))
5429          result)
5430     (when (or (not gnus-summary-check-current)
5431               (not unread)
5432               (not (gnus-data-unread-p (car arts))))
5433       (setq arts (cdr arts)))
5434     (while arts
5435       (and (or (not unread)
5436                (gnus-data-unread-p (car arts)))
5437            (vectorp (gnus-data-header (car arts)))
5438            (gnus-subject-equal
5439             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5440            (setq result (car arts)
5441                  arts nil))
5442       (setq arts (cdr arts)))
5443     (and result
5444          (goto-char (gnus-data-pos result))
5445          (gnus-data-number result))))
5446
5447 (defun gnus-summary-search-forward (&optional unread subject backward)
5448   "Search forward for an article.
5449 If UNREAD, look for unread articles.  If SUBJECT, look for
5450 articles with that subject.  If BACKWARD, search backward instead."
5451   (cond (subject (gnus-summary-find-subject subject unread backward))
5452         (backward (gnus-summary-find-prev unread))
5453         (t (gnus-summary-find-next unread))))
5454
5455 (defun gnus-recenter (&optional n)
5456   "Center point in window and redisplay frame.
5457 Also do horizontal recentering."
5458   (interactive "P")
5459   (when (and gnus-auto-center-summary
5460              (not (eq gnus-auto-center-summary 'vertical)))
5461     (gnus-horizontal-recenter))
5462   (recenter n))
5463
5464 (defun gnus-summary-recenter ()
5465   "Center point in the summary window.
5466 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5467 displayed, no centering will be performed."
5468   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5469   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5470   (interactive)
5471   (let* ((top (cond ((< (window-height) 4) 0)
5472                     ((< (window-height) 7) 1)
5473                     (t (if (numberp gnus-auto-center-summary)
5474                            gnus-auto-center-summary
5475                          2))))
5476          (height (1- (window-height)))
5477          (bottom (save-excursion (goto-char (point-max))
5478                                  (forward-line (- height))
5479                                  (point)))
5480          (window (get-buffer-window (current-buffer))))
5481     ;; The user has to want it.
5482     (when gnus-auto-center-summary
5483       (when (get-buffer-window gnus-article-buffer)
5484         ;; Only do recentering when the article buffer is displayed,
5485         ;; Set the window start to either `bottom', which is the biggest
5486         ;; possible valid number, or the second line from the top,
5487         ;; whichever is the least.
5488         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5489           (if (> bottom top-pos)
5490               ;; Keep the second line from the top visible
5491               (set-window-start window top-pos t)
5492             ;; Try to keep the bottom line visible; if it's partially
5493             ;; obscured, either scroll one more line to make it fully
5494             ;; visible, or revert to using TOP-POS.
5495             (save-excursion
5496               (goto-char (point-max))
5497               (forward-line -1)
5498               (let ((last-line-start (point)))
5499                 (goto-char bottom)
5500                 (set-window-start window (point) t)
5501                 (when (not (pos-visible-in-window-p last-line-start window))
5502                   (forward-line 1)
5503                   (set-window-start window (min (point) top-pos) t)))))))
5504       ;; Do horizontal recentering while we're at it.
5505       (when (and (get-buffer-window (current-buffer) t)
5506                  (not (eq gnus-auto-center-summary 'vertical)))
5507         (let ((selected (selected-window)))
5508           (select-window (get-buffer-window (current-buffer) t))
5509           (gnus-summary-position-point)
5510           (gnus-horizontal-recenter)
5511           (select-window selected))))))
5512
5513 (defun gnus-summary-jump-to-group (newsgroup)
5514   "Move point to NEWSGROUP in group mode buffer."
5515   ;; Keep update point of group mode buffer if visible.
5516   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5517       (save-window-excursion
5518         ;; Take care of tree window mode.
5519         (when (get-buffer-window gnus-group-buffer)
5520           (pop-to-buffer gnus-group-buffer))
5521         (gnus-group-jump-to-group newsgroup))
5522     (save-excursion
5523       ;; Take care of tree window mode.
5524       (if (get-buffer-window gnus-group-buffer)
5525           (pop-to-buffer gnus-group-buffer)
5526         (set-buffer gnus-group-buffer))
5527       (gnus-group-jump-to-group newsgroup))))
5528
5529 ;; This function returns a list of article numbers based on the
5530 ;; difference between the ranges of read articles in this group and
5531 ;; the range of active articles.
5532 (defun gnus-list-of-unread-articles (group)
5533   (let* ((read (gnus-info-read (gnus-get-info group)))
5534          (active (or (gnus-active group) (gnus-activate-group group)))
5535          (last (cdr active))
5536          first nlast unread)
5537     ;; If none are read, then all are unread.
5538     (if (not read)
5539         (setq first (car active))
5540       ;; If the range of read articles is a single range, then the
5541       ;; first unread article is the article after the last read
5542       ;; article.  Sounds logical, doesn't it?
5543       (if (and (not (listp (cdr read)))
5544                (or (< (car read) (car active))
5545                    (progn (setq read (list read))
5546                           nil)))
5547           (setq first (max (car active) (1+ (cdr read))))
5548         ;; `read' is a list of ranges.
5549         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5550                                   (caar read)))
5551                   1)
5552           (setq first (car active)))
5553         (while read
5554           (when first
5555             (while (< first nlast)
5556               (push first unread)
5557               (setq first (1+ first))))
5558           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5559           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5560           (setq read (cdr read)))))
5561     ;; And add the last unread articles.
5562     (while (<= first last)
5563       (push first unread)
5564       (setq first (1+ first)))
5565     ;; Return the list of unread articles.
5566     (delq 0 (nreverse unread))))
5567
5568 (defun gnus-list-of-read-articles (group)
5569   "Return a list of unread, unticked and non-dormant articles."
5570   (let* ((info (gnus-get-info group))
5571          (marked (gnus-info-marks info))
5572          (active (gnus-active group)))
5573     (and info active
5574          (gnus-set-difference
5575           (gnus-sorted-complement
5576            (gnus-uncompress-range active)
5577            (gnus-list-of-unread-articles group))
5578           (append
5579            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5580            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5581
5582 ;; Various summary commands
5583
5584 (defun gnus-summary-select-article-buffer ()
5585   "Reconfigure windows to show article buffer."
5586   (interactive)
5587   (if (not (gnus-buffer-live-p gnus-article-buffer))
5588       (error "There is no article buffer for this summary buffer")
5589     (gnus-configure-windows 'article)
5590     (select-window (get-buffer-window gnus-article-buffer))))
5591
5592 (defun gnus-summary-universal-argument (arg)
5593   "Perform any operation on all articles that are process/prefixed."
5594   (interactive "P")
5595   (let ((articles (gnus-summary-work-articles arg))
5596         func article)
5597     (if (eq
5598          (setq
5599           func
5600           (key-binding
5601            (read-key-sequence
5602             (substitute-command-keys
5603              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5604          'undefined)
5605         (gnus-error 1 "Undefined key")
5606       (save-excursion
5607         (while articles
5608           (gnus-summary-goto-subject (setq article (pop articles)))
5609           (let (gnus-newsgroup-processable)
5610             (command-execute func))
5611           (gnus-summary-remove-process-mark article)))))
5612   (gnus-summary-position-point))
5613
5614 (defun gnus-summary-toggle-truncation (&optional arg)
5615   "Toggle truncation of summary lines.
5616 With arg, turn line truncation on iff arg is positive."
5617   (interactive "P")
5618   (setq truncate-lines
5619         (if (null arg) (not truncate-lines)
5620           (> (prefix-numeric-value arg) 0)))
5621   (redraw-display))
5622
5623 (defun gnus-summary-reselect-current-group (&optional all rescan)
5624   "Exit and then reselect the current newsgroup.
5625 The prefix argument ALL means to select all articles."
5626   (interactive "P")
5627   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5628     (error "Ephemeral groups can't be reselected"))
5629   (let ((current-subject (gnus-summary-article-number))
5630         (group gnus-newsgroup-name))
5631     (setq gnus-newsgroup-begin nil)
5632     (gnus-summary-exit)
5633     ;; We have to adjust the point of group mode buffer because
5634     ;; point was moved to the next unread newsgroup by exiting.
5635     (gnus-summary-jump-to-group group)
5636     (when rescan
5637       (save-excursion
5638         (save-window-excursion
5639           ;; Don't show group contents.
5640           (set-window-start (selected-window) (point-max))
5641           (gnus-group-get-new-news-this-group 1))))
5642     (gnus-group-read-group all t)
5643     (gnus-summary-goto-subject current-subject nil t)))
5644
5645 (defun gnus-summary-rescan-group (&optional all)
5646   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5647   (interactive "P")
5648   (gnus-summary-reselect-current-group all t))
5649
5650 (defun gnus-summary-update-info (&optional non-destructive)
5651   (save-excursion
5652     (let ((group gnus-newsgroup-name))
5653       (when group
5654         (when gnus-newsgroup-kill-headers
5655           (setq gnus-newsgroup-killed
5656                 (gnus-compress-sequence
5657                  (nconc
5658                   (gnus-set-sorted-intersection
5659                    (gnus-uncompress-range gnus-newsgroup-killed)
5660                    (setq gnus-newsgroup-unselected
5661                          (sort gnus-newsgroup-unselected '<)))
5662                   (setq gnus-newsgroup-unreads
5663                         (sort gnus-newsgroup-unreads '<)))
5664                  t)))
5665         (unless (listp (cdr gnus-newsgroup-killed))
5666           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5667         (let ((headers gnus-newsgroup-headers))
5668           ;; Set the new ranges of read articles.
5669           (save-excursion
5670             (set-buffer gnus-group-buffer)
5671             (gnus-undo-force-boundary))
5672           (gnus-update-read-articles
5673            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5674           ;; Set the current article marks.
5675           (let ((gnus-newsgroup-scored
5676                  (if (and (not gnus-save-score)
5677                           (not non-destructive))
5678                      nil
5679                    gnus-newsgroup-scored)))
5680             (save-excursion
5681               (gnus-update-marks)))
5682           ;; Do the cross-ref thing.
5683           (when gnus-use-cross-reference
5684             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5685           ;; Do not switch windows but change the buffer to work.
5686           (set-buffer gnus-group-buffer)
5687           (unless (gnus-ephemeral-group-p group)
5688             (gnus-group-update-group group)))))))
5689
5690 (defun gnus-summary-save-newsrc (&optional force)
5691   "Save the current number of read/marked articles in the dribble buffer.
5692 The dribble buffer will then be saved.
5693 If FORCE (the prefix), also save the .newsrc file(s)."
5694   (interactive "P")
5695   (gnus-summary-update-info t)
5696   (if force
5697       (gnus-save-newsrc-file)
5698     (gnus-dribble-save)))
5699
5700 (defun gnus-summary-exit (&optional temporary)
5701   "Exit reading current newsgroup, and then return to group selection mode.
5702 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5703   (interactive)
5704   (gnus-set-global-variables)
5705   (gnus-kill-save-kill-buffer)
5706   (gnus-async-halt-prefetch)
5707   (let* ((group gnus-newsgroup-name)
5708          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5709          (mode major-mode)
5710          (group-point nil)
5711          (buf (current-buffer)))
5712     (unless quit-config
5713       ;; Do adaptive scoring, and possibly save score files.
5714       (when gnus-newsgroup-adaptive
5715         (gnus-score-adaptive))
5716       (when gnus-use-scoring
5717         (gnus-score-save)))
5718     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5719     ;; If we have several article buffers, we kill them at exit.
5720     (unless gnus-single-article-buffer
5721       (gnus-kill-buffer gnus-original-article-buffer)
5722       (setq gnus-article-current nil))
5723     (when gnus-use-cache
5724       (gnus-cache-possibly-remove-articles)
5725       (gnus-cache-save-buffers))
5726     (gnus-async-prefetch-remove-group group)
5727     (when gnus-suppress-duplicates
5728       (gnus-dup-enter-articles))
5729     (when gnus-use-trees
5730       (gnus-tree-close group))
5731     (when gnus-use-cache
5732       (gnus-cache-write-active))
5733     ;; Remove entries for this group.
5734     (nnmail-purge-split-history (gnus-group-real-name group))
5735     ;; Make all changes in this group permanent.
5736     (unless quit-config
5737       (gnus-run-hooks 'gnus-exit-group-hook)
5738       (gnus-summary-update-info))
5739     (gnus-close-group group)
5740     ;; Make sure where we were, and go to next newsgroup.
5741     (set-buffer gnus-group-buffer)
5742     (unless quit-config
5743       (gnus-group-jump-to-group group))
5744     (gnus-run-hooks 'gnus-summary-exit-hook)
5745     (unless (or quit-config
5746                 ;; If this group has disappeared from the summary
5747                 ;; buffer, don't skip forwards.
5748                 (not (string= group (gnus-group-group-name))))
5749       (gnus-group-next-unread-group 1))
5750     (setq group-point (point))
5751     (if temporary
5752         nil                             ;Nothing to do.
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       (set-buffer buf)
5759       (if (not gnus-kill-summary-on-exit)
5760           (progn
5761             (gnus-deaden-summary)
5762             (setq mode nil))
5763         ;; We set all buffer-local variables to nil.  It is unclear why
5764         ;; this is needed, but if we don't, buffer-local variables are
5765         ;; not garbage-collected, it seems.  This would the lead to en
5766         ;; ever-growing Emacs.
5767         (gnus-summary-clear-local-variables)
5768         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5769           (gnus-summary-clear-local-variables))
5770         (when (get-buffer gnus-article-buffer)
5771           (bury-buffer gnus-article-buffer))
5772         ;; We clear the global counterparts of the buffer-local
5773         ;; variables as well, just to be on the safe side.
5774         (set-buffer gnus-group-buffer)
5775         (gnus-summary-clear-local-variables)
5776         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5777           (gnus-summary-clear-local-variables)))
5778       (setq gnus-current-select-method gnus-select-method)
5779       (pop-to-buffer gnus-group-buffer)
5780       (if (not quit-config)
5781           (progn
5782             (goto-char group-point)
5783             (gnus-configure-windows 'group 'force)
5784             (unless (pos-visible-in-window-p)
5785               (forward-line (/ (static-if (featurep 'xemacs)
5786                                    (window-displayed-height)
5787                                  (1- (window-height)))
5788                                -2))
5789               (set-window-start (selected-window) (point))
5790               (goto-char group-point)))
5791         (gnus-handle-ephemeral-exit quit-config))
5792       ;; Return to group mode buffer.
5793       (when (eq mode 'gnus-summary-mode)
5794         (gnus-kill-buffer buf))
5795       ;; Clear the current group name.
5796       (unless quit-config
5797         (setq gnus-newsgroup-name nil)))))
5798
5799 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5800 (defun gnus-summary-exit-no-update (&optional no-questions)
5801   "Quit reading current newsgroup without updating read article info."
5802   (interactive)
5803   (let* ((group gnus-newsgroup-name)
5804          (quit-config (gnus-group-quit-config group)))
5805     (when (or no-questions
5806               gnus-expert-user
5807               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5808       (gnus-async-halt-prefetch)
5809       (mapcar 'funcall
5810               (delq 'gnus-summary-expire-articles
5811                     (copy-sequence gnus-summary-prepare-exit-hook)))
5812       ;; If we have several article buffers, we kill them at exit.
5813       (unless gnus-single-article-buffer
5814         (gnus-kill-buffer gnus-article-buffer)
5815         (gnus-kill-buffer gnus-original-article-buffer)
5816         (setq gnus-article-current nil))
5817       (if (not gnus-kill-summary-on-exit)
5818           (gnus-deaden-summary)
5819         (gnus-close-group group)
5820         (gnus-summary-clear-local-variables)
5821         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5822           (gnus-summary-clear-local-variables))
5823         (set-buffer gnus-group-buffer)
5824         (gnus-summary-clear-local-variables)
5825         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5826           (gnus-summary-clear-local-variables))
5827         (when (get-buffer gnus-summary-buffer)
5828           (kill-buffer gnus-summary-buffer)))
5829       (unless gnus-single-article-buffer
5830         (setq gnus-article-current nil))
5831       (when gnus-use-trees
5832         (gnus-tree-close group))
5833       (gnus-async-prefetch-remove-group group)
5834       (when (get-buffer gnus-article-buffer)
5835         (bury-buffer gnus-article-buffer))
5836       ;; Return to the group buffer.
5837       (gnus-configure-windows 'group 'force)
5838       ;; Clear the current group name.
5839       (setq gnus-newsgroup-name nil)
5840       (when (equal (gnus-group-group-name) group)
5841         (gnus-group-next-unread-group 1))
5842       (when quit-config
5843         (gnus-handle-ephemeral-exit quit-config)))))
5844
5845 (defun gnus-handle-ephemeral-exit (quit-config)
5846   "Handle movement when leaving an ephemeral group.
5847 The state which existed when entering the ephemeral is reset."
5848   (if (not (buffer-name (car quit-config)))
5849       (gnus-configure-windows 'group 'force)
5850     (set-buffer (car quit-config))
5851     (cond ((eq major-mode 'gnus-summary-mode)
5852            (gnus-set-global-variables))
5853           ((eq major-mode 'gnus-article-mode)
5854            (save-excursion
5855              ;; The `gnus-summary-buffer' variable may point
5856              ;; to the old summary buffer when using a single
5857              ;; article buffer.
5858              (unless (gnus-buffer-live-p gnus-summary-buffer)
5859                (set-buffer gnus-group-buffer))
5860              (set-buffer gnus-summary-buffer)
5861              (gnus-set-global-variables))))
5862     (if (or (eq (cdr quit-config) 'article)
5863             (eq (cdr quit-config) 'pick))
5864         (progn
5865           ;; The current article may be from the ephemeral group
5866           ;; thus it is best that we reload this article
5867           (gnus-summary-show-article)
5868           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5869               (gnus-configure-windows 'pick 'force)
5870             (gnus-configure-windows (cdr quit-config) 'force)))
5871       (gnus-configure-windows (cdr quit-config) 'force))
5872     (when (eq major-mode 'gnus-summary-mode)
5873       (gnus-summary-next-subject 1 nil t)
5874       (gnus-summary-recenter)
5875       (gnus-summary-position-point))))
5876
5877 (defun gnus-summary-preview-mime-message ()
5878   "MIME decode and play this message."
5879   (interactive)
5880   (let ((gnus-break-pages nil)
5881         (gnus-show-mime t))
5882     (gnus-summary-select-article gnus-show-all-headers t))
5883   (select-window (get-buffer-window gnus-article-buffer)))
5884
5885 ;;; Dead summaries.
5886
5887 (defvar gnus-dead-summary-mode-map nil)
5888
5889 (unless gnus-dead-summary-mode-map
5890   (setq gnus-dead-summary-mode-map (make-keymap))
5891   (suppress-keymap gnus-dead-summary-mode-map)
5892   (substitute-key-definition
5893    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5894   (let ((keys '("\C-d" "\r" "\177" [delete])))
5895     (while keys
5896       (define-key gnus-dead-summary-mode-map
5897         (pop keys) 'gnus-summary-wake-up-the-dead))))
5898
5899 (defvar gnus-dead-summary-mode nil
5900   "Minor mode for Gnus summary buffers.")
5901
5902 (defun gnus-dead-summary-mode (&optional arg)
5903   "Minor mode for Gnus summary buffers."
5904   (interactive "P")
5905   (when (eq major-mode 'gnus-summary-mode)
5906     (make-local-variable 'gnus-dead-summary-mode)
5907     (setq gnus-dead-summary-mode
5908           (if (null arg) (not gnus-dead-summary-mode)
5909             (> (prefix-numeric-value arg) 0)))
5910     (when gnus-dead-summary-mode
5911       (gnus-add-minor-mode
5912        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5913
5914 (defun gnus-deaden-summary ()
5915   "Make the current summary buffer into a dead summary buffer."
5916   ;; Kill any previous dead summary buffer.
5917   (when (and gnus-dead-summary
5918              (buffer-name gnus-dead-summary))
5919     (save-excursion
5920       (set-buffer gnus-dead-summary)
5921       (when gnus-dead-summary-mode
5922         (kill-buffer (current-buffer)))))
5923   ;; Make this the current dead summary.
5924   (setq gnus-dead-summary (current-buffer))
5925   (gnus-dead-summary-mode 1)
5926   (let ((name (buffer-name)))
5927     (when (string-match "Summary" name)
5928       (rename-buffer
5929        (concat (substring name 0 (match-beginning 0)) "Dead "
5930                (substring name (match-beginning 0)))
5931        t)
5932       (bury-buffer))))
5933
5934 (defun gnus-kill-or-deaden-summary (buffer)
5935   "Kill or deaden the summary BUFFER."
5936   (save-excursion
5937     (when (and (buffer-name buffer)
5938                (not gnus-single-article-buffer))
5939       (save-excursion
5940         (set-buffer buffer)
5941         (gnus-kill-buffer gnus-article-buffer)
5942         (gnus-kill-buffer gnus-original-article-buffer)))
5943     (cond (gnus-kill-summary-on-exit
5944            (when (and gnus-use-trees
5945                       (gnus-buffer-exists-p buffer))
5946              (save-excursion
5947                (set-buffer buffer)
5948                (gnus-tree-close gnus-newsgroup-name)))
5949            (gnus-kill-buffer buffer))
5950           ((gnus-buffer-exists-p buffer)
5951            (save-excursion
5952              (set-buffer buffer)
5953              (gnus-deaden-summary))))))
5954
5955 (defun gnus-summary-wake-up-the-dead (&rest args)
5956   "Wake up the dead summary buffer."
5957   (interactive)
5958   (gnus-dead-summary-mode -1)
5959   (let ((name (buffer-name)))
5960     (when (string-match "Dead " name)
5961       (rename-buffer
5962        (concat (substring name 0 (match-beginning 0))
5963                (substring name (match-end 0)))
5964        t)))
5965   (gnus-message 3 "This dead summary is now alive again"))
5966
5967 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5968 (defun gnus-summary-fetch-faq (&optional faq-dir)
5969   "Fetch the FAQ for the current group.
5970 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5971 in."
5972   (interactive
5973    (list
5974     (when current-prefix-arg
5975       (completing-read
5976        "Faq dir: " (and (listp gnus-group-faq-directory)
5977                         (mapcar (lambda (file) (list file))
5978                                 gnus-group-faq-directory))))))
5979   (let (gnus-faq-buffer)
5980     (when (setq gnus-faq-buffer
5981                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5982       (gnus-configure-windows 'summary-faq))))
5983
5984 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5985 (defun gnus-summary-describe-group (&optional force)
5986   "Describe the current newsgroup."
5987   (interactive "P")
5988   (gnus-group-describe-group force gnus-newsgroup-name))
5989
5990 (defun gnus-summary-describe-briefly ()
5991   "Describe summary mode commands briefly."
5992   (interactive)
5993   (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")))
5994
5995 ;; Walking around group mode buffer from summary mode.
5996
5997 (defun gnus-summary-next-group (&optional no-article target-group backward)
5998   "Exit current newsgroup and then select next unread newsgroup.
5999 If prefix argument NO-ARTICLE is non-nil, no article is selected
6000 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6001 previous group instead."
6002   (interactive "P")
6003   ;; Stop pre-fetching.
6004   (gnus-async-halt-prefetch)
6005   (let ((current-group gnus-newsgroup-name)
6006         (current-buffer (current-buffer))
6007         entered)
6008     ;; First we semi-exit this group to update Xrefs and all variables.
6009     ;; We can't do a real exit, because the window conf must remain
6010     ;; the same in case the user is prompted for info, and we don't
6011     ;; want the window conf to change before that...
6012     (gnus-summary-exit t)
6013     (while (not entered)
6014       ;; Then we find what group we are supposed to enter.
6015       (set-buffer gnus-group-buffer)
6016       (gnus-group-jump-to-group current-group)
6017       (setq target-group
6018             (or target-group
6019                 (if (eq gnus-keep-same-level 'best)
6020                     (gnus-summary-best-group gnus-newsgroup-name)
6021                   (gnus-summary-search-group backward gnus-keep-same-level))))
6022       (if (not target-group)
6023           ;; There are no further groups, so we return to the group
6024           ;; buffer.
6025           (progn
6026             (gnus-message 5 "Returning to the group buffer")
6027             (setq entered t)
6028             (when (gnus-buffer-live-p current-buffer)
6029               (set-buffer current-buffer)
6030               (gnus-summary-exit))
6031             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6032         ;; We try to enter the target group.
6033         (gnus-group-jump-to-group target-group)
6034         (let ((unreads (gnus-group-group-unread)))
6035           (if (and (or (eq t unreads)
6036                        (and unreads (not (zerop unreads))))
6037                    (gnus-summary-read-group
6038                     target-group nil no-article
6039                     (and (buffer-name current-buffer) current-buffer)
6040                     nil backward))
6041               (setq entered t)
6042             (setq current-group target-group
6043                   target-group nil)))))))
6044
6045 (defun gnus-summary-prev-group (&optional no-article)
6046   "Exit current newsgroup and then select previous unread newsgroup.
6047 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6048   (interactive "P")
6049   (gnus-summary-next-group no-article nil t))
6050
6051 ;; Walking around summary lines.
6052
6053 (defun gnus-summary-first-subject (&optional unread undownloaded)
6054   "Go to the first unread subject.
6055 If UNREAD is non-nil, go to the first unread article.
6056 Returns the article selected or nil if there are no unread articles."
6057   (interactive "P")
6058   (prog1
6059       (cond
6060        ;; Empty summary.
6061        ((null gnus-newsgroup-data)
6062         (gnus-message 3 "No articles in the group")
6063         nil)
6064        ;; Pick the first article.
6065        ((not unread)
6066         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6067         (gnus-data-number (car gnus-newsgroup-data)))
6068        ;; No unread articles.
6069        ((null gnus-newsgroup-unreads)
6070         (gnus-message 3 "No more unread articles")
6071         nil)
6072        ;; Find the first unread article.
6073        (t
6074         (let ((data gnus-newsgroup-data))
6075           (while (and data
6076                       (and (not (and undownloaded
6077                                      (eq gnus-undownloaded-mark
6078                                          (gnus-data-mark (car data)))))
6079                            (not (gnus-data-unread-p (car data)))))
6080             (setq data (cdr data)))
6081           (when data
6082             (goto-char (gnus-data-pos (car data)))
6083             (gnus-data-number (car data))))))
6084     (gnus-summary-position-point)))
6085
6086 (defun gnus-summary-next-subject (n &optional unread dont-display)
6087   "Go to next N'th summary line.
6088 If N is negative, go to the previous N'th subject line.
6089 If UNREAD is non-nil, only unread articles are selected.
6090 The difference between N and the actual number of steps taken is
6091 returned."
6092   (interactive "p")
6093   (let ((backward (< n 0))
6094         (n (abs n)))
6095     (while (and (> n 0)
6096                 (if backward
6097                     (gnus-summary-find-prev unread)
6098                   (gnus-summary-find-next unread)))
6099       (unless (zerop (setq n (1- n)))
6100         (gnus-summary-show-thread)))
6101     (when (/= 0 n)
6102       (gnus-message 7 "No more%s articles"
6103                     (if unread " unread" "")))
6104     (unless dont-display
6105       (gnus-summary-recenter)
6106       (gnus-summary-position-point))
6107     n))
6108
6109 (defun gnus-summary-next-unread-subject (n)
6110   "Go to next N'th unread summary line."
6111   (interactive "p")
6112   (gnus-summary-next-subject n t))
6113
6114 (defun gnus-summary-prev-subject (n &optional unread)
6115   "Go to previous N'th summary line.
6116 If optional argument UNREAD is non-nil, only unread article is selected."
6117   (interactive "p")
6118   (gnus-summary-next-subject (- n) unread))
6119
6120 (defun gnus-summary-prev-unread-subject (n)
6121   "Go to previous N'th unread summary line."
6122   (interactive "p")
6123   (gnus-summary-next-subject (- n) t))
6124
6125 (defun gnus-summary-goto-subject (article &optional force silent)
6126   "Go the subject line of ARTICLE.
6127 If FORCE, also allow jumping to articles not currently shown."
6128   (interactive "nArticle number: ")
6129   (let ((b (point))
6130         (data (gnus-data-find article)))
6131     ;; We read in the article if we have to.
6132     (and (not data)
6133          force
6134          (gnus-summary-insert-subject
6135           article
6136           (if (or (numberp force) (vectorp force)) force)
6137           t)
6138          (setq data (gnus-data-find article)))
6139     (goto-char b)
6140     (if (not data)
6141         (progn
6142           (unless silent
6143             (gnus-message 3 "Can't find article %d" article))
6144           nil)
6145       (goto-char (gnus-data-pos data))
6146       (gnus-summary-position-point)
6147       article)))
6148
6149 ;; Walking around summary lines with displaying articles.
6150
6151 (defun gnus-summary-expand-window (&optional arg)
6152   "Make the summary buffer take up the entire Emacs frame.
6153 Given a prefix, will force an `article' buffer configuration."
6154   (interactive "P")
6155   (if arg
6156       (gnus-configure-windows 'article 'force)
6157     (gnus-configure-windows 'summary 'force)))
6158
6159 (defun gnus-summary-display-article (article &optional all-header)
6160   "Display ARTICLE in article buffer."
6161   (when (gnus-buffer-live-p gnus-article-buffer)
6162     (with-current-buffer gnus-article-buffer
6163       (set-buffer-multibyte t)))
6164   (gnus-set-global-variables)
6165   (when (gnus-buffer-live-p gnus-article-buffer)
6166     (with-current-buffer gnus-article-buffer
6167       (setq gnus-article-charset gnus-newsgroup-charset)
6168       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)))
6169   (if (null article)
6170       nil
6171     (prog1
6172         (if gnus-summary-display-article-function
6173             (funcall gnus-summary-display-article-function article all-header)
6174           (gnus-article-prepare article all-header))
6175       (with-current-buffer gnus-article-buffer
6176         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6177              nil))
6178       (gnus-run-hooks 'gnus-select-article-hook)
6179       (when (and gnus-current-article
6180                  (not (zerop gnus-current-article)))
6181         (gnus-summary-goto-subject gnus-current-article))
6182       (gnus-summary-recenter)
6183       (when (and gnus-use-trees gnus-show-threads)
6184         (gnus-possibly-generate-tree article)
6185         (gnus-highlight-selected-tree article))
6186       ;; Successfully display article.
6187       (gnus-article-set-window-start
6188        (cdr (assq article gnus-newsgroup-bookmarks))))))
6189
6190 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6191   "Select the current article.
6192 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6193 non-nil, the article will be re-fetched even if it already present in
6194 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6195 be displayed."
6196   ;; Make sure we are in the summary buffer to work around bbdb bug.
6197   (unless (eq major-mode 'gnus-summary-mode)
6198     (set-buffer gnus-summary-buffer))
6199   (let ((article (or article (gnus-summary-article-number)))
6200         (all-headers (not (not all-headers))) ;Must be T or NIL.
6201         gnus-summary-display-article-function)
6202     (and (not pseudo)
6203          (gnus-summary-article-pseudo-p article)
6204          (error "This is a pseudo-article"))
6205     (save-excursion
6206       (set-buffer gnus-summary-buffer)
6207       (if (or (and gnus-single-article-buffer
6208                    (or (null gnus-current-article)
6209                        (null gnus-article-current)
6210                        (null (get-buffer gnus-article-buffer))
6211                        (not (eq article (cdr gnus-article-current)))
6212                        (not (equal (car gnus-article-current)
6213                                    gnus-newsgroup-name))))
6214               (and (not gnus-single-article-buffer)
6215                    (or (null gnus-current-article)
6216                        (not (eq gnus-current-article article))))
6217               force)
6218           ;; The requested article is different from the current article.
6219           (progn
6220             (gnus-summary-display-article article all-headers)
6221             (when (or all-headers gnus-show-all-headers)
6222               (gnus-article-show-all-headers))
6223             (gnus-article-set-window-start
6224              (cdr (assq article gnus-newsgroup-bookmarks)))
6225             article)
6226         (when (or all-headers gnus-show-all-headers)
6227           (gnus-article-show-all-headers))
6228         'old))))
6229
6230 (defun gnus-summary-force-verify-and-decrypt ()
6231   (interactive)
6232   (let ((mm-verify-option 'known)
6233         (mm-decrypt-option 'known))
6234     (gnus-summary-select-article nil 'force)))
6235
6236 (defun gnus-summary-set-current-mark (&optional current-mark)
6237   "Obsolete function."
6238   nil)
6239
6240 (defun gnus-summary-next-article (&optional unread subject backward push)
6241   "Select the next article.
6242 If UNREAD, only unread articles are selected.
6243 If SUBJECT, only articles with SUBJECT are selected.
6244 If BACKWARD, the previous article is selected instead of the next."
6245   (interactive "P")
6246   (cond
6247    ;; Is there such an article?
6248    ((and (gnus-summary-search-forward unread subject backward)
6249          (or (gnus-summary-display-article (gnus-summary-article-number))
6250              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6251     (gnus-summary-position-point))
6252    ;; If not, we try the first unread, if that is wanted.
6253    ((and subject
6254          gnus-auto-select-same
6255          (gnus-summary-first-unread-article))
6256     (gnus-summary-position-point)
6257     (gnus-message 6 "Wrapped"))
6258    ;; Try to get next/previous article not displayed in this group.
6259    ((and gnus-auto-extend-newsgroup
6260          (not unread) (not subject))
6261     (gnus-summary-goto-article
6262      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6263      nil (count-lines (point-min) (point))))
6264    ;; Go to next/previous group.
6265    (t
6266     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6267       (gnus-summary-jump-to-group gnus-newsgroup-name))
6268     (let ((cmd last-command-char)
6269           (point
6270            (save-excursion
6271              (set-buffer gnus-group-buffer)
6272              (point)))
6273           (group
6274            (if (eq gnus-keep-same-level 'best)
6275                (gnus-summary-best-group gnus-newsgroup-name)
6276              (gnus-summary-search-group backward gnus-keep-same-level))))
6277       ;; For some reason, the group window gets selected.  We change
6278       ;; it back.
6279       (select-window (get-buffer-window (current-buffer)))
6280       ;; Select next unread newsgroup automagically.
6281       (cond
6282        ((or (not gnus-auto-select-next)
6283             (not cmd))
6284         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6285        ((or (eq gnus-auto-select-next 'quietly)
6286             (and (eq gnus-auto-select-next 'slightly-quietly)
6287                  push)
6288             (and (eq gnus-auto-select-next 'almost-quietly)
6289                  (gnus-summary-last-article-p)))
6290         ;; Select quietly.
6291         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6292             (gnus-summary-exit)
6293           (gnus-message 7 "No more%s articles (%s)..."
6294                         (if unread " unread" "")
6295                         (if group (concat "selecting " group)
6296                           "exiting"))
6297           (gnus-summary-next-group nil group backward)))
6298        (t
6299         (when (gnus-key-press-event-p last-input-event)
6300           (gnus-summary-walk-group-buffer
6301            gnus-newsgroup-name cmd unread backward point))))))))
6302
6303 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6304   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6305                       (?\C-p (gnus-group-prev-unread-group 1))))
6306         (cursor-in-echo-area t)
6307         keve key group ended)
6308     (save-excursion
6309       (set-buffer gnus-group-buffer)
6310       (goto-char start)
6311       (setq group
6312             (if (eq gnus-keep-same-level 'best)
6313                 (gnus-summary-best-group gnus-newsgroup-name)
6314               (gnus-summary-search-group backward gnus-keep-same-level))))
6315     (while (not ended)
6316       (gnus-message
6317        5 "No more%s articles%s" (if unread " unread" "")
6318        (if (and group
6319                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6320            (format " (Type %s for %s [%s])"
6321                    (single-key-description cmd) group
6322                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6323          (format " (Type %s to exit %s)"
6324                  (single-key-description cmd)
6325                  gnus-newsgroup-name)))
6326       ;; Confirm auto selection.
6327       (setq key (car (setq keve (gnus-read-event-char))))
6328       (setq ended t)
6329       (cond
6330        ((assq key keystrokes)
6331         (let ((obuf (current-buffer)))
6332           (switch-to-buffer gnus-group-buffer)
6333           (when group
6334             (gnus-group-jump-to-group group))
6335           (eval (cadr (assq key keystrokes)))
6336           (setq group (gnus-group-group-name))
6337           (switch-to-buffer obuf))
6338         (setq ended nil))
6339        ((equal key cmd)
6340         (if (or (not group)
6341                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6342             (gnus-summary-exit)
6343           (gnus-summary-next-group nil group backward)))
6344        (t
6345         (push (cdr keve) unread-command-events))))))
6346
6347 (defun gnus-summary-next-unread-article ()
6348   "Select unread article after current one."
6349   (interactive)
6350   (gnus-summary-next-article
6351    (or (not (eq gnus-summary-goto-unread 'never))
6352        (gnus-summary-last-article-p (gnus-summary-article-number)))
6353    (and gnus-auto-select-same
6354         (gnus-summary-article-subject))))
6355
6356 (defun gnus-summary-prev-article (&optional unread subject)
6357   "Select the article after the current one.
6358 If UNREAD is non-nil, only unread articles are selected."
6359   (interactive "P")
6360   (gnus-summary-next-article unread subject t))
6361
6362 (defun gnus-summary-prev-unread-article ()
6363   "Select unread article before current one."
6364   (interactive)
6365   (gnus-summary-prev-article
6366    (or (not (eq gnus-summary-goto-unread 'never))
6367        (gnus-summary-first-article-p (gnus-summary-article-number)))
6368    (and gnus-auto-select-same
6369         (gnus-summary-article-subject))))
6370
6371 (defun gnus-summary-next-page (&optional lines circular)
6372   "Show next page of the selected article.
6373 If at the end of the current article, select the next article.
6374 LINES says how many lines should be scrolled up.
6375
6376 If CIRCULAR is non-nil, go to the start of the article instead of
6377 selecting the next article when reaching the end of the current
6378 article."
6379   (interactive "P")
6380   (setq gnus-summary-buffer (current-buffer))
6381   (gnus-set-global-variables)
6382   (let ((article (gnus-summary-article-number))
6383         (article-window (get-buffer-window gnus-article-buffer t))
6384         endp)
6385     ;; If the buffer is empty, we have no article.
6386     (unless article
6387       (error "No article to select"))
6388     (gnus-configure-windows 'article)
6389     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6390         (if (and (eq gnus-summary-goto-unread 'never)
6391                  (not (gnus-summary-last-article-p article)))
6392             (gnus-summary-next-article)
6393           (gnus-summary-next-unread-article))
6394       (if (or (null gnus-current-article)
6395               (null gnus-article-current)
6396               (/= article (cdr gnus-article-current))
6397               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6398           ;; Selected subject is different from current article's.
6399           (gnus-summary-display-article article)
6400         (when article-window
6401           (gnus-eval-in-buffer-window gnus-article-buffer
6402             (setq endp (gnus-article-next-page lines)))
6403           (when endp
6404             (cond (circular
6405                    (gnus-summary-beginning-of-article))
6406                   (lines
6407                    (gnus-message 3 "End of message"))
6408                   ((null lines)
6409                    (if (and (eq gnus-summary-goto-unread 'never)
6410                             (not (gnus-summary-last-article-p article)))
6411                        (gnus-summary-next-article)
6412                      (gnus-summary-next-unread-article))))))))
6413     (gnus-summary-recenter)
6414     (gnus-summary-position-point)))
6415
6416 (defun gnus-summary-prev-page (&optional lines move)
6417   "Show previous page of selected article.
6418 Argument LINES specifies lines to be scrolled down.
6419 If MOVE, move to the previous unread article if point is at
6420 the beginning of the buffer."
6421   (interactive "P")
6422   (let ((article (gnus-summary-article-number))
6423         (article-window (get-buffer-window gnus-article-buffer t))
6424         endp)
6425     (gnus-configure-windows 'article)
6426     (if (or (null gnus-current-article)
6427             (null gnus-article-current)
6428             (/= article (cdr gnus-article-current))
6429             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6430         ;; Selected subject is different from current article's.
6431         (gnus-summary-display-article article)
6432       (gnus-summary-recenter)
6433       (when article-window
6434         (gnus-eval-in-buffer-window gnus-article-buffer
6435           (setq endp (gnus-article-prev-page lines)))
6436         (when (and move endp)
6437           (cond (lines
6438                  (gnus-message 3 "Beginning of message"))
6439                 ((null lines)
6440                  (if (and (eq gnus-summary-goto-unread 'never)
6441                           (not (gnus-summary-first-article-p article)))
6442                      (gnus-summary-prev-article)
6443                    (gnus-summary-prev-unread-article))))))))
6444   (gnus-summary-position-point))
6445
6446 (defun gnus-summary-prev-page-or-article (&optional lines)
6447   "Show previous page of selected article.
6448 Argument LINES specifies lines to be scrolled down.
6449 If at the beginning of the article, go to the next article."
6450   (interactive "P")
6451   (gnus-summary-prev-page lines t))
6452
6453 (defun gnus-summary-scroll-up (lines)
6454   "Scroll up (or down) one line current article.
6455 Argument LINES specifies lines to be scrolled up (or down if negative)."
6456   (interactive "p")
6457   (gnus-configure-windows 'article)
6458   (gnus-summary-show-thread)
6459   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6460     (gnus-eval-in-buffer-window gnus-article-buffer
6461       (cond ((> lines 0)
6462              (when (gnus-article-next-page lines)
6463                (gnus-message 3 "End of message")))
6464             ((< lines 0)
6465              (gnus-article-prev-page (- lines))))))
6466   (gnus-summary-recenter)
6467   (gnus-summary-position-point))
6468
6469 (defun gnus-summary-scroll-down (lines)
6470   "Scroll down (or up) one line current article.
6471 Argument LINES specifies lines to be scrolled down (or up if negative)."
6472   (interactive "p")
6473   (gnus-summary-scroll-up (- lines)))
6474
6475 (defun gnus-summary-next-same-subject ()
6476   "Select next article which has the same subject as current one."
6477   (interactive)
6478   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6479
6480 (defun gnus-summary-prev-same-subject ()
6481   "Select previous article which has the same subject as current one."
6482   (interactive)
6483   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6484
6485 (defun gnus-summary-next-unread-same-subject ()
6486   "Select next unread article which has the same subject as current one."
6487   (interactive)
6488   (gnus-summary-next-article t (gnus-summary-article-subject)))
6489
6490 (defun gnus-summary-prev-unread-same-subject ()
6491   "Select previous unread article which has the same subject as current one."
6492   (interactive)
6493   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6494
6495 (defun gnus-summary-first-unread-article ()
6496   "Select the first unread article.
6497 Return nil if there are no unread articles."
6498   (interactive)
6499   (prog1
6500       (when (gnus-summary-first-subject t)
6501         (gnus-summary-show-thread)
6502         (gnus-summary-first-subject t)
6503         (gnus-summary-display-article (gnus-summary-article-number)))
6504     (gnus-summary-position-point)))
6505
6506 (defun gnus-summary-first-unread-subject ()
6507   "Place the point on the subject line of the first unread article.
6508 Return nil if there are no unread articles."
6509   (interactive)
6510   (prog1
6511       (when (gnus-summary-first-subject t)
6512         (gnus-summary-show-thread)
6513         (gnus-summary-first-subject t))
6514     (gnus-summary-position-point)))
6515
6516 (defun gnus-summary-first-article ()
6517   "Select the first article.
6518 Return nil if there are no articles."
6519   (interactive)
6520   (prog1
6521       (when (gnus-summary-first-subject)
6522         (gnus-summary-show-thread)
6523         (gnus-summary-first-subject)
6524         (gnus-summary-display-article (gnus-summary-article-number)))
6525     (gnus-summary-position-point)))
6526
6527 (defun gnus-summary-best-unread-article ()
6528   "Select the unread article with the highest score."
6529   (interactive)
6530   (let ((best -1000000)
6531         (data gnus-newsgroup-data)
6532         article score)
6533     (while data
6534       (and (gnus-data-unread-p (car data))
6535            (> (setq score
6536                     (gnus-summary-article-score (gnus-data-number (car data))))
6537               best)
6538            (setq best score
6539                  article (gnus-data-number (car data))))
6540       (setq data (cdr data)))
6541     (prog1
6542         (if article
6543             (gnus-summary-goto-article article)
6544           (error "No unread articles"))
6545       (gnus-summary-position-point))))
6546
6547 (defun gnus-summary-last-subject ()
6548   "Go to the last displayed subject line in the group."
6549   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6550     (when article
6551       (gnus-summary-goto-subject article))))
6552
6553 (defun gnus-summary-goto-article (article &optional all-headers force)
6554   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6555 If ALL-HEADERS is non-nil, no header lines are hidden.
6556 If FORCE, go to the article even if it isn't displayed.  If FORCE
6557 is a number, it is the line the article is to be displayed on."
6558   (interactive
6559    (list
6560     (completing-read
6561      "Article number or Message-ID: "
6562      (mapcar (lambda (number) (list (int-to-string number)))
6563              gnus-newsgroup-limit))
6564     current-prefix-arg
6565     t))
6566   (prog1
6567       (if (and (stringp article)
6568                (string-match "@" article))
6569           (gnus-summary-refer-article article)
6570         (when (stringp article)
6571           (setq article (string-to-number article)))
6572         (if (gnus-summary-goto-subject article force)
6573             (gnus-summary-display-article article all-headers)
6574           (gnus-message 4 "Couldn't go to article %s" article) nil))
6575     (gnus-summary-position-point)))
6576
6577 (defun gnus-summary-goto-last-article ()
6578   "Go to the previously read article."
6579   (interactive)
6580   (prog1
6581       (when gnus-last-article
6582         (gnus-summary-goto-article gnus-last-article nil t))
6583     (gnus-summary-position-point)))
6584
6585 (defun gnus-summary-pop-article (number)
6586   "Pop one article off the history and go to the previous.
6587 NUMBER articles will be popped off."
6588   (interactive "p")
6589   (let (to)
6590     (setq gnus-newsgroup-history
6591           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6592     (if to
6593         (gnus-summary-goto-article (car to) nil t)
6594       (error "Article history empty")))
6595   (gnus-summary-position-point))
6596
6597 ;; Summary commands and functions for limiting the summary buffer.
6598
6599 (defun gnus-summary-limit-to-articles (n)
6600   "Limit the summary buffer to the next N articles.
6601 If not given a prefix, use the process marked articles instead."
6602   (interactive "P")
6603   (prog1
6604       (let ((articles (gnus-summary-work-articles n)))
6605         (setq gnus-newsgroup-processable nil)
6606         (gnus-summary-limit articles))
6607     (gnus-summary-position-point)))
6608
6609 (defun gnus-summary-pop-limit (&optional total)
6610   "Restore the previous limit.
6611 If given a prefix, remove all limits."
6612   (interactive "P")
6613   (when total
6614     (setq gnus-newsgroup-limits
6615           (list (mapcar (lambda (h) (mail-header-number h))
6616                         gnus-newsgroup-headers))))
6617   (unless gnus-newsgroup-limits
6618     (error "No limit to pop"))
6619   (prog1
6620       (gnus-summary-limit nil 'pop)
6621     (gnus-summary-position-point)))
6622
6623 (defun gnus-summary-limit-to-subject (subject &optional header)
6624   "Limit the summary buffer to articles that have subjects that match a regexp."
6625   (interactive "sLimit to subject (regexp): ")
6626   (unless header
6627     (setq header "subject"))
6628   (when (not (equal "" subject))
6629     (prog1
6630         (let ((articles (gnus-summary-find-matching
6631                          (or header "subject") subject 'all)))
6632           (unless articles
6633             (error "Found no matches for \"%s\"" subject))
6634           (gnus-summary-limit articles))
6635       (gnus-summary-position-point))))
6636
6637 (defun gnus-summary-limit-to-author (from)
6638   "Limit the summary buffer to articles that have authors that match a regexp."
6639   (interactive "sLimit to author (regexp): ")
6640   (gnus-summary-limit-to-subject from "from"))
6641
6642 (defun gnus-summary-limit-to-age (age &optional younger-p)
6643   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6644 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6645 articles that are younger than AGE days."
6646   (interactive
6647    (let ((younger current-prefix-arg)
6648          (days-got nil)
6649          days)
6650      (while (not days-got)
6651        (setq days (if younger
6652                       (read-string "Limit to articles within (in days): ")
6653                     (read-string "Limit to articles older than (in days): ")))
6654        (when (> (length days) 0)
6655          (setq days (read days)))
6656        (if (numberp days)
6657            (setq days-got t)
6658          (message "Please enter a number.")
6659          (sleep-for 1)))
6660      (list days younger)))
6661   (prog1
6662       (let ((data gnus-newsgroup-data)
6663             (cutoff (days-to-time age))
6664             articles d date is-younger)
6665         (while (setq d (pop data))
6666           (when (and (vectorp (gnus-data-header d))
6667                      (setq date (mail-header-date (gnus-data-header d))))
6668             (setq is-younger (time-less-p
6669                               (time-since (condition-case ()
6670                                               (date-to-time date)
6671                                             (error '(0 0))))
6672                               cutoff))
6673             (when (if younger-p
6674                       is-younger
6675                     (not is-younger))
6676               (push (gnus-data-number d) articles))))
6677         (gnus-summary-limit (nreverse articles)))
6678     (gnus-summary-position-point)))
6679
6680 (defun gnus-summary-limit-to-extra (header regexp)
6681   "Limit the summary buffer to articles that match an 'extra' header."
6682   (interactive
6683    (let ((header
6684           (intern
6685            (gnus-completing-read
6686             (symbol-name (car gnus-extra-headers))
6687             "Limit extra header:"
6688             (mapcar (lambda (x)
6689                       (cons (symbol-name x) x))
6690                     gnus-extra-headers)
6691             nil
6692             t))))
6693      (list header
6694            (read-string (format "Limit to header %s (regexp): " header)))))
6695   (when (not (equal "" regexp))
6696     (prog1
6697         (let ((articles (gnus-summary-find-matching
6698                          (cons 'extra header) regexp 'all)))
6699           (unless articles
6700             (error "Found no matches for \"%s\"" regexp))
6701           (gnus-summary-limit articles))
6702       (gnus-summary-position-point))))
6703
6704 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6705 (make-obsolete
6706  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6707
6708 (defun gnus-summary-limit-to-unread (&optional all)
6709   "Limit the summary buffer to articles that are not marked as read.
6710 If ALL is non-nil, limit strictly to unread articles."
6711   (interactive "P")
6712   (if all
6713       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6714     (gnus-summary-limit-to-marks
6715      ;; Concat all the marks that say that an article is read and have
6716      ;; those removed.
6717      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6718            gnus-killed-mark gnus-kill-file-mark
6719            gnus-low-score-mark gnus-expirable-mark
6720            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6721            gnus-duplicate-mark gnus-souped-mark)
6722      'reverse)))
6723
6724 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6725 (make-obsolete 'gnus-summary-delete-marked-with
6726                'gnus-summary-limit-exlude-marks)
6727
6728 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6729   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6730 If REVERSE, limit the summary buffer to articles that are marked
6731 with MARKS.  MARKS can either be a string of marks or a list of marks.
6732 Returns how many articles were removed."
6733   (interactive "sMarks: ")
6734   (gnus-summary-limit-to-marks marks t))
6735
6736 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6737   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6738 If REVERSE (the prefix), limit the summary buffer to articles that are
6739 not marked with MARKS.  MARKS can either be a string of marks or a
6740 list of marks.
6741 Returns how many articles were removed."
6742   (interactive "sMarks: \nP")
6743   (prog1
6744       (let ((data gnus-newsgroup-data)
6745             (marks (if (listp marks) marks
6746                      (append marks nil))) ; Transform to list.
6747             articles)
6748         (while data
6749           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6750                   (memq (gnus-data-mark (car data)) marks))
6751             (push (gnus-data-number (car data)) articles))
6752           (setq data (cdr data)))
6753         (gnus-summary-limit articles))
6754     (gnus-summary-position-point)))
6755
6756 (defun gnus-summary-limit-to-score (&optional score)
6757   "Limit to articles with score at or above SCORE."
6758   (interactive "P")
6759   (setq score (if score
6760                   (prefix-numeric-value score)
6761                 (or gnus-summary-default-score 0)))
6762   (let ((data gnus-newsgroup-data)
6763         articles)
6764     (while data
6765       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6766                 score)
6767         (push (gnus-data-number (car data)) articles))
6768       (setq data (cdr data)))
6769     (prog1
6770         (gnus-summary-limit articles)
6771       (gnus-summary-position-point))))
6772
6773 (defun gnus-summary-limit-include-thread (id)
6774   "Display all the hidden articles that is in the thread with ID in it.
6775 When called interactively, ID is the Message-ID of the current
6776 article."
6777   (interactive (list (mail-header-id (gnus-summary-article-header))))
6778   (let ((articles (gnus-articles-in-thread
6779                    (gnus-id-to-thread (gnus-root-id id)))))
6780     (prog1
6781         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6782         (gnus-summary-limit-include-matching-articles
6783          "subject"
6784          (regexp-quote (gnus-simplify-subject-re
6785                         (mail-header-subject (gnus-id-to-header id)))))
6786       (gnus-summary-position-point))))
6787
6788 (defun gnus-summary-limit-include-matching-articles (header regexp)
6789   "Display all the hidden articles that have HEADERs that match REGEXP."
6790   (interactive (list (read-string "Match on header: ")
6791                      (read-string "Regexp: ")))
6792   (let ((articles (gnus-find-matching-articles header regexp)))
6793     (prog1
6794         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6795       (gnus-summary-position-point))))
6796
6797 (defun gnus-summary-limit-include-dormant ()
6798   "Display all the hidden articles that are marked as dormant.
6799 Note that this command only works on a subset of the articles currently
6800 fetched for this group."
6801   (interactive)
6802   (unless gnus-newsgroup-dormant
6803     (error "There are no dormant articles in this group"))
6804   (prog1
6805       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6806     (gnus-summary-position-point)))
6807
6808 (defun gnus-summary-limit-exclude-dormant ()
6809   "Hide all dormant articles."
6810   (interactive)
6811   (prog1
6812       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6813     (gnus-summary-position-point)))
6814
6815 (defun gnus-summary-limit-exclude-childless-dormant ()
6816   "Hide all dormant articles that have no children."
6817   (interactive)
6818   (let ((data (gnus-data-list t))
6819         articles d children)
6820     ;; Find all articles that are either not dormant or have
6821     ;; children.
6822     (while (setq d (pop data))
6823       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6824                 (and (setq children
6825                            (gnus-article-children (gnus-data-number d)))
6826                      (let (found)
6827                        (while children
6828                          (when (memq (car children) articles)
6829                            (setq children nil
6830                                  found t))
6831                          (pop children))
6832                        found)))
6833         (push (gnus-data-number d) articles)))
6834     ;; Do the limiting.
6835     (prog1
6836         (gnus-summary-limit articles)
6837       (gnus-summary-position-point))))
6838
6839 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6840   "Mark all unread excluded articles as read.
6841 If ALL, mark even excluded ticked and dormants as read."
6842   (interactive "P")
6843   (let ((articles (gnus-sorted-complement
6844                    (sort
6845                     (mapcar (lambda (h) (mail-header-number h))
6846                             gnus-newsgroup-headers)
6847                     '<)
6848                    (sort gnus-newsgroup-limit '<)))
6849         article)
6850     (setq gnus-newsgroup-unreads
6851           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6852     (if all
6853         (setq gnus-newsgroup-dormant nil
6854               gnus-newsgroup-marked nil
6855               gnus-newsgroup-reads
6856               (nconc
6857                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6858                gnus-newsgroup-reads))
6859       (while (setq article (pop articles))
6860         (unless (or (memq article gnus-newsgroup-dormant)
6861                     (memq article gnus-newsgroup-marked))
6862           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6863
6864 (defun gnus-summary-limit (articles &optional pop)
6865   (if pop
6866       ;; We pop the previous limit off the stack and use that.
6867       (setq articles (car gnus-newsgroup-limits)
6868             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6869     ;; We use the new limit, so we push the old limit on the stack.
6870     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6871   ;; Set the limit.
6872   (setq gnus-newsgroup-limit articles)
6873   (let ((total (length gnus-newsgroup-data))
6874         (data (gnus-data-find-list (gnus-summary-article-number)))
6875         (gnus-summary-mark-below nil)   ; Inhibit this.
6876         found)
6877     ;; This will do all the work of generating the new summary buffer
6878     ;; according to the new limit.
6879     (gnus-summary-prepare)
6880     ;; Hide any threads, possibly.
6881     (and gnus-show-threads
6882          gnus-thread-hide-subtree
6883          (gnus-summary-hide-all-threads))
6884     ;; Try to return to the article you were at, or one in the
6885     ;; neighborhood.
6886     (when data
6887       ;; We try to find some article after the current one.
6888       (while data
6889         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6890           (setq data nil
6891                 found t))
6892         (setq data (cdr data))))
6893     (unless found
6894       ;; If there is no data, that means that we were after the last
6895       ;; article.  The same goes when we can't find any articles
6896       ;; after the current one.
6897       (goto-char (point-max))
6898       (gnus-summary-find-prev))
6899     (gnus-set-mode-line 'summary)
6900     ;; We return how many articles were removed from the summary
6901     ;; buffer as a result of the new limit.
6902     (- total (length gnus-newsgroup-data))))
6903
6904 (defsubst gnus-invisible-cut-children (threads)
6905   (let ((num 0))
6906     (while threads
6907       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6908         (incf num))
6909       (pop threads))
6910     (< num 2)))
6911
6912 (defsubst gnus-cut-thread (thread)
6913   "Go forwards in the thread until we find an article that we want to display."
6914   (when (or (eq gnus-fetch-old-headers 'some)
6915             (eq gnus-fetch-old-headers 'invisible)
6916             (numberp gnus-fetch-old-headers)
6917             (eq gnus-build-sparse-threads 'some)
6918             (eq gnus-build-sparse-threads 'more))
6919     ;; Deal with old-fetched headers and sparse threads.
6920     (while (and
6921             thread
6922             (or
6923              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6924              (gnus-summary-article-ancient-p
6925               (mail-header-number (car thread))))
6926             (if (or (<= (length (cdr thread)) 1)
6927                     (eq gnus-fetch-old-headers 'invisible))
6928                 (setq gnus-newsgroup-limit
6929                       (delq (mail-header-number (car thread))
6930                             gnus-newsgroup-limit)
6931                       thread (cadr thread))
6932               (when (gnus-invisible-cut-children (cdr thread))
6933                 (let ((th (cdr thread)))
6934                   (while th
6935                     (if (memq (mail-header-number (caar th))
6936                               gnus-newsgroup-limit)
6937                         (setq thread (car th)
6938                               th nil)
6939                       (setq th (cdr th))))))))))
6940   thread)
6941
6942 (defun gnus-cut-threads (threads)
6943   "Cut off all uninteresting articles from the beginning of threads."
6944   (when (or (eq gnus-fetch-old-headers 'some)
6945             (eq gnus-fetch-old-headers 'invisible)
6946             (numberp gnus-fetch-old-headers)
6947             (eq gnus-build-sparse-threads 'some)
6948             (eq gnus-build-sparse-threads 'more))
6949     (let ((th threads))
6950       (while th
6951         (setcar th (gnus-cut-thread (car th)))
6952         (setq th (cdr th)))))
6953   ;; Remove nixed out threads.
6954   (delq nil threads))
6955
6956 (defun gnus-summary-initial-limit (&optional show-if-empty)
6957   "Figure out what the initial limit is supposed to be on group entry.
6958 This entails weeding out unwanted dormants, low-scored articles,
6959 fetch-old-headers verbiage, and so on."
6960   ;; Most groups have nothing to remove.
6961   (if (or gnus-inhibit-limiting
6962           (and (null gnus-newsgroup-dormant)
6963                (not (eq gnus-fetch-old-headers 'some))
6964                (not (numberp gnus-fetch-old-headers))
6965                (not (eq gnus-fetch-old-headers 'invisible))
6966                (null gnus-summary-expunge-below)
6967                (not (eq gnus-build-sparse-threads 'some))
6968                (not (eq gnus-build-sparse-threads 'more))
6969                (null gnus-thread-expunge-below)
6970                (not gnus-use-nocem)))
6971       ()                                ; Do nothing.
6972     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6973     (setq gnus-newsgroup-limit nil)
6974     (mapatoms
6975      (lambda (node)
6976        (unless (car (symbol-value node))
6977          ;; These threads have no parents -- they are roots.
6978          (let ((nodes (cdr (symbol-value node)))
6979                thread)
6980            (while nodes
6981              (if (and gnus-thread-expunge-below
6982                       (< (gnus-thread-total-score (car nodes))
6983                          gnus-thread-expunge-below))
6984                  (gnus-expunge-thread (pop nodes))
6985                (setq thread (pop nodes))
6986                (gnus-summary-limit-children thread))))))
6987      gnus-newsgroup-dependencies)
6988     ;; If this limitation resulted in an empty group, we might
6989     ;; pop the previous limit and use it instead.
6990     (when (and (not gnus-newsgroup-limit)
6991                show-if-empty)
6992       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6993     gnus-newsgroup-limit))
6994
6995 (defun gnus-summary-limit-children (thread)
6996   "Return 1 if this subthread is visible and 0 if it is not."
6997   ;; First we get the number of visible children to this thread.  This
6998   ;; is done by recursing down the thread using this function, so this
6999   ;; will really go down to a leaf article first, before slowly
7000   ;; working its way up towards the root.
7001   (when thread
7002     (let ((children
7003            (if (cdr thread)
7004                (apply '+ (mapcar 'gnus-summary-limit-children
7005                                  (cdr thread)))
7006              0))
7007           (number (mail-header-number (car thread)))
7008           score)
7009       (if (and
7010            (not (memq number gnus-newsgroup-marked))
7011            (or
7012             ;; If this article is dormant and has absolutely no visible
7013             ;; children, then this article isn't visible.
7014             (and (memq number gnus-newsgroup-dormant)
7015                  (zerop children))
7016             ;; If this is "fetch-old-headered" and there is no
7017             ;; visible children, then we don't want this article.
7018             (and (or (eq gnus-fetch-old-headers 'some)
7019                      (numberp gnus-fetch-old-headers))
7020                  (gnus-summary-article-ancient-p number)
7021                  (zerop children))
7022             ;; If this is "fetch-old-headered" and `invisible', then
7023             ;; we don't want this article.
7024             (and (eq gnus-fetch-old-headers 'invisible)
7025                  (gnus-summary-article-ancient-p number))
7026             ;; If this is a sparsely inserted article with no children,
7027             ;; we don't want it.
7028             (and (eq gnus-build-sparse-threads 'some)
7029                  (gnus-summary-article-sparse-p number)
7030                  (zerop children))
7031             ;; If we use expunging, and this article is really
7032             ;; low-scored, then we don't want this article.
7033             (when (and gnus-summary-expunge-below
7034                        (< (setq score
7035                                 (or (cdr (assq number gnus-newsgroup-scored))
7036                                     gnus-summary-default-score))
7037                           gnus-summary-expunge-below))
7038               ;; We increase the expunge-tally here, but that has
7039               ;; nothing to do with the limits, really.
7040               (incf gnus-newsgroup-expunged-tally)
7041               ;; We also mark as read here, if that's wanted.
7042               (when (and gnus-summary-mark-below
7043                          (< score gnus-summary-mark-below))
7044                 (setq gnus-newsgroup-unreads
7045                       (delq number gnus-newsgroup-unreads))
7046                 (if gnus-newsgroup-auto-expire
7047                     (push number gnus-newsgroup-expirable)
7048                   (push (cons number gnus-low-score-mark)
7049                         gnus-newsgroup-reads)))
7050               t)
7051             ;; Check NoCeM things.
7052             (if (and gnus-use-nocem
7053                      (gnus-nocem-unwanted-article-p
7054                       (mail-header-id (car thread))))
7055                 (progn
7056                   (setq gnus-newsgroup-unreads
7057                         (delq number gnus-newsgroup-unreads))
7058                   t))))
7059           ;; Nope, invisible article.
7060           0
7061         ;; Ok, this article is to be visible, so we add it to the limit
7062         ;; and return 1.
7063         (push number gnus-newsgroup-limit)
7064         1))))
7065
7066 (defun gnus-expunge-thread (thread)
7067   "Mark all articles in THREAD as read."
7068   (let* ((number (mail-header-number (car thread))))
7069     (incf gnus-newsgroup-expunged-tally)
7070     ;; We also mark as read here, if that's wanted.
7071     (setq gnus-newsgroup-unreads
7072           (delq number gnus-newsgroup-unreads))
7073     (if gnus-newsgroup-auto-expire
7074         (push number gnus-newsgroup-expirable)
7075       (push (cons number gnus-low-score-mark)
7076             gnus-newsgroup-reads)))
7077   ;; Go recursively through all subthreads.
7078   (mapcar 'gnus-expunge-thread (cdr thread)))
7079
7080 ;; Summary article oriented commands
7081
7082 (defun gnus-summary-refer-parent-article (n)
7083   "Refer parent article N times.
7084 If N is negative, go to ancestor -N instead.
7085 The difference between N and the number of articles fetched is returned."
7086   (interactive "p")
7087   (let ((skip 1)
7088         error header ref)
7089     (when (not (natnump n))
7090       (setq skip (abs n)
7091             n 1))
7092     (while (and (> n 0)
7093                 (not error))
7094       (setq header (gnus-summary-article-header))
7095       (if (and (eq (mail-header-number header)
7096                    (cdr gnus-article-current))
7097                (equal gnus-newsgroup-name
7098                       (car gnus-article-current)))
7099           ;; If we try to find the parent of the currently
7100           ;; displayed article, then we take a look at the actual
7101           ;; References header, since this is slightly more
7102           ;; reliable than the References field we got from the
7103           ;; server.
7104           (save-excursion
7105             (set-buffer gnus-original-article-buffer)
7106             (nnheader-narrow-to-headers)
7107             (unless (setq ref (message-fetch-field "references"))
7108               (setq ref (message-fetch-field "in-reply-to")))
7109             (widen))
7110         (setq ref
7111               ;; It's not the current article, so we take a bet on
7112               ;; the value we got from the server.
7113               (mail-header-references header)))
7114       (if (and ref
7115                (not (equal ref "")))
7116           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7117             (gnus-message 1 "Couldn't find parent"))
7118         (gnus-message 1 "No references in article %d"
7119                       (gnus-summary-article-number))
7120         (setq error t))
7121       (decf n))
7122     (gnus-summary-position-point)
7123     n))
7124
7125 (defun gnus-summary-refer-references ()
7126   "Fetch all articles mentioned in the References header.
7127 Return the number of articles fetched."
7128   (interactive)
7129   (let ((ref (mail-header-references (gnus-summary-article-header)))
7130         (current (gnus-summary-article-number))
7131         (n 0))
7132     (if (or (not ref)
7133             (equal ref ""))
7134         (error "No References in the current article")
7135       ;; For each Message-ID in the References header...
7136       (while (string-match "<[^>]*>" ref)
7137         (incf n)
7138         ;; ... fetch that article.
7139         (gnus-summary-refer-article
7140          (prog1 (match-string 0 ref)
7141            (setq ref (substring ref (match-end 0))))))
7142       (gnus-summary-goto-subject current)
7143       (gnus-summary-position-point)
7144       n)))
7145
7146 (defun gnus-summary-refer-thread (&optional limit)
7147   "Fetch all articles in the current thread.
7148 If LIMIT (the numerical prefix), fetch that many old headers instead
7149 of what's specified by the `gnus-refer-thread-limit' variable."
7150   (interactive "P")
7151   (let ((id (mail-header-id (gnus-summary-article-header)))
7152         (limit (if limit (prefix-numeric-value limit)
7153                  gnus-refer-thread-limit)))
7154     ;; We want to fetch LIMIT *old* headers, but we also have to
7155     ;; re-fetch all the headers in the current buffer, because many of
7156     ;; them may be undisplayed.  So we adjust LIMIT.
7157     (when (numberp limit)
7158       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7159     (unless (eq gnus-fetch-old-headers 'invisible)
7160       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7161       ;; Retrieve the headers and read them in.
7162       (if (eq (gnus-retrieve-headers
7163                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7164               'nov)
7165           (gnus-build-all-threads)
7166         (error "Can't fetch thread from backends that don't support NOV"))
7167       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7168     (gnus-summary-limit-include-thread id)))
7169
7170 (defun gnus-summary-refer-article (message-id)
7171   "Fetch an article specified by MESSAGE-ID."
7172   (interactive "sMessage-ID: ")
7173   (when (and (stringp message-id)
7174              (not (zerop (length message-id))))
7175     ;; Construct the correct Message-ID if necessary.
7176     ;; Suggested by tale@pawl.rpi.edu.
7177     (unless (string-match "^<" message-id)
7178       (setq message-id (concat "<" message-id)))
7179     (unless (string-match ">$" message-id)
7180       (setq message-id (concat message-id ">")))
7181     (let* ((header (gnus-id-to-header message-id))
7182            (sparse (and header
7183                         (gnus-summary-article-sparse-p
7184                          (mail-header-number header))
7185                         (memq (mail-header-number header)
7186                               gnus-newsgroup-limit)))
7187            number)
7188       (cond
7189        ;; If the article is present in the buffer we just go to it.
7190        ((and header
7191              (or (not (gnus-summary-article-sparse-p
7192                        (mail-header-number header)))
7193                  sparse))
7194         (prog1
7195             (gnus-summary-goto-article
7196              (mail-header-number header) nil t)
7197           (when sparse
7198             (gnus-summary-update-article (mail-header-number header)))))
7199        (t
7200         ;; We fetch the article.
7201         (catch 'found
7202           (dolist (gnus-override-method (gnus-refer-article-methods))
7203             (gnus-check-server gnus-override-method)
7204             ;; Fetch the header, and display the article.
7205             (when (setq number (gnus-summary-insert-subject message-id))
7206               (gnus-summary-select-article nil nil nil number)
7207               (throw 'found t)))
7208           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7209
7210 (defun gnus-refer-article-methods ()
7211   "Return a list of referrable methods."
7212   (cond
7213    ;; No method, so we default to current and native.
7214    ((null gnus-refer-article-method)
7215     (list gnus-current-select-method gnus-select-method))
7216    ;; Current.
7217    ((eq 'current gnus-refer-article-method)
7218     (list gnus-current-select-method))
7219    ;; List of select methods.
7220    ((not (and (symbolp (car gnus-refer-article-method))
7221               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7222     (let (out)
7223       (dolist (method gnus-refer-article-method)
7224         (push (if (eq 'current method)
7225                   gnus-current-select-method
7226                 method)
7227               out))
7228       (nreverse out)))
7229    ;; One single select method.
7230    (t
7231     (list gnus-refer-article-method))))
7232
7233 (defun gnus-summary-edit-parameters ()
7234   "Edit the group parameters of the current group."
7235   (interactive)
7236   (gnus-group-edit-group gnus-newsgroup-name 'params))
7237
7238 (defun gnus-summary-customize-parameters ()
7239   "Customize the group parameters of the current group."
7240   (interactive)
7241   (gnus-group-customize gnus-newsgroup-name))
7242
7243 (defun gnus-summary-enter-digest-group (&optional force)
7244   "Enter an nndoc group based on the current article.
7245 If FORCE, force a digest interpretation.  If not, try
7246 to guess what the document format is."
7247   (interactive "P")
7248   (let ((conf gnus-current-window-configuration))
7249     (save-excursion
7250       (gnus-summary-select-article))
7251     (setq gnus-current-window-configuration conf)
7252     (let* ((name (format "%s-%d"
7253                          (gnus-group-prefixed-name
7254                           gnus-newsgroup-name (list 'nndoc ""))
7255                          (save-excursion
7256                            (set-buffer gnus-summary-buffer)
7257                            gnus-current-article)))
7258            (ogroup gnus-newsgroup-name)
7259            (params (append (gnus-info-params (gnus-get-info ogroup))
7260                            (list (cons 'to-group ogroup))
7261                            (list (cons 'save-article-group ogroup))))
7262            (case-fold-search t)
7263            (buf (current-buffer))
7264            dig to-address)
7265       (save-excursion
7266         (set-buffer gnus-original-article-buffer)
7267         ;; Have the digest group inherit the main mail address of
7268         ;; the parent article.
7269         (when (setq to-address (or (message-fetch-field "reply-to")
7270                                    (message-fetch-field "from")))
7271           (setq params (append
7272                         (list (cons 'to-address
7273                                     (funcall gnus-decode-encoded-word-function
7274                                              to-address))))))
7275         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7276         (insert-buffer-substring gnus-original-article-buffer)
7277         ;; Remove lines that may lead nndoc to misinterpret the
7278         ;; document type.
7279         (narrow-to-region
7280          (goto-char (point-min))
7281          (or (search-forward "\n\n" nil t) (point)))
7282         (goto-char (point-min))
7283         (delete-matching-lines "^Path:\\|^From ")
7284         (widen))
7285       (unwind-protect
7286           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7287                     (gnus-newsgroup-ephemeral-ignored-charsets
7288                      gnus-newsgroup-ignored-charsets))
7289                 (gnus-group-read-ephemeral-group
7290                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7291                               (nndoc-article-type
7292                                ,(if force 'mbox 'guess))) t))
7293               ;; Make all postings to this group go to the parent group.
7294               (nconc (gnus-info-params (gnus-get-info name))
7295                      params)
7296             ;; Couldn't select this doc group.
7297             (switch-to-buffer buf)
7298             (gnus-set-global-variables)
7299             (gnus-configure-windows 'summary)
7300             (gnus-message 3 "Article couldn't be entered?"))
7301         (kill-buffer dig)))))
7302
7303 (defun gnus-summary-read-document (n)
7304   "Open a new group based on the current article(s).
7305 This will allow you to read digests and other similar
7306 documents as newsgroups.
7307 Obeys the standard process/prefix convention."
7308   (interactive "P")
7309   (let* ((articles (gnus-summary-work-articles n))
7310          (ogroup gnus-newsgroup-name)
7311          (params (append (gnus-info-params (gnus-get-info ogroup))
7312                          (list (cons 'to-group ogroup))))
7313          article group egroup groups vgroup)
7314     (while (setq article (pop articles))
7315       (setq group (format "%s-%d" gnus-newsgroup-name article))
7316       (gnus-summary-remove-process-mark article)
7317       (when (gnus-summary-display-article article)
7318         (save-excursion
7319           (with-temp-buffer
7320             (insert-buffer-substring gnus-original-article-buffer)
7321             ;; Remove some headers that may lead nndoc to make
7322             ;; the wrong guess.
7323             (message-narrow-to-head)
7324             (goto-char (point-min))
7325             (delete-matching-lines "^\\(Path\\):\\|^From ")
7326             (widen)
7327             (if (setq egroup
7328                       (gnus-group-read-ephemeral-group
7329                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7330                                      (nndoc-article-type guess))
7331                        t nil t))
7332                 (progn
7333                   ;; Make all postings to this group go to the parent group.
7334                   (nconc (gnus-info-params (gnus-get-info egroup))
7335                          params)
7336                   (push egroup groups))
7337               ;; Couldn't select this doc group.
7338               (gnus-error 3 "Article couldn't be entered"))))))
7339     ;; Now we have selected all the documents.
7340     (cond
7341      ((not groups)
7342       (error "None of the articles could be interpreted as documents"))
7343      ((gnus-group-read-ephemeral-group
7344        (setq vgroup (format
7345                      "nnvirtual:%s-%s" gnus-newsgroup-name
7346                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7347        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7348        t
7349        (cons (current-buffer) 'summary)))
7350      (t
7351       (error "Couldn't select virtual nndoc group")))))
7352
7353 (defun gnus-summary-isearch-article (&optional regexp-p)
7354   "Do incremental search forward on the current article.
7355 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7356   (interactive "P")
7357   (let* ((gnus-inhibit-treatment t)
7358          (old (gnus-summary-select-article)))
7359     (gnus-configure-windows 'article)
7360     (gnus-eval-in-buffer-window gnus-article-buffer
7361       (save-restriction
7362         (widen)
7363         (when (eq 'old old)
7364           (gnus-article-show-all-headers))
7365         (goto-char (point-min))
7366         (isearch-forward regexp-p)))))
7367
7368 (defun gnus-summary-search-article-forward (regexp &optional backward)
7369   "Search for an article containing REGEXP forward.
7370 If BACKWARD, search backward instead."
7371   (interactive
7372    (list (read-string
7373           (format "Search article %s (regexp%s): "
7374                   (if current-prefix-arg "backward" "forward")
7375                   (if gnus-last-search-regexp
7376                       (concat ", default " gnus-last-search-regexp)
7377                     "")))
7378          current-prefix-arg))
7379   (if (string-equal regexp "")
7380       (setq regexp (or gnus-last-search-regexp ""))
7381     (setq gnus-last-search-regexp regexp)
7382     (setq gnus-article-before-search gnus-current-article))
7383   ;; Intentionally set gnus-last-article.
7384   (setq gnus-last-article gnus-article-before-search)
7385   (let ((gnus-last-article gnus-last-article))
7386     (if (gnus-summary-search-article regexp backward)
7387         (gnus-summary-show-thread)
7388       (error "Search failed: \"%s\"" regexp))))
7389
7390 (defun gnus-summary-search-article-backward (regexp)
7391   "Search for an article containing REGEXP backward."
7392   (interactive
7393    (list (read-string
7394           (format "Search article backward (regexp%s): "
7395                   (if gnus-last-search-regexp
7396                       (concat ", default " gnus-last-search-regexp)
7397                     "")))))
7398   (gnus-summary-search-article-forward regexp 'backward))
7399
7400 (eval-when-compile
7401   (defmacro gnus-summary-search-article-position-point (regexp backward)
7402     "Dehighlight the last matched text and goto the beginning position."
7403     (` (if (and gnus-summary-search-article-matched-data
7404                 (let ((text (caddr gnus-summary-search-article-matched-data))
7405                       (inhibit-read-only t)
7406                       buffer-read-only)
7407                   (delete-region
7408                    (goto-char (car gnus-summary-search-article-matched-data))
7409                    (cadr gnus-summary-search-article-matched-data))
7410                   (insert text)
7411                   (string-match (, regexp) text)))
7412            (if (, backward) (beginning-of-line) (end-of-line))
7413          (goto-char (if (, backward) (point-max) (point-min))))))
7414
7415   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7416     "Place point where X-Face image is displayed."
7417     (if (featurep 'xemacs)
7418         (` (let ((end (if (search-forward "\n\n" nil t)
7419                           (goto-char (1- (point)))
7420                         (point-min)))
7421                  extent)
7422              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7423              (unless (and (re-search-forward "^From:" end t)
7424                           (setq extent (extent-at (point)))
7425                           (extent-begin-glyph extent))
7426                (goto-char (, opoint)))))
7427       (` (let ((end (if (search-forward "\n\n" nil t)
7428                         (goto-char (1- (point)))
7429                       (point-min)))
7430                (start (or (search-backward "\n\n" nil t) (point-min))))
7431            (goto-char
7432             (or (text-property-any start end 'x-face-image t);; x-face-e21
7433                 (text-property-any start end 'x-face-mule-bitmap-image t)
7434                 (, opoint)))))))
7435
7436   (defmacro gnus-summary-search-article-highlight-matched-text
7437     (backward treated x-face)
7438     "Highlight matched text in the function `gnus-summary-search-article'."
7439     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7440              (end (set-marker (make-marker) (match-end 0)))
7441              (inhibit-read-only t)
7442              buffer-read-only)
7443          (unless treated
7444            (let ((,@
7445                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7446                     (mapcar
7447                      (lambda (item) (setq items (delq item items)))
7448                      '(gnus-treat-buttonize
7449                        gnus-treat-fill-article
7450                        gnus-treat-fill-long-lines
7451                        gnus-treat-emphasize
7452                        gnus-treat-highlight-headers
7453                        gnus-treat-highlight-citation
7454                        gnus-treat-highlight-signature
7455                        gnus-treat-overstrike
7456                        gnus-treat-display-xface
7457                        gnus-treat-buttonize-head
7458                        gnus-treat-decode-article-as-default-mime-charset))
7459                     (static-if (featurep 'xemacs)
7460                         items
7461                       (cons '(x-face-mule-delete-x-face-field
7462                               (quote never))
7463                             items))))
7464                  (gnus-treat-display-xface
7465                   (when (, x-face) gnus-treat-display-xface)))
7466              (gnus-article-prepare-mime-display)))
7467          (goto-char (if (, backward) start end))
7468          (when (, x-face)
7469            (gnus-summary-search-article-highlight-goto-x-face (point)))
7470          (setq gnus-summary-search-article-matched-data
7471                (list start end (buffer-substring start end)))
7472          (unless (eq start end);; matched text has been deleted. :-<
7473            (put-text-property start end 'face
7474                               (or (find-face 'isearch)
7475                                   'secondary-selection))))))
7476   )
7477
7478 (defun gnus-summary-search-article (regexp &optional backward)
7479   "Search for an article containing REGEXP.
7480 Optional argument BACKWARD means do search for backward.
7481 `gnus-select-article-hook' is not called during the search."
7482   ;; We have to require this here to make sure that the following
7483   ;; dynamic binding isn't shadowed by autoloading.
7484   (require 'gnus-async)
7485   (require 'gnus-art)
7486   (let ((gnus-select-article-hook nil)  ;Disable hook.
7487         (gnus-article-prepare-hook nil)
7488         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7489         (gnus-use-article-prefetch nil)
7490         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7491         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7492         (sum (current-buffer))
7493         (found nil)
7494         point treated)
7495     (gnus-save-hidden-threads
7496       (static-if (featurep 'xemacs)
7497           (let ((gnus-inhibit-treatment t))
7498             (setq treated (eq 'old (gnus-summary-select-article)))
7499             (when (and treated
7500                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7501                                  (window-live-p (get-buffer-window
7502                                                  gnus-article-buffer t)))))
7503               (gnus-summary-select-article nil t)
7504               (setq treated nil)))
7505         (let ((gnus-inhibit-treatment t)
7506               (x-face-mule-delete-x-face-field 'never))
7507           (setq treated (eq 'old (gnus-summary-select-article)))
7508           (when (and treated
7509                      (not
7510                       (and (gnus-buffer-live-p gnus-article-buffer)
7511                            (window-live-p (get-buffer-window
7512                                            gnus-article-buffer t))
7513                            (or (not (string-match "^\\^X-Face:" regexp))
7514                                (with-current-buffer gnus-article-buffer
7515                                  gnus-summary-search-article-matched-data)))))
7516             (gnus-summary-select-article nil t)
7517             (setq treated nil))))
7518       (set-buffer gnus-article-buffer)
7519       (widen)
7520       (if treated
7521           (progn
7522             (gnus-article-show-all-headers)
7523             (gnus-summary-search-article-position-point regexp backward))
7524         (goto-char (if backward (point-max) (point-min))))
7525       (while (not found)
7526         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7527         (if (if backward
7528                 (re-search-backward regexp nil t)
7529               (re-search-forward regexp nil t))
7530             ;; We found the regexp.
7531             (progn
7532               (gnus-summary-search-article-highlight-matched-text
7533                backward treated (string-match "^\\^X-Face:" regexp))
7534               (setq found 'found)
7535               (forward-line
7536                (/ (- 2 (window-height
7537                         (get-buffer-window gnus-article-buffer t)))
7538                   2))
7539               (set-window-start
7540                (get-buffer-window (current-buffer))
7541                (point))
7542               (set-buffer sum)
7543               (setq point (point)))
7544           ;; We didn't find it, so we go to the next article.
7545           (set-buffer sum)
7546           (setq found 'not)
7547           (while (eq found 'not)
7548             (if (not (if backward (gnus-summary-find-prev)
7549                        (gnus-summary-find-next)))
7550                 ;; No more articles.
7551                 (setq found t)
7552               ;; Select the next article and adjust point.
7553               (unless (gnus-summary-article-sparse-p
7554                        (gnus-summary-article-number))
7555                 (setq found nil)
7556                 (let ((gnus-inhibit-treatment t))
7557                   (gnus-summary-select-article))
7558                 (setq treated nil)
7559                 (set-buffer gnus-article-buffer)
7560                 (widen)
7561                 (goto-char (if backward (point-max) (point-min))))))))
7562       (gnus-message 7 ""))
7563     ;; Return whether we found the regexp.
7564     (when (eq found 'found)
7565       (goto-char point)
7566       (gnus-summary-show-thread)
7567       (gnus-summary-goto-subject gnus-current-article)
7568       (gnus-summary-position-point)
7569       t)))
7570
7571 (defun gnus-find-matching-articles (header regexp)
7572   "Return a list of all articles that match REGEXP on HEADER.
7573 This search includes all articles in the current group that Gnus has
7574 fetched headers for, whether they are displayed or not."
7575   (let ((articles nil)
7576         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7577         (case-fold-search t))
7578     (dolist (header gnus-newsgroup-headers)
7579       (when (string-match regexp (funcall func header))
7580         (push (mail-header-number header) articles)))
7581     (nreverse articles)))
7582
7583 (defun gnus-summary-find-matching (header regexp &optional backward unread
7584                                           not-case-fold)
7585   "Return a list of all articles that match REGEXP on HEADER.
7586 The search stars on the current article and goes forwards unless
7587 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7588 If UNREAD is non-nil, only unread articles will
7589 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7590 in the comparisons."
7591   (let ((case-fold-search (not not-case-fold))
7592         articles d func)
7593     (if (consp header)
7594         (if (eq (car header) 'extra)
7595             (setq func
7596                   `(lambda (h)
7597                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7598                          "")))
7599           (error "%s is an invalid header" header))
7600       (unless (fboundp (intern (concat "mail-header-" header)))
7601         (error "%s is not a valid header" header))
7602       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7603     (dolist (d (if (eq backward 'all)
7604                    gnus-newsgroup-data
7605                  (gnus-data-find-list
7606                   (gnus-summary-article-number)
7607                   (gnus-data-list backward))))
7608       (when (and (or (not unread)       ; We want all articles...
7609                      (gnus-data-unread-p d)) ; Or just unreads.
7610                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7611                  (string-match regexp
7612                                (funcall func (gnus-data-header d)))) ; Match.
7613         (push (gnus-data-number d) articles))) ; Success!
7614     (nreverse articles)))
7615
7616 (defun gnus-summary-execute-command (header regexp command &optional backward)
7617   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7618 If HEADER is an empty string (or nil), the match is done on the entire
7619 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7620   (interactive
7621    (list (let ((completion-ignore-case t))
7622            (completing-read
7623             "Header name: "
7624             (mapcar (lambda (header) (list (format "%s" header)))
7625                     (append
7626                      '("Number" "Subject" "From" "Lines" "Date"
7627                        "Message-ID" "Xref" "References" "Body")
7628                      gnus-extra-headers))
7629             nil 'require-match))
7630          (read-string "Regexp: ")
7631          (read-key-sequence "Command: ")
7632          current-prefix-arg))
7633   (when (equal header "Body")
7634     (setq header ""))
7635   ;; Hidden thread subtrees must be searched as well.
7636   (gnus-summary-show-all-threads)
7637   ;; We don't want to change current point nor window configuration.
7638   (save-excursion
7639     (save-window-excursion
7640       (gnus-message 6 "Executing %s..." (key-description command))
7641       ;; We'd like to execute COMMAND interactively so as to give arguments.
7642       (gnus-execute header regexp
7643                     `(call-interactively ',(key-binding command))
7644                     backward)
7645       (gnus-message 6 "Executing %s...done" (key-description command)))))
7646
7647 (defun gnus-summary-beginning-of-article ()
7648   "Scroll the article back to the beginning."
7649   (interactive)
7650   (gnus-summary-select-article)
7651   (gnus-configure-windows 'article)
7652   (gnus-eval-in-buffer-window gnus-article-buffer
7653     (widen)
7654     (goto-char (point-min))
7655     (when gnus-page-broken
7656       (gnus-narrow-to-page))))
7657
7658 (defun gnus-summary-end-of-article ()
7659   "Scroll to the end of the article."
7660   (interactive)
7661   (gnus-summary-select-article)
7662   (gnus-configure-windows 'article)
7663   (gnus-eval-in-buffer-window gnus-article-buffer
7664     (widen)
7665     (goto-char (point-max))
7666     (recenter -3)
7667     (when gnus-page-broken
7668       (gnus-narrow-to-page))))
7669
7670 (defun gnus-summary-print-article (&optional filename n)
7671   "Generate and print a PostScript image of the N next (mail) articles.
7672
7673 If N is negative, print the N previous articles.  If N is nil and articles
7674 have been marked with the process mark, print these instead.
7675
7676 If the optional first argument FILENAME is nil, send the image to the
7677 printer.  If FILENAME is a string, save the PostScript image in a file with
7678 that name.  If FILENAME is a number, prompt the user for the name of the file
7679 to save in."
7680   (interactive (list (ps-print-preprint current-prefix-arg)))
7681   (dolist (article (gnus-summary-work-articles n))
7682     (gnus-summary-select-article nil nil 'pseudo article)
7683     (gnus-eval-in-buffer-window gnus-article-buffer
7684       (let ((buffer (generate-new-buffer " *print*")))
7685         (unwind-protect
7686             (progn
7687               (copy-to-buffer buffer (point-min) (point-max))
7688               (set-buffer buffer)
7689               (gnus-article-delete-invisible-text)
7690               (when (gnus-visual-p 'article-highlight 'highlight)
7691                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7692                 ;; highlight.
7693                 (let ((gnus-article-buffer buffer))
7694                   (gnus-article-highlight-citation t)
7695                   (gnus-article-highlight-signature)))
7696               (let ((ps-left-header
7697                      (list
7698                       (concat "("
7699                               (mail-header-subject gnus-current-headers) ")")
7700                       (concat "("
7701                               (mail-header-from gnus-current-headers) ")")))
7702                     (ps-right-header
7703                      (list
7704                       "/pagenumberstring load"
7705                       (concat "("
7706                               (mail-header-date gnus-current-headers) ")"))))
7707                 (gnus-run-hooks 'gnus-ps-print-hook)
7708                 (save-excursion
7709                   (if window-system
7710                       (ps-spool-buffer-with-faces)
7711                     (ps-spool-buffer)))))
7712           (kill-buffer buffer))))
7713     (gnus-summary-remove-process-mark article))
7714   (ps-despool filename))
7715
7716 (defun gnus-summary-show-article (&optional arg)
7717   "Force re-fetching of the current article.
7718 If ARG (the prefix) is a number, show the article with the charset
7719 defined in `gnus-summary-show-article-charset-alist', or the charset
7720 inputed.
7721 If ARG (the prefix) is non-nil and not a number, show the raw article
7722 without any article massaging functions being run."
7723   (interactive "P")
7724   (cond
7725    ((numberp arg)
7726     (let ((gnus-newsgroup-charset
7727            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7728                (read-coding-system "Charset: ")))
7729           (gnus-newsgroup-ignored-charsets 'gnus-all))
7730       (gnus-summary-select-article nil 'force)
7731       (let ((deps gnus-newsgroup-dependencies)
7732             head header)
7733         (save-excursion
7734           (set-buffer gnus-original-article-buffer)
7735           (save-restriction
7736             (message-narrow-to-head)
7737             (setq head (buffer-string)))
7738           (with-temp-buffer
7739             (insert (format "211 %d Article retrieved.\n"
7740                             (cdr gnus-article-current)))
7741             (insert head)
7742             (insert ".\n")
7743             (let ((nntp-server-buffer (current-buffer)))
7744               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7745         (gnus-data-set-header
7746          (gnus-data-find (cdr gnus-article-current))
7747          header)
7748         (gnus-summary-update-article-line
7749          (cdr gnus-article-current) header))))
7750    ((not arg)
7751     ;; Select the article the normal way.
7752     (gnus-summary-select-article nil 'force))
7753    (t
7754     ;; We have to require this here to make sure that the following
7755     ;; dynamic binding isn't shadowed by autoloading.
7756     (require 'gnus-async)
7757     (require 'gnus-art)
7758     ;; Bind the article treatment functions to nil.
7759     (let ((gnus-have-all-headers t)
7760           gnus-article-prepare-hook
7761           gnus-article-decode-hook
7762           gnus-break-pages
7763           gnus-show-mime
7764           (gnus-inhibit-treatment t))
7765       (gnus-summary-select-article nil 'force))))
7766   (gnus-summary-goto-subject gnus-current-article)
7767   (gnus-summary-position-point))
7768
7769 (defun gnus-summary-verbose-headers (&optional arg)
7770   "Toggle permanent full header display.
7771 If ARG is a positive number, turn header display on.
7772 If ARG is a negative number, turn header display off."
7773   (interactive "P")
7774   (setq gnus-show-all-headers
7775         (cond ((or (not (numberp arg))
7776                    (zerop arg))
7777                (not gnus-show-all-headers))
7778               ((natnump arg)
7779                t)))
7780   (gnus-summary-show-article))
7781
7782 (defun gnus-summary-toggle-header (&optional arg)
7783   "Show the headers if they are hidden, or hide them if they are shown.
7784 If ARG is a positive number, show the entire header.
7785 If ARG is a negative number, hide the unwanted header lines."
7786   (interactive "P")
7787   (save-excursion
7788     (set-buffer gnus-article-buffer)
7789     (save-restriction
7790       (let* ((buffer-read-only nil)
7791              (inhibit-point-motion-hooks t)
7792              hidden e)
7793         (setq hidden
7794               (if (numberp arg)
7795                   (>= arg 0)
7796                 (save-restriction
7797                   (article-narrow-to-head)
7798                   (gnus-article-hidden-text-p 'headers))))
7799         (goto-char (point-min))
7800         (when (search-forward "\n\n" nil t)
7801           (delete-region (point-min) (1- (point))))
7802         (goto-char (point-min))
7803         (save-excursion
7804           (set-buffer gnus-original-article-buffer)
7805           (goto-char (point-min))
7806           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7807         (insert-buffer-substring gnus-original-article-buffer 1 e)
7808         (save-restriction
7809           (narrow-to-region (point-min) (point))
7810           (article-decode-encoded-words)
7811           (if  hidden
7812               (let ((gnus-treat-hide-headers nil)
7813                     (gnus-treat-hide-boring-headers nil))
7814                 (setq gnus-article-wash-types
7815                       (delq 'headers gnus-article-wash-types))
7816                 (gnus-treat-article 'head))
7817             (gnus-treat-article 'head)))
7818         (gnus-set-mode-line 'article)))))
7819
7820 (defun gnus-summary-show-all-headers ()
7821   "Make all header lines visible."
7822   (interactive)
7823   (gnus-summary-toggle-header 1))
7824
7825 (defun gnus-summary-toggle-mime (&optional arg)
7826   "Toggle MIME processing.
7827 If ARG is a positive number, turn MIME processing on."
7828   (interactive "P")
7829   (setq gnus-show-mime
7830         (if (null arg)
7831             (not gnus-show-mime)
7832           (> (prefix-numeric-value arg) 0)))
7833   (gnus-summary-select-article t 'force))
7834
7835 (defun gnus-summary-caesar-message (&optional arg)
7836   "Caesar rotate the current article by 13.
7837 The numerical prefix specifies how many places to rotate each letter
7838 forward."
7839   (interactive "P")
7840   (gnus-summary-select-article)
7841   (let ((mail-header-separator ""))
7842     (gnus-eval-in-buffer-window gnus-article-buffer
7843       (save-restriction
7844         (widen)
7845         (let ((start (window-start))
7846               buffer-read-only)
7847           (message-caesar-buffer-body arg)
7848           (set-window-start (get-buffer-window (current-buffer)) start))))))
7849
7850 (defun gnus-summary-stop-page-breaking ()
7851   "Stop page breaking in the current article."
7852   (interactive)
7853   (gnus-summary-select-article)
7854   (gnus-eval-in-buffer-window gnus-article-buffer
7855     (widen)
7856     (when (gnus-visual-p 'page-marker)
7857       (let ((buffer-read-only nil))
7858         (gnus-remove-text-with-property 'gnus-prev)
7859         (gnus-remove-text-with-property 'gnus-next))
7860       (setq gnus-page-broken nil))))
7861
7862 (defun gnus-summary-move-article (&optional n to-newsgroup
7863                                             select-method action)
7864   "Move the current article to a different newsgroup.
7865 If N is a positive number, move the N next articles.
7866 If N is a negative number, move the N previous articles.
7867 If N is nil and any articles have been marked with the process mark,
7868 move those articles instead.
7869 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7870 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7871 re-spool using this method.
7872
7873 For this function to work, both the current newsgroup and the
7874 newsgroup that you want to move to have to support the `request-move'
7875 and `request-accept' functions.
7876
7877 ACTION can be either `move' (the default), `crosspost' or `copy'."
7878   (interactive "P")
7879   (unless action
7880     (setq action 'move))
7881   ;; Disable marking as read.
7882   (let (gnus-mark-article-hook)
7883     (save-window-excursion
7884       (gnus-summary-select-article)))
7885   ;; Check whether the source group supports the required functions.
7886   (cond ((and (eq action 'move)
7887               (not (gnus-check-backend-function
7888                     'request-move-article gnus-newsgroup-name)))
7889          (error "The current group does not support article moving"))
7890         ((and (eq action 'crosspost)
7891               (not (gnus-check-backend-function
7892                     'request-replace-article gnus-newsgroup-name)))
7893          (error "The current group does not support article editing")))
7894   (let ((articles (gnus-summary-work-articles n))
7895         (prefix (if (gnus-check-backend-function
7896                     'request-move-article gnus-newsgroup-name)
7897                     (gnus-group-real-prefix gnus-newsgroup-name)
7898                   ""))
7899         (names '((move "Move" "Moving")
7900                  (copy "Copy" "Copying")
7901                  (crosspost "Crosspost" "Crossposting")))
7902         (copy-buf (save-excursion
7903                     (nnheader-set-temp-buffer " *copy article*")))
7904         (default-marks gnus-article-mark-lists)
7905         (no-expire-marks (delete '(expirable . expire)
7906                                  (copy-sequence gnus-article-mark-lists)))
7907         art-group to-method new-xref article to-groups)
7908     (unless (assq action names)
7909       (error "Unknown action %s" action))
7910     ;; Read the newsgroup name.
7911     (when (and (not to-newsgroup)
7912                (not select-method))
7913       (setq to-newsgroup
7914             (gnus-read-move-group-name
7915              (cadr (assq action names))
7916              (symbol-value (intern (format "gnus-current-%s-group" action)))
7917              articles prefix))
7918       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7919     (setq to-method (or select-method
7920                         (gnus-server-to-method
7921                          (gnus-group-method to-newsgroup))))
7922     ;; Check the method we are to move this article to...
7923     (unless (gnus-check-backend-function
7924              'request-accept-article (car to-method))
7925       (error "%s does not support article copying" (car to-method)))
7926     (unless (gnus-check-server to-method)
7927       (error "Can't open server %s" (car to-method)))
7928     (gnus-message 6 "%s to %s: %s..."
7929                   (caddr (assq action names))
7930                   (or (car select-method) to-newsgroup) articles)
7931     (while articles
7932       (setq article (pop articles))
7933       (setq
7934        art-group
7935        (cond
7936         ;; Move the article.
7937         ((eq action 'move)
7938          ;; Remove this article from future suppression.
7939          (gnus-dup-unsuppress-article article)
7940          (gnus-request-move-article
7941           article                       ; Article to move
7942           gnus-newsgroup-name           ; From newsgroup
7943           (nth 1 (gnus-find-method-for-group
7944                   gnus-newsgroup-name)) ; Server
7945           (list 'gnus-request-accept-article
7946                 to-newsgroup (list 'quote select-method)
7947                 (not articles) t)       ; Accept form
7948           (not articles)))              ; Only save nov last time
7949         ;; Copy the article.
7950         ((eq action 'copy)
7951          (save-excursion
7952            (set-buffer copy-buf)
7953            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7954              (gnus-request-accept-article
7955               to-newsgroup select-method (not articles) t))))
7956         ;; Crosspost the article.
7957         ((eq action 'crosspost)
7958          (let ((xref (message-tokenize-header
7959                       (mail-header-xref (gnus-summary-article-header article))
7960                       " ")))
7961            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7962                                   ":" article))
7963            (unless xref
7964              (setq xref (list (system-name))))
7965            (setq new-xref
7966                  (concat
7967                   (mapconcat 'identity
7968                              (delete "Xref:" (delete new-xref xref))
7969                              " ")
7970                   " " new-xref))
7971            (save-excursion
7972              (set-buffer copy-buf)
7973              ;; First put the article in the destination group.
7974              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7975              (when (consp (setq art-group
7976                                 (gnus-request-accept-article
7977                                  to-newsgroup select-method (not articles))))
7978                (setq new-xref (concat new-xref " " (car art-group)
7979                                       ":" (cdr art-group)))
7980                ;; Now we have the new Xrefs header, so we insert
7981                ;; it and replace the new article.
7982                (nnheader-replace-header "Xref" new-xref)
7983                (gnus-request-replace-article
7984                 (cdr art-group) to-newsgroup (current-buffer))
7985                art-group))))))
7986       (cond
7987        ((not art-group)
7988         (gnus-message 1 "Couldn't %s article %s: %s"
7989                       (cadr (assq action names)) article
7990                       (nnheader-get-report (car to-method))))
7991        ((eq art-group 'junk)
7992         (when (eq action 'move)
7993           (gnus-summary-mark-article article gnus-canceled-mark)
7994           (gnus-message 4 "Deleted article %s" article)))
7995        (t
7996         (let* ((pto-group (gnus-group-prefixed-name
7997                            (car art-group) to-method))
7998                (entry
7999                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8000                (info (nth 2 entry))
8001                (to-group (gnus-info-group info))
8002                to-marks)
8003           ;; Update the group that has been moved to.
8004           (when (and info
8005                      (memq action '(move copy)))
8006             (unless (member to-group to-groups)
8007               (push to-group to-groups))
8008
8009             (unless (memq article gnus-newsgroup-unreads)
8010               (push 'read to-marks)
8011               (gnus-info-set-read
8012                info (gnus-add-to-range (gnus-info-read info)
8013                                        (list (cdr art-group)))))
8014
8015             ;; See whether the article is to be put in the cache.
8016             (let ((marks (if (gnus-group-auto-expirable-p to-group)
8017                              default-marks
8018                            no-expire-marks))
8019                   (to-article (cdr art-group)))
8020
8021               ;; Enter the article into the cache in the new group,
8022               ;; if that is required.
8023               (when gnus-use-cache
8024                 (gnus-cache-possibly-enter-article
8025                  to-group to-article
8026                  (let ((header (copy-sequence
8027                                 (gnus-summary-article-header article))))
8028                    (mail-header-set-number header to-article)
8029                    header)
8030                  (memq article gnus-newsgroup-marked)
8031                  (memq article gnus-newsgroup-dormant)
8032                  (memq article gnus-newsgroup-unreads)))
8033
8034               (when gnus-preserve-marks
8035                 ;; Copy any marks over to the new group.
8036                 (when (and (equal to-group gnus-newsgroup-name)
8037                            (not (memq article gnus-newsgroup-unreads)))
8038                   ;; Mark this article as read in this group.
8039                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8040                   (setcdr (gnus-active to-group) to-article)
8041                   (setcdr gnus-newsgroup-active to-article))
8042
8043                 (while marks
8044                   (when (memq article (symbol-value
8045                                        (intern (format "gnus-newsgroup-%s"
8046                                                        (caar marks)))))
8047                     (push (cdar marks) to-marks)
8048                     ;; If the other group is the same as this group,
8049                     ;; then we have to add the mark to the list.
8050                     (when (equal to-group gnus-newsgroup-name)
8051                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8052                            (cons to-article
8053                                  (symbol-value
8054                                   (intern (format "gnus-newsgroup-%s"
8055                                                   (caar marks)))))))
8056                     ;; Copy the marks to other group.
8057                     (gnus-add-marked-articles
8058                      to-group (cdar marks) (list to-article) info))
8059                   (setq marks (cdr marks)))
8060
8061                 (gnus-request-set-mark to-group (list (list (list to-article)
8062                                                             'set
8063                                                             to-marks))))
8064
8065               (gnus-dribble-enter
8066                (concat "(gnus-group-set-info '"
8067                        (gnus-prin1-to-string (gnus-get-info to-group))
8068                        ")"))))
8069
8070           ;; Update the Xref header in this article to point to
8071           ;; the new crossposted article we have just created.
8072           (when (eq action 'crosspost)
8073             (save-excursion
8074               (set-buffer copy-buf)
8075               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8076               (nnheader-replace-header "Xref" new-xref)
8077               (gnus-request-replace-article
8078                article gnus-newsgroup-name (current-buffer)))))
8079
8080         ;;;!!!Why is this necessary?
8081         (set-buffer gnus-summary-buffer)
8082
8083         (gnus-summary-goto-subject article)
8084         (when (eq action 'move)
8085           (gnus-summary-mark-article article gnus-canceled-mark))))
8086       (gnus-summary-remove-process-mark article))
8087     ;; Re-activate all groups that have been moved to.
8088     (while to-groups
8089       (save-excursion
8090         (set-buffer gnus-group-buffer)
8091         (when (gnus-group-goto-group (car to-groups) t)
8092           (gnus-group-get-new-news-this-group 1 t))
8093         (pop to-groups)))
8094
8095     (gnus-kill-buffer copy-buf)
8096     (gnus-summary-position-point)
8097     (gnus-set-mode-line 'summary)))
8098
8099 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8100   "Move the current article to a different newsgroup.
8101 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8102 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8103 re-spool using this method."
8104   (interactive "P")
8105   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8106
8107 (defun gnus-summary-crosspost-article (&optional n)
8108   "Crosspost the current article to some other group."
8109   (interactive "P")
8110   (gnus-summary-move-article n nil nil 'crosspost))
8111
8112 (defcustom gnus-summary-respool-default-method nil
8113   "Default method for respooling an article.
8114 If nil, use to the current newsgroup method."
8115   :type '(choice (gnus-select-method :value (nnml ""))
8116                  (const nil))
8117   :group 'gnus-summary-mail)
8118
8119 (defun gnus-summary-respool-article (&optional n method)
8120   "Respool the current article.
8121 The article will be squeezed through the mail spooling process again,
8122 which means that it will be put in some mail newsgroup or other
8123 depending on `nnmail-split-methods'.
8124 If N is a positive number, respool the N next articles.
8125 If N is a negative number, respool the N previous articles.
8126 If N is nil and any articles have been marked with the process mark,
8127 respool those articles instead.
8128
8129 Respooling can be done both from mail groups and \"real\" newsgroups.
8130 In the former case, the articles in question will be moved from the
8131 current group into whatever groups they are destined to.  In the
8132 latter case, they will be copied into the relevant groups."
8133   (interactive
8134    (list current-prefix-arg
8135          (let* ((methods (gnus-methods-using 'respool))
8136                 (methname
8137                  (symbol-name (or gnus-summary-respool-default-method
8138                                   (car (gnus-find-method-for-group
8139                                         gnus-newsgroup-name)))))
8140                 (method
8141                  (gnus-completing-read
8142                   methname "What backend do you want to use when respooling?"
8143                   methods nil t nil 'gnus-mail-method-history))
8144                 ms)
8145            (cond
8146             ((zerop (length (setq ms (gnus-servers-using-backend
8147                                       (intern method)))))
8148              (list (intern method) ""))
8149             ((= 1 (length ms))
8150              (car ms))
8151             (t
8152              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8153                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8154                            ms-alist))))))))
8155   (unless method
8156     (error "No method given for respooling"))
8157   (if (assoc (symbol-name
8158               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8159              (gnus-methods-using 'respool))
8160       (gnus-summary-move-article n nil method)
8161     (gnus-summary-copy-article n nil method)))
8162
8163 (defun gnus-summary-import-article (file &optional edit)
8164   "Import an arbitrary file into a mail newsgroup."
8165   (interactive "fImport file: \nP")
8166   (let ((group gnus-newsgroup-name)
8167         (now (current-time))
8168         atts lines group-art)
8169     (unless (gnus-check-backend-function 'request-accept-article group)
8170       (error "%s does not support article importing" group))
8171     (or (file-readable-p file)
8172         (not (file-regular-p file))
8173         (error "Can't read %s" file))
8174     (save-excursion
8175       (set-buffer (gnus-get-buffer-create " *import file*"))
8176       (erase-buffer)
8177       (nnheader-insert-file-contents file)
8178       (goto-char (point-min))
8179       (unless (nnheader-article-p)
8180         ;; This doesn't look like an article, so we fudge some headers.
8181         (setq atts (file-attributes file)
8182               lines (count-lines (point-min) (point-max)))
8183         (insert "From: " (read-string "From: ") "\n"
8184                 "Subject: " (read-string "Subject: ") "\n"
8185                 "Date: " (message-make-date (nth 5 atts)) "\n"
8186                 "Message-ID: " (message-make-message-id) "\n"
8187                 "Lines: " (int-to-string lines) "\n"
8188                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8189       (setq group-art (gnus-request-accept-article group nil t))
8190       (kill-buffer (current-buffer)))
8191     (setq gnus-newsgroup-active (gnus-activate-group group))
8192     (forward-line 1)
8193     (gnus-summary-goto-article (cdr group-art) nil t)
8194     (when edit
8195       (gnus-summary-edit-article))))
8196
8197 (defun gnus-summary-create-article ()
8198   "Create an article in a mail newsgroup."
8199   (interactive)
8200   (let ((group gnus-newsgroup-name)
8201         (now (current-time))
8202         group-art)
8203     (unless (gnus-check-backend-function 'request-accept-article group)
8204       (error "%s does not support article importing" group))
8205     (save-excursion
8206       (set-buffer (gnus-get-buffer-create " *import file*"))
8207       (erase-buffer)
8208       (goto-char (point-min))
8209       ;; This doesn't look like an article, so we fudge some headers.
8210       (insert "From: " (read-string "From: ") "\n"
8211               "Subject: " (read-string "Subject: ") "\n"
8212               "Date: " (message-make-date now) "\n"
8213               "Message-ID: " (message-make-message-id) "\n")
8214       (setq group-art (gnus-request-accept-article group nil t))
8215       (kill-buffer (current-buffer)))
8216     (setq gnus-newsgroup-active (gnus-activate-group group))
8217     (forward-line 1)
8218     (gnus-summary-goto-article (cdr group-art) nil t)
8219     (gnus-summary-edit-article)))
8220
8221 (defun gnus-summary-article-posted-p ()
8222   "Say whether the current (mail) article is available from news as well.
8223 This will be the case if the article has both been mailed and posted."
8224   (interactive)
8225   (let ((id (mail-header-references (gnus-summary-article-header)))
8226         (gnus-override-method (car (gnus-refer-article-methods))))
8227     (if (gnus-request-head id "")
8228         (gnus-message 2 "The current message was found on %s"
8229                       gnus-override-method)
8230       (gnus-message 2 "The current message couldn't be found on %s"
8231                     gnus-override-method)
8232       nil)))
8233
8234 (defun gnus-summary-expire-articles (&optional now)
8235   "Expire all articles that are marked as expirable in the current group."
8236   (interactive)
8237   (when (gnus-check-backend-function
8238          'request-expire-articles gnus-newsgroup-name)
8239     ;; This backend supports expiry.
8240     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8241            (expirable (if total
8242                           (progn
8243                             ;; We need to update the info for
8244                             ;; this group for `gnus-list-of-read-articles'
8245                             ;; to give us the right answer.
8246                             (gnus-run-hooks 'gnus-exit-group-hook)
8247                             (gnus-summary-update-info)
8248                             (gnus-list-of-read-articles gnus-newsgroup-name))
8249                         (setq gnus-newsgroup-expirable
8250                               (sort gnus-newsgroup-expirable '<))))
8251            (expiry-wait (if now 'immediate
8252                           (gnus-group-find-parameter
8253                            gnus-newsgroup-name 'expiry-wait)))
8254            (nnmail-expiry-target
8255             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8256                 nnmail-expiry-target))
8257            es)
8258       (when expirable
8259         ;; There are expirable articles in this group, so we run them
8260         ;; through the expiry process.
8261         (gnus-message 6 "Expiring articles...")
8262         (unless (gnus-check-group gnus-newsgroup-name)
8263           (error "Can't open server for %s" gnus-newsgroup-name))
8264         ;; The list of articles that weren't expired is returned.
8265         (save-excursion
8266           (if expiry-wait
8267               (let ((nnmail-expiry-wait-function nil)
8268                     (nnmail-expiry-wait expiry-wait))
8269                 (setq es (gnus-request-expire-articles
8270                           expirable gnus-newsgroup-name)))
8271             (setq es (gnus-request-expire-articles
8272                       expirable gnus-newsgroup-name)))
8273           (unless total
8274             (setq gnus-newsgroup-expirable es))
8275           ;; We go through the old list of expirable, and mark all
8276           ;; really expired articles as nonexistent.
8277           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8278             (let ((gnus-use-cache nil))
8279               (while expirable
8280                 (unless (memq (car expirable) es)
8281                   (when (gnus-data-find (car expirable))
8282                     (gnus-summary-mark-article
8283                      (car expirable) gnus-canceled-mark)))
8284                 (setq expirable (cdr expirable))))))
8285         (gnus-message 6 "Expiring articles...done")))))
8286
8287 (defun gnus-summary-expire-articles-now ()
8288   "Expunge all expirable articles in the current group.
8289 This means that *all* articles that are marked as expirable will be
8290 deleted forever, right now."
8291   (interactive)
8292   (or gnus-expert-user
8293       (gnus-yes-or-no-p
8294        "Are you really, really, really sure you want to delete all these messages? ")
8295       (error "Phew!"))
8296   (gnus-summary-expire-articles t))
8297
8298 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8299 (defun gnus-summary-delete-article (&optional n)
8300   "Delete the N next (mail) articles.
8301 This command actually deletes articles.  This is not a marking
8302 command.  The article will disappear forever from your life, never to
8303 return.
8304 If N is negative, delete backwards.
8305 If N is nil and articles have been marked with the process mark,
8306 delete these instead."
8307   (interactive "P")
8308   (unless (gnus-check-backend-function 'request-expire-articles
8309                                        gnus-newsgroup-name)
8310     (error "The current newsgroup does not support article deletion"))
8311   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8312     (error "Couldn't open server"))
8313   ;; Compute the list of articles to delete.
8314   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8315         not-deleted)
8316     (if (and gnus-novice-user
8317              (not (gnus-yes-or-no-p
8318                    (format "Do you really want to delete %s forever? "
8319                            (if (> (length articles) 1)
8320                                (format "these %s articles" (length articles))
8321                              "this article")))))
8322         ()
8323       ;; Delete the articles.
8324       (setq not-deleted (gnus-request-expire-articles
8325                          articles gnus-newsgroup-name 'force))
8326       (while articles
8327         (gnus-summary-remove-process-mark (car articles))
8328         ;; The backend might not have been able to delete the article
8329         ;; after all.
8330         (unless (memq (car articles) not-deleted)
8331           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8332         (setq articles (cdr articles)))
8333       (when not-deleted
8334         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8335     (gnus-summary-position-point)
8336     (gnus-set-mode-line 'summary)
8337     not-deleted))
8338
8339 (defun gnus-summary-edit-article (&optional force)
8340   "Edit the current article.
8341 This will have permanent effect only in mail groups.
8342 If FORCE is non-nil, allow editing of articles even in read-only
8343 groups."
8344   (interactive "P")
8345   (save-excursion
8346     (set-buffer gnus-summary-buffer)
8347     (let ((mail-parse-charset gnus-newsgroup-charset)
8348           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8349       (gnus-set-global-variables)
8350       (when (and (not force)
8351                  (gnus-group-read-only-p))
8352         (error "The current newsgroup does not support article editing"))
8353       (gnus-summary-show-article t)
8354       (gnus-article-edit-article
8355        'ignore
8356        `(lambda (no-highlight)
8357           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8358                 (message-options message-options)
8359                 (message-options-set-recipient)
8360                 (mail-parse-ignored-charsets
8361                  ',gnus-newsgroup-ignored-charsets))
8362             (gnus-summary-edit-article-done
8363              ,(or (mail-header-references gnus-current-headers) "")
8364              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8365
8366 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8367
8368 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8369                                                  no-highlight)
8370   "Make edits to the current article permanent."
8371   (interactive)
8372   (save-excursion
8373     ;; The buffer restriction contains the entire article if it exists.
8374     (when (article-goto-body)
8375       (let ((lines (count-lines (point) (point-max)))
8376             (length (- (point-max) (point)))
8377             (case-fold-search t)
8378             (body (copy-marker (point))))
8379         (goto-char (point-min))
8380         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8381           (delete-region (match-beginning 1) (match-end 1))
8382           (insert (number-to-string length)))
8383         (goto-char (point-min))
8384         (when (re-search-forward
8385                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8386           (delete-region (match-beginning 1) (match-end 1))
8387           (insert (number-to-string length)))
8388         (goto-char (point-min))
8389         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8390           (delete-region (match-beginning 1) (match-end 1))
8391           (insert (number-to-string lines))))))
8392   ;; Replace the article.
8393   (let ((buf (current-buffer)))
8394     (with-temp-buffer
8395       (insert-buffer-substring buf)
8396
8397       (if (and (not read-only)
8398                (not (gnus-request-replace-article
8399                      (cdr gnus-article-current) (car gnus-article-current)
8400                      (current-buffer) t)))
8401           (error "Couldn't replace article")
8402         ;; Update the summary buffer.
8403         (if (and references
8404                  (equal (message-tokenize-header references " ")
8405                         (message-tokenize-header
8406                          (or (message-fetch-field "references") "") " ")))
8407             ;; We only have to update this line.
8408             (save-excursion
8409               (save-restriction
8410                 (message-narrow-to-head)
8411                 (let ((head (buffer-string))
8412                       header)
8413                   (with-temp-buffer
8414                     (insert (format "211 %d Article retrieved.\n"
8415                                     (cdr gnus-article-current)))
8416                     (insert head)
8417                     (insert ".\n")
8418                     (let ((nntp-server-buffer (current-buffer)))
8419                       (setq header (car (gnus-get-newsgroup-headers
8420                                          (save-excursion
8421                                            (set-buffer gnus-summary-buffer)
8422                                            gnus-newsgroup-dependencies)
8423                                          t))))
8424                     (save-excursion
8425                       (set-buffer gnus-summary-buffer)
8426                       (gnus-data-set-header
8427                        (gnus-data-find (cdr gnus-article-current))
8428                        header)
8429                       (gnus-summary-update-article-line
8430                        (cdr gnus-article-current) header))))))
8431           ;; Update threads.
8432           (set-buffer (or buffer gnus-summary-buffer))
8433           (gnus-summary-update-article (cdr gnus-article-current)))
8434         ;; Prettify the article buffer again.
8435         (unless no-highlight
8436           (save-excursion
8437             (set-buffer gnus-article-buffer)
8438             ;;;!!! Fix this -- article should be rehighlighted.
8439             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8440             (set-buffer gnus-original-article-buffer)
8441             (gnus-request-article
8442              (cdr gnus-article-current)
8443              (car gnus-article-current) (current-buffer))))
8444         ;; Prettify the summary buffer line.
8445         (when (gnus-visual-p 'summary-highlight 'highlight)
8446           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8447
8448 (defun gnus-summary-edit-wash (key)
8449   "Perform editing command KEY in the article buffer."
8450   (interactive
8451    (list
8452     (progn
8453       (message "%s" (concat (this-command-keys) "- "))
8454       (read-char))))
8455   (message "")
8456   (gnus-summary-edit-article)
8457   (execute-kbd-macro (concat (this-command-keys) key))
8458   (gnus-article-edit-done))
8459
8460 ;;; Respooling
8461
8462 (defun gnus-summary-respool-query (&optional silent trace)
8463   "Query where the respool algorithm would put this article."
8464   (interactive)
8465   (let (gnus-mark-article-hook)
8466     (gnus-summary-select-article)
8467     (save-excursion
8468       (set-buffer gnus-original-article-buffer)
8469       (save-restriction
8470         (message-narrow-to-head)
8471         (let ((groups (nnmail-article-group 'identity trace)))
8472           (unless silent
8473             (if groups
8474                 (message "This message would go to %s"
8475                          (mapconcat 'car groups ", "))
8476               (message "This message would go to no groups"))
8477             groups))))))
8478
8479 (defun gnus-summary-respool-trace ()
8480   "Trace where the respool algorithm would put this article.
8481 Display a buffer showing all fancy splitting patterns which matched."
8482   (interactive)
8483   (gnus-summary-respool-query nil t))
8484
8485 ;; Summary marking commands.
8486
8487 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8488   "Mark articles which has the same subject as read, and then select the next.
8489 If UNMARK is positive, remove any kind of mark.
8490 If UNMARK is negative, tick articles."
8491   (interactive "P")
8492   (when unmark
8493     (setq unmark (prefix-numeric-value unmark)))
8494   (let ((count
8495          (gnus-summary-mark-same-subject
8496           (gnus-summary-article-subject) unmark)))
8497     ;; Select next unread article.  If auto-select-same mode, should
8498     ;; select the first unread article.
8499     (gnus-summary-next-article t (and gnus-auto-select-same
8500                                       (gnus-summary-article-subject)))
8501     (gnus-message 7 "%d article%s marked as %s"
8502                   count (if (= count 1) " is" "s are")
8503                   (if unmark "unread" "read"))))
8504
8505 (defun gnus-summary-kill-same-subject (&optional unmark)
8506   "Mark articles which has the same subject as read.
8507 If UNMARK is positive, remove any kind of mark.
8508 If UNMARK is negative, tick articles."
8509   (interactive "P")
8510   (when unmark
8511     (setq unmark (prefix-numeric-value unmark)))
8512   (let ((count
8513          (gnus-summary-mark-same-subject
8514           (gnus-summary-article-subject) unmark)))
8515     ;; If marked as read, go to next unread subject.
8516     (when (null unmark)
8517       ;; Go to next unread subject.
8518       (gnus-summary-next-subject 1 t))
8519     (gnus-message 7 "%d articles are marked as %s"
8520                   count (if unmark "unread" "read"))))
8521
8522 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8523   "Mark articles with same SUBJECT as read, and return marked number.
8524 If optional argument UNMARK is positive, remove any kinds of marks.
8525 If optional argument UNMARK is negative, mark articles as unread instead."
8526   (let ((count 1))
8527     (save-excursion
8528       (cond
8529        ((null unmark)                   ; Mark as read.
8530         (while (and
8531                 (progn
8532                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8533                   (gnus-summary-show-thread) t)
8534                 (gnus-summary-find-subject subject))
8535           (setq count (1+ count))))
8536        ((> unmark 0)                    ; Tick.
8537         (while (and
8538                 (progn
8539                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8540                   (gnus-summary-show-thread) t)
8541                 (gnus-summary-find-subject subject))
8542           (setq count (1+ count))))
8543        (t                               ; Mark as unread.
8544         (while (and
8545                 (progn
8546                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8547                   (gnus-summary-show-thread) t)
8548                 (gnus-summary-find-subject subject))
8549           (setq count (1+ count)))))
8550       (gnus-set-mode-line 'summary)
8551       ;; Return the number of marked articles.
8552       count)))
8553
8554 (defun gnus-summary-mark-as-processable (n &optional unmark)
8555   "Set the process mark on the next N articles.
8556 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8557 the process mark instead.  The difference between N and the actual
8558 number of articles marked is returned."
8559   (interactive "P")
8560   (if (and (null n) (gnus-region-active-p))
8561       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8562     (setq n (prefix-numeric-value n))
8563     (let ((backward (< n 0))
8564           (n (abs n)))
8565       (while (and
8566               (> n 0)
8567               (if unmark
8568                 (gnus-summary-remove-process-mark
8569                  (gnus-summary-article-number))
8570                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8571               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8572         (setq n (1- n)))
8573       (when (/= 0 n)
8574         (gnus-message 7 "No more articles"))
8575       (gnus-summary-recenter)
8576       (gnus-summary-position-point)
8577       n)))
8578
8579 (defun gnus-summary-unmark-as-processable (n)
8580   "Remove the process mark from the next N articles.
8581 If N is negative, unmark backward instead.  The difference between N and
8582 the actual number of articles unmarked is returned."
8583   (interactive "P")
8584   (gnus-summary-mark-as-processable n t))
8585
8586 (defun gnus-summary-unmark-all-processable ()
8587   "Remove the process mark from all articles."
8588   (interactive)
8589   (save-excursion
8590     (while gnus-newsgroup-processable
8591       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8592   (gnus-summary-position-point))
8593
8594 (defun gnus-summary-add-mark (article type)
8595   "Mark ARTICLE with a mark of TYPE."
8596   (let ((vtype (car (assq type gnus-article-mark-lists)))
8597         var)
8598     (if (not vtype)
8599         (error "No such mark type: %s" type)
8600       (setq var (intern (format "gnus-newsgroup-%s" type)))
8601       (set var (cons article (symbol-value var)))
8602       (if (memq type '(processable cached replied forwarded saved))
8603           (gnus-summary-update-secondary-mark article)
8604         ;;; !!! This is bobus.  We should find out what primary
8605         ;;; !!! mark we want to set.
8606         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8607
8608 (defun gnus-summary-mark-as-expirable (n)
8609   "Mark N articles forward as expirable.
8610 If N is negative, mark backward instead.  The difference between N and
8611 the actual number of articles marked is returned."
8612   (interactive "p")
8613   (gnus-summary-mark-forward n gnus-expirable-mark))
8614
8615 (defun gnus-summary-mark-article-as-replied (article)
8616   "Mark ARTICLE as replied to and update the summary line.
8617 ARTICLE can also be a list of articles."
8618   (interactive (list (gnus-summary-article-number)))
8619   (let ((articles (if (listp article) article (list article))))
8620     (dolist (article articles)
8621       (push article gnus-newsgroup-replied)
8622       (let ((buffer-read-only nil))
8623         (when (gnus-summary-goto-subject article nil t)
8624           (gnus-summary-update-secondary-mark article))))))
8625
8626 (defun gnus-summary-mark-article-as-forwarded (article)
8627   "Mark ARTICLE as forwarded and update the summary line.
8628 ARTICLE can also be a list of articles."
8629   (let ((articles (if (listp article) article (list article))))
8630     (dolist (article articles)
8631       (push article gnus-newsgroup-forwarded)
8632       (let ((buffer-read-only nil))
8633         (when (gnus-summary-goto-subject article nil t)
8634           (gnus-summary-update-secondary-mark article))))))
8635
8636 (defun gnus-summary-set-bookmark (article)
8637   "Set a bookmark in current article."
8638   (interactive (list (gnus-summary-article-number)))
8639   (when (or (not (get-buffer gnus-article-buffer))
8640             (not gnus-current-article)
8641             (not gnus-article-current)
8642             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8643     (error "No current article selected"))
8644   ;; Remove old bookmark, if one exists.
8645   (let ((old (assq article gnus-newsgroup-bookmarks)))
8646     (when old
8647       (setq gnus-newsgroup-bookmarks
8648             (delq old gnus-newsgroup-bookmarks))))
8649   ;; Set the new bookmark, which is on the form
8650   ;; (article-number . line-number-in-body).
8651   (push
8652    (cons article
8653          (save-excursion
8654            (set-buffer gnus-article-buffer)
8655            (count-lines
8656             (min (point)
8657                  (save-excursion
8658                    (goto-char (point-min))
8659                    (search-forward "\n\n" nil t)
8660                    (point)))
8661             (point))))
8662    gnus-newsgroup-bookmarks)
8663   (gnus-message 6 "A bookmark has been added to the current article."))
8664
8665 (defun gnus-summary-remove-bookmark (article)
8666   "Remove the bookmark from the current article."
8667   (interactive (list (gnus-summary-article-number)))
8668   ;; Remove old bookmark, if one exists.
8669   (let ((old (assq article gnus-newsgroup-bookmarks)))
8670     (if old
8671         (progn
8672           (setq gnus-newsgroup-bookmarks
8673                 (delq old gnus-newsgroup-bookmarks))
8674           (gnus-message 6 "Removed bookmark."))
8675       (gnus-message 6 "No bookmark in current article."))))
8676
8677 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8678 (defun gnus-summary-mark-as-dormant (n)
8679   "Mark N articles forward as dormant.
8680 If N is negative, mark backward instead.  The difference between N and
8681 the actual number of articles marked is returned."
8682   (interactive "p")
8683   (gnus-summary-mark-forward n gnus-dormant-mark))
8684
8685 (defun gnus-summary-set-process-mark (article)
8686   "Set the process mark on ARTICLE and update the summary line."
8687   (setq gnus-newsgroup-processable
8688         (cons article
8689               (delq article gnus-newsgroup-processable)))
8690   (when (gnus-summary-goto-subject article)
8691     (gnus-summary-show-thread)
8692     (gnus-summary-goto-subject article)
8693     (gnus-summary-update-secondary-mark article)))
8694
8695 (defun gnus-summary-remove-process-mark (article)
8696   "Remove the process mark from ARTICLE and update the summary line."
8697   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8698   (when (gnus-summary-goto-subject article)
8699     (gnus-summary-show-thread)
8700     (gnus-summary-goto-subject article)
8701     (gnus-summary-update-secondary-mark article)))
8702
8703 (defun gnus-summary-set-saved-mark (article)
8704   "Set the process mark on ARTICLE and update the summary line."
8705   (push article gnus-newsgroup-saved)
8706   (when (gnus-summary-goto-subject article)
8707     (gnus-summary-update-secondary-mark article)))
8708
8709 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8710   "Mark N articles as read forwards.
8711 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8712 The difference between N and the actual number of articles marked is
8713 returned.
8714 Iff NO-EXPIRE, auto-expiry will be inhibited."
8715   (interactive "p")
8716   (gnus-summary-show-thread)
8717   (let ((backward (< n 0))
8718         (gnus-summary-goto-unread
8719          (and gnus-summary-goto-unread
8720               (not (eq gnus-summary-goto-unread 'never))
8721               (not (memq mark (list gnus-unread-mark
8722                                     gnus-ticked-mark gnus-dormant-mark)))))
8723         (n (abs n))
8724         (mark (or mark gnus-del-mark)))
8725     (while (and (> n 0)
8726                 (gnus-summary-mark-article nil mark no-expire)
8727                 (zerop (gnus-summary-next-subject
8728                         (if backward -1 1)
8729                         (and gnus-summary-goto-unread
8730                              (not (eq gnus-summary-goto-unread 'never)))
8731                         t)))
8732       (setq n (1- n)))
8733     (when (/= 0 n)
8734       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8735     (gnus-summary-recenter)
8736     (gnus-summary-position-point)
8737     (gnus-set-mode-line 'summary)
8738     n))
8739
8740 (defun gnus-summary-mark-article-as-read (mark)
8741   "Mark the current article quickly as read with MARK."
8742   (let ((article (gnus-summary-article-number)))
8743     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8744     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8745     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8746     (push (cons article mark) gnus-newsgroup-reads)
8747     ;; Possibly remove from cache, if that is used.
8748     (when gnus-use-cache
8749       (gnus-cache-enter-remove-article article))
8750     ;; Allow the backend to change the mark.
8751     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8752     ;; Check for auto-expiry.
8753     (when (and gnus-newsgroup-auto-expire
8754                (memq mark gnus-auto-expirable-marks))
8755       (setq mark gnus-expirable-mark)
8756       ;; Let the backend know about the mark change.
8757       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8758       (push article gnus-newsgroup-expirable))
8759     ;; Set the mark in the buffer.
8760     (gnus-summary-update-mark mark 'unread)
8761     t))
8762
8763 (defun gnus-summary-mark-article-as-unread (mark)
8764   "Mark the current article quickly as unread with MARK."
8765   (let* ((article (gnus-summary-article-number))
8766          (old-mark (gnus-summary-article-mark article)))
8767     ;; Allow the backend to change the mark.
8768     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8769     (if (eq mark old-mark)
8770         t
8771       (if (<= article 0)
8772           (progn
8773             (gnus-error 1 "Can't mark negative article numbers")
8774             nil)
8775         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8776         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8777         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8778         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8779         (cond ((= mark gnus-ticked-mark)
8780                (push article gnus-newsgroup-marked))
8781               ((= mark gnus-dormant-mark)
8782                (push article gnus-newsgroup-dormant))
8783               (t
8784                (push article gnus-newsgroup-unreads)))
8785         (gnus-pull article gnus-newsgroup-reads)
8786
8787         ;; See whether the article is to be put in the cache.
8788         (and gnus-use-cache
8789              (vectorp (gnus-summary-article-header article))
8790              (save-excursion
8791                (gnus-cache-possibly-enter-article
8792                 gnus-newsgroup-name article
8793                 (gnus-summary-article-header article)
8794                 (= mark gnus-ticked-mark)
8795                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8796
8797         ;; Fix the mark.
8798         (gnus-summary-update-mark mark 'unread)
8799         t))))
8800
8801 (defun gnus-summary-mark-article (&optional article mark no-expire)
8802   "Mark ARTICLE with MARK.  MARK can be any character.
8803 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8804 `??' (dormant) and `?E' (expirable).
8805 If MARK is nil, then the default character `?r' is used.
8806 If ARTICLE is nil, then the article on the current line will be
8807 marked.
8808 Iff NO-EXPIRE, auto-expiry will be inhibited."
8809   ;; The mark might be a string.
8810   (when (stringp mark)
8811     (setq mark (aref mark 0)))
8812   ;; If no mark is given, then we check auto-expiring.
8813   (when (null mark)
8814     (setq mark gnus-del-mark))
8815   (when (and (not no-expire)
8816              gnus-newsgroup-auto-expire
8817              (memq mark gnus-auto-expirable-marks))
8818     (setq mark gnus-expirable-mark))
8819   (let ((article (or article (gnus-summary-article-number)))
8820         (old-mark (gnus-summary-article-mark article)))
8821     ;; Allow the backend to change the mark.
8822     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8823     (if (eq mark old-mark)
8824         t
8825       (unless article
8826         (error "No article on current line"))
8827       (if (not (if (or (= mark gnus-unread-mark)
8828                        (= mark gnus-ticked-mark)
8829                        (= mark gnus-dormant-mark))
8830                    (gnus-mark-article-as-unread article mark)
8831                  (gnus-mark-article-as-read article mark)))
8832           t
8833         ;; See whether the article is to be put in the cache.
8834         (and gnus-use-cache
8835              (not (= mark gnus-canceled-mark))
8836              (vectorp (gnus-summary-article-header article))
8837              (save-excursion
8838                (gnus-cache-possibly-enter-article
8839                 gnus-newsgroup-name article
8840                 (gnus-summary-article-header article)
8841                 (= mark gnus-ticked-mark)
8842                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8843
8844         (when (gnus-summary-goto-subject article nil t)
8845           (let ((buffer-read-only nil))
8846             (gnus-summary-show-thread)
8847             ;; Fix the mark.
8848             (gnus-summary-update-mark mark 'unread)
8849             t))))))
8850
8851 (defun gnus-summary-update-secondary-mark (article)
8852   "Update the secondary (read, process, cache) mark."
8853   (gnus-summary-update-mark
8854    (cond ((memq article gnus-newsgroup-processable)
8855           gnus-process-mark)
8856          ((memq article gnus-newsgroup-cached)
8857           gnus-cached-mark)
8858          ((memq article gnus-newsgroup-replied)
8859           gnus-replied-mark)
8860          ((memq article gnus-newsgroup-forwarded)
8861           gnus-forwarded-mark)
8862          ((memq article gnus-newsgroup-saved)
8863           gnus-saved-mark)
8864          (t gnus-no-mark))
8865    'replied)
8866   (when (gnus-visual-p 'summary-highlight 'highlight)
8867     (gnus-run-hooks 'gnus-summary-update-hook))
8868   t)
8869
8870 (defun gnus-summary-update-mark (mark type)
8871   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8872         (buffer-read-only nil))
8873     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8874     (when forward
8875       (when (looking-at "\r")
8876         (incf forward))
8877       (when (<= (+ forward (point)) (point-max))
8878         ;; Go to the right position on the line.
8879         (goto-char (+ forward (point)))
8880         ;; Replace the old mark with the new mark.
8881         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8882         ;; Optionally update the marks by some user rule.
8883         (when (eq type 'unread)
8884           (gnus-data-set-mark
8885            (gnus-data-find (gnus-summary-article-number)) mark)
8886           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8887
8888 (defun gnus-mark-article-as-read (article &optional mark)
8889   "Enter ARTICLE in the pertinent lists and remove it from others."
8890   ;; Make the article expirable.
8891   (let ((mark (or mark gnus-del-mark)))
8892     (if (= mark gnus-expirable-mark)
8893         (push article gnus-newsgroup-expirable)
8894       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8895     ;; Remove from unread and marked lists.
8896     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8897     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8898     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8899     (push (cons article mark) gnus-newsgroup-reads)
8900     ;; Possibly remove from cache, if that is used.
8901     (when gnus-use-cache
8902       (gnus-cache-enter-remove-article article))
8903     t))
8904
8905 (defun gnus-mark-article-as-unread (article &optional mark)
8906   "Enter ARTICLE in the pertinent lists and remove it from others."
8907   (let ((mark (or mark gnus-ticked-mark)))
8908     (if (<= article 0)
8909         (progn
8910           (gnus-error 1 "Can't mark negative article numbers")
8911           nil)
8912       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8913             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8914             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8915             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8916
8917       ;; Unsuppress duplicates?
8918       (when gnus-suppress-duplicates
8919         (gnus-dup-unsuppress-article article))
8920
8921       (cond ((= mark gnus-ticked-mark)
8922              (push article gnus-newsgroup-marked))
8923             ((= mark gnus-dormant-mark)
8924              (push article gnus-newsgroup-dormant))
8925             (t
8926              (push article gnus-newsgroup-unreads)))
8927       (gnus-pull article gnus-newsgroup-reads)
8928       t)))
8929
8930 (defalias 'gnus-summary-mark-as-unread-forward
8931   'gnus-summary-tick-article-forward)
8932 (make-obsolete 'gnus-summary-mark-as-unread-forward
8933                'gnus-summary-tick-article-forward)
8934 (defun gnus-summary-tick-article-forward (n)
8935   "Tick N articles forwards.
8936 If N is negative, tick backwards instead.
8937 The difference between N and the number of articles ticked is returned."
8938   (interactive "p")
8939   (gnus-summary-mark-forward n gnus-ticked-mark))
8940
8941 (defalias 'gnus-summary-mark-as-unread-backward
8942   'gnus-summary-tick-article-backward)
8943 (make-obsolete 'gnus-summary-mark-as-unread-backward
8944                'gnus-summary-tick-article-backward)
8945 (defun gnus-summary-tick-article-backward (n)
8946   "Tick N articles backwards.
8947 The difference between N and the number of articles ticked is returned."
8948   (interactive "p")
8949   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8950
8951 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8952 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8953 (defun gnus-summary-tick-article (&optional article clear-mark)
8954   "Mark current article as unread.
8955 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8956 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8957   (interactive)
8958   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8959                                        gnus-ticked-mark)))
8960
8961 (defun gnus-summary-mark-as-read-forward (n)
8962   "Mark N articles as read forwards.
8963 If N is negative, mark backwards instead.
8964 The difference between N and the actual number of articles marked is
8965 returned."
8966   (interactive "p")
8967   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8968
8969 (defun gnus-summary-mark-as-read-backward (n)
8970   "Mark the N articles as read backwards.
8971 The difference between N and the actual number of articles marked is
8972 returned."
8973   (interactive "p")
8974   (gnus-summary-mark-forward
8975    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8976
8977 (defun gnus-summary-mark-as-read (&optional article mark)
8978   "Mark current article as read.
8979 ARTICLE specifies the article to be marked as read.
8980 MARK specifies a string to be inserted at the beginning of the line."
8981   (gnus-summary-mark-article article mark))
8982
8983 (defun gnus-summary-clear-mark-forward (n)
8984   "Clear marks from N articles forward.
8985 If N is negative, clear backward instead.
8986 The difference between N and the number of marks cleared is returned."
8987   (interactive "p")
8988   (gnus-summary-mark-forward n gnus-unread-mark))
8989
8990 (defun gnus-summary-clear-mark-backward (n)
8991   "Clear marks from N articles backward.
8992 The difference between N and the number of marks cleared is returned."
8993   (interactive "p")
8994   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8995
8996 (defun gnus-summary-mark-unread-as-read ()
8997   "Intended to be used by `gnus-summary-mark-article-hook'."
8998   (when (memq gnus-current-article gnus-newsgroup-unreads)
8999     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9000
9001 (defun gnus-summary-mark-read-and-unread-as-read ()
9002   "Intended to be used by `gnus-summary-mark-article-hook'."
9003   (let ((mark (gnus-summary-article-mark)))
9004     (when (or (gnus-unread-mark-p mark)
9005               (gnus-read-mark-p mark))
9006       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9007
9008 (defun gnus-summary-mark-unread-as-ticked ()
9009    "Intended to be used by `gnus-summary-mark-article-hook'."
9010   (when (memq gnus-current-article gnus-newsgroup-unreads)
9011     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9012
9013 (defun gnus-summary-mark-region-as-read (point mark all)
9014   "Mark all unread articles between point and mark as read.
9015 If given a prefix, mark all articles between point and mark as read,
9016 even ticked and dormant ones."
9017   (interactive "r\nP")
9018   (save-excursion
9019     (let (article)
9020       (goto-char point)
9021       (beginning-of-line)
9022       (while (and
9023               (< (point) mark)
9024               (progn
9025                 (when (or all
9026                           (memq (setq article (gnus-summary-article-number))
9027                                 gnus-newsgroup-unreads))
9028                   (gnus-summary-mark-article article gnus-del-mark))
9029                 t)
9030               (gnus-summary-find-next))))))
9031
9032 (defun gnus-summary-mark-below (score mark)
9033   "Mark articles with score less than SCORE with MARK."
9034   (interactive "P\ncMark: ")
9035   (setq score (if score
9036                   (prefix-numeric-value score)
9037                 (or gnus-summary-default-score 0)))
9038   (save-excursion
9039     (set-buffer gnus-summary-buffer)
9040     (goto-char (point-min))
9041     (while
9042         (progn
9043           (and (< (gnus-summary-article-score) score)
9044                (gnus-summary-mark-article nil mark))
9045           (gnus-summary-find-next)))))
9046
9047 (defun gnus-summary-kill-below (&optional score)
9048   "Mark articles with score below SCORE as read."
9049   (interactive "P")
9050   (gnus-summary-mark-below score gnus-killed-mark))
9051
9052 (defun gnus-summary-clear-above (&optional score)
9053   "Clear all marks from articles with score above SCORE."
9054   (interactive "P")
9055   (gnus-summary-mark-above score gnus-unread-mark))
9056
9057 (defun gnus-summary-tick-above (&optional score)
9058   "Tick all articles with score above SCORE."
9059   (interactive "P")
9060   (gnus-summary-mark-above score gnus-ticked-mark))
9061
9062 (defun gnus-summary-mark-above (score mark)
9063   "Mark articles with score over SCORE with MARK."
9064   (interactive "P\ncMark: ")
9065   (setq score (if score
9066                   (prefix-numeric-value score)
9067                 (or gnus-summary-default-score 0)))
9068   (save-excursion
9069     (set-buffer gnus-summary-buffer)
9070     (goto-char (point-min))
9071     (while (and (progn
9072                   (when (> (gnus-summary-article-score) score)
9073                     (gnus-summary-mark-article nil mark))
9074                   t)
9075                 (gnus-summary-find-next)))))
9076
9077 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9078 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9079 (defun gnus-summary-limit-include-expunged (&optional no-error)
9080   "Display all the hidden articles that were expunged for low scores."
9081   (interactive)
9082   (let ((buffer-read-only nil))
9083     (let ((scored gnus-newsgroup-scored)
9084           headers h)
9085       (while scored
9086         (unless (gnus-summary-article-header (caar scored))
9087           (and (setq h (gnus-number-to-header (caar scored)))
9088                (< (cdar scored) gnus-summary-expunge-below)
9089                (push h headers)))
9090         (setq scored (cdr scored)))
9091       (if (not headers)
9092           (when (not no-error)
9093             (error "No expunged articles hidden"))
9094         (goto-char (point-min))
9095         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9096         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9097         (mapcar (lambda (x) (push (mail-header-number x) 
9098                                   gnus-newsgroup-limit))
9099                 headers)
9100         (gnus-summary-prepare-unthreaded (nreverse headers))
9101         (goto-char (point-min))
9102         (gnus-summary-position-point)
9103         t))))
9104
9105 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
9106   "Mark all unread articles in this newsgroup as read.
9107 If prefix argument ALL is non-nil, ticked and dormant articles will
9108 also be marked as read.
9109 If QUIETLY is non-nil, no questions will be asked.
9110 If TO-HERE is non-nil, it should be a point in the buffer.  All
9111 articles before this point will be marked as read.
9112 Note that this function will only catch up the unread article
9113 in the current summary buffer limitation.
9114 The number of articles marked as read is returned."
9115   (interactive "P")
9116   (prog1
9117       (save-excursion
9118         (when (or quietly
9119                   (not gnus-interactive-catchup) ;Without confirmation?
9120                   gnus-expert-user
9121                   (gnus-y-or-n-p
9122                    (if all
9123                        "Mark absolutely all articles as read? "
9124                      "Mark all unread articles as read? ")))
9125           (if (and not-mark
9126                    (not gnus-newsgroup-adaptive)
9127                    (not gnus-newsgroup-auto-expire)
9128                    (not gnus-suppress-duplicates)
9129                    (or (not gnus-use-cache)
9130                        (eq gnus-use-cache 'passive)))
9131               (progn
9132                 (when all
9133                   (setq gnus-newsgroup-marked nil
9134                         gnus-newsgroup-dormant nil))
9135                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9136             ;; We actually mark all articles as canceled, which we
9137             ;; have to do when using auto-expiry or adaptive scoring.
9138             (gnus-summary-show-all-threads)
9139             (when (gnus-summary-first-subject (not all) t)
9140               (while (and
9141                       (if to-here (< (point) to-here) t)
9142                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
9143                       (gnus-summary-find-next (not all) nil nil t))))
9144             (gnus-set-mode-line 'summary))
9145           t))
9146     (gnus-summary-position-point)))
9147
9148 (defun gnus-summary-catchup-to-here (&optional all)
9149   "Mark all unticked articles before the current one as read.
9150 If ALL is non-nil, also mark ticked and dormant articles as read."
9151   (interactive "P")
9152   (save-excursion
9153     (gnus-save-hidden-threads
9154       (let ((beg (point)))
9155         ;; We check that there are unread articles.
9156         (when (or all (gnus-summary-find-prev))
9157           (gnus-summary-catchup all t beg)))))
9158   (gnus-summary-position-point))
9159
9160 (defun gnus-summary-catchup-all (&optional quietly)
9161   "Mark all articles in this newsgroup as read."
9162   (interactive "P")
9163   (gnus-summary-catchup t quietly))
9164
9165 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9166   "Mark all unread articles in this group as read, then exit.
9167 If prefix argument ALL is non-nil, all articles are marked as read.
9168 If QUIETLY is non-nil, no questions will be asked."
9169   (interactive "P")
9170   (when (gnus-summary-catchup all quietly nil 'fast)
9171     ;; Select next newsgroup or exit.
9172     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9173              (eq gnus-auto-select-next 'quietly))
9174         (gnus-summary-next-group nil)
9175       (gnus-summary-exit))))
9176
9177 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9178   "Mark all articles in this newsgroup as read, and then exit."
9179   (interactive "P")
9180   (gnus-summary-catchup-and-exit t quietly))
9181
9182 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9183   "Mark all articles in this group as read and select the next group.
9184 If given a prefix, mark all articles, unread as well as ticked, as
9185 read."
9186   (interactive "P")
9187   (save-excursion
9188     (gnus-summary-catchup all))
9189   (gnus-summary-next-group))
9190
9191 ;;;
9192 ;;; with article
9193 ;;;
9194
9195 (defmacro gnus-with-article (article &rest forms)
9196   "Select ARTICLE and perform FORMS in the original article buffer.
9197 Then replace the article with the result."
9198   `(progn
9199      ;; We don't want the article to be marked as read.
9200      (let (gnus-mark-article-hook)
9201        (gnus-summary-select-article t t nil ,article))
9202      (set-buffer gnus-original-article-buffer)
9203      ,@forms
9204      (if (not (gnus-check-backend-function
9205                'request-replace-article (car gnus-article-current)))
9206          (gnus-message 5 "Read-only group; not replacing")
9207        (unless (gnus-request-replace-article
9208                 ,article (car gnus-article-current)
9209                 (current-buffer) t)
9210          (error "Couldn't replace article")))
9211      ;; The cache and backlog have to be flushed somewhat.
9212      (when gnus-keep-backlog
9213        (gnus-backlog-remove-article
9214         (car gnus-article-current) (cdr gnus-article-current)))
9215      (when gnus-use-cache
9216        (gnus-cache-update-article
9217         (car gnus-article-current) (cdr gnus-article-current)))))
9218
9219 (put 'gnus-with-article 'lisp-indent-function 1)
9220 (put 'gnus-with-article 'edebug-form-spec '(form body))
9221
9222 ;; Thread-based commands.
9223
9224 (defun gnus-summary-articles-in-thread (&optional article)
9225   "Return a list of all articles in the current thread.
9226 If ARTICLE is non-nil, return all articles in the thread that starts
9227 with that article."
9228   (let* ((article (or article (gnus-summary-article-number)))
9229          (data (gnus-data-find-list article))
9230          (top-level (gnus-data-level (car data)))
9231          (top-subject
9232           (cond ((null gnus-thread-operation-ignore-subject)
9233                  (gnus-simplify-subject-re
9234                   (mail-header-subject (gnus-data-header (car data)))))
9235                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9236                  (gnus-simplify-subject-fuzzy
9237                   (mail-header-subject (gnus-data-header (car data)))))
9238                 (t nil)))
9239          (end-point (save-excursion
9240                       (if (gnus-summary-go-to-next-thread)
9241                           (point) (point-max))))
9242          articles)
9243     (while (and data
9244                 (< (gnus-data-pos (car data)) end-point))
9245       (when (or (not top-subject)
9246                 (string= top-subject
9247                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9248                              (gnus-simplify-subject-fuzzy
9249                               (mail-header-subject
9250                                (gnus-data-header (car data))))
9251                            (gnus-simplify-subject-re
9252                             (mail-header-subject
9253                              (gnus-data-header (car data)))))))
9254         (push (gnus-data-number (car data)) articles))
9255       (unless (and (setq data (cdr data))
9256                    (> (gnus-data-level (car data)) top-level))
9257         (setq data nil)))
9258     ;; Return the list of articles.
9259     (nreverse articles)))
9260
9261 (defun gnus-summary-rethread-current ()
9262   "Rethread the thread the current article is part of."
9263   (interactive)
9264   (let* ((gnus-show-threads t)
9265          (article (gnus-summary-article-number))
9266          (id (mail-header-id (gnus-summary-article-header)))
9267          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9268     (unless id
9269       (error "No article on the current line"))
9270     (gnus-rebuild-thread id)
9271     (gnus-summary-goto-subject article)))
9272
9273 (defun gnus-summary-reparent-thread ()
9274   "Make the current article child of the marked (or previous) article.
9275
9276 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9277 is non-nil or the Subject: of both articles are the same."
9278   (interactive)
9279   (unless (not (gnus-group-read-only-p))
9280     (error "The current newsgroup does not support article editing"))
9281   (unless (<= (length gnus-newsgroup-processable) 1)
9282     (error "No more than one article may be marked"))
9283   (save-window-excursion
9284     (let ((gnus-article-buffer " *reparent*")
9285           (current-article (gnus-summary-article-number))
9286           ;; First grab the marked article, otherwise one line up.
9287           (parent-article (if (not (null gnus-newsgroup-processable))
9288                               (car gnus-newsgroup-processable)
9289                             (save-excursion
9290                               (if (eq (forward-line -1) 0)
9291                                   (gnus-summary-article-number)
9292                                 (error "Beginning of summary buffer"))))))
9293       (unless (not (eq current-article parent-article))
9294         (error "An article may not be self-referential"))
9295       (let ((message-id (mail-header-id
9296                          (gnus-summary-article-header parent-article))))
9297         (unless (and message-id (not (equal message-id "")))
9298           (error "No message-id in desired parent"))
9299         (gnus-with-article current-article
9300           (save-restriction
9301             (goto-char (point-min))
9302             (message-narrow-to-head)
9303             (if (re-search-forward "^References: " nil t)
9304                 (progn
9305                   (re-search-forward "^[^ \t]" nil t)
9306                   (forward-line -1)
9307                   (end-of-line)
9308                   (insert " " message-id))
9309               (insert "References: " message-id "\n"))))
9310         (set-buffer gnus-summary-buffer)
9311         (gnus-summary-unmark-all-processable)
9312         (gnus-summary-update-article current-article)
9313         (gnus-summary-rethread-current)
9314         (gnus-message 3 "Article %d is now the child of article %d"
9315                       current-article parent-article)))))
9316
9317 (defun gnus-summary-toggle-threads (&optional arg)
9318   "Toggle showing conversation threads.
9319 If ARG is positive number, turn showing conversation threads on."
9320   (interactive "P")
9321   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9322     (setq gnus-show-threads
9323           (if (null arg) (not gnus-show-threads)
9324             (> (prefix-numeric-value arg) 0)))
9325     (gnus-summary-prepare)
9326     (gnus-summary-goto-subject current)
9327     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9328     (gnus-summary-position-point)))
9329
9330 (defun gnus-summary-show-all-threads ()
9331   "Show all threads."
9332   (interactive)
9333   (save-excursion
9334     (let ((buffer-read-only nil))
9335       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9336   (gnus-summary-position-point))
9337
9338 (defun gnus-summary-show-thread ()
9339   "Show thread subtrees.
9340 Returns nil if no thread was there to be shown."
9341   (interactive)
9342   (let ((buffer-read-only nil)
9343         (orig (point))
9344         ;; first goto end then to beg, to have point at beg after let
9345         (end (progn (end-of-line) (point)))
9346         (beg (progn (beginning-of-line) (point))))
9347     (prog1
9348         ;; Any hidden lines here?
9349         (search-forward "\r" end t)
9350       (subst-char-in-region beg end ?\^M ?\n t)
9351       (goto-char orig)
9352       (gnus-summary-position-point))))
9353
9354 (defun gnus-summary-hide-all-threads ()
9355   "Hide all thread subtrees."
9356   (interactive)
9357   (save-excursion
9358     (goto-char (point-min))
9359     (gnus-summary-hide-thread)
9360     (while (zerop (gnus-summary-next-thread 1 t))
9361       (gnus-summary-hide-thread)))
9362   (gnus-summary-position-point))
9363
9364 (defun gnus-summary-hide-thread ()
9365   "Hide thread subtrees.
9366 Returns nil if no threads were there to be hidden."
9367   (interactive)
9368   (let ((buffer-read-only nil)
9369         (start (point))
9370         (article (gnus-summary-article-number)))
9371     (goto-char start)
9372     ;; Go forward until either the buffer ends or the subthread
9373     ;; ends.
9374     (when (and (not (eobp))
9375                (or (zerop (gnus-summary-next-thread 1 t))
9376                    (goto-char (point-max))))
9377       (prog1
9378           (if (and (> (point) start)
9379                    (search-backward "\n" start t))
9380               (progn
9381                 (subst-char-in-region start (point) ?\n ?\^M)
9382                 (gnus-summary-goto-subject article))
9383             (goto-char start)
9384             nil)))))
9385
9386 (defun gnus-summary-go-to-next-thread (&optional previous)
9387   "Go to the same level (or less) next thread.
9388 If PREVIOUS is non-nil, go to previous thread instead.
9389 Return the article number moved to, or nil if moving was impossible."
9390   (let ((level (gnus-summary-thread-level))
9391         (way (if previous -1 1))
9392         (beg (point)))
9393     (forward-line way)
9394     (while (and (not (eobp))
9395                 (< level (gnus-summary-thread-level)))
9396       (forward-line way))
9397     (if (eobp)
9398         (progn
9399           (goto-char beg)
9400           nil)
9401       (setq beg (point))
9402       (prog1
9403           (gnus-summary-article-number)
9404         (goto-char beg)))))
9405
9406 (defun gnus-summary-next-thread (n &optional silent)
9407   "Go to the same level next N'th thread.
9408 If N is negative, search backward instead.
9409 Returns the difference between N and the number of skips actually
9410 done.
9411
9412 If SILENT, don't output messages."
9413   (interactive "p")
9414   (let ((backward (< n 0))
9415         (n (abs n)))
9416     (while (and (> n 0)
9417                 (gnus-summary-go-to-next-thread backward))
9418       (decf n))
9419     (unless silent
9420       (gnus-summary-position-point))
9421     (when (and (not silent) (/= 0 n))
9422       (gnus-message 7 "No more threads"))
9423     n))
9424
9425 (defun gnus-summary-prev-thread (n)
9426   "Go to the same level previous N'th thread.
9427 Returns the difference between N and the number of skips actually
9428 done."
9429   (interactive "p")
9430   (gnus-summary-next-thread (- n)))
9431
9432 (defun gnus-summary-go-down-thread ()
9433   "Go down one level in the current thread."
9434   (let ((children (gnus-summary-article-children)))
9435     (when children
9436       (gnus-summary-goto-subject (car children)))))
9437
9438 (defun gnus-summary-go-up-thread ()
9439   "Go up one level in the current thread."
9440   (let ((parent (gnus-summary-article-parent)))
9441     (when parent
9442       (gnus-summary-goto-subject parent))))
9443
9444 (defun gnus-summary-down-thread (n)
9445   "Go down thread N steps.
9446 If N is negative, go up instead.
9447 Returns the difference between N and how many steps down that were
9448 taken."
9449   (interactive "p")
9450   (let ((up (< n 0))
9451         (n (abs n)))
9452     (while (and (> n 0)
9453                 (if up (gnus-summary-go-up-thread)
9454                   (gnus-summary-go-down-thread)))
9455       (setq n (1- n)))
9456     (gnus-summary-position-point)
9457     (when (/= 0 n)
9458       (gnus-message 7 "Can't go further"))
9459     n))
9460
9461 (defun gnus-summary-up-thread (n)
9462   "Go up thread N steps.
9463 If N is negative, go down instead.
9464 Returns the difference between N and how many steps down that were
9465 taken."
9466   (interactive "p")
9467   (gnus-summary-down-thread (- n)))
9468
9469 (defun gnus-summary-top-thread ()
9470   "Go to the top of the thread."
9471   (interactive)
9472   (while (gnus-summary-go-up-thread))
9473   (gnus-summary-article-number))
9474
9475 (defun gnus-summary-kill-thread (&optional unmark)
9476   "Mark articles under current thread as read.
9477 If the prefix argument is positive, remove any kinds of marks.
9478 If the prefix argument is negative, tick articles instead."
9479   (interactive "P")
9480   (when unmark
9481     (setq unmark (prefix-numeric-value unmark)))
9482   (let ((articles (gnus-summary-articles-in-thread)))
9483     (save-excursion
9484       ;; Expand the thread.
9485       (gnus-summary-show-thread)
9486       ;; Mark all the articles.
9487       (while articles
9488         (gnus-summary-goto-subject (car articles))
9489         (cond ((null unmark)
9490                (gnus-summary-mark-article-as-read gnus-killed-mark))
9491               ((> unmark 0)
9492                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9493               (t
9494                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9495         (setq articles (cdr articles))))
9496     ;; Hide killed subtrees.
9497     (and (null unmark)
9498          gnus-thread-hide-killed
9499          (gnus-summary-hide-thread))
9500     ;; If marked as read, go to next unread subject.
9501     (when (null unmark)
9502       ;; Go to next unread subject.
9503       (gnus-summary-next-subject 1 t)))
9504   (gnus-set-mode-line 'summary))
9505
9506 ;; Summary sorting commands
9507
9508 (defun gnus-summary-sort-by-number (&optional reverse)
9509   "Sort the summary buffer by article number.
9510 Argument REVERSE means reverse order."
9511   (interactive "P")
9512   (gnus-summary-sort 'number reverse))
9513
9514 (defun gnus-summary-sort-by-author (&optional reverse)
9515   "Sort the summary buffer by author name alphabetically.
9516 If `case-fold-search' is non-nil, case of letters is ignored.
9517 Argument REVERSE means reverse order."
9518   (interactive "P")
9519   (gnus-summary-sort 'author reverse))
9520
9521 (defun gnus-summary-sort-by-subject (&optional reverse)
9522   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9523 If `case-fold-search' is non-nil, case of letters is ignored.
9524 Argument REVERSE means reverse order."
9525   (interactive "P")
9526   (gnus-summary-sort 'subject reverse))
9527
9528 (defun gnus-summary-sort-by-date (&optional reverse)
9529   "Sort the summary buffer by date.
9530 Argument REVERSE means reverse order."
9531   (interactive "P")
9532   (gnus-summary-sort 'date reverse))
9533
9534 (defun gnus-summary-sort-by-score (&optional reverse)
9535   "Sort the summary buffer by score.
9536 Argument REVERSE means reverse order."
9537   (interactive "P")
9538   (gnus-summary-sort 'score reverse))
9539
9540 (defun gnus-summary-sort-by-lines (&optional reverse)
9541   "Sort the summary buffer by the number of lines.
9542 Argument REVERSE means reverse order."
9543   (interactive "P")
9544   (gnus-summary-sort 'lines reverse))
9545
9546 (defun gnus-summary-sort-by-chars (&optional reverse)
9547   "Sort the summary buffer by article length.
9548 Argument REVERSE means reverse order."
9549   (interactive "P")
9550   (gnus-summary-sort 'chars reverse))
9551
9552 (defun gnus-summary-sort-by-original (&optional reverse)
9553   "Sort the summary buffer using the default sorting method.
9554 Argument REVERSE means reverse order."
9555   (interactive "P")
9556   (let* ((buffer-read-only)
9557          (gnus-summary-prepare-hook nil))
9558     ;; We do the sorting by regenerating the threads.
9559     (gnus-summary-prepare)
9560     ;; Hide subthreads if needed.
9561     (when (and gnus-show-threads gnus-thread-hide-subtree)
9562       (gnus-summary-hide-all-threads))))
9563
9564 (defun gnus-summary-sort (predicate reverse)
9565   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9566   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9567          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9568          (gnus-thread-sort-functions
9569           (if (not reverse)
9570               thread
9571             `(lambda (t1 t2)
9572                (,thread t2 t1))))
9573          (gnus-sort-gathered-threads-function
9574           gnus-thread-sort-functions)
9575          (gnus-article-sort-functions
9576           (if (not reverse)
9577               article
9578             `(lambda (t1 t2)
9579                (,article t2 t1))))
9580          (buffer-read-only)
9581          (gnus-summary-prepare-hook nil))
9582     ;; We do the sorting by regenerating the threads.
9583     (gnus-summary-prepare)
9584     ;; Hide subthreads if needed.
9585     (when (and gnus-show-threads gnus-thread-hide-subtree)
9586       (gnus-summary-hide-all-threads))))
9587
9588 ;; Summary saving commands.
9589
9590 (defun gnus-summary-save-article (&optional n not-saved)
9591   "Save the current article using the default saver function.
9592 If N is a positive number, save the N next articles.
9593 If N is a negative number, save the N previous articles.
9594 If N is nil and any articles have been marked with the process mark,
9595 save those articles instead.
9596 The variable `gnus-default-article-saver' specifies the saver function."
9597   (interactive "P")
9598   (let* ((articles (gnus-summary-work-articles n))
9599          (save-buffer (save-excursion
9600                         (nnheader-set-temp-buffer " *Gnus Save*")))
9601          (num (length articles))
9602          header file)
9603     (dolist (article articles)
9604       (setq header (gnus-summary-article-header article))
9605       (if (not (vectorp header))
9606           ;; This is a pseudo-article.
9607           (if (assq 'name header)
9608               (gnus-copy-file (cdr (assq 'name header)))
9609             (gnus-message 1 "Article %d is unsaveable" article))
9610         ;; This is a real article.
9611         (save-window-excursion
9612           (gnus-summary-select-article t nil nil article))
9613         (save-excursion
9614           (set-buffer save-buffer)
9615           (erase-buffer)
9616           (insert-buffer-substring gnus-original-article-buffer))
9617         (setq file (gnus-article-save save-buffer file num))
9618         (gnus-summary-remove-process-mark article)
9619         (unless not-saved
9620           (gnus-summary-set-saved-mark article))))
9621     (gnus-kill-buffer save-buffer)
9622     (gnus-summary-position-point)
9623     (gnus-set-mode-line 'summary)
9624     n))
9625
9626 (defun gnus-summary-pipe-output (&optional arg)
9627   "Pipe the current article to a subprocess.
9628 If N is a positive number, pipe the N next articles.
9629 If N is a negative number, pipe the N previous articles.
9630 If N is nil and any articles have been marked with the process mark,
9631 pipe those articles instead."
9632   (interactive "P")
9633   (require 'gnus-art)
9634   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9635     (gnus-summary-save-article arg t))
9636   (gnus-configure-windows 'pipe))
9637
9638 (defun gnus-summary-save-article-mail (&optional arg)
9639   "Append the current article to an mail file.
9640 If N is a positive number, save the N next articles.
9641 If N is a negative number, save the N previous articles.
9642 If N is nil and any articles have been marked with the process mark,
9643 save those articles instead."
9644   (interactive "P")
9645   (require 'gnus-art)
9646   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9647     (gnus-summary-save-article arg)))
9648
9649 (defun gnus-summary-save-article-rmail (&optional arg)
9650   "Append the current article to an rmail file.
9651 If N is a positive number, save the N next articles.
9652 If N is a negative number, save the N previous articles.
9653 If N is nil and any articles have been marked with the process mark,
9654 save those articles instead."
9655   (interactive "P")
9656   (require 'gnus-art)
9657   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9658     (gnus-summary-save-article arg)))
9659
9660 (defun gnus-summary-save-article-file (&optional arg)
9661   "Append the current article to a file.
9662 If N is a positive number, save the N next articles.
9663 If N is a negative number, save the N previous articles.
9664 If N is nil and any articles have been marked with the process mark,
9665 save those articles instead."
9666   (interactive "P")
9667   (require 'gnus-art)
9668   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9669     (gnus-summary-save-article arg)))
9670
9671 (defun gnus-summary-write-article-file (&optional arg)
9672   "Write the current article to a file, deleting the previous file.
9673 If N is a positive number, save the N next articles.
9674 If N is a negative number, save the N previous articles.
9675 If N is nil and any articles have been marked with the process mark,
9676 save those articles instead."
9677   (interactive "P")
9678   (require 'gnus-art)
9679   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9680     (gnus-summary-save-article arg)))
9681
9682 (defun gnus-summary-save-article-body-file (&optional arg)
9683   "Append the current article body to a file.
9684 If N is a positive number, save the N next articles.
9685 If N is a negative number, save the N previous articles.
9686 If N is nil and any articles have been marked with the process mark,
9687 save those articles instead."
9688   (interactive "P")
9689   (require 'gnus-art)
9690   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9691     (gnus-summary-save-article arg)))
9692
9693 (defun gnus-summary-pipe-message (program)
9694   "Pipe the current article through PROGRAM."
9695   (interactive "sProgram: ")
9696   (gnus-summary-select-article)
9697   (let ((mail-header-separator ""))
9698     (gnus-eval-in-buffer-window gnus-article-buffer
9699       (save-restriction
9700         (widen)
9701         (let ((start (window-start))
9702               buffer-read-only)
9703           (message-pipe-buffer-body program)
9704           (set-window-start (get-buffer-window (current-buffer)) start))))))
9705
9706 (defun gnus-get-split-value (methods)
9707   "Return a value based on the split METHODS."
9708   (let (split-name method result match)
9709     (when methods
9710       (save-excursion
9711         (set-buffer gnus-original-article-buffer)
9712         (save-restriction
9713           (nnheader-narrow-to-headers)
9714           (while (and methods (not split-name))
9715             (goto-char (point-min))
9716             (setq method (pop methods))
9717             (setq match (car method))
9718             (when (cond
9719                    ((stringp match)
9720                     ;; Regular expression.
9721                     (ignore-errors
9722                       (re-search-forward match nil t)))
9723                    ((gnus-functionp match)
9724                     ;; Function.
9725                     (save-restriction
9726                       (widen)
9727                       (setq result (funcall match gnus-newsgroup-name))))
9728                    ((consp match)
9729                     ;; Form.
9730                     (save-restriction
9731                       (widen)
9732                       (setq result (eval match)))))
9733               (setq split-name (cdr method))
9734               (cond ((stringp result)
9735                      (push (expand-file-name
9736                             result gnus-article-save-directory)
9737                            split-name))
9738                     ((consp result)
9739                      (setq split-name (append result split-name)))))))))
9740     (nreverse split-name)))
9741
9742 (defun gnus-valid-move-group-p (group)
9743   (and (boundp group)
9744        (symbol-name group)
9745        (symbol-value group)
9746        (gnus-get-function (gnus-find-method-for-group
9747                            (symbol-name group)) 'request-accept-article t)))
9748
9749 (defun gnus-read-move-group-name (prompt default articles prefix)
9750   "Read a group name."
9751   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9752          (minibuffer-confirm-incomplete nil) ; XEmacs
9753          (prom
9754           (format "%s %s to:"
9755                   prompt
9756                   (if (> (length articles) 1)
9757                       (format "these %d articles" (length articles))
9758                     "this article")))
9759          (to-newsgroup
9760           (cond
9761            ((null split-name)
9762             (gnus-completing-read default prom
9763                                   gnus-active-hashtb
9764                                   'gnus-valid-move-group-p
9765                                   nil prefix
9766                                   'gnus-group-history))
9767            ((= 1 (length split-name))
9768             (gnus-completing-read (car split-name) prom
9769                                   gnus-active-hashtb
9770                                   'gnus-valid-move-group-p
9771                                   nil nil
9772                                   'gnus-group-history))
9773            (t
9774             (gnus-completing-read nil prom
9775                                   (mapcar (lambda (el) (list el))
9776                                           (nreverse split-name))
9777                                   nil nil nil
9778                                   'gnus-group-history))))
9779          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9780     (when to-newsgroup
9781       (if (or (string= to-newsgroup "")
9782               (string= to-newsgroup prefix))
9783           (setq to-newsgroup default))
9784       (unless to-newsgroup
9785         (error "No group name entered"))
9786       (or (gnus-active to-newsgroup)
9787           (gnus-activate-group to-newsgroup nil nil to-method)
9788           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9789                                      to-newsgroup))
9790               (or (and (gnus-request-create-group to-newsgroup to-method)
9791                        (gnus-activate-group
9792                         to-newsgroup nil nil to-method)
9793                        (gnus-subscribe-group to-newsgroup))
9794                   (error "Couldn't create group %s" to-newsgroup)))
9795           (error "No such group: %s" to-newsgroup)))
9796     to-newsgroup))
9797
9798 (defun gnus-summary-save-parts (type dir n &optional reverse)
9799   "Save parts matching TYPE to DIR.
9800 If REVERSE, save parts that do not match TYPE."
9801   (interactive
9802    (list (read-string "Save parts of type: "
9803                       (or (car gnus-summary-save-parts-type-history)
9804                           gnus-summary-save-parts-default-mime)
9805                       'gnus-summary-save-parts-type-history)
9806          (setq gnus-summary-save-parts-last-directory
9807                (read-file-name "Save to directory: "
9808                                gnus-summary-save-parts-last-directory
9809                                nil t))
9810          current-prefix-arg))
9811   (gnus-summary-iterate n
9812     (let ((gnus-display-mime-function nil)
9813           (gnus-inhibit-treatment t))
9814       (gnus-summary-select-article))
9815     (save-excursion
9816       (set-buffer gnus-article-buffer)
9817       (let ((handles (or gnus-article-mime-handles
9818                          (mm-dissect-buffer) (mm-uu-dissect))))
9819         (when handles
9820           (gnus-summary-save-parts-1 type dir handles reverse)
9821           (unless gnus-article-mime-handles ;; Don't destroy this case.
9822             (mm-destroy-parts handles)))))))
9823
9824 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9825   (if (stringp (car handle))
9826       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9827               (cdr handle))
9828     (when (if reverse
9829               (not (string-match type (mm-handle-media-type handle)))
9830             (string-match type (mm-handle-media-type handle)))
9831       (let ((file (expand-file-name
9832                    (file-name-nondirectory
9833                     (or
9834                      (mail-content-type-get
9835                       (mm-handle-disposition handle) 'filename)
9836                      (concat gnus-newsgroup-name
9837                              "." (number-to-string
9838                                   (cdr gnus-article-current)))))
9839                    dir)))
9840         (unless (file-exists-p file)
9841           (mm-save-part-to-file handle file))))))
9842
9843 ;; Summary extract commands
9844
9845 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9846   (let ((buffer-read-only nil)
9847         (article (gnus-summary-article-number))
9848         after-article b e)
9849     (unless (gnus-summary-goto-subject article)
9850       (error "No such article: %d" article))
9851     (gnus-summary-position-point)
9852     ;; If all commands are to be bunched up on one line, we collect
9853     ;; them here.
9854     (unless gnus-view-pseudos-separately
9855       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9856             files action)
9857         (while ps
9858           (setq action (cdr (assq 'action (car ps))))
9859           (setq files (list (cdr (assq 'name (car ps)))))
9860           (while (and ps (cdr ps)
9861                       (string= (or action "1")
9862                                (or (cdr (assq 'action (cadr ps))) "2")))
9863             (push (cdr (assq 'name (cadr ps))) files)
9864             (setcdr ps (cddr ps)))
9865           (when files
9866             (when (not (string-match "%s" action))
9867               (push " " files))
9868             (push " " files)
9869             (when (assq 'execute (car ps))
9870               (setcdr (assq 'execute (car ps))
9871                       (funcall (if (string-match "%s" action)
9872                                    'format 'concat)
9873                                action
9874                                (mapconcat
9875                                 (lambda (f)
9876                                   (if (equal f " ")
9877                                       f
9878                                     (gnus-quote-arg-for-sh-or-csh f)))
9879                                 files " ")))))
9880           (setq ps (cdr ps)))))
9881     (if (and gnus-view-pseudos (not not-view))
9882         (while pslist
9883           (when (assq 'execute (car pslist))
9884             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9885                                   (eq gnus-view-pseudos 'not-confirm)))
9886           (setq pslist (cdr pslist)))
9887       (save-excursion
9888         (while pslist
9889           (setq after-article (or (cdr (assq 'article (car pslist)))
9890                                   (gnus-summary-article-number)))
9891           (gnus-summary-goto-subject after-article)
9892           (forward-line 1)
9893           (setq b (point))
9894           (insert "    " (file-name-nondirectory
9895                           (cdr (assq 'name (car pslist))))
9896                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9897           (setq e (point))
9898           (forward-line -1)             ; back to `b'
9899           (gnus-add-text-properties
9900            b (1- e) (list 'gnus-number gnus-reffed-article-number
9901                           gnus-mouse-face-prop gnus-mouse-face))
9902           (gnus-data-enter
9903            after-article gnus-reffed-article-number
9904            gnus-unread-mark b (car pslist) 0 (- e b))
9905           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9906           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9907           (setq pslist (cdr pslist)))))))
9908
9909 (defun gnus-pseudos< (p1 p2)
9910   (let ((c1 (cdr (assq 'action p1)))
9911         (c2 (cdr (assq 'action p2))))
9912     (and c1 c2 (string< c1 c2))))
9913
9914 (defun gnus-request-pseudo-article (props)
9915   (cond ((assq 'execute props)
9916          (gnus-execute-command (cdr (assq 'execute props)))))
9917   (let ((gnus-current-article (gnus-summary-article-number)))
9918     (gnus-run-hooks 'gnus-mark-article-hook)))
9919
9920 (defun gnus-execute-command (command &optional automatic)
9921   (save-excursion
9922     (gnus-article-setup-buffer)
9923     (set-buffer gnus-article-buffer)
9924     (setq buffer-read-only nil)
9925     (let ((command (if automatic command
9926                      (read-string "Command: " (cons command 0)))))
9927       (erase-buffer)
9928       (insert "$ " command "\n\n")
9929       (if gnus-view-pseudo-asynchronously
9930           (start-process "gnus-execute" (current-buffer) shell-file-name
9931                          shell-command-switch command)
9932         (call-process shell-file-name nil t nil
9933                       shell-command-switch command)))))
9934
9935 ;; Summary kill commands.
9936
9937 (defun gnus-summary-edit-global-kill (article)
9938   "Edit the \"global\" kill file."
9939   (interactive (list (gnus-summary-article-number)))
9940   (gnus-group-edit-global-kill article))
9941
9942 (defun gnus-summary-edit-local-kill ()
9943   "Edit a local kill file applied to the current newsgroup."
9944   (interactive)
9945   (setq gnus-current-headers (gnus-summary-article-header))
9946   (gnus-group-edit-local-kill
9947    (gnus-summary-article-number) gnus-newsgroup-name))
9948
9949 ;;; Header reading.
9950
9951 (defun gnus-read-header (id &optional header)
9952   "Read the headers of article ID and enter them into the Gnus system."
9953   (let ((group gnus-newsgroup-name)
9954         (gnus-override-method
9955          (or
9956           gnus-override-method
9957           (and (gnus-news-group-p gnus-newsgroup-name)
9958                (car (gnus-refer-article-methods)))))
9959         where)
9960     ;; First we check to see whether the header in question is already
9961     ;; fetched.
9962     (if (stringp id)
9963         ;; This is a Message-ID.
9964         (setq header (or header (gnus-id-to-header id)))
9965       ;; This is an article number.
9966       (setq header (or header (gnus-summary-article-header id))))
9967     (if (and header
9968              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9969         ;; We have found the header.
9970         header
9971       ;; If this is a sparse article, we have to nix out its
9972       ;; previous entry in the thread hashtb.
9973       (when (and header
9974                  (gnus-summary-article-sparse-p (mail-header-number header)))
9975         (let* ((parent (gnus-parent-id (mail-header-references header)))
9976                (thread (and parent (gnus-id-to-thread parent))))
9977           (when thread
9978             (delq (assq header thread) thread))))
9979       ;; We have to really fetch the header to this article.
9980       (save-excursion
9981         (set-buffer nntp-server-buffer)
9982         (when (setq where (gnus-request-head id group))
9983           (nnheader-fold-continuation-lines)
9984           (goto-char (point-max))
9985           (insert ".\n")
9986           (goto-char (point-min))
9987           (insert "211 ")
9988           (princ (cond
9989                   ((numberp id) id)
9990                   ((cdr where) (cdr where))
9991                   (header (mail-header-number header))
9992                   (t gnus-reffed-article-number))
9993                  (current-buffer))
9994           (insert " Article retrieved.\n"))
9995         (if (or (not where)
9996                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9997             ()                          ; Malformed head.
9998           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9999             (when (and (stringp id)
10000                        (not (string= (gnus-group-real-name group)
10001                                      (car where))))
10002               ;; If we fetched by Message-ID and the article came
10003               ;; from a different group, we fudge some bogus article
10004               ;; numbers for this article.
10005               (mail-header-set-number header gnus-reffed-article-number))
10006             (save-excursion
10007               (set-buffer gnus-summary-buffer)
10008               (decf gnus-reffed-article-number)
10009               (gnus-remove-header (mail-header-number header))
10010               (push header gnus-newsgroup-headers)
10011               (setq gnus-current-headers header)
10012               (push (mail-header-number header) gnus-newsgroup-limit)))
10013           header)))))
10014
10015 (defun gnus-remove-header (number)
10016   "Remove header NUMBER from `gnus-newsgroup-headers'."
10017   (if (and gnus-newsgroup-headers
10018            (= number (mail-header-number (car gnus-newsgroup-headers))))
10019       (pop gnus-newsgroup-headers)
10020     (let ((headers gnus-newsgroup-headers))
10021       (while (and (cdr headers)
10022                   (not (= number (mail-header-number (cadr headers)))))
10023         (pop headers))
10024       (when (cdr headers)
10025         (setcdr headers (cddr headers))))))
10026
10027 ;;;
10028 ;;; summary highlights
10029 ;;;
10030
10031 (defun gnus-highlight-selected-summary ()
10032   "Highlight selected article in summary buffer."
10033   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10034   (when gnus-summary-selected-face
10035     (save-excursion
10036       (let* ((beg (progn (beginning-of-line) (point)))
10037              (end (progn (end-of-line) (point)))
10038              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10039              (from (if (get-text-property beg gnus-mouse-face-prop)
10040                        beg
10041                      (or (next-single-property-change
10042                           beg gnus-mouse-face-prop nil end)
10043                          beg)))
10044              (to
10045               (if (= from end)
10046                   (- from 2)
10047                 (or (next-single-property-change
10048                      from gnus-mouse-face-prop nil end)
10049                     end))))
10050         ;; If no mouse-face prop on line we will have to = from = end,
10051         ;; so we highlight the entire line instead.
10052         (when (= (+ to 2) from)
10053           (setq from beg)
10054           (setq to end))
10055         (if gnus-newsgroup-selected-overlay
10056             ;; Move old overlay.
10057             (gnus-move-overlay
10058              gnus-newsgroup-selected-overlay from to (current-buffer))
10059           ;; Create new overlay.
10060           (gnus-overlay-put
10061            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10062            'face gnus-summary-selected-face))))))
10063
10064 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10065 (defun gnus-summary-highlight-line ()
10066   "Highlight current line according to `gnus-summary-highlight'."
10067   (let* ((list gnus-summary-highlight)
10068          (p (point))
10069          (end (progn (end-of-line) (point)))
10070          ;; now find out where the line starts and leave point there.
10071          (beg (progn (beginning-of-line) (point)))
10072          (article (gnus-summary-article-number))
10073          (score (or (cdr (assq (or article gnus-current-article)
10074                                gnus-newsgroup-scored))
10075                     gnus-summary-default-score 0))
10076          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10077          (inhibit-read-only t))
10078     ;; Eval the cars of the lists until we find a match.
10079     (let ((default gnus-summary-default-score))
10080       (while (and list
10081                   (not (eval (caar list))))
10082         (setq list (cdr list))))
10083     (let ((face (cdar list)))
10084       (unless (eq face (get-text-property beg 'face))
10085         (gnus-put-text-property-excluding-characters-with-faces
10086          beg end 'face
10087          (setq face (if (boundp face) (symbol-value face) face)))
10088         (when gnus-summary-highlight-line-function
10089           (funcall gnus-summary-highlight-line-function article face))))
10090     (goto-char p)))
10091
10092 (defun gnus-update-read-articles (group unread &optional compute)
10093   "Update the list of read articles in GROUP."
10094   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10095          (entry (gnus-gethash group gnus-newsrc-hashtb))
10096          (info (nth 2 entry))
10097          (prev 1)
10098          (unread (sort (copy-sequence unread) '<))
10099          read)
10100     (if (or (not info) (not active))
10101         ;; There is no info on this group if it was, in fact,
10102         ;; killed.  Gnus stores no information on killed groups, so
10103         ;; there's nothing to be done.
10104         ;; One could store the information somewhere temporarily,
10105         ;; perhaps...  Hmmm...
10106         ()
10107       ;; Remove any negative articles numbers.
10108       (while (and unread (< (car unread) 0))
10109         (setq unread (cdr unread)))
10110       ;; Remove any expired article numbers
10111       (while (and unread (< (car unread) (car active)))
10112         (setq unread (cdr unread)))
10113       ;; Compute the ranges of read articles by looking at the list of
10114       ;; unread articles.
10115       (while unread
10116         (when (/= (car unread) prev)
10117           (push (if (= prev (1- (car unread))) prev
10118                   (cons prev (1- (car unread))))
10119                 read))
10120         (setq prev (1+ (car unread)))
10121         (setq unread (cdr unread)))
10122       (when (<= prev (cdr active))
10123         (push (cons prev (cdr active)) read))
10124       (setq read (if (> (length read) 1) (nreverse read) read))
10125       (if compute
10126           read
10127         (save-excursion
10128           (let (setmarkundo)
10129             ;; Propagate the read marks to the backend.
10130             (when (gnus-check-backend-function 'request-set-mark group)
10131               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10132                     (add (gnus-remove-from-range read (gnus-info-read info))))
10133                 (when (or add del)
10134                   (unless (gnus-check-group group)
10135                     (error "Can't open server for %s" group))
10136                   (gnus-request-set-mark
10137                    group (delq nil (list (if add (list add 'add '(read)))
10138                                          (if del (list del 'del '(read))))))
10139                   (setq setmarkundo
10140                         `(gnus-request-set-mark
10141                           ,group
10142                           ',(delq nil (list
10143                                        (if del (list del 'add '(read)))
10144                                        (if add (list add 'del '(read))))))))))
10145             (set-buffer gnus-group-buffer)
10146             (gnus-undo-register
10147               `(progn
10148                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10149                  (gnus-info-set-read ',info ',(gnus-info-read info))
10150                  (gnus-get-unread-articles-in-group ',info
10151                                                     (gnus-active ,group))
10152                  (gnus-group-update-group ,group t)
10153                  ,setmarkundo))))
10154         ;; Enter this list into the group info.
10155         (gnus-info-set-read info read)
10156         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10157         (gnus-get-unread-articles-in-group info (gnus-active group))
10158         t))))
10159
10160 (defun gnus-offer-save-summaries ()
10161   "Offer to save all active summary buffers."
10162   (let (buffers)
10163     ;; Go through all buffers and find all summaries.
10164     (dolist (buffer (buffer-list))
10165       (when (and (setq buffer (buffer-name buffer))
10166                  (string-match "Summary" buffer)
10167                  (save-excursion
10168                    (set-buffer buffer)
10169                    ;; We check that this is, indeed, a summary buffer.
10170                    (and (eq major-mode 'gnus-summary-mode)
10171                         ;; Also make sure this isn't bogus.
10172                         gnus-newsgroup-prepared
10173                         ;; Also make sure that this isn't a
10174                         ;; dead summary buffer.
10175                         (not gnus-dead-summary-mode))))
10176         (push buffer buffers)))
10177     ;; Go through all these summary buffers and offer to save them.
10178     (when buffers
10179       (save-excursion
10180         (map-y-or-n-p
10181          "Update summary buffer %s? "
10182          (lambda (buf)
10183            (switch-to-buffer buf)
10184            (gnus-summary-exit))
10185          buffers)))))
10186
10187
10188 ;;; @ for mime-partial
10189 ;;;
10190
10191 (defun gnus-request-partial-message ()
10192   (save-excursion
10193     (let ((number (gnus-summary-article-number))
10194           (group gnus-newsgroup-name)
10195           (mother gnus-article-buffer))
10196       (set-buffer (get-buffer-create " *Partial Article*"))
10197       (erase-buffer)
10198       (setq mime-preview-buffer mother)
10199       (gnus-request-article-this-buffer number group)
10200       (mime-parse-buffer)
10201       )))
10202
10203 (autoload 'mime-combine-message/partial-pieces-automatically
10204   "mime-partial"
10205   "Internal method to combine message/partial messages automatically.")
10206
10207 (mime-add-condition
10208  'action '((type . message)(subtype . partial)
10209            (major-mode . gnus-original-article-mode)
10210            (method . mime-combine-message/partial-pieces-automatically)
10211            (summary-buffer-exp . gnus-summary-buffer)
10212            (request-partial-message-method . gnus-request-partial-message)
10213            ))
10214
10215
10216 ;;; @ for message/rfc822
10217 ;;;
10218
10219 (defun gnus-mime-extract-message/rfc822 (entity situation)
10220   (let (group article num cwin swin cur)
10221     (with-temp-buffer
10222       (mime-insert-entity-content entity)
10223       (setq group (or (cdr (assq 'group situation))
10224                       (completing-read "Group: "
10225                                        gnus-active-hashtb
10226                                        nil
10227                                        (gnus-read-active-file-p)
10228                                        gnus-newsgroup-name))
10229             article (gnus-request-accept-article group)))
10230     (when (and (consp article)
10231                (numberp (setq article (cdr article))))
10232       (setq num (1+ (or (cdr (assq 'number situation)) 0))
10233             cwin (get-buffer-window (current-buffer) t))
10234       (save-window-excursion
10235         (if (setq swin (get-buffer-window gnus-summary-buffer t))
10236             (select-window swin)
10237           (set-buffer gnus-summary-buffer))
10238         (setq cur gnus-current-article)
10239         (forward-line num)
10240         (let (gnus-show-threads)
10241           (gnus-summary-goto-subject article t))
10242         (gnus-summary-clear-mark-forward 1)
10243         (gnus-summary-goto-subject cur))
10244       (when (and cwin (window-frame cwin))
10245         (select-frame (window-frame cwin)))
10246       (when (boundp 'mime-acting-situation-to-override)
10247         (set-alist 'mime-acting-situation-to-override
10248                    'group
10249                    group)
10250         (set-alist 'mime-acting-situation-to-override
10251                    'after-method
10252                    `(progn
10253                       (save-current-buffer
10254                         (set-buffer gnus-group-buffer)
10255                         (gnus-activate-group ,group))
10256                       (gnus-summary-goto-article ,cur
10257                                                  gnus-show-all-headers)))
10258         (set-alist 'mime-acting-situation-to-override
10259                    'number num)))))
10260
10261 (mime-add-condition
10262  'action '((type . message)(subtype . rfc822)
10263            (major-mode . gnus-original-article-mode)
10264            (method . gnus-mime-extract-message/rfc822)
10265            (mode . "extract")
10266            ))
10267
10268 (mime-add-condition
10269  'action '((type . message)(subtype . news)
10270            (major-mode . gnus-original-article-mode)
10271            (method . gnus-mime-extract-message/rfc822)
10272            (mode . "extract")
10273            ))
10274
10275 (defun gnus-mime-extract-multipart (entity situation)
10276   (let ((children (mime-entity-children entity))
10277         mime-acting-situation-to-override
10278         f)
10279     (while children
10280       (mime-play-entity (car children)
10281                         (cons (assq 'mode situation)
10282                               mime-acting-situation-to-override))
10283       (setq children (cdr children)))
10284     (if (setq f (cdr (assq 'after-method
10285                            mime-acting-situation-to-override)))
10286         (eval f)
10287       )))
10288
10289 (mime-add-condition
10290  'action '((type . multipart)
10291            (method . gnus-mime-extract-multipart)
10292            (mode . "extract")
10293            )
10294  'with-default)
10295
10296
10297 ;;; @ end
10298 ;;;
10299
10300 (defun gnus-summary-setup-default-charset ()
10301   "Setup newsgroup default charset."
10302   (if (equal gnus-newsgroup-name "nndraft:drafts")
10303       (setq gnus-newsgroup-charset nil)
10304     (let* ((ignored-charsets
10305             (or gnus-newsgroup-ephemeral-ignored-charsets
10306                 (append
10307                  (and gnus-newsgroup-name
10308                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10309                  gnus-newsgroup-ignored-charsets))))
10310       (setq gnus-newsgroup-charset
10311             (or gnus-newsgroup-ephemeral-charset
10312                 (and gnus-newsgroup-name
10313                      (gnus-parameter-charset gnus-newsgroup-name))
10314                 gnus-default-charset))
10315       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10316            ignored-charsets))))
10317
10318 ;;;
10319 ;;; Mime Commands
10320 ;;;
10321
10322 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10323   "Display the current article buffer fully MIME-buttonized.
10324 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10325 treated as multipart/mixed."
10326   (interactive "P")
10327   (require 'gnus-art)
10328   (let ((gnus-unbuttonized-mime-types nil)
10329         (gnus-mime-display-multipart-as-mixed show-all-parts))
10330     (gnus-summary-show-article)))
10331
10332 (defun gnus-summary-repair-multipart (article)
10333   "Add a Content-Type header to a multipart article without one."
10334   (interactive (list (gnus-summary-article-number)))
10335   (gnus-with-article article
10336     (message-narrow-to-head)
10337     (message-remove-header "Mime-Version")
10338     (goto-char (point-max))
10339     (insert "Mime-Version: 1.0\n")
10340     (widen)
10341     (when (search-forward "\n--" nil t)
10342       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10343         (message-narrow-to-head)
10344         (message-remove-header "Content-Type")
10345         (goto-char (point-max))
10346         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10347                         separator))
10348         (widen))))
10349   (let (gnus-mark-article-hook)
10350     (gnus-summary-select-article t t nil article)))
10351
10352 (defun gnus-summary-toggle-display-buttonized ()
10353   "Toggle the buttonizing of the article buffer."
10354   (interactive)
10355   (require 'gnus-art)
10356   (if (setq gnus-inhibit-mime-unbuttonizing
10357             (not gnus-inhibit-mime-unbuttonizing))
10358       (let ((gnus-unbuttonized-mime-types nil))
10359         (gnus-summary-show-article))
10360     (gnus-summary-show-article)))
10361
10362 ;;;
10363 ;;; Intelli-mouse commmands
10364 ;;;
10365
10366 (defun gnus-wheel-summary-scroll (event)
10367   (interactive "e")
10368   (let ((amount (if (memq 'shift (event-modifiers event))
10369                     (car gnus-wheel-scroll-amount)
10370                   (cdr gnus-wheel-scroll-amount)))
10371         (direction (- (* (static-if (featurep 'xemacs)
10372                              (event-button event)
10373                            (cond ((eq 'mouse-4 (event-basic-type event))
10374                                   4)
10375                                  ((eq 'mouse-5 (event-basic-type event))
10376                                   5)))
10377                          2) 9))
10378         edge)
10379     (gnus-summary-scroll-up (* amount direction))
10380     (when (gnus-eval-in-buffer-window gnus-article-buffer
10381             (save-restriction
10382               (widen)
10383               (and (if (< 0 direction)
10384                        (gnus-article-next-page 0)
10385                      (gnus-article-prev-page 0)
10386                      (bobp))
10387                    (if (setq edge (get-text-property
10388                                    (point-min) 'gnus-wheel-edge))
10389                        (setq edge (* edge direction))
10390                      (setq edge -1))
10391                    (or (plusp edge)
10392                        (let ((buffer-read-only nil)
10393                              (inhibit-read-only t))
10394                          (put-text-property (point-min) (point-max)
10395                                             'gnus-wheel-edge direction)
10396                          nil))
10397                    (or (> edge gnus-wheel-edge-resistance)
10398                        (let ((buffer-read-only nil)
10399                              (inhibit-read-only t))
10400                          (put-text-property (point-min) (point-max)
10401                                             'gnus-wheel-edge
10402                                             (* (1+ edge) direction))
10403                          nil))
10404                    (eq last-command 'gnus-wheel-summary-scroll))))
10405       (gnus-summary-next-article nil nil (minusp direction)))))
10406
10407 (defun gnus-wheel-install ()
10408   "Enable mouse wheel support on summary window."
10409   (when gnus-use-wheel
10410     (let ((keys
10411            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
10412       (dolist (key keys)
10413         (define-key gnus-summary-mode-map key
10414           'gnus-wheel-summary-scroll)))))
10415
10416 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
10417
10418 ;;;
10419 ;;; Traditional PGP commmands
10420 ;;;
10421
10422 (defun gnus-summary-decrypt-article (&optional force)
10423   "Decrypt the current article in traditional PGP way.
10424 This will have permanent effect only in mail groups.
10425 If FORCE is non-nil, allow editing of articles even in read-only
10426 groups."
10427   (interactive "P")
10428   (gnus-summary-select-article t)
10429   (gnus-eval-in-buffer-window gnus-article-buffer
10430     (save-excursion
10431       (save-restriction
10432         (widen)
10433         (goto-char (point-min))
10434         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10435           (error "Not a traditional PGP message!"))
10436         (let ((armor-start (match-beginning 0)))
10437           (if (and (pgg-decrypt-region armor-start (point-max))
10438                    (or force (not (gnus-group-read-only-p))))
10439               (let ((inhibit-read-only t)
10440                     buffer-read-only)
10441                 (delete-region armor-start
10442                                (progn
10443                                  (re-search-forward "^-+END PGP" nil t)
10444                                  (beginning-of-line 2)
10445                                  (point)))
10446                 (insert-buffer-substring pgg-output-buffer))))))))
10447
10448 (defun gnus-summary-verify-article ()
10449   "Verify the current article in traditional PGP way."
10450   (interactive)
10451   (save-excursion
10452     (set-buffer gnus-original-article-buffer)
10453     (goto-char (point-min))
10454     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10455       (error "Not a traditional PGP message!"))
10456     (re-search-forward "^-+END PGP" nil t)
10457     (beginning-of-line 2)
10458     (call-interactively (function pgg-verify-region))))
10459
10460 ;;;
10461 ;;; Generic summary marking commands
10462 ;;;
10463
10464 (defvar gnus-summary-marking-alist
10465   '((read gnus-del-mark "d")
10466     (unread gnus-unread-mark "u")
10467     (ticked gnus-ticked-mark "!")
10468     (dormant gnus-dormant-mark "?")
10469     (expirable gnus-expirable-mark "e"))
10470   "An alist of names/marks/keystrokes.")
10471
10472 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10473 (defvar gnus-summary-mark-map)
10474
10475 (defun gnus-summary-make-all-marking-commands ()
10476   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10477   (dolist (elem gnus-summary-marking-alist)
10478     (apply 'gnus-summary-make-marking-command elem)))
10479
10480 (defun gnus-summary-make-marking-command (name mark keystroke)
10481   (let ((map (make-sparse-keymap)))
10482     (define-key gnus-summary-generic-mark-map keystroke map)
10483     (dolist (lway `((next "next" next nil "n")
10484                     (next-unread "next unread" next t "N")
10485                     (prev "previous" prev nil "p")
10486                     (prev-unread "previous unread" prev t "P")
10487                     (nomove "" nil nil ,keystroke)))
10488       (let ((func (gnus-summary-make-marking-command-1
10489                    mark (car lway) lway name)))
10490         (setq func (eval func))
10491         (define-key map (nth 4 lway) func)))))
10492
10493 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10494   `(defun ,(intern
10495             (format "gnus-summary-put-mark-as-%s%s"
10496                     name (if (eq way 'nomove)
10497                              ""
10498                            (concat "-" (symbol-name way)))))
10499      (n)
10500      ,(format
10501        "Mark the current article as %s%s.
10502 If N, the prefix, then repeat N times.
10503 If N is negative, move in reverse order.
10504 The difference between N and the actual number of articles marked is
10505 returned."
10506        name (car (cdr lway)))
10507      (interactive "p")
10508      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10509
10510 (defun gnus-summary-generic-mark (n mark move unread)
10511   "Mark N articles with MARK."
10512   (unless (eq major-mode 'gnus-summary-mode)
10513     (error "This command can only be used in the summary buffer"))
10514   (gnus-summary-show-thread)
10515   (let ((nummove
10516          (cond
10517           ((eq move 'next) 1)
10518           ((eq move 'prev) -1)
10519           (t 0))))
10520     (if (zerop nummove)
10521         (setq n 1)
10522       (when (< n 0)
10523         (setq n (abs n)
10524               nummove (* -1 nummove))))
10525     (while (and (> n 0)
10526                 (gnus-summary-mark-article nil mark)
10527                 (zerop (gnus-summary-next-subject nummove unread t)))
10528       (setq n (1- n)))
10529     (when (/= 0 n)
10530       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10531     (gnus-summary-recenter)
10532     (gnus-summary-position-point)
10533     (gnus-set-mode-line 'summary)
10534     n))
10535
10536 (defun gnus-summary-insert-articles (articles)
10537   (when (setq articles
10538               (gnus-set-difference articles
10539                                    (mapcar (lambda (h) (mail-header-number h))
10540                                            gnus-newsgroup-headers)))
10541     (setq gnus-newsgroup-headers 
10542           (merge 'list
10543                  gnus-newsgroup-headers
10544                  (gnus-fetch-headers articles)
10545                  'gnus-article-sort-by-number))
10546     ;; Suppress duplicates?
10547     (when gnus-suppress-duplicates
10548       (gnus-dup-suppress-articles))
10549     
10550     ;; We might want to build some more threads first.
10551     (when (and gnus-fetch-old-headers
10552                (eq gnus-headers-retrieved-by 'nov))
10553       (if (eq gnus-fetch-old-headers 'invisible)
10554         (gnus-build-all-threads)
10555         (gnus-build-old-threads)))
10556     ;; Let the Gnus agent mark articles as read.
10557     (when gnus-agent
10558       (gnus-agent-get-undownloaded-list))
10559     ;; Remove list identifiers from subject
10560     (when gnus-list-identifiers
10561       (gnus-summary-remove-list-identifiers))
10562     ;; First and last article in this newsgroup.
10563     (when gnus-newsgroup-headers
10564       (setq gnus-newsgroup-begin
10565             (mail-header-number (car gnus-newsgroup-headers))
10566             gnus-newsgroup-end
10567             (mail-header-number
10568              (gnus-last-element gnus-newsgroup-headers))))
10569     (when gnus-use-scoring
10570       (gnus-possibly-score-headers))))
10571
10572 (defun gnus-summary-insert-old-articles (&optional all)
10573   "Insert all old articles in this group.
10574 If ALL is non-nil, already read articles become readable.
10575 If ALL is a number, fetch this number of articles."
10576   (interactive "P")
10577   (prog1
10578       (let ((old (mapcar 'car gnus-newsgroup-data))
10579             (i (car gnus-newsgroup-active))
10580             older len)
10581         (while (<= i (cdr gnus-newsgroup-active))
10582           (or (memq i old) (push i older))
10583           (incf i))
10584         (setq len (length older))
10585         (cond 
10586          ((null older) nil)
10587          ((numberp all) 
10588           (if (< all len)
10589               (setq older (subseq older 0 all))))
10590          (all nil)
10591          (t
10592           (if (and (numberp gnus-large-newsgroup)
10593                    (> len gnus-large-newsgroup))
10594               (let ((input
10595                      (read-string
10596                       (format
10597                        "How many articles from %s (default %d): "
10598                        (gnus-limit-string 
10599                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10600                        len))))
10601                 (unless (string-match "^[ \t]*$" input) 
10602                   (setq all (string-to-number input))
10603                   (if (< all len)
10604                       (setq older (subseq older 0 all))))))))
10605         (if (not older)
10606             (message "No old news.")
10607           (gnus-summary-insert-articles older)
10608           (gnus-summary-limit (gnus-union older old))))
10609     (gnus-summary-position-point)))
10610
10611 (defun gnus-summary-insert-new-articles ()
10612   "Insert all new articles in this group."
10613   (interactive)
10614   (prog1
10615       (let ((old (mapcar 'car gnus-newsgroup-data))
10616             (old-active gnus-newsgroup-active)
10617             (nnmail-fetched-sources (list t))
10618             i new)
10619         (setq gnus-newsgroup-active 
10620               (gnus-activate-group gnus-newsgroup-name 'scan))
10621         (setq i (1+ (cdr old-active)))
10622         (while (<= i (cdr gnus-newsgroup-active))
10623           (push i new)
10624           (incf i))
10625         (if (not new)
10626             (message "No gnus is bad news.")
10627           (setq new (nreverse new))
10628           (gnus-summary-insert-articles new)
10629           (setq gnus-newsgroup-unreads
10630                 (append gnus-newsgroup-unreads new))
10631           (gnus-summary-limit (gnus-union old new))))
10632     (gnus-summary-position-point)))
10633
10634 (gnus-summary-make-all-marking-commands)
10635
10636 (gnus-ems-redefine)
10637
10638 (provide 'gnus-sum)
10639
10640 (run-hooks 'gnus-sum-load-hook)
10641
10642 ;;; gnus-sum.el ends here