Synch with Oort Gnus.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
8 ;; Keywords: mail, news, MIME
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32
33 (require 'gnus)
34 (require 'gnus-group)
35 (require 'gnus-spec)
36 (require 'gnus-range)
37 (require 'gnus-int)
38 (require 'gnus-undo)
39 (require 'gnus-util)
40 ;; Recursive :-(.
41 ;; (require 'gnus-art)
42 (require 'nnoo)
43 (require 'mime-view)
44
45 (eval-when-compile
46   (require 'mime-play)
47   (require 'static))
48
49 (eval-and-compile
50   (autoload 'gnus-cache-articles-in-group "gnus-cache")
51   (autoload 'pgg-decrypt-region "pgg" nil t)
52   (autoload 'pgg-verify-region "pgg" nil t))
53
54 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
55 (autoload 'gnus-cache-write-active "gnus-cache")
56 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
57 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
58 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
59 (autoload 'mm-uu-dissect "mm-uu")
60
61 (defcustom gnus-kill-summary-on-exit t
62   "*If non-nil, kill the summary buffer when you exit from it.
63 If nil, the summary will become a \"*Dead Summary*\" buffer, and
64 it will be killed sometime later."
65   :group 'gnus-summary-exit
66   :type 'boolean)
67
68 (defcustom gnus-fetch-old-headers nil
69   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
70 If an unread article in the group refers to an older, already read (or
71 just marked as read) article, the old article will not normally be
72 displayed in the Summary buffer.  If this variable is non-nil, Gnus
73 will attempt to grab the headers to the old articles, and thereby
74 build complete threads.  If it has the value `some', only enough
75 headers to connect otherwise loose threads will be displayed.  This
76 variable can also be a number.  In that case, no more than that number
77 of old headers will be fetched.  If it has the value `invisible', all
78 old headers will be fetched, but none will be displayed.
79
80 The server has to support NOV for any of this to work."
81   :group 'gnus-thread
82   :type '(choice (const :tag "off" nil)
83                  (const some)
84                  number
85                  (sexp :menu-tag "other" t)))
86
87 (defcustom gnus-refer-thread-limit 200
88   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
89 If t, fetch all the available old headers."
90   :group 'gnus-thread
91   :type '(choice number
92                  (sexp :menu-tag "other" t)))
93
94 (defcustom gnus-summary-make-false-root 'adopt
95   "*nil means that Gnus won't gather loose threads.
96 If the root of a thread has expired or been read in a previous
97 session, the information necessary to build a complete thread has been
98 lost.  Instead of having many small sub-threads from this original thread
99 scattered all over the summary buffer, Gnus can gather them.
100
101 If non-nil, Gnus will try to gather all loose sub-threads from an
102 original thread into one large thread.
103
104 If this variable is non-nil, it should be one of `none', `adopt',
105 `dummy' or `empty'.
106
107 If this variable is `none', Gnus will not make a false root, but just
108 present the sub-threads after another.
109 If this variable is `dummy', Gnus will create a dummy root that will
110 have all the sub-threads as children.
111 If this variable is `adopt', Gnus will make one of the \"children\"
112 the parent and mark all the step-children as such.
113 If this variable is `empty', the \"children\" are printed with empty
114 subject fields.  (Or rather, they will be printed with a string
115 given by the `gnus-summary-same-subject' variable.)"
116   :group 'gnus-thread
117   :type '(choice (const :tag "off" nil)
118                  (const none)
119                  (const dummy)
120                  (const adopt)
121                  (const empty)))
122
123 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
124   "*A regexp to match subjects to be excluded from loose thread gathering.
125 As loose thread gathering is done on subjects only, that means that
126 there can be many false gatherings performed.  By rooting out certain
127 common subjects, gathering might become saner."
128   :group 'gnus-thread
129   :type 'regexp)
130
131 (defcustom gnus-summary-gather-subject-limit nil
132   "*Maximum length of subject comparisons when gathering loose threads.
133 Use nil to compare full subjects.  Setting this variable to a low
134 number will help gather threads that have been corrupted by
135 newsreaders chopping off subject lines, but it might also mean that
136 unrelated articles that have subject that happen to begin with the
137 same few characters will be incorrectly gathered.
138
139 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
140 comparing subjects."
141   :group 'gnus-thread
142   :type '(choice (const :tag "off" nil)
143                  (const fuzzy)
144                  (sexp :menu-tag "on" t)))
145
146 (defcustom gnus-simplify-subject-functions nil
147   "List of functions taking a string argument that simplify subjects.
148 The functions are applied recursively.
149
150 Useful functions to put in this list include: `gnus-simplify-subject-re',
151 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
152   :group 'gnus-thread
153   :type '(repeat function))
154
155 (defcustom gnus-simplify-ignored-prefixes nil
156   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
157   :group 'gnus-thread
158   :type '(choice (const :tag "off" nil)
159                  regexp))
160
161 (defcustom gnus-build-sparse-threads nil
162   "*If non-nil, fill in the gaps in threads.
163 If `some', only fill in the gaps that are needed to tie loose threads
164 together.  If `more', fill in all leaf nodes that Gnus can find.  If
165 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
166   :group 'gnus-thread
167   :type '(choice (const :tag "off" nil)
168                  (const some)
169                  (const more)
170                  (sexp :menu-tag "all" t)))
171
172 (defcustom gnus-summary-thread-gathering-function
173   'gnus-gather-threads-by-subject
174   "*Function used for gathering loose threads.
175 There are two pre-defined functions: `gnus-gather-threads-by-subject',
176 which only takes Subjects into consideration; and
177 `gnus-gather-threads-by-references', which compared the References
178 headers of the articles to find matches."
179   :group 'gnus-thread
180   :type '(radio (function-item gnus-gather-threads-by-subject)
181                 (function-item gnus-gather-threads-by-references)
182                 (function :tag "other")))
183
184 (defcustom gnus-summary-same-subject ""
185   "*String indicating that the current article has the same subject as the previous.
186 This variable will only be used if the value of
187 `gnus-summary-make-false-root' is `empty'."
188   :group 'gnus-summary-format
189   :type 'string)
190
191 (defcustom gnus-summary-goto-unread t
192   "*If t, many commands will go to the next unread article.
193 This applies to marking commands as well as other commands that
194 \"naturally\" select the next article, like, for instance, `SPC' at
195 the end of an article.
196
197 If nil, the marking commands do NOT go to the next unread article
198 (they go to the next article instead).  If `never', commands that
199 usually go to the next unread article, will go to the next article,
200 whether it is read or not."
201   :group 'gnus-summary-marks
202   :link '(custom-manual "(gnus)Setting Marks")
203   :type '(choice (const :tag "off" nil)
204                  (const never)
205                  (sexp :menu-tag "on" t)))
206
207 (defcustom gnus-summary-default-score 0
208   "*Default article score level.
209 All scores generated by the score files will be added to this score.
210 If this variable is nil, scoring will be disabled."
211   :group 'gnus-score-default
212   :type '(choice (const :tag "disable")
213                  integer))
214
215 (defcustom gnus-summary-zcore-fuzz 0
216   "*Fuzziness factor for the zcore in the summary buffer.
217 Articles with scores closer than this to `gnus-summary-default-score'
218 will not be marked."
219   :group 'gnus-summary-format
220   :type 'integer)
221
222 (defcustom gnus-simplify-subject-fuzzy-regexp nil
223   "*Strings to be removed when doing fuzzy matches.
224 This can either be a regular expression or list of regular expressions
225 that will be removed from subject strings if fuzzy subject
226 simplification is selected."
227   :group 'gnus-thread
228   :type '(repeat regexp))
229
230 (defcustom gnus-show-threads t
231   "*If non-nil, display threads in summary mode."
232   :group 'gnus-thread
233   :type 'boolean)
234
235 (defcustom gnus-thread-hide-subtree nil
236   "*If non-nil, hide all threads initially.
237 If threads are hidden, you have to run the command
238 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
239 to expose hidden threads."
240   :group 'gnus-thread
241   :type 'boolean)
242
243 (defcustom gnus-thread-hide-killed t
244   "*If non-nil, hide killed threads automatically."
245   :group 'gnus-thread
246   :type 'boolean)
247
248 (defcustom gnus-thread-ignore-subject t
249   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
250 If nil, articles that have different subjects from their parents will
251 start separate threads."
252   :group 'gnus-thread
253   :type 'boolean)
254
255 (defcustom gnus-thread-operation-ignore-subject t
256   "*If non-nil, subjects will be ignored when doing thread commands.
257 This affects commands like `gnus-summary-kill-thread' and
258 `gnus-summary-lower-thread'.
259
260 If this variable is nil, articles in the same thread with different
261 subjects will not be included in the operation in question.  If this
262 variable is `fuzzy', only articles that have subjects that are fuzzily
263 equal will be included."
264   :group 'gnus-thread
265   :type '(choice (const :tag "off" nil)
266                  (const fuzzy)
267                  (sexp :tag "on" t)))
268
269 (defcustom gnus-thread-indent-level 4
270   "*Number that says how much each sub-thread should be indented."
271   :group 'gnus-thread
272   :type 'integer)
273
274 (defcustom gnus-auto-extend-newsgroup t
275   "*If non-nil, extend newsgroup forward and backward when requested."
276   :group 'gnus-summary-choose
277   :type 'boolean)
278
279 (defcustom gnus-auto-select-first t
280   "*If nil, don't select the first unread article when entering a group.
281 If this variable is `best', select the highest-scored unread article
282 in the group.  If t, select the first unread article.
283
284 This variable can also be a function to place point on a likely
285 subject line.  Useful values include `gnus-summary-first-unread-subject',
286 `gnus-summary-first-unread-article' and
287 `gnus-summary-best-unread-article'.
288
289 If you want to prevent automatic selection of the first unread article
290 in some newsgroups, set the variable to nil in
291 `gnus-select-group-hook'."
292   :group 'gnus-group-select
293   :type '(choice (const :tag "none" nil)
294                  (const best)
295                  (sexp :menu-tag "first" t)
296                  (function-item gnus-summary-first-unread-subject)
297                  (function-item gnus-summary-first-unread-article)
298                  (function-item gnus-summary-best-unread-article)))
299
300 (defcustom gnus-dont-select-after-jump-to-other-group nil
301   "If non-nil, don't select the first unread article after entering the
302 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
303 it is depend on the value of `gnus-auto-select-first' whether to select
304 or not."
305   :group 'gnus-group-select
306   :type 'boolean)
307
308 (defcustom gnus-auto-select-next t
309   "*If non-nil, offer to go to the next group from the end of the previous.
310 If the value is t and the next newsgroup is empty, Gnus will exit
311 summary mode and go back to group mode.  If the value is neither nil
312 nor t, Gnus will select the following unread newsgroup.  In
313 particular, if the value is the symbol `quietly', the next unread
314 newsgroup will be selected without any confirmation, and if it is
315 `almost-quietly', the next group will be selected without any
316 confirmation if you are located on the last article in the group.
317 Finally, if this variable is `slightly-quietly', the `Z n' command
318 will go to the next group without confirmation."
319   :group 'gnus-summary-maneuvering
320   :type '(choice (const :tag "off" nil)
321                  (const quietly)
322                  (const almost-quietly)
323                  (const slightly-quietly)
324                  (sexp :menu-tag "on" t)))
325
326 (defcustom gnus-auto-select-same nil
327   "*If non-nil, select the next article with the same subject.
328 If there are no more articles with the same subject, go to
329 the first unread article."
330   :group 'gnus-summary-maneuvering
331   :type 'boolean)
332
333 (defcustom gnus-summary-check-current nil
334   "*If non-nil, consider the current article when moving.
335 The \"unread\" movement commands will stay on the same line if the
336 current article is unread."
337   :group 'gnus-summary-maneuvering
338   :type 'boolean)
339
340 (defcustom gnus-auto-center-summary t
341   "*If non-nil, always center the current summary buffer.
342 In particular, if `vertical' do only vertical recentering.  If non-nil
343 and non-`vertical', do both horizontal and vertical recentering."
344   :group 'gnus-summary-maneuvering
345   :type '(choice (const :tag "none" nil)
346                  (const vertical)
347                  (integer :tag "height")
348                  (sexp :menu-tag "both" t)))
349
350 (defcustom gnus-show-all-headers nil
351   "*If non-nil, don't hide any headers."
352   :group 'gnus-article-hiding
353   :group 'gnus-article-headers
354   :type 'boolean)
355
356 (defcustom gnus-summary-ignore-duplicates nil
357   "*If non-nil, ignore articles with identical Message-ID headers."
358   :group 'gnus-summary
359   :type 'boolean)
360
361 (defcustom gnus-single-article-buffer t
362   "*If non-nil, display all articles in the same buffer.
363 If nil, each group will get its own article buffer."
364   :group 'gnus-article-various
365   :type 'boolean)
366
367 (defcustom gnus-break-pages t
368   "*If non-nil, do page breaking on articles.
369 The page delimiter is specified by the `gnus-page-delimiter'
370 variable."
371   :group 'gnus-article-various
372   :type 'boolean)
373
374 (defcustom gnus-show-mime t
375   "*If non-nil, do mime processing of articles.
376 The articles will simply be fed to the function given by
377 `gnus-article-display-method-for-mime'."
378   :group 'gnus-article-mime
379   :type 'boolean)
380
381 (defcustom gnus-move-split-methods nil
382   "*Variable used to suggest where articles are to be moved to.
383 It uses the same syntax as the `gnus-split-methods' variable.
384 However, whereas `gnus-split-methods' specifies file names as targets,
385 this variable specifies group names."
386   :group 'gnus-summary-mail
387   :type '(repeat (choice (list :value (fun) function)
388                          (cons :value ("" "") regexp (repeat string))
389                          (sexp :value nil))))
390
391 (defcustom gnus-unread-mark ?  ;Whitespace
392   "*Mark used for unread articles."
393   :group 'gnus-summary-marks
394   :type 'character)
395
396 (defcustom gnus-ticked-mark ?!
397   "*Mark used for ticked articles."
398   :group 'gnus-summary-marks
399   :type 'character)
400
401 (defcustom gnus-dormant-mark ??
402   "*Mark used for dormant articles."
403   :group 'gnus-summary-marks
404   :type 'character)
405
406 (defcustom gnus-del-mark ?r
407   "*Mark used for del'd articles."
408   :group 'gnus-summary-marks
409   :type 'character)
410
411 (defcustom gnus-read-mark ?R
412   "*Mark used for read articles."
413   :group 'gnus-summary-marks
414   :type 'character)
415
416 (defcustom gnus-expirable-mark ?E
417   "*Mark used for expirable articles."
418   :group 'gnus-summary-marks
419   :type 'character)
420
421 (defcustom gnus-killed-mark ?K
422   "*Mark used for killed articles."
423   :group 'gnus-summary-marks
424   :type 'character)
425
426 (defcustom gnus-souped-mark ?F
427   "*Mark used for killed articles."
428   :group 'gnus-summary-marks
429   :type 'character)
430
431 (defcustom gnus-kill-file-mark ?X
432   "*Mark used for articles killed by kill files."
433   :group 'gnus-summary-marks
434   :type 'character)
435
436 (defcustom gnus-low-score-mark ?Y
437   "*Mark used for articles with a low score."
438   :group 'gnus-summary-marks
439   :type 'character)
440
441 (defcustom gnus-catchup-mark ?C
442   "*Mark used for articles that are caught up."
443   :group 'gnus-summary-marks
444   :type 'character)
445
446 (defcustom gnus-replied-mark ?A
447   "*Mark used for articles that have been replied to."
448   :group 'gnus-summary-marks
449   :type 'character)
450
451 (defcustom gnus-cached-mark ?*
452   "*Mark used for articles that are in the cache."
453   :group 'gnus-summary-marks
454   :type 'character)
455
456 (defcustom gnus-saved-mark ?S
457   "*Mark used for articles that have been saved to."
458   :group 'gnus-summary-marks
459   :type 'character)
460
461 (defcustom gnus-no-mark ?  ;Whitespace
462   "*Mark used for articles that have no other secondary mark."
463   :group 'gnus-summary-marks
464   :type 'character)
465
466 (defcustom gnus-ancient-mark ?O
467   "*Mark used for ancient articles."
468   :group 'gnus-summary-marks
469   :type 'character)
470
471 (defcustom gnus-sparse-mark ?Q
472   "*Mark used for sparsely reffed articles."
473   :group 'gnus-summary-marks
474   :type 'character)
475
476 (defcustom gnus-canceled-mark ?G
477   "*Mark used for canceled articles."
478   :group 'gnus-summary-marks
479   :type 'character)
480
481 (defcustom gnus-duplicate-mark ?M
482   "*Mark used for duplicate articles."
483   :group 'gnus-summary-marks
484   :type 'character)
485
486 (defcustom gnus-undownloaded-mark ?@
487   "*Mark used for articles that weren't downloaded."
488   :group 'gnus-summary-marks
489   :type 'character)
490
491 (defcustom gnus-downloadable-mark ?%
492   "*Mark used for articles that are to be downloaded."
493   :group 'gnus-summary-marks
494   :type 'character)
495
496 (defcustom gnus-unsendable-mark ?=
497   "*Mark used for articles that won't be sent."
498   :group 'gnus-summary-marks
499   :type 'character)
500
501 (defcustom gnus-score-over-mark ?+
502   "*Score mark used for articles with high scores."
503   :group 'gnus-summary-marks
504   :type 'character)
505
506 (defcustom gnus-score-below-mark ?-
507   "*Score mark used for articles with low scores."
508   :group 'gnus-summary-marks
509   :type 'character)
510
511 (defcustom gnus-empty-thread-mark ?  ;Whitespace
512   "*There is no thread under the article."
513   :group 'gnus-summary-marks
514   :type 'character)
515
516 (defcustom gnus-not-empty-thread-mark ?=
517   "*There is a thread under the article."
518   :group 'gnus-summary-marks
519   :type 'character)
520
521 (defcustom gnus-view-pseudo-asynchronously nil
522   "*If non-nil, Gnus will view pseudo-articles asynchronously."
523   :group 'gnus-extract-view
524   :type 'boolean)
525
526 (defcustom gnus-auto-expirable-marks
527   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
528         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
529         gnus-souped-mark gnus-duplicate-mark)
530   "*The list of marks converted into expiration if a group is auto-expirable."
531   :version "21.1"
532   :group 'gnus-summary
533   :type '(repeat character))
534
535 (defcustom gnus-inhibit-user-auto-expire t
536   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
537   :version "21.1"
538   :group 'gnus-summary
539   :type 'boolean)
540
541 (defcustom gnus-view-pseudos nil
542   "*If `automatic', pseudo-articles will be viewed automatically.
543 If `not-confirm', pseudos will be viewed automatically, and the user
544 will not be asked to confirm the command."
545   :group 'gnus-extract-view
546   :type '(choice (const :tag "off" nil)
547                  (const automatic)
548                  (const not-confirm)))
549
550 (defcustom gnus-view-pseudos-separately t
551   "*If non-nil, one pseudo-article will be created for each file to be viewed.
552 If nil, all files that use the same viewing command will be given as a
553 list of parameters to that command."
554   :group 'gnus-extract-view
555   :type 'boolean)
556
557 (defcustom gnus-insert-pseudo-articles t
558   "*If non-nil, insert pseudo-articles when decoding articles."
559   :group 'gnus-extract-view
560   :type 'boolean)
561
562 (defcustom gnus-summary-dummy-line-format
563   "  %(:                          :%) %S\n"
564   "*The format specification for the dummy roots in the summary buffer.
565 It works along the same lines as a normal formatting string,
566 with some simple extensions.
567
568 %S  The subject"
569   :group 'gnus-threading
570   :type 'string)
571
572 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
573   "*The format specification for the summary mode line.
574 It works along the same lines as a normal formatting string,
575 with some simple extensions:
576
577 %G  Group name
578 %p  Unprefixed group name
579 %A  Current article number
580 %z  Current article score
581 %V  Gnus version
582 %U  Number of unread articles in the group
583 %e  Number of unselected articles in the group
584 %Z  A string with unread/unselected article counts
585 %g  Shortish group name
586 %S  Subject of the current article
587 %u  User-defined spec
588 %s  Current score file name
589 %d  Number of dormant articles
590 %r  Number of articles that have been marked as read in this session
591 %E  Number of articles expunged by the score files"
592   :group 'gnus-summary-format
593   :type 'string)
594
595 (defcustom gnus-list-identifiers nil
596   "Regexp that matches list identifiers to be removed from subject.
597 This can also be a list of regexps."
598   :version "21.1"
599   :group 'gnus-summary-format
600   :group 'gnus-article-hiding
601   :type '(choice (const :tag "none" nil)
602                  (regexp :value ".*")
603                  (repeat :value (".*") regexp)))
604
605 (defcustom gnus-summary-mark-below 0
606   "*Mark all articles with a score below this variable as read.
607 This variable is local to each summary buffer and usually set by the
608 score file."
609   :group 'gnus-score-default
610   :type 'integer)
611
612 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
613   "*List of functions used for sorting articles in the summary buffer.
614
615 Each function takes two articles and returns non-nil if the first
616 article should be sorted before the other.  If you use more than one
617 function, the primary sort function should be the last.  You should
618 probably always include `gnus-article-sort-by-number' in the list of
619 sorting functions -- preferably first.  Also note that sorting by date
620 is often much slower than sorting by number, and the sorting order is
621 very similar.  (Sorting by date means sorting by the time the message
622 was sent, sorting by number means sorting by arrival time.)
623
624 Ready-made functions include `gnus-article-sort-by-number',
625 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
626 `gnus-article-sort-by-date' and `gnus-article-sort-by-score'.
627
628 When threading is turned on, the variable `gnus-thread-sort-functions'
629 controls how articles are sorted."
630   :group 'gnus-summary-sort
631   :type '(repeat (choice (function-item gnus-article-sort-by-number)
632                          (function-item gnus-article-sort-by-author)
633                          (function-item gnus-article-sort-by-subject)
634                          (function-item gnus-article-sort-by-date)
635                          (function-item gnus-article-sort-by-score)
636                          (function :tag "other"))))
637
638 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
639   "*List of functions used for sorting threads in the summary buffer.
640 By default, threads are sorted by article number.
641
642 Each function takes two threads and returns non-nil if the first
643 thread should be sorted before the other.  If you use more than one
644 function, the primary sort function should be the last.  You should
645 probably always include `gnus-thread-sort-by-number' in the list of
646 sorting functions -- preferably first.  Also note that sorting by date
647 is often much slower than sorting by number, and the sorting order is
648 very similar.  (Sorting by date means sorting by the time the message
649 was sent, sorting by number means sorting by arrival time.)
650
651 Ready-made functions include `gnus-thread-sort-by-number',
652 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
653 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
654 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
655
656 When threading is turned off, the variable
657 `gnus-article-sort-functions' controls how articles are sorted."
658   :group 'gnus-summary-sort
659   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
660                          (function-item gnus-thread-sort-by-author)
661                          (function-item gnus-thread-sort-by-subject)
662                          (function-item gnus-thread-sort-by-date)
663                          (function-item gnus-thread-sort-by-score)
664                          (function-item gnus-thread-sort-by-total-score)
665                          (function :tag "other"))))
666
667 (defcustom gnus-thread-score-function '+
668   "*Function used for calculating the total score of a thread.
669
670 The function is called with the scores of the article and each
671 subthread and should then return the score of the thread.
672
673 Some functions you can use are `+', `max', or `min'."
674   :group 'gnus-summary-sort
675   :type 'function)
676
677 (defcustom gnus-summary-expunge-below nil
678   "All articles that have a score less than this variable will be expunged.
679 This variable is local to the summary buffers."
680   :group 'gnus-score-default
681   :type '(choice (const :tag "off" nil)
682                  integer))
683
684 (defcustom gnus-thread-expunge-below nil
685   "All threads that have a total score less than this variable will be expunged.
686 See `gnus-thread-score-function' for en explanation of what a
687 \"thread score\" is.
688
689 This variable is local to the summary buffers."
690   :group 'gnus-threading
691   :group 'gnus-score-default
692   :type '(choice (const :tag "off" nil)
693                  integer))
694
695 (defcustom gnus-summary-mode-hook nil
696   "*A hook for Gnus summary mode.
697 This hook is run before any variables are set in the summary buffer."
698   :options '(turn-on-gnus-mailing-list-mode)
699   :group 'gnus-summary-various
700   :type 'hook)
701
702 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
703 (when (featurep 'xemacs)
704   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
705   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
706   (add-hook 'gnus-summary-mode-hook
707             'gnus-xmas-switch-horizontal-scrollbar-off))
708
709 (defcustom gnus-summary-menu-hook nil
710   "*Hook run after the creation of the summary mode menu."
711   :group 'gnus-summary-visual
712   :type 'hook)
713
714 (defcustom gnus-summary-exit-hook nil
715   "*A hook called on exit from the summary buffer.
716 It will be called with point in the group buffer."
717   :group 'gnus-summary-exit
718   :type 'hook)
719
720 (defcustom gnus-summary-prepare-hook nil
721   "*A hook called after the summary buffer has been generated.
722 If you want to modify the summary buffer, you can use this hook."
723   :group 'gnus-summary-various
724   :type 'hook)
725
726 (defcustom gnus-summary-prepared-hook nil
727   "*A hook called as the last thing after the summary buffer has been generated."
728   :group 'gnus-summary-various
729   :type 'hook)
730
731 (defcustom gnus-summary-generate-hook nil
732   "*A hook run just before generating the summary buffer.
733 This hook is commonly used to customize threading variables and the
734 like."
735   :group 'gnus-summary-various
736   :type 'hook)
737
738 (defcustom gnus-select-group-hook nil
739   "*A hook called when a newsgroup is selected.
740
741 If you'd like to simplify subjects like the
742 `gnus-summary-next-same-subject' command does, you can use the
743 following hook:
744
745  (setq gnus-select-group-hook
746       (list
747         (lambda ()
748           (mapcar (lambda (header)
749                      (mail-header-set-subject
750                       header
751                       (gnus-simplify-subject
752                        (mail-header-subject header) 're-only)))
753                   gnus-newsgroup-headers))))"
754   :group 'gnus-group-select
755   :type 'hook)
756
757 (defcustom gnus-select-article-hook nil
758   "*A hook called when an article is selected."
759   :group 'gnus-summary-choose
760   :type 'hook)
761
762 (defcustom gnus-visual-mark-article-hook
763   (list 'gnus-highlight-selected-summary)
764   "*Hook run after selecting an article in the summary buffer.
765 It is meant to be used for highlighting the article in some way.  It
766 is not run if `gnus-visual' is nil."
767   :group 'gnus-summary-visual
768   :type 'hook)
769
770 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
771   "*A hook called before parsing the headers."
772   :group 'gnus-various
773   :type 'hook)
774
775 (defcustom gnus-exit-group-hook nil
776   "*A hook called when exiting summary mode.
777 This hook is not called from the non-updating exit commands like `Q'."
778   :group 'gnus-various
779   :type 'hook)
780
781 (defcustom gnus-summary-update-hook
782   (list 'gnus-summary-highlight-line)
783   "*A hook called when a summary line is changed.
784 The hook will not be called if `gnus-visual' is nil.
785
786 The default function `gnus-summary-highlight-line' will
787 highlight the line according to the `gnus-summary-highlight'
788 variable."
789   :group 'gnus-summary-visual
790   :type 'hook)
791
792 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
793   "*A hook called when an article is selected for the first time.
794 The hook is intended to mark an article as read (or unread)
795 automatically when it is selected."
796   :group 'gnus-summary-choose
797   :type 'hook)
798
799 (defcustom gnus-group-no-more-groups-hook nil
800   "*A hook run when returning to group mode having no more (unread) groups."
801   :group 'gnus-group-select
802   :type 'hook)
803
804 (defcustom gnus-ps-print-hook nil
805   "*A hook run before ps-printing something from Gnus."
806   :group 'gnus-summary
807   :type 'hook)
808
809 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
810   "Face used for highlighting the current article in the summary buffer."
811   :group 'gnus-summary-visual
812   :type 'face)
813
814 (defcustom gnus-summary-highlight
815   '(((= mark gnus-canceled-mark)
816      . gnus-summary-cancelled-face)
817     ((and (> score default)
818           (or (= mark gnus-dormant-mark)
819               (= mark gnus-ticked-mark)))
820      . gnus-summary-high-ticked-face)
821     ((and (< score default)
822           (or (= mark gnus-dormant-mark)
823               (= mark gnus-ticked-mark)))
824      . gnus-summary-low-ticked-face)
825     ((or (= mark gnus-dormant-mark)
826          (= mark gnus-ticked-mark))
827      . gnus-summary-normal-ticked-face)
828     ((and (> score default) (= mark gnus-ancient-mark))
829      . gnus-summary-high-ancient-face)
830     ((and (< score default) (= mark gnus-ancient-mark))
831      . gnus-summary-low-ancient-face)
832     ((= mark gnus-ancient-mark)
833      . gnus-summary-normal-ancient-face)
834     ((and (> score default) (= mark gnus-unread-mark))
835      . gnus-summary-high-unread-face)
836     ((and (< score default) (= mark gnus-unread-mark))
837      . gnus-summary-low-unread-face)
838     ((and (memq article gnus-newsgroup-incorporated)
839           (= mark gnus-unread-mark))
840      . gnus-summary-incorporated-face)
841     ((= mark gnus-unread-mark)
842      . gnus-summary-normal-unread-face)
843     ((and (> score default) (memq mark (list gnus-downloadable-mark
844                                              gnus-undownloaded-mark)))
845      . gnus-summary-high-unread-face)
846     ((and (< score default) (memq mark (list gnus-downloadable-mark
847                                              gnus-undownloaded-mark)))
848      . gnus-summary-low-unread-face)
849     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
850      . gnus-summary-normal-unread-face)
851     ((> score default)
852      . gnus-summary-high-read-face)
853     ((< score default)
854      . gnus-summary-low-read-face)
855     (t
856      . gnus-summary-normal-read-face))
857   "*Controls the highlighting of summary buffer lines.
858
859 A list of (FORM . FACE) pairs.  When deciding how a a particular
860 summary line should be displayed, each form is evaluated.  The content
861 of the face field after the first true form is used.  You can change
862 how those summary lines are displayed, by editing the face field.
863
864 You can use the following variables in the FORM field.
865
866 score:   The articles score
867 default: The default article score.
868 below:   The score below which articles are automatically marked as read.
869 mark:    The articles mark."
870   :group 'gnus-summary-visual
871   :type '(repeat (cons (sexp :tag "Form" nil)
872                        face)))
873
874 (defcustom gnus-alter-header-function nil
875   "Function called to allow alteration of article header structures.
876 The function is called with one parameter, the article header vector,
877 which it may alter in any way.")
878
879 (defvar gnus-decode-encoded-word-function
880   (mime-find-field-decoder 'From 'nov)
881   "Variable that says which function should be used to decode a string with encoded words.")
882
883 (defcustom gnus-extra-headers nil
884   "*Extra headers to parse."
885   :version "21.1"
886   :group 'gnus-summary
887   :type '(repeat symbol))
888
889 (defcustom gnus-ignored-from-addresses
890   (and user-mail-address (regexp-quote user-mail-address))
891   "*Regexp of From headers that may be suppressed in favor of To headers."
892   :version "21.1"
893   :group 'gnus-summary
894   :type 'regexp)
895
896 (gnus-define-group-parameter
897  charset
898  :function-document
899  "Return the default charset of GROUP."
900  :variable gnus-group-charset-alist
901  :variable-default 
902  '(("\\(^\\|:\\)hk\\>\\|\\(^\\|:\\)tw\\>\\|\\<big5\\>" cn-big5)
903    ("\\(^\\|:\\)cn\\>\\|\\<chinese\\>" cn-gb-2312)
904    ("\\(^\\|:\\)fj\\>\\|\\(^\\|:\\)japan\\>" iso-2022-jp-2)
905    ("\\(^\\|:\\)tnn\\>\\|\\(^\\|:\\)pin\\>\\|\\(^\\|:\\)sci.lang.japan" iso-2022-7bit)
906    ("\\(^\\|:\\)relcom\\>" koi8-r)
907    ("\\(^\\|:\\)fido7\\>" koi8-r)
908    ("\\(^\\|:\\)\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
909    ("\\(^\\|:\\)israel\\>" iso-8859-1)
910    ("\\(^\\|:\\)han\\>" euc-kr)
911    ("\\(^\\|:\\)alt.chinese.text.big5\\>" chinese-big5)
912    ("\\(^\\|:\\)soc.culture.vietnamese\\>" vietnamese-viqr)
913    ("\\(^\\|:\\)\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
914    (".*" iso-8859-1))
915  :variable-document
916   "Alist of regexps (to match group names) and default charsets to be used when reading."
917   :variable-group gnus-charset
918   :variable-type '(repeat (list (regexp :tag "Group")
919                                 (symbol :tag "Charset")))
920   :parameter-type '(symbol :tag "Charset")
921   :parameter-document "\
922 The default charset to use in the group.")
923
924 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
925   "List of charsets that should be ignored.
926 When these charsets are used in the \"charset\" parameter, the
927 default charset will be used instead."
928   :version "21.1"
929   :type '(repeat symbol)
930   :group 'gnus-charset)
931
932 (gnus-define-group-parameter
933  ignored-charsets
934  :type list
935  :function-document
936  "Return the ignored charsets of GROUP."
937  :variable gnus-group-ignored-charsets-alist
938  :variable-default 
939  '(("alt\\.chinese\\.text" iso-8859-1))
940  :variable-document
941  "Alist of regexps (to match group names) and charsets that should be ignored.
942 When these charsets are used in the \"charset\" parameter, the
943 default charset will be used instead."
944  :variable-group gnus-charset
945  :variable-type '(repeat (cons (regexp :tag "Group")
946                                (repeat symbol)))
947  :parameter-type '(choice :tag "Ignored charsets" 
948                           :value nil
949                           (repeat (symbol)))
950  :parameter-document       "\
951 List of charsets that should be ignored.
952
953 When these charsets are used in the \"charset\" parameter, the
954 default charset will be used instead.")
955
956 (defcustom gnus-group-highlight-words-alist nil
957   "Alist of group regexps and highlight regexps.
958 This variable uses the same syntax as `gnus-emphasis-alist'."
959   :version "21.1"
960   :type '(repeat (cons (regexp :tag "Group")
961                        (repeat (list (regexp :tag "Highlight regexp")
962                                      (number :tag "Group for entire word" 0)
963                                      (number :tag "Group for displayed part" 0)
964                                      (symbol :tag "Face"
965                                              gnus-emphasis-highlight-words)))))
966   :group 'gnus-summary-visual)
967
968 (defcustom gnus-use-wheel nil
969   "Use Intelli-mouse on summary movement"
970   :type 'boolean
971   :group 'gnus-summary-maneuvering)
972
973 (defcustom gnus-wheel-scroll-amount '(5 . 1)
974   "Amount to scroll messages by spinning the mouse wheel.
975 This is actually a cons cell, where the first item is the amount to scroll
976 on a normal wheel event, and the second is the amount to scroll when the
977 wheel is moved with the shift key depressed."
978   :type '(cons (integer :tag "Shift") integer)
979   :group 'gnus-summary-maneuvering)
980
981 (defcustom gnus-wheel-edge-resistance 2
982   "How hard it should be to change the current article
983 by moving the mouse over the edge of the article window."
984   :type 'integer
985   :group 'gnus-summary-maneuvering)
986
987 (defcustom gnus-summary-show-article-charset-alist
988   nil
989   "Alist of number and charset.
990 The article will be shown with the charset corresponding to the
991 numbered argument.
992 For example: ((1 . cn-gb-2312) (2 . big5))."
993   :version "21.1"
994   :type '(repeat (cons (number :tag "Argument" 1)
995                        (symbol :tag "Charset")))
996   :group 'gnus-charset)
997
998 (defcustom gnus-preserve-marks t
999   "Whether marks are preserved when moving, copying and respooling messages."
1000   :version "21.1"
1001   :type 'boolean
1002   :group 'gnus-summary-marks)
1003
1004 (defcustom gnus-alter-articles-to-read-function nil
1005   "Function to be called to alter the list of articles to be selected."
1006   :type 'function
1007   :group 'gnus-summary)
1008
1009 (defcustom gnus-orphan-score nil
1010   "*All orphans get this score added.  Set in the score file."
1011   :group 'gnus-score-default
1012   :type '(choice (const nil)
1013                  integer))
1014
1015 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1016   "*A regexp to match MIME parts when saving multiple parts of a message
1017 with gnus-summary-save-parts (X m). This regexp will be used by default
1018 when prompting the user for which type of files to save."
1019   :group 'gnus-summary
1020   :type 'regexp)
1021
1022
1023 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1024   "*A regexp to match MIME parts when saving multiple parts of a message
1025 with gnus-summary-save-parts (X m). This regexp will be used by default
1026 when prompting the user for which type of files to save."
1027   :group 'gnus-summary
1028   :type 'regexp)
1029
1030
1031 ;;; Internal variables
1032
1033 (defvar gnus-article-mime-handles nil)
1034 (defvar gnus-article-decoded-p nil)
1035 (defvar gnus-article-charset nil)
1036 (defvar gnus-article-ignored-charsets nil)
1037 (defvar gnus-scores-exclude-files nil)
1038 (defvar gnus-page-broken nil)
1039 (defvar gnus-inhibit-mime-unbuttonizing nil)
1040
1041 (defvar gnus-original-article nil)
1042 (defvar gnus-article-internal-prepare-hook nil)
1043 (defvar gnus-newsgroup-process-stack nil)
1044
1045 (defvar gnus-thread-indent-array nil)
1046 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1047 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1048   "Function called to sort the articles within a thread after it has been gathered together.")
1049
1050 (defvar gnus-summary-save-parts-type-history nil)
1051 (defvar gnus-summary-save-parts-last-directory nil)
1052
1053 (defvar gnus-summary-save-parts-type-history nil)
1054 (defvar gnus-summary-save-parts-last-directory nil)
1055
1056 ;; Avoid highlighting in kill files.
1057 (defvar gnus-summary-inhibit-highlight nil)
1058 (defvar gnus-newsgroup-selected-overlay nil)
1059 (defvar gnus-inhibit-limiting nil)
1060 (defvar gnus-newsgroup-adaptive-score-file nil)
1061 (defvar gnus-current-score-file nil)
1062 (defvar gnus-current-move-group nil)
1063 (defvar gnus-current-copy-group nil)
1064 (defvar gnus-current-crosspost-group nil)
1065
1066 (defvar gnus-newsgroup-dependencies nil)
1067 (defvar gnus-newsgroup-adaptive nil)
1068 (defvar gnus-summary-display-article-function nil)
1069 (defvar gnus-summary-highlight-line-function nil
1070   "Function called after highlighting a summary line.")
1071
1072 (defvar gnus-summary-line-format-alist
1073   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1074     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1075     (?s gnus-tmp-subject-or-nil ?s)
1076     (?n gnus-tmp-name ?s)
1077     (?A (std11-address-string
1078          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1079     (?a (or (std11-full-name-string
1080              (car (mime-entity-read-field gnus-tmp-header 'From)))
1081             gnus-tmp-from) ?s)
1082     (?F gnus-tmp-from ?s)
1083     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1084     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1085     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1086     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1087     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1088     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1089     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1090     (?L gnus-tmp-lines ?d)
1091     (?I gnus-tmp-indentation ?s)
1092     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1093     (?R gnus-tmp-replied ?c)
1094     (?\[ gnus-tmp-opening-bracket ?c)
1095     (?\] gnus-tmp-closing-bracket ?c)
1096     (?\> (make-string gnus-tmp-level ? ) ?s)
1097     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1098     (?i gnus-tmp-score ?d)
1099     (?z gnus-tmp-score-char ?c)
1100     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1101     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1102     (?U gnus-tmp-unread ?c)
1103     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1104     (?t (gnus-summary-number-of-articles-in-thread
1105          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1106         ?d)
1107     (?e (gnus-summary-number-of-articles-in-thread
1108          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1109         ?c)
1110     (?u gnus-tmp-user-defined ?s)
1111     (?P (gnus-pick-line-number) ?d))
1112   "An alist of format specifications that can appear in summary lines.
1113 These are paired with what variables they correspond with, along with
1114 the type of the variable (string, integer, character, etc).")
1115
1116 (defvar gnus-summary-dummy-line-format-alist
1117   `((?S gnus-tmp-subject ?s)
1118     (?N gnus-tmp-number ?d)
1119     (?u gnus-tmp-user-defined ?s)))
1120
1121 (defvar gnus-summary-mode-line-format-alist
1122   `((?G gnus-tmp-group-name ?s)
1123     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1124     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1125     (?A gnus-tmp-article-number ?d)
1126     (?Z gnus-tmp-unread-and-unselected ?s)
1127     (?V gnus-version ?s)
1128     (?U gnus-tmp-unread-and-unticked ?d)
1129     (?S gnus-tmp-subject ?s)
1130     (?e gnus-tmp-unselected ?d)
1131     (?u gnus-tmp-user-defined ?s)
1132     (?d (length gnus-newsgroup-dormant) ?d)
1133     (?t (length gnus-newsgroup-marked) ?d)
1134     (?r (length gnus-newsgroup-reads) ?d)
1135     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1136     (?E gnus-newsgroup-expunged-tally ?d)
1137     (?s (gnus-current-score-file-nondirectory) ?s)))
1138
1139 (defvar gnus-last-search-regexp nil
1140   "Default regexp for article search command.")
1141
1142 (defvar gnus-summary-search-article-matched-data nil
1143   "Last matched data of article search command.  It is the local variable
1144 in `gnus-article-buffer' which consists of the list of start position,
1145 end position and text.")
1146
1147 (defvar gnus-last-shell-command nil
1148   "Default shell command on article.")
1149
1150 (defvar gnus-newsgroup-begin nil)
1151 (defvar gnus-newsgroup-end nil)
1152 (defvar gnus-newsgroup-last-rmail nil)
1153 (defvar gnus-newsgroup-last-mail nil)
1154 (defvar gnus-newsgroup-last-folder nil)
1155 (defvar gnus-newsgroup-last-file nil)
1156 (defvar gnus-newsgroup-auto-expire nil)
1157 (defvar gnus-newsgroup-active nil)
1158
1159 (defvar gnus-newsgroup-data nil)
1160 (defvar gnus-newsgroup-data-reverse nil)
1161 (defvar gnus-newsgroup-limit nil)
1162 (defvar gnus-newsgroup-limits nil)
1163
1164 (defvar gnus-newsgroup-unreads nil
1165   "List of unread articles in the current newsgroup.")
1166
1167 (defvar gnus-newsgroup-unselected nil
1168   "List of unselected unread articles in the current newsgroup.")
1169
1170 (defvar gnus-newsgroup-reads nil
1171   "Alist of read articles and article marks in the current newsgroup.")
1172
1173 (defvar gnus-newsgroup-expunged-tally nil)
1174
1175 (defvar gnus-newsgroup-marked nil
1176   "List of ticked articles in the current newsgroup (a subset of unread art).")
1177
1178 (defvar gnus-newsgroup-killed nil
1179   "List of ranges of articles that have been through the scoring process.")
1180
1181 (defvar gnus-newsgroup-cached nil
1182   "List of articles that come from the article cache.")
1183
1184 (defvar gnus-newsgroup-saved nil
1185   "List of articles that have been saved.")
1186
1187 (defvar gnus-newsgroup-kill-headers nil)
1188
1189 (defvar gnus-newsgroup-replied nil
1190   "List of articles that have been replied to in the current newsgroup.")
1191
1192 (defvar gnus-newsgroup-expirable nil
1193   "List of articles in the current newsgroup that can be expired.")
1194
1195 (defvar gnus-newsgroup-processable nil
1196   "List of articles in the current newsgroup that can be processed.")
1197
1198 (defvar gnus-newsgroup-downloadable nil
1199   "List of articles in the current newsgroup that can be processed.")
1200
1201 (defvar gnus-newsgroup-undownloaded nil
1202   "List of articles in the current newsgroup that haven't been downloaded..")
1203
1204 (defvar gnus-newsgroup-unsendable nil
1205   "List of articles in the current newsgroup that won't be sent.")
1206
1207 (defvar gnus-newsgroup-bookmarks nil
1208   "List of articles in the current newsgroup that have bookmarks.")
1209
1210 (defvar gnus-newsgroup-dormant nil
1211   "List of dormant articles in the current newsgroup.")
1212
1213 (defvar gnus-newsgroup-scored nil
1214   "List of scored articles in the current newsgroup.")
1215
1216 (defvar gnus-newsgroup-incorporated nil
1217   "List of incorporated articles in the current newsgroup.")
1218
1219 (defvar gnus-newsgroup-headers nil
1220   "List of article headers in the current newsgroup.")
1221
1222 (defvar gnus-newsgroup-threads nil)
1223
1224 (defvar gnus-newsgroup-prepared nil
1225   "Whether the current group has been prepared properly.")
1226
1227 (defvar gnus-newsgroup-ancient nil
1228   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1229
1230 (defvar gnus-newsgroup-sparse nil)
1231
1232 (defvar gnus-current-article nil)
1233 (defvar gnus-article-current nil)
1234 (defvar gnus-current-headers nil)
1235 (defvar gnus-have-all-headers nil)
1236 (defvar gnus-last-article nil)
1237 (defvar gnus-newsgroup-history nil)
1238 (defvar gnus-newsgroup-charset nil)
1239 (defvar gnus-newsgroup-ephemeral-charset nil)
1240 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1241
1242 (defvar gnus-article-before-search nil)
1243
1244 (defconst gnus-summary-local-variables
1245   '(gnus-newsgroup-name
1246     gnus-newsgroup-begin gnus-newsgroup-end
1247     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1248     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1249     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1250     gnus-newsgroup-unselected gnus-newsgroup-marked
1251     gnus-newsgroup-reads gnus-newsgroup-saved
1252     gnus-newsgroup-replied gnus-newsgroup-expirable
1253     gnus-newsgroup-processable gnus-newsgroup-killed
1254     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1255     gnus-newsgroup-unsendable
1256     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1257     gnus-newsgroup-headers gnus-newsgroup-threads
1258     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1259     gnus-current-article gnus-current-headers gnus-have-all-headers
1260     gnus-last-article gnus-article-internal-prepare-hook
1261     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1262     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1263     gnus-thread-expunge-below
1264     gnus-score-alist gnus-current-score-file
1265     (gnus-summary-expunge-below . global)
1266     (gnus-summary-mark-below . global)
1267     (gnus-orphan-score . global)
1268     gnus-newsgroup-active gnus-scores-exclude-files
1269     gnus-newsgroup-history gnus-newsgroup-ancient
1270     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1271     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1272     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1273     (gnus-newsgroup-expunged-tally . 0)
1274     gnus-cache-removable-articles gnus-newsgroup-cached
1275     gnus-newsgroup-data gnus-newsgroup-data-reverse
1276     gnus-newsgroup-limit gnus-newsgroup-limits
1277     gnus-newsgroup-charset
1278     gnus-newsgroup-incorporated)
1279   "Variables that are buffer-local to the summary buffers.")
1280
1281 (defvar gnus-newsgroup-variables nil
1282   "Variables that have separate values in the newsgroups.")
1283
1284 ;; Byte-compiler warning.
1285 (eval-when-compile (defvar gnus-article-mode-map))
1286
1287 ;; Subject simplification.
1288
1289 (defun gnus-simplify-whitespace (str)
1290   "Remove excessive whitespace from STR."
1291   (let ((mystr str))
1292     ;; Multiple spaces.
1293     (while (string-match "[ \t][ \t]+" mystr)
1294       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1295                           " "
1296                           (substring mystr (match-end 0)))))
1297     ;; Leading spaces.
1298     (when (string-match "^[ \t]+" mystr)
1299       (setq mystr (substring mystr (match-end 0))))
1300     ;; Trailing spaces.
1301     (when (string-match "[ \t]+$" mystr)
1302       (setq mystr (substring mystr 0 (match-beginning 0))))
1303     mystr))
1304
1305 (defsubst gnus-simplify-subject-re (subject)
1306   "Remove \"Re:\" from subject lines."
1307   (if (string-match message-subject-re-regexp subject)
1308       (substring subject (match-end 0))
1309     subject))
1310
1311 (defun gnus-simplify-subject (subject &optional re-only)
1312   "Remove `Re:' and words in parentheses.
1313 If RE-ONLY is non-nil, strip leading `Re:'s only."
1314   (let ((case-fold-search t))           ;Ignore case.
1315     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1316     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1317       (setq subject (substring subject (match-end 0))))
1318     ;; Remove uninteresting prefixes.
1319     (when (and (not re-only)
1320                gnus-simplify-ignored-prefixes
1321                (string-match gnus-simplify-ignored-prefixes subject))
1322       (setq subject (substring subject (match-end 0))))
1323     ;; Remove words in parentheses from end.
1324     (unless re-only
1325       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1326         (setq subject (substring subject 0 (match-beginning 0)))))
1327     ;; Return subject string.
1328     subject))
1329
1330 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1331 ;; all whitespace.
1332 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1333   (goto-char (point-min))
1334   (while (re-search-forward regexp nil t)
1335     (replace-match (or newtext ""))))
1336
1337 (defun gnus-simplify-buffer-fuzzy ()
1338   "Simplify string in the buffer fuzzily.
1339 The string in the accessible portion of the current buffer is simplified.
1340 It is assumed to be a single-line subject.
1341 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1342 matter is removed.  Additional things can be deleted by setting
1343 `gnus-simplify-subject-fuzzy-regexp'."
1344   (let ((case-fold-search t)
1345         (modified-tick))
1346     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1347
1348     (while (not (eq modified-tick (buffer-modified-tick)))
1349       (setq modified-tick (buffer-modified-tick))
1350       (cond
1351        ((listp gnus-simplify-subject-fuzzy-regexp)
1352         (mapcar 'gnus-simplify-buffer-fuzzy-step
1353                 gnus-simplify-subject-fuzzy-regexp))
1354        (gnus-simplify-subject-fuzzy-regexp
1355         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1356       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1357       (gnus-simplify-buffer-fuzzy-step
1358        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1359       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1360
1361     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1362     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1363     (gnus-simplify-buffer-fuzzy-step " $")
1364     (gnus-simplify-buffer-fuzzy-step "^ +")))
1365
1366 (defun gnus-simplify-subject-fuzzy (subject)
1367   "Simplify a subject string fuzzily.
1368 See `gnus-simplify-buffer-fuzzy' for details."
1369   (save-excursion
1370     (gnus-set-work-buffer)
1371     (let ((case-fold-search t))
1372       ;; Remove uninteresting prefixes.
1373       (when (and gnus-simplify-ignored-prefixes
1374                  (string-match gnus-simplify-ignored-prefixes subject))
1375         (setq subject (substring subject (match-end 0))))
1376       (insert subject)
1377       (inline (gnus-simplify-buffer-fuzzy))
1378       (buffer-string))))
1379
1380 (defsubst gnus-simplify-subject-fully (subject)
1381   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1382   (cond
1383    (gnus-simplify-subject-functions
1384     (gnus-map-function gnus-simplify-subject-functions subject))
1385    ((null gnus-summary-gather-subject-limit)
1386     (gnus-simplify-subject-re subject))
1387    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1388     (gnus-simplify-subject-fuzzy subject))
1389    ((numberp gnus-summary-gather-subject-limit)
1390     (gnus-limit-string (gnus-simplify-subject-re subject)
1391                        gnus-summary-gather-subject-limit))
1392    (t
1393     subject)))
1394
1395 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1396   "Check whether two subjects are equal.
1397 If optional argument simple-first is t, first argument is already
1398 simplified."
1399   (cond
1400    ((null simple-first)
1401     (equal (gnus-simplify-subject-fully s1)
1402            (gnus-simplify-subject-fully s2)))
1403    (t
1404     (equal s1
1405            (gnus-simplify-subject-fully s2)))))
1406
1407 (defun gnus-summary-bubble-group ()
1408   "Increase the score of the current group.
1409 This is a handy function to add to `gnus-summary-exit-hook' to
1410 increase the score of each group you read."
1411   (gnus-group-add-score gnus-newsgroup-name))
1412
1413 \f
1414 ;;;
1415 ;;; Gnus summary mode
1416 ;;;
1417
1418 (put 'gnus-summary-mode 'mode-class 'special)
1419
1420 (defvar gnus-article-commands-menu)
1421
1422 (when t
1423   ;; Non-orthogonal keys
1424
1425   (gnus-define-keys gnus-summary-mode-map
1426     " " gnus-summary-next-page
1427     "\177" gnus-summary-prev-page
1428     [delete] gnus-summary-prev-page
1429     [backspace] gnus-summary-prev-page
1430     "\r" gnus-summary-scroll-up
1431     "\M-\r" gnus-summary-scroll-down
1432     "n" gnus-summary-next-unread-article
1433     "p" gnus-summary-prev-unread-article
1434     "N" gnus-summary-next-article
1435     "P" gnus-summary-prev-article
1436     "\M-\C-n" gnus-summary-next-same-subject
1437     "\M-\C-p" gnus-summary-prev-same-subject
1438     "\M-n" gnus-summary-next-unread-subject
1439     "\M-p" gnus-summary-prev-unread-subject
1440     "." gnus-summary-first-unread-article
1441     "," gnus-summary-best-unread-article
1442     "\M-s" gnus-summary-search-article-forward
1443     "\M-r" gnus-summary-search-article-backward
1444     "<" gnus-summary-beginning-of-article
1445     ">" gnus-summary-end-of-article
1446     "j" gnus-summary-goto-article
1447     "^" gnus-summary-refer-parent-article
1448     "\M-^" gnus-summary-refer-article
1449     "u" gnus-summary-tick-article-forward
1450     "!" gnus-summary-tick-article-forward
1451     "U" gnus-summary-tick-article-backward
1452     "d" gnus-summary-mark-as-read-forward
1453     "D" gnus-summary-mark-as-read-backward
1454     "E" gnus-summary-mark-as-expirable
1455     "\M-u" gnus-summary-clear-mark-forward
1456     "\M-U" gnus-summary-clear-mark-backward
1457     "k" gnus-summary-kill-same-subject-and-select
1458     "\C-k" gnus-summary-kill-same-subject
1459     "\M-\C-k" gnus-summary-kill-thread
1460     "\M-\C-l" gnus-summary-lower-thread
1461     "e" gnus-summary-edit-article
1462     "#" gnus-summary-mark-as-processable
1463     "\M-#" gnus-summary-unmark-as-processable
1464     "\M-\C-t" gnus-summary-toggle-threads
1465     "\M-\C-s" gnus-summary-show-thread
1466     "\M-\C-h" gnus-summary-hide-thread
1467     "\M-\C-f" gnus-summary-next-thread
1468     "\M-\C-b" gnus-summary-prev-thread
1469     [(meta down)] gnus-summary-next-thread
1470     [(meta up)] gnus-summary-prev-thread
1471     "\M-\C-u" gnus-summary-up-thread
1472     "\M-\C-d" gnus-summary-down-thread
1473     "&" gnus-summary-execute-command
1474     "c" gnus-summary-catchup-and-exit
1475     "\C-w" gnus-summary-mark-region-as-read
1476     "\C-t" gnus-summary-toggle-truncation
1477     "?" gnus-summary-mark-as-dormant
1478     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1479     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1480     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1481     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1482     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1483     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1484     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1485     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1486     "=" gnus-summary-expand-window
1487     "\C-x\C-s" gnus-summary-reselect-current-group
1488     "\M-g" gnus-summary-rescan-group
1489     "w" gnus-summary-stop-page-breaking
1490     "\C-c\C-r" gnus-summary-caesar-message
1491     "\M-t" gnus-summary-toggle-mime
1492     "f" gnus-summary-followup
1493     "F" gnus-summary-followup-with-original
1494     "C" gnus-summary-cancel-article
1495     "r" gnus-summary-reply
1496     "R" gnus-summary-reply-with-original
1497     "\C-c\C-f" gnus-summary-mail-forward
1498     "o" gnus-summary-save-article
1499     "\C-o" gnus-summary-save-article-mail
1500     "|" gnus-summary-pipe-output
1501     "\M-k" gnus-summary-edit-local-kill
1502     "\M-K" gnus-summary-edit-global-kill
1503     ;; "V" gnus-version
1504     "\C-c\C-d" gnus-summary-describe-group
1505     "q" gnus-summary-exit
1506     "Q" gnus-summary-exit-no-update
1507     "\C-c\C-i" gnus-info-find-node
1508     gnus-mouse-2 gnus-mouse-pick-article
1509     "m" gnus-summary-mail-other-window
1510     "a" gnus-summary-post-news
1511     "x" gnus-summary-limit-to-unread
1512     "s" gnus-summary-isearch-article
1513     "t" gnus-article-toggle-headers
1514     "g" gnus-summary-show-article
1515     "l" gnus-summary-goto-last-article
1516     "v" gnus-summary-preview-mime-message
1517     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1518     "\C-d" gnus-summary-enter-digest-group
1519     "\M-\C-d" gnus-summary-read-document
1520     "\M-\C-e" gnus-summary-edit-parameters
1521     "\M-\C-a" gnus-summary-customize-parameters
1522     "\C-c\C-b" gnus-bug
1523     "*" gnus-cache-enter-article
1524     "\M-*" gnus-cache-remove-article
1525     "\M-&" gnus-summary-universal-argument
1526     "\C-l" gnus-recenter
1527     "I" gnus-summary-increase-score
1528     "L" gnus-summary-lower-score
1529     "\M-i" gnus-symbolic-argument
1530     "h" gnus-summary-select-article-buffer
1531
1532     "V" gnus-summary-score-map
1533     "X" gnus-uu-extract-map
1534     "S" gnus-summary-send-map)
1535
1536   ;; Sort of orthogonal keymap
1537   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1538     "t" gnus-summary-tick-article-forward
1539     "!" gnus-summary-tick-article-forward
1540     "d" gnus-summary-mark-as-read-forward
1541     "r" gnus-summary-mark-as-read-forward
1542     "c" gnus-summary-clear-mark-forward
1543     " " gnus-summary-clear-mark-forward
1544     "e" gnus-summary-mark-as-expirable
1545     "x" gnus-summary-mark-as-expirable
1546     "?" gnus-summary-mark-as-dormant
1547     "b" gnus-summary-set-bookmark
1548     "B" gnus-summary-remove-bookmark
1549     "#" gnus-summary-mark-as-processable
1550     "\M-#" gnus-summary-unmark-as-processable
1551     "S" gnus-summary-limit-include-expunged
1552     "C" gnus-summary-catchup
1553     "H" gnus-summary-catchup-to-here
1554     "\C-c" gnus-summary-catchup-all
1555     "k" gnus-summary-kill-same-subject-and-select
1556     "K" gnus-summary-kill-same-subject
1557     "P" gnus-uu-mark-map)
1558
1559   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1560     "c" gnus-summary-clear-above
1561     "u" gnus-summary-tick-above
1562     "m" gnus-summary-mark-above
1563     "k" gnus-summary-kill-below)
1564
1565   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1566     "/" gnus-summary-limit-to-subject
1567     "n" gnus-summary-limit-to-articles
1568     "w" gnus-summary-pop-limit
1569     "s" gnus-summary-limit-to-subject
1570     "a" gnus-summary-limit-to-author
1571     "u" gnus-summary-limit-to-unread
1572     "m" gnus-summary-limit-to-marks
1573     "M" gnus-summary-limit-exclude-marks
1574     "v" gnus-summary-limit-to-score
1575     "*" gnus-summary-limit-include-cached
1576     "D" gnus-summary-limit-include-dormant
1577     "T" gnus-summary-limit-include-thread
1578     "d" gnus-summary-limit-exclude-dormant
1579     "t" gnus-summary-limit-to-age
1580     "x" gnus-summary-limit-to-extra
1581     "E" gnus-summary-limit-include-expunged
1582     "c" gnus-summary-limit-exclude-childless-dormant
1583     "C" gnus-summary-limit-mark-excluded-as-read
1584     "o" gnus-summary-insert-old-articles
1585     "N" gnus-summary-insert-new-articles)
1586
1587   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1588     "n" gnus-summary-next-unread-article
1589     "p" gnus-summary-prev-unread-article
1590     "N" gnus-summary-next-article
1591     "P" gnus-summary-prev-article
1592     "\C-n" gnus-summary-next-same-subject
1593     "\C-p" gnus-summary-prev-same-subject
1594     "\M-n" gnus-summary-next-unread-subject
1595     "\M-p" gnus-summary-prev-unread-subject
1596     "f" gnus-summary-first-unread-article
1597     "b" gnus-summary-best-unread-article
1598     "j" gnus-summary-goto-article
1599     "g" gnus-summary-goto-subject
1600     "l" gnus-summary-goto-last-article
1601     "o" gnus-summary-pop-article)
1602
1603   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1604     "k" gnus-summary-kill-thread
1605     "l" gnus-summary-lower-thread
1606     "i" gnus-summary-raise-thread
1607     "T" gnus-summary-toggle-threads
1608     "t" gnus-summary-rethread-current
1609     "^" gnus-summary-reparent-thread
1610     "s" gnus-summary-show-thread
1611     "S" gnus-summary-show-all-threads
1612     "h" gnus-summary-hide-thread
1613     "H" gnus-summary-hide-all-threads
1614     "n" gnus-summary-next-thread
1615     "p" gnus-summary-prev-thread
1616     "u" gnus-summary-up-thread
1617     "o" gnus-summary-top-thread
1618     "d" gnus-summary-down-thread
1619     "#" gnus-uu-mark-thread
1620     "\M-#" gnus-uu-unmark-thread)
1621
1622   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1623     "g" gnus-summary-prepare
1624     "c" gnus-summary-insert-cached-articles)
1625
1626   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1627     "c" gnus-summary-catchup-and-exit
1628     "C" gnus-summary-catchup-all-and-exit
1629     "E" gnus-summary-exit-no-update
1630     "J" gnus-summary-jump-to-other-group
1631     "Q" gnus-summary-exit
1632     "Z" gnus-summary-exit
1633     "n" gnus-summary-catchup-and-goto-next-group
1634     "R" gnus-summary-reselect-current-group
1635     "G" gnus-summary-rescan-group
1636     "N" gnus-summary-next-group
1637     "s" gnus-summary-save-newsrc
1638     "P" gnus-summary-prev-group)
1639
1640   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1641     " " gnus-summary-next-page
1642     "n" gnus-summary-next-page
1643     "\177" gnus-summary-prev-page
1644     [delete] gnus-summary-prev-page
1645     "p" gnus-summary-prev-page
1646     "\r" gnus-summary-scroll-up
1647     "\M-\r" gnus-summary-scroll-down
1648     "<" gnus-summary-beginning-of-article
1649     ">" gnus-summary-end-of-article
1650     "b" gnus-summary-beginning-of-article
1651     "e" gnus-summary-end-of-article
1652     "^" gnus-summary-refer-parent-article
1653     "r" gnus-summary-refer-parent-article
1654     "D" gnus-summary-enter-digest-group
1655     "R" gnus-summary-refer-references
1656     "T" gnus-summary-refer-thread
1657     "g" gnus-summary-show-article
1658     "s" gnus-summary-isearch-article
1659     "P" gnus-summary-print-article
1660     "M" gnus-mailing-list-insinuate
1661     "t" gnus-article-babel)
1662
1663   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1664     "b" gnus-article-add-buttons
1665     "B" gnus-article-add-buttons-to-head
1666     "o" gnus-article-treat-overstrike
1667     "e" gnus-article-emphasize
1668     "w" gnus-article-fill-cited-article
1669     "Q" gnus-article-fill-long-lines
1670     "C" gnus-article-capitalize-sentences
1671     "c" gnus-article-remove-cr
1672     "Z" gnus-article-decode-HZ
1673     "f" gnus-article-display-x-face
1674     "l" gnus-summary-stop-page-breaking
1675     "r" gnus-summary-caesar-message
1676     "t" gnus-article-toggle-headers
1677     "v" gnus-summary-verbose-headers
1678     "m" gnus-summary-toggle-mime
1679     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1680     "p" gnus-article-verify-x-pgp-sig
1681     "d" gnus-article-treat-dumbquotes
1682     "s" gnus-smiley-display)
1683
1684   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1685     "a" gnus-article-hide
1686     "h" gnus-article-toggle-headers
1687     "b" gnus-article-hide-boring-headers
1688     "s" gnus-article-hide-signature
1689     "c" gnus-article-hide-citation
1690     "C" gnus-article-hide-citation-in-followups
1691     "l" gnus-article-hide-list-identifiers
1692     "p" gnus-article-hide-pgp
1693     "B" gnus-article-strip-banner
1694     "P" gnus-article-hide-pem
1695     "\C-c" gnus-article-hide-citation-maybe)
1696
1697   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1698     "a" gnus-article-highlight
1699     "h" gnus-article-highlight-headers
1700     "c" gnus-article-highlight-citation
1701     "s" gnus-article-highlight-signature)
1702
1703   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1704     "z" gnus-article-date-ut
1705     "u" gnus-article-date-ut
1706     "l" gnus-article-date-local
1707     "p" gnus-article-date-english
1708     "e" gnus-article-date-lapsed
1709     "o" gnus-article-date-original
1710     "i" gnus-article-date-iso8601
1711     "s" gnus-article-date-user)
1712
1713   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1714     "t" gnus-article-remove-trailing-blank-lines
1715     "l" gnus-article-strip-leading-blank-lines
1716     "m" gnus-article-strip-multiple-blank-lines
1717     "a" gnus-article-strip-blank-lines
1718     "A" gnus-article-strip-all-blank-lines
1719     "s" gnus-article-strip-leading-space
1720     "e" gnus-article-strip-trailing-space
1721     "w" gnus-article-remove-leading-whitespace)
1722
1723   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1724     "v" gnus-version
1725     "f" gnus-summary-fetch-faq
1726     "d" gnus-summary-describe-group
1727     "h" gnus-summary-describe-briefly
1728     "i" gnus-info-find-node)
1729
1730   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1731     "e" gnus-summary-expire-articles
1732     "\M-\C-e" gnus-summary-expire-articles-now
1733     "\177" gnus-summary-delete-article
1734     [delete] gnus-summary-delete-article
1735     [backspace] gnus-summary-delete-article
1736     "m" gnus-summary-move-article
1737     "r" gnus-summary-respool-article
1738     "w" gnus-summary-edit-article
1739     "c" gnus-summary-copy-article
1740     "B" gnus-summary-crosspost-article
1741     "q" gnus-summary-respool-query
1742     "t" gnus-summary-respool-trace
1743     "i" gnus-summary-import-article
1744     "p" gnus-summary-article-posted-p)
1745
1746   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1747     "o" gnus-summary-save-article
1748     "m" gnus-summary-save-article-mail
1749     "F" gnus-summary-write-article-file
1750     "r" gnus-summary-save-article-rmail
1751     "f" gnus-summary-save-article-file
1752     "b" gnus-summary-save-article-body-file
1753     "h" gnus-summary-save-article-folder
1754     "v" gnus-summary-save-article-vm
1755     "p" gnus-summary-pipe-output
1756     "s" gnus-soup-add-article)
1757
1758   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1759     "b" gnus-summary-display-buttonized
1760     "m" gnus-summary-repair-multipart
1761     "v" gnus-article-view-part
1762     "o" gnus-article-save-part
1763     "c" gnus-article-copy-part
1764     "C" gnus-article-view-part-as-charset
1765     "e" gnus-article-externalize-part
1766     "E" gnus-article-encrypt-body
1767     "i" gnus-article-inline-part
1768     "|" gnus-article-pipe-part))
1769
1770 (defun gnus-summary-make-menu-bar ()
1771   (gnus-turn-off-edit-menu 'summary)
1772
1773   (unless (boundp 'gnus-summary-misc-menu)
1774
1775     (easy-menu-define
1776      gnus-summary-kill-menu gnus-summary-mode-map ""
1777      (cons
1778       "Score"
1779       (nconc
1780        (list
1781         ["Customize" gnus-score-customize t])
1782        (gnus-make-score-map 'increase)
1783        (gnus-make-score-map 'lower)
1784        '(("Mark"
1785           ["Kill below" gnus-summary-kill-below t]
1786           ["Mark above" gnus-summary-mark-above t]
1787           ["Tick above" gnus-summary-tick-above t]
1788           ["Clear above" gnus-summary-clear-above t])
1789          ["Current score" gnus-summary-current-score t]
1790          ["Set score" gnus-summary-set-score t]
1791          ["Switch current score file..." gnus-score-change-score-file t]
1792          ["Set mark below..." gnus-score-set-mark-below t]
1793          ["Set expunge below..." gnus-score-set-expunge-below t]
1794          ["Edit current score file" gnus-score-edit-current-scores t]
1795          ["Edit score file" gnus-score-edit-file t]
1796          ["Trace score" gnus-score-find-trace t]
1797          ["Find words" gnus-score-find-favourite-words t]
1798          ["Rescore buffer" gnus-summary-rescore t]
1799          ["Increase score..." gnus-summary-increase-score t]
1800          ["Lower score..." gnus-summary-lower-score t]))))
1801
1802     ;; Define both the Article menu in the summary buffer and the equivalent
1803     ;; Commands menu in the article buffer here for consistency.
1804     (let ((innards
1805            `(("Hide"
1806               ["All" gnus-article-hide t]
1807               ["Headers" gnus-article-toggle-headers t]
1808               ["Signature" gnus-article-hide-signature t]
1809               ["Citation" gnus-article-hide-citation t]
1810               ["List identifiers" gnus-article-hide-list-identifiers t]
1811               ["PGP" gnus-article-hide-pgp t]
1812               ["Banner" gnus-article-strip-banner t]
1813               ["Boring headers" gnus-article-hide-boring-headers t])
1814              ("Highlight"
1815               ["All" gnus-article-highlight t]
1816               ["Headers" gnus-article-highlight-headers t]
1817               ["Signature" gnus-article-highlight-signature t]
1818               ["Citation" gnus-article-highlight-citation t])
1819              ("Date"
1820               ["Local" gnus-article-date-local t]
1821               ["ISO8601" gnus-article-date-iso8601 t]
1822               ["UT" gnus-article-date-ut t]
1823               ["Original" gnus-article-date-original t]
1824               ["Lapsed" gnus-article-date-lapsed t]
1825               ["User-defined" gnus-article-date-user t])
1826              ("Washing"
1827               ("Remove Blanks"
1828                ["Leading" gnus-article-strip-leading-blank-lines t]
1829                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1830                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1831                ["All of the above" gnus-article-strip-blank-lines t]
1832                ["All" gnus-article-strip-all-blank-lines t]
1833                ["Leading space" gnus-article-strip-leading-space t]
1834                ["Trailing space" gnus-article-strip-trailing-space t]
1835                ["Leading space in headers" 
1836                 gnus-article-remove-leading-whitespace t])
1837               ["Overstrike" gnus-article-treat-overstrike t]
1838               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1839               ["Emphasis" gnus-article-emphasize t]
1840               ["Word wrap" gnus-article-fill-cited-article t]
1841               ["Fill long lines" gnus-article-fill-long-lines t]
1842               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1843               ["CR" gnus-article-remove-cr t]
1844               ["Show X-Face" gnus-article-display-x-face t]
1845               ["Rot 13" gnus-summary-caesar-message
1846                ,@(if (featurep 'xemacs) '(t)
1847                    '(:help "\"Caesar rotate\" article by 13"))]
1848               ["Unix pipe" gnus-summary-pipe-message t]
1849               ["Add buttons" gnus-article-add-buttons t]
1850               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1851               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1852               ["Toggle MIME" gnus-summary-toggle-mime t]
1853               ["Verbose header" gnus-summary-verbose-headers t]
1854               ["Toggle header" gnus-summary-toggle-header t]
1855               ["Toggle smileys" gnus-smiley-display t]
1856               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1857               ["HZ" gnus-article-decode-HZ t])
1858              ("Output"
1859               ["Save in default format" gnus-summary-save-article
1860                ,@(if (featurep 'xemacs) '(t)
1861                    '(:help "Save article using default method"))]
1862               ["Save in file" gnus-summary-save-article-file
1863                ,@(if (featurep 'xemacs) '(t)
1864                    '(:help "Save article in file"))]
1865               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1866               ["Save in MH folder" gnus-summary-save-article-folder t]
1867               ["Save in VM folder" gnus-summary-save-article-vm t]
1868               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1869               ["Save body in file" gnus-summary-save-article-body-file t]
1870               ["Pipe through a filter" gnus-summary-pipe-output t]
1871               ["Add to SOUP packet" gnus-soup-add-article t]
1872               ["Print" gnus-summary-print-article t])
1873              ("Backend"
1874               ["Respool article..." gnus-summary-respool-article t]
1875               ["Move article..." gnus-summary-move-article
1876                (gnus-check-backend-function
1877                 'request-move-article gnus-newsgroup-name)]
1878               ["Copy article..." gnus-summary-copy-article t]
1879               ["Crosspost article..." gnus-summary-crosspost-article
1880                (gnus-check-backend-function
1881                 'request-replace-article gnus-newsgroup-name)]
1882               ["Import file..." gnus-summary-import-article t]
1883               ["Check if posted" gnus-summary-article-posted-p t]
1884               ["Edit article" gnus-summary-edit-article
1885                (not (gnus-group-read-only-p))]
1886               ["Delete article" gnus-summary-delete-article
1887                (gnus-check-backend-function
1888                 'request-expire-articles gnus-newsgroup-name)]
1889               ["Query respool" gnus-summary-respool-query t]
1890               ["Trace respool" gnus-summary-respool-trace t]
1891               ["Delete expirable articles" gnus-summary-expire-articles-now
1892                (gnus-check-backend-function
1893                 'request-expire-articles gnus-newsgroup-name)])
1894              ("Extract"
1895               ["Uudecode" gnus-uu-decode-uu
1896                ,@(if (featurep 'xemacs) '(t)
1897                    '(:help "Decode uuencoded article(s)"))]
1898               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1899               ["Unshar" gnus-uu-decode-unshar t]
1900               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1901               ["Save" gnus-uu-decode-save t]
1902               ["Binhex" gnus-uu-decode-binhex t]
1903               ["Postscript" gnus-uu-decode-postscript t])
1904              ("Cache"
1905               ["Enter article" gnus-cache-enter-article t]
1906               ["Remove article" gnus-cache-remove-article t])
1907              ["Translate" gnus-article-babel t]
1908              ["Select article buffer" gnus-summary-select-article-buffer t]
1909              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1910              ["Isearch article..." gnus-summary-isearch-article t]
1911              ["Beginning of the article" gnus-summary-beginning-of-article t]
1912              ["End of the article" gnus-summary-end-of-article t]
1913              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1914              ["Fetch referenced articles" gnus-summary-refer-references t]
1915              ["Fetch current thread" gnus-summary-refer-thread t]
1916              ["Fetch article with id..." gnus-summary-refer-article t]
1917              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1918              ["Redisplay" gnus-summary-show-article t])))
1919       (easy-menu-define
1920        gnus-summary-article-menu gnus-summary-mode-map ""
1921        (cons "Article" innards))
1922
1923       (if (not (keymapp gnus-summary-article-menu))
1924           (easy-menu-define
1925             gnus-article-commands-menu gnus-article-mode-map ""
1926             (cons "Commands" innards))
1927         ;; in Emacs, don't share menu.
1928         (setq gnus-article-commands-menu 
1929               (copy-keymap gnus-summary-article-menu))
1930         (define-key gnus-article-mode-map [menu-bar commands]
1931           (cons "Commands" gnus-article-commands-menu))))
1932
1933     (easy-menu-define
1934      gnus-summary-thread-menu gnus-summary-mode-map ""
1935      '("Threads"
1936        ["Toggle threading" gnus-summary-toggle-threads t]
1937        ["Hide threads" gnus-summary-hide-all-threads t]
1938        ["Show threads" gnus-summary-show-all-threads t]
1939        ["Hide thread" gnus-summary-hide-thread t]
1940        ["Show thread" gnus-summary-show-thread t]
1941        ["Go to next thread" gnus-summary-next-thread t]
1942        ["Go to previous thread" gnus-summary-prev-thread t]
1943        ["Go down thread" gnus-summary-down-thread t]
1944        ["Go up thread" gnus-summary-up-thread t]
1945        ["Top of thread" gnus-summary-top-thread t]
1946        ["Mark thread as read" gnus-summary-kill-thread t]
1947        ["Lower thread score" gnus-summary-lower-thread t]
1948        ["Raise thread score" gnus-summary-raise-thread t]
1949        ["Rethread current" gnus-summary-rethread-current t]))
1950
1951     (easy-menu-define
1952      gnus-summary-post-menu gnus-summary-mode-map ""
1953      `("Post"
1954        ["Post an article" gnus-summary-post-news
1955         ,@(if (featurep 'xemacs) '(t)
1956             '(:help "Post an article"))]
1957        ["Followup" gnus-summary-followup
1958         ,@(if (featurep 'xemacs) '(t)
1959             '(:help "Post followup to this article"))]
1960        ["Followup and yank" gnus-summary-followup-with-original
1961         ,@(if (featurep 'xemacs) '(t)
1962             '(:help "Post followup to this article, quoting its contents"))]
1963        ["Supersede article" gnus-summary-supersede-article t]
1964        ["Cancel article" gnus-summary-cancel-article
1965         ,@(if (featurep 'xemacs) '(t)
1966             '(:help "Cancel an article you posted"))]
1967        ["Reply" gnus-summary-reply t]
1968        ["Reply and yank" gnus-summary-reply-with-original t]
1969        ["Wide reply" gnus-summary-wide-reply t]
1970        ["Wide reply and yank" gnus-summary-wide-reply-with-original
1971         ,@(if (featurep 'xemacs) '(t)
1972             '(:help "Mail a reply, quoting this article"))]
1973        ["Mail forward" gnus-summary-mail-forward t]
1974        ["Post forward" gnus-summary-post-forward t]
1975        ["Digest and mail" gnus-uu-digest-mail-forward t]
1976        ["Digest and post" gnus-uu-digest-post-forward t]
1977        ["Resend message" gnus-summary-resend-message t]
1978        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1979        ["Send a mail" gnus-summary-mail-other-window t]
1980        ["Uuencode and post" gnus-uu-post-news
1981         ,@(if (featurep 'xemacs) '(t)
1982             '(:help "Post a uuencoded article"))]
1983        ["Followup via news" gnus-summary-followup-to-mail t]
1984        ["Followup via news and yank"
1985         gnus-summary-followup-to-mail-with-original t]
1986        ;;("Draft"
1987        ;;["Send" gnus-summary-send-draft t]
1988        ;;["Send bounced" gnus-resend-bounced-mail t])
1989        ))
1990
1991     (easy-menu-define
1992      gnus-summary-misc-menu gnus-summary-mode-map ""
1993      `("Misc"
1994        ("Mark Read"
1995         ["Mark as read" gnus-summary-mark-as-read-forward t]
1996         ["Mark same subject and select"
1997          gnus-summary-kill-same-subject-and-select t]
1998         ["Mark same subject" gnus-summary-kill-same-subject t]
1999         ["Catchup" gnus-summary-catchup
2000          ,@(if (featurep 'xemacs) '(t)
2001              '(:help "Mark unread articles in this group as read"))]
2002         ["Catchup all" gnus-summary-catchup-all t]
2003         ["Catchup to here" gnus-summary-catchup-to-here t]
2004         ["Catchup region" gnus-summary-mark-region-as-read t]
2005         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2006        ("Mark Various"
2007         ["Tick" gnus-summary-tick-article-forward t]
2008         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2009         ["Remove marks" gnus-summary-clear-mark-forward t]
2010         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2011         ["Set bookmark" gnus-summary-set-bookmark t]
2012         ["Remove bookmark" gnus-summary-remove-bookmark t])
2013        ("Mark Limit"
2014         ["Marks..." gnus-summary-limit-to-marks t]
2015         ["Subject..." gnus-summary-limit-to-subject t]
2016         ["Author..." gnus-summary-limit-to-author t]
2017         ["Age..." gnus-summary-limit-to-age t]
2018         ["Extra..." gnus-summary-limit-to-extra t]
2019         ["Score" gnus-summary-limit-to-score t]
2020         ["Unread" gnus-summary-limit-to-unread t]
2021         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2022         ["Articles" gnus-summary-limit-to-articles t]
2023         ["Pop limit" gnus-summary-pop-limit t]
2024         ["Show dormant" gnus-summary-limit-include-dormant t]
2025         ["Hide childless dormant"
2026          gnus-summary-limit-exclude-childless-dormant t]
2027         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2028         ["Hide marked" gnus-summary-limit-exclude-marks t]
2029         ["Show expunged" gnus-summary-show-all-expunged t])
2030        ("Process Mark"
2031         ["Set mark" gnus-summary-mark-as-processable t]
2032         ["Remove mark" gnus-summary-unmark-as-processable t]
2033         ["Remove all marks" gnus-summary-unmark-all-processable t]
2034         ["Mark above" gnus-uu-mark-over t]
2035         ["Mark series" gnus-uu-mark-series t]
2036         ["Mark region" gnus-uu-mark-region t]
2037         ["Unmark region" gnus-uu-unmark-region t]
2038         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2039         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2040         ["Mark all" gnus-uu-mark-all t]
2041         ["Mark buffer" gnus-uu-mark-buffer t]
2042         ["Mark sparse" gnus-uu-mark-sparse t]
2043         ["Mark thread" gnus-uu-mark-thread t]
2044         ["Unmark thread" gnus-uu-unmark-thread t]
2045         ("Process Mark Sets"
2046          ["Kill" gnus-summary-kill-process-mark t]
2047          ["Yank" gnus-summary-yank-process-mark
2048           gnus-newsgroup-process-stack]
2049          ["Save" gnus-summary-save-process-mark t]))
2050        ("Scroll article"
2051         ["Page forward" gnus-summary-next-page
2052          ,@(if (featurep 'xemacs) '(t)
2053              '(:help "Show next page of article"))]
2054         ["Page backward" gnus-summary-prev-page
2055          ,@(if (featurep 'xemacs) '(t)
2056              '(:help "Show previous page of article"))]
2057         ["Line forward" gnus-summary-scroll-up t])
2058        ("Move"
2059         ["Next unread article" gnus-summary-next-unread-article t]
2060         ["Previous unread article" gnus-summary-prev-unread-article t]
2061         ["Next article" gnus-summary-next-article t]
2062         ["Previous article" gnus-summary-prev-article t]
2063         ["Next unread subject" gnus-summary-next-unread-subject t]
2064         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2065         ["Next article same subject" gnus-summary-next-same-subject t]
2066         ["Previous article same subject" gnus-summary-prev-same-subject t]
2067         ["First unread article" gnus-summary-first-unread-article t]
2068         ["Best unread article" gnus-summary-best-unread-article t]
2069         ["Go to subject number..." gnus-summary-goto-subject t]
2070         ["Go to article number..." gnus-summary-goto-article t]
2071         ["Go to the last article" gnus-summary-goto-last-article t]
2072         ["Pop article off history" gnus-summary-pop-article t])
2073        ("Sort"
2074         ["Sort by number" gnus-summary-sort-by-number t]
2075         ["Sort by author" gnus-summary-sort-by-author t]
2076         ["Sort by subject" gnus-summary-sort-by-subject t]
2077         ["Sort by date" gnus-summary-sort-by-date t]
2078         ["Sort by score" gnus-summary-sort-by-score t]
2079         ["Sort by lines" gnus-summary-sort-by-lines t]
2080         ["Sort by characters" gnus-summary-sort-by-chars t])
2081        ("Help"
2082         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2083         ["Describe group" gnus-summary-describe-group t]
2084         ["Read manual" gnus-info-find-node t])
2085        ("Modes"
2086         ["Pick and read" gnus-pick-mode t]
2087         ["Binary" gnus-binary-mode t])
2088        ("Regeneration"
2089         ["Regenerate" gnus-summary-prepare t]
2090         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2091         ["Toggle threading" gnus-summary-toggle-threads t])
2092        ["See old articles" gnus-summary-insert-old-articles t]
2093        ["See new articles" gnus-summary-insert-new-articles t]
2094        ["Filter articles..." gnus-summary-execute-command t]
2095        ["Run command on subjects..." gnus-summary-universal-argument t]
2096        ["Search articles forward..." gnus-summary-search-article-forward t]
2097        ["Search articles backward..." gnus-summary-search-article-backward t]
2098        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2099        ["Expand window" gnus-summary-expand-window t]
2100        ["Expire expirable articles" gnus-summary-expire-articles
2101         (gnus-check-backend-function
2102          'request-expire-articles gnus-newsgroup-name)]
2103        ["Edit local kill file" gnus-summary-edit-local-kill t]
2104        ["Edit main kill file" gnus-summary-edit-global-kill t]
2105        ["Edit group parameters" gnus-summary-edit-parameters t]
2106        ["Customize group parameters" gnus-summary-customize-parameters t]
2107        ["Send a bug report" gnus-bug t]
2108        ("Exit"
2109         ["Catchup and exit" gnus-summary-catchup-and-exit
2110          ,@(if (featurep 'xemacs) '(t)
2111              '(:help "Mark unread articles in this group as read, then exit"))]
2112         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2113         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2114         ["Exit group" gnus-summary-exit
2115          ,@(if (featurep 'xemacs) '(t)
2116              '(:help "Exit current group, return to group selection mode"))]
2117         ["Exit group without updating" gnus-summary-exit-no-update t]
2118         ["Exit and goto next group" gnus-summary-next-group t]
2119         ["Exit and goto prev group" gnus-summary-prev-group t]
2120         ["Reselect group" gnus-summary-reselect-current-group t]
2121         ["Rescan group" gnus-summary-rescan-group t]
2122         ["Update dribble" gnus-summary-save-newsrc t])))
2123
2124     (gnus-run-hooks 'gnus-summary-menu-hook)))
2125
2126 (defvar gnus-summary-tool-bar-map nil)
2127
2128 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2129 (defun gnus-summary-make-tool-bar ()
2130   (if (and (fboundp 'tool-bar-add-item-from-menu)
2131            (default-value 'tool-bar-mode)
2132            (not gnus-summary-tool-bar-map))
2133       (setq gnus-summary-tool-bar-map
2134             (let ((tool-bar-map (make-sparse-keymap))
2135                   (load-path (mm-image-load-path)))
2136               (tool-bar-add-item-from-menu
2137                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2138               (tool-bar-add-item-from-menu
2139                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2140               (tool-bar-add-item-from-menu
2141                'gnus-summary-post-news "post" gnus-summary-mode-map)
2142               (tool-bar-add-item-from-menu
2143                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2144               (tool-bar-add-item-from-menu
2145                'gnus-summary-followup "followup" gnus-summary-mode-map)
2146               (tool-bar-add-item-from-menu
2147                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2148               (tool-bar-add-item-from-menu
2149                'gnus-summary-reply "reply" gnus-summary-mode-map)
2150               (tool-bar-add-item-from-menu
2151                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2152               (tool-bar-add-item-from-menu
2153                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2154               (tool-bar-add-item-from-menu
2155                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2156               (tool-bar-add-item-from-menu
2157                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2158               (tool-bar-add-item-from-menu
2159                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2160               (tool-bar-add-item-from-menu
2161                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2162               (tool-bar-add-item-from-menu
2163                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2164               (tool-bar-add-item-from-menu
2165                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2166               tool-bar-map)))
2167   (if gnus-summary-tool-bar-map
2168       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2169
2170 (defun gnus-score-set-default (var value)
2171   "A version of set that updates the GNU Emacs menu-bar."
2172   (set var value)
2173   ;; It is the message that forces the active status to be updated.
2174   (message ""))
2175
2176 (defun gnus-make-score-map (type)
2177   "Make a summary score map of type TYPE."
2178   (if t
2179       nil
2180     (let ((headers '(("author" "from" string)
2181                      ("subject" "subject" string)
2182                      ("article body" "body" string)
2183                      ("article head" "head" string)
2184                      ("xref" "xref" string)
2185                      ("extra header" "extra" string)
2186                      ("lines" "lines" number)
2187                      ("followups to author" "followup" string)))
2188           (types '((number ("less than" <)
2189                            ("greater than" >)
2190                            ("equal" =))
2191                    (string ("substring" s)
2192                            ("exact string" e)
2193                            ("fuzzy string" f)
2194                            ("regexp" r))))
2195           (perms '(("temporary" (current-time-string))
2196                    ("permanent" nil)
2197                    ("immediate" now)))
2198           header)
2199       (list
2200        (apply
2201         'nconc
2202         (list
2203          (if (eq type 'lower)
2204              "Lower score"
2205            "Increase score"))
2206         (let (outh)
2207           (while headers
2208             (setq header (car headers))
2209             (setq outh
2210                   (cons
2211                    (apply
2212                     'nconc
2213                     (list (car header))
2214                     (let ((ts (cdr (assoc (nth 2 header) types)))
2215                           outt)
2216                       (while ts
2217                         (setq outt
2218                               (cons
2219                                (apply
2220                                 'nconc
2221                                 (list (caar ts))
2222                                 (let ((ps perms)
2223                                       outp)
2224                                   (while ps
2225                                     (setq outp
2226                                           (cons
2227                                            (vector
2228                                             (caar ps)
2229                                             (list
2230                                              'gnus-summary-score-entry
2231                                              (nth 1 header)
2232                                              (if (or (string= (nth 1 header)
2233                                                               "head")
2234                                                      (string= (nth 1 header)
2235                                                               "body"))
2236                                                  ""
2237                                                (list 'gnus-summary-header
2238                                                      (nth 1 header)))
2239                                              (list 'quote (nth 1 (car ts)))
2240                                              (list 'gnus-score-delta-default
2241                                                    nil)
2242                                              (nth 1 (car ps))
2243                                              t)
2244                                             t)
2245                                            outp))
2246                                     (setq ps (cdr ps)))
2247                                   (list (nreverse outp))))
2248                                outt))
2249                         (setq ts (cdr ts)))
2250                       (list (nreverse outt))))
2251                    outh))
2252             (setq headers (cdr headers)))
2253           (list (nreverse outh))))))))
2254
2255 \f
2256
2257 (defun gnus-summary-mode (&optional group)
2258   "Major mode for reading articles.
2259
2260 All normal editing commands are switched off.
2261 \\<gnus-summary-mode-map>
2262 Each line in this buffer represents one article.  To read an
2263 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2264 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2265 respectively.
2266
2267 You can also post articles and send mail from this buffer.  To
2268 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2269 of an article, type `\\[gnus-summary-reply]'.
2270
2271 There are approx. one gazillion commands you can execute in this
2272 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2273
2274 The following commands are available:
2275
2276 \\{gnus-summary-mode-map}"
2277   (interactive)
2278   (kill-all-local-variables)
2279   (when (gnus-visual-p 'summary-menu 'menu)
2280     (gnus-summary-make-menu-bar)
2281     (gnus-summary-make-tool-bar))
2282   (gnus-summary-make-local-variables)
2283   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2284     (gnus-summary-make-local-variables))
2285   (gnus-make-thread-indent-array)
2286   (gnus-simplify-mode-line)
2287   (setq major-mode 'gnus-summary-mode)
2288   (setq mode-name "Summary")
2289   (make-local-variable 'minor-mode-alist)
2290   (use-local-map gnus-summary-mode-map)
2291   (buffer-disable-undo)
2292   (setq buffer-read-only t)             ;Disable modification
2293   (setq truncate-lines t)
2294   (setq selective-display t)
2295   (setq selective-display-ellipses t)   ;Display `...'
2296   (gnus-summary-set-display-table)
2297   (gnus-set-default-directory)
2298   (setq gnus-newsgroup-name group)
2299   (unless (gnus-news-group-p group)
2300     (setq gnus-newsgroup-incorporated
2301           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2302   (make-local-variable 'gnus-summary-line-format)
2303   (make-local-variable 'gnus-summary-line-format-spec)
2304   (make-local-variable 'gnus-summary-dummy-line-format)
2305   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2306   (make-local-variable 'gnus-summary-mark-positions)
2307   (make-local-hook 'pre-command-hook)
2308   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2309   (gnus-run-hooks 'gnus-summary-mode-hook)
2310   (turn-on-gnus-mailing-list-mode)
2311   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2312   (gnus-update-summary-mark-positions))
2313
2314 (defun gnus-summary-make-local-variables ()
2315   "Make all the local summary buffer variables."
2316   (let (global)
2317     (dolist (local gnus-summary-local-variables)
2318       (if (consp local)
2319           (progn
2320             (if (eq (cdr local) 'global)
2321                 ;; Copy the global value of the variable.
2322                 (setq global (symbol-value (car local)))
2323               ;; Use the value from the list.
2324               (setq global (eval (cdr local))))
2325             (set (make-local-variable (car local)) global))
2326         ;; Simple nil-valued local variable.
2327         (set (make-local-variable local) nil)))))
2328
2329 (defun gnus-summary-clear-local-variables ()
2330   (let ((locals gnus-summary-local-variables))
2331     (while locals
2332       (if (consp (car locals))
2333           (and (vectorp (caar locals))
2334                (set (caar locals) nil))
2335         (and (vectorp (car locals))
2336              (set (car locals) nil)))
2337       (setq locals (cdr locals)))))
2338
2339 ;; Summary data functions.
2340
2341 (defmacro gnus-data-number (data)
2342   `(car ,data))
2343
2344 (defmacro gnus-data-set-number (data number)
2345   `(setcar ,data ,number))
2346
2347 (defmacro gnus-data-mark (data)
2348   `(nth 1 ,data))
2349
2350 (defmacro gnus-data-set-mark (data mark)
2351   `(setcar (nthcdr 1 ,data) ,mark))
2352
2353 (defmacro gnus-data-pos (data)
2354   `(nth 2 ,data))
2355
2356 (defmacro gnus-data-set-pos (data pos)
2357   `(setcar (nthcdr 2 ,data) ,pos))
2358
2359 (defmacro gnus-data-header (data)
2360   `(nth 3 ,data))
2361
2362 (defmacro gnus-data-set-header (data header)
2363   `(setcar (nthcdr 3 ,data) ,header))
2364
2365 (defmacro gnus-data-level (data)
2366   `(nth 4 ,data))
2367
2368 (defmacro gnus-data-unread-p (data)
2369   `(= (nth 1 ,data) gnus-unread-mark))
2370
2371 (defmacro gnus-data-read-p (data)
2372   `(/= (nth 1 ,data) gnus-unread-mark))
2373
2374 (defmacro gnus-data-pseudo-p (data)
2375   `(consp (nth 3 ,data)))
2376
2377 (defmacro gnus-data-find (number)
2378   `(assq ,number gnus-newsgroup-data))
2379
2380 (defmacro gnus-data-find-list (number &optional data)
2381   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2382      (memq (assq ,number bdata)
2383            bdata)))
2384
2385 (defmacro gnus-data-make (number mark pos header level)
2386   `(list ,number ,mark ,pos ,header ,level))
2387
2388 (defun gnus-data-enter (after-article number mark pos header level offset)
2389   (let ((data (gnus-data-find-list after-article)))
2390     (unless data
2391       (error "No such article: %d" after-article))
2392     (setcdr data (cons (gnus-data-make number mark pos header level)
2393                        (cdr data)))
2394     (setq gnus-newsgroup-data-reverse nil)
2395     (gnus-data-update-list (cddr data) offset)))
2396
2397 (defun gnus-data-enter-list (after-article list &optional offset)
2398   (when list
2399     (let ((data (and after-article (gnus-data-find-list after-article)))
2400           (ilist list))
2401       (if (not (or data
2402                    after-article))
2403           (let ((odata gnus-newsgroup-data))
2404             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2405             (when offset
2406               (gnus-data-update-list odata offset)))
2407         ;; Find the last element in the list to be spliced into the main
2408         ;; list.
2409         (while (cdr list)
2410           (setq list (cdr list)))
2411         (if (not data)
2412             (progn
2413               (setcdr list gnus-newsgroup-data)
2414               (setq gnus-newsgroup-data ilist)
2415               (when offset
2416                 (gnus-data-update-list (cdr list) offset)))
2417           (setcdr list (cdr data))
2418           (setcdr data ilist)
2419           (when offset
2420             (gnus-data-update-list (cdr list) offset))))
2421       (setq gnus-newsgroup-data-reverse nil))))
2422
2423 (defun gnus-data-remove (article &optional offset)
2424   (let ((data gnus-newsgroup-data))
2425     (if (= (gnus-data-number (car data)) article)
2426         (progn
2427           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2428                 gnus-newsgroup-data-reverse nil)
2429           (when offset
2430             (gnus-data-update-list gnus-newsgroup-data offset)))
2431       (while (cdr data)
2432         (when (= (gnus-data-number (cadr data)) article)
2433           (setcdr data (cddr data))
2434           (when offset
2435             (gnus-data-update-list (cdr data) offset))
2436           (setq data nil
2437                 gnus-newsgroup-data-reverse nil))
2438         (setq data (cdr data))))))
2439
2440 (defmacro gnus-data-list (backward)
2441   `(if ,backward
2442        (or gnus-newsgroup-data-reverse
2443            (setq gnus-newsgroup-data-reverse
2444                  (reverse gnus-newsgroup-data)))
2445      gnus-newsgroup-data))
2446
2447 (defun gnus-data-update-list (data offset)
2448   "Add OFFSET to the POS of all data entries in DATA."
2449   (setq gnus-newsgroup-data-reverse nil)
2450   (while data
2451     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2452     (setq data (cdr data))))
2453
2454 (defun gnus-summary-article-pseudo-p (article)
2455   "Say whether this article is a pseudo article or not."
2456   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2457
2458 (defmacro gnus-summary-article-sparse-p (article)
2459   "Say whether this article is a sparse article or not."
2460   `(memq ,article gnus-newsgroup-sparse))
2461
2462 (defmacro gnus-summary-article-ancient-p (article)
2463   "Say whether this article is a sparse article or not."
2464   `(memq ,article gnus-newsgroup-ancient))
2465
2466 (defun gnus-article-parent-p (number)
2467   "Say whether this article is a parent or not."
2468   (let ((data (gnus-data-find-list number)))
2469     (and (cdr data)                     ; There has to be an article after...
2470          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2471             (gnus-data-level (nth 1 data))))))
2472
2473 (defun gnus-article-children (number)
2474   "Return a list of all children to NUMBER."
2475   (let* ((data (gnus-data-find-list number))
2476          (level (gnus-data-level (car data)))
2477          children)
2478     (setq data (cdr data))
2479     (while (and data
2480                 (= (gnus-data-level (car data)) (1+ level)))
2481       (push (gnus-data-number (car data)) children)
2482       (setq data (cdr data)))
2483     children))
2484
2485 (defmacro gnus-summary-skip-intangible ()
2486   "If the current article is intangible, then jump to a different article."
2487   '(let ((to (get-text-property (point) 'gnus-intangible)))
2488      (and to (gnus-summary-goto-subject to))))
2489
2490 (defmacro gnus-summary-article-intangible-p ()
2491   "Say whether this article is intangible or not."
2492   '(get-text-property (point) 'gnus-intangible))
2493
2494 (defun gnus-article-read-p (article)
2495   "Say whether ARTICLE is read or not."
2496   (not (or (memq article gnus-newsgroup-marked)
2497            (memq article gnus-newsgroup-unreads)
2498            (memq article gnus-newsgroup-unselected)
2499            (memq article gnus-newsgroup-dormant))))
2500
2501 ;; Some summary mode macros.
2502
2503 (defmacro gnus-summary-article-number ()
2504   "The article number of the article on the current line.
2505 If there isn's an article number here, then we return the current
2506 article number."
2507   '(progn
2508      (gnus-summary-skip-intangible)
2509      (or (get-text-property (point) 'gnus-number)
2510          (gnus-summary-last-subject))))
2511
2512 (defmacro gnus-summary-article-header (&optional number)
2513   "Return the header of article NUMBER."
2514   `(gnus-data-header (gnus-data-find
2515                       ,(or number '(gnus-summary-article-number)))))
2516
2517 (defmacro gnus-summary-thread-level (&optional number)
2518   "Return the level of thread that starts with article NUMBER."
2519   `(if (and (eq gnus-summary-make-false-root 'dummy)
2520             (get-text-property (point) 'gnus-intangible))
2521        0
2522      (gnus-data-level (gnus-data-find
2523                        ,(or number '(gnus-summary-article-number))))))
2524
2525 (defmacro gnus-summary-article-mark (&optional number)
2526   "Return the mark of article NUMBER."
2527   `(gnus-data-mark (gnus-data-find
2528                     ,(or number '(gnus-summary-article-number)))))
2529
2530 (defmacro gnus-summary-article-pos (&optional number)
2531   "Return the position of the line of article NUMBER."
2532   `(gnus-data-pos (gnus-data-find
2533                    ,(or number '(gnus-summary-article-number)))))
2534
2535 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2536 (defmacro gnus-summary-article-subject (&optional number)
2537   "Return current subject string or nil if nothing."
2538   `(let ((headers
2539           ,(if number
2540                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2541              '(gnus-data-header (assq (gnus-summary-article-number)
2542                                       gnus-newsgroup-data)))))
2543      (and headers
2544           (vectorp headers)
2545           (mail-header-subject headers))))
2546
2547 (defmacro gnus-summary-article-score (&optional number)
2548   "Return current article score."
2549   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2550                   gnus-newsgroup-scored))
2551        gnus-summary-default-score 0))
2552
2553 (defun gnus-summary-article-children (&optional number)
2554   "Return a list of article numbers that are children of article NUMBER."
2555   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2556          (level (gnus-data-level (car data)))
2557          l children)
2558     (while (and (setq data (cdr data))
2559                 (> (setq l (gnus-data-level (car data))) level))
2560       (and (= (1+ level) l)
2561            (push (gnus-data-number (car data))
2562                  children)))
2563     (nreverse children)))
2564
2565 (defun gnus-summary-article-parent (&optional number)
2566   "Return the article number of the parent of article NUMBER."
2567   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2568                                     (gnus-data-list t)))
2569          (level (gnus-data-level (car data))))
2570     (if (zerop level)
2571         ()                              ; This is a root.
2572       ;; We search until we find an article with a level less than
2573       ;; this one.  That function has to be the parent.
2574       (while (and (setq data (cdr data))
2575                   (not (< (gnus-data-level (car data)) level))))
2576       (and data (gnus-data-number (car data))))))
2577
2578 (defun gnus-unread-mark-p (mark)
2579   "Say whether MARK is the unread mark."
2580   (= mark gnus-unread-mark))
2581
2582 (defun gnus-read-mark-p (mark)
2583   "Say whether MARK is one of the marks that mark as read.
2584 This is all marks except unread, ticked, dormant, and expirable."
2585   (not (or (= mark gnus-unread-mark)
2586            (= mark gnus-ticked-mark)
2587            (= mark gnus-dormant-mark)
2588            (= mark gnus-expirable-mark))))
2589
2590 (defmacro gnus-article-mark (number)
2591   "Return the MARK of article NUMBER.
2592 This macro should only be used when computing the mark the \"first\"
2593 time; i.e., when generating the summary lines.  After that,
2594 `gnus-summary-article-mark' should be used to examine the
2595 marks of articles."
2596   `(cond
2597     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2598     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2599     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2600     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2601     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2602     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2603     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2604     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2605            gnus-ancient-mark))))
2606
2607 ;; Saving hidden threads.
2608
2609 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2610 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2611
2612 (defmacro gnus-save-hidden-threads (&rest forms)
2613   "Save hidden threads, eval FORMS, and restore the hidden threads."
2614   (let ((config (make-symbol "config")))
2615     `(let ((,config (gnus-hidden-threads-configuration)))
2616        (unwind-protect
2617            (save-excursion
2618              ,@forms)
2619          (gnus-restore-hidden-threads-configuration ,config)))))
2620
2621 (defun gnus-data-compute-positions ()
2622   "Compute the positions of all articles."
2623   (setq gnus-newsgroup-data-reverse nil)
2624   (let ((data gnus-newsgroup-data))
2625     (save-excursion
2626       (gnus-save-hidden-threads
2627         (gnus-summary-show-all-threads)
2628         (goto-char (point-min))
2629         (while data
2630           (while (get-text-property (point) 'gnus-intangible)
2631             (forward-line 1))
2632           (gnus-data-set-pos (car data) (+ (point) 3))
2633           (setq data (cdr data))
2634           (forward-line 1))))))
2635
2636 (defun gnus-hidden-threads-configuration ()
2637   "Return the current hidden threads configuration."
2638   (save-excursion
2639     (let (config)
2640       (goto-char (point-min))
2641       (while (search-forward "\r" nil t)
2642         (push (1- (point)) config))
2643       config)))
2644
2645 (defun gnus-restore-hidden-threads-configuration (config)
2646   "Restore hidden threads configuration from CONFIG."
2647   (save-excursion
2648     (let (point buffer-read-only)
2649       (while (setq point (pop config))
2650         (when (and (< point (point-max))
2651                    (goto-char point)
2652                    (eq (char-after) ?\n))
2653           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2654
2655 ;; Various summary mode internalish functions.
2656
2657 (defun gnus-mouse-pick-article (e)
2658   (interactive "e")
2659   (mouse-set-point e)
2660   (gnus-summary-next-page nil t))
2661
2662 (defun gnus-summary-set-display-table ()
2663   "Change the display table.
2664 Odd characters have a tendency to mess
2665 up nicely formatted displays - we make all possible glyphs
2666 display only a single character."
2667
2668   ;; We start from the standard display table, if any.
2669   (let ((table (or (copy-sequence standard-display-table)
2670                    (make-display-table)))
2671         (i 32))
2672     ;; Nix out all the control chars...
2673     (while (>= (setq i (1- i)) 0)
2674       (aset table i [??]))
2675     ;; ... but not newline and cr, of course.  (cr is necessary for the
2676     ;; selective display).
2677     (aset table ?\n nil)
2678     (aset table ?\r nil)
2679     ;; We keep TAB as well.
2680     (aset table ?\t nil)
2681     ;; We nix out any glyphs over 126 that are not set already.
2682     (let ((i 256))
2683       (while (>= (setq i (1- i)) 127)
2684         ;; Only modify if the entry is nil.
2685         (unless (aref table i)
2686           (aset table i [??]))))
2687     (setq buffer-display-table table)))
2688
2689 (defun gnus-summary-buffer-name (group)
2690   "Return the summary buffer name of GROUP."
2691   (concat "*Summary " group "*"))
2692
2693 (defun gnus-summary-setup-buffer (group)
2694   "Initialize summary buffer."
2695   (let ((buffer (gnus-summary-buffer-name group)))
2696     (if (get-buffer buffer)
2697         (progn
2698           (set-buffer buffer)
2699           (setq gnus-summary-buffer (current-buffer))
2700           (not gnus-newsgroup-prepared))
2701       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2702       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2703       (gnus-summary-mode group)
2704       (when gnus-carpal
2705         (gnus-carpal-setup-buffer 'summary))
2706       (unless gnus-single-article-buffer
2707         (make-local-variable 'gnus-article-buffer)
2708         (make-local-variable 'gnus-article-current)
2709         (make-local-variable 'gnus-original-article-buffer))
2710       (setq gnus-newsgroup-name group)
2711       ;; Set any local variables in the group parameters.
2712       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2713       t)))
2714
2715 (defun gnus-set-global-variables ()
2716   "Set the global equivalents of the buffer-local variables.
2717 They are set to the latest values they had.  These reflect the summary
2718 buffer that was in action when the last article was fetched."
2719   (when (eq major-mode 'gnus-summary-mode)
2720     (setq gnus-summary-buffer (current-buffer))
2721     (let ((name gnus-newsgroup-name)
2722           (marked gnus-newsgroup-marked)
2723           (unread gnus-newsgroup-unreads)
2724           (headers gnus-current-headers)
2725           (data gnus-newsgroup-data)
2726           (summary gnus-summary-buffer)
2727           (article-buffer gnus-article-buffer)
2728           (original gnus-original-article-buffer)
2729           (gac gnus-article-current)
2730           (reffed gnus-reffed-article-number)
2731           (score-file gnus-current-score-file)
2732           (default-charset gnus-newsgroup-charset)
2733           vlist)
2734       (let ((locals gnus-newsgroup-variables))
2735         (while locals
2736           (if (consp (car locals))
2737               (push (eval (caar locals)) vlist)
2738             (push (eval (car locals)) vlist))
2739           (setq locals (cdr locals)))
2740         (setq vlist (nreverse vlist)))
2741       (save-excursion
2742         (set-buffer gnus-group-buffer)
2743         (setq gnus-newsgroup-name name
2744               gnus-newsgroup-marked marked
2745               gnus-newsgroup-unreads unread
2746               gnus-current-headers headers
2747               gnus-newsgroup-data data
2748               gnus-article-current gac
2749               gnus-summary-buffer summary
2750               gnus-article-buffer article-buffer
2751               gnus-original-article-buffer original
2752               gnus-reffed-article-number reffed
2753               gnus-current-score-file score-file
2754               gnus-newsgroup-charset default-charset)
2755         (let ((locals gnus-newsgroup-variables))
2756           (while locals
2757             (if (consp (car locals))
2758                 (set (caar locals) (pop vlist))
2759               (set (car locals) (pop vlist)))
2760             (setq locals (cdr locals))))
2761         ;; The article buffer also has local variables.
2762         (when (gnus-buffer-live-p gnus-article-buffer)
2763           (set-buffer gnus-article-buffer)
2764           (setq gnus-summary-buffer summary))))))
2765
2766 (defun gnus-summary-article-unread-p (article)
2767   "Say whether ARTICLE is unread or not."
2768   (memq article gnus-newsgroup-unreads))
2769
2770 (defun gnus-summary-first-article-p (&optional article)
2771   "Return whether ARTICLE is the first article in the buffer."
2772   (if (not (setq article (or article (gnus-summary-article-number))))
2773       nil
2774     (eq article (caar gnus-newsgroup-data))))
2775
2776 (defun gnus-summary-last-article-p (&optional article)
2777   "Return whether ARTICLE is the last article in the buffer."
2778   (if (not (setq article (or article (gnus-summary-article-number))))
2779       ;; All non-existent numbers are the last article.  :-)
2780       t
2781     (not (cdr (gnus-data-find-list article)))))
2782
2783 (defun gnus-make-thread-indent-array ()
2784   (let ((n 200))
2785     (unless (and gnus-thread-indent-array
2786                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2787       (setq gnus-thread-indent-array (make-vector 201 "")
2788             gnus-thread-indent-array-level gnus-thread-indent-level)
2789       (while (>= n 0)
2790         (aset gnus-thread-indent-array n
2791               (make-string (* n gnus-thread-indent-level) ? ))
2792         (setq n (1- n))))))
2793
2794 (defun gnus-update-summary-mark-positions ()
2795   "Compute where the summary marks are to go."
2796   (save-excursion
2797     (when (gnus-buffer-exists-p gnus-summary-buffer)
2798       (set-buffer gnus-summary-buffer))
2799     (let ((gnus-replied-mark 129)
2800           (gnus-score-below-mark 130)
2801           (gnus-score-over-mark 130)
2802           (gnus-download-mark 131)
2803           (spec gnus-summary-line-format-spec)
2804           gnus-visual pos)
2805       (save-excursion
2806         (gnus-set-work-buffer)
2807         (let ((gnus-summary-line-format-spec spec)
2808               (gnus-newsgroup-downloadable '((0 . t))))
2809           (gnus-summary-insert-line
2810            (make-full-mail-header 0 "" "nobody" "" "" "" 0 0 "" nil)
2811            0 nil 128 t nil "" nil 1)
2812           (goto-char (point-min))
2813           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2814                                              (- (point) 2)))))
2815           (goto-char (point-min))
2816           (push (cons 'replied (and (search-forward "\201" nil t)
2817                                     (- (point) 2)))
2818                 pos)
2819           (goto-char (point-min))
2820           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2821                 pos)
2822           (goto-char (point-min))
2823           (push (cons 'download
2824                       (and (search-forward "\203" nil t) (- (point) 2)))
2825                 pos)))
2826       (setq gnus-summary-mark-positions pos))))
2827
2828 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2829   "Insert a dummy root in the summary buffer."
2830   (beginning-of-line)
2831   (gnus-add-text-properties
2832    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2833    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2834
2835 (defun gnus-summary-from-or-to-or-newsgroups (header)
2836   (let ((to (cdr (assq 'To (mail-header-extra header))))
2837         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2838         (default-mime-charset (with-current-buffer gnus-summary-buffer
2839                                 default-mime-charset)))
2840     (cond
2841      ((and to
2842            gnus-ignored-from-addresses
2843            (string-match gnus-ignored-from-addresses
2844                          (mail-header-from header)))
2845       (concat "-> "
2846               (or (car (funcall gnus-extract-address-components
2847                                 (funcall
2848                                  gnus-decode-encoded-word-function to)))
2849                   (funcall gnus-decode-encoded-word-function to))))
2850      ((and newsgroups
2851            gnus-ignored-from-addresses
2852            (string-match gnus-ignored-from-addresses
2853                          (mail-header-from header)))
2854       (concat "=> " newsgroups))
2855      (t
2856       (or (car (funcall gnus-extract-address-components
2857                         (mail-header-from header)))
2858           (mail-header-from header))))))
2859
2860 (defun gnus-summary-insert-line (gnus-tmp-header
2861                                  gnus-tmp-level gnus-tmp-current
2862                                  gnus-tmp-unread gnus-tmp-replied
2863                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2864                                  &optional gnus-tmp-dummy gnus-tmp-score
2865                                  gnus-tmp-process)
2866   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2867          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2868          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2869          (gnus-tmp-score-char
2870           (if (or (null gnus-summary-default-score)
2871                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2872                       gnus-summary-zcore-fuzz))
2873               ?  ;Whitespace
2874             (if (< gnus-tmp-score gnus-summary-default-score)
2875                 gnus-score-below-mark gnus-score-over-mark)))
2876          (gnus-tmp-replied
2877           (cond (gnus-tmp-process gnus-process-mark)
2878                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2879                  gnus-cached-mark)
2880                 (gnus-tmp-replied gnus-replied-mark)
2881                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2882                  gnus-saved-mark)
2883                 (t gnus-no-mark)))
2884          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2885          (gnus-tmp-name
2886           (cond
2887            ((string-match "<[^>]+> *$" gnus-tmp-from)
2888             (let ((beg (match-beginning 0)))
2889               (or (and (string-match "^\".+\"" gnus-tmp-from)
2890                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2891                   (substring gnus-tmp-from 0 beg))))
2892            ((string-match "(.+)" gnus-tmp-from)
2893             (substring gnus-tmp-from
2894                        (1+ (match-beginning 0)) (1- (match-end 0))))
2895            (t gnus-tmp-from)))
2896          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2897          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2898          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2899          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2900          (buffer-read-only nil))
2901     (when (string= gnus-tmp-name "")
2902       (setq gnus-tmp-name gnus-tmp-from))
2903     (unless (numberp gnus-tmp-lines)
2904       (setq gnus-tmp-lines 0))
2905     (gnus-put-text-property-excluding-characters-with-faces
2906      (point)
2907      (progn (eval gnus-summary-line-format-spec) (point))
2908      'gnus-number gnus-tmp-number)
2909     (when (gnus-visual-p 'summary-highlight 'highlight)
2910       (forward-line -1)
2911       (gnus-run-hooks 'gnus-summary-update-hook)
2912       (forward-line 1))))
2913
2914 (defun gnus-summary-update-line (&optional dont-update)
2915   "Update summary line after change."
2916   (when (and gnus-summary-default-score
2917              (not gnus-summary-inhibit-highlight))
2918     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2919            (article (gnus-summary-article-number))
2920            (score (gnus-summary-article-score article)))
2921       (unless dont-update
2922         (if (and gnus-summary-mark-below
2923                  (< (gnus-summary-article-score)
2924                     gnus-summary-mark-below))
2925             ;; This article has a low score, so we mark it as read.
2926             (when (memq article gnus-newsgroup-unreads)
2927               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2928           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2929             ;; This article was previously marked as read on account
2930             ;; of a low score, but now it has risen, so we mark it as
2931             ;; unread.
2932             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2933         (gnus-summary-update-mark
2934          (if (or (null gnus-summary-default-score)
2935                  (<= (abs (- score gnus-summary-default-score))
2936                      gnus-summary-zcore-fuzz))
2937              ?  ;Whitespace
2938            (if (< score gnus-summary-default-score)
2939                gnus-score-below-mark gnus-score-over-mark))
2940          'score))
2941       ;; Do visual highlighting.
2942       (when (gnus-visual-p 'summary-highlight 'highlight)
2943         (gnus-run-hooks 'gnus-summary-update-hook)))))
2944
2945 (defvar gnus-tmp-new-adopts nil)
2946
2947 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2948   "Return the number of articles in THREAD.
2949 This may be 0 in some cases -- if none of the articles in
2950 the thread are to be displayed."
2951   (let* ((number
2952           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2953           (cond
2954            ((not (listp thread))
2955             1)
2956            ((and (consp thread) (cdr thread))
2957             (apply
2958              '+ 1 (mapcar
2959                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2960            ((null thread)
2961             1)
2962            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2963             1)
2964            (t 0))))
2965     (when (and level (zerop level) gnus-tmp-new-adopts)
2966       (incf number
2967             (apply '+ (mapcar
2968                        'gnus-summary-number-of-articles-in-thread
2969                        gnus-tmp-new-adopts))))
2970     (if char
2971         (if (> number 1) gnus-not-empty-thread-mark
2972           gnus-empty-thread-mark)
2973       number)))
2974
2975 (defun gnus-summary-set-local-parameters (group)
2976   "Go through the local params of GROUP and set all variable specs in that list."
2977   (let ((params (gnus-group-find-parameter group))
2978         elem)
2979     (while params
2980       (setq elem (car params)
2981             params (cdr params))
2982       (and (consp elem)                 ; Has to be a cons.
2983            (consp (cdr elem))           ; The cdr has to be a list.
2984            (symbolp (car elem))         ; Has to be a symbol in there.
2985            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2986            (ignore-errors               ; So we set it.
2987              (make-local-variable (car elem))
2988              (set (car elem) (eval (nth 1 elem))))))))
2989
2990 (defun gnus-summary-read-group (group &optional show-all no-article
2991                                       kill-buffer no-display backward
2992                                       select-articles)
2993   "Start reading news in newsgroup GROUP.
2994 If SHOW-ALL is non-nil, already read articles are also listed.
2995 If NO-ARTICLE is non-nil, no article is selected initially.
2996 If NO-DISPLAY, don't generate a summary buffer."
2997   (let (result)
2998     (while (and group
2999                 (null (setq result
3000                             (let ((gnus-auto-select-next nil))
3001                               (or (gnus-summary-read-group-1
3002                                    group show-all no-article
3003                                    kill-buffer no-display
3004                                    select-articles)
3005                                   (setq show-all nil
3006                                         select-articles nil)))))
3007                 (eq gnus-auto-select-next 'quietly))
3008       (set-buffer gnus-group-buffer)
3009       ;; The entry function called above goes to the next
3010       ;; group automatically, so we go two groups back
3011       ;; if we are searching for the previous group.
3012       (when backward
3013         (gnus-group-prev-unread-group 2))
3014       (if (not (equal group (gnus-group-group-name)))
3015           (setq group (gnus-group-group-name))
3016         (setq group nil)))
3017     result))
3018
3019 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3020   "Directly jump to the other GROUP from summary buffer.
3021 If SHOW-ALL is non-nil, already read articles are also listed."
3022   (interactive
3023    (if (eq gnus-summary-buffer (current-buffer))
3024        (list (completing-read
3025               "Group: " gnus-active-hashtb nil t
3026               (when (and gnus-newsgroup-name
3027                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3028                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3029               'gnus-group-history)
3030              current-prefix-arg)
3031      (error "%s must be invoked from a gnus summary buffer." this-command)))
3032   (unless (or (zerop (length group))
3033               (and gnus-newsgroup-name
3034                    (string-equal gnus-newsgroup-name group)))
3035     (gnus-summary-exit)
3036     (gnus-summary-read-group group show-all
3037                              gnus-dont-select-after-jump-to-other-group)))
3038
3039 (defun gnus-summary-read-group-1 (group show-all no-article
3040                                         kill-buffer no-display
3041                                         &optional select-articles)
3042   ;; Killed foreign groups can't be entered.
3043   ;;  (when (and (not (gnus-group-native-p group))
3044   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3045   ;;    (error "Dead non-native groups can't be entered"))
3046   (gnus-message 5 "Retrieving newsgroup: %s..."
3047                 (gnus-group-decoded-name group))
3048   (let* ((new-group (gnus-summary-setup-buffer group))
3049          (quit-config (gnus-group-quit-config group))
3050          (did-select (and new-group (gnus-select-newsgroup
3051                                      group show-all select-articles))))
3052     (cond
3053      ;; This summary buffer exists already, so we just select it.
3054      ((not new-group)
3055       (gnus-set-global-variables)
3056       (when kill-buffer
3057         (gnus-kill-or-deaden-summary kill-buffer))
3058       (gnus-configure-windows 'summary 'force)
3059       (gnus-set-mode-line 'summary)
3060       (gnus-summary-position-point)
3061       (message "")
3062       t)
3063      ;; We couldn't select this group.
3064      ((null did-select)
3065       (when (and (eq major-mode 'gnus-summary-mode)
3066                  (not (equal (current-buffer) kill-buffer)))
3067         (kill-buffer (current-buffer))
3068         (if (not quit-config)
3069             (progn
3070               ;; Update the info -- marks might need to be removed,
3071               ;; for instance.
3072               (gnus-summary-update-info)
3073               (set-buffer gnus-group-buffer)
3074               (gnus-group-jump-to-group group)
3075               (gnus-group-next-unread-group 1))
3076           (gnus-handle-ephemeral-exit quit-config)))
3077       (let ((grpinfo (gnus-get-info group)))
3078         (if (null (gnus-info-read grpinfo))
3079             (gnus-message 3 "Group %s contains no messages" 
3080                           (gnus-group-decoded-name group))
3081           (gnus-message 3 "Can't select group")))
3082       nil)
3083      ;; The user did a `C-g' while prompting for number of articles,
3084      ;; so we exit this group.
3085      ((eq did-select 'quit)
3086       (and (eq major-mode 'gnus-summary-mode)
3087            (not (equal (current-buffer) kill-buffer))
3088            (kill-buffer (current-buffer)))
3089       (when kill-buffer
3090         (gnus-kill-or-deaden-summary kill-buffer))
3091       (if (not quit-config)
3092           (progn
3093             (set-buffer gnus-group-buffer)
3094             (gnus-group-jump-to-group group)
3095             (gnus-group-next-unread-group 1)
3096             (gnus-configure-windows 'group 'force))
3097         (gnus-handle-ephemeral-exit quit-config))
3098       ;; Finally signal the quit.
3099       (signal 'quit nil))
3100      ;; The group was successfully selected.
3101      (t
3102       (gnus-set-global-variables)
3103       ;; Save the active value in effect when the group was entered.
3104       (setq gnus-newsgroup-active
3105             (gnus-copy-sequence
3106              (gnus-active gnus-newsgroup-name)))
3107       ;; You can change the summary buffer in some way with this hook.
3108       (gnus-run-hooks 'gnus-select-group-hook)
3109       (gnus-update-format-specifications
3110        nil 'summary 'summary-mode 'summary-dummy)
3111       (gnus-update-summary-mark-positions)
3112       ;; Do score processing.
3113       (when gnus-use-scoring
3114         (gnus-possibly-score-headers))
3115       ;; Check whether to fill in the gaps in the threads.
3116       (when gnus-build-sparse-threads
3117         (gnus-build-sparse-threads))
3118       ;; Find the initial limit.
3119       (if gnus-show-threads
3120           (if show-all
3121               (let ((gnus-newsgroup-dormant nil))
3122                 (gnus-summary-initial-limit show-all))
3123             (gnus-summary-initial-limit show-all))
3124         ;; When untreaded, all articles are always shown.
3125         (setq gnus-newsgroup-limit
3126               (mapcar
3127                (lambda (header) (mail-header-number header))
3128                gnus-newsgroup-headers)))
3129       ;; Generate the summary buffer.
3130       (unless no-display
3131         (gnus-summary-prepare))
3132       (when gnus-use-trees
3133         (gnus-tree-open group)
3134         (setq gnus-summary-highlight-line-function
3135               'gnus-tree-highlight-article))
3136       ;; If the summary buffer is empty, but there are some low-scored
3137       ;; articles or some excluded dormants, we include these in the
3138       ;; buffer.
3139       (when (and (zerop (buffer-size))
3140                  (not no-display))
3141         (cond (gnus-newsgroup-dormant
3142                (gnus-summary-limit-include-dormant))
3143               ((and gnus-newsgroup-scored show-all)
3144                (gnus-summary-limit-include-expunged t))))
3145       ;; Function `gnus-apply-kill-file' must be called in this hook.
3146       (gnus-run-hooks 'gnus-apply-kill-hook)
3147       (if (and (zerop (buffer-size))
3148                (not no-display))
3149           (progn
3150             ;; This newsgroup is empty.
3151             (gnus-summary-catchup-and-exit nil t)
3152             (gnus-message 6 "No unread news")
3153             (when kill-buffer
3154               (gnus-kill-or-deaden-summary kill-buffer))
3155             ;; Return nil from this function.
3156             nil)
3157         ;; Hide conversation thread subtrees.  We cannot do this in
3158         ;; gnus-summary-prepare-hook since kill processing may not
3159         ;; work with hidden articles.
3160         (and gnus-show-threads
3161              gnus-thread-hide-subtree
3162              (gnus-summary-hide-all-threads))
3163         (when kill-buffer
3164           (gnus-kill-or-deaden-summary kill-buffer))
3165         ;; Show first unread article if requested.
3166         (if (and (not no-article)
3167                  (not no-display)
3168                  gnus-newsgroup-unreads
3169                  gnus-auto-select-first)
3170             (progn
3171               (gnus-configure-windows 'summary)
3172               (cond
3173                ((eq gnus-auto-select-first 'best)
3174                 (gnus-summary-best-unread-article))
3175                ((eq gnus-auto-select-first t)
3176                 (gnus-summary-first-unread-article))
3177                ((gnus-functionp gnus-auto-select-first)
3178                 (funcall gnus-auto-select-first))))
3179           ;; Don't select any articles, just move point to the first
3180           ;; article in the group.
3181           (goto-char (point-min))
3182           (gnus-summary-position-point)
3183           (gnus-configure-windows 'summary 'force)
3184           (gnus-set-mode-line 'summary))
3185         (when (get-buffer-window gnus-group-buffer t)
3186           ;; Gotta use windows, because recenter does weird stuff if
3187           ;; the current buffer ain't the displayed window.
3188           (let ((owin (selected-window)))
3189             (select-window (get-buffer-window gnus-group-buffer t))
3190             (when (gnus-group-goto-group group)
3191               (recenter))
3192             (select-window owin)))
3193         ;; Mark this buffer as "prepared".
3194         (setq gnus-newsgroup-prepared t)
3195         (gnus-run-hooks 'gnus-summary-prepared-hook)
3196         t)))))
3197
3198 (defun gnus-summary-prepare ()
3199   "Generate the summary buffer."
3200   (interactive)
3201   (let ((buffer-read-only nil))
3202     (erase-buffer)
3203     (setq gnus-newsgroup-data nil
3204           gnus-newsgroup-data-reverse nil)
3205     (gnus-run-hooks 'gnus-summary-generate-hook)
3206     ;; Generate the buffer, either with threads or without.
3207     (when gnus-newsgroup-headers
3208       (gnus-summary-prepare-threads
3209        (if gnus-show-threads
3210            (gnus-sort-gathered-threads
3211             (funcall gnus-summary-thread-gathering-function
3212                      (gnus-sort-threads
3213                       (gnus-cut-threads (gnus-make-threads)))))
3214          ;; Unthreaded display.
3215          (gnus-sort-articles gnus-newsgroup-headers))))
3216     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3217     ;; Call hooks for modifying summary buffer.
3218     (goto-char (point-min))
3219     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3220
3221 (defsubst gnus-general-simplify-subject (subject)
3222   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3223   (setq subject
3224         (cond
3225          ;; Truncate the subject.
3226          (gnus-simplify-subject-functions
3227           (gnus-map-function gnus-simplify-subject-functions subject))
3228          ((numberp gnus-summary-gather-subject-limit)
3229           (setq subject (gnus-simplify-subject-re subject))
3230           (if (> (length subject) gnus-summary-gather-subject-limit)
3231               (substring subject 0 gnus-summary-gather-subject-limit)
3232             subject))
3233          ;; Fuzzily simplify it.
3234          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3235           (gnus-simplify-subject-fuzzy subject))
3236          ;; Just remove the leading "Re:".
3237          (t
3238           (gnus-simplify-subject-re subject))))
3239
3240   (if (and gnus-summary-gather-exclude-subject
3241            (string-match gnus-summary-gather-exclude-subject subject))
3242       nil                               ; This article shouldn't be gathered
3243     subject))
3244
3245 (defun gnus-summary-simplify-subject-query ()
3246   "Query where the respool algorithm would put this article."
3247   (interactive)
3248   (gnus-summary-select-article)
3249   (message "%s"
3250            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3251
3252 (defun gnus-gather-threads-by-subject (threads)
3253   "Gather threads by looking at Subject headers."
3254   (if (not gnus-summary-make-false-root)
3255       threads
3256     (let ((hashtb (gnus-make-hashtable 1024))
3257           (prev threads)
3258           (result threads)
3259           subject hthread whole-subject)
3260       (while threads
3261         (setq subject (gnus-general-simplify-subject
3262                        (setq whole-subject (mail-header-subject
3263                                             (caar threads)))))
3264         (when subject
3265           (if (setq hthread (gnus-gethash subject hashtb))
3266               (progn
3267                 ;; We enter a dummy root into the thread, if we
3268                 ;; haven't done that already.
3269                 (unless (stringp (caar hthread))
3270                   (setcar hthread (list whole-subject (car hthread))))
3271                 ;; We add this new gathered thread to this gathered
3272                 ;; thread.
3273                 (setcdr (car hthread)
3274                         (nconc (cdar hthread) (list (car threads))))
3275                 ;; Remove it from the list of threads.
3276                 (setcdr prev (cdr threads))
3277                 (setq threads prev))
3278             ;; Enter this thread into the hash table.
3279             (gnus-sethash subject threads hashtb)))
3280         (setq prev threads)
3281         (setq threads (cdr threads)))
3282       result)))
3283
3284 (defun gnus-gather-threads-by-references (threads)
3285   "Gather threads by looking at References headers."
3286   (let ((idhashtb (gnus-make-hashtable 1024))
3287         (thhashtb (gnus-make-hashtable 1024))
3288         (prev threads)
3289         (result threads)
3290         ids references id gthread gid entered ref)
3291     (while threads
3292       (when (setq references (mail-header-references (caar threads)))
3293         (setq id (mail-header-id (caar threads))
3294               ids (gnus-split-references references)
3295               entered nil)
3296         (while (setq ref (pop ids))
3297           (setq ids (delete ref ids))
3298           (if (not (setq gid (gnus-gethash ref idhashtb)))
3299               (progn
3300                 (gnus-sethash ref id idhashtb)
3301                 (gnus-sethash id threads thhashtb))
3302             (setq gthread (gnus-gethash gid thhashtb))
3303             (unless entered
3304               ;; We enter a dummy root into the thread, if we
3305               ;; haven't done that already.
3306               (unless (stringp (caar gthread))
3307                 (setcar gthread (list (mail-header-subject (caar gthread))
3308                                       (car gthread))))
3309               ;; We add this new gathered thread to this gathered
3310               ;; thread.
3311               (setcdr (car gthread)
3312                       (nconc (cdar gthread) (list (car threads)))))
3313             ;; Add it into the thread hash table.
3314             (gnus-sethash id gthread thhashtb)
3315             (setq entered t)
3316             ;; Remove it from the list of threads.
3317             (setcdr prev (cdr threads))
3318             (setq threads prev))))
3319       (setq prev threads)
3320       (setq threads (cdr threads)))
3321     result))
3322
3323 (defun gnus-sort-gathered-threads (threads)
3324   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3325   (let ((result threads))
3326     (while threads
3327       (when (stringp (caar threads))
3328         (setcdr (car threads)
3329                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3330       (setq threads (cdr threads)))
3331     result))
3332
3333 (defun gnus-thread-loop-p (root thread)
3334   "Say whether ROOT is in THREAD."
3335   (let ((stack (list thread))
3336         (infloop 0)
3337         th)
3338     (while (setq thread (pop stack))
3339       (setq th (cdr thread))
3340       (while (and th
3341                   (not (eq (caar th) root)))
3342         (pop th))
3343       (if th
3344           ;; We have found a loop.
3345           (let (ref-dep)
3346             (setcdr thread (delq (car th) (cdr thread)))
3347             (if (boundp (setq ref-dep (intern "none"
3348                                               gnus-newsgroup-dependencies)))
3349                 (setcdr (symbol-value ref-dep)
3350                         (nconc (cdr (symbol-value ref-dep))
3351                                (list (car th))))
3352               (set ref-dep (list nil (car th))))
3353             (setq infloop 1
3354                   stack nil))
3355         ;; Push all the subthreads onto the stack.
3356         (push (cdr thread) stack)))
3357     infloop))
3358
3359 (defun gnus-make-threads ()
3360   "Go through the dependency hashtb and find the roots.  Return all threads."
3361   (let (threads)
3362     (while (catch 'infloop
3363              (mapatoms
3364               (lambda (refs)
3365                 ;; Deal with self-referencing References loops.
3366                 (when (and (car (symbol-value refs))
3367                            (not (zerop
3368                                  (apply
3369                                   '+
3370                                   (mapcar
3371                                    (lambda (thread)
3372                                      (gnus-thread-loop-p
3373                                       (car (symbol-value refs)) thread))
3374                                    (cdr (symbol-value refs)))))))
3375                   (setq threads nil)
3376                   (throw 'infloop t))
3377                 (unless (car (symbol-value refs))
3378                   ;; These threads do not refer back to any other articles,
3379                   ;; so they're roots.
3380                   (setq threads (append (cdr (symbol-value refs)) threads))))
3381               gnus-newsgroup-dependencies)))
3382     threads))
3383
3384 ;; Build the thread tree.
3385 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3386   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3387
3388 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3389 if it was already present.
3390
3391 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3392 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3393 Message-IDs will be renamed be renamed to a unique Message-ID before
3394 being entered.
3395
3396 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3397   (let* ((id (mail-header-id header))
3398          (id-dep (and id (intern id dependencies)))
3399          ref ref-dep ref-header)
3400     ;; Enter this `header' in the `dependencies' table.
3401     (cond
3402      ((not id-dep)
3403       (setq header nil))
3404      ;; The first two cases do the normal part: enter a new `header'
3405      ;; in the `dependencies' table.
3406      ((not (boundp id-dep))
3407       (set id-dep (list header)))
3408      ((null (car (symbol-value id-dep)))
3409       (setcar (symbol-value id-dep) header))
3410
3411      ;; From here the `header' was already present in the
3412      ;; `dependencies' table.
3413      (force-new
3414       ;; Overrides an existing entry;
3415       ;; just set the header part of the entry.
3416       (setcar (symbol-value id-dep) header))
3417
3418      ;; Renames the existing `header' to a unique Message-ID.
3419      ((not gnus-summary-ignore-duplicates)
3420       ;; An article with this Message-ID has already been seen.
3421       ;; We rename the Message-ID.
3422       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3423            (list header))
3424       (mail-header-set-id header id))
3425
3426      ;; The last case ignores an existing entry, except it adds any
3427      ;; additional Xrefs (in case the two articles came from different
3428      ;; servers.
3429      ;; Also sets `header' to `nil' meaning that the `dependencies'
3430      ;; table was *not* modified.
3431      (t
3432       (mail-header-set-xref
3433        (car (symbol-value id-dep))
3434        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3435                    "")
3436                (or (mail-header-xref header) "")))
3437       (setq header nil)))
3438
3439     (when header
3440       ;; First check if that we are not creating a References loop.
3441       (setq ref (gnus-parent-id (mail-header-references header)))
3442       (while (and ref
3443                   (setq ref-dep (intern-soft ref dependencies))
3444                   (boundp ref-dep)
3445                   (setq ref-header (car (symbol-value ref-dep))))
3446         (if (string= id ref)
3447             ;; Yuk!  This is a reference loop.  Make the article be a
3448             ;; root article.
3449             (progn
3450               (mail-header-set-references (car (symbol-value id-dep)) "none")
3451               (setq ref nil))
3452           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3453       (setq ref (gnus-parent-id (mail-header-references header)))
3454       (setq ref-dep (intern (or ref "none") dependencies))
3455       (if (boundp ref-dep)
3456           (setcdr (symbol-value ref-dep)
3457                   (nconc (cdr (symbol-value ref-dep))
3458                          (list (symbol-value id-dep))))
3459         (set ref-dep (list nil (symbol-value id-dep)))))
3460     header))
3461
3462 (defun gnus-build-sparse-threads ()
3463   (let ((headers gnus-newsgroup-headers)
3464         (mail-parse-charset gnus-newsgroup-charset)
3465         (gnus-summary-ignore-duplicates t)
3466         header references generation relations
3467         subject child end new-child date)
3468     ;; First we create an alist of generations/relations, where
3469     ;; generations is how much we trust the relation, and the relation
3470     ;; is parent/child.
3471     (gnus-message 7 "Making sparse threads...")
3472     (save-excursion
3473       (nnheader-set-temp-buffer " *gnus sparse threads*")
3474       (while (setq header (pop headers))
3475         (when (and (setq references (mail-header-references header))
3476                    (not (string= references "")))
3477           (insert references)
3478           (setq child (mail-header-id header)
3479                 subject (mail-header-subject header)
3480                 date (mail-header-date header)
3481                 generation 0)
3482           (while (search-backward ">" nil t)
3483             (setq end (1+ (point)))
3484             (when (search-backward "<" nil t)
3485               (setq new-child (buffer-substring (point) end))
3486               (push (list (incf generation)
3487                           child (setq child new-child)
3488                           subject date)
3489                     relations)))
3490           (when child
3491             (push (list (1+ generation) child nil subject) relations))
3492           (erase-buffer)))
3493       (kill-buffer (current-buffer)))
3494     ;; Sort over trustworthiness.
3495     (mapcar
3496      (lambda (relation)
3497        (when (gnus-dependencies-add-header
3498               (make-full-mail-header-from-decoded-header
3499                gnus-reffed-article-number
3500                (nth 3 relation) "" (or (nth 4 relation) "")
3501                (nth 1 relation)
3502                (or (nth 2 relation) "") 0 0 "")
3503               gnus-newsgroup-dependencies nil)
3504          (push gnus-reffed-article-number gnus-newsgroup-limit)
3505          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3506          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3507                gnus-newsgroup-reads)
3508          (decf gnus-reffed-article-number)))
3509      (sort relations 'car-less-than-car))
3510     (gnus-message 7 "Making sparse threads...done")))
3511
3512 (defun gnus-build-old-threads ()
3513   ;; Look at all the articles that refer back to old articles, and
3514   ;; fetch the headers for the articles that aren't there.  This will
3515   ;; build complete threads - if the roots haven't been expired by the
3516   ;; server, that is.
3517   (let ((mail-parse-charset gnus-newsgroup-charset)
3518         id heads)
3519     (mapatoms
3520      (lambda (refs)
3521        (when (not (car (symbol-value refs)))
3522          (setq heads (cdr (symbol-value refs)))
3523          (while heads
3524            (if (memq (mail-header-number (caar heads))
3525                      gnus-newsgroup-dormant)
3526                (setq heads (cdr heads))
3527              (setq id (symbol-name refs))
3528              (while (and (setq id (gnus-build-get-header id))
3529                          (not (car (gnus-id-to-thread id)))))
3530              (setq heads nil)))))
3531      gnus-newsgroup-dependencies)))
3532
3533 ;; This function has to be called with point after the article number
3534 ;; on the beginning of the line.
3535 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3536   (let ((eol (gnus-point-at-eol))
3537         (buffer (current-buffer))
3538         header)
3539
3540     ;; overview: [num subject from date id refs chars lines misc]
3541     (unwind-protect
3542         (progn
3543           (narrow-to-region (point) eol)
3544           (unless (eobp)
3545             (forward-char))
3546
3547           (setq header
3548                 (make-full-mail-header
3549                  number                         ; number
3550                  (nnheader-nov-field)           ; subject
3551                  (nnheader-nov-field)           ; from
3552                  (nnheader-nov-field)           ; date
3553                  (nnheader-nov-read-message-id) ; id
3554                  (nnheader-nov-field)           ; refs
3555                  (nnheader-nov-read-integer)    ; chars
3556                  (nnheader-nov-read-integer)    ; lines
3557                  (unless (eobp)
3558                    (if (looking-at "Xref: ")
3559                        (goto-char (match-end 0)))
3560                    (nnheader-nov-field))        ; Xref
3561                  (nnheader-nov-parse-extra))))  ; extra
3562
3563       (widen))
3564
3565     (when gnus-alter-header-function
3566       (funcall gnus-alter-header-function header))
3567     (gnus-dependencies-add-header header dependencies force-new)))
3568
3569 (defun gnus-build-get-header (id)
3570   "Look through the buffer of NOV lines and find the header to ID.
3571 Enter this line into the dependencies hash table, and return
3572 the id of the parent article (if any)."
3573   (let ((deps gnus-newsgroup-dependencies)
3574         found header)
3575     (prog1
3576         (save-excursion
3577           (set-buffer nntp-server-buffer)
3578           (let ((case-fold-search nil))
3579             (goto-char (point-min))
3580             (while (and (not found)
3581                         (search-forward id nil t))
3582               (beginning-of-line)
3583               (setq found (looking-at
3584                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3585                                    (regexp-quote id))))
3586               (or found (beginning-of-line 2)))
3587             (when found
3588               (beginning-of-line)
3589               (and
3590                (setq header (gnus-nov-parse-line
3591                              (read (current-buffer)) deps))
3592                (gnus-parent-id (mail-header-references header))))))
3593       (when header
3594         (let ((number (mail-header-number header)))
3595           (push number gnus-newsgroup-limit)
3596           (push header gnus-newsgroup-headers)
3597           (if (memq number gnus-newsgroup-unselected)
3598               (progn
3599                 (push number gnus-newsgroup-unreads)
3600                 (setq gnus-newsgroup-unselected
3601                       (delq number gnus-newsgroup-unselected)))
3602             (push number gnus-newsgroup-ancient)))))))
3603
3604 (defun gnus-build-all-threads ()
3605   "Read all the headers."
3606   (let ((gnus-summary-ignore-duplicates t)
3607         (mail-parse-charset gnus-newsgroup-charset)
3608         (dependencies gnus-newsgroup-dependencies)
3609         header article)
3610     (save-excursion
3611       (set-buffer nntp-server-buffer)
3612       (let ((case-fold-search nil))
3613         (goto-char (point-min))
3614         (while (not (eobp))
3615           (ignore-errors
3616             (setq article (read (current-buffer))
3617                   header (gnus-nov-parse-line article dependencies)))
3618           (when header
3619             (save-excursion
3620               (set-buffer gnus-summary-buffer)
3621               (push header gnus-newsgroup-headers)
3622               (if (memq (setq article (mail-header-number header))
3623                         gnus-newsgroup-unselected)
3624                   (progn
3625                     (push article gnus-newsgroup-unreads)
3626                     (setq gnus-newsgroup-unselected
3627                           (delq article gnus-newsgroup-unselected)))
3628                 (push article gnus-newsgroup-ancient)))
3629             (forward-line 1)))))))
3630
3631 (defun gnus-summary-update-article-line (article header)
3632   "Update the line for ARTICLE using HEADERS."
3633   (let* ((id (mail-header-id header))
3634          (thread (gnus-id-to-thread id)))
3635     (unless thread
3636       (error "Article in no thread"))
3637     ;; Update the thread.
3638     (setcar thread header)
3639     (gnus-summary-goto-subject article)
3640     (let* ((datal (gnus-data-find-list article))
3641            (data (car datal))
3642            (length (when (cdr datal)
3643                      (- (gnus-data-pos data)
3644                         (gnus-data-pos (cadr datal)))))
3645            (buffer-read-only nil)
3646            (level (gnus-summary-thread-level)))
3647       (gnus-delete-line)
3648       (gnus-summary-insert-line
3649        header level nil (gnus-article-mark article)
3650        (memq article gnus-newsgroup-replied)
3651        (memq article gnus-newsgroup-expirable)
3652        ;; Only insert the Subject string when it's different
3653        ;; from the previous Subject string.
3654        (if (and
3655             gnus-show-threads
3656             (gnus-subject-equal
3657              (condition-case ()
3658                  (mail-header-subject
3659                   (gnus-data-header
3660                    (cadr
3661                     (gnus-data-find-list
3662                      article
3663                      (gnus-data-list t)))))
3664                ;; Error on the side of excessive subjects.
3665                (error ""))
3666              (mail-header-subject header)))
3667            ""
3668          (mail-header-subject header))
3669        nil (cdr (assq article gnus-newsgroup-scored))
3670        (memq article gnus-newsgroup-processable))
3671       (when length
3672         (gnus-data-update-list
3673          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3674
3675 (defun gnus-summary-update-article (article &optional iheader)
3676   "Update ARTICLE in the summary buffer."
3677   (set-buffer gnus-summary-buffer)
3678   (let* ((header (gnus-summary-article-header article))
3679          (id (mail-header-id header))
3680          (data (gnus-data-find article))
3681          (thread (gnus-id-to-thread id))
3682          (references (mail-header-references header))
3683          (parent
3684           (gnus-id-to-thread
3685            (or (gnus-parent-id
3686                 (when (and references
3687                            (not (equal "" references)))
3688                   references))
3689                "none")))
3690          (buffer-read-only nil)
3691          (old (car thread)))
3692     (when thread
3693       (unless iheader
3694         (setcar thread nil)
3695         (when parent
3696           (delq thread parent)))
3697       (if (gnus-summary-insert-subject id header)
3698           ;; Set the (possibly) new article number in the data structure.
3699           (gnus-data-set-number data (gnus-id-to-article id))
3700         (setcar thread old)
3701         nil))))
3702
3703 (defun gnus-rebuild-thread (id &optional line)
3704   "Rebuild the thread containing ID.
3705 If LINE, insert the rebuilt thread starting on line LINE."
3706   (let ((buffer-read-only nil)
3707         old-pos current thread data)
3708     (if (not gnus-show-threads)
3709         (setq thread (list (car (gnus-id-to-thread id))))
3710       ;; Get the thread this article is part of.
3711       (setq thread (gnus-remove-thread id)))
3712     (setq old-pos (gnus-point-at-bol))
3713     (setq current (save-excursion
3714                     (and (zerop (forward-line -1))
3715                          (gnus-summary-article-number))))
3716     ;; If this is a gathered thread, we have to go some re-gathering.
3717     (when (stringp (car thread))
3718       (let ((subject (car thread))
3719             roots thr)
3720         (setq thread (cdr thread))
3721         (while thread
3722           (unless (memq (setq thr (gnus-id-to-thread
3723                                    (gnus-root-id
3724                                     (mail-header-id (caar thread)))))
3725                         roots)
3726             (push thr roots))
3727           (setq thread (cdr thread)))
3728         ;; We now have all (unique) roots.
3729         (if (= (length roots) 1)
3730             ;; All the loose roots are now one solid root.
3731             (setq thread (car roots))
3732           (setq thread (cons subject (gnus-sort-threads roots))))))
3733     (let (threads)
3734       ;; We then insert this thread into the summary buffer.
3735       (when line
3736         (goto-char (point-min))
3737         (forward-line (1- line)))
3738       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3739         (if gnus-show-threads
3740             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3741           (gnus-summary-prepare-unthreaded thread))
3742         (setq data (nreverse gnus-newsgroup-data))
3743         (setq threads gnus-newsgroup-threads))
3744       ;; We splice the new data into the data structure.
3745       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3746       ;;!!! then we want to insert at the beginning of the buffer.
3747       ;;!!! That happens to be true with Gnus now, but that may
3748       ;;!!! change in the future.  Perhaps.
3749       (gnus-data-enter-list
3750        (if line nil current) data (- (point) old-pos))
3751       (setq gnus-newsgroup-threads
3752             (nconc threads gnus-newsgroup-threads))
3753       (gnus-data-compute-positions))))
3754
3755 (defun gnus-number-to-header (number)
3756   "Return the header for article NUMBER."
3757   (let ((headers gnus-newsgroup-headers))
3758     (while (and headers
3759                 (not (= number (mail-header-number (car headers)))))
3760       (pop headers))
3761     (when headers
3762       (car headers))))
3763
3764 (defun gnus-parent-headers (in-headers &optional generation)
3765   "Return the headers of the GENERATIONeth parent of HEADERS."
3766   (unless generation
3767     (setq generation 1))
3768   (let ((parent t)
3769         (headers in-headers)
3770         references)
3771     (while (and parent
3772                 (not (zerop generation))
3773                 (setq references (mail-header-references headers)))
3774       (setq headers (if (and references
3775                              (setq parent (gnus-parent-id references)))
3776                         (car (gnus-id-to-thread parent))
3777                       nil))
3778       (decf generation))
3779     (and (not (eq headers in-headers))
3780          headers)))
3781
3782 (defun gnus-id-to-thread (id)
3783   "Return the (sub-)thread where ID appears."
3784   (gnus-gethash id gnus-newsgroup-dependencies))
3785
3786 (defun gnus-id-to-article (id)
3787   "Return the article number of ID."
3788   (let ((thread (gnus-id-to-thread id)))
3789     (when (and thread
3790                (car thread))
3791       (mail-header-number (car thread)))))
3792
3793 (defun gnus-id-to-header (id)
3794   "Return the article headers of ID."
3795   (car (gnus-id-to-thread id)))
3796
3797 (defun gnus-article-displayed-root-p (article)
3798   "Say whether ARTICLE is a root(ish) article."
3799   (let ((level (gnus-summary-thread-level article))
3800         (refs (mail-header-references  (gnus-summary-article-header article)))
3801         particle)
3802     (cond
3803      ((null level) nil)
3804      ((zerop level) t)
3805      ((null refs) t)
3806      ((null (gnus-parent-id refs)) t)
3807      ((and (= 1 level)
3808            (null (setq particle (gnus-id-to-article
3809                                  (gnus-parent-id refs))))
3810            (null (gnus-summary-thread-level particle)))))))
3811
3812 (defun gnus-root-id (id)
3813   "Return the id of the root of the thread where ID appears."
3814   (let (last-id prev)
3815     (while (and id (setq prev (car (gnus-id-to-thread id))))
3816       (setq last-id id
3817             id (gnus-parent-id (mail-header-references prev))))
3818     last-id))
3819
3820 (defun gnus-articles-in-thread (thread)
3821   "Return the list of articles in THREAD."
3822   (cons (mail-header-number (car thread))
3823         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3824
3825 (defun gnus-remove-thread (id &optional dont-remove)
3826   "Remove the thread that has ID in it."
3827   (let (headers thread last-id)
3828     ;; First go up in this thread until we find the root.
3829     (setq last-id (gnus-root-id id)
3830           headers (message-flatten-list (gnus-id-to-thread last-id)))
3831     ;; We have now found the real root of this thread.  It might have
3832     ;; been gathered into some loose thread, so we have to search
3833     ;; through the threads to find the thread we wanted.
3834     (let ((threads gnus-newsgroup-threads)
3835           sub)
3836       (while threads
3837         (setq sub (car threads))
3838         (if (stringp (car sub))
3839             ;; This is a gathered thread, so we look at the roots
3840             ;; below it to find whether this article is in this
3841             ;; gathered root.
3842             (progn
3843               (setq sub (cdr sub))
3844               (while sub
3845                 (when (member (caar sub) headers)
3846                   (setq thread (car threads)
3847                         threads nil
3848                         sub nil))
3849                 (setq sub (cdr sub))))
3850           ;; It's an ordinary thread, so we check it.
3851           (when (eq (car sub) (car headers))
3852             (setq thread sub
3853                   threads nil)))
3854         (setq threads (cdr threads)))
3855       ;; If this article is in no thread, then it's a root.
3856       (if thread
3857           (unless dont-remove
3858             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3859         (setq thread (gnus-id-to-thread last-id)))
3860       (when thread
3861         (prog1
3862             thread                      ; We return this thread.
3863           (unless dont-remove
3864             (if (stringp (car thread))
3865                 (progn
3866                   ;; If we use dummy roots, then we have to remove the
3867                   ;; dummy root as well.
3868                   (when (eq gnus-summary-make-false-root 'dummy)
3869                     ;; We go to the dummy root by going to
3870                     ;; the first sub-"thread", and then one line up.
3871                     (gnus-summary-goto-article
3872                      (mail-header-number (caadr thread)))
3873                     (forward-line -1)
3874                     (gnus-delete-line)
3875                     (gnus-data-compute-positions))
3876                   (setq thread (cdr thread))
3877                   (while thread
3878                     (gnus-remove-thread-1 (car thread))
3879                     (setq thread (cdr thread))))
3880               (gnus-remove-thread-1 thread))))))))
3881
3882 (defun gnus-remove-thread-1 (thread)
3883   "Remove the thread THREAD recursively."
3884   (let ((number (mail-header-number (pop thread)))
3885         d)
3886     (setq thread (reverse thread))
3887     (while thread
3888       (gnus-remove-thread-1 (pop thread)))
3889     (when (setq d (gnus-data-find number))
3890       (goto-char (gnus-data-pos d))
3891       (gnus-summary-show-thread)
3892       (gnus-data-remove
3893        number
3894        (- (gnus-point-at-bol)
3895           (prog1
3896               (1+ (gnus-point-at-eol))
3897             (gnus-delete-line)))))))
3898
3899 (defun gnus-sort-threads-1 (threads func)
3900   (sort (mapcar (lambda (thread)
3901                   (cons (car thread)
3902                         (and (cdr thread)
3903                              (gnus-sort-threads-1 (cdr thread) func))))
3904                 threads) func))
3905
3906 (defun gnus-sort-threads (threads)
3907   "Sort THREADS."
3908   (if (not gnus-thread-sort-functions)
3909       threads
3910     (gnus-message 8 "Sorting threads...")
3911     (prog1
3912         (gnus-sort-threads-1
3913          threads
3914          (gnus-make-sort-function gnus-thread-sort-functions))
3915       (gnus-message 8 "Sorting threads...done"))))
3916
3917 (defun gnus-sort-articles (articles)
3918   "Sort ARTICLES."
3919   (when gnus-article-sort-functions
3920     (gnus-message 7 "Sorting articles...")
3921     (prog1
3922         (setq gnus-newsgroup-headers
3923               (sort articles (gnus-make-sort-function
3924                               gnus-article-sort-functions)))
3925       (gnus-message 7 "Sorting articles...done"))))
3926
3927 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3928 (defmacro gnus-thread-header (thread)
3929   "Return header of first article in THREAD.
3930 Note that THREAD must never, ever be anything else than a variable -
3931 using some other form will lead to serious barfage."
3932   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3933   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3934   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3935         (vector thread) 2))
3936
3937 (defsubst gnus-article-sort-by-number (h1 h2)
3938   "Sort articles by article number."
3939   (< (mail-header-number h1)
3940      (mail-header-number h2)))
3941
3942 (defun gnus-thread-sort-by-number (h1 h2)
3943   "Sort threads by root article number."
3944   (gnus-article-sort-by-number
3945    (gnus-thread-header h1) (gnus-thread-header h2)))
3946
3947 (defsubst gnus-article-sort-by-lines (h1 h2)
3948   "Sort articles by article Lines header."
3949   (< (mail-header-lines h1)
3950      (mail-header-lines h2)))
3951
3952 (defun gnus-thread-sort-by-lines (h1 h2)
3953   "Sort threads by root article Lines header."
3954   (gnus-article-sort-by-lines
3955    (gnus-thread-header h1) (gnus-thread-header h2)))
3956
3957 (defsubst gnus-article-sort-by-chars (h1 h2)
3958   "Sort articles by octet length."
3959   (< (mail-header-chars h1)
3960      (mail-header-chars h2)))
3961
3962 (defun gnus-thread-sort-by-chars (h1 h2)
3963   "Sort threads by root article octet length."
3964   (gnus-article-sort-by-chars
3965    (gnus-thread-header h1) (gnus-thread-header h2)))
3966
3967 (defsubst gnus-article-sort-by-author (h1 h2)
3968   "Sort articles by root author."
3969   (string-lessp
3970    (let ((addr (car (mime-entity-read-field h1 'From))))
3971      (or (std11-full-name-string addr)
3972          (std11-address-string addr)
3973          ""))
3974    (let ((addr (car (mime-entity-read-field h2 'From))))
3975      (or (std11-full-name-string addr)
3976          (std11-address-string addr)
3977          ""))
3978    ))
3979
3980 (defun gnus-thread-sort-by-author (h1 h2)
3981   "Sort threads by root author."
3982   (gnus-article-sort-by-author
3983    (gnus-thread-header h1)  (gnus-thread-header h2)))
3984
3985 (defsubst gnus-article-sort-by-subject (h1 h2)
3986   "Sort articles by root subject."
3987   (string-lessp
3988    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3989    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3990
3991 (defun gnus-thread-sort-by-subject (h1 h2)
3992   "Sort threads by root subject."
3993   (gnus-article-sort-by-subject
3994    (gnus-thread-header h1) (gnus-thread-header h2)))
3995
3996 (defsubst gnus-article-sort-by-date (h1 h2)
3997   "Sort articles by root article date."
3998   (time-less-p
3999    (gnus-date-get-time (mail-header-date h1))
4000    (gnus-date-get-time (mail-header-date h2))))
4001
4002 (defun gnus-thread-sort-by-date (h1 h2)
4003   "Sort threads by root article date."
4004   (gnus-article-sort-by-date
4005    (gnus-thread-header h1) (gnus-thread-header h2)))
4006
4007 (defsubst gnus-article-sort-by-score (h1 h2)
4008   "Sort articles by root article score.
4009 Unscored articles will be counted as having a score of zero."
4010   (> (or (cdr (assq (mail-header-number h1)
4011                     gnus-newsgroup-scored))
4012          gnus-summary-default-score 0)
4013      (or (cdr (assq (mail-header-number h2)
4014                     gnus-newsgroup-scored))
4015          gnus-summary-default-score 0)))
4016
4017 (defun gnus-thread-sort-by-score (h1 h2)
4018   "Sort threads by root article score."
4019   (gnus-article-sort-by-score
4020    (gnus-thread-header h1) (gnus-thread-header h2)))
4021
4022 (defun gnus-thread-sort-by-total-score (h1 h2)
4023   "Sort threads by the sum of all scores in the thread.
4024 Unscored articles will be counted as having a score of zero."
4025   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4026
4027 (defun gnus-thread-total-score (thread)
4028   ;; This function find the total score of THREAD.
4029   (cond ((null thread)
4030          0)
4031         ((consp thread)
4032          (if (stringp (car thread))
4033              (apply gnus-thread-score-function 0
4034                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4035            (gnus-thread-total-score-1 thread)))
4036         (t
4037          (gnus-thread-total-score-1 (list thread)))))
4038
4039 (defun gnus-thread-total-score-1 (root)
4040   ;; This function find the total score of the thread below ROOT.
4041   (setq root (car root))
4042   (apply gnus-thread-score-function
4043          (or (append
4044               (mapcar 'gnus-thread-total-score
4045                       (cdr (gnus-id-to-thread (mail-header-id root))))
4046               (when (> (mail-header-number root) 0)
4047                 (list (or (cdr (assq (mail-header-number root)
4048                                      gnus-newsgroup-scored))
4049                           gnus-summary-default-score 0))))
4050              (list gnus-summary-default-score)
4051              '(0))))
4052
4053 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4054 (defvar gnus-tmp-prev-subject nil)
4055 (defvar gnus-tmp-false-parent nil)
4056 (defvar gnus-tmp-root-expunged nil)
4057 (defvar gnus-tmp-dummy-line nil)
4058
4059 (eval-when-compile (defvar gnus-tmp-header))
4060 (defun gnus-extra-header (type &optional header)
4061   "Return the extra header of TYPE."
4062   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4063       ""))
4064
4065 (defun gnus-summary-prepare-threads (threads)
4066   "Prepare summary buffer from THREADS and indentation LEVEL.
4067 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4068 or a straight list of headers."
4069   (gnus-message 7 "Generating summary...")
4070
4071   (setq gnus-newsgroup-threads threads)
4072   (beginning-of-line)
4073
4074   (let ((gnus-tmp-level 0)
4075         (default-score (or gnus-summary-default-score 0))
4076         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4077         thread number subject stack state gnus-tmp-gathered beg-match
4078         new-roots gnus-tmp-new-adopts thread-end
4079         gnus-tmp-header gnus-tmp-unread
4080         gnus-tmp-replied gnus-tmp-subject-or-nil
4081         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4082         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4083         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
4084
4085     (setq gnus-tmp-prev-subject nil)
4086
4087     (if (vectorp (car threads))
4088         ;; If this is a straight (sic) list of headers, then a
4089         ;; threaded summary display isn't required, so we just create
4090         ;; an unthreaded one.
4091         (gnus-summary-prepare-unthreaded threads)
4092
4093       ;; Do the threaded display.
4094
4095       (while (or threads stack gnus-tmp-new-adopts new-roots)
4096
4097         (if (and (= gnus-tmp-level 0)
4098                  (or (not stack)
4099                      (= (caar stack) 0))
4100                  (not gnus-tmp-false-parent)
4101                  (or gnus-tmp-new-adopts new-roots))
4102             (if gnus-tmp-new-adopts
4103                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4104                       thread (list (car gnus-tmp-new-adopts))
4105                       gnus-tmp-header (caar thread)
4106                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4107               (when new-roots
4108                 (setq thread (list (car new-roots))
4109                       gnus-tmp-header (caar thread)
4110                       new-roots (cdr new-roots))))
4111
4112           (if threads
4113               ;; If there are some threads, we do them before the
4114               ;; threads on the stack.
4115               (setq thread threads
4116                     gnus-tmp-header (caar thread))
4117             ;; There were no current threads, so we pop something off
4118             ;; the stack.
4119             (setq state (car stack)
4120                   gnus-tmp-level (car state)
4121                   thread (cdr state)
4122                   stack (cdr stack)
4123                   gnus-tmp-header (caar thread))))
4124
4125         (setq gnus-tmp-false-parent nil)
4126         (setq gnus-tmp-root-expunged nil)
4127         (setq thread-end nil)
4128
4129         (if (stringp gnus-tmp-header)
4130             ;; The header is a dummy root.
4131             (cond
4132              ((eq gnus-summary-make-false-root 'adopt)
4133               ;; We let the first article adopt the rest.
4134               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4135                                                (cddar thread)))
4136               (setq gnus-tmp-gathered
4137                     (nconc (mapcar
4138                             (lambda (h) (mail-header-number (car h)))
4139                             (cddar thread))
4140                            gnus-tmp-gathered))
4141               (setq thread (cons (list (caar thread)
4142                                        (cadar thread))
4143                                  (cdr thread)))
4144               (setq gnus-tmp-level -1
4145                     gnus-tmp-false-parent t))
4146              ((eq gnus-summary-make-false-root 'empty)
4147               ;; We print adopted articles with empty subject fields.
4148               (setq gnus-tmp-gathered
4149                     (nconc (mapcar
4150                             (lambda (h) (mail-header-number (car h)))
4151                             (cddar thread))
4152                            gnus-tmp-gathered))
4153               (setq gnus-tmp-level -1))
4154              ((eq gnus-summary-make-false-root 'dummy)
4155               ;; We remember that we probably want to output a dummy
4156               ;; root.
4157               (setq gnus-tmp-dummy-line gnus-tmp-header)
4158               (setq gnus-tmp-prev-subject gnus-tmp-header))
4159              (t
4160               ;; We do not make a root for the gathered
4161               ;; sub-threads at all.
4162               (setq gnus-tmp-level -1)))
4163
4164           (setq number (mail-header-number gnus-tmp-header)
4165                 subject (mail-header-subject gnus-tmp-header))
4166
4167           (cond
4168            ;; If the thread has changed subject, we might want to make
4169            ;; this subthread into a root.
4170            ((and (null gnus-thread-ignore-subject)
4171                  (not (zerop gnus-tmp-level))
4172                  gnus-tmp-prev-subject
4173                  (not (inline
4174                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4175             (setq new-roots (nconc new-roots (list (car thread)))
4176                   thread-end t
4177                   gnus-tmp-header nil))
4178            ;; If the article lies outside the current limit,
4179            ;; then we do not display it.
4180            ((not (memq number gnus-newsgroup-limit))
4181             (setq gnus-tmp-gathered
4182                   (nconc (mapcar
4183                           (lambda (h) (mail-header-number (car h)))
4184                           (cdar thread))
4185                          gnus-tmp-gathered))
4186             (setq gnus-tmp-new-adopts (if (cdar thread)
4187                                           (append gnus-tmp-new-adopts
4188                                                   (cdar thread))
4189                                         gnus-tmp-new-adopts)
4190                   thread-end t
4191                   gnus-tmp-header nil)
4192             (when (zerop gnus-tmp-level)
4193               (setq gnus-tmp-root-expunged t)))
4194            ;; Perhaps this article is to be marked as read?
4195            ((and gnus-summary-mark-below
4196                  (< (or (cdr (assq number gnus-newsgroup-scored))
4197                         default-score)
4198                     gnus-summary-mark-below)
4199                  ;; Don't touch sparse articles.
4200                  (not (gnus-summary-article-sparse-p number))
4201                  (not (gnus-summary-article-ancient-p number)))
4202             (setq gnus-newsgroup-unreads
4203                   (delq number gnus-newsgroup-unreads))
4204             (if gnus-newsgroup-auto-expire
4205                 (push number gnus-newsgroup-expirable)
4206               (push (cons number gnus-low-score-mark)
4207                     gnus-newsgroup-reads))))
4208
4209           (when gnus-tmp-header
4210             ;; We may have an old dummy line to output before this
4211             ;; article.
4212             (when (and gnus-tmp-dummy-line
4213                        (gnus-subject-equal
4214                         gnus-tmp-dummy-line
4215                         (mail-header-subject gnus-tmp-header)))
4216               (gnus-summary-insert-dummy-line
4217                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4218               (setq gnus-tmp-dummy-line nil))
4219
4220             ;; Compute the mark.
4221             (setq gnus-tmp-unread (gnus-article-mark number))
4222
4223             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4224                                   gnus-tmp-header gnus-tmp-level)
4225                   gnus-newsgroup-data)
4226
4227             ;; Actually insert the line.
4228             (setq
4229              gnus-tmp-subject-or-nil
4230              (cond
4231               ((and gnus-thread-ignore-subject
4232                     gnus-tmp-prev-subject
4233                     (not (inline (gnus-subject-equal
4234                                   gnus-tmp-prev-subject subject))))
4235                subject)
4236               ((zerop gnus-tmp-level)
4237                (if (and (eq gnus-summary-make-false-root 'empty)
4238                         (memq number gnus-tmp-gathered)
4239                         gnus-tmp-prev-subject
4240                         (inline (gnus-subject-equal
4241                                  gnus-tmp-prev-subject subject)))
4242                    gnus-summary-same-subject
4243                  subject))
4244               (t gnus-summary-same-subject)))
4245             (if (and (eq gnus-summary-make-false-root 'adopt)
4246                      (= gnus-tmp-level 1)
4247                      (memq number gnus-tmp-gathered))
4248                 (setq gnus-tmp-opening-bracket ?\<
4249                       gnus-tmp-closing-bracket ?\>)
4250               (setq gnus-tmp-opening-bracket ?\[
4251                     gnus-tmp-closing-bracket ?\]))
4252             (setq
4253              gnus-tmp-indentation
4254              (aref gnus-thread-indent-array gnus-tmp-level)
4255              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4256              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4257                                 gnus-summary-default-score 0)
4258              gnus-tmp-score-char
4259              (if (or (null gnus-summary-default-score)
4260                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4261                          gnus-summary-zcore-fuzz))
4262                  ?  ;Whitespace
4263                (if (< gnus-tmp-score gnus-summary-default-score)
4264                    gnus-score-below-mark gnus-score-over-mark))
4265              gnus-tmp-replied
4266              (cond ((memq number gnus-newsgroup-processable)
4267                     gnus-process-mark)
4268                    ((memq number gnus-newsgroup-cached)
4269                     gnus-cached-mark)
4270                    ((memq number gnus-newsgroup-replied)
4271                     gnus-replied-mark)
4272                    ((memq number gnus-newsgroup-saved)
4273                     gnus-saved-mark)
4274                    (t gnus-no-mark))
4275              gnus-tmp-from (mail-header-from gnus-tmp-header)
4276              gnus-tmp-name
4277              (cond
4278               ((string-match "<[^>]+> *$" gnus-tmp-from)
4279                (setq beg-match (match-beginning 0))
4280                (or (and (string-match "^\".+\"" gnus-tmp-from)
4281                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4282                    (substring gnus-tmp-from 0 beg-match)))
4283               ((string-match "(.+)" gnus-tmp-from)
4284                (substring gnus-tmp-from
4285                           (1+ (match-beginning 0)) (1- (match-end 0))))
4286               (t gnus-tmp-from)))
4287             (when (string= gnus-tmp-name "")
4288               (setq gnus-tmp-name gnus-tmp-from))
4289             (unless (numberp gnus-tmp-lines)
4290               (setq gnus-tmp-lines 0))
4291             (gnus-put-text-property
4292              (point)
4293              (progn (eval gnus-summary-line-format-spec) (point))
4294              'gnus-number number)
4295             (when gnus-visual-p
4296               (forward-line -1)
4297               (gnus-run-hooks 'gnus-summary-update-hook)
4298               (forward-line 1))
4299
4300             (setq gnus-tmp-prev-subject subject)))
4301
4302         (when (nth 1 thread)
4303           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4304         (incf gnus-tmp-level)
4305         (setq threads (if thread-end nil (cdar thread)))
4306         (unless threads
4307           (setq gnus-tmp-level 0)))))
4308   (gnus-message 7 "Generating summary...done"))
4309
4310 (defun gnus-summary-prepare-unthreaded (headers)
4311   "Generate an unthreaded summary buffer based on HEADERS."
4312   (let (header number mark)
4313
4314     (beginning-of-line)
4315
4316     (while headers
4317       ;; We may have to root out some bad articles...
4318       (when (memq (setq number (mail-header-number
4319                                 (setq header (pop headers))))
4320                   gnus-newsgroup-limit)
4321         ;; Mark article as read when it has a low score.
4322         (when (and gnus-summary-mark-below
4323                    (< (or (cdr (assq number gnus-newsgroup-scored))
4324                           gnus-summary-default-score 0)
4325                       gnus-summary-mark-below)
4326                    (not (gnus-summary-article-ancient-p number)))
4327           (setq gnus-newsgroup-unreads
4328                 (delq number gnus-newsgroup-unreads))
4329           (if gnus-newsgroup-auto-expire
4330               (push number gnus-newsgroup-expirable)
4331             (push (cons number gnus-low-score-mark)
4332                   gnus-newsgroup-reads)))
4333
4334         (setq mark (gnus-article-mark number))
4335         (push (gnus-data-make number mark (1+ (point)) header 0)
4336               gnus-newsgroup-data)
4337         (gnus-summary-insert-line
4338          header 0 number
4339          mark (memq number gnus-newsgroup-replied)
4340          (memq number gnus-newsgroup-expirable)
4341          (mail-header-subject header) nil
4342          (cdr (assq number gnus-newsgroup-scored))
4343          (memq number gnus-newsgroup-processable))))))
4344
4345 (defun gnus-summary-remove-list-identifiers ()
4346   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4347   (let ((regexp (if (consp gnus-list-identifiers)
4348                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4349                   gnus-list-identifiers))
4350         changed subject)
4351     (when regexp
4352       (dolist (header gnus-newsgroup-headers)
4353         (setq subject (mail-header-subject header)
4354               changed nil)
4355         (while (string-match
4356                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4357                 subject)
4358           (setq subject
4359                 (concat (substring subject 0 (match-beginning 2))
4360                         (substring subject (match-end 0)))
4361                 changed t))
4362         (when (and changed
4363                    (string-match
4364                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4365           (setq subject
4366                 (concat (substring subject 0 (match-beginning 1))
4367                         (substring subject (match-end 1)))))
4368         (when changed
4369           (mail-header-set-subject header subject))))))
4370
4371 (defun gnus-fetch-headers (articles)
4372   "Fetch headers of ARTICLES."
4373   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4374     (gnus-message 5 "Fetching headers for %s..." name)
4375     (prog1
4376         ;;;!!! FIXME: temporary fix for an infloop on nnimap.
4377         (if (eq 'nnimap (car (gnus-find-method-for-group name)))
4378             (if (eq 'nov
4379                     (setq gnus-headers-retrieved-by
4380                           (gnus-retrieve-headers
4381                            articles gnus-newsgroup-name
4382                            ;; We might want to fetch old headers, but
4383                            ;; not if there is only 1 article.
4384                            (and (or (and
4385                                      (not (eq gnus-fetch-old-headers 'some))
4386                                      (not (numberp gnus-fetch-old-headers)))
4387                                     (> (length articles) 1))
4388                                 gnus-fetch-old-headers))))
4389                 (gnus-get-newsgroup-headers-xover
4390                  articles nil nil gnus-newsgroup-name t)
4391               (gnus-get-newsgroup-headers))
4392           (gnus-retrieve-parsed-headers
4393            articles gnus-newsgroup-name
4394            ;; We might want to fetch old headers, but
4395            ;; not if there is only 1 article.
4396            (and (or (and (not (eq gnus-fetch-old-headers 'some))
4397                          (not (numberp gnus-fetch-old-headers)))
4398                     (> (length articles) 1))
4399                 gnus-fetch-old-headers)))
4400       (gnus-message 5 "Fetching headers for %s...done" name))))
4401
4402 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4403   "Select newsgroup GROUP.
4404 If READ-ALL is non-nil, all articles in the group are selected.
4405 If SELECT-ARTICLES, only select those articles from GROUP."
4406   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4407          ;;!!! Dirty hack; should be removed.
4408          (gnus-summary-ignore-duplicates
4409           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4410               t
4411             gnus-summary-ignore-duplicates))
4412          (info (nth 2 entry))
4413          articles fetched-articles cached)
4414
4415     (unless (gnus-check-server
4416              (setq gnus-current-select-method
4417                    (gnus-find-method-for-group group)))
4418       (error "Couldn't open server"))
4419
4420     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4421         (gnus-activate-group group)     ; Or we can activate it...
4422         (progn                          ; Or we bug out.
4423           (when (equal major-mode 'gnus-summary-mode)
4424             (kill-buffer (current-buffer)))
4425           (error "Couldn't activate group %s: %s"
4426                  group (gnus-status-message group))))
4427
4428     (unless (gnus-request-group group t)
4429       (when (equal major-mode 'gnus-summary-mode)
4430         (kill-buffer (current-buffer)))
4431       (error "Couldn't request group %s: %s"
4432              group (gnus-status-message group)))
4433
4434     (setq gnus-newsgroup-name group)
4435     (setq gnus-newsgroup-unselected nil)
4436     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4437     (gnus-summary-setup-default-charset)
4438
4439     ;; Adjust and set lists of article marks.
4440     (when info
4441       (gnus-adjust-marked-articles info))
4442
4443     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4444     (setq cached
4445           (if (gnus-virtual-group-p group)
4446               gnus-newsgroup-cached
4447             (gnus-cache-articles-in-group group)))
4448
4449     (setq gnus-newsgroup-unreads
4450           (gnus-set-difference
4451            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4452            gnus-newsgroup-dormant))
4453
4454     (setq gnus-newsgroup-processable nil)
4455
4456     (gnus-update-read-articles group gnus-newsgroup-unreads)
4457
4458     (if (setq articles select-articles)
4459         (setq gnus-newsgroup-unselected
4460               (gnus-sorted-intersection
4461                gnus-newsgroup-unreads
4462                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4463       (setq articles (gnus-articles-to-read group read-all)))
4464
4465     (cond
4466      ((null articles)
4467       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4468       'quit)
4469      ((eq articles 0) nil)
4470      (t
4471       ;; Init the dependencies hash table.
4472       (setq gnus-newsgroup-dependencies
4473             (gnus-make-hashtable (length articles)))
4474       (gnus-set-global-variables)
4475       ;; Retrieve the headers and read them in.
4476       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4477
4478       ;; Suppress duplicates?
4479       (when gnus-suppress-duplicates
4480         (gnus-dup-suppress-articles))
4481
4482       ;; Set the initial limit.
4483       (setq gnus-newsgroup-limit (copy-sequence articles))
4484       ;; Remove canceled articles from the list of unread articles.
4485       (setq gnus-newsgroup-unreads
4486             (gnus-set-sorted-intersection
4487              gnus-newsgroup-unreads
4488              (setq fetched-articles
4489                    (mapcar (lambda (headers) (mail-header-number headers))
4490                            gnus-newsgroup-headers))))
4491       ;; Removed marked articles that do not exist.
4492       (gnus-update-missing-marks
4493        (gnus-sorted-complement fetched-articles articles))
4494
4495       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4496       (when cached
4497         (setq gnus-newsgroup-cached cached))
4498
4499       ;; We might want to build some more threads first.
4500       (when (and gnus-fetch-old-headers
4501                  (eq gnus-headers-retrieved-by 'nov))
4502         (if (eq gnus-fetch-old-headers 'invisible)
4503             (gnus-build-all-threads)
4504           (gnus-build-old-threads)))
4505       ;; Let the Gnus agent mark articles as read.
4506       (when gnus-agent
4507         (gnus-agent-get-undownloaded-list))
4508       ;; Remove list identifiers from subject
4509       (when gnus-list-identifiers
4510         (gnus-summary-remove-list-identifiers))
4511       ;; Check whether auto-expire is to be done in this group.
4512       (setq gnus-newsgroup-auto-expire
4513             (gnus-group-auto-expirable-p group))
4514       ;; Set up the article buffer now, if necessary.
4515       (unless gnus-single-article-buffer
4516         (gnus-article-setup-buffer))
4517       ;; First and last article in this newsgroup.
4518       (when gnus-newsgroup-headers
4519         (setq gnus-newsgroup-begin
4520               (mail-header-number (car gnus-newsgroup-headers))
4521               gnus-newsgroup-end
4522               (mail-header-number
4523                (gnus-last-element gnus-newsgroup-headers))))
4524       ;; GROUP is successfully selected.
4525       (or gnus-newsgroup-headers t)))))
4526
4527 (defun gnus-articles-to-read (group &optional read-all)
4528   "Find out what articles the user wants to read."
4529   (let* ((articles
4530           ;; Select all articles if `read-all' is non-nil, or if there
4531           ;; are no unread articles.
4532           (if (or read-all
4533                   (and (zerop (length gnus-newsgroup-marked))
4534                        (zerop (length gnus-newsgroup-unreads)))
4535                   (eq (gnus-group-find-parameter group 'display)
4536                       'all))
4537               (or
4538                (gnus-uncompress-range (gnus-active group))
4539                (gnus-cache-articles-in-group group))
4540             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4541                           (copy-sequence gnus-newsgroup-unreads))
4542                   '<)))
4543          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4544          (scored (length scored-list))
4545          (number (length articles))
4546          (marked (+ (length gnus-newsgroup-marked)
4547                     (length gnus-newsgroup-dormant)))
4548          (select
4549           (cond
4550            ((numberp read-all)
4551             read-all)
4552            (t
4553             (condition-case ()
4554                 (cond
4555                  ((and (or (<= scored marked) (= scored number))
4556                        (natnump gnus-large-newsgroup)
4557                        (> number gnus-large-newsgroup))
4558                   (let* ((cursor-in-echo-area nil)
4559                          (input (read-from-minibuffer
4560                                  (format
4561                                   "How many articles from %s (max %d): "
4562                                   (gnus-limit-string gnus-newsgroup-name 35)
4563                                   number)
4564                                  (cons (number-to-string gnus-large-newsgroup)
4565                                        0))))
4566                     (if (string-match "^[ \t]*$" input)
4567                         number
4568                       input)))
4569                  ((and (> scored marked) (< scored number)
4570                        (> (- scored number) 20))
4571                   (let ((input
4572                          (read-string
4573                           (format "%s %s (%d scored, %d total): "
4574                                   "How many articles from"
4575                                   group scored number))))
4576                     (if (string-match "^[ \t]*$" input)
4577                         number input)))
4578                  (t number))
4579               (quit
4580                (message "Quit getting the articles to read")
4581                nil))))))
4582     (setq select (if (stringp select) (string-to-number select) select))
4583     (if (or (null select) (zerop select))
4584         select
4585       (if (and (not (zerop scored)) (<= (abs select) scored))
4586           (progn
4587             (setq articles (sort scored-list '<))
4588             (setq number (length articles)))
4589         (setq articles (copy-sequence articles)))
4590
4591       (when (< (abs select) number)
4592         (if (< select 0)
4593             ;; Select the N oldest articles.
4594             (setcdr (nthcdr (1- (abs select)) articles) nil)
4595           ;; Select the N most recent articles.
4596           (setq articles (nthcdr (- number select) articles))))
4597       (setq gnus-newsgroup-unselected
4598             (gnus-sorted-intersection
4599              gnus-newsgroup-unreads
4600              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4601       (when gnus-alter-articles-to-read-function
4602         (setq gnus-newsgroup-unreads
4603               (sort
4604                (funcall gnus-alter-articles-to-read-function
4605                         gnus-newsgroup-name gnus-newsgroup-unreads)
4606                '<)))
4607       articles)))
4608
4609 (defun gnus-killed-articles (killed articles)
4610   (let (out)
4611     (while articles
4612       (when (inline (gnus-member-of-range (car articles) killed))
4613         (push (car articles) out))
4614       (setq articles (cdr articles)))
4615     out))
4616
4617 (defun gnus-uncompress-marks (marks)
4618   "Uncompress the mark ranges in MARKS."
4619   (let ((uncompressed '(score bookmark))
4620         out)
4621     (while marks
4622       (if (memq (caar marks) uncompressed)
4623           (push (car marks) out)
4624         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4625       (setq marks (cdr marks)))
4626     out))
4627
4628 (defun gnus-adjust-marked-articles (info)
4629   "Set all article lists and remove all marks that are no longer valid."
4630   (let* ((marked-lists (gnus-info-marks info))
4631          (active (gnus-active (gnus-info-group info)))
4632          (min (car active))
4633          (max (cdr active))
4634          (types gnus-article-mark-lists)
4635          (uncompressed '(score bookmark killed))
4636          marks var articles article mark)
4637
4638     (while marked-lists
4639       (setq marks (pop marked-lists))
4640       (set (setq var (intern (format "gnus-newsgroup-%s"
4641                                      (car (rassq (setq mark (car marks))
4642                                                  types)))))
4643            (if (memq (car marks) uncompressed) (cdr marks)
4644              (gnus-uncompress-range (cdr marks))))
4645
4646       (setq articles (symbol-value var))
4647
4648       ;; All articles have to be subsets of the active articles.
4649       (cond
4650        ;; Adjust "simple" lists.
4651        ((memq mark '(tick dormant expire reply save))
4652         (while articles
4653           (when (or (< (setq article (pop articles)) min) (> article max))
4654             (set var (delq article (symbol-value var))))))
4655        ;; Adjust assocs.
4656        ((memq mark uncompressed)
4657         (when (not (listp (cdr (symbol-value var))))
4658           (set var (list (symbol-value var))))
4659         (when (not (listp (cdr articles)))
4660           (setq articles (list articles)))
4661         (while articles
4662           (when (or (not (consp (setq article (pop articles))))
4663                     (< (car article) min)
4664                     (> (car article) max))
4665             (set var (delq article (symbol-value var))))))))))
4666
4667 (defun gnus-update-missing-marks (missing)
4668   "Go through the list of MISSING articles and remove them from the mark lists."
4669   (when missing
4670     (let ((types gnus-article-mark-lists)
4671           var m)
4672       ;; Go through all types.
4673       (while types
4674         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4675         (when (symbol-value var)
4676           ;; This list has articles.  So we delete all missing articles
4677           ;; from it.
4678           (setq m missing)
4679           (while m
4680             (set var (delq (pop m) (symbol-value var)))))))))
4681
4682 (defun gnus-update-marks ()
4683   "Enter the various lists of marked articles into the newsgroup info list."
4684   (let ((types gnus-article-mark-lists)
4685         (info (gnus-get-info gnus-newsgroup-name))
4686         (uncompressed '(score bookmark killed))
4687         type list newmarked symbol delta-marks)
4688     (when info
4689       ;; Add all marks lists to the list of marks lists.
4690       (while (setq type (pop types))
4691         (setq list (symbol-value
4692                     (setq symbol
4693                           (intern (format "gnus-newsgroup-%s"
4694                                           (car type))))))
4695
4696         (when list
4697           ;; Get rid of the entries of the articles that have the
4698           ;; default score.
4699           (when (and (eq (cdr type) 'score)
4700                      gnus-save-score
4701                      list)
4702             (let* ((arts list)
4703                    (prev (cons nil list))
4704                    (all prev))
4705               (while arts
4706                 (if (or (not (consp (car arts)))
4707                         (= (cdar arts) gnus-summary-default-score))
4708                     (setcdr prev (cdr arts))
4709                   (setq prev arts))
4710                 (setq arts (cdr arts)))
4711               (setq list (cdr all)))))
4712
4713         (unless (memq (cdr type) uncompressed)
4714           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4715
4716         (when (gnus-check-backend-function
4717                'request-set-mark gnus-newsgroup-name)
4718           ;; propagate flags to server, with the following exceptions:
4719           ;; uncompressed:s are not proper flags (they are cons cells)
4720           ;; cache is a internal gnus flag
4721           ;; download are local to one gnus installation (well)
4722           ;; unsend are for nndraft groups only
4723           ;; xxx: generality of this?  this suits nnimap anyway
4724           (unless (memq (cdr type) (append '(cache download unsend)
4725                                            uncompressed))
4726             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4727                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4728                    (add (gnus-remove-from-range
4729                          (gnus-copy-sequence list) old)))
4730               (when add
4731                 (push (list add 'add (list (cdr type))) delta-marks))
4732               (when del
4733                 (push (list del 'del (list (cdr type))) delta-marks)))))
4734
4735         (when list
4736           (push (cons (cdr type) list) newmarked)))
4737
4738       (when delta-marks
4739         (unless (gnus-check-group gnus-newsgroup-name)
4740           (error "Can't open server for %s" gnus-newsgroup-name))
4741         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4742
4743       ;; Enter these new marks into the info of the group.
4744       (if (nthcdr 3 info)
4745           (setcar (nthcdr 3 info) newmarked)
4746         ;; Add the marks lists to the end of the info.
4747         (when newmarked
4748           (setcdr (nthcdr 2 info) (list newmarked))))
4749
4750       ;; Cut off the end of the info if there's nothing else there.
4751       (let ((i 5))
4752         (while (and (> i 2)
4753                     (not (nth i info)))
4754           (when (nthcdr (decf i) info)
4755             (setcdr (nthcdr i info) nil)))))))
4756
4757 (defun gnus-set-mode-line (where)
4758   "Set the mode line of the article or summary buffers.
4759 If WHERE is `summary', the summary mode line format will be used."
4760   ;; Is this mode line one we keep updated?
4761   (when (and (memq where gnus-updated-mode-lines)
4762              (symbol-value
4763               (intern (format "gnus-%s-mode-line-format-spec" where))))
4764     (let (mode-string)
4765       (save-excursion
4766         ;; We evaluate this in the summary buffer since these
4767         ;; variables are buffer-local to that buffer.
4768         (set-buffer gnus-summary-buffer)
4769         ;; We bind all these variables that are used in the `eval' form
4770         ;; below.
4771         (let* ((mformat (symbol-value
4772                          (intern
4773                           (format "gnus-%s-mode-line-format-spec" where))))
4774                (gnus-tmp-group-name (gnus-group-decoded-name 
4775                                      gnus-newsgroup-name))
4776                (gnus-tmp-article-number (or gnus-current-article 0))
4777                (gnus-tmp-unread gnus-newsgroup-unreads)
4778                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4779                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4780                (gnus-tmp-unread-and-unselected
4781                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4782                             (zerop gnus-tmp-unselected))
4783                        "")
4784                       ((zerop gnus-tmp-unselected)
4785                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4786                       (t (format "{%d(+%d) more}"
4787                                  gnus-tmp-unread-and-unticked
4788                                  gnus-tmp-unselected))))
4789                (gnus-tmp-subject
4790                 (if (and gnus-current-headers
4791                          (vectorp gnus-current-headers))
4792                     (gnus-mode-string-quote
4793                      (mail-header-subject gnus-current-headers))
4794                   ""))
4795                bufname-length max-len
4796                gnus-tmp-header);; passed as argument to any user-format-funcs
4797           (setq mode-string (eval mformat))
4798           (setq bufname-length (if (string-match "%b" mode-string)
4799                                    (- (length
4800                                        (buffer-name
4801                                         (if (eq where 'summary)
4802                                             nil
4803                                           (get-buffer gnus-article-buffer))))
4804                                       2)
4805                                  0))
4806           (setq max-len (max 4 (if gnus-mode-non-string-length
4807                                    (- (window-width)
4808                                       gnus-mode-non-string-length
4809                                       bufname-length)
4810                                  (length mode-string))))
4811           ;; We might have to chop a bit of the string off...
4812           (when (> (length mode-string) max-len)
4813             (setq mode-string
4814                   (concat (gnus-truncate-string mode-string (- max-len 3))
4815                           "...")))
4816           ;; Pad the mode string a bit.
4817           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4818       ;; Update the mode line.
4819       (setq mode-line-buffer-identification
4820             (gnus-mode-line-buffer-identification (list mode-string)))
4821       (set-buffer-modified-p t))))
4822
4823 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4824   "Go through the HEADERS list and add all Xrefs to a hash table.
4825 The resulting hash table is returned, or nil if no Xrefs were found."
4826   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4827          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4828          (xref-hashtb (gnus-make-hashtable))
4829          start group entry number xrefs header)
4830     (while headers
4831       (setq header (pop headers))
4832       (when (and (setq xrefs (mail-header-xref header))
4833                  (not (memq (setq number (mail-header-number header))
4834                             unreads)))
4835         (setq start 0)
4836         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4837           (setq start (match-end 0))
4838           (setq group (if prefix
4839                           (concat prefix (substring xrefs (match-beginning 1)
4840                                                     (match-end 1)))
4841                         (substring xrefs (match-beginning 1) (match-end 1))))
4842           (setq number
4843                 (string-to-int (substring xrefs (match-beginning 2)
4844                                           (match-end 2))))
4845           (if (setq entry (gnus-gethash group xref-hashtb))
4846               (setcdr entry (cons number (cdr entry)))
4847             (gnus-sethash group (cons number nil) xref-hashtb)))))
4848     (and start xref-hashtb)))
4849
4850 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4851   "Look through all the headers and mark the Xrefs as read."
4852   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4853         name entry info xref-hashtb idlist method nth4)
4854     (save-excursion
4855       (set-buffer gnus-group-buffer)
4856       (when (setq xref-hashtb
4857                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4858         (mapatoms
4859          (lambda (group)
4860            (unless (string= from-newsgroup (setq name (symbol-name group)))
4861              (setq idlist (symbol-value group))
4862              ;; Dead groups are not updated.
4863              (and (prog1
4864                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4865                             info (nth 2 entry))
4866                     (when (stringp (setq nth4 (gnus-info-method info)))
4867                       (setq nth4 (gnus-server-to-method nth4))))
4868                   ;; Only do the xrefs if the group has the same
4869                   ;; select method as the group we have just read.
4870                   (or (gnus-methods-equal-p
4871                        nth4 (gnus-find-method-for-group from-newsgroup))
4872                       virtual
4873                       (equal nth4 (setq method (gnus-find-method-for-group
4874                                                 from-newsgroup)))
4875                       (and (equal (car nth4) (car method))
4876                            (equal (nth 1 nth4) (nth 1 method))))
4877                   gnus-use-cross-reference
4878                   (or (not (eq gnus-use-cross-reference t))
4879                       virtual
4880                       ;; Only do cross-references on subscribed
4881                       ;; groups, if that is what is wanted.
4882                       (<= (gnus-info-level info) gnus-level-subscribed))
4883                   (gnus-group-make-articles-read name idlist))))
4884          xref-hashtb)))))
4885
4886 (defun gnus-compute-read-articles (group articles)
4887   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4888          (info (nth 2 entry))
4889          (active (gnus-active group))
4890          ninfo)
4891     (when entry
4892       ;; First peel off all invalid article numbers.
4893       (when active
4894         (let ((ids articles)
4895               id first)
4896           (while (setq id (pop ids))
4897             (when (and first (> id (cdr active)))
4898               ;; We'll end up in this situation in one particular
4899               ;; obscure situation.  If you re-scan a group and get
4900               ;; a new article that is cross-posted to a different
4901               ;; group that has not been re-scanned, you might get
4902               ;; crossposted article that has a higher number than
4903               ;; Gnus believes possible.  So we re-activate this
4904               ;; group as well.  This might mean doing the
4905               ;; crossposting thingy will *increase* the number
4906               ;; of articles in some groups.  Tsk, tsk.
4907               (setq active (or (gnus-activate-group group) active)))
4908             (when (or (> id (cdr active))
4909                       (< id (car active)))
4910               (setq articles (delq id articles))))))
4911       ;; If the read list is nil, we init it.
4912       (if (and active
4913                (null (gnus-info-read info))
4914                (> (car active) 1))
4915           (setq ninfo (cons 1 (1- (car active))))
4916         (setq ninfo (gnus-info-read info)))
4917       ;; Then we add the read articles to the range.
4918       (gnus-add-to-range
4919        ninfo (setq articles (sort articles '<))))))
4920
4921 (defun gnus-group-make-articles-read (group articles)
4922   "Update the info of GROUP to say that ARTICLES are read."
4923   (let* ((num 0)
4924          (entry (gnus-gethash group gnus-newsrc-hashtb))
4925          (info (nth 2 entry))
4926          (active (gnus-active group))
4927          range)
4928     (when entry
4929       (setq range (gnus-compute-read-articles group articles))
4930       (save-excursion
4931         (set-buffer gnus-group-buffer)
4932         (gnus-undo-register
4933           `(progn
4934              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4935              (gnus-info-set-read ',info ',(gnus-info-read info))
4936              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4937              (gnus-group-update-group ,group t))))
4938       ;; Add the read articles to the range.
4939       (gnus-info-set-read info range)
4940       ;; Then we have to re-compute how many unread
4941       ;; articles there are in this group.
4942       (when active
4943         (cond
4944          ((not range)
4945           (setq num (- (1+ (cdr active)) (car active))))
4946          ((not (listp (cdr range)))
4947           (setq num (- (cdr active) (- (1+ (cdr range))
4948                                        (car range)))))
4949          (t
4950           (while range
4951             (if (numberp (car range))
4952                 (setq num (1+ num))
4953               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4954             (setq range (cdr range)))
4955           (setq num (- (cdr active) num))))
4956         ;; Update the number of unread articles.
4957         (setcar entry num)
4958         ;; Update the group buffer.
4959         (gnus-group-update-group group t)))))
4960
4961 (defvar gnus-newsgroup-none-id 0)
4962
4963 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4964   (let ((cur nntp-server-buffer)
4965         (dependencies
4966          (or dependencies
4967              (save-excursion (set-buffer gnus-summary-buffer)
4968                              gnus-newsgroup-dependencies)))
4969         headers id end ref
4970         (mail-parse-charset gnus-newsgroup-charset)
4971         (mail-parse-ignored-charsets
4972          (save-excursion (condition-case nil
4973                              (set-buffer gnus-summary-buffer)
4974                            (error))
4975                          gnus-newsgroup-ignored-charsets)))
4976     (save-excursion
4977       (set-buffer nntp-server-buffer)
4978       ;; Translate all TAB characters into SPACE characters.
4979       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4980       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4981       (gnus-run-hooks 'gnus-parse-headers-hook)
4982       (let ((case-fold-search t)
4983             in-reply-to header p lines chars ctype)
4984         (goto-char (point-min))
4985         ;; Search to the beginning of the next header.  Error messages
4986         ;; do not begin with 2 or 3.
4987         (while (re-search-forward "^[23][0-9]+ " nil t)
4988           (setq id nil
4989                 ref nil)
4990           ;; This implementation of this function, with nine
4991           ;; search-forwards instead of the one re-search-forward and
4992           ;; a case (which basically was the old function) is actually
4993           ;; about twice as fast, even though it looks messier.  You
4994           ;; can't have everything, I guess.  Speed and elegance
4995           ;; doesn't always go hand in hand.
4996           (setq
4997            header
4998            (make-full-mail-header
4999             ;; Number.
5000             (prog1
5001                 (read cur)
5002               (end-of-line)
5003               (setq p (point))
5004               (narrow-to-region (point)
5005                                 (or (and (search-forward "\n.\n" nil t)
5006                                          (- (point) 2))
5007                                     (point))))
5008             ;; Subject.
5009             (progn
5010               (goto-char p)
5011               (if (search-forward "\nsubject: " nil t)
5012                   (nnheader-header-value)
5013                 "(none)"))
5014             ;; From.
5015             (progn
5016               (goto-char p)
5017               (if (or (search-forward "\nfrom: " nil t)
5018                       (search-forward "\nfrom:" nil t))
5019                   (nnheader-header-value)
5020                 "(nobody)"))
5021             ;; Date.
5022             (progn
5023               (goto-char p)
5024               (if (search-forward "\ndate: " nil t)
5025                   (nnheader-header-value)
5026                 ""))
5027             ;; Message-ID.
5028             (progn
5029               (goto-char p)
5030               (setq id (if (re-search-forward
5031                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5032                            ;; We do it this way to make sure the Message-ID
5033                            ;; is (somewhat) syntactically valid.
5034                            (buffer-substring (match-beginning 1)
5035                                              (match-end 1))
5036                          ;; If there was no message-id, we just fake one
5037                          ;; to make subsequent routines simpler.
5038                          (nnheader-generate-fake-message-id))))
5039             ;; References.
5040             (progn
5041               (goto-char p)
5042               (if (search-forward "\nreferences: " nil t)
5043                   (progn
5044                     (setq end (point))
5045                     (prog1
5046                         (nnheader-header-value)
5047                       (setq ref
5048                             (buffer-substring
5049                              (progn
5050                                ;; (end-of-line)
5051                                (search-backward ">" end t)
5052                                (1+ (point)))
5053                              (progn
5054                                (search-backward "<" end t)
5055                                (point))))))
5056                 ;; Get the references from the in-reply-to header if there
5057                 ;; were no references and the in-reply-to header looks
5058                 ;; promising.
5059                 (if (and (search-forward "\nin-reply-to: " nil t)
5060                          (setq in-reply-to (nnheader-header-value))
5061                          (string-match "<[^>]+>" in-reply-to))
5062                     (let (ref2)
5063                       (setq ref (substring in-reply-to (match-beginning 0)
5064                                            (match-end 0)))
5065                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5066                         (setq ref2 (substring in-reply-to (match-beginning 0)
5067                                               (match-end 0)))
5068                         (when (> (length ref2) (length ref))
5069                           (setq ref ref2)))
5070                       ref)
5071                   (setq ref nil))))
5072             ;; Chars.
5073             (progn
5074               (goto-char p)
5075               (if (search-forward "\nchars: " nil t)
5076                   (if (numberp (setq chars (ignore-errors (read cur))))
5077                       chars 0)
5078                 0))
5079             ;; Lines.
5080             (progn
5081               (goto-char p)
5082               (if (search-forward "\nlines: " nil t)
5083                   (if (numberp (setq lines (ignore-errors (read cur))))
5084                       lines 0)
5085                 0))
5086             ;; Xref.
5087             (progn
5088               (goto-char p)
5089               (and (search-forward "\nxref: " nil t)
5090                    (nnheader-header-value)))
5091             ;; Extra.
5092             (when gnus-extra-headers
5093               (let ((extra gnus-extra-headers)
5094                     out)
5095                 (while extra
5096                   (goto-char p)
5097                   (when (search-forward
5098                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
5099                     (push (cons (car extra) (nnheader-header-value)) out))
5100                   (pop extra))
5101                 out))))
5102           (goto-char p)
5103           (if (and (search-forward "\ncontent-type: " nil t)
5104                    (setq ctype (nnheader-header-value)))
5105               (mime-entity-set-content-type-internal
5106                header (mime-parse-Content-Type ctype)))
5107           (when (equal id ref)
5108             (setq ref nil))
5109
5110           (when gnus-alter-header-function
5111             (funcall gnus-alter-header-function header)
5112             (setq id (mail-header-id header)
5113                   ref (gnus-parent-id (mail-header-references header))))
5114
5115           (when (setq header
5116                       (gnus-dependencies-add-header
5117                        header dependencies force-new))
5118             (push header headers))
5119           (goto-char (point-max))
5120           (widen))
5121         (nreverse headers)))))
5122
5123 ;; Goes through the xover lines and returns a list of vectors
5124 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5125                                                   force-new dependencies
5126                                                   group also-fetch-heads)
5127   "Parse the news overview data in the server buffer.
5128 Return a list of headers that match SEQUENCE (see
5129 `nntp-retrieve-headers')."
5130   ;; Get the Xref when the users reads the articles since most/some
5131   ;; NNTP servers do not include Xrefs when using XOVER.
5132   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5133   (let ((mail-parse-charset gnus-newsgroup-charset)
5134         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5135         (cur nntp-server-buffer)
5136         (dependencies (or dependencies gnus-newsgroup-dependencies))
5137         number headers header)
5138     (save-excursion
5139       (set-buffer nntp-server-buffer)
5140       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5141       ;; Allow the user to mangle the headers before parsing them.
5142       (gnus-run-hooks 'gnus-parse-headers-hook)
5143       (goto-char (point-min))
5144       (while (not (eobp))
5145         (condition-case ()
5146             (while (and sequence (not (eobp)))
5147               (setq number (read cur))
5148               (while (and sequence
5149                           (< (car sequence) number))
5150                 (setq sequence (cdr sequence)))
5151               (and sequence
5152                    (eq number (car sequence))
5153                    (progn
5154                      (setq sequence (cdr sequence))
5155                      (setq header (inline
5156                                     (gnus-nov-parse-line
5157                                      number dependencies force-new))))
5158                    (push header headers))
5159               (forward-line 1))
5160           (error
5161            (gnus-error 4 "Strange nov line (%d)"
5162                        (count-lines (point-min) (point)))))
5163         (forward-line 1))
5164       ;; A common bug in inn is that if you have posted an article and
5165       ;; then retrieves the active file, it will answer correctly --
5166       ;; the new article is included.  However, a NOV entry for the
5167       ;; article may not have been generated yet, so this may fail.
5168       ;; We work around this problem by retrieving the last few
5169       ;; headers using HEAD.
5170       (if (or (not also-fetch-heads)
5171               (not sequence))
5172           ;; We (probably) got all the headers.
5173           (nreverse headers)
5174         (let ((gnus-nov-is-evil t))
5175           (nconc
5176            (nreverse headers)
5177            ;;;!!! FIXME: temporary fix for an infloop on nnimap.
5178            (if (eq 'nnimap (car (gnus-find-method-for-group group)))
5179                (when (gnus-retrieve-headers sequence group)
5180                  (gnus-get-newsgroup-headers))
5181              (gnus-retrieve-parsed-headers sequence group))))))))
5182
5183 (defun gnus-article-get-xrefs ()
5184   "Fill in the Xref value in `gnus-current-headers', if necessary.
5185 This is meant to be called in `gnus-article-internal-prepare-hook'."
5186   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5187                                  gnus-current-headers)))
5188     (or (not gnus-use-cross-reference)
5189         (not headers)
5190         (and (mail-header-xref headers)
5191              (not (string= (mail-header-xref headers) "")))
5192         (let ((case-fold-search t)
5193               xref)
5194           (save-restriction
5195             (nnheader-narrow-to-headers)
5196             (goto-char (point-min))
5197             (when (or (and (not (eobp))
5198                            (eq (downcase (char-after)) ?x)
5199                            (looking-at "Xref:"))
5200                       (search-forward "\nXref:" nil t))
5201               (goto-char (1+ (match-end 0)))
5202               (setq xref (buffer-substring (point)
5203                                            (progn (end-of-line) (point))))
5204               (mail-header-set-xref headers xref)))))))
5205
5206 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5207   "Find article ID and insert the summary line for that article.
5208 OLD-HEADER can either be a header or a line number to insert
5209 the subject line on."
5210   (let* ((line (and (numberp old-header) old-header))
5211          (old-header (and (vectorp old-header) old-header))
5212          (header (cond ((and old-header use-old-header)
5213                         old-header)
5214                        ((and (numberp id)
5215                              (gnus-number-to-header id))
5216                         (gnus-number-to-header id))
5217                        (t
5218                         (gnus-read-header id))))
5219          (number (and (numberp id) id))
5220          d)
5221     (when header
5222       ;; Rebuild the thread that this article is part of and go to the
5223       ;; article we have fetched.
5224       (when (and (not gnus-show-threads)
5225                  old-header)
5226         (when (and number
5227                    (setq d (gnus-data-find (mail-header-number old-header))))
5228           (goto-char (gnus-data-pos d))
5229           (gnus-data-remove
5230            number
5231            (- (gnus-point-at-bol)
5232               (prog1
5233                   (1+ (gnus-point-at-eol))
5234                 (gnus-delete-line))))))
5235       (when old-header
5236         (mail-header-set-number header (mail-header-number old-header)))
5237       (setq gnus-newsgroup-sparse
5238             (delq (setq number (mail-header-number header))
5239                   gnus-newsgroup-sparse))
5240       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5241       (push number gnus-newsgroup-limit)
5242       (gnus-rebuild-thread (mail-header-id header) line)
5243       (gnus-summary-goto-subject number nil t))
5244     (when (and (numberp number)
5245                (> number 0))
5246       ;; We have to update the boundaries even if we can't fetch the
5247       ;; article if ID is a number -- so that the next `P' or `N'
5248       ;; command will fetch the previous (or next) article even
5249       ;; if the one we tried to fetch this time has been canceled.
5250       (when (> number gnus-newsgroup-end)
5251         (setq gnus-newsgroup-end number))
5252       (when (< number gnus-newsgroup-begin)
5253         (setq gnus-newsgroup-begin number))
5254       (setq gnus-newsgroup-unselected
5255             (delq number gnus-newsgroup-unselected)))
5256     ;; Report back a success?
5257     (and header (mail-header-number header))))
5258
5259 ;;; Process/prefix in the summary buffer
5260
5261 (defun gnus-summary-work-articles (n)
5262   "Return a list of articles to be worked upon.
5263 The prefix argument, the list of process marked articles, and the
5264 current article will be taken into consideration."
5265   (save-excursion
5266     (set-buffer gnus-summary-buffer)
5267     (cond
5268      (n
5269       ;; A numerical prefix has been given.
5270       (setq n (prefix-numeric-value n))
5271       (let ((backward (< n 0))
5272             (n (abs (prefix-numeric-value n)))
5273             articles article)
5274         (save-excursion
5275           (while
5276               (and (> n 0)
5277                    (push (setq article (gnus-summary-article-number))
5278                          articles)
5279                    (if backward
5280                        (gnus-summary-find-prev nil article)
5281                      (gnus-summary-find-next nil article)))
5282             (decf n)))
5283         (nreverse articles)))
5284      ((and (gnus-region-active-p) (mark))
5285       (message "region active")
5286       ;; Work on the region between point and mark.
5287       (let ((max (max (point) (mark)))
5288             articles article)
5289         (save-excursion
5290           (goto-char (min (point) (mark)))
5291           (while
5292               (and
5293                (push (setq article (gnus-summary-article-number)) articles)
5294                (gnus-summary-find-next nil article)
5295                (< (point) max)))
5296           (nreverse articles))))
5297      (gnus-newsgroup-processable
5298       ;; There are process-marked articles present.
5299       ;; Save current state.
5300       (gnus-summary-save-process-mark)
5301       ;; Return the list.
5302       (reverse gnus-newsgroup-processable))
5303      (t
5304       ;; Just return the current article.
5305       (list (gnus-summary-article-number))))))
5306
5307 (defmacro gnus-summary-iterate (arg &rest forms)
5308   "Iterate over the process/prefixed articles and do FORMS.
5309 ARG is the interactive prefix given to the command.  FORMS will be
5310 executed with point over the summary line of the articles."
5311   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5312     `(let ((,articles (gnus-summary-work-articles ,arg)))
5313        (while ,articles
5314          (gnus-summary-goto-subject (car ,articles))
5315          ,@forms
5316          (pop ,articles)))))
5317
5318 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5319 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5320
5321 (defun gnus-summary-save-process-mark ()
5322   "Push the current set of process marked articles on the stack."
5323   (interactive)
5324   (push (copy-sequence gnus-newsgroup-processable)
5325         gnus-newsgroup-process-stack))
5326
5327 (defun gnus-summary-kill-process-mark ()
5328   "Push the current set of process marked articles on the stack and unmark."
5329   (interactive)
5330   (gnus-summary-save-process-mark)
5331   (gnus-summary-unmark-all-processable))
5332
5333 (defun gnus-summary-yank-process-mark ()
5334   "Pop the last process mark state off the stack and restore it."
5335   (interactive)
5336   (unless gnus-newsgroup-process-stack
5337     (error "Empty mark stack"))
5338   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5339
5340 (defun gnus-summary-process-mark-set (set)
5341   "Make SET into the current process marked articles."
5342   (gnus-summary-unmark-all-processable)
5343   (while set
5344     (gnus-summary-set-process-mark (pop set))))
5345
5346 ;;; Searching and stuff
5347
5348 (defun gnus-summary-search-group (&optional backward use-level)
5349   "Search for next unread newsgroup.
5350 If optional argument BACKWARD is non-nil, search backward instead."
5351   (save-excursion
5352     (set-buffer gnus-group-buffer)
5353     (when (gnus-group-search-forward
5354            backward nil (if use-level (gnus-group-group-level) nil))
5355       (gnus-group-group-name))))
5356
5357 (defun gnus-summary-best-group (&optional exclude-group)
5358   "Find the name of the best unread group.
5359 If EXCLUDE-GROUP, do not go to this group."
5360   (save-excursion
5361     (set-buffer gnus-group-buffer)
5362     (save-excursion
5363       (gnus-group-best-unread-group exclude-group))))
5364
5365 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5366   (if backward (gnus-summary-find-prev)
5367     (let* ((dummy (gnus-summary-article-intangible-p))
5368            (article (or article (gnus-summary-article-number)))
5369            (arts (gnus-data-find-list article))
5370            result)
5371       (when (and (not dummy)
5372                  (or (not gnus-summary-check-current)
5373                      (not unread)
5374                      (not (gnus-data-unread-p (car arts)))))
5375         (setq arts (cdr arts)))
5376       (when (setq result
5377                   (if unread
5378                       (progn
5379                         (while arts
5380                           (when (or (and undownloaded
5381                                          (eq gnus-undownloaded-mark
5382                                              (gnus-data-mark (car arts))))
5383                                     (gnus-data-unread-p (car arts)))
5384                             (setq result (car arts)
5385                                   arts nil))
5386                           (setq arts (cdr arts)))
5387                         result)
5388                     (car arts)))
5389         (goto-char (gnus-data-pos result))
5390         (gnus-data-number result)))))
5391
5392 (defun gnus-summary-find-prev (&optional unread article)
5393   (let* ((eobp (eobp))
5394          (article (or article (gnus-summary-article-number)))
5395          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5396          result)
5397     (when (and (not eobp)
5398                (or (not gnus-summary-check-current)
5399                    (not unread)
5400                    (not (gnus-data-unread-p (car arts)))))
5401       (setq arts (cdr arts)))
5402     (when (setq result
5403                 (if unread
5404                     (progn
5405                       (while arts
5406                         (when (gnus-data-unread-p (car arts))
5407                           (setq result (car arts)
5408                                 arts nil))
5409                         (setq arts (cdr arts)))
5410                       result)
5411                   (car arts)))
5412       (goto-char (gnus-data-pos result))
5413       (gnus-data-number result))))
5414
5415 (defun gnus-summary-find-subject (subject &optional unread backward article)
5416   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5417          (article (or article (gnus-summary-article-number)))
5418          (articles (gnus-data-list backward))
5419          (arts (gnus-data-find-list article articles))
5420          result)
5421     (when (or (not gnus-summary-check-current)
5422               (not unread)
5423               (not (gnus-data-unread-p (car arts))))
5424       (setq arts (cdr arts)))
5425     (while arts
5426       (and (or (not unread)
5427                (gnus-data-unread-p (car arts)))
5428            (vectorp (gnus-data-header (car arts)))
5429            (gnus-subject-equal
5430             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5431            (setq result (car arts)
5432                  arts nil))
5433       (setq arts (cdr arts)))
5434     (and result
5435          (goto-char (gnus-data-pos result))
5436          (gnus-data-number result))))
5437
5438 (defun gnus-summary-search-forward (&optional unread subject backward)
5439   "Search forward for an article.
5440 If UNREAD, look for unread articles.  If SUBJECT, look for
5441 articles with that subject.  If BACKWARD, search backward instead."
5442   (cond (subject (gnus-summary-find-subject subject unread backward))
5443         (backward (gnus-summary-find-prev unread))
5444         (t (gnus-summary-find-next unread))))
5445
5446 (defun gnus-recenter (&optional n)
5447   "Center point in window and redisplay frame.
5448 Also do horizontal recentering."
5449   (interactive "P")
5450   (when (and gnus-auto-center-summary
5451              (not (eq gnus-auto-center-summary 'vertical)))
5452     (gnus-horizontal-recenter))
5453   (recenter n))
5454
5455 (defun gnus-summary-recenter ()
5456   "Center point in the summary window.
5457 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5458 displayed, no centering will be performed."
5459   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5460   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5461   (interactive)
5462   (let* ((top (cond ((< (window-height) 4) 0)
5463                     ((< (window-height) 7) 1)
5464                     (t (if (numberp gnus-auto-center-summary)
5465                            gnus-auto-center-summary
5466                          2))))
5467          (height (1- (window-height)))
5468          (bottom (save-excursion (goto-char (point-max))
5469                                  (forward-line (- height))
5470                                  (point)))
5471          (window (get-buffer-window (current-buffer))))
5472     ;; The user has to want it.
5473     (when gnus-auto-center-summary
5474       (when (get-buffer-window gnus-article-buffer)
5475         ;; Only do recentering when the article buffer is displayed,
5476         ;; Set the window start to either `bottom', which is the biggest
5477         ;; possible valid number, or the second line from the top,
5478         ;; whichever is the least.
5479         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5480           (if (> bottom top-pos)
5481               ;; Keep the second line from the top visible
5482               (set-window-start window top-pos t)
5483             ;; Try to keep the bottom line visible; if it's partially
5484             ;; obscured, either scroll one more line to make it fully
5485             ;; visible, or revert to using TOP-POS.
5486             (save-excursion
5487               (goto-char (point-max))
5488               (forward-line -1)
5489               (let ((last-line-start (point)))
5490                 (goto-char bottom)
5491                 (set-window-start window (point) t)
5492                 (when (not (pos-visible-in-window-p last-line-start window))
5493                   (forward-line 1)
5494                   (set-window-start window (min (point) top-pos) t)))))))
5495       ;; Do horizontal recentering while we're at it.
5496       (when (and (get-buffer-window (current-buffer) t)
5497                  (not (eq gnus-auto-center-summary 'vertical)))
5498         (let ((selected (selected-window)))
5499           (select-window (get-buffer-window (current-buffer) t))
5500           (gnus-summary-position-point)
5501           (gnus-horizontal-recenter)
5502           (select-window selected))))))
5503
5504 (defun gnus-summary-jump-to-group (newsgroup)
5505   "Move point to NEWSGROUP in group mode buffer."
5506   ;; Keep update point of group mode buffer if visible.
5507   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5508       (save-window-excursion
5509         ;; Take care of tree window mode.
5510         (when (get-buffer-window gnus-group-buffer)
5511           (pop-to-buffer gnus-group-buffer))
5512         (gnus-group-jump-to-group newsgroup))
5513     (save-excursion
5514       ;; Take care of tree window mode.
5515       (if (get-buffer-window gnus-group-buffer)
5516           (pop-to-buffer gnus-group-buffer)
5517         (set-buffer gnus-group-buffer))
5518       (gnus-group-jump-to-group newsgroup))))
5519
5520 ;; This function returns a list of article numbers based on the
5521 ;; difference between the ranges of read articles in this group and
5522 ;; the range of active articles.
5523 (defun gnus-list-of-unread-articles (group)
5524   (let* ((read (gnus-info-read (gnus-get-info group)))
5525          (active (or (gnus-active group) (gnus-activate-group group)))
5526          (last (cdr active))
5527          first nlast unread)
5528     ;; If none are read, then all are unread.
5529     (if (not read)
5530         (setq first (car active))
5531       ;; If the range of read articles is a single range, then the
5532       ;; first unread article is the article after the last read
5533       ;; article.  Sounds logical, doesn't it?
5534       (if (and (not (listp (cdr read)))
5535                (or (< (car read) (car active))
5536                    (progn (setq read (list read))
5537                           nil)))
5538           (setq first (max (car active) (1+ (cdr read))))
5539         ;; `read' is a list of ranges.
5540         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5541                                   (caar read)))
5542                   1)
5543           (setq first (car active)))
5544         (while read
5545           (when first
5546             (while (< first nlast)
5547               (push first unread)
5548               (setq first (1+ first))))
5549           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5550           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5551           (setq read (cdr read)))))
5552     ;; And add the last unread articles.
5553     (while (<= first last)
5554       (push first unread)
5555       (setq first (1+ first)))
5556     ;; Return the list of unread articles.
5557     (delq 0 (nreverse unread))))
5558
5559 (defun gnus-list-of-read-articles (group)
5560   "Return a list of unread, unticked and non-dormant articles."
5561   (let* ((info (gnus-get-info group))
5562          (marked (gnus-info-marks info))
5563          (active (gnus-active group)))
5564     (and info active
5565          (gnus-set-difference
5566           (gnus-sorted-complement
5567            (gnus-uncompress-range active)
5568            (gnus-list-of-unread-articles group))
5569           (append
5570            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5571            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5572
5573 ;; Various summary commands
5574
5575 (defun gnus-summary-select-article-buffer ()
5576   "Reconfigure windows to show article buffer."
5577   (interactive)
5578   (if (not (gnus-buffer-live-p gnus-article-buffer))
5579       (error "There is no article buffer for this summary buffer")
5580     (gnus-configure-windows 'article)
5581     (select-window (get-buffer-window gnus-article-buffer))))
5582
5583 (defun gnus-summary-universal-argument (arg)
5584   "Perform any operation on all articles that are process/prefixed."
5585   (interactive "P")
5586   (let ((articles (gnus-summary-work-articles arg))
5587         func article)
5588     (if (eq
5589          (setq
5590           func
5591           (key-binding
5592            (read-key-sequence
5593             (substitute-command-keys
5594              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5595          'undefined)
5596         (gnus-error 1 "Undefined key")
5597       (save-excursion
5598         (while articles
5599           (gnus-summary-goto-subject (setq article (pop articles)))
5600           (let (gnus-newsgroup-processable)
5601             (command-execute func))
5602           (gnus-summary-remove-process-mark article)))))
5603   (gnus-summary-position-point))
5604
5605 (defun gnus-summary-toggle-truncation (&optional arg)
5606   "Toggle truncation of summary lines.
5607 With arg, turn line truncation on iff arg is positive."
5608   (interactive "P")
5609   (setq truncate-lines
5610         (if (null arg) (not truncate-lines)
5611           (> (prefix-numeric-value arg) 0)))
5612   (redraw-display))
5613
5614 (defun gnus-summary-reselect-current-group (&optional all rescan)
5615   "Exit and then reselect the current newsgroup.
5616 The prefix argument ALL means to select all articles."
5617   (interactive "P")
5618   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5619     (error "Ephemeral groups can't be reselected"))
5620   (let ((current-subject (gnus-summary-article-number))
5621         (group gnus-newsgroup-name))
5622     (setq gnus-newsgroup-begin nil)
5623     (gnus-summary-exit)
5624     ;; We have to adjust the point of group mode buffer because
5625     ;; point was moved to the next unread newsgroup by exiting.
5626     (gnus-summary-jump-to-group group)
5627     (when rescan
5628       (save-excursion
5629         (save-window-excursion
5630           ;; Don't show group contents.
5631           (set-window-start (selected-window) (point-max))
5632           (gnus-group-get-new-news-this-group 1))))
5633     (gnus-group-read-group all t)
5634     (gnus-summary-goto-subject current-subject nil t)))
5635
5636 (defun gnus-summary-rescan-group (&optional all)
5637   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5638   (interactive "P")
5639   (gnus-summary-reselect-current-group all t))
5640
5641 (defun gnus-summary-update-info (&optional non-destructive)
5642   (save-excursion
5643     (let ((group gnus-newsgroup-name))
5644       (when group
5645         (when gnus-newsgroup-kill-headers
5646           (setq gnus-newsgroup-killed
5647                 (gnus-compress-sequence
5648                  (nconc
5649                   (gnus-set-sorted-intersection
5650                    (gnus-uncompress-range gnus-newsgroup-killed)
5651                    (setq gnus-newsgroup-unselected
5652                          (sort gnus-newsgroup-unselected '<)))
5653                   (setq gnus-newsgroup-unreads
5654                         (sort gnus-newsgroup-unreads '<)))
5655                  t)))
5656         (unless (listp (cdr gnus-newsgroup-killed))
5657           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5658         (let ((headers gnus-newsgroup-headers))
5659           ;; Set the new ranges of read articles.
5660           (save-excursion
5661             (set-buffer gnus-group-buffer)
5662             (gnus-undo-force-boundary))
5663           (gnus-update-read-articles
5664            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5665           ;; Set the current article marks.
5666           (let ((gnus-newsgroup-scored
5667                  (if (and (not gnus-save-score)
5668                           (not non-destructive))
5669                      nil
5670                    gnus-newsgroup-scored)))
5671             (save-excursion
5672               (gnus-update-marks)))
5673           ;; Do the cross-ref thing.
5674           (when gnus-use-cross-reference
5675             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5676           ;; Do not switch windows but change the buffer to work.
5677           (set-buffer gnus-group-buffer)
5678           (unless (gnus-ephemeral-group-p group)
5679             (gnus-group-update-group group)))))))
5680
5681 (defun gnus-summary-save-newsrc (&optional force)
5682   "Save the current number of read/marked articles in the dribble buffer.
5683 The dribble buffer will then be saved.
5684 If FORCE (the prefix), also save the .newsrc file(s)."
5685   (interactive "P")
5686   (gnus-summary-update-info t)
5687   (if force
5688       (gnus-save-newsrc-file)
5689     (gnus-dribble-save)))
5690
5691 (defun gnus-summary-exit (&optional temporary)
5692   "Exit reading current newsgroup, and then return to group selection mode.
5693 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5694   (interactive)
5695   (gnus-set-global-variables)
5696   (gnus-kill-save-kill-buffer)
5697   (gnus-async-halt-prefetch)
5698   (let* ((group gnus-newsgroup-name)
5699          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5700          (mode major-mode)
5701          (group-point nil)
5702          (buf (current-buffer)))
5703     (unless quit-config
5704       ;; Do adaptive scoring, and possibly save score files.
5705       (when gnus-newsgroup-adaptive
5706         (gnus-score-adaptive))
5707       (when gnus-use-scoring
5708         (gnus-score-save)))
5709     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5710     ;; If we have several article buffers, we kill them at exit.
5711     (unless gnus-single-article-buffer
5712       (gnus-kill-buffer gnus-original-article-buffer)
5713       (setq gnus-article-current nil))
5714     (when gnus-use-cache
5715       (gnus-cache-possibly-remove-articles)
5716       (gnus-cache-save-buffers))
5717     (gnus-async-prefetch-remove-group group)
5718     (when gnus-suppress-duplicates
5719       (gnus-dup-enter-articles))
5720     (when gnus-use-trees
5721       (gnus-tree-close group))
5722     (when gnus-use-cache
5723       (gnus-cache-write-active))
5724     ;; Remove entries for this group.
5725     (nnmail-purge-split-history (gnus-group-real-name group))
5726     ;; Make all changes in this group permanent.
5727     (unless quit-config
5728       (gnus-run-hooks 'gnus-exit-group-hook)
5729       (gnus-summary-update-info))
5730     (gnus-close-group group)
5731     ;; Make sure where we were, and go to next newsgroup.
5732     (set-buffer gnus-group-buffer)
5733     (unless quit-config
5734       (gnus-group-jump-to-group group))
5735     (gnus-run-hooks 'gnus-summary-exit-hook)
5736     (unless (or quit-config
5737                 ;; If this group has disappeared from the summary
5738                 ;; buffer, don't skip forwards.
5739                 (not (string= group (gnus-group-group-name))))
5740       (gnus-group-next-unread-group 1))
5741     (setq group-point (point))
5742     (if temporary
5743         nil                             ;Nothing to do.
5744       ;; If we have several article buffers, we kill them at exit.
5745       (unless gnus-single-article-buffer
5746         (gnus-kill-buffer gnus-article-buffer)
5747         (gnus-kill-buffer gnus-original-article-buffer)
5748         (setq gnus-article-current nil))
5749       (set-buffer buf)
5750       (if (not gnus-kill-summary-on-exit)
5751           (gnus-deaden-summary)
5752         ;; We set all buffer-local variables to nil.  It is unclear why
5753         ;; this is needed, but if we don't, buffer-local variables are
5754         ;; not garbage-collected, it seems.  This would the lead to en
5755         ;; ever-growing Emacs.
5756         (gnus-summary-clear-local-variables)
5757         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5758           (gnus-summary-clear-local-variables))
5759         (when (get-buffer gnus-article-buffer)
5760           (bury-buffer gnus-article-buffer))
5761         ;; We clear the global counterparts of the buffer-local
5762         ;; variables as well, just to be on the safe side.
5763         (set-buffer gnus-group-buffer)
5764         (gnus-summary-clear-local-variables)
5765         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5766           (gnus-summary-clear-local-variables))
5767         ;; Return to group mode buffer.
5768         (when (eq mode 'gnus-summary-mode)
5769           (gnus-kill-buffer buf)))
5770       (setq gnus-current-select-method gnus-select-method)
5771       (pop-to-buffer gnus-group-buffer)
5772       (if (not quit-config)
5773           (progn
5774             (goto-char group-point)
5775             (gnus-configure-windows 'group 'force)
5776             (unless (pos-visible-in-window-p)
5777               (forward-line (/ (static-if (featurep 'xemacs)
5778                                    (window-displayed-height)
5779                                  (1- (window-height)))
5780                                -2))
5781               (set-window-start (selected-window) (point))
5782               (goto-char group-point)))
5783         (gnus-handle-ephemeral-exit quit-config))
5784       ;; Clear the current group name.
5785       (unless quit-config
5786         (setq gnus-newsgroup-name nil)))))
5787
5788 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5789 (defun gnus-summary-exit-no-update (&optional no-questions)
5790   "Quit reading current newsgroup without updating read article info."
5791   (interactive)
5792   (let* ((group gnus-newsgroup-name)
5793          (quit-config (gnus-group-quit-config group)))
5794     (when (or no-questions
5795               gnus-expert-user
5796               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5797       (gnus-async-halt-prefetch)
5798       (mapcar 'funcall
5799               (delq 'gnus-summary-expire-articles
5800                     (copy-sequence gnus-summary-prepare-exit-hook)))
5801       ;; If we have several article buffers, we kill them at exit.
5802       (unless gnus-single-article-buffer
5803         (gnus-kill-buffer gnus-article-buffer)
5804         (gnus-kill-buffer gnus-original-article-buffer)
5805         (setq gnus-article-current nil))
5806       (if (not gnus-kill-summary-on-exit)
5807           (gnus-deaden-summary)
5808         (gnus-close-group group)
5809         (gnus-summary-clear-local-variables)
5810         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5811           (gnus-summary-clear-local-variables))
5812         (set-buffer gnus-group-buffer)
5813         (gnus-summary-clear-local-variables)
5814         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5815           (gnus-summary-clear-local-variables))
5816         (when (get-buffer gnus-summary-buffer)
5817           (kill-buffer gnus-summary-buffer)))
5818       (unless gnus-single-article-buffer
5819         (setq gnus-article-current nil))
5820       (when gnus-use-trees
5821         (gnus-tree-close group))
5822       (gnus-async-prefetch-remove-group group)
5823       (when (get-buffer gnus-article-buffer)
5824         (bury-buffer gnus-article-buffer))
5825       ;; Return to the group buffer.
5826       (gnus-configure-windows 'group 'force)
5827       ;; Clear the current group name.
5828       (setq gnus-newsgroup-name nil)
5829       (when (equal (gnus-group-group-name) group)
5830         (gnus-group-next-unread-group 1))
5831       (when quit-config
5832         (gnus-handle-ephemeral-exit quit-config)))))
5833
5834 (defun gnus-handle-ephemeral-exit (quit-config)
5835   "Handle movement when leaving an ephemeral group.
5836 The state which existed when entering the ephemeral is reset."
5837   (if (not (buffer-name (car quit-config)))
5838       (gnus-configure-windows 'group 'force)
5839     (set-buffer (car quit-config))
5840     (cond ((eq major-mode 'gnus-summary-mode)
5841            (gnus-set-global-variables))
5842           ((eq major-mode 'gnus-article-mode)
5843            (save-excursion
5844              ;; The `gnus-summary-buffer' variable may point
5845              ;; to the old summary buffer when using a single
5846              ;; article buffer.
5847              (unless (gnus-buffer-live-p gnus-summary-buffer)
5848                (set-buffer gnus-group-buffer))
5849              (set-buffer gnus-summary-buffer)
5850              (gnus-set-global-variables))))
5851     (if (or (eq (cdr quit-config) 'article)
5852             (eq (cdr quit-config) 'pick))
5853         (progn
5854           ;; The current article may be from the ephemeral group
5855           ;; thus it is best that we reload this article
5856           (gnus-summary-show-article)
5857           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5858               (gnus-configure-windows 'pick 'force)
5859             (gnus-configure-windows (cdr quit-config) 'force)))
5860       (gnus-configure-windows (cdr quit-config) 'force))
5861     (when (eq major-mode 'gnus-summary-mode)
5862       (gnus-summary-next-subject 1 nil t)
5863       (gnus-summary-recenter)
5864       (gnus-summary-position-point))))
5865
5866 (defun gnus-summary-preview-mime-message ()
5867   "MIME decode and play this message."
5868   (interactive)
5869   (let ((gnus-break-pages nil)
5870         (gnus-show-mime t))
5871     (gnus-summary-select-article gnus-show-all-headers t))
5872   (select-window (get-buffer-window gnus-article-buffer)))
5873
5874 ;;; Dead summaries.
5875
5876 (defvar gnus-dead-summary-mode-map nil)
5877
5878 (unless gnus-dead-summary-mode-map
5879   (setq gnus-dead-summary-mode-map (make-keymap))
5880   (suppress-keymap gnus-dead-summary-mode-map)
5881   (substitute-key-definition
5882    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5883   (let ((keys '("\C-d" "\r" "\177" [delete])))
5884     (while keys
5885       (define-key gnus-dead-summary-mode-map
5886         (pop keys) 'gnus-summary-wake-up-the-dead))))
5887
5888 (defvar gnus-dead-summary-mode nil
5889   "Minor mode for Gnus summary buffers.")
5890
5891 (defun gnus-dead-summary-mode (&optional arg)
5892   "Minor mode for Gnus summary buffers."
5893   (interactive "P")
5894   (when (eq major-mode 'gnus-summary-mode)
5895     (make-local-variable 'gnus-dead-summary-mode)
5896     (setq gnus-dead-summary-mode
5897           (if (null arg) (not gnus-dead-summary-mode)
5898             (> (prefix-numeric-value arg) 0)))
5899     (when gnus-dead-summary-mode
5900       (gnus-add-minor-mode
5901        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5902
5903 (defun gnus-deaden-summary ()
5904   "Make the current summary buffer into a dead summary buffer."
5905   ;; Kill any previous dead summary buffer.
5906   (when (and gnus-dead-summary
5907              (buffer-name gnus-dead-summary))
5908     (save-excursion
5909       (set-buffer gnus-dead-summary)
5910       (when gnus-dead-summary-mode
5911         (kill-buffer (current-buffer)))))
5912   ;; Make this the current dead summary.
5913   (setq gnus-dead-summary (current-buffer))
5914   (gnus-dead-summary-mode 1)
5915   (let ((name (buffer-name)))
5916     (when (string-match "Summary" name)
5917       (rename-buffer
5918        (concat (substring name 0 (match-beginning 0)) "Dead "
5919                (substring name (match-beginning 0)))
5920        t)
5921       (bury-buffer))))
5922
5923 (defun gnus-kill-or-deaden-summary (buffer)
5924   "Kill or deaden the summary BUFFER."
5925   (save-excursion
5926     (when (and (buffer-name buffer)
5927                (not gnus-single-article-buffer))
5928       (save-excursion
5929         (set-buffer buffer)
5930         (gnus-kill-buffer gnus-article-buffer)
5931         (gnus-kill-buffer gnus-original-article-buffer)))
5932     (cond (gnus-kill-summary-on-exit
5933            (when (and gnus-use-trees
5934                       (gnus-buffer-exists-p buffer))
5935              (save-excursion
5936                (set-buffer buffer)
5937                (gnus-tree-close gnus-newsgroup-name)))
5938            (gnus-kill-buffer buffer))
5939           ((gnus-buffer-exists-p buffer)
5940            (save-excursion
5941              (set-buffer buffer)
5942              (gnus-deaden-summary))))))
5943
5944 (defun gnus-summary-wake-up-the-dead (&rest args)
5945   "Wake up the dead summary buffer."
5946   (interactive)
5947   (gnus-dead-summary-mode -1)
5948   (let ((name (buffer-name)))
5949     (when (string-match "Dead " name)
5950       (rename-buffer
5951        (concat (substring name 0 (match-beginning 0))
5952                (substring name (match-end 0)))
5953        t)))
5954   (gnus-message 3 "This dead summary is now alive again"))
5955
5956 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5957 (defun gnus-summary-fetch-faq (&optional faq-dir)
5958   "Fetch the FAQ for the current group.
5959 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5960 in."
5961   (interactive
5962    (list
5963     (when current-prefix-arg
5964       (completing-read
5965        "Faq dir: " (and (listp gnus-group-faq-directory)
5966                         (mapcar (lambda (file) (list file))
5967                                 gnus-group-faq-directory))))))
5968   (let (gnus-faq-buffer)
5969     (when (setq gnus-faq-buffer
5970                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5971       (gnus-configure-windows 'summary-faq))))
5972
5973 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5974 (defun gnus-summary-describe-group (&optional force)
5975   "Describe the current newsgroup."
5976   (interactive "P")
5977   (gnus-group-describe-group force gnus-newsgroup-name))
5978
5979 (defun gnus-summary-describe-briefly ()
5980   "Describe summary mode commands briefly."
5981   (interactive)
5982   (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")))
5983
5984 ;; Walking around group mode buffer from summary mode.
5985
5986 (defun gnus-summary-next-group (&optional no-article target-group backward)
5987   "Exit current newsgroup and then select next unread newsgroup.
5988 If prefix argument NO-ARTICLE is non-nil, no article is selected
5989 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5990 previous group instead."
5991   (interactive "P")
5992   ;; Stop pre-fetching.
5993   (gnus-async-halt-prefetch)
5994   (let ((current-group gnus-newsgroup-name)
5995         (current-buffer (current-buffer))
5996         entered)
5997     ;; First we semi-exit this group to update Xrefs and all variables.
5998     ;; We can't do a real exit, because the window conf must remain
5999     ;; the same in case the user is prompted for info, and we don't
6000     ;; want the window conf to change before that...
6001     (gnus-summary-exit t)
6002     (while (not entered)
6003       ;; Then we find what group we are supposed to enter.
6004       (set-buffer gnus-group-buffer)
6005       (gnus-group-jump-to-group current-group)
6006       (setq target-group
6007             (or target-group
6008                 (if (eq gnus-keep-same-level 'best)
6009                     (gnus-summary-best-group gnus-newsgroup-name)
6010                   (gnus-summary-search-group backward gnus-keep-same-level))))
6011       (if (not target-group)
6012           ;; There are no further groups, so we return to the group
6013           ;; buffer.
6014           (progn
6015             (gnus-message 5 "Returning to the group buffer")
6016             (setq entered t)
6017             (when (gnus-buffer-live-p current-buffer)
6018               (set-buffer current-buffer)
6019               (gnus-summary-exit))
6020             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6021         ;; We try to enter the target group.
6022         (gnus-group-jump-to-group target-group)
6023         (let ((unreads (gnus-group-group-unread)))
6024           (if (and (or (eq t unreads)
6025                        (and unreads (not (zerop unreads))))
6026                    (gnus-summary-read-group
6027                     target-group nil no-article
6028                     (and (buffer-name current-buffer) current-buffer)
6029                     nil backward))
6030               (setq entered t)
6031             (setq current-group target-group
6032                   target-group nil)))))))
6033
6034 (defun gnus-summary-prev-group (&optional no-article)
6035   "Exit current newsgroup and then select previous unread newsgroup.
6036 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6037   (interactive "P")
6038   (gnus-summary-next-group no-article nil t))
6039
6040 ;; Walking around summary lines.
6041
6042 (defun gnus-summary-first-subject (&optional unread undownloaded)
6043   "Go to the first unread subject.
6044 If UNREAD is non-nil, go to the first unread article.
6045 Returns the article selected or nil if there are no unread articles."
6046   (interactive "P")
6047   (prog1
6048       (cond
6049        ;; Empty summary.
6050        ((null gnus-newsgroup-data)
6051         (gnus-message 3 "No articles in the group")
6052         nil)
6053        ;; Pick the first article.
6054        ((not unread)
6055         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6056         (gnus-data-number (car gnus-newsgroup-data)))
6057        ;; No unread articles.
6058        ((null gnus-newsgroup-unreads)
6059         (gnus-message 3 "No more unread articles")
6060         nil)
6061        ;; Find the first unread article.
6062        (t
6063         (let ((data gnus-newsgroup-data))
6064           (while (and data
6065                       (and (not (and undownloaded
6066                                      (eq gnus-undownloaded-mark
6067                                          (gnus-data-mark (car data)))))
6068                            (not (gnus-data-unread-p (car data)))))
6069             (setq data (cdr data)))
6070           (when data
6071             (goto-char (gnus-data-pos (car data)))
6072             (gnus-data-number (car data))))))
6073     (gnus-summary-position-point)))
6074
6075 (defun gnus-summary-next-subject (n &optional unread dont-display)
6076   "Go to next N'th summary line.
6077 If N is negative, go to the previous N'th subject line.
6078 If UNREAD is non-nil, only unread articles are selected.
6079 The difference between N and the actual number of steps taken is
6080 returned."
6081   (interactive "p")
6082   (let ((backward (< n 0))
6083         (n (abs n)))
6084     (while (and (> n 0)
6085                 (if backward
6086                     (gnus-summary-find-prev unread)
6087                   (gnus-summary-find-next unread)))
6088       (unless (zerop (setq n (1- n)))
6089         (gnus-summary-show-thread)))
6090     (when (/= 0 n)
6091       (gnus-message 7 "No more%s articles"
6092                     (if unread " unread" "")))
6093     (unless dont-display
6094       (gnus-summary-recenter)
6095       (gnus-summary-position-point))
6096     n))
6097
6098 (defun gnus-summary-next-unread-subject (n)
6099   "Go to next N'th unread summary line."
6100   (interactive "p")
6101   (gnus-summary-next-subject n t))
6102
6103 (defun gnus-summary-prev-subject (n &optional unread)
6104   "Go to previous N'th summary line.
6105 If optional argument UNREAD is non-nil, only unread article is selected."
6106   (interactive "p")
6107   (gnus-summary-next-subject (- n) unread))
6108
6109 (defun gnus-summary-prev-unread-subject (n)
6110   "Go to previous N'th unread summary line."
6111   (interactive "p")
6112   (gnus-summary-next-subject (- n) t))
6113
6114 (defun gnus-summary-goto-subject (article &optional force silent)
6115   "Go the subject line of ARTICLE.
6116 If FORCE, also allow jumping to articles not currently shown."
6117   (interactive "nArticle number: ")
6118   (let ((b (point))
6119         (data (gnus-data-find article)))
6120     ;; We read in the article if we have to.
6121     (and (not data)
6122          force
6123          (gnus-summary-insert-subject
6124           article
6125           (if (or (numberp force) (vectorp force)) force)
6126           t)
6127          (setq data (gnus-data-find article)))
6128     (goto-char b)
6129     (if (not data)
6130         (progn
6131           (unless silent
6132             (gnus-message 3 "Can't find article %d" article))
6133           nil)
6134       (goto-char (gnus-data-pos data))
6135       (gnus-summary-position-point)
6136       article)))
6137
6138 ;; Walking around summary lines with displaying articles.
6139
6140 (defun gnus-summary-expand-window (&optional arg)
6141   "Make the summary buffer take up the entire Emacs frame.
6142 Given a prefix, will force an `article' buffer configuration."
6143   (interactive "P")
6144   (if arg
6145       (gnus-configure-windows 'article 'force)
6146     (gnus-configure-windows 'summary 'force)))
6147
6148 (defun gnus-summary-display-article (article &optional all-header)
6149   "Display ARTICLE in article buffer."
6150   (when (gnus-buffer-live-p gnus-article-buffer)
6151     (with-current-buffer gnus-article-buffer
6152       (set-buffer-multibyte t)))
6153   (gnus-set-global-variables)
6154   (when (gnus-buffer-live-p gnus-article-buffer)
6155     (with-current-buffer gnus-article-buffer
6156       (setq gnus-article-charset gnus-newsgroup-charset)
6157       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)))
6158   (if (null article)
6159       nil
6160     (prog1
6161         (if gnus-summary-display-article-function
6162             (funcall gnus-summary-display-article-function article all-header)
6163           (gnus-article-prepare article all-header))
6164       (with-current-buffer gnus-article-buffer
6165         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6166              nil))
6167       (gnus-run-hooks 'gnus-select-article-hook)
6168       (when (and gnus-current-article
6169                  (not (zerop gnus-current-article)))
6170         (gnus-summary-goto-subject gnus-current-article))
6171       (gnus-summary-recenter)
6172       (when (and gnus-use-trees gnus-show-threads)
6173         (gnus-possibly-generate-tree article)
6174         (gnus-highlight-selected-tree article))
6175       ;; Successfully display article.
6176       (gnus-article-set-window-start
6177        (cdr (assq article gnus-newsgroup-bookmarks))))))
6178
6179 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6180   "Select the current article.
6181 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6182 non-nil, the article will be re-fetched even if it already present in
6183 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6184 be displayed."
6185   ;; Make sure we are in the summary buffer to work around bbdb bug.
6186   (unless (eq major-mode 'gnus-summary-mode)
6187     (set-buffer gnus-summary-buffer))
6188   (let ((article (or article (gnus-summary-article-number)))
6189         (all-headers (not (not all-headers))) ;Must be T or NIL.
6190         gnus-summary-display-article-function)
6191     (and (not pseudo)
6192          (gnus-summary-article-pseudo-p article)
6193          (error "This is a pseudo-article"))
6194     (save-excursion
6195       (set-buffer gnus-summary-buffer)
6196       (if (or (and gnus-single-article-buffer
6197                    (or (null gnus-current-article)
6198                        (null gnus-article-current)
6199                        (null (get-buffer gnus-article-buffer))
6200                        (not (eq article (cdr gnus-article-current)))
6201                        (not (equal (car gnus-article-current)
6202                                    gnus-newsgroup-name))))
6203               (and (not gnus-single-article-buffer)
6204                    (or (null gnus-current-article)
6205                        (not (eq gnus-current-article article))))
6206               force)
6207           ;; The requested article is different from the current article.
6208           (progn
6209             (gnus-summary-display-article article all-headers)
6210             (when (or all-headers gnus-show-all-headers)
6211               (gnus-article-show-all-headers))
6212             (gnus-article-set-window-start
6213              (cdr (assq article gnus-newsgroup-bookmarks)))
6214             article)
6215         (when (or all-headers gnus-show-all-headers)
6216           (gnus-article-show-all-headers))
6217         'old))))
6218
6219 (defun gnus-summary-force-verify-and-decrypt ()
6220   (interactive)
6221   (let ((mm-verify-option 'known)
6222         (mm-decrypt-option 'known))
6223     (gnus-summary-select-article nil 'force)))
6224
6225 (defun gnus-summary-set-current-mark (&optional current-mark)
6226   "Obsolete function."
6227   nil)
6228
6229 (defun gnus-summary-next-article (&optional unread subject backward push)
6230   "Select the next article.
6231 If UNREAD, only unread articles are selected.
6232 If SUBJECT, only articles with SUBJECT are selected.
6233 If BACKWARD, the previous article is selected instead of the next."
6234   (interactive "P")
6235   (cond
6236    ;; Is there such an article?
6237    ((and (gnus-summary-search-forward unread subject backward)
6238          (or (gnus-summary-display-article (gnus-summary-article-number))
6239              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6240     (gnus-summary-position-point))
6241    ;; If not, we try the first unread, if that is wanted.
6242    ((and subject
6243          gnus-auto-select-same
6244          (gnus-summary-first-unread-article))
6245     (gnus-summary-position-point)
6246     (gnus-message 6 "Wrapped"))
6247    ;; Try to get next/previous article not displayed in this group.
6248    ((and gnus-auto-extend-newsgroup
6249          (not unread) (not subject))
6250     (gnus-summary-goto-article
6251      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6252      nil (count-lines (point-min) (point))))
6253    ;; Go to next/previous group.
6254    (t
6255     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6256       (gnus-summary-jump-to-group gnus-newsgroup-name))
6257     (let ((cmd last-command-char)
6258           (point
6259            (save-excursion
6260              (set-buffer gnus-group-buffer)
6261              (point)))
6262           (group
6263            (if (eq gnus-keep-same-level 'best)
6264                (gnus-summary-best-group gnus-newsgroup-name)
6265              (gnus-summary-search-group backward gnus-keep-same-level))))
6266       ;; For some reason, the group window gets selected.  We change
6267       ;; it back.
6268       (select-window (get-buffer-window (current-buffer)))
6269       ;; Select next unread newsgroup automagically.
6270       (cond
6271        ((or (not gnus-auto-select-next)
6272             (not cmd))
6273         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6274        ((or (eq gnus-auto-select-next 'quietly)
6275             (and (eq gnus-auto-select-next 'slightly-quietly)
6276                  push)
6277             (and (eq gnus-auto-select-next 'almost-quietly)
6278                  (gnus-summary-last-article-p)))
6279         ;; Select quietly.
6280         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6281             (gnus-summary-exit)
6282           (gnus-message 7 "No more%s articles (%s)..."
6283                         (if unread " unread" "")
6284                         (if group (concat "selecting " group)
6285                           "exiting"))
6286           (gnus-summary-next-group nil group backward)))
6287        (t
6288         (when (gnus-key-press-event-p last-input-event)
6289           (gnus-summary-walk-group-buffer
6290            gnus-newsgroup-name cmd unread backward point))))))))
6291
6292 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6293   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6294                       (?\C-p (gnus-group-prev-unread-group 1))))
6295         (cursor-in-echo-area t)
6296         keve key group ended)
6297     (save-excursion
6298       (set-buffer gnus-group-buffer)
6299       (goto-char start)
6300       (setq group
6301             (if (eq gnus-keep-same-level 'best)
6302                 (gnus-summary-best-group gnus-newsgroup-name)
6303               (gnus-summary-search-group backward gnus-keep-same-level))))
6304     (while (not ended)
6305       (gnus-message
6306        5 "No more%s articles%s" (if unread " unread" "")
6307        (if (and group
6308                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6309            (format " (Type %s for %s [%s])"
6310                    (single-key-description cmd) group
6311                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6312          (format " (Type %s to exit %s)"
6313                  (single-key-description cmd)
6314                  gnus-newsgroup-name)))
6315       ;; Confirm auto selection.
6316       (setq key (car (setq keve (gnus-read-event-char))))
6317       (setq ended t)
6318       (cond
6319        ((assq key keystrokes)
6320         (let ((obuf (current-buffer)))
6321           (switch-to-buffer gnus-group-buffer)
6322           (when group
6323             (gnus-group-jump-to-group group))
6324           (eval (cadr (assq key keystrokes)))
6325           (setq group (gnus-group-group-name))
6326           (switch-to-buffer obuf))
6327         (setq ended nil))
6328        ((equal key cmd)
6329         (if (or (not group)
6330                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6331             (gnus-summary-exit)
6332           (gnus-summary-next-group nil group backward)))
6333        (t
6334         (push (cdr keve) unread-command-events))))))
6335
6336 (defun gnus-summary-next-unread-article ()
6337   "Select unread article after current one."
6338   (interactive)
6339   (gnus-summary-next-article
6340    (or (not (eq gnus-summary-goto-unread 'never))
6341        (gnus-summary-last-article-p (gnus-summary-article-number)))
6342    (and gnus-auto-select-same
6343         (gnus-summary-article-subject))))
6344
6345 (defun gnus-summary-prev-article (&optional unread subject)
6346   "Select the article after the current one.
6347 If UNREAD is non-nil, only unread articles are selected."
6348   (interactive "P")
6349   (gnus-summary-next-article unread subject t))
6350
6351 (defun gnus-summary-prev-unread-article ()
6352   "Select unread article before current one."
6353   (interactive)
6354   (gnus-summary-prev-article
6355    (or (not (eq gnus-summary-goto-unread 'never))
6356        (gnus-summary-first-article-p (gnus-summary-article-number)))
6357    (and gnus-auto-select-same
6358         (gnus-summary-article-subject))))
6359
6360 (defun gnus-summary-next-page (&optional lines circular)
6361   "Show next page of the selected article.
6362 If at the end of the current article, select the next article.
6363 LINES says how many lines should be scrolled up.
6364
6365 If CIRCULAR is non-nil, go to the start of the article instead of
6366 selecting the next article when reaching the end of the current
6367 article."
6368   (interactive "P")
6369   (setq gnus-summary-buffer (current-buffer))
6370   (gnus-set-global-variables)
6371   (let ((article (gnus-summary-article-number))
6372         (article-window (get-buffer-window gnus-article-buffer t))
6373         endp)
6374     ;; If the buffer is empty, we have no article.
6375     (unless article
6376       (error "No article to select"))
6377     (gnus-configure-windows 'article)
6378     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6379         (if (and (eq gnus-summary-goto-unread 'never)
6380                  (not (gnus-summary-last-article-p article)))
6381             (gnus-summary-next-article)
6382           (gnus-summary-next-unread-article))
6383       (if (or (null gnus-current-article)
6384               (null gnus-article-current)
6385               (/= article (cdr gnus-article-current))
6386               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6387           ;; Selected subject is different from current article's.
6388           (gnus-summary-display-article article)
6389         (when article-window
6390           (gnus-eval-in-buffer-window gnus-article-buffer
6391             (setq endp (gnus-article-next-page lines)))
6392           (when endp
6393             (cond (circular
6394                    (gnus-summary-beginning-of-article))
6395                   (lines
6396                    (gnus-message 3 "End of message"))
6397                   ((null lines)
6398                    (if (and (eq gnus-summary-goto-unread 'never)
6399                             (not (gnus-summary-last-article-p article)))
6400                        (gnus-summary-next-article)
6401                      (gnus-summary-next-unread-article))))))))
6402     (gnus-summary-recenter)
6403     (gnus-summary-position-point)))
6404
6405 (defun gnus-summary-prev-page (&optional lines move)
6406   "Show previous page of selected article.
6407 Argument LINES specifies lines to be scrolled down.
6408 If MOVE, move to the previous unread article if point is at
6409 the beginning of the buffer."
6410   (interactive "P")
6411   (let ((article (gnus-summary-article-number))
6412         (article-window (get-buffer-window gnus-article-buffer t))
6413         endp)
6414     (gnus-configure-windows 'article)
6415     (if (or (null gnus-current-article)
6416             (null gnus-article-current)
6417             (/= article (cdr gnus-article-current))
6418             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6419         ;; Selected subject is different from current article's.
6420         (gnus-summary-display-article article)
6421       (gnus-summary-recenter)
6422       (when article-window
6423         (gnus-eval-in-buffer-window gnus-article-buffer
6424           (setq endp (gnus-article-prev-page lines)))
6425         (when (and move endp)
6426           (cond (lines
6427                  (gnus-message 3 "Beginning of message"))
6428                 ((null lines)
6429                  (if (and (eq gnus-summary-goto-unread 'never)
6430                           (not (gnus-summary-first-article-p article)))
6431                      (gnus-summary-prev-article)
6432                    (gnus-summary-prev-unread-article))))))))
6433   (gnus-summary-position-point))
6434
6435 (defun gnus-summary-prev-page-or-article (&optional lines)
6436   "Show previous page of selected article.
6437 Argument LINES specifies lines to be scrolled down.
6438 If at the beginning of the article, go to the next article."
6439   (interactive "P")
6440   (gnus-summary-prev-page lines t))
6441
6442 (defun gnus-summary-scroll-up (lines)
6443   "Scroll up (or down) one line current article.
6444 Argument LINES specifies lines to be scrolled up (or down if negative)."
6445   (interactive "p")
6446   (gnus-configure-windows 'article)
6447   (gnus-summary-show-thread)
6448   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6449     (gnus-eval-in-buffer-window gnus-article-buffer
6450       (cond ((> lines 0)
6451              (when (gnus-article-next-page lines)
6452                (gnus-message 3 "End of message")))
6453             ((< lines 0)
6454              (gnus-article-prev-page (- lines))))))
6455   (gnus-summary-recenter)
6456   (gnus-summary-position-point))
6457
6458 (defun gnus-summary-scroll-down (lines)
6459   "Scroll down (or up) one line current article.
6460 Argument LINES specifies lines to be scrolled down (or up if negative)."
6461   (interactive "p")
6462   (gnus-summary-scroll-up (- lines)))
6463
6464 (defun gnus-summary-next-same-subject ()
6465   "Select next article which has the same subject as current one."
6466   (interactive)
6467   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6468
6469 (defun gnus-summary-prev-same-subject ()
6470   "Select previous article which has the same subject as current one."
6471   (interactive)
6472   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6473
6474 (defun gnus-summary-next-unread-same-subject ()
6475   "Select next unread article which has the same subject as current one."
6476   (interactive)
6477   (gnus-summary-next-article t (gnus-summary-article-subject)))
6478
6479 (defun gnus-summary-prev-unread-same-subject ()
6480   "Select previous unread article which has the same subject as current one."
6481   (interactive)
6482   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6483
6484 (defun gnus-summary-first-unread-article ()
6485   "Select the first unread article.
6486 Return nil if there are no unread articles."
6487   (interactive)
6488   (prog1
6489       (when (gnus-summary-first-subject t)
6490         (gnus-summary-show-thread)
6491         (gnus-summary-first-subject t)
6492         (gnus-summary-display-article (gnus-summary-article-number)))
6493     (gnus-summary-position-point)))
6494
6495 (defun gnus-summary-first-unread-subject ()
6496   "Place the point on the subject line of 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-position-point)))
6504
6505 (defun gnus-summary-first-article ()
6506   "Select the first article.
6507 Return nil if there are no articles."
6508   (interactive)
6509   (prog1
6510       (when (gnus-summary-first-subject)
6511         (gnus-summary-show-thread)
6512         (gnus-summary-first-subject)
6513         (gnus-summary-display-article (gnus-summary-article-number)))
6514     (gnus-summary-position-point)))
6515
6516 (defun gnus-summary-best-unread-article ()
6517   "Select the unread article with the highest score."
6518   (interactive)
6519   (let ((best -1000000)
6520         (data gnus-newsgroup-data)
6521         article score)
6522     (while data
6523       (and (gnus-data-unread-p (car data))
6524            (> (setq score
6525                     (gnus-summary-article-score (gnus-data-number (car data))))
6526               best)
6527            (setq best score
6528                  article (gnus-data-number (car data))))
6529       (setq data (cdr data)))
6530     (prog1
6531         (if article
6532             (gnus-summary-goto-article article)
6533           (error "No unread articles"))
6534       (gnus-summary-position-point))))
6535
6536 (defun gnus-summary-last-subject ()
6537   "Go to the last displayed subject line in the group."
6538   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6539     (when article
6540       (gnus-summary-goto-subject article))))
6541
6542 (defun gnus-summary-goto-article (article &optional all-headers force)
6543   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6544 If ALL-HEADERS is non-nil, no header lines are hidden.
6545 If FORCE, go to the article even if it isn't displayed.  If FORCE
6546 is a number, it is the line the article is to be displayed on."
6547   (interactive
6548    (list
6549     (completing-read
6550      "Article number or Message-ID: "
6551      (mapcar (lambda (number) (list (int-to-string number)))
6552              gnus-newsgroup-limit))
6553     current-prefix-arg
6554     t))
6555   (prog1
6556       (if (and (stringp article)
6557                (string-match "@" article))
6558           (gnus-summary-refer-article article)
6559         (when (stringp article)
6560           (setq article (string-to-number article)))
6561         (if (gnus-summary-goto-subject article force)
6562             (gnus-summary-display-article article all-headers)
6563           (gnus-message 4 "Couldn't go to article %s" article) nil))
6564     (gnus-summary-position-point)))
6565
6566 (defun gnus-summary-goto-last-article ()
6567   "Go to the previously read article."
6568   (interactive)
6569   (prog1
6570       (when gnus-last-article
6571         (gnus-summary-goto-article gnus-last-article nil t))
6572     (gnus-summary-position-point)))
6573
6574 (defun gnus-summary-pop-article (number)
6575   "Pop one article off the history and go to the previous.
6576 NUMBER articles will be popped off."
6577   (interactive "p")
6578   (let (to)
6579     (setq gnus-newsgroup-history
6580           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6581     (if to
6582         (gnus-summary-goto-article (car to) nil t)
6583       (error "Article history empty")))
6584   (gnus-summary-position-point))
6585
6586 ;; Summary commands and functions for limiting the summary buffer.
6587
6588 (defun gnus-summary-limit-to-articles (n)
6589   "Limit the summary buffer to the next N articles.
6590 If not given a prefix, use the process marked articles instead."
6591   (interactive "P")
6592   (prog1
6593       (let ((articles (gnus-summary-work-articles n)))
6594         (setq gnus-newsgroup-processable nil)
6595         (gnus-summary-limit articles))
6596     (gnus-summary-position-point)))
6597
6598 (defun gnus-summary-pop-limit (&optional total)
6599   "Restore the previous limit.
6600 If given a prefix, remove all limits."
6601   (interactive "P")
6602   (when total
6603     (setq gnus-newsgroup-limits
6604           (list (mapcar (lambda (h) (mail-header-number h))
6605                         gnus-newsgroup-headers))))
6606   (unless gnus-newsgroup-limits
6607     (error "No limit to pop"))
6608   (prog1
6609       (gnus-summary-limit nil 'pop)
6610     (gnus-summary-position-point)))
6611
6612 (defun gnus-summary-limit-to-subject (subject &optional header)
6613   "Limit the summary buffer to articles that have subjects that match a regexp."
6614   (interactive "sLimit to subject (regexp): ")
6615   (unless header
6616     (setq header "subject"))
6617   (when (not (equal "" subject))
6618     (prog1
6619         (let ((articles (gnus-summary-find-matching
6620                          (or header "subject") subject 'all)))
6621           (unless articles
6622             (error "Found no matches for \"%s\"" subject))
6623           (gnus-summary-limit articles))
6624       (gnus-summary-position-point))))
6625
6626 (defun gnus-summary-limit-to-author (from)
6627   "Limit the summary buffer to articles that have authors that match a regexp."
6628   (interactive "sLimit to author (regexp): ")
6629   (gnus-summary-limit-to-subject from "from"))
6630
6631 (defun gnus-summary-limit-to-age (age &optional younger-p)
6632   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6633 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6634 articles that are younger than AGE days."
6635   (interactive
6636    (let ((younger current-prefix-arg)
6637          (days-got nil)
6638          days)
6639      (while (not days-got)
6640        (setq days (if younger
6641                       (read-string "Limit to articles within (in days): ")
6642                     (read-string "Limit to articles older than (in days): ")))
6643        (when (> (length days) 0)
6644          (setq days (read days)))
6645        (if (numberp days)
6646            (setq days-got t)
6647          (message "Please enter a number.")
6648          (sleep-for 1)))
6649      (list days younger)))
6650   (prog1
6651       (let ((data gnus-newsgroup-data)
6652             (cutoff (days-to-time age))
6653             articles d date is-younger)
6654         (while (setq d (pop data))
6655           (when (and (vectorp (gnus-data-header d))
6656                      (setq date (mail-header-date (gnus-data-header d))))
6657             (setq is-younger (time-less-p
6658                               (time-since (condition-case ()
6659                                               (date-to-time date)
6660                                             (error '(0 0))))
6661                               cutoff))
6662             (when (if younger-p
6663                       is-younger
6664                     (not is-younger))
6665               (push (gnus-data-number d) articles))))
6666         (gnus-summary-limit (nreverse articles)))
6667     (gnus-summary-position-point)))
6668
6669 (defun gnus-summary-limit-to-extra (header regexp)
6670   "Limit the summary buffer to articles that match an 'extra' header."
6671   (interactive
6672    (let ((header
6673           (intern
6674            (gnus-completing-read
6675             (symbol-name (car gnus-extra-headers))
6676             "Limit extra header:"
6677             (mapcar (lambda (x)
6678                       (cons (symbol-name x) x))
6679                     gnus-extra-headers)
6680             nil
6681             t))))
6682      (list header
6683            (read-string (format "Limit to header %s (regexp): " header)))))
6684   (when (not (equal "" regexp))
6685     (prog1
6686         (let ((articles (gnus-summary-find-matching
6687                          (cons 'extra header) regexp 'all)))
6688           (unless articles
6689             (error "Found no matches for \"%s\"" regexp))
6690           (gnus-summary-limit articles))
6691       (gnus-summary-position-point))))
6692
6693 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6694 (make-obsolete
6695  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6696
6697 (defun gnus-summary-limit-to-unread (&optional all)
6698   "Limit the summary buffer to articles that are not marked as read.
6699 If ALL is non-nil, limit strictly to unread articles."
6700   (interactive "P")
6701   (if all
6702       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6703     (gnus-summary-limit-to-marks
6704      ;; Concat all the marks that say that an article is read and have
6705      ;; those removed.
6706      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6707            gnus-killed-mark gnus-kill-file-mark
6708            gnus-low-score-mark gnus-expirable-mark
6709            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6710            gnus-duplicate-mark gnus-souped-mark)
6711      'reverse)))
6712
6713 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6714 (make-obsolete 'gnus-summary-delete-marked-with
6715                'gnus-summary-limit-exlude-marks)
6716
6717 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6718   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6719 If REVERSE, limit the summary buffer to articles that are marked
6720 with MARKS.  MARKS can either be a string of marks or a list of marks.
6721 Returns how many articles were removed."
6722   (interactive "sMarks: ")
6723   (gnus-summary-limit-to-marks marks t))
6724
6725 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6726   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6727 If REVERSE (the prefix), limit the summary buffer to articles that are
6728 not marked with MARKS.  MARKS can either be a string of marks or a
6729 list of marks.
6730 Returns how many articles were removed."
6731   (interactive "sMarks: \nP")
6732   (prog1
6733       (let ((data gnus-newsgroup-data)
6734             (marks (if (listp marks) marks
6735                      (append marks nil))) ; Transform to list.
6736             articles)
6737         (while data
6738           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6739                   (memq (gnus-data-mark (car data)) marks))
6740             (push (gnus-data-number (car data)) articles))
6741           (setq data (cdr data)))
6742         (gnus-summary-limit articles))
6743     (gnus-summary-position-point)))
6744
6745 (defun gnus-summary-limit-to-score (&optional score)
6746   "Limit to articles with score at or above SCORE."
6747   (interactive "P")
6748   (setq score (if score
6749                   (prefix-numeric-value score)
6750                 (or gnus-summary-default-score 0)))
6751   (let ((data gnus-newsgroup-data)
6752         articles)
6753     (while data
6754       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6755                 score)
6756         (push (gnus-data-number (car data)) articles))
6757       (setq data (cdr data)))
6758     (prog1
6759         (gnus-summary-limit articles)
6760       (gnus-summary-position-point))))
6761
6762 (defun gnus-summary-limit-include-thread (id)
6763   "Display all the hidden articles that in the current thread."
6764   (interactive (list (mail-header-id (gnus-summary-article-header))))
6765   (let ((articles (gnus-articles-in-thread
6766                    (gnus-id-to-thread (gnus-root-id id)))))
6767     (prog1
6768         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6769       (gnus-summary-position-point))))
6770
6771 (defun gnus-summary-limit-include-dormant ()
6772   "Display all the hidden articles that are marked as dormant.
6773 Note that this command only works on a subset of the articles currently
6774 fetched for this group."
6775   (interactive)
6776   (unless gnus-newsgroup-dormant
6777     (error "There are no dormant articles in this group"))
6778   (prog1
6779       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6780     (gnus-summary-position-point)))
6781
6782 (defun gnus-summary-limit-exclude-dormant ()
6783   "Hide all dormant articles."
6784   (interactive)
6785   (prog1
6786       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6787     (gnus-summary-position-point)))
6788
6789 (defun gnus-summary-limit-exclude-childless-dormant ()
6790   "Hide all dormant articles that have no children."
6791   (interactive)
6792   (let ((data (gnus-data-list t))
6793         articles d children)
6794     ;; Find all articles that are either not dormant or have
6795     ;; children.
6796     (while (setq d (pop data))
6797       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6798                 (and (setq children
6799                            (gnus-article-children (gnus-data-number d)))
6800                      (let (found)
6801                        (while children
6802                          (when (memq (car children) articles)
6803                            (setq children nil
6804                                  found t))
6805                          (pop children))
6806                        found)))
6807         (push (gnus-data-number d) articles)))
6808     ;; Do the limiting.
6809     (prog1
6810         (gnus-summary-limit articles)
6811       (gnus-summary-position-point))))
6812
6813 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6814   "Mark all unread excluded articles as read.
6815 If ALL, mark even excluded ticked and dormants as read."
6816   (interactive "P")
6817   (let ((articles (gnus-sorted-complement
6818                    (sort
6819                     (mapcar (lambda (h) (mail-header-number h))
6820                             gnus-newsgroup-headers)
6821                     '<)
6822                    (sort gnus-newsgroup-limit '<)))
6823         article)
6824     (setq gnus-newsgroup-unreads
6825           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6826     (if all
6827         (setq gnus-newsgroup-dormant nil
6828               gnus-newsgroup-marked nil
6829               gnus-newsgroup-reads
6830               (nconc
6831                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6832                gnus-newsgroup-reads))
6833       (while (setq article (pop articles))
6834         (unless (or (memq article gnus-newsgroup-dormant)
6835                     (memq article gnus-newsgroup-marked))
6836           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6837
6838 (defun gnus-summary-limit (articles &optional pop)
6839   (if pop
6840       ;; We pop the previous limit off the stack and use that.
6841       (setq articles (car gnus-newsgroup-limits)
6842             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6843     ;; We use the new limit, so we push the old limit on the stack.
6844     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6845   ;; Set the limit.
6846   (setq gnus-newsgroup-limit articles)
6847   (let ((total (length gnus-newsgroup-data))
6848         (data (gnus-data-find-list (gnus-summary-article-number)))
6849         (gnus-summary-mark-below nil)   ; Inhibit this.
6850         found)
6851     ;; This will do all the work of generating the new summary buffer
6852     ;; according to the new limit.
6853     (gnus-summary-prepare)
6854     ;; Hide any threads, possibly.
6855     (and gnus-show-threads
6856          gnus-thread-hide-subtree
6857          (gnus-summary-hide-all-threads))
6858     ;; Try to return to the article you were at, or one in the
6859     ;; neighborhood.
6860     (when data
6861       ;; We try to find some article after the current one.
6862       (while data
6863         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6864           (setq data nil
6865                 found t))
6866         (setq data (cdr data))))
6867     (unless found
6868       ;; If there is no data, that means that we were after the last
6869       ;; article.  The same goes when we can't find any articles
6870       ;; after the current one.
6871       (goto-char (point-max))
6872       (gnus-summary-find-prev))
6873     (gnus-set-mode-line 'summary)
6874     ;; We return how many articles were removed from the summary
6875     ;; buffer as a result of the new limit.
6876     (- total (length gnus-newsgroup-data))))
6877
6878 (defsubst gnus-invisible-cut-children (threads)
6879   (let ((num 0))
6880     (while threads
6881       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6882         (incf num))
6883       (pop threads))
6884     (< num 2)))
6885
6886 (defsubst gnus-cut-thread (thread)
6887   "Go forwards in the thread until we find an article that we want to display."
6888   (when (or (eq gnus-fetch-old-headers 'some)
6889             (eq gnus-fetch-old-headers 'invisible)
6890             (numberp gnus-fetch-old-headers)
6891             (eq gnus-build-sparse-threads 'some)
6892             (eq gnus-build-sparse-threads 'more))
6893     ;; Deal with old-fetched headers and sparse threads.
6894     (while (and
6895             thread
6896             (or
6897              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6898              (gnus-summary-article-ancient-p
6899               (mail-header-number (car thread))))
6900             (if (or (<= (length (cdr thread)) 1)
6901                     (eq gnus-fetch-old-headers 'invisible))
6902                 (setq gnus-newsgroup-limit
6903                       (delq (mail-header-number (car thread))
6904                             gnus-newsgroup-limit)
6905                       thread (cadr thread))
6906               (when (gnus-invisible-cut-children (cdr thread))
6907                 (let ((th (cdr thread)))
6908                   (while th
6909                     (if (memq (mail-header-number (caar th))
6910                               gnus-newsgroup-limit)
6911                         (setq thread (car th)
6912                               th nil)
6913                       (setq th (cdr th))))))))))
6914   thread)
6915
6916 (defun gnus-cut-threads (threads)
6917   "Cut off all uninteresting articles from the beginning of threads."
6918   (when (or (eq gnus-fetch-old-headers 'some)
6919             (eq gnus-fetch-old-headers 'invisible)
6920             (numberp gnus-fetch-old-headers)
6921             (eq gnus-build-sparse-threads 'some)
6922             (eq gnus-build-sparse-threads 'more))
6923     (let ((th threads))
6924       (while th
6925         (setcar th (gnus-cut-thread (car th)))
6926         (setq th (cdr th)))))
6927   ;; Remove nixed out threads.
6928   (delq nil threads))
6929
6930 (defun gnus-summary-initial-limit (&optional show-if-empty)
6931   "Figure out what the initial limit is supposed to be on group entry.
6932 This entails weeding out unwanted dormants, low-scored articles,
6933 fetch-old-headers verbiage, and so on."
6934   ;; Most groups have nothing to remove.
6935   (if (or gnus-inhibit-limiting
6936           (and (null gnus-newsgroup-dormant)
6937                (not (eq gnus-fetch-old-headers 'some))
6938                (not (numberp gnus-fetch-old-headers))
6939                (not (eq gnus-fetch-old-headers 'invisible))
6940                (null gnus-summary-expunge-below)
6941                (not (eq gnus-build-sparse-threads 'some))
6942                (not (eq gnus-build-sparse-threads 'more))
6943                (null gnus-thread-expunge-below)
6944                (not gnus-use-nocem)))
6945       ()                                ; Do nothing.
6946     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6947     (setq gnus-newsgroup-limit nil)
6948     (mapatoms
6949      (lambda (node)
6950        (unless (car (symbol-value node))
6951          ;; These threads have no parents -- they are roots.
6952          (let ((nodes (cdr (symbol-value node)))
6953                thread)
6954            (while nodes
6955              (if (and gnus-thread-expunge-below
6956                       (< (gnus-thread-total-score (car nodes))
6957                          gnus-thread-expunge-below))
6958                  (gnus-expunge-thread (pop nodes))
6959                (setq thread (pop nodes))
6960                (gnus-summary-limit-children thread))))))
6961      gnus-newsgroup-dependencies)
6962     ;; If this limitation resulted in an empty group, we might
6963     ;; pop the previous limit and use it instead.
6964     (when (and (not gnus-newsgroup-limit)
6965                show-if-empty)
6966       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6967     gnus-newsgroup-limit))
6968
6969 (defun gnus-summary-limit-children (thread)
6970   "Return 1 if this subthread is visible and 0 if it is not."
6971   ;; First we get the number of visible children to this thread.  This
6972   ;; is done by recursing down the thread using this function, so this
6973   ;; will really go down to a leaf article first, before slowly
6974   ;; working its way up towards the root.
6975   (when thread
6976     (let ((children
6977            (if (cdr thread)
6978                (apply '+ (mapcar 'gnus-summary-limit-children
6979                                  (cdr thread)))
6980              0))
6981           (number (mail-header-number (car thread)))
6982           score)
6983       (if (and
6984            (not (memq number gnus-newsgroup-marked))
6985            (or
6986             ;; If this article is dormant and has absolutely no visible
6987             ;; children, then this article isn't visible.
6988             (and (memq number gnus-newsgroup-dormant)
6989                  (zerop children))
6990             ;; If this is "fetch-old-headered" and there is no
6991             ;; visible children, then we don't want this article.
6992             (and (or (eq gnus-fetch-old-headers 'some)
6993                      (numberp gnus-fetch-old-headers))
6994                  (gnus-summary-article-ancient-p number)
6995                  (zerop children))
6996             ;; If this is "fetch-old-headered" and `invisible', then
6997             ;; we don't want this article.
6998             (and (eq gnus-fetch-old-headers 'invisible)
6999                  (gnus-summary-article-ancient-p number))
7000             ;; If this is a sparsely inserted article with no children,
7001             ;; we don't want it.
7002             (and (eq gnus-build-sparse-threads 'some)
7003                  (gnus-summary-article-sparse-p number)
7004                  (zerop children))
7005             ;; If we use expunging, and this article is really
7006             ;; low-scored, then we don't want this article.
7007             (when (and gnus-summary-expunge-below
7008                        (< (setq score
7009                                 (or (cdr (assq number gnus-newsgroup-scored))
7010                                     gnus-summary-default-score))
7011                           gnus-summary-expunge-below))
7012               ;; We increase the expunge-tally here, but that has
7013               ;; nothing to do with the limits, really.
7014               (incf gnus-newsgroup-expunged-tally)
7015               ;; We also mark as read here, if that's wanted.
7016               (when (and gnus-summary-mark-below
7017                          (< score gnus-summary-mark-below))
7018                 (setq gnus-newsgroup-unreads
7019                       (delq number gnus-newsgroup-unreads))
7020                 (if gnus-newsgroup-auto-expire
7021                     (push number gnus-newsgroup-expirable)
7022                   (push (cons number gnus-low-score-mark)
7023                         gnus-newsgroup-reads)))
7024               t)
7025             ;; Check NoCeM things.
7026             (if (and gnus-use-nocem
7027                      (gnus-nocem-unwanted-article-p
7028                       (mail-header-id (car thread))))
7029                 (progn
7030                   (setq gnus-newsgroup-unreads
7031                         (delq number gnus-newsgroup-unreads))
7032                   t))))
7033           ;; Nope, invisible article.
7034           0
7035         ;; Ok, this article is to be visible, so we add it to the limit
7036         ;; and return 1.
7037         (push number gnus-newsgroup-limit)
7038         1))))
7039
7040 (defun gnus-expunge-thread (thread)
7041   "Mark all articles in THREAD as read."
7042   (let* ((number (mail-header-number (car thread))))
7043     (incf gnus-newsgroup-expunged-tally)
7044     ;; We also mark as read here, if that's wanted.
7045     (setq gnus-newsgroup-unreads
7046           (delq number gnus-newsgroup-unreads))
7047     (if gnus-newsgroup-auto-expire
7048         (push number gnus-newsgroup-expirable)
7049       (push (cons number gnus-low-score-mark)
7050             gnus-newsgroup-reads)))
7051   ;; Go recursively through all subthreads.
7052   (mapcar 'gnus-expunge-thread (cdr thread)))
7053
7054 ;; Summary article oriented commands
7055
7056 (defun gnus-summary-refer-parent-article (n)
7057   "Refer parent article N times.
7058 If N is negative, go to ancestor -N instead.
7059 The difference between N and the number of articles fetched is returned."
7060   (interactive "p")
7061   (let ((skip 1)
7062         error header ref)
7063     (when (not (natnump n))
7064       (setq skip (abs n)
7065             n 1))
7066     (while (and (> n 0)
7067                 (not error))
7068       (setq header (gnus-summary-article-header))
7069       (if (and (eq (mail-header-number header)
7070                    (cdr gnus-article-current))
7071                (equal gnus-newsgroup-name
7072                       (car gnus-article-current)))
7073           ;; If we try to find the parent of the currently
7074           ;; displayed article, then we take a look at the actual
7075           ;; References header, since this is slightly more
7076           ;; reliable than the References field we got from the
7077           ;; server.
7078           (save-excursion
7079             (set-buffer gnus-original-article-buffer)
7080             (nnheader-narrow-to-headers)
7081             (unless (setq ref (message-fetch-field "references"))
7082               (setq ref (message-fetch-field "in-reply-to")))
7083             (widen))
7084         (setq ref
7085               ;; It's not the current article, so we take a bet on
7086               ;; the value we got from the server.
7087               (mail-header-references header)))
7088       (if (and ref
7089                (not (equal ref "")))
7090           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7091             (gnus-message 1 "Couldn't find parent"))
7092         (gnus-message 1 "No references in article %d"
7093                       (gnus-summary-article-number))
7094         (setq error t))
7095       (decf n))
7096     (gnus-summary-position-point)
7097     n))
7098
7099 (defun gnus-summary-refer-references ()
7100   "Fetch all articles mentioned in the References header.
7101 Return the number of articles fetched."
7102   (interactive)
7103   (let ((ref (mail-header-references (gnus-summary-article-header)))
7104         (current (gnus-summary-article-number))
7105         (n 0))
7106     (if (or (not ref)
7107             (equal ref ""))
7108         (error "No References in the current article")
7109       ;; For each Message-ID in the References header...
7110       (while (string-match "<[^>]*>" ref)
7111         (incf n)
7112         ;; ... fetch that article.
7113         (gnus-summary-refer-article
7114          (prog1 (match-string 0 ref)
7115            (setq ref (substring ref (match-end 0))))))
7116       (gnus-summary-goto-subject current)
7117       (gnus-summary-position-point)
7118       n)))
7119
7120 (defun gnus-summary-refer-thread (&optional limit)
7121   "Fetch all articles in the current thread.
7122 If LIMIT (the numerical prefix), fetch that many old headers instead
7123 of what's specified by the `gnus-refer-thread-limit' variable."
7124   (interactive "P")
7125   (let ((id (mail-header-id (gnus-summary-article-header)))
7126         (limit (if limit (prefix-numeric-value limit)
7127                  gnus-refer-thread-limit)))
7128     ;; We want to fetch LIMIT *old* headers, but we also have to
7129     ;; re-fetch all the headers in the current buffer, because many of
7130     ;; them may be undisplayed.  So we adjust LIMIT.
7131     (when (numberp limit)
7132       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7133     (unless (eq gnus-fetch-old-headers 'invisible)
7134       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7135       ;; Retrieve the headers and read them in.
7136       (if (eq (gnus-retrieve-headers
7137                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7138               'nov)
7139           (gnus-build-all-threads)
7140         (error "Can't fetch thread from backends that don't support NOV"))
7141       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7142     (gnus-summary-limit-include-thread id)))
7143
7144 (defun gnus-summary-refer-article (message-id)
7145   "Fetch an article specified by MESSAGE-ID."
7146   (interactive "sMessage-ID: ")
7147   (when (and (stringp message-id)
7148              (not (zerop (length message-id))))
7149     ;; Construct the correct Message-ID if necessary.
7150     ;; Suggested by tale@pawl.rpi.edu.
7151     (unless (string-match "^<" message-id)
7152       (setq message-id (concat "<" message-id)))
7153     (unless (string-match ">$" message-id)
7154       (setq message-id (concat message-id ">")))
7155     (let* ((header (gnus-id-to-header message-id))
7156            (sparse (and header
7157                         (gnus-summary-article-sparse-p
7158                          (mail-header-number header))
7159                         (memq (mail-header-number header)
7160                               gnus-newsgroup-limit)))
7161            number)
7162       (cond
7163        ;; If the article is present in the buffer we just go to it.
7164        ((and header
7165              (or (not (gnus-summary-article-sparse-p
7166                        (mail-header-number header)))
7167                  sparse))
7168         (prog1
7169             (gnus-summary-goto-article
7170              (mail-header-number header) nil t)
7171           (when sparse
7172             (gnus-summary-update-article (mail-header-number header)))))
7173        (t
7174         ;; We fetch the article.
7175         (catch 'found
7176           (dolist (gnus-override-method (gnus-refer-article-methods))
7177             (gnus-check-server gnus-override-method)
7178             ;; Fetch the header, and display the article.
7179             (when (setq number (gnus-summary-insert-subject message-id))
7180               (gnus-summary-select-article nil nil nil number)
7181               (throw 'found t)))
7182           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7183
7184 (defun gnus-refer-article-methods ()
7185   "Return a list of referrable methods."
7186   (cond
7187    ;; No method, so we default to current and native.
7188    ((null gnus-refer-article-method)
7189     (list gnus-current-select-method gnus-select-method))
7190    ;; Current.
7191    ((eq 'current gnus-refer-article-method)
7192     (list gnus-current-select-method))
7193    ;; List of select methods.
7194    ((not (and (symbolp (car gnus-refer-article-method))
7195               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7196     (let (out)
7197       (dolist (method gnus-refer-article-method)
7198         (push (if (eq 'current method)
7199                   gnus-current-select-method
7200                 method)
7201               out))
7202       (nreverse out)))
7203    ;; One single select method.
7204    (t
7205     (list gnus-refer-article-method))))
7206
7207 (defun gnus-summary-edit-parameters ()
7208   "Edit the group parameters of the current group."
7209   (interactive)
7210   (gnus-group-edit-group gnus-newsgroup-name 'params))
7211
7212 (defun gnus-summary-customize-parameters ()
7213   "Customize the group parameters of the current group."
7214   (interactive)
7215   (gnus-group-customize gnus-newsgroup-name))
7216
7217 (defun gnus-summary-enter-digest-group (&optional force)
7218   "Enter an nndoc group based on the current article.
7219 If FORCE, force a digest interpretation.  If not, try
7220 to guess what the document format is."
7221   (interactive "P")
7222   (let ((conf gnus-current-window-configuration))
7223     (save-excursion
7224       (gnus-summary-select-article))
7225     (setq gnus-current-window-configuration conf)
7226     (let* ((name (format "%s-%d"
7227                          (gnus-group-prefixed-name
7228                           gnus-newsgroup-name (list 'nndoc ""))
7229                          (save-excursion
7230                            (set-buffer gnus-summary-buffer)
7231                            gnus-current-article)))
7232            (ogroup gnus-newsgroup-name)
7233            (params (append (gnus-info-params (gnus-get-info ogroup))
7234                            (list (cons 'to-group ogroup))
7235                            (list (cons 'save-article-group ogroup))))
7236            (case-fold-search t)
7237            (buf (current-buffer))
7238            dig to-address)
7239       (save-excursion
7240         (set-buffer gnus-original-article-buffer)
7241         ;; Have the digest group inherit the main mail address of
7242         ;; the parent article.
7243         (when (setq to-address (or (message-fetch-field "reply-to")
7244                                    (message-fetch-field "from")))
7245           (setq params (append
7246                         (list (cons 'to-address
7247                                     (funcall gnus-decode-encoded-word-function
7248                                              to-address))))))
7249         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7250         (insert-buffer-substring gnus-original-article-buffer)
7251         ;; Remove lines that may lead nndoc to misinterpret the
7252         ;; document type.
7253         (narrow-to-region
7254          (goto-char (point-min))
7255          (or (search-forward "\n\n" nil t) (point)))
7256         (goto-char (point-min))
7257         (delete-matching-lines "^Path:\\|^From ")
7258         (widen))
7259       (unwind-protect
7260           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7261                     (gnus-newsgroup-ephemeral-ignored-charsets
7262                      gnus-newsgroup-ignored-charsets))
7263                 (gnus-group-read-ephemeral-group
7264                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7265                               (nndoc-article-type
7266                                ,(if force 'mbox 'guess))) t))
7267               ;; Make all postings to this group go to the parent group.
7268               (nconc (gnus-info-params (gnus-get-info name))
7269                      params)
7270             ;; Couldn't select this doc group.
7271             (switch-to-buffer buf)
7272             (gnus-set-global-variables)
7273             (gnus-configure-windows 'summary)
7274             (gnus-message 3 "Article couldn't be entered?"))
7275         (kill-buffer dig)))))
7276
7277 (defun gnus-summary-read-document (n)
7278   "Open a new group based on the current article(s).
7279 This will allow you to read digests and other similar
7280 documents as newsgroups.
7281 Obeys the standard process/prefix convention."
7282   (interactive "P")
7283   (let* ((articles (gnus-summary-work-articles n))
7284          (ogroup gnus-newsgroup-name)
7285          (params (append (gnus-info-params (gnus-get-info ogroup))
7286                          (list (cons 'to-group ogroup))))
7287          article group egroup groups vgroup)
7288     (while (setq article (pop articles))
7289       (setq group (format "%s-%d" gnus-newsgroup-name article))
7290       (gnus-summary-remove-process-mark article)
7291       (when (gnus-summary-display-article article)
7292         (save-excursion
7293           (with-temp-buffer
7294             (insert-buffer-substring gnus-original-article-buffer)
7295             ;; Remove some headers that may lead nndoc to make
7296             ;; the wrong guess.
7297             (message-narrow-to-head)
7298             (goto-char (point-min))
7299             (delete-matching-lines "^\\(Path\\):\\|^From ")
7300             (widen)
7301             (if (setq egroup
7302                       (gnus-group-read-ephemeral-group
7303                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7304                                      (nndoc-article-type guess))
7305                        t nil t))
7306                 (progn
7307                   ;; Make all postings to this group go to the parent group.
7308                   (nconc (gnus-info-params (gnus-get-info egroup))
7309                          params)
7310                   (push egroup groups))
7311               ;; Couldn't select this doc group.
7312               (gnus-error 3 "Article couldn't be entered"))))))
7313     ;; Now we have selected all the documents.
7314     (cond
7315      ((not groups)
7316       (error "None of the articles could be interpreted as documents"))
7317      ((gnus-group-read-ephemeral-group
7318        (setq vgroup (format
7319                      "nnvirtual:%s-%s" gnus-newsgroup-name
7320                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7321        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7322        t
7323        (cons (current-buffer) 'summary)))
7324      (t
7325       (error "Couldn't select virtual nndoc group")))))
7326
7327 (defun gnus-summary-isearch-article (&optional regexp-p)
7328   "Do incremental search forward on the current article.
7329 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7330   (interactive "P")
7331   (let* ((gnus-inhibit-treatment t)
7332          (old (gnus-summary-select-article)))
7333     (gnus-configure-windows 'article)
7334     (gnus-eval-in-buffer-window gnus-article-buffer
7335       (save-restriction
7336         (widen)
7337         (when (eq 'old old)
7338           (gnus-article-show-all-headers))
7339         (goto-char (point-min))
7340         (isearch-forward regexp-p)))))
7341
7342 (defun gnus-summary-search-article-forward (regexp &optional backward)
7343   "Search for an article containing REGEXP forward.
7344 If BACKWARD, search backward instead."
7345   (interactive
7346    (list (read-string
7347           (format "Search article %s (regexp%s): "
7348                   (if current-prefix-arg "backward" "forward")
7349                   (if gnus-last-search-regexp
7350                       (concat ", default " gnus-last-search-regexp)
7351                     "")))
7352          current-prefix-arg))
7353   (if (string-equal regexp "")
7354       (setq regexp (or gnus-last-search-regexp ""))
7355     (setq gnus-last-search-regexp regexp)
7356     (setq gnus-article-before-search gnus-current-article))
7357   ;; Intentionally set gnus-last-article.
7358   (setq gnus-last-article gnus-article-before-search)
7359   (let ((gnus-last-article gnus-last-article))
7360     (if (gnus-summary-search-article regexp backward)
7361         (gnus-summary-show-thread)
7362       (error "Search failed: \"%s\"" regexp))))
7363
7364 (defun gnus-summary-search-article-backward (regexp)
7365   "Search for an article containing REGEXP backward."
7366   (interactive
7367    (list (read-string
7368           (format "Search article backward (regexp%s): "
7369                   (if gnus-last-search-regexp
7370                       (concat ", default " gnus-last-search-regexp)
7371                     "")))))
7372   (gnus-summary-search-article-forward regexp 'backward))
7373
7374 (eval-when-compile
7375   (defmacro gnus-summary-search-article-position-point (regexp backward)
7376     "Dehighlight the last matched text and goto the beginning position."
7377     (` (if (and gnus-summary-search-article-matched-data
7378                 (let ((text (caddr gnus-summary-search-article-matched-data))
7379                       (inhibit-read-only t)
7380                       buffer-read-only)
7381                   (delete-region
7382                    (goto-char (car gnus-summary-search-article-matched-data))
7383                    (cadr gnus-summary-search-article-matched-data))
7384                   (insert text)
7385                   (string-match (, regexp) text)))
7386            (if (, backward) (beginning-of-line) (end-of-line))
7387          (goto-char (if (, backward) (point-max) (point-min))))))
7388
7389   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7390     "Place point where X-Face image is displayed."
7391     (if (featurep 'xemacs)
7392         (` (let ((end (if (search-forward "\n\n" nil t)
7393                           (goto-char (1- (point)))
7394                         (point-min)))
7395                  extent)
7396              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7397              (unless (and (re-search-forward "^From:" end t)
7398                           (setq extent (extent-at (point)))
7399                           (extent-begin-glyph extent))
7400                (goto-char (, opoint)))))
7401       (` (let ((end (if (search-forward "\n\n" nil t)
7402                         (goto-char (1- (point)))
7403                       (point-min)))
7404                (start (or (search-backward "\n\n" nil t) (point-min))))
7405            (goto-char
7406             (or (text-property-any start end 'x-face-image t);; x-face-e21
7407                 (text-property-any start end 'x-face-mule-bitmap-image t)
7408                 (, opoint)))))))
7409
7410   (defmacro gnus-summary-search-article-highlight-matched-text
7411     (backward treated x-face)
7412     "Highlight matched text in the function `gnus-summary-search-article'."
7413     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7414              (end (set-marker (make-marker) (match-end 0)))
7415              (inhibit-read-only t)
7416              buffer-read-only)
7417          (unless treated
7418            (let ((,@
7419                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7420                     (mapcar
7421                      (lambda (item) (setq items (delq item items)))
7422                      '(gnus-treat-buttonize
7423                        gnus-treat-fill-article
7424                        gnus-treat-fill-long-lines
7425                        gnus-treat-emphasize
7426                        gnus-treat-highlight-headers
7427                        gnus-treat-highlight-citation
7428                        gnus-treat-highlight-signature
7429                        gnus-treat-overstrike
7430                        gnus-treat-display-xface
7431                        gnus-treat-buttonize-head
7432                        gnus-treat-decode-article-as-default-mime-charset))
7433                     (static-if (featurep 'xemacs)
7434                         items
7435                       (cons '(x-face-mule-delete-x-face-field
7436                               (quote never))
7437                             items))))
7438                  (gnus-treat-display-xface
7439                   (when (, x-face) gnus-treat-display-xface)))
7440              (gnus-article-prepare-mime-display)))
7441          (goto-char (if (, backward) start end))
7442          (when (, x-face)
7443            (gnus-summary-search-article-highlight-goto-x-face (point)))
7444          (setq gnus-summary-search-article-matched-data
7445                (list start end (buffer-substring start end)))
7446          (unless (eq start end);; matched text has been deleted. :-<
7447            (put-text-property start end 'face
7448                               (or (find-face 'isearch)
7449                                   'secondary-selection))))))
7450   )
7451
7452 (defun gnus-summary-search-article (regexp &optional backward)
7453   "Search for an article containing REGEXP.
7454 Optional argument BACKWARD means do search for backward.
7455 `gnus-select-article-hook' is not called during the search."
7456   ;; We have to require this here to make sure that the following
7457   ;; dynamic binding isn't shadowed by autoloading.
7458   (require 'gnus-async)
7459   (require 'gnus-art)
7460   (let ((gnus-select-article-hook nil)  ;Disable hook.
7461         (gnus-article-prepare-hook nil)
7462         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7463         (gnus-use-article-prefetch nil)
7464         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7465         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7466         (sum (current-buffer))
7467         (found nil)
7468         point treated)
7469     (gnus-save-hidden-threads
7470       (static-if (featurep 'xemacs)
7471           (let ((gnus-inhibit-treatment t))
7472             (setq treated (eq 'old (gnus-summary-select-article)))
7473             (when (and treated
7474                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7475                                  (window-live-p (get-buffer-window
7476                                                  gnus-article-buffer t)))))
7477               (gnus-summary-select-article nil t)
7478               (setq treated nil)))
7479         (let ((gnus-inhibit-treatment t)
7480               (x-face-mule-delete-x-face-field 'never))
7481           (setq treated (eq 'old (gnus-summary-select-article)))
7482           (when (and treated
7483                      (not
7484                       (and (gnus-buffer-live-p gnus-article-buffer)
7485                            (window-live-p (get-buffer-window
7486                                            gnus-article-buffer t))
7487                            (or (not (string-match "^\\^X-Face:" regexp))
7488                                (with-current-buffer gnus-article-buffer
7489                                  gnus-summary-search-article-matched-data)))))
7490             (gnus-summary-select-article nil t)
7491             (setq treated nil))))
7492       (set-buffer gnus-article-buffer)
7493       (widen)
7494       (if treated
7495           (progn
7496             (gnus-article-show-all-headers)
7497             (gnus-summary-search-article-position-point regexp backward))
7498         (goto-char (if backward (point-max) (point-min))))
7499       (while (not found)
7500         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7501         (if (if backward
7502                 (re-search-backward regexp nil t)
7503               (re-search-forward regexp nil t))
7504             ;; We found the regexp.
7505             (progn
7506               (gnus-summary-search-article-highlight-matched-text
7507                backward treated (string-match "^\\^X-Face:" regexp))
7508               (setq found 'found)
7509               (forward-line
7510                (/ (- 2 (window-height
7511                         (get-buffer-window gnus-article-buffer t)))
7512                   2))
7513               (set-window-start
7514                (get-buffer-window (current-buffer))
7515                (point))
7516               (set-buffer sum)
7517               (setq point (point)))
7518           ;; We didn't find it, so we go to the next article.
7519           (set-buffer sum)
7520           (setq found 'not)
7521           (while (eq found 'not)
7522             (if (not (if backward (gnus-summary-find-prev)
7523                        (gnus-summary-find-next)))
7524                 ;; No more articles.
7525                 (setq found t)
7526               ;; Select the next article and adjust point.
7527               (unless (gnus-summary-article-sparse-p
7528                        (gnus-summary-article-number))
7529                 (setq found nil)
7530                 (let ((gnus-inhibit-treatment t))
7531                   (gnus-summary-select-article))
7532                 (setq treated nil)
7533                 (set-buffer gnus-article-buffer)
7534                 (widen)
7535                 (goto-char (if backward (point-max) (point-min))))))))
7536       (gnus-message 7 ""))
7537     ;; Return whether we found the regexp.
7538     (when (eq found 'found)
7539       (goto-char point)
7540       (gnus-summary-show-thread)
7541       (gnus-summary-goto-subject gnus-current-article)
7542       (gnus-summary-position-point)
7543       t)))
7544
7545 (defun gnus-summary-find-matching (header regexp &optional backward unread
7546                                           not-case-fold)
7547   "Return a list of all articles that match REGEXP on HEADER.
7548 The search stars on the current article and goes forwards unless
7549 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7550 If UNREAD is non-nil, only unread articles will
7551 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7552 in the comparisons."
7553   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7554                 (gnus-data-find-list
7555                  (gnus-summary-article-number) (gnus-data-list backward))))
7556         (case-fold-search (not not-case-fold))
7557         articles d func)
7558     (if (consp header)
7559         (if (eq (car header) 'extra)
7560             (setq func
7561                   `(lambda (h)
7562                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7563                          "")))
7564           (error "%s is an invalid header" header))
7565       (unless (fboundp (intern (concat "mail-header-" header)))
7566         (error "%s is not a valid header" header))
7567       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7568     (while data
7569       (setq d (car data))
7570       (and (or (not unread)             ; We want all articles...
7571                (gnus-data-unread-p d))  ; Or just unreads.
7572            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7573            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7574            (push (gnus-data-number d) articles)) ; Success!
7575       (setq data (cdr data)))
7576     (nreverse articles)))
7577
7578 (defun gnus-summary-execute-command (header regexp command &optional backward)
7579   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7580 If HEADER is an empty string (or nil), the match is done on the entire
7581 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7582   (interactive
7583    (list (let ((completion-ignore-case t))
7584            (completing-read
7585             "Header name: "
7586             (mapcar (lambda (string) (list string))
7587                     '("Number" "Subject" "From" "Lines" "Date"
7588                       "Message-ID" "Xref" "References" "Body"))
7589             nil 'require-match))
7590          (read-string "Regexp: ")
7591          (read-key-sequence "Command: ")
7592          current-prefix-arg))
7593   (when (equal header "Body")
7594     (setq header ""))
7595   ;; Hidden thread subtrees must be searched as well.
7596   (gnus-summary-show-all-threads)
7597   ;; We don't want to change current point nor window configuration.
7598   (save-excursion
7599     (save-window-excursion
7600       (gnus-message 6 "Executing %s..." (key-description command))
7601       ;; We'd like to execute COMMAND interactively so as to give arguments.
7602       (gnus-execute header regexp
7603                     `(call-interactively ',(key-binding command))
7604                     backward)
7605       (gnus-message 6 "Executing %s...done" (key-description command)))))
7606
7607 (defun gnus-summary-beginning-of-article ()
7608   "Scroll the article back to the beginning."
7609   (interactive)
7610   (gnus-summary-select-article)
7611   (gnus-configure-windows 'article)
7612   (gnus-eval-in-buffer-window gnus-article-buffer
7613     (widen)
7614     (goto-char (point-min))
7615     (when gnus-page-broken
7616       (gnus-narrow-to-page))))
7617
7618 (defun gnus-summary-end-of-article ()
7619   "Scroll to the end of the article."
7620   (interactive)
7621   (gnus-summary-select-article)
7622   (gnus-configure-windows 'article)
7623   (gnus-eval-in-buffer-window gnus-article-buffer
7624     (widen)
7625     (goto-char (point-max))
7626     (recenter -3)
7627     (when gnus-page-broken
7628       (gnus-narrow-to-page))))
7629
7630 (defun gnus-summary-print-article (&optional filename n)
7631   "Generate and print a PostScript image of the N next (mail) articles.
7632
7633 If N is negative, print the N previous articles.  If N is nil and articles
7634 have been marked with the process mark, print these instead.
7635
7636 If the optional first argument FILENAME is nil, send the image to the
7637 printer.  If FILENAME is a string, save the PostScript image in a file with
7638 that name.  If FILENAME is a number, prompt the user for the name of the file
7639 to save in."
7640   (interactive (list (ps-print-preprint current-prefix-arg)))
7641   (dolist (article (gnus-summary-work-articles n))
7642     (gnus-summary-select-article nil nil 'pseudo article)
7643     (gnus-eval-in-buffer-window gnus-article-buffer
7644       (let ((buffer (generate-new-buffer " *print*")))
7645         (unwind-protect
7646             (progn
7647               (copy-to-buffer buffer (point-min) (point-max))
7648               (set-buffer buffer)
7649               (gnus-article-delete-invisible-text)
7650               (when (gnus-visual-p 'article-highlight 'highlight)
7651                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7652                 ;; highlight.
7653                 (let ((gnus-article-buffer buffer))
7654                   (gnus-article-highlight-citation t)
7655                   (gnus-article-highlight-signature)))
7656               (let ((ps-left-header
7657                      (list
7658                       (concat "("
7659                               (mail-header-subject gnus-current-headers) ")")
7660                       (concat "("
7661                               (mail-header-from gnus-current-headers) ")")))
7662                     (ps-right-header
7663                      (list
7664                       "/pagenumberstring load"
7665                       (concat "("
7666                               (mail-header-date gnus-current-headers) ")"))))
7667                 (gnus-run-hooks 'gnus-ps-print-hook)
7668                 (save-excursion
7669                   (ps-spool-buffer-with-faces))))
7670           (kill-buffer buffer))))
7671     (gnus-summary-remove-process-mark article))
7672   (ps-despool filename))
7673
7674 (defun gnus-summary-show-article (&optional arg)
7675   "Force re-fetching of the current article.
7676 If ARG (the prefix) is a number, show the article with the charset
7677 defined in `gnus-summary-show-article-charset-alist', or the charset
7678 inputed.
7679 If ARG (the prefix) is non-nil and not a number, show the raw article
7680 without any article massaging functions being run."
7681   (interactive "P")
7682   (cond
7683    ((numberp arg)
7684     (let ((gnus-newsgroup-charset
7685            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7686                (read-coding-system "Charset: ")))
7687           (gnus-newsgroup-ignored-charsets 'gnus-all))
7688       (gnus-summary-select-article nil 'force)
7689       (let ((deps gnus-newsgroup-dependencies)
7690             head header)
7691         (save-excursion
7692           (set-buffer gnus-original-article-buffer)
7693           (save-restriction
7694             (message-narrow-to-head)
7695             (setq head (buffer-string)))
7696           (with-temp-buffer
7697             (insert (format "211 %d Article retrieved.\n"
7698                             (cdr gnus-article-current)))
7699             (insert head)
7700             (insert ".\n")
7701             (let ((nntp-server-buffer (current-buffer)))
7702               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7703         (gnus-data-set-header
7704          (gnus-data-find (cdr gnus-article-current))
7705          header)
7706         (gnus-summary-update-article-line
7707          (cdr gnus-article-current) header))))
7708    ((not arg)
7709     ;; Select the article the normal way.
7710     (gnus-summary-select-article nil 'force))
7711    (t
7712     ;; We have to require this here to make sure that the following
7713     ;; dynamic binding isn't shadowed by autoloading.
7714     (require 'gnus-async)
7715     (require 'gnus-art)
7716     ;; Bind the article treatment functions to nil.
7717     (let ((gnus-have-all-headers t)
7718           gnus-article-prepare-hook
7719           gnus-article-decode-hook
7720           gnus-break-pages
7721           gnus-show-mime)
7722       (gnus-summary-select-article nil 'force))))
7723   (gnus-summary-goto-subject gnus-current-article)
7724   (gnus-summary-position-point))
7725
7726 (defun gnus-summary-verbose-headers (&optional arg)
7727   "Toggle permanent full header display.
7728 If ARG is a positive number, turn header display on.
7729 If ARG is a negative number, turn header display off."
7730   (interactive "P")
7731   (setq gnus-show-all-headers
7732         (cond ((or (not (numberp arg))
7733                    (zerop arg))
7734                (not gnus-show-all-headers))
7735               ((natnump arg)
7736                t)))
7737   (gnus-summary-show-article))
7738
7739 (defun gnus-summary-toggle-header (&optional arg)
7740   "Show the headers if they are hidden, or hide them if they are shown.
7741 If ARG is a positive number, show the entire header.
7742 If ARG is a negative number, hide the unwanted header lines."
7743   (interactive "P")
7744   (save-excursion
7745     (set-buffer gnus-article-buffer)
7746     (save-restriction
7747       (let* ((buffer-read-only nil)
7748              (inhibit-point-motion-hooks t)
7749              hidden e)
7750         (setq hidden
7751               (if (numberp arg)
7752                   (>= arg 0)
7753                 (save-restriction
7754                   (article-narrow-to-head)
7755                   (gnus-article-hidden-text-p 'headers))))
7756         (goto-char (point-min))
7757         (when (search-forward "\n\n" nil t)
7758           (delete-region (point-min) (1- (point))))
7759         (goto-char (point-min))
7760         (save-excursion
7761           (set-buffer gnus-original-article-buffer)
7762           (goto-char (point-min))
7763           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7764         (insert-buffer-substring gnus-original-article-buffer 1 e)
7765         (save-restriction
7766           (narrow-to-region (point-min) (point))
7767           (article-decode-encoded-words)
7768           (if  hidden
7769               (let ((gnus-treat-hide-headers nil)
7770                     (gnus-treat-hide-boring-headers nil))
7771                 (setq gnus-article-wash-types
7772                       (delq 'headers gnus-article-wash-types))
7773                 (gnus-treat-article 'head))
7774             (gnus-treat-article 'head)))
7775         (gnus-set-mode-line 'article)))))
7776
7777 (defun gnus-summary-show-all-headers ()
7778   "Make all header lines visible."
7779   (interactive)
7780   (gnus-article-show-all-headers))
7781
7782 (defun gnus-summary-toggle-mime (&optional arg)
7783   "Toggle MIME processing.
7784 If ARG is a positive number, turn MIME processing on."
7785   (interactive "P")
7786   (setq gnus-show-mime
7787         (if (null arg)
7788             (not gnus-show-mime)
7789           (> (prefix-numeric-value arg) 0)))
7790   (gnus-summary-select-article t 'force))
7791
7792 (defun gnus-summary-caesar-message (&optional arg)
7793   "Caesar rotate the current article by 13.
7794 The numerical prefix specifies how many places to rotate each letter
7795 forward."
7796   (interactive "P")
7797   (gnus-summary-select-article)
7798   (let ((mail-header-separator ""))
7799     (gnus-eval-in-buffer-window gnus-article-buffer
7800       (save-restriction
7801         (widen)
7802         (let ((start (window-start))
7803               buffer-read-only)
7804           (message-caesar-buffer-body arg)
7805           (set-window-start (get-buffer-window (current-buffer)) start))))))
7806
7807 (defun gnus-summary-stop-page-breaking ()
7808   "Stop page breaking in the current article."
7809   (interactive)
7810   (gnus-summary-select-article)
7811   (gnus-eval-in-buffer-window gnus-article-buffer
7812     (widen)
7813     (when (gnus-visual-p 'page-marker)
7814       (let ((buffer-read-only nil))
7815         (gnus-remove-text-with-property 'gnus-prev)
7816         (gnus-remove-text-with-property 'gnus-next))
7817       (setq gnus-page-broken nil))))
7818
7819 (defun gnus-summary-move-article (&optional n to-newsgroup
7820                                             select-method action)
7821   "Move the current article to a different newsgroup.
7822 If N is a positive number, move the N next articles.
7823 If N is a negative number, move the N previous articles.
7824 If N is nil and any articles have been marked with the process mark,
7825 move those articles instead.
7826 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7827 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7828 re-spool using this method.
7829
7830 For this function to work, both the current newsgroup and the
7831 newsgroup that you want to move to have to support the `request-move'
7832 and `request-accept' functions.
7833
7834 ACTION can be either `move' (the default), `crosspost' or `copy'."
7835   (interactive "P")
7836   (unless action
7837     (setq action 'move))
7838   ;; Disable marking as read.
7839   (let (gnus-mark-article-hook)
7840     (save-window-excursion
7841       (gnus-summary-select-article)))
7842   ;; Check whether the source group supports the required functions.
7843   (cond ((and (eq action 'move)
7844               (not (gnus-check-backend-function
7845                     'request-move-article gnus-newsgroup-name)))
7846          (error "The current group does not support article moving"))
7847         ((and (eq action 'crosspost)
7848               (not (gnus-check-backend-function
7849                     'request-replace-article gnus-newsgroup-name)))
7850          (error "The current group does not support article editing")))
7851   (let ((articles (gnus-summary-work-articles n))
7852         (prefix (if (gnus-check-backend-function
7853                     'request-move-article gnus-newsgroup-name)
7854                     (gnus-group-real-prefix gnus-newsgroup-name)
7855                   ""))
7856         (names '((move "Move" "Moving")
7857                  (copy "Copy" "Copying")
7858                  (crosspost "Crosspost" "Crossposting")))
7859         (copy-buf (save-excursion
7860                     (nnheader-set-temp-buffer " *copy article*")))
7861         (default-marks gnus-article-mark-lists)
7862         (no-expire-marks (delete '(expirable . expire)
7863                                  (copy-sequence gnus-article-mark-lists)))
7864         art-group to-method new-xref article to-groups)
7865     (unless (assq action names)
7866       (error "Unknown action %s" action))
7867     ;; Read the newsgroup name.
7868     (when (and (not to-newsgroup)
7869                (not select-method))
7870       (setq to-newsgroup
7871             (gnus-read-move-group-name
7872              (cadr (assq action names))
7873              (symbol-value (intern (format "gnus-current-%s-group" action)))
7874              articles prefix))
7875       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7876     (setq to-method (or select-method
7877                         (gnus-server-to-method
7878                          (gnus-group-method to-newsgroup))))
7879     ;; Check the method we are to move this article to...
7880     (unless (gnus-check-backend-function
7881              'request-accept-article (car to-method))
7882       (error "%s does not support article copying" (car to-method)))
7883     (unless (gnus-check-server to-method)
7884       (error "Can't open server %s" (car to-method)))
7885     (gnus-message 6 "%s to %s: %s..."
7886                   (caddr (assq action names))
7887                   (or (car select-method) to-newsgroup) articles)
7888     (while articles
7889       (setq article (pop articles))
7890       (setq
7891        art-group
7892        (cond
7893         ;; Move the article.
7894         ((eq action 'move)
7895          ;; Remove this article from future suppression.
7896          (gnus-dup-unsuppress-article article)
7897          (gnus-request-move-article
7898           article                       ; Article to move
7899           gnus-newsgroup-name           ; From newsgroup
7900           (nth 1 (gnus-find-method-for-group
7901                   gnus-newsgroup-name)) ; Server
7902           (list 'gnus-request-accept-article
7903                 to-newsgroup (list 'quote select-method)
7904                 (not articles) t)       ; Accept form
7905           (not articles)))              ; Only save nov last time
7906         ;; Copy the article.
7907         ((eq action 'copy)
7908          (save-excursion
7909            (set-buffer copy-buf)
7910            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7911              (gnus-request-accept-article
7912               to-newsgroup select-method (not articles) t))))
7913         ;; Crosspost the article.
7914         ((eq action 'crosspost)
7915          (let ((xref (message-tokenize-header
7916                       (mail-header-xref (gnus-summary-article-header article))
7917                       " ")))
7918            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7919                                   ":" article))
7920            (unless xref
7921              (setq xref (list (system-name))))
7922            (setq new-xref
7923                  (concat
7924                   (mapconcat 'identity
7925                              (delete "Xref:" (delete new-xref xref))
7926                              " ")
7927                   " " new-xref))
7928            (save-excursion
7929              (set-buffer copy-buf)
7930              ;; First put the article in the destination group.
7931              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7932              (when (consp (setq art-group
7933                                 (gnus-request-accept-article
7934                                  to-newsgroup select-method (not articles))))
7935                (setq new-xref (concat new-xref " " (car art-group)
7936                                       ":" (cdr art-group)))
7937                ;; Now we have the new Xrefs header, so we insert
7938                ;; it and replace the new article.
7939                (nnheader-replace-header "Xref" new-xref)
7940                (gnus-request-replace-article
7941                 (cdr art-group) to-newsgroup (current-buffer))
7942                art-group))))))
7943       (cond
7944        ((not art-group)
7945         (gnus-message 1 "Couldn't %s article %s: %s"
7946                       (cadr (assq action names)) article
7947                       (nnheader-get-report (car to-method))))
7948        ((eq art-group 'junk)
7949         (when (eq action 'move)
7950           (gnus-summary-mark-article article gnus-canceled-mark)
7951           (gnus-message 4 "Deleted article %s" article)))
7952        (t
7953         (let* ((pto-group (gnus-group-prefixed-name
7954                            (car art-group) to-method))
7955                (entry
7956                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7957                (info (nth 2 entry))
7958                (to-group (gnus-info-group info))
7959                to-marks)
7960           ;; Update the group that has been moved to.
7961           (when (and info
7962                      (memq action '(move copy)))
7963             (unless (member to-group to-groups)
7964               (push to-group to-groups))
7965
7966             (unless (memq article gnus-newsgroup-unreads)
7967               (push 'read to-marks)
7968               (gnus-info-set-read
7969                info (gnus-add-to-range (gnus-info-read info)
7970                                        (list (cdr art-group)))))
7971
7972             ;; See whether the article is to be put in the cache.
7973             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7974                              default-marks
7975                            no-expire-marks))
7976                   (to-article (cdr art-group)))
7977
7978               ;; Enter the article into the cache in the new group,
7979               ;; if that is required.
7980               (when gnus-use-cache
7981                 (gnus-cache-possibly-enter-article
7982                  to-group to-article
7983                  (let ((header (copy-sequence
7984                                 (gnus-summary-article-header article))))
7985                    (mail-header-set-number header to-article)
7986                    header)
7987                  (memq article gnus-newsgroup-marked)
7988                  (memq article gnus-newsgroup-dormant)
7989                  (memq article gnus-newsgroup-unreads)))
7990
7991               (when gnus-preserve-marks
7992                 ;; Copy any marks over to the new group.
7993                 (when (and (equal to-group gnus-newsgroup-name)
7994                            (not (memq article gnus-newsgroup-unreads)))
7995                   ;; Mark this article as read in this group.
7996                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7997                   (setcdr (gnus-active to-group) to-article)
7998                   (setcdr gnus-newsgroup-active to-article))
7999
8000                 (while marks
8001                   (when (memq article (symbol-value
8002                                        (intern (format "gnus-newsgroup-%s"
8003                                                        (caar marks)))))
8004                     (push (cdar marks) to-marks)
8005                     ;; If the other group is the same as this group,
8006                     ;; then we have to add the mark to the list.
8007                     (when (equal to-group gnus-newsgroup-name)
8008                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8009                            (cons to-article
8010                                  (symbol-value
8011                                   (intern (format "gnus-newsgroup-%s"
8012                                                   (caar marks)))))))
8013                     ;; Copy the marks to other group.
8014                     (gnus-add-marked-articles
8015                      to-group (cdar marks) (list to-article) info))
8016                   (setq marks (cdr marks)))
8017
8018                 (gnus-request-set-mark to-group (list (list (list to-article)
8019                                                             'set
8020                                                             to-marks))))
8021
8022               (gnus-dribble-enter
8023                (concat "(gnus-group-set-info '"
8024                        (gnus-prin1-to-string (gnus-get-info to-group))
8025                        ")"))))
8026
8027           ;; Update the Xref header in this article to point to
8028           ;; the new crossposted article we have just created.
8029           (when (eq action 'crosspost)
8030             (save-excursion
8031               (set-buffer copy-buf)
8032               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8033               (nnheader-replace-header "Xref" new-xref)
8034               (gnus-request-replace-article
8035                article gnus-newsgroup-name (current-buffer)))))
8036
8037         ;;;!!!Why is this necessary?
8038         (set-buffer gnus-summary-buffer)
8039
8040         (gnus-summary-goto-subject article)
8041         (when (eq action 'move)
8042           (gnus-summary-mark-article article gnus-canceled-mark))))
8043       (gnus-summary-remove-process-mark article))
8044     ;; Re-activate all groups that have been moved to.
8045     (while to-groups
8046       (save-excursion
8047         (set-buffer gnus-group-buffer)
8048         (when (gnus-group-goto-group (car to-groups) t)
8049           (gnus-group-get-new-news-this-group 1 t))
8050         (pop to-groups)))
8051
8052     (gnus-kill-buffer copy-buf)
8053     (gnus-summary-position-point)
8054     (gnus-set-mode-line 'summary)))
8055
8056 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8057   "Move the current article to a different newsgroup.
8058 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8059 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8060 re-spool using this method."
8061   (interactive "P")
8062   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8063
8064 (defun gnus-summary-crosspost-article (&optional n)
8065   "Crosspost the current article to some other group."
8066   (interactive "P")
8067   (gnus-summary-move-article n nil nil 'crosspost))
8068
8069 (defcustom gnus-summary-respool-default-method nil
8070   "Default method for respooling an article.
8071 If nil, use to the current newsgroup method."
8072   :type '(choice (gnus-select-method :value (nnml ""))
8073                  (const nil))
8074   :group 'gnus-summary-mail)
8075
8076 (defun gnus-summary-respool-article (&optional n method)
8077   "Respool the current article.
8078 The article will be squeezed through the mail spooling process again,
8079 which means that it will be put in some mail newsgroup or other
8080 depending on `nnmail-split-methods'.
8081 If N is a positive number, respool the N next articles.
8082 If N is a negative number, respool the N previous articles.
8083 If N is nil and any articles have been marked with the process mark,
8084 respool those articles instead.
8085
8086 Respooling can be done both from mail groups and \"real\" newsgroups.
8087 In the former case, the articles in question will be moved from the
8088 current group into whatever groups they are destined to.  In the
8089 latter case, they will be copied into the relevant groups."
8090   (interactive
8091    (list current-prefix-arg
8092          (let* ((methods (gnus-methods-using 'respool))
8093                 (methname
8094                  (symbol-name (or gnus-summary-respool-default-method
8095                                   (car (gnus-find-method-for-group
8096                                         gnus-newsgroup-name)))))
8097                 (method
8098                  (gnus-completing-read
8099                   methname "What backend do you want to use when respooling?"
8100                   methods nil t nil 'gnus-mail-method-history))
8101                 ms)
8102            (cond
8103             ((zerop (length (setq ms (gnus-servers-using-backend
8104                                       (intern method)))))
8105              (list (intern method) ""))
8106             ((= 1 (length ms))
8107              (car ms))
8108             (t
8109              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8110                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8111                            ms-alist))))))))
8112   (unless method
8113     (error "No method given for respooling"))
8114   (if (assoc (symbol-name
8115               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8116              (gnus-methods-using 'respool))
8117       (gnus-summary-move-article n nil method)
8118     (gnus-summary-copy-article n nil method)))
8119
8120 (defun gnus-summary-import-article (file)
8121   "Import an arbitrary file into a mail newsgroup."
8122   (interactive "fImport file: ")
8123   (let ((group gnus-newsgroup-name)
8124         (now (current-time))
8125         atts lines)
8126     (unless (gnus-check-backend-function 'request-accept-article group)
8127       (error "%s does not support article importing" group))
8128     (or (file-readable-p file)
8129         (not (file-regular-p file))
8130         (error "Can't read %s" file))
8131     (save-excursion
8132       (set-buffer (gnus-get-buffer-create " *import file*"))
8133       (erase-buffer)
8134       (nnheader-insert-file-contents file)
8135       (goto-char (point-min))
8136       (unless (nnheader-article-p)
8137         ;; This doesn't look like an article, so we fudge some headers.
8138         (setq atts (file-attributes file)
8139               lines (count-lines (point-min) (point-max)))
8140         (insert "From: " (read-string "From: ") "\n"
8141                 "Subject: " (read-string "Subject: ") "\n"
8142                 "Date: " (message-make-date (nth 5 atts))
8143                 "\n"
8144                 "Message-ID: " (message-make-message-id) "\n"
8145                 "Lines: " (int-to-string lines) "\n"
8146                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8147       (gnus-request-accept-article group nil t)
8148       (kill-buffer (current-buffer)))))
8149
8150 (defun gnus-summary-article-posted-p ()
8151   "Say whether the current (mail) article is available from news as well.
8152 This will be the case if the article has both been mailed and posted."
8153   (interactive)
8154   (let ((id (mail-header-references (gnus-summary-article-header)))
8155         (gnus-override-method (car (gnus-refer-article-methods))))
8156     (if (gnus-request-head id "")
8157         (gnus-message 2 "The current message was found on %s"
8158                       gnus-override-method)
8159       (gnus-message 2 "The current message couldn't be found on %s"
8160                     gnus-override-method)
8161       nil)))
8162
8163 (defun gnus-summary-expire-articles (&optional now)
8164   "Expire all articles that are marked as expirable in the current group."
8165   (interactive)
8166   (when (gnus-check-backend-function
8167          'request-expire-articles gnus-newsgroup-name)
8168     ;; This backend supports expiry.
8169     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8170            (expirable (if total
8171                           (progn
8172                             ;; We need to update the info for
8173                             ;; this group for `gnus-list-of-read-articles'
8174                             ;; to give us the right answer.
8175                             (gnus-run-hooks 'gnus-exit-group-hook)
8176                             (gnus-summary-update-info)
8177                             (gnus-list-of-read-articles gnus-newsgroup-name))
8178                         (setq gnus-newsgroup-expirable
8179                               (sort gnus-newsgroup-expirable '<))))
8180            (expiry-wait (if now 'immediate
8181                           (gnus-group-find-parameter
8182                            gnus-newsgroup-name 'expiry-wait)))
8183            (nnmail-expiry-target
8184             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8185                 nnmail-expiry-target))
8186            es)
8187       (when expirable
8188         ;; There are expirable articles in this group, so we run them
8189         ;; through the expiry process.
8190         (gnus-message 6 "Expiring articles...")
8191         (unless (gnus-check-group gnus-newsgroup-name)
8192           (error "Can't open server for %s" gnus-newsgroup-name))
8193         ;; The list of articles that weren't expired is returned.
8194         (save-excursion
8195           (if expiry-wait
8196               (let ((nnmail-expiry-wait-function nil)
8197                     (nnmail-expiry-wait expiry-wait))
8198                 (setq es (gnus-request-expire-articles
8199                           expirable gnus-newsgroup-name)))
8200             (setq es (gnus-request-expire-articles
8201                       expirable gnus-newsgroup-name)))
8202           (unless total
8203             (setq gnus-newsgroup-expirable es))
8204           ;; We go through the old list of expirable, and mark all
8205           ;; really expired articles as nonexistent.
8206           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8207             (let ((gnus-use-cache nil))
8208               (while expirable
8209                 (unless (memq (car expirable) es)
8210                   (when (gnus-data-find (car expirable))
8211                     (gnus-summary-mark-article
8212                      (car expirable) gnus-canceled-mark)))
8213                 (setq expirable (cdr expirable))))))
8214         (gnus-message 6 "Expiring articles...done")))))
8215
8216 (defun gnus-summary-expire-articles-now ()
8217   "Expunge all expirable articles in the current group.
8218 This means that *all* articles that are marked as expirable will be
8219 deleted forever, right now."
8220   (interactive)
8221   (or gnus-expert-user
8222       (gnus-yes-or-no-p
8223        "Are you really, really, really sure you want to delete all these messages? ")
8224       (error "Phew!"))
8225   (gnus-summary-expire-articles t))
8226
8227 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8228 (defun gnus-summary-delete-article (&optional n)
8229   "Delete the N next (mail) articles.
8230 This command actually deletes articles.  This is not a marking
8231 command.  The article will disappear forever from your life, never to
8232 return.
8233 If N is negative, delete backwards.
8234 If N is nil and articles have been marked with the process mark,
8235 delete these instead."
8236   (interactive "P")
8237   (unless (gnus-check-backend-function 'request-expire-articles
8238                                        gnus-newsgroup-name)
8239     (error "The current newsgroup does not support article deletion"))
8240   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8241     (error "Couldn't open server"))
8242   ;; Compute the list of articles to delete.
8243   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8244         not-deleted)
8245     (if (and gnus-novice-user
8246              (not (gnus-yes-or-no-p
8247                    (format "Do you really want to delete %s forever? "
8248                            (if (> (length articles) 1)
8249                                (format "these %s articles" (length articles))
8250                              "this article")))))
8251         ()
8252       ;; Delete the articles.
8253       (setq not-deleted (gnus-request-expire-articles
8254                          articles gnus-newsgroup-name 'force))
8255       (while articles
8256         (gnus-summary-remove-process-mark (car articles))
8257         ;; The backend might not have been able to delete the article
8258         ;; after all.
8259         (unless (memq (car articles) not-deleted)
8260           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8261         (setq articles (cdr articles)))
8262       (when not-deleted
8263         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8264     (gnus-summary-position-point)
8265     (gnus-set-mode-line 'summary)
8266     not-deleted))
8267
8268 (defun gnus-summary-edit-article (&optional force)
8269   "Edit the current article.
8270 This will have permanent effect only in mail groups.
8271 If FORCE is non-nil, allow editing of articles even in read-only
8272 groups."
8273   (interactive "P")
8274   (save-excursion
8275     (set-buffer gnus-summary-buffer)
8276     (let ((mail-parse-charset gnus-newsgroup-charset)
8277           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8278       (gnus-set-global-variables)
8279       (when (and (not force)
8280                  (gnus-group-read-only-p))
8281         (error "The current newsgroup does not support article editing"))
8282       (gnus-summary-show-article t)
8283       (gnus-article-edit-article
8284        'ignore
8285        `(lambda (no-highlight)
8286           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8287                 (message-options message-options)
8288                 (message-options-set-recipient)
8289                 (mail-parse-ignored-charsets
8290                  ',gnus-newsgroup-ignored-charsets))
8291             (gnus-summary-edit-article-done
8292              ,(or (mail-header-references gnus-current-headers) "")
8293              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8294
8295 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8296
8297 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8298                                                  no-highlight)
8299   "Make edits to the current article permanent."
8300   (interactive)
8301   (save-excursion
8302     ;; The buffer restriction contains the entire article if it exists.
8303     (when (article-goto-body)
8304       (let ((lines (count-lines (point) (point-max)))
8305             (length (- (point-max) (point)))
8306             (case-fold-search t)
8307             (body (copy-marker (point))))
8308         (goto-char (point-min))
8309         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8310           (delete-region (match-beginning 1) (match-end 1))
8311           (insert (number-to-string length)))
8312         (goto-char (point-min))
8313         (when (re-search-forward
8314                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8315           (delete-region (match-beginning 1) (match-end 1))
8316           (insert (number-to-string length)))
8317         (goto-char (point-min))
8318         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8319           (delete-region (match-beginning 1) (match-end 1))
8320           (insert (number-to-string lines))))))
8321   ;; Replace the article.
8322   (let ((buf (current-buffer)))
8323     (with-temp-buffer
8324       (insert-buffer-substring buf)
8325
8326       (if (and (not read-only)
8327                (not (gnus-request-replace-article
8328                      (cdr gnus-article-current) (car gnus-article-current)
8329                      (current-buffer) t)))
8330           (error "Couldn't replace article")
8331         ;; Update the summary buffer.
8332         (if (and references
8333                  (equal (message-tokenize-header references " ")
8334                         (message-tokenize-header
8335                          (or (message-fetch-field "references") "") " ")))
8336             ;; We only have to update this line.
8337             (save-excursion
8338               (save-restriction
8339                 (message-narrow-to-head)
8340                 (let ((head (buffer-string))
8341                       header)
8342                   (with-temp-buffer
8343                     (insert (format "211 %d Article retrieved.\n"
8344                                     (cdr gnus-article-current)))
8345                     (insert head)
8346                     (insert ".\n")
8347                     (let ((nntp-server-buffer (current-buffer)))
8348                       (setq header (car (gnus-get-newsgroup-headers
8349                                          (save-excursion
8350                                            (set-buffer gnus-summary-buffer)
8351                                            gnus-newsgroup-dependencies)
8352                                          t))))
8353                     (save-excursion
8354                       (set-buffer gnus-summary-buffer)
8355                       (gnus-data-set-header
8356                        (gnus-data-find (cdr gnus-article-current))
8357                        header)
8358                       (gnus-summary-update-article-line
8359                        (cdr gnus-article-current) header))))))
8360           ;; Update threads.
8361           (set-buffer (or buffer gnus-summary-buffer))
8362           (gnus-summary-update-article (cdr gnus-article-current)))
8363         ;; Prettify the article buffer again.
8364         (unless no-highlight
8365           (save-excursion
8366             (set-buffer gnus-article-buffer)
8367             ;;;!!! Fix this -- article should be rehighlighted.
8368             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8369             (set-buffer gnus-original-article-buffer)
8370             (gnus-request-article
8371              (cdr gnus-article-current)
8372              (car gnus-article-current) (current-buffer))))
8373         ;; Prettify the summary buffer line.
8374         (when (gnus-visual-p 'summary-highlight 'highlight)
8375           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8376
8377 (defun gnus-summary-edit-wash (key)
8378   "Perform editing command KEY in the article buffer."
8379   (interactive
8380    (list
8381     (progn
8382       (message "%s" (concat (this-command-keys) "- "))
8383       (read-char))))
8384   (message "")
8385   (gnus-summary-edit-article)
8386   (execute-kbd-macro (concat (this-command-keys) key))
8387   (gnus-article-edit-done))
8388
8389 ;;; Respooling
8390
8391 (defun gnus-summary-respool-query (&optional silent trace)
8392   "Query where the respool algorithm would put this article."
8393   (interactive)
8394   (let (gnus-mark-article-hook)
8395     (gnus-summary-select-article)
8396     (save-excursion
8397       (set-buffer gnus-original-article-buffer)
8398       (save-restriction
8399         (message-narrow-to-head)
8400         (let ((groups (nnmail-article-group 'identity trace)))
8401           (unless silent
8402             (if groups
8403                 (message "This message would go to %s"
8404                          (mapconcat 'car groups ", "))
8405               (message "This message would go to no groups"))
8406             groups))))))
8407
8408 (defun gnus-summary-respool-trace ()
8409   "Trace where the respool algorithm would put this article.
8410 Display a buffer showing all fancy splitting patterns which matched."
8411   (interactive)
8412   (gnus-summary-respool-query nil t))
8413
8414 ;; Summary marking commands.
8415
8416 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8417   "Mark articles which has the same subject as read, and then select the next.
8418 If UNMARK is positive, remove any kind of mark.
8419 If UNMARK is negative, tick articles."
8420   (interactive "P")
8421   (when unmark
8422     (setq unmark (prefix-numeric-value unmark)))
8423   (let ((count
8424          (gnus-summary-mark-same-subject
8425           (gnus-summary-article-subject) unmark)))
8426     ;; Select next unread article.  If auto-select-same mode, should
8427     ;; select the first unread article.
8428     (gnus-summary-next-article t (and gnus-auto-select-same
8429                                       (gnus-summary-article-subject)))
8430     (gnus-message 7 "%d article%s marked as %s"
8431                   count (if (= count 1) " is" "s are")
8432                   (if unmark "unread" "read"))))
8433
8434 (defun gnus-summary-kill-same-subject (&optional unmark)
8435   "Mark articles which has the same subject as read.
8436 If UNMARK is positive, remove any kind of mark.
8437 If UNMARK is negative, tick articles."
8438   (interactive "P")
8439   (when unmark
8440     (setq unmark (prefix-numeric-value unmark)))
8441   (let ((count
8442          (gnus-summary-mark-same-subject
8443           (gnus-summary-article-subject) unmark)))
8444     ;; If marked as read, go to next unread subject.
8445     (when (null unmark)
8446       ;; Go to next unread subject.
8447       (gnus-summary-next-subject 1 t))
8448     (gnus-message 7 "%d articles are marked as %s"
8449                   count (if unmark "unread" "read"))))
8450
8451 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8452   "Mark articles with same SUBJECT as read, and return marked number.
8453 If optional argument UNMARK is positive, remove any kinds of marks.
8454 If optional argument UNMARK is negative, mark articles as unread instead."
8455   (let ((count 1))
8456     (save-excursion
8457       (cond
8458        ((null unmark)                   ; Mark as read.
8459         (while (and
8460                 (progn
8461                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8462                   (gnus-summary-show-thread) t)
8463                 (gnus-summary-find-subject subject))
8464           (setq count (1+ count))))
8465        ((> unmark 0)                    ; Tick.
8466         (while (and
8467                 (progn
8468                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8469                   (gnus-summary-show-thread) t)
8470                 (gnus-summary-find-subject subject))
8471           (setq count (1+ count))))
8472        (t                               ; Mark as unread.
8473         (while (and
8474                 (progn
8475                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8476                   (gnus-summary-show-thread) t)
8477                 (gnus-summary-find-subject subject))
8478           (setq count (1+ count)))))
8479       (gnus-set-mode-line 'summary)
8480       ;; Return the number of marked articles.
8481       count)))
8482
8483 (defun gnus-summary-mark-as-processable (n &optional unmark)
8484   "Set the process mark on the next N articles.
8485 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8486 the process mark instead.  The difference between N and the actual
8487 number of articles marked is returned."
8488   (interactive "P")
8489   (if (and (null n) (gnus-region-active-p))
8490       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8491     (setq n (prefix-numeric-value n))
8492     (let ((backward (< n 0))
8493           (n (abs n)))
8494       (while (and
8495               (> n 0)
8496               (if unmark
8497                 (gnus-summary-remove-process-mark
8498                  (gnus-summary-article-number))
8499                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8500               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8501         (setq n (1- n)))
8502       (when (/= 0 n)
8503         (gnus-message 7 "No more articles"))
8504       (gnus-summary-recenter)
8505       (gnus-summary-position-point)
8506       n)))
8507
8508 (defun gnus-summary-unmark-as-processable (n)
8509   "Remove the process mark from the next N articles.
8510 If N is negative, unmark backward instead.  The difference between N and
8511 the actual number of articles unmarked is returned."
8512   (interactive "P")
8513   (gnus-summary-mark-as-processable n t))
8514
8515 (defun gnus-summary-unmark-all-processable ()
8516   "Remove the process mark from all articles."
8517   (interactive)
8518   (save-excursion
8519     (while gnus-newsgroup-processable
8520       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8521   (gnus-summary-position-point))
8522
8523 (defun gnus-summary-add-mark (article type)
8524   "Mark ARTICLE with a mark of TYPE."
8525   (let ((vtype (car (assq type gnus-article-mark-lists)))
8526         var)
8527     (if (not vtype)
8528         (error "No such mark type: %s" type)
8529       (setq var (intern (format "gnus-newsgroup-%s" type)))
8530       (set var (cons article (symbol-value var)))
8531       (if (memq type '(processable cached replied saved))
8532           (gnus-summary-update-secondary-mark article)
8533         ;;; !!! This is bobus.  We should find out what primary
8534         ;;; !!! mark we want to set.
8535         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8536
8537 (defun gnus-summary-mark-as-expirable (n)
8538   "Mark N articles forward as expirable.
8539 If N is negative, mark backward instead.  The difference between N and
8540 the actual number of articles marked is returned."
8541   (interactive "p")
8542   (gnus-summary-mark-forward n gnus-expirable-mark))
8543
8544 (defun gnus-summary-mark-article-as-replied (article)
8545   "Mark ARTICLE replied and update the summary line."
8546   (push article gnus-newsgroup-replied)
8547   (let ((buffer-read-only nil))
8548     (when (gnus-summary-goto-subject article nil t)
8549       (gnus-summary-update-secondary-mark article))))
8550
8551 (defun gnus-summary-set-bookmark (article)
8552   "Set a bookmark in current article."
8553   (interactive (list (gnus-summary-article-number)))
8554   (when (or (not (get-buffer gnus-article-buffer))
8555             (not gnus-current-article)
8556             (not gnus-article-current)
8557             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8558     (error "No current article selected"))
8559   ;; Remove old bookmark, if one exists.
8560   (let ((old (assq article gnus-newsgroup-bookmarks)))
8561     (when old
8562       (setq gnus-newsgroup-bookmarks
8563             (delq old gnus-newsgroup-bookmarks))))
8564   ;; Set the new bookmark, which is on the form
8565   ;; (article-number . line-number-in-body).
8566   (push
8567    (cons article
8568          (save-excursion
8569            (set-buffer gnus-article-buffer)
8570            (count-lines
8571             (min (point)
8572                  (save-excursion
8573                    (goto-char (point-min))
8574                    (search-forward "\n\n" nil t)
8575                    (point)))
8576             (point))))
8577    gnus-newsgroup-bookmarks)
8578   (gnus-message 6 "A bookmark has been added to the current article."))
8579
8580 (defun gnus-summary-remove-bookmark (article)
8581   "Remove the bookmark from the current article."
8582   (interactive (list (gnus-summary-article-number)))
8583   ;; Remove old bookmark, if one exists.
8584   (let ((old (assq article gnus-newsgroup-bookmarks)))
8585     (if old
8586         (progn
8587           (setq gnus-newsgroup-bookmarks
8588                 (delq old gnus-newsgroup-bookmarks))
8589           (gnus-message 6 "Removed bookmark."))
8590       (gnus-message 6 "No bookmark in current article."))))
8591
8592 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8593 (defun gnus-summary-mark-as-dormant (n)
8594   "Mark N articles forward as dormant.
8595 If N is negative, mark backward instead.  The difference between N and
8596 the actual number of articles marked is returned."
8597   (interactive "p")
8598   (gnus-summary-mark-forward n gnus-dormant-mark))
8599
8600 (defun gnus-summary-set-process-mark (article)
8601   "Set the process mark on ARTICLE and update the summary line."
8602   (setq gnus-newsgroup-processable
8603         (cons article
8604               (delq article gnus-newsgroup-processable)))
8605   (when (gnus-summary-goto-subject article)
8606     (gnus-summary-show-thread)
8607     (gnus-summary-goto-subject article)
8608     (gnus-summary-update-secondary-mark article)))
8609
8610 (defun gnus-summary-remove-process-mark (article)
8611   "Remove the process mark from ARTICLE and update the summary line."
8612   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8613   (when (gnus-summary-goto-subject article)
8614     (gnus-summary-show-thread)
8615     (gnus-summary-goto-subject article)
8616     (gnus-summary-update-secondary-mark article)))
8617
8618 (defun gnus-summary-set-saved-mark (article)
8619   "Set the process mark on ARTICLE and update the summary line."
8620   (push article gnus-newsgroup-saved)
8621   (when (gnus-summary-goto-subject article)
8622     (gnus-summary-update-secondary-mark article)))
8623
8624 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8625   "Mark N articles as read forwards.
8626 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8627 The difference between N and the actual number of articles marked is
8628 returned.
8629 Iff NO-EXPIRE, auto-expiry will be inhibited."
8630   (interactive "p")
8631   (gnus-summary-show-thread)
8632   (let ((backward (< n 0))
8633         (gnus-summary-goto-unread
8634          (and gnus-summary-goto-unread
8635               (not (eq gnus-summary-goto-unread 'never))
8636               (not (memq mark (list gnus-unread-mark
8637                                     gnus-ticked-mark gnus-dormant-mark)))))
8638         (n (abs n))
8639         (mark (or mark gnus-del-mark)))
8640     (while (and (> n 0)
8641                 (gnus-summary-mark-article nil mark no-expire)
8642                 (zerop (gnus-summary-next-subject
8643                         (if backward -1 1)
8644                         (and gnus-summary-goto-unread
8645                              (not (eq gnus-summary-goto-unread 'never)))
8646                         t)))
8647       (setq n (1- n)))
8648     (when (/= 0 n)
8649       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8650     (gnus-summary-recenter)
8651     (gnus-summary-position-point)
8652     (gnus-set-mode-line 'summary)
8653     n))
8654
8655 (defun gnus-summary-mark-article-as-read (mark)
8656   "Mark the current article quickly as read with MARK."
8657   (let ((article (gnus-summary-article-number)))
8658     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8659     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8660     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8661     (push (cons article mark) gnus-newsgroup-reads)
8662     ;; Possibly remove from cache, if that is used.
8663     (when gnus-use-cache
8664       (gnus-cache-enter-remove-article article))
8665     ;; Allow the backend to change the mark.
8666     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8667     ;; Check for auto-expiry.
8668     (when (and gnus-newsgroup-auto-expire
8669                (memq mark gnus-auto-expirable-marks))
8670       (setq mark gnus-expirable-mark)
8671       ;; Let the backend know about the mark change.
8672       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8673       (push article gnus-newsgroup-expirable))
8674     ;; Set the mark in the buffer.
8675     (gnus-summary-update-mark mark 'unread)
8676     t))
8677
8678 (defun gnus-summary-mark-article-as-unread (mark)
8679   "Mark the current article quickly as unread with MARK."
8680   (let* ((article (gnus-summary-article-number))
8681          (old-mark (gnus-summary-article-mark article)))
8682     ;; Allow the backend to change the mark.
8683     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8684     (if (eq mark old-mark)
8685         t
8686       (if (<= article 0)
8687           (progn
8688             (gnus-error 1 "Can't mark negative article numbers")
8689             nil)
8690         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8691         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8692         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8693         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8694         (cond ((= mark gnus-ticked-mark)
8695                (push article gnus-newsgroup-marked))
8696               ((= mark gnus-dormant-mark)
8697                (push article gnus-newsgroup-dormant))
8698               (t
8699                (push article gnus-newsgroup-unreads)))
8700         (gnus-pull article gnus-newsgroup-reads)
8701
8702         ;; See whether the article is to be put in the cache.
8703         (and gnus-use-cache
8704              (vectorp (gnus-summary-article-header article))
8705              (save-excursion
8706                (gnus-cache-possibly-enter-article
8707                 gnus-newsgroup-name article
8708                 (gnus-summary-article-header article)
8709                 (= mark gnus-ticked-mark)
8710                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8711
8712         ;; Fix the mark.
8713         (gnus-summary-update-mark mark 'unread)
8714         t))))
8715
8716 (defun gnus-summary-mark-article (&optional article mark no-expire)
8717   "Mark ARTICLE with MARK.  MARK can be any character.
8718 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8719 `??' (dormant) and `?E' (expirable).
8720 If MARK is nil, then the default character `?r' is used.
8721 If ARTICLE is nil, then the article on the current line will be
8722 marked.
8723 Iff NO-EXPIRE, auto-expiry will be inhibited."
8724   ;; The mark might be a string.
8725   (when (stringp mark)
8726     (setq mark (aref mark 0)))
8727   ;; If no mark is given, then we check auto-expiring.
8728   (when (null mark)
8729     (setq mark gnus-del-mark))
8730   (when (and (not no-expire)
8731              gnus-newsgroup-auto-expire
8732              (memq mark gnus-auto-expirable-marks))
8733     (setq mark gnus-expirable-mark))
8734   (let ((article (or article (gnus-summary-article-number)))
8735         (old-mark (gnus-summary-article-mark article)))
8736     ;; Allow the backend to change the mark.
8737     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8738     (if (eq mark old-mark)
8739         t
8740       (unless article
8741         (error "No article on current line"))
8742       (if (not (if (or (= mark gnus-unread-mark)
8743                        (= mark gnus-ticked-mark)
8744                        (= mark gnus-dormant-mark))
8745                    (gnus-mark-article-as-unread article mark)
8746                  (gnus-mark-article-as-read article mark)))
8747           t
8748         ;; See whether the article is to be put in the cache.
8749         (and gnus-use-cache
8750              (not (= mark gnus-canceled-mark))
8751              (vectorp (gnus-summary-article-header article))
8752              (save-excursion
8753                (gnus-cache-possibly-enter-article
8754                 gnus-newsgroup-name article
8755                 (gnus-summary-article-header article)
8756                 (= mark gnus-ticked-mark)
8757                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8758
8759         (when (gnus-summary-goto-subject article nil t)
8760           (let ((buffer-read-only nil))
8761             (gnus-summary-show-thread)
8762             ;; Fix the mark.
8763             (gnus-summary-update-mark mark 'unread)
8764             t))))))
8765
8766 (defun gnus-summary-update-secondary-mark (article)
8767   "Update the secondary (read, process, cache) mark."
8768   (gnus-summary-update-mark
8769    (cond ((memq article gnus-newsgroup-processable)
8770           gnus-process-mark)
8771          ((memq article gnus-newsgroup-cached)
8772           gnus-cached-mark)
8773          ((memq article gnus-newsgroup-replied)
8774           gnus-replied-mark)
8775          ((memq article gnus-newsgroup-saved)
8776           gnus-saved-mark)
8777          (t gnus-no-mark))
8778    'replied)
8779   (when (gnus-visual-p 'summary-highlight 'highlight)
8780     (gnus-run-hooks 'gnus-summary-update-hook))
8781   t)
8782
8783 (defun gnus-summary-update-mark (mark type)
8784   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8785         (buffer-read-only nil))
8786     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8787     (when forward
8788       (when (looking-at "\r")
8789         (incf forward))
8790       (when (<= (+ forward (point)) (point-max))
8791         ;; Go to the right position on the line.
8792         (goto-char (+ forward (point)))
8793         ;; Replace the old mark with the new mark.
8794         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8795         ;; Optionally update the marks by some user rule.
8796         (when (eq type 'unread)
8797           (gnus-data-set-mark
8798            (gnus-data-find (gnus-summary-article-number)) mark)
8799           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8800
8801 (defun gnus-mark-article-as-read (article &optional mark)
8802   "Enter ARTICLE in the pertinent lists and remove it from others."
8803   ;; Make the article expirable.
8804   (let ((mark (or mark gnus-del-mark)))
8805     (if (= mark gnus-expirable-mark)
8806         (push article gnus-newsgroup-expirable)
8807       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8808     ;; Remove from unread and marked lists.
8809     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8810     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8811     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8812     (push (cons article mark) gnus-newsgroup-reads)
8813     ;; Possibly remove from cache, if that is used.
8814     (when gnus-use-cache
8815       (gnus-cache-enter-remove-article article))
8816     t))
8817
8818 (defun gnus-mark-article-as-unread (article &optional mark)
8819   "Enter ARTICLE in the pertinent lists and remove it from others."
8820   (let ((mark (or mark gnus-ticked-mark)))
8821     (if (<= article 0)
8822         (progn
8823           (gnus-error 1 "Can't mark negative article numbers")
8824           nil)
8825       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8826             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8827             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8828             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8829
8830       ;; Unsuppress duplicates?
8831       (when gnus-suppress-duplicates
8832         (gnus-dup-unsuppress-article article))
8833
8834       (cond ((= mark gnus-ticked-mark)
8835              (push article gnus-newsgroup-marked))
8836             ((= mark gnus-dormant-mark)
8837              (push article gnus-newsgroup-dormant))
8838             (t
8839              (push article gnus-newsgroup-unreads)))
8840       (gnus-pull article gnus-newsgroup-reads)
8841       t)))
8842
8843 (defalias 'gnus-summary-mark-as-unread-forward
8844   'gnus-summary-tick-article-forward)
8845 (make-obsolete 'gnus-summary-mark-as-unread-forward
8846                'gnus-summary-tick-article-forward)
8847 (defun gnus-summary-tick-article-forward (n)
8848   "Tick N articles forwards.
8849 If N is negative, tick backwards instead.
8850 The difference between N and the number of articles ticked is returned."
8851   (interactive "p")
8852   (gnus-summary-mark-forward n gnus-ticked-mark))
8853
8854 (defalias 'gnus-summary-mark-as-unread-backward
8855   'gnus-summary-tick-article-backward)
8856 (make-obsolete 'gnus-summary-mark-as-unread-backward
8857                'gnus-summary-tick-article-backward)
8858 (defun gnus-summary-tick-article-backward (n)
8859   "Tick N articles backwards.
8860 The difference between N and the number of articles ticked is returned."
8861   (interactive "p")
8862   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8863
8864 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8865 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8866 (defun gnus-summary-tick-article (&optional article clear-mark)
8867   "Mark current article as unread.
8868 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8869 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8870   (interactive)
8871   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8872                                        gnus-ticked-mark)))
8873
8874 (defun gnus-summary-mark-as-read-forward (n)
8875   "Mark N articles as read forwards.
8876 If N is negative, mark backwards instead.
8877 The difference between N and the actual number of articles marked is
8878 returned."
8879   (interactive "p")
8880   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8881
8882 (defun gnus-summary-mark-as-read-backward (n)
8883   "Mark the N articles as read backwards.
8884 The difference between N and the actual number of articles marked is
8885 returned."
8886   (interactive "p")
8887   (gnus-summary-mark-forward
8888    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8889
8890 (defun gnus-summary-mark-as-read (&optional article mark)
8891   "Mark current article as read.
8892 ARTICLE specifies the article to be marked as read.
8893 MARK specifies a string to be inserted at the beginning of the line."
8894   (gnus-summary-mark-article article mark))
8895
8896 (defun gnus-summary-clear-mark-forward (n)
8897   "Clear marks from N articles forward.
8898 If N is negative, clear backward instead.
8899 The difference between N and the number of marks cleared is returned."
8900   (interactive "p")
8901   (gnus-summary-mark-forward n gnus-unread-mark))
8902
8903 (defun gnus-summary-clear-mark-backward (n)
8904   "Clear marks from N articles backward.
8905 The difference between N and the number of marks cleared is returned."
8906   (interactive "p")
8907   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8908
8909 (defun gnus-summary-mark-unread-as-read ()
8910   "Intended to be used by `gnus-summary-mark-article-hook'."
8911   (when (memq gnus-current-article gnus-newsgroup-unreads)
8912     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8913
8914 (defun gnus-summary-mark-read-and-unread-as-read ()
8915   "Intended to be used by `gnus-summary-mark-article-hook'."
8916   (let ((mark (gnus-summary-article-mark)))
8917     (when (or (gnus-unread-mark-p mark)
8918               (gnus-read-mark-p mark))
8919       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8920
8921 (defun gnus-summary-mark-unread-as-ticked ()
8922    "Intended to be used by `gnus-summary-mark-article-hook'."
8923   (when (memq gnus-current-article gnus-newsgroup-unreads)
8924     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
8925
8926 (defun gnus-summary-mark-region-as-read (point mark all)
8927   "Mark all unread articles between point and mark as read.
8928 If given a prefix, mark all articles between point and mark as read,
8929 even ticked and dormant ones."
8930   (interactive "r\nP")
8931   (save-excursion
8932     (let (article)
8933       (goto-char point)
8934       (beginning-of-line)
8935       (while (and
8936               (< (point) mark)
8937               (progn
8938                 (when (or all
8939                           (memq (setq article (gnus-summary-article-number))
8940                                 gnus-newsgroup-unreads))
8941                   (gnus-summary-mark-article article gnus-del-mark))
8942                 t)
8943               (gnus-summary-find-next))))))
8944
8945 (defun gnus-summary-mark-below (score mark)
8946   "Mark articles with score less than SCORE with MARK."
8947   (interactive "P\ncMark: ")
8948   (setq score (if score
8949                   (prefix-numeric-value score)
8950                 (or gnus-summary-default-score 0)))
8951   (save-excursion
8952     (set-buffer gnus-summary-buffer)
8953     (goto-char (point-min))
8954     (while
8955         (progn
8956           (and (< (gnus-summary-article-score) score)
8957                (gnus-summary-mark-article nil mark))
8958           (gnus-summary-find-next)))))
8959
8960 (defun gnus-summary-kill-below (&optional score)
8961   "Mark articles with score below SCORE as read."
8962   (interactive "P")
8963   (gnus-summary-mark-below score gnus-killed-mark))
8964
8965 (defun gnus-summary-clear-above (&optional score)
8966   "Clear all marks from articles with score above SCORE."
8967   (interactive "P")
8968   (gnus-summary-mark-above score gnus-unread-mark))
8969
8970 (defun gnus-summary-tick-above (&optional score)
8971   "Tick all articles with score above SCORE."
8972   (interactive "P")
8973   (gnus-summary-mark-above score gnus-ticked-mark))
8974
8975 (defun gnus-summary-mark-above (score mark)
8976   "Mark articles with score over SCORE with MARK."
8977   (interactive "P\ncMark: ")
8978   (setq score (if score
8979                   (prefix-numeric-value score)
8980                 (or gnus-summary-default-score 0)))
8981   (save-excursion
8982     (set-buffer gnus-summary-buffer)
8983     (goto-char (point-min))
8984     (while (and (progn
8985                   (when (> (gnus-summary-article-score) score)
8986                     (gnus-summary-mark-article nil mark))
8987                   t)
8988                 (gnus-summary-find-next)))))
8989
8990 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8991 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8992 (defun gnus-summary-limit-include-expunged (&optional no-error)
8993   "Display all the hidden articles that were expunged for low scores."
8994   (interactive)
8995   (let ((buffer-read-only nil))
8996     (let ((scored gnus-newsgroup-scored)
8997           headers h)
8998       (while scored
8999         (unless (gnus-number-to-header (caar scored))
9000           (and (setq h (gnus-summary-article-header (caar scored)))
9001                (< (cdar scored) gnus-summary-expunge-below)
9002                (push h headers)))
9003         (setq scored (cdr scored)))
9004       (if (not headers)
9005           (when (not no-error)
9006             (error "No expunged articles hidden"))
9007         (goto-char (point-min))
9008         (gnus-summary-prepare-unthreaded (nreverse headers))
9009         (goto-char (point-min))
9010         (gnus-summary-position-point)
9011         t))))
9012
9013 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
9014   "Mark all unread articles in this newsgroup as read.
9015 If prefix argument ALL is non-nil, ticked and dormant articles will
9016 also be marked as read.
9017 If QUIETLY is non-nil, no questions will be asked.
9018 If TO-HERE is non-nil, it should be a point in the buffer.  All
9019 articles before this point will be marked as read.
9020 Note that this function will only catch up the unread article
9021 in the current summary buffer limitation.
9022 The number of articles marked as read is returned."
9023   (interactive "P")
9024   (prog1
9025       (save-excursion
9026         (when (or quietly
9027                   (not gnus-interactive-catchup) ;Without confirmation?
9028                   gnus-expert-user
9029                   (gnus-y-or-n-p
9030                    (if all
9031                        "Mark absolutely all articles as read? "
9032                      "Mark all unread articles as read? ")))
9033           (if (and not-mark
9034                    (not gnus-newsgroup-adaptive)
9035                    (not gnus-newsgroup-auto-expire)
9036                    (not gnus-suppress-duplicates)
9037                    (or (not gnus-use-cache)
9038                        (eq gnus-use-cache 'passive)))
9039               (progn
9040                 (when all
9041                   (setq gnus-newsgroup-marked nil
9042                         gnus-newsgroup-dormant nil))
9043                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9044             ;; We actually mark all articles as canceled, which we
9045             ;; have to do when using auto-expiry or adaptive scoring.
9046             (gnus-summary-show-all-threads)
9047             (when (gnus-summary-first-subject (not all) t)
9048               (while (and
9049                       (if to-here (< (point) to-here) t)
9050                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
9051                       (gnus-summary-find-next (not all) nil nil t))))
9052             (gnus-set-mode-line 'summary))
9053           t))
9054     (gnus-summary-position-point)))
9055
9056 (defun gnus-summary-catchup-to-here (&optional all)
9057   "Mark all unticked articles before the current one as read.
9058 If ALL is non-nil, also mark ticked and dormant articles as read."
9059   (interactive "P")
9060   (save-excursion
9061     (gnus-save-hidden-threads
9062       (let ((beg (point)))
9063         ;; We check that there are unread articles.
9064         (when (or all (gnus-summary-find-prev))
9065           (gnus-summary-catchup all t beg)))))
9066   (gnus-summary-position-point))
9067
9068 (defun gnus-summary-catchup-all (&optional quietly)
9069   "Mark all articles in this newsgroup as read."
9070   (interactive "P")
9071   (gnus-summary-catchup t quietly))
9072
9073 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9074   "Mark all unread articles in this group as read, then exit.
9075 If prefix argument ALL is non-nil, all articles are marked as read."
9076   (interactive "P")
9077   (when (gnus-summary-catchup all quietly nil 'fast)
9078     ;; Select next newsgroup or exit.
9079     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9080              (eq gnus-auto-select-next 'quietly))
9081         (gnus-summary-next-group nil)
9082       (gnus-summary-exit))))
9083
9084 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9085   "Mark all articles in this newsgroup as read, and then exit."
9086   (interactive "P")
9087   (gnus-summary-catchup-and-exit t quietly))
9088
9089 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9090   "Mark all articles in this group as read and select the next group.
9091 If given a prefix, mark all articles, unread as well as ticked, as
9092 read."
9093   (interactive "P")
9094   (save-excursion
9095     (gnus-summary-catchup all))
9096   (gnus-summary-next-group))
9097
9098 ;;;
9099 ;;; with article
9100 ;;;
9101
9102 (defmacro gnus-with-article (article &rest forms)
9103   "Select ARTICLE and perform FORMS in the original article buffer.
9104 Then replace the article with the result."
9105   `(progn
9106      ;; We don't want the article to be marked as read.
9107      (let (gnus-mark-article-hook)
9108        (gnus-summary-select-article t t nil ,article))
9109      (set-buffer gnus-original-article-buffer)
9110      ,@forms
9111      (if (not (gnus-check-backend-function
9112                'request-replace-article (car gnus-article-current)))
9113          (gnus-message 5 "Read-only group; not replacing")
9114        (unless (gnus-request-replace-article
9115                 ,article (car gnus-article-current)
9116                 (current-buffer) t)
9117          (error "Couldn't replace article")))
9118      ;; The cache and backlog have to be flushed somewhat.
9119      (when gnus-keep-backlog
9120        (gnus-backlog-remove-article
9121         (car gnus-article-current) (cdr gnus-article-current)))
9122      (when gnus-use-cache
9123        (gnus-cache-update-article
9124         (car gnus-article-current) (cdr gnus-article-current)))))
9125
9126 (put 'gnus-with-article 'lisp-indent-function 1)
9127 (put 'gnus-with-article 'edebug-form-spec '(form body))
9128
9129 ;; Thread-based commands.
9130
9131 (defun gnus-summary-articles-in-thread (&optional article)
9132   "Return a list of all articles in the current thread.
9133 If ARTICLE is non-nil, return all articles in the thread that starts
9134 with that article."
9135   (let* ((article (or article (gnus-summary-article-number)))
9136          (data (gnus-data-find-list article))
9137          (top-level (gnus-data-level (car data)))
9138          (top-subject
9139           (cond ((null gnus-thread-operation-ignore-subject)
9140                  (gnus-simplify-subject-re
9141                   (mail-header-subject (gnus-data-header (car data)))))
9142                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9143                  (gnus-simplify-subject-fuzzy
9144                   (mail-header-subject (gnus-data-header (car data)))))
9145                 (t nil)))
9146          (end-point (save-excursion
9147                       (if (gnus-summary-go-to-next-thread)
9148                           (point) (point-max))))
9149          articles)
9150     (while (and data
9151                 (< (gnus-data-pos (car data)) end-point))
9152       (when (or (not top-subject)
9153                 (string= top-subject
9154                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9155                              (gnus-simplify-subject-fuzzy
9156                               (mail-header-subject
9157                                (gnus-data-header (car data))))
9158                            (gnus-simplify-subject-re
9159                             (mail-header-subject
9160                              (gnus-data-header (car data)))))))
9161         (push (gnus-data-number (car data)) articles))
9162       (unless (and (setq data (cdr data))
9163                    (> (gnus-data-level (car data)) top-level))
9164         (setq data nil)))
9165     ;; Return the list of articles.
9166     (nreverse articles)))
9167
9168 (defun gnus-summary-rethread-current ()
9169   "Rethread the thread the current article is part of."
9170   (interactive)
9171   (let* ((gnus-show-threads t)
9172          (article (gnus-summary-article-number))
9173          (id (mail-header-id (gnus-summary-article-header)))
9174          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9175     (unless id
9176       (error "No article on the current line"))
9177     (gnus-rebuild-thread id)
9178     (gnus-summary-goto-subject article)))
9179
9180 (defun gnus-summary-reparent-thread ()
9181   "Make the current article child of the marked (or previous) article.
9182
9183 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9184 is non-nil or the Subject: of both articles are the same."
9185   (interactive)
9186   (unless (not (gnus-group-read-only-p))
9187     (error "The current newsgroup does not support article editing"))
9188   (unless (<= (length gnus-newsgroup-processable) 1)
9189     (error "No more than one article may be marked"))
9190   (save-window-excursion
9191     (let ((gnus-article-buffer " *reparent*")
9192           (current-article (gnus-summary-article-number))
9193           ;; First grab the marked article, otherwise one line up.
9194           (parent-article (if (not (null gnus-newsgroup-processable))
9195                               (car gnus-newsgroup-processable)
9196                             (save-excursion
9197                               (if (eq (forward-line -1) 0)
9198                                   (gnus-summary-article-number)
9199                                 (error "Beginning of summary buffer"))))))
9200       (unless (not (eq current-article parent-article))
9201         (error "An article may not be self-referential"))
9202       (let ((message-id (mail-header-id
9203                          (gnus-summary-article-header parent-article))))
9204         (unless (and message-id (not (equal message-id "")))
9205           (error "No message-id in desired parent"))
9206         (gnus-with-article current-article
9207           (save-restriction
9208             (goto-char (point-min))
9209             (message-narrow-to-head)
9210             (if (re-search-forward "^References: " nil t)
9211                 (progn
9212                   (re-search-forward "^[^ \t]" nil t)
9213                   (forward-line -1)
9214                   (end-of-line)
9215                   (insert " " message-id))
9216               (insert "References: " message-id "\n"))))
9217         (set-buffer gnus-summary-buffer)
9218         (gnus-summary-unmark-all-processable)
9219         (gnus-summary-update-article current-article)
9220         (gnus-summary-rethread-current)
9221         (gnus-message 3 "Article %d is now the child of article %d"
9222                       current-article parent-article)))))
9223
9224 (defun gnus-summary-toggle-threads (&optional arg)
9225   "Toggle showing conversation threads.
9226 If ARG is positive number, turn showing conversation threads on."
9227   (interactive "P")
9228   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9229     (setq gnus-show-threads
9230           (if (null arg) (not gnus-show-threads)
9231             (> (prefix-numeric-value arg) 0)))
9232     (gnus-summary-prepare)
9233     (gnus-summary-goto-subject current)
9234     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9235     (gnus-summary-position-point)))
9236
9237 (defun gnus-summary-show-all-threads ()
9238   "Show all threads."
9239   (interactive)
9240   (save-excursion
9241     (let ((buffer-read-only nil))
9242       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9243   (gnus-summary-position-point))
9244
9245 (defun gnus-summary-show-thread ()
9246   "Show thread subtrees.
9247 Returns nil if no thread was there to be shown."
9248   (interactive)
9249   (let ((buffer-read-only nil)
9250         (orig (point))
9251         ;; first goto end then to beg, to have point at beg after let
9252         (end (progn (end-of-line) (point)))
9253         (beg (progn (beginning-of-line) (point))))
9254     (prog1
9255         ;; Any hidden lines here?
9256         (search-forward "\r" end t)
9257       (subst-char-in-region beg end ?\^M ?\n t)
9258       (goto-char orig)
9259       (gnus-summary-position-point))))
9260
9261 (defun gnus-summary-hide-all-threads ()
9262   "Hide all thread subtrees."
9263   (interactive)
9264   (save-excursion
9265     (goto-char (point-min))
9266     (gnus-summary-hide-thread)
9267     (while (zerop (gnus-summary-next-thread 1 t))
9268       (gnus-summary-hide-thread)))
9269   (gnus-summary-position-point))
9270
9271 (defun gnus-summary-hide-thread ()
9272   "Hide thread subtrees.
9273 Returns nil if no threads were there to be hidden."
9274   (interactive)
9275   (let ((buffer-read-only nil)
9276         (start (point))
9277         (article (gnus-summary-article-number)))
9278     (goto-char start)
9279     ;; Go forward until either the buffer ends or the subthread
9280     ;; ends.
9281     (when (and (not (eobp))
9282                (or (zerop (gnus-summary-next-thread 1 t))
9283                    (goto-char (point-max))))
9284       (prog1
9285           (if (and (> (point) start)
9286                    (search-backward "\n" start t))
9287               (progn
9288                 (subst-char-in-region start (point) ?\n ?\^M)
9289                 (gnus-summary-goto-subject article))
9290             (goto-char start)
9291             nil)))))
9292
9293 (defun gnus-summary-go-to-next-thread (&optional previous)
9294   "Go to the same level (or less) next thread.
9295 If PREVIOUS is non-nil, go to previous thread instead.
9296 Return the article number moved to, or nil if moving was impossible."
9297   (let ((level (gnus-summary-thread-level))
9298         (way (if previous -1 1))
9299         (beg (point)))
9300     (forward-line way)
9301     (while (and (not (eobp))
9302                 (< level (gnus-summary-thread-level)))
9303       (forward-line way))
9304     (if (eobp)
9305         (progn
9306           (goto-char beg)
9307           nil)
9308       (setq beg (point))
9309       (prog1
9310           (gnus-summary-article-number)
9311         (goto-char beg)))))
9312
9313 (defun gnus-summary-next-thread (n &optional silent)
9314   "Go to the same level next N'th thread.
9315 If N is negative, search backward instead.
9316 Returns the difference between N and the number of skips actually
9317 done.
9318
9319 If SILENT, don't output messages."
9320   (interactive "p")
9321   (let ((backward (< n 0))
9322         (n (abs n)))
9323     (while (and (> n 0)
9324                 (gnus-summary-go-to-next-thread backward))
9325       (decf n))
9326     (unless silent
9327       (gnus-summary-position-point))
9328     (when (and (not silent) (/= 0 n))
9329       (gnus-message 7 "No more threads"))
9330     n))
9331
9332 (defun gnus-summary-prev-thread (n)
9333   "Go to the same level previous N'th thread.
9334 Returns the difference between N and the number of skips actually
9335 done."
9336   (interactive "p")
9337   (gnus-summary-next-thread (- n)))
9338
9339 (defun gnus-summary-go-down-thread ()
9340   "Go down one level in the current thread."
9341   (let ((children (gnus-summary-article-children)))
9342     (when children
9343       (gnus-summary-goto-subject (car children)))))
9344
9345 (defun gnus-summary-go-up-thread ()
9346   "Go up one level in the current thread."
9347   (let ((parent (gnus-summary-article-parent)))
9348     (when parent
9349       (gnus-summary-goto-subject parent))))
9350
9351 (defun gnus-summary-down-thread (n)
9352   "Go down thread N steps.
9353 If N is negative, go up instead.
9354 Returns the difference between N and how many steps down that were
9355 taken."
9356   (interactive "p")
9357   (let ((up (< n 0))
9358         (n (abs n)))
9359     (while (and (> n 0)
9360                 (if up (gnus-summary-go-up-thread)
9361                   (gnus-summary-go-down-thread)))
9362       (setq n (1- n)))
9363     (gnus-summary-position-point)
9364     (when (/= 0 n)
9365       (gnus-message 7 "Can't go further"))
9366     n))
9367
9368 (defun gnus-summary-up-thread (n)
9369   "Go up thread N steps.
9370 If N is negative, go up instead.
9371 Returns the difference between N and how many steps down that were
9372 taken."
9373   (interactive "p")
9374   (gnus-summary-down-thread (- n)))
9375
9376 (defun gnus-summary-top-thread ()
9377   "Go to the top of the thread."
9378   (interactive)
9379   (while (gnus-summary-go-up-thread))
9380   (gnus-summary-article-number))
9381
9382 (defun gnus-summary-kill-thread (&optional unmark)
9383   "Mark articles under current thread as read.
9384 If the prefix argument is positive, remove any kinds of marks.
9385 If the prefix argument is negative, tick articles instead."
9386   (interactive "P")
9387   (when unmark
9388     (setq unmark (prefix-numeric-value unmark)))
9389   (let ((articles (gnus-summary-articles-in-thread)))
9390     (save-excursion
9391       ;; Expand the thread.
9392       (gnus-summary-show-thread)
9393       ;; Mark all the articles.
9394       (while articles
9395         (gnus-summary-goto-subject (car articles))
9396         (cond ((null unmark)
9397                (gnus-summary-mark-article-as-read gnus-killed-mark))
9398               ((> unmark 0)
9399                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9400               (t
9401                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9402         (setq articles (cdr articles))))
9403     ;; Hide killed subtrees.
9404     (and (null unmark)
9405          gnus-thread-hide-killed
9406          (gnus-summary-hide-thread))
9407     ;; If marked as read, go to next unread subject.
9408     (when (null unmark)
9409       ;; Go to next unread subject.
9410       (gnus-summary-next-subject 1 t)))
9411   (gnus-set-mode-line 'summary))
9412
9413 ;; Summary sorting commands
9414
9415 (defun gnus-summary-sort-by-number (&optional reverse)
9416   "Sort the summary buffer by article number.
9417 Argument REVERSE means reverse order."
9418   (interactive "P")
9419   (gnus-summary-sort 'number reverse))
9420
9421 (defun gnus-summary-sort-by-author (&optional reverse)
9422   "Sort the summary buffer by author name alphabetically.
9423 If `case-fold-search' is non-nil, case of letters is ignored.
9424 Argument REVERSE means reverse order."
9425   (interactive "P")
9426   (gnus-summary-sort 'author reverse))
9427
9428 (defun gnus-summary-sort-by-subject (&optional reverse)
9429   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9430 If `case-fold-search' is non-nil, case of letters is ignored.
9431 Argument REVERSE means reverse order."
9432   (interactive "P")
9433   (gnus-summary-sort 'subject reverse))
9434
9435 (defun gnus-summary-sort-by-date (&optional reverse)
9436   "Sort the summary buffer by date.
9437 Argument REVERSE means reverse order."
9438   (interactive "P")
9439   (gnus-summary-sort 'date reverse))
9440
9441 (defun gnus-summary-sort-by-score (&optional reverse)
9442   "Sort the summary buffer by score.
9443 Argument REVERSE means reverse order."
9444   (interactive "P")
9445   (gnus-summary-sort 'score reverse))
9446
9447 (defun gnus-summary-sort-by-lines (&optional reverse)
9448   "Sort the summary buffer by the number of lines.
9449 Argument REVERSE means reverse order."
9450   (interactive "P")
9451   (gnus-summary-sort 'lines reverse))
9452
9453 (defun gnus-summary-sort-by-chars (&optional reverse)
9454   "Sort the summary buffer by article length.
9455 Argument REVERSE means reverse order."
9456   (interactive "P")
9457   (gnus-summary-sort 'chars reverse))
9458
9459 (defun gnus-summary-sort (predicate reverse)
9460   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9461   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9462          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9463          (gnus-thread-sort-functions
9464           (if (not reverse)
9465               thread
9466             `(lambda (t1 t2)
9467                (,thread t2 t1))))
9468          (gnus-sort-gathered-threads-function
9469           gnus-thread-sort-functions)
9470          (gnus-article-sort-functions
9471           (if (not reverse)
9472               article
9473             `(lambda (t1 t2)
9474                (,article t2 t1))))
9475          (buffer-read-only)
9476          (gnus-summary-prepare-hook nil))
9477     ;; We do the sorting by regenerating the threads.
9478     (gnus-summary-prepare)
9479     ;; Hide subthreads if needed.
9480     (when (and gnus-show-threads gnus-thread-hide-subtree)
9481       (gnus-summary-hide-all-threads))))
9482
9483 ;; Summary saving commands.
9484
9485 (defun gnus-summary-save-article (&optional n not-saved)
9486   "Save the current article using the default saver function.
9487 If N is a positive number, save the N next articles.
9488 If N is a negative number, save the N previous articles.
9489 If N is nil and any articles have been marked with the process mark,
9490 save those articles instead.
9491 The variable `gnus-default-article-saver' specifies the saver function."
9492   (interactive "P")
9493   (let* ((articles (gnus-summary-work-articles n))
9494          (save-buffer (save-excursion
9495                         (nnheader-set-temp-buffer " *Gnus Save*")))
9496          (num (length articles))
9497          header file)
9498     (dolist (article articles)
9499       (setq header (gnus-summary-article-header article))
9500       (if (not (vectorp header))
9501           ;; This is a pseudo-article.
9502           (if (assq 'name header)
9503               (gnus-copy-file (cdr (assq 'name header)))
9504             (gnus-message 1 "Article %d is unsaveable" article))
9505         ;; This is a real article.
9506         (save-window-excursion
9507           (gnus-summary-select-article t nil nil article))
9508         (save-excursion
9509           (set-buffer save-buffer)
9510           (erase-buffer)
9511           (insert-buffer-substring gnus-original-article-buffer))
9512         (setq file (gnus-article-save save-buffer file num))
9513         (gnus-summary-remove-process-mark article)
9514         (unless not-saved
9515           (gnus-summary-set-saved-mark article))))
9516     (gnus-kill-buffer save-buffer)
9517     (gnus-summary-position-point)
9518     (gnus-set-mode-line 'summary)
9519     n))
9520
9521 (defun gnus-summary-pipe-output (&optional arg)
9522   "Pipe the current article to a subprocess.
9523 If N is a positive number, pipe the N next articles.
9524 If N is a negative number, pipe the N previous articles.
9525 If N is nil and any articles have been marked with the process mark,
9526 pipe those articles instead."
9527   (interactive "P")
9528   (require 'gnus-art)
9529   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9530     (gnus-summary-save-article arg t))
9531   (gnus-configure-windows 'pipe))
9532
9533 (defun gnus-summary-save-article-mail (&optional arg)
9534   "Append the current article to an mail file.
9535 If N is a positive number, save the N next articles.
9536 If N is a negative number, save the N previous articles.
9537 If N is nil and any articles have been marked with the process mark,
9538 save those articles instead."
9539   (interactive "P")
9540   (require 'gnus-art)
9541   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9542     (gnus-summary-save-article arg)))
9543
9544 (defun gnus-summary-save-article-rmail (&optional arg)
9545   "Append the current article to an rmail file.
9546 If N is a positive number, save the N next articles.
9547 If N is a negative number, save the N previous articles.
9548 If N is nil and any articles have been marked with the process mark,
9549 save those articles instead."
9550   (interactive "P")
9551   (require 'gnus-art)
9552   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9553     (gnus-summary-save-article arg)))
9554
9555 (defun gnus-summary-save-article-file (&optional arg)
9556   "Append the current article to a file.
9557 If N is a positive number, save the N next articles.
9558 If N is a negative number, save the N previous articles.
9559 If N is nil and any articles have been marked with the process mark,
9560 save those articles instead."
9561   (interactive "P")
9562   (require 'gnus-art)
9563   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9564     (gnus-summary-save-article arg)))
9565
9566 (defun gnus-summary-write-article-file (&optional arg)
9567   "Write the current article to a file, deleting the previous file.
9568 If N is a positive number, save the N next articles.
9569 If N is a negative number, save the N previous articles.
9570 If N is nil and any articles have been marked with the process mark,
9571 save those articles instead."
9572   (interactive "P")
9573   (require 'gnus-art)
9574   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9575     (gnus-summary-save-article arg)))
9576
9577 (defun gnus-summary-save-article-body-file (&optional arg)
9578   "Append the current article body to a file.
9579 If N is a positive number, save the N next articles.
9580 If N is a negative number, save the N previous articles.
9581 If N is nil and any articles have been marked with the process mark,
9582 save those articles instead."
9583   (interactive "P")
9584   (require 'gnus-art)
9585   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9586     (gnus-summary-save-article arg)))
9587
9588 (defun gnus-summary-pipe-message (program)
9589   "Pipe the current article through PROGRAM."
9590   (interactive "sProgram: ")
9591   (gnus-summary-select-article)
9592   (let ((mail-header-separator ""))
9593     (gnus-eval-in-buffer-window gnus-article-buffer
9594       (save-restriction
9595         (widen)
9596         (let ((start (window-start))
9597               buffer-read-only)
9598           (message-pipe-buffer-body program)
9599           (set-window-start (get-buffer-window (current-buffer)) start))))))
9600
9601 (defun gnus-get-split-value (methods)
9602   "Return a value based on the split METHODS."
9603   (let (split-name method result match)
9604     (when methods
9605       (save-excursion
9606         (set-buffer gnus-original-article-buffer)
9607         (save-restriction
9608           (nnheader-narrow-to-headers)
9609           (while (and methods (not split-name))
9610             (goto-char (point-min))
9611             (setq method (pop methods))
9612             (setq match (car method))
9613             (when (cond
9614                    ((stringp match)
9615                     ;; Regular expression.
9616                     (ignore-errors
9617                       (re-search-forward match nil t)))
9618                    ((gnus-functionp match)
9619                     ;; Function.
9620                     (save-restriction
9621                       (widen)
9622                       (setq result (funcall match gnus-newsgroup-name))))
9623                    ((consp match)
9624                     ;; Form.
9625                     (save-restriction
9626                       (widen)
9627                       (setq result (eval match)))))
9628               (setq split-name (cdr method))
9629               (cond ((stringp result)
9630                      (push (expand-file-name
9631                             result gnus-article-save-directory)
9632                            split-name))
9633                     ((consp result)
9634                      (setq split-name (append result split-name)))))))))
9635     (nreverse split-name)))
9636
9637 (defun gnus-valid-move-group-p (group)
9638   (and (boundp group)
9639        (symbol-name group)
9640        (symbol-value group)
9641        (gnus-get-function (gnus-find-method-for-group
9642                            (symbol-name group)) 'request-accept-article t)))
9643
9644 (defun gnus-read-move-group-name (prompt default articles prefix)
9645   "Read a group name."
9646   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9647          (minibuffer-confirm-incomplete nil) ; XEmacs
9648          (prom
9649           (format "%s %s to:"
9650                   prompt
9651                   (if (> (length articles) 1)
9652                       (format "these %d articles" (length articles))
9653                     "this article")))
9654          (to-newsgroup
9655           (cond
9656            ((null split-name)
9657             (gnus-completing-read default prom
9658                                   gnus-active-hashtb
9659                                   'gnus-valid-move-group-p
9660                                   nil prefix
9661                                   'gnus-group-history))
9662            ((= 1 (length split-name))
9663             (gnus-completing-read (car split-name) prom
9664                                   gnus-active-hashtb
9665                                   'gnus-valid-move-group-p
9666                                   nil nil
9667                                   'gnus-group-history))
9668            (t
9669             (gnus-completing-read nil prom
9670                                   (mapcar (lambda (el) (list el))
9671                                           (nreverse split-name))
9672                                   nil nil nil
9673                                   'gnus-group-history))))
9674          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9675     (when to-newsgroup
9676       (if (or (string= to-newsgroup "")
9677               (string= to-newsgroup prefix))
9678           (setq to-newsgroup default))
9679       (unless to-newsgroup
9680         (error "No group name entered"))
9681       (or (gnus-active to-newsgroup)
9682           (gnus-activate-group to-newsgroup nil nil to-method)
9683           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9684                                      to-newsgroup))
9685               (or (and (gnus-request-create-group to-newsgroup to-method)
9686                        (gnus-activate-group
9687                         to-newsgroup nil nil to-method)
9688                        (gnus-subscribe-group to-newsgroup))
9689                   (error "Couldn't create group %s" to-newsgroup)))
9690           (error "No such group: %s" to-newsgroup)))
9691     to-newsgroup))
9692
9693 (defun gnus-summary-save-parts (type dir n &optional reverse)
9694   "Save parts matching TYPE to DIR.
9695 If REVERSE, save parts that do not match TYPE."
9696   (interactive
9697    (list (read-string "Save parts of type: "
9698                       (or (car gnus-summary-save-parts-type-history)
9699                           gnus-summary-save-parts-default-mime)
9700                       'gnus-summary-save-parts-type-history)
9701          (setq gnus-summary-save-parts-last-directory
9702                (read-file-name "Save to directory: "
9703                                gnus-summary-save-parts-last-directory
9704                                nil t))
9705          current-prefix-arg))
9706   (gnus-summary-iterate n
9707     (let ((gnus-display-mime-function nil)
9708           (gnus-inhibit-treatment t))
9709       (gnus-summary-select-article))
9710     (save-excursion
9711       (set-buffer gnus-article-buffer)
9712       (let ((handles (or gnus-article-mime-handles
9713                          (mm-dissect-buffer) (mm-uu-dissect))))
9714         (when handles
9715           (gnus-summary-save-parts-1 type dir handles reverse)
9716           (unless gnus-article-mime-handles ;; Don't destroy this case.
9717             (mm-destroy-parts handles)))))))
9718
9719 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9720   (if (stringp (car handle))
9721       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9722               (cdr handle))
9723     (when (if reverse
9724               (not (string-match type (mm-handle-media-type handle)))
9725             (string-match type (mm-handle-media-type handle)))
9726       (let ((file (expand-file-name
9727                    (file-name-nondirectory
9728                     (or
9729                      (mail-content-type-get
9730                       (mm-handle-disposition handle) 'filename)
9731                      (concat gnus-newsgroup-name
9732                              "." (number-to-string
9733                                   (cdr gnus-article-current)))))
9734                    dir)))
9735         (unless (file-exists-p file)
9736           (mm-save-part-to-file handle file))))))
9737
9738 ;; Summary extract commands
9739
9740 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9741   (let ((buffer-read-only nil)
9742         (article (gnus-summary-article-number))
9743         after-article b e)
9744     (unless (gnus-summary-goto-subject article)
9745       (error "No such article: %d" article))
9746     (gnus-summary-position-point)
9747     ;; If all commands are to be bunched up on one line, we collect
9748     ;; them here.
9749     (unless gnus-view-pseudos-separately
9750       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9751             files action)
9752         (while ps
9753           (setq action (cdr (assq 'action (car ps))))
9754           (setq files (list (cdr (assq 'name (car ps)))))
9755           (while (and ps (cdr ps)
9756                       (string= (or action "1")
9757                                (or (cdr (assq 'action (cadr ps))) "2")))
9758             (push (cdr (assq 'name (cadr ps))) files)
9759             (setcdr ps (cddr ps)))
9760           (when files
9761             (when (not (string-match "%s" action))
9762               (push " " files))
9763             (push " " files)
9764             (when (assq 'execute (car ps))
9765               (setcdr (assq 'execute (car ps))
9766                       (funcall (if (string-match "%s" action)
9767                                    'format 'concat)
9768                                action
9769                                (mapconcat
9770                                 (lambda (f)
9771                                   (if (equal f " ")
9772                                       f
9773                                     (gnus-quote-arg-for-sh-or-csh f)))
9774                                 files " ")))))
9775           (setq ps (cdr ps)))))
9776     (if (and gnus-view-pseudos (not not-view))
9777         (while pslist
9778           (when (assq 'execute (car pslist))
9779             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9780                                   (eq gnus-view-pseudos 'not-confirm)))
9781           (setq pslist (cdr pslist)))
9782       (save-excursion
9783         (while pslist
9784           (setq after-article (or (cdr (assq 'article (car pslist)))
9785                                   (gnus-summary-article-number)))
9786           (gnus-summary-goto-subject after-article)
9787           (forward-line 1)
9788           (setq b (point))
9789           (insert "    " (file-name-nondirectory
9790                           (cdr (assq 'name (car pslist))))
9791                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9792           (setq e (point))
9793           (forward-line -1)             ; back to `b'
9794           (gnus-add-text-properties
9795            b (1- e) (list 'gnus-number gnus-reffed-article-number
9796                           gnus-mouse-face-prop gnus-mouse-face))
9797           (gnus-data-enter
9798            after-article gnus-reffed-article-number
9799            gnus-unread-mark b (car pslist) 0 (- e b))
9800           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9801           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9802           (setq pslist (cdr pslist)))))))
9803
9804 (defun gnus-pseudos< (p1 p2)
9805   (let ((c1 (cdr (assq 'action p1)))
9806         (c2 (cdr (assq 'action p2))))
9807     (and c1 c2 (string< c1 c2))))
9808
9809 (defun gnus-request-pseudo-article (props)
9810   (cond ((assq 'execute props)
9811          (gnus-execute-command (cdr (assq 'execute props)))))
9812   (let ((gnus-current-article (gnus-summary-article-number)))
9813     (gnus-run-hooks 'gnus-mark-article-hook)))
9814
9815 (defun gnus-execute-command (command &optional automatic)
9816   (save-excursion
9817     (gnus-article-setup-buffer)
9818     (set-buffer gnus-article-buffer)
9819     (setq buffer-read-only nil)
9820     (let ((command (if automatic command
9821                      (read-string "Command: " (cons command 0)))))
9822       (erase-buffer)
9823       (insert "$ " command "\n\n")
9824       (if gnus-view-pseudo-asynchronously
9825           (start-process "gnus-execute" (current-buffer) shell-file-name
9826                          shell-command-switch command)
9827         (call-process shell-file-name nil t nil
9828                       shell-command-switch command)))))
9829
9830 ;; Summary kill commands.
9831
9832 (defun gnus-summary-edit-global-kill (article)
9833   "Edit the \"global\" kill file."
9834   (interactive (list (gnus-summary-article-number)))
9835   (gnus-group-edit-global-kill article))
9836
9837 (defun gnus-summary-edit-local-kill ()
9838   "Edit a local kill file applied to the current newsgroup."
9839   (interactive)
9840   (setq gnus-current-headers (gnus-summary-article-header))
9841   (gnus-group-edit-local-kill
9842    (gnus-summary-article-number) gnus-newsgroup-name))
9843
9844 ;;; Header reading.
9845
9846 (defun gnus-read-header (id &optional header)
9847   "Read the headers of article ID and enter them into the Gnus system."
9848   (let ((group gnus-newsgroup-name)
9849         (gnus-override-method
9850          (or
9851           gnus-override-method
9852           (and (gnus-news-group-p gnus-newsgroup-name)
9853                (car (gnus-refer-article-methods)))))
9854         where)
9855     ;; First we check to see whether the header in question is already
9856     ;; fetched.
9857     (if (stringp id)
9858         ;; This is a Message-ID.
9859         (setq header (or header (gnus-id-to-header id)))
9860       ;; This is an article number.
9861       (setq header (or header (gnus-summary-article-header id))))
9862     (if (and header
9863              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9864         ;; We have found the header.
9865         header
9866       ;; If this is a sparse article, we have to nix out its
9867       ;; previous entry in the thread hashtb.
9868       (when (and header
9869                  (gnus-summary-article-sparse-p (mail-header-number header)))
9870         (let* ((parent (gnus-parent-id (mail-header-references header)))
9871                (thread (and parent (gnus-id-to-thread parent))))
9872           (when thread
9873             (delq (assq header thread) thread))))
9874       ;; We have to really fetch the header to this article.
9875       (save-excursion
9876         (set-buffer nntp-server-buffer)
9877         (when (setq where (gnus-request-head id group))
9878           (nnheader-fold-continuation-lines)
9879           (goto-char (point-max))
9880           (insert ".\n")
9881           (goto-char (point-min))
9882           (insert "211 ")
9883           (princ (cond
9884                   ((numberp id) id)
9885                   ((cdr where) (cdr where))
9886                   (header (mail-header-number header))
9887                   (t gnus-reffed-article-number))
9888                  (current-buffer))
9889           (insert " Article retrieved.\n"))
9890         (if (or (not where)
9891                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9892             ()                          ; Malformed head.
9893           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9894             (when (and (stringp id)
9895                        (not (string= (gnus-group-real-name group)
9896                                      (car where))))
9897               ;; If we fetched by Message-ID and the article came
9898               ;; from a different group, we fudge some bogus article
9899               ;; numbers for this article.
9900               (mail-header-set-number header gnus-reffed-article-number))
9901             (save-excursion
9902               (set-buffer gnus-summary-buffer)
9903               (decf gnus-reffed-article-number)
9904               (gnus-remove-header (mail-header-number header))
9905               (push header gnus-newsgroup-headers)
9906               (setq gnus-current-headers header)
9907               (push (mail-header-number header) gnus-newsgroup-limit)))
9908           header)))))
9909
9910 (defun gnus-remove-header (number)
9911   "Remove header NUMBER from `gnus-newsgroup-headers'."
9912   (if (and gnus-newsgroup-headers
9913            (= number (mail-header-number (car gnus-newsgroup-headers))))
9914       (pop gnus-newsgroup-headers)
9915     (let ((headers gnus-newsgroup-headers))
9916       (while (and (cdr headers)
9917                   (not (= number (mail-header-number (cadr headers)))))
9918         (pop headers))
9919       (when (cdr headers)
9920         (setcdr headers (cddr headers))))))
9921
9922 ;;;
9923 ;;; summary highlights
9924 ;;;
9925
9926 (defun gnus-highlight-selected-summary ()
9927   "Highlight selected article in summary buffer."
9928   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9929   (when gnus-summary-selected-face
9930     (save-excursion
9931       (let* ((beg (progn (beginning-of-line) (point)))
9932              (end (progn (end-of-line) (point)))
9933              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9934              (from (if (get-text-property beg gnus-mouse-face-prop)
9935                        beg
9936                      (or (next-single-property-change
9937                           beg gnus-mouse-face-prop nil end)
9938                          beg)))
9939              (to
9940               (if (= from end)
9941                   (- from 2)
9942                 (or (next-single-property-change
9943                      from gnus-mouse-face-prop nil end)
9944                     end))))
9945         ;; If no mouse-face prop on line we will have to = from = end,
9946         ;; so we highlight the entire line instead.
9947         (when (= (+ to 2) from)
9948           (setq from beg)
9949           (setq to end))
9950         (if gnus-newsgroup-selected-overlay
9951             ;; Move old overlay.
9952             (gnus-move-overlay
9953              gnus-newsgroup-selected-overlay from to (current-buffer))
9954           ;; Create new overlay.
9955           (gnus-overlay-put
9956            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9957            'face gnus-summary-selected-face))))))
9958
9959 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9960 (defun gnus-summary-highlight-line ()
9961   "Highlight current line according to `gnus-summary-highlight'."
9962   (let* ((list gnus-summary-highlight)
9963          (p (point))
9964          (end (progn (end-of-line) (point)))
9965          ;; now find out where the line starts and leave point there.
9966          (beg (progn (beginning-of-line) (point)))
9967          (article (gnus-summary-article-number))
9968          (score (or (cdr (assq (or article gnus-current-article)
9969                                gnus-newsgroup-scored))
9970                     gnus-summary-default-score 0))
9971          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9972          (inhibit-read-only t))
9973     ;; Eval the cars of the lists until we find a match.
9974     (let ((default gnus-summary-default-score))
9975       (while (and list
9976                   (not (eval (caar list))))
9977         (setq list (cdr list))))
9978     (let ((face (cdar list)))
9979       (unless (eq face (get-text-property beg 'face))
9980         (gnus-put-text-property-excluding-characters-with-faces
9981          beg end 'face
9982          (setq face (if (boundp face) (symbol-value face) face)))
9983         (when gnus-summary-highlight-line-function
9984           (funcall gnus-summary-highlight-line-function article face))))
9985     (goto-char p)))
9986
9987 (defun gnus-update-read-articles (group unread &optional compute)
9988   "Update the list of read articles in GROUP."
9989   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9990          (entry (gnus-gethash group gnus-newsrc-hashtb))
9991          (info (nth 2 entry))
9992          (prev 1)
9993          (unread (sort (copy-sequence unread) '<))
9994          read)
9995     (if (or (not info) (not active))
9996         ;; There is no info on this group if it was, in fact,
9997         ;; killed.  Gnus stores no information on killed groups, so
9998         ;; there's nothing to be done.
9999         ;; One could store the information somewhere temporarily,
10000         ;; perhaps...  Hmmm...
10001         ()
10002       ;; Remove any negative articles numbers.
10003       (while (and unread (< (car unread) 0))
10004         (setq unread (cdr unread)))
10005       ;; Remove any expired article numbers
10006       (while (and unread (< (car unread) (car active)))
10007         (setq unread (cdr unread)))
10008       ;; Compute the ranges of read articles by looking at the list of
10009       ;; unread articles.
10010       (while unread
10011         (when (/= (car unread) prev)
10012           (push (if (= prev (1- (car unread))) prev
10013                   (cons prev (1- (car unread))))
10014                 read))
10015         (setq prev (1+ (car unread)))
10016         (setq unread (cdr unread)))
10017       (when (<= prev (cdr active))
10018         (push (cons prev (cdr active)) read))
10019       (setq read (if (> (length read) 1) (nreverse read) read))
10020       (if compute
10021           read
10022         (save-excursion
10023           (let (setmarkundo)
10024             ;; Propagate the read marks to the backend.
10025             (when (gnus-check-backend-function 'request-set-mark group)
10026               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10027                     (add (gnus-remove-from-range read (gnus-info-read info))))
10028                 (when (or add del)
10029                   (unless (gnus-check-group group)
10030                     (error "Can't open server for %s" group))
10031                   (gnus-request-set-mark
10032                    group (delq nil (list (if add (list add 'add '(read)))
10033                                          (if del (list del 'del '(read))))))
10034                   (setq setmarkundo
10035                         `(gnus-request-set-mark
10036                           ,group
10037                           ',(delq nil (list
10038                                        (if del (list del 'add '(read)))
10039                                        (if add (list add 'del '(read))))))))))
10040             (set-buffer gnus-group-buffer)
10041             (gnus-undo-register
10042               `(progn
10043                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10044                  (gnus-info-set-read ',info ',(gnus-info-read info))
10045                  (gnus-get-unread-articles-in-group ',info
10046                                                     (gnus-active ,group))
10047                  (gnus-group-update-group ,group t)
10048                  ,setmarkundo))))
10049         ;; Enter this list into the group info.
10050         (gnus-info-set-read info read)
10051         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10052         (gnus-get-unread-articles-in-group info (gnus-active group))
10053         t))))
10054
10055 (defun gnus-offer-save-summaries ()
10056   "Offer to save all active summary buffers."
10057   (save-excursion
10058     (let ((buflist (buffer-list))
10059           buffers bufname)
10060       ;; Go through all buffers and find all summaries.
10061       (while buflist
10062         (and (setq bufname (buffer-name (car buflist)))
10063              (string-match "Summary" bufname)
10064              (save-excursion
10065                (set-buffer bufname)
10066                ;; We check that this is, indeed, a summary buffer.
10067                (and (eq major-mode 'gnus-summary-mode)
10068                     ;; Also make sure this isn't bogus.
10069                     gnus-newsgroup-prepared
10070                     ;; Also make sure that this isn't a dead summary buffer.
10071                     (not gnus-dead-summary-mode)))
10072              (push bufname buffers))
10073         (setq buflist (cdr buflist)))
10074       ;; Go through all these summary buffers and offer to save them.
10075       (when buffers
10076         (map-y-or-n-p
10077          "Update summary buffer %s? "
10078          (lambda (buf)
10079            (switch-to-buffer buf)
10080            (gnus-summary-exit))
10081          buffers)))))
10082
10083
10084 ;;; @ for mime-partial
10085 ;;;
10086
10087 (defun gnus-request-partial-message ()
10088   (save-excursion
10089     (let ((number (gnus-summary-article-number))
10090           (group gnus-newsgroup-name)
10091           (mother gnus-article-buffer))
10092       (set-buffer (get-buffer-create " *Partial Article*"))
10093       (erase-buffer)
10094       (setq mime-preview-buffer mother)
10095       (gnus-request-article-this-buffer number group)
10096       (mime-parse-buffer)
10097       )))
10098
10099 (autoload 'mime-combine-message/partial-pieces-automatically
10100   "mime-partial"
10101   "Internal method to combine message/partial messages automatically.")
10102
10103 (mime-add-condition
10104  'action '((type . message)(subtype . partial)
10105            (major-mode . gnus-original-article-mode)
10106            (method . mime-combine-message/partial-pieces-automatically)
10107            (summary-buffer-exp . gnus-summary-buffer)
10108            (request-partial-message-method . gnus-request-partial-message)
10109            ))
10110
10111
10112 ;;; @ for message/rfc822
10113 ;;;
10114
10115 (defun gnus-mime-extract-message/rfc822 (entity situation)
10116   (let (group article num cwin swin cur)
10117     (with-temp-buffer
10118       (mime-insert-entity-content entity)
10119       (setq group (or (cdr (assq 'group situation))
10120                       (completing-read "Group: "
10121                                        gnus-active-hashtb
10122                                        nil
10123                                        (gnus-read-active-file-p)
10124                                        gnus-newsgroup-name))
10125             article (gnus-request-accept-article group)))
10126     (when (and (consp article)
10127                (numberp (setq article (cdr article))))
10128       (setq num (1+ (or (cdr (assq 'number situation)) 0))
10129             cwin (get-buffer-window (current-buffer) t))
10130       (save-window-excursion
10131         (if (setq swin (get-buffer-window gnus-summary-buffer t))
10132             (select-window swin)
10133           (set-buffer gnus-summary-buffer))
10134         (setq cur gnus-current-article)
10135         (forward-line num)
10136         (let (gnus-show-threads)
10137           (gnus-summary-goto-subject article t))
10138         (gnus-summary-clear-mark-forward 1)
10139         (gnus-summary-goto-subject cur))
10140       (when (and cwin (window-frame cwin))
10141         (select-frame (window-frame cwin)))
10142       (when (boundp 'mime-acting-situation-to-override)
10143         (set-alist 'mime-acting-situation-to-override
10144                    'group
10145                    group)
10146         (set-alist 'mime-acting-situation-to-override
10147                    'after-method
10148                    `(progn
10149                       (save-current-buffer
10150                         (set-buffer gnus-group-buffer)
10151                         (gnus-activate-group ,group))
10152                       (gnus-summary-goto-article ,cur
10153                                                  gnus-show-all-headers)))
10154         (set-alist 'mime-acting-situation-to-override
10155                    'number num)))))
10156
10157 (mime-add-condition
10158  'action '((type . message)(subtype . rfc822)
10159            (major-mode . gnus-original-article-mode)
10160            (method . gnus-mime-extract-message/rfc822)
10161            (mode . "extract")
10162            ))
10163
10164 (mime-add-condition
10165  'action '((type . message)(subtype . news)
10166            (major-mode . gnus-original-article-mode)
10167            (method . gnus-mime-extract-message/rfc822)
10168            (mode . "extract")
10169            ))
10170
10171 (defun gnus-mime-extract-multipart (entity situation)
10172   (let ((children (mime-entity-children entity))
10173         mime-acting-situation-to-override
10174         f)
10175     (while children
10176       (mime-play-entity (car children)
10177                         (cons (assq 'mode situation)
10178                               mime-acting-situation-to-override))
10179       (setq children (cdr children)))
10180     (if (setq f (cdr (assq 'after-method
10181                            mime-acting-situation-to-override)))
10182         (eval f)
10183       )))
10184
10185 (mime-add-condition
10186  'action '((type . multipart)
10187            (method . gnus-mime-extract-multipart)
10188            (mode . "extract")
10189            )
10190  'with-default)
10191
10192
10193 ;;; @ end
10194 ;;;
10195
10196 (defun gnus-summary-setup-default-charset ()
10197   "Setup newsgroup default charset."
10198   (if (equal gnus-newsgroup-name "nndraft:drafts")
10199       (setq gnus-newsgroup-charset nil)
10200     (let* ((ignored-charsets
10201             (or gnus-newsgroup-ephemeral-ignored-charsets
10202                 (append
10203                  (and gnus-newsgroup-name
10204                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10205                  gnus-newsgroup-ignored-charsets))))
10206       (setq gnus-newsgroup-charset
10207             (or gnus-newsgroup-ephemeral-charset
10208                 (and gnus-newsgroup-name
10209                      (gnus-parameter-charset gnus-newsgroup-name))
10210                 gnus-default-charset))
10211       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10212            ignored-charsets))))
10213
10214 ;;;
10215 ;;; Mime Commands
10216 ;;;
10217
10218 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10219   "Display the current article buffer fully MIME-buttonized.
10220 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10221 treated as multipart/mixed."
10222   (interactive "P")
10223   (require 'gnus-art)
10224   (let ((gnus-unbuttonized-mime-types nil)
10225         (gnus-mime-display-multipart-as-mixed show-all-parts))
10226     (gnus-summary-show-article)))
10227
10228 (defun gnus-summary-repair-multipart (article)
10229   "Add a Content-Type header to a multipart article without one."
10230   (interactive (list (gnus-summary-article-number)))
10231   (gnus-with-article article
10232     (message-narrow-to-head)
10233     (message-remove-header "Mime-Version")
10234     (goto-char (point-max))
10235     (insert "Mime-Version: 1.0\n")
10236     (widen)
10237     (when (search-forward "\n--" nil t)
10238       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10239         (message-narrow-to-head)
10240         (message-remove-header "Content-Type")
10241         (goto-char (point-max))
10242         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10243                         separator))
10244         (widen))))
10245   (let (gnus-mark-article-hook)
10246     (gnus-summary-select-article t t nil article)))
10247
10248 (defun gnus-summary-toggle-display-buttonized ()
10249   "Toggle the buttonizing of the article buffer."
10250   (interactive)
10251   (require 'gnus-art)
10252   (if (setq gnus-inhibit-mime-unbuttonizing
10253             (not gnus-inhibit-mime-unbuttonizing))
10254       (let ((gnus-unbuttonized-mime-types nil))
10255         (gnus-summary-show-article))
10256     (gnus-summary-show-article)))
10257
10258 ;;;
10259 ;;; Intelli-mouse commmands
10260 ;;;
10261
10262 (defun gnus-wheel-summary-scroll (event)
10263   (interactive "e")
10264   (let ((amount (if (memq 'shift (event-modifiers event))
10265                     (car gnus-wheel-scroll-amount)
10266                   (cdr gnus-wheel-scroll-amount)))
10267         (direction (- (* (static-if (featurep 'xemacs)
10268                              (event-button event)
10269                            (cond ((eq 'mouse-4 (event-basic-type event))
10270                                   4)
10271                                  ((eq 'mouse-5 (event-basic-type event))
10272                                   5)))
10273                          2) 9))
10274         edge)
10275     (gnus-summary-scroll-up (* amount direction))
10276     (when (gnus-eval-in-buffer-window gnus-article-buffer
10277             (save-restriction
10278               (widen)
10279               (and (if (< 0 direction)
10280                        (gnus-article-next-page 0)
10281                      (gnus-article-prev-page 0)
10282                      (bobp))
10283                    (if (setq edge (get-text-property
10284                                    (point-min) 'gnus-wheel-edge))
10285                        (setq edge (* edge direction))
10286                      (setq edge -1))
10287                    (or (plusp edge)
10288                        (let ((buffer-read-only nil)
10289                              (inhibit-read-only t))
10290                          (put-text-property (point-min) (point-max)
10291                                             'gnus-wheel-edge direction)
10292                          nil))
10293                    (or (> edge gnus-wheel-edge-resistance)
10294                        (let ((buffer-read-only nil)
10295                              (inhibit-read-only t))
10296                          (put-text-property (point-min) (point-max)
10297                                             'gnus-wheel-edge
10298                                             (* (1+ edge) direction))
10299                          nil))
10300                    (eq last-command 'gnus-wheel-summary-scroll))))
10301       (gnus-summary-next-article nil nil (minusp direction)))))
10302
10303 (defun gnus-wheel-install ()
10304   "Enable mouse wheel support on summary window."
10305   (when gnus-use-wheel
10306     (let ((keys
10307            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
10308       (dolist (key keys)
10309         (define-key gnus-summary-mode-map key
10310           'gnus-wheel-summary-scroll)))))
10311
10312 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
10313
10314 ;;;
10315 ;;; Traditional PGP commmands
10316 ;;;
10317
10318 (defun gnus-summary-decrypt-article (&optional force)
10319   "Decrypt the current article in traditional PGP way.
10320 This will have permanent effect only in mail groups.
10321 If FORCE is non-nil, allow editing of articles even in read-only
10322 groups."
10323   (interactive "P")
10324   (gnus-summary-select-article t)
10325   (gnus-eval-in-buffer-window gnus-article-buffer
10326     (save-excursion
10327       (save-restriction
10328         (widen)
10329         (goto-char (point-min))
10330         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10331           (error "Not a traditional PGP message!"))
10332         (let ((armor-start (match-beginning 0)))
10333           (if (and (pgg-decrypt-region armor-start (point-max))
10334                    (or force (not (gnus-group-read-only-p))))
10335               (let ((inhibit-read-only t)
10336                     buffer-read-only)
10337                 (delete-region armor-start
10338                                (progn
10339                                  (re-search-forward "^-+END PGP" nil t)
10340                                  (beginning-of-line 2)
10341                                  (point)))
10342                 (insert-buffer-substring pgg-output-buffer))))))))
10343
10344 (defun gnus-summary-verify-article ()
10345   "Verify the current article in traditional PGP way."
10346   (interactive)
10347   (save-excursion
10348     (set-buffer gnus-original-article-buffer)
10349     (goto-char (point-min))
10350     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10351       (error "Not a traditional PGP message!"))
10352     (re-search-forward "^-+END PGP" nil t)
10353     (beginning-of-line 2)
10354     (call-interactively (function pgg-verify-region))))
10355
10356 ;;;
10357 ;;; Generic summary marking commands
10358 ;;;
10359
10360 (defvar gnus-summary-marking-alist
10361   '((read gnus-del-mark "d")
10362     (unread gnus-unread-mark "u")
10363     (ticked gnus-ticked-mark "!")
10364     (dormant gnus-dormant-mark "?")
10365     (expirable gnus-expirable-mark "e"))
10366   "An alist of names/marks/keystrokes.")
10367
10368 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10369 (defvar gnus-summary-mark-map)
10370
10371 (defun gnus-summary-make-all-marking-commands ()
10372   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10373   (dolist (elem gnus-summary-marking-alist)
10374     (apply 'gnus-summary-make-marking-command elem)))
10375
10376 (defun gnus-summary-make-marking-command (name mark keystroke)
10377   (let ((map (make-sparse-keymap)))
10378     (define-key gnus-summary-generic-mark-map keystroke map)
10379     (dolist (lway `((next "next" next nil "n")
10380                     (next-unread "next unread" next t "N")
10381                     (prev "previous" prev nil "p")
10382                     (prev-unread "previous unread" prev t "P")
10383                     (nomove "" nil nil ,keystroke)))
10384       (let ((func (gnus-summary-make-marking-command-1
10385                    mark (car lway) lway name)))
10386         (setq func (eval func))
10387         (define-key map (nth 4 lway) func)))))
10388
10389 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10390   `(defun ,(intern
10391             (format "gnus-summary-put-mark-as-%s%s"
10392                     name (if (eq way 'nomove)
10393                              ""
10394                            (concat "-" (symbol-name way)))))
10395      (n)
10396      ,(format
10397        "Mark the current article as %s%s.
10398 If N, the prefix, then repeat N times.
10399 If N is negative, move in reverse order.
10400 The difference between N and the actual number of articles marked is
10401 returned."
10402        name (car (cdr lway)))
10403      (interactive "p")
10404      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10405
10406 (defun gnus-summary-generic-mark (n mark move unread)
10407   "Mark N articles with MARK."
10408   (unless (eq major-mode 'gnus-summary-mode)
10409     (error "This command can only be used in the summary buffer"))
10410   (gnus-summary-show-thread)
10411   (let ((nummove
10412          (cond
10413           ((eq move 'next) 1)
10414           ((eq move 'prev) -1)
10415           (t 0))))
10416     (if (zerop nummove)
10417         (setq n 1)
10418       (when (< n 0)
10419         (setq n (abs n)
10420               nummove (* -1 nummove))))
10421     (while (and (> n 0)
10422                 (gnus-summary-mark-article nil mark)
10423                 (zerop (gnus-summary-next-subject nummove unread t)))
10424       (setq n (1- n)))
10425     (when (/= 0 n)
10426       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10427     (gnus-summary-recenter)
10428     (gnus-summary-position-point)
10429     (gnus-set-mode-line 'summary)
10430     n))
10431
10432 (defun gnus-summary-insert-articles (articles)
10433   (when (setq articles
10434               (gnus-set-difference articles
10435                                    (mapcar (lambda (h) (mail-header-number h))
10436                                            gnus-newsgroup-headers)))
10437     (setq gnus-newsgroup-headers 
10438           (merge 'list
10439                  gnus-newsgroup-headers
10440                  (gnus-fetch-headers articles)
10441                  'gnus-article-sort-by-number))
10442     ;; Suppress duplicates?
10443     (when gnus-suppress-duplicates
10444       (gnus-dup-suppress-articles))
10445     
10446     ;; We might want to build some more threads first.
10447     (when (and gnus-fetch-old-headers
10448                (eq gnus-headers-retrieved-by 'nov))
10449       (if (eq gnus-fetch-old-headers 'invisible)
10450         (gnus-build-all-threads)
10451         (gnus-build-old-threads)))
10452     ;; Let the Gnus agent mark articles as read.
10453     (when gnus-agent
10454       (gnus-agent-get-undownloaded-list))
10455     ;; Remove list identifiers from subject
10456     (when gnus-list-identifiers
10457       (gnus-summary-remove-list-identifiers))
10458     ;; First and last article in this newsgroup.
10459     (when gnus-newsgroup-headers
10460       (setq gnus-newsgroup-begin
10461             (mail-header-number (car gnus-newsgroup-headers))
10462             gnus-newsgroup-end
10463             (mail-header-number
10464              (gnus-last-element gnus-newsgroup-headers))))
10465     (when gnus-use-scoring
10466       (gnus-possibly-score-headers))))
10467
10468 (defun gnus-summary-insert-old-articles (&optional all)
10469   "Insert all old articles in this group.
10470 If ALL is non-nil, already read articles become readable.
10471 If ALL is a number, fetch this number of articles."
10472   (interactive "P")
10473   (prog1
10474       (let ((old (mapcar 'car gnus-newsgroup-data))
10475             (i (car gnus-newsgroup-active))
10476             older len)
10477         (while (<= i (cdr gnus-newsgroup-active))
10478           (or (memq i old) (push i older))
10479           (incf i))
10480         (setq len (length older))
10481         (cond 
10482          ((null older) nil)
10483          ((numberp all) 
10484           (if (< all len)
10485               (setq older (subseq older 0 all))))
10486          (all nil)
10487          (t
10488           (if (and (numberp gnus-large-newsgroup)
10489                    (> len gnus-large-newsgroup))
10490               (let ((input
10491                      (read-string
10492                       (format
10493                        "How many articles from %s (default %d): "
10494                        (gnus-limit-string 
10495                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10496                        len))))
10497                 (unless (string-match "^[ \t]*$" input) 
10498                   (setq all (string-to-number input))
10499                   (if (< all len)
10500                       (setq older (subseq older 0 all))))))))
10501         (if (not older)
10502             (message "No old news.")
10503           (gnus-summary-insert-articles older)
10504           (gnus-summary-limit (gnus-union older old))))
10505     (gnus-summary-position-point)))
10506
10507 (defun gnus-summary-insert-new-articles ()
10508   "Insert all new articles in this group."
10509   (interactive)
10510   (prog1
10511       (let ((old (mapcar 'car gnus-newsgroup-data))
10512             (old-active gnus-newsgroup-active)
10513             (nnmail-fetched-sources (list t))
10514             i new)
10515         (setq gnus-newsgroup-active 
10516               (gnus-activate-group gnus-newsgroup-name 'scan))
10517         (setq i (1+ (cdr old-active)))
10518         (while (<= i (cdr gnus-newsgroup-active))
10519           (push i new)
10520           (incf i))
10521         (if (not new)
10522             (message "No gnus is bad news.")
10523           (gnus-summary-insert-articles new)
10524           (setq gnus-newsgroup-unreads
10525                 (append gnus-newsgroup-unreads new))
10526           (gnus-summary-limit (gnus-union old new))))
10527     (gnus-summary-position-point)))
10528
10529 (gnus-summary-make-all-marking-commands)
10530
10531 (gnus-ems-redefine)
10532
10533 (provide 'gnus-sum)
10534
10535 (run-hooks 'gnus-sum-load-hook)
10536
10537 ;;; gnus-sum.el ends here