b13687502a40c6676017c2dc1eddb1c97f2d33b7
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
8 ;; Keywords: mail, news, MIME
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32 (eval-when-compile (require 'gnus-clfns))
33
34 (require 'gnus)
35 (require 'gnus-group)
36 (require 'gnus-spec)
37 (require 'gnus-range)
38 (require 'gnus-int)
39 (require 'gnus-undo)
40 (require 'gnus-util)
41 ;; Recursive :-(.
42 ;; (require 'gnus-art)
43 (require 'nnoo)
44 (require 'mime-view)
45
46 (eval-when-compile
47   (require 'mime-play)
48   (require 'static))
49
50 (eval-and-compile
51   (autoload 'gnus-cache-articles-in-group "gnus-cache")
52   (autoload 'pgg-decrypt-region "pgg" nil t)
53   (autoload 'pgg-verify-region "pgg" nil t))
54
55 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
56 (autoload 'gnus-cache-write-active "gnus-cache")
57 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
58 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
59 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
60 (autoload 'mm-uu-dissect "mm-uu")
61
62 (defcustom gnus-kill-summary-on-exit t
63   "*If non-nil, kill the summary buffer when you exit from it.
64 If nil, the summary will become a \"*Dead Summary*\" buffer, and
65 it will be killed sometime later."
66   :group 'gnus-summary-exit
67   :type 'boolean)
68
69 (defcustom gnus-fetch-old-headers nil
70   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
71 If an unread article in the group refers to an older, already read (or
72 just marked as read) article, the old article will not normally be
73 displayed in the Summary buffer.  If this variable is non-nil, Gnus
74 will attempt to grab the headers to the old articles, and thereby
75 build complete threads.  If it has the value `some', only enough
76 headers to connect otherwise loose threads will be displayed.  This
77 variable can also be a number.  In that case, no more than that number
78 of old headers will be fetched.  If it has the value `invisible', all
79 old headers will be fetched, but none will be displayed.
80
81 The server has to support NOV for any of this to work."
82   :group 'gnus-thread
83   :type '(choice (const :tag "off" nil)
84                  (const some)
85                  number
86                  (sexp :menu-tag "other" t)))
87
88 (defcustom gnus-refer-thread-limit 200
89   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
90 If t, fetch all the available old headers."
91   :group 'gnus-thread
92   :type '(choice number
93                  (sexp :menu-tag "other" t)))
94
95 (defcustom gnus-summary-make-false-root 'adopt
96   "*nil means that Gnus won't gather loose threads.
97 If the root of a thread has expired or been read in a previous
98 session, the information necessary to build a complete thread has been
99 lost.  Instead of having many small sub-threads from this original thread
100 scattered all over the summary buffer, Gnus can gather them.
101
102 If non-nil, Gnus will try to gather all loose sub-threads from an
103 original thread into one large thread.
104
105 If this variable is non-nil, it should be one of `none', `adopt',
106 `dummy' or `empty'.
107
108 If this variable is `none', Gnus will not make a false root, but just
109 present the sub-threads after another.
110 If this variable is `dummy', Gnus will create a dummy root that will
111 have all the sub-threads as children.
112 If this variable is `adopt', Gnus will make one of the \"children\"
113 the parent and mark all the step-children as such.
114 If this variable is `empty', the \"children\" are printed with empty
115 subject fields.  (Or rather, they will be printed with a string
116 given by the `gnus-summary-same-subject' variable.)"
117   :group 'gnus-thread
118   :type '(choice (const :tag "off" nil)
119                  (const none)
120                  (const dummy)
121                  (const adopt)
122                  (const empty)))
123
124 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
125   "*A regexp to match subjects to be excluded from loose thread gathering.
126 As loose thread gathering is done on subjects only, that means that
127 there can be many false gatherings performed.  By rooting out certain
128 common subjects, gathering might become saner."
129   :group 'gnus-thread
130   :type 'regexp)
131
132 (defcustom gnus-summary-gather-subject-limit nil
133   "*Maximum length of subject comparisons when gathering loose threads.
134 Use nil to compare full subjects.  Setting this variable to a low
135 number will help gather threads that have been corrupted by
136 newsreaders chopping off subject lines, but it might also mean that
137 unrelated articles that have subject that happen to begin with the
138 same few characters will be incorrectly gathered.
139
140 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
141 comparing subjects."
142   :group 'gnus-thread
143   :type '(choice (const :tag "off" nil)
144                  (const fuzzy)
145                  (sexp :menu-tag "on" t)))
146
147 (defcustom gnus-simplify-subject-functions nil
148   "List of functions taking a string argument that simplify subjects.
149 The functions are applied recursively.
150
151 Useful functions to put in this list include: `gnus-simplify-subject-re',
152 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
153   :group 'gnus-thread
154   :type '(repeat function))
155
156 (defcustom gnus-simplify-ignored-prefixes nil
157   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
158   :group 'gnus-thread
159   :type '(choice (const :tag "off" nil)
160                  regexp))
161
162 (defcustom gnus-build-sparse-threads nil
163   "*If non-nil, fill in the gaps in threads.
164 If `some', only fill in the gaps that are needed to tie loose threads
165 together.  If `more', fill in all leaf nodes that Gnus can find.  If
166 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
167   :group 'gnus-thread
168   :type '(choice (const :tag "off" nil)
169                  (const some)
170                  (const more)
171                  (sexp :menu-tag "all" t)))
172
173 (defcustom gnus-summary-thread-gathering-function
174   'gnus-gather-threads-by-subject
175   "*Function used for gathering loose threads.
176 There are two pre-defined functions: `gnus-gather-threads-by-subject',
177 which only takes Subjects into consideration; and
178 `gnus-gather-threads-by-references', which compared the References
179 headers of the articles to find matches."
180   :group 'gnus-thread
181   :type '(radio (function-item gnus-gather-threads-by-subject)
182                 (function-item gnus-gather-threads-by-references)
183                 (function :tag "other")))
184
185 (defcustom gnus-summary-same-subject ""
186   "*String indicating that the current article has the same subject as the previous.
187 This variable will only be used if the value of
188 `gnus-summary-make-false-root' is `empty'."
189   :group 'gnus-summary-format
190   :type 'string)
191
192 (defcustom gnus-summary-goto-unread t
193   "*If t, many commands will go to the next unread article.
194 This applies to marking commands as well as other commands that
195 \"naturally\" select the next article, like, for instance, `SPC' at
196 the end of an article.
197
198 If nil, the marking commands do NOT go to the next unread article
199 (they go to the next article instead).  If `never', commands that
200 usually go to the next unread article, will go to the next article,
201 whether it is read or not."
202   :group 'gnus-summary-marks
203   :link '(custom-manual "(gnus)Setting Marks")
204   :type '(choice (const :tag "off" nil)
205                  (const never)
206                  (sexp :menu-tag "on" t)))
207
208 (defcustom gnus-summary-default-score 0
209   "*Default article score level.
210 All scores generated by the score files will be added to this score.
211 If this variable is nil, scoring will be disabled."
212   :group 'gnus-score-default
213   :type '(choice (const :tag "disable")
214                  integer))
215
216 (defcustom gnus-summary-zcore-fuzz 0
217   "*Fuzziness factor for the zcore in the summary buffer.
218 Articles with scores closer than this to `gnus-summary-default-score'
219 will not be marked."
220   :group 'gnus-summary-format
221   :type 'integer)
222
223 (defcustom gnus-simplify-subject-fuzzy-regexp nil
224   "*Strings to be removed when doing fuzzy matches.
225 This can either be a regular expression or list of regular expressions
226 that will be removed from subject strings if fuzzy subject
227 simplification is selected."
228   :group 'gnus-thread
229   :type '(repeat regexp))
230
231 (defcustom gnus-show-threads t
232   "*If non-nil, display threads in summary mode."
233   :group 'gnus-thread
234   :type 'boolean)
235
236 (defcustom gnus-thread-hide-subtree nil
237   "*If non-nil, hide all threads initially.
238 If threads are hidden, you have to run the command
239 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
240 to expose hidden threads."
241   :group 'gnus-thread
242   :type 'boolean)
243
244 (defcustom gnus-thread-hide-killed t
245   "*If non-nil, hide killed threads automatically."
246   :group 'gnus-thread
247   :type 'boolean)
248
249 (defcustom gnus-thread-ignore-subject t
250   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
251 If nil, articles that have different subjects from their parents will
252 start separate threads."
253   :group 'gnus-thread
254   :type 'boolean)
255
256 (defcustom gnus-thread-operation-ignore-subject t
257   "*If non-nil, subjects will be ignored when doing thread commands.
258 This affects commands like `gnus-summary-kill-thread' and
259 `gnus-summary-lower-thread'.
260
261 If this variable is nil, articles in the same thread with different
262 subjects will not be included in the operation in question.  If this
263 variable is `fuzzy', only articles that have subjects that are fuzzily
264 equal will be included."
265   :group 'gnus-thread
266   :type '(choice (const :tag "off" nil)
267                  (const fuzzy)
268                  (sexp :tag "on" t)))
269
270 (defcustom gnus-thread-indent-level 4
271   "*Number that says how much each sub-thread should be indented."
272   :group 'gnus-thread
273   :type 'integer)
274
275 (defcustom gnus-auto-extend-newsgroup t
276   "*If non-nil, extend newsgroup forward and backward when requested."
277   :group 'gnus-summary-choose
278   :type 'boolean)
279
280 (defcustom gnus-auto-select-first t
281   "*If nil, don't select the first unread article when entering a group.
282 If this variable is `best', select the highest-scored unread article
283 in the group.  If t, select the first unread article.
284
285 This variable can also be a function to place point on a likely
286 subject line.  Useful values include `gnus-summary-first-unread-subject',
287 `gnus-summary-first-unread-article' and
288 `gnus-summary-best-unread-article'.
289
290 If you want to prevent automatic selection of the first unread article
291 in some newsgroups, set the variable to nil in
292 `gnus-select-group-hook'."
293   :group 'gnus-group-select
294   :type '(choice (const :tag "none" nil)
295                  (const best)
296                  (sexp :menu-tag "first" t)
297                  (function-item gnus-summary-first-unread-subject)
298                  (function-item gnus-summary-first-unread-article)
299                  (function-item gnus-summary-best-unread-article)))
300
301 (defcustom gnus-dont-select-after-jump-to-other-group nil
302   "If non-nil, don't select the first unread article after entering the
303 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
304 it is depend on the value of `gnus-auto-select-first' whether to select
305 or not."
306   :group 'gnus-group-select
307   :type 'boolean)
308
309 (defcustom gnus-auto-select-next t
310   "*If non-nil, offer to go to the next group from the end of the previous.
311 If the value is t and the next newsgroup is empty, Gnus will exit
312 summary mode and go back to group mode.  If the value is neither nil
313 nor t, Gnus will select the following unread newsgroup.  In
314 particular, if the value is the symbol `quietly', the next unread
315 newsgroup will be selected without any confirmation, and if it is
316 `almost-quietly', the next group will be selected without any
317 confirmation if you are located on the last article in the group.
318 Finally, if this variable is `slightly-quietly', the `Z n' command
319 will go to the next group without confirmation."
320   :group 'gnus-summary-maneuvering
321   :type '(choice (const :tag "off" nil)
322                  (const quietly)
323                  (const almost-quietly)
324                  (const slightly-quietly)
325                  (sexp :menu-tag "on" t)))
326
327 (defcustom gnus-auto-select-same nil
328   "*If non-nil, select the next article with the same subject.
329 If there are no more articles with the same subject, go to
330 the first unread article."
331   :group 'gnus-summary-maneuvering
332   :type 'boolean)
333
334 (defcustom gnus-summary-check-current nil
335   "*If non-nil, consider the current article when moving.
336 The \"unread\" movement commands will stay on the same line if the
337 current article is unread."
338   :group 'gnus-summary-maneuvering
339   :type 'boolean)
340
341 (defcustom gnus-auto-center-summary t
342   "*If non-nil, always center the current summary buffer.
343 In particular, if `vertical' do only vertical recentering.  If non-nil
344 and non-`vertical', do both horizontal and vertical recentering."
345   :group 'gnus-summary-maneuvering
346   :type '(choice (const :tag "none" nil)
347                  (const vertical)
348                  (integer :tag "height")
349                  (sexp :menu-tag "both" t)))
350
351 (defcustom gnus-show-all-headers nil
352   "*If non-nil, don't hide any headers."
353   :group 'gnus-article-hiding
354   :group 'gnus-article-headers
355   :type 'boolean)
356
357 (defcustom gnus-summary-ignore-duplicates nil
358   "*If non-nil, ignore articles with identical Message-ID headers."
359   :group 'gnus-summary
360   :type 'boolean)
361
362 (defcustom gnus-single-article-buffer t
363   "*If non-nil, display all articles in the same buffer.
364 If nil, each group will get its own article buffer."
365   :group 'gnus-article-various
366   :type 'boolean)
367
368 (defcustom gnus-break-pages t
369   "*If non-nil, do page breaking on articles.
370 The page delimiter is specified by the `gnus-page-delimiter'
371 variable."
372   :group 'gnus-article-various
373   :type 'boolean)
374
375 (defcustom gnus-show-mime t
376   "*If non-nil, do mime processing of articles.
377 The articles will simply be fed to the function given by
378 `gnus-article-display-method-for-mime'."
379   :group 'gnus-article-mime
380   :type 'boolean)
381
382 (defcustom gnus-move-split-methods nil
383   "*Variable used to suggest where articles are to be moved to.
384 It uses the same syntax as the `gnus-split-methods' variable.
385 However, whereas `gnus-split-methods' specifies file names as targets,
386 this variable specifies group names."
387   :group 'gnus-summary-mail
388   :type '(repeat (choice (list :value (fun) function)
389                          (cons :value ("" "") regexp (repeat string))
390                          (sexp :value nil))))
391
392 (defcustom gnus-unread-mark ?  ;Whitespace
393   "*Mark used for unread articles."
394   :group 'gnus-summary-marks
395   :type 'character)
396
397 (defcustom gnus-ticked-mark ?!
398   "*Mark used for ticked articles."
399   :group 'gnus-summary-marks
400   :type 'character)
401
402 (defcustom gnus-dormant-mark ??
403   "*Mark used for dormant articles."
404   :group 'gnus-summary-marks
405   :type 'character)
406
407 (defcustom gnus-del-mark ?r
408   "*Mark used for del'd articles."
409   :group 'gnus-summary-marks
410   :type 'character)
411
412 (defcustom gnus-read-mark ?R
413   "*Mark used for read articles."
414   :group 'gnus-summary-marks
415   :type 'character)
416
417 (defcustom gnus-expirable-mark ?E
418   "*Mark used for expirable articles."
419   :group 'gnus-summary-marks
420   :type 'character)
421
422 (defcustom gnus-killed-mark ?K
423   "*Mark used for killed articles."
424   :group 'gnus-summary-marks
425   :type 'character)
426
427 (defcustom gnus-souped-mark ?F
428   "*Mark used for souped articles."
429   :group 'gnus-summary-marks
430   :type 'character)
431
432 (defcustom gnus-kill-file-mark ?X
433   "*Mark used for articles killed by kill files."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-low-score-mark ?Y
438   "*Mark used for articles with a low score."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-catchup-mark ?C
443   "*Mark used for articles that are caught up."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-replied-mark ?A
448   "*Mark used for articles that have been replied to."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-forwarded-mark ?O
453   "*Mark used for articles that have been forwarded."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-cached-mark ?*
458   "*Mark used for articles that are in the cache."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-saved-mark ?S
463   "*Mark used for articles that have been saved to."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-no-mark ?  ;Whitespace
468   "*Mark used for articles that have no other secondary mark."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-ancient-mark ?O
473   "*Mark used for ancient articles."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-sparse-mark ?Q
478   "*Mark used for sparsely reffed articles."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-canceled-mark ?G
483   "*Mark used for canceled articles."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-duplicate-mark ?M
488   "*Mark used for duplicate articles."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-undownloaded-mark ?@
493   "*Mark used for articles that weren't downloaded."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-downloadable-mark ?%
498   "*Mark used for articles that are to be downloaded."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-unsendable-mark ?=
503   "*Mark used for articles that won't be sent."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-score-over-mark ?+
508   "*Score mark used for articles with high scores."
509   :group 'gnus-summary-marks
510   :type 'character)
511
512 (defcustom gnus-score-below-mark ?-
513   "*Score mark used for articles with low scores."
514   :group 'gnus-summary-marks
515   :type 'character)
516
517 (defcustom gnus-empty-thread-mark ?  ;Whitespace
518   "*There is no thread under the article."
519   :group 'gnus-summary-marks
520   :type 'character)
521
522 (defcustom gnus-not-empty-thread-mark ?=
523   "*There is a thread under the article."
524   :group 'gnus-summary-marks
525   :type 'character)
526
527 (defcustom gnus-view-pseudo-asynchronously nil
528   "*If non-nil, Gnus will view pseudo-articles asynchronously."
529   :group 'gnus-extract-view
530   :type 'boolean)
531
532 (defcustom gnus-auto-expirable-marks
533   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
534         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
535         gnus-souped-mark gnus-duplicate-mark)
536   "*The list of marks converted into expiration if a group is auto-expirable."
537   :version "21.1"
538   :group 'gnus-summary
539   :type '(repeat character))
540
541 (defcustom gnus-inhibit-user-auto-expire t
542   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
543   :version "21.1"
544   :group 'gnus-summary
545   :type 'boolean)
546
547 (defcustom gnus-view-pseudos nil
548   "*If `automatic', pseudo-articles will be viewed automatically.
549 If `not-confirm', pseudos will be viewed automatically, and the user
550 will not be asked to confirm the command."
551   :group 'gnus-extract-view
552   :type '(choice (const :tag "off" nil)
553                  (const automatic)
554                  (const not-confirm)))
555
556 (defcustom gnus-view-pseudos-separately t
557   "*If non-nil, one pseudo-article will be created for each file to be viewed.
558 If nil, all files that use the same viewing command will be given as a
559 list of parameters to that command."
560   :group 'gnus-extract-view
561   :type 'boolean)
562
563 (defcustom gnus-insert-pseudo-articles t
564   "*If non-nil, insert pseudo-articles when decoding articles."
565   :group 'gnus-extract-view
566   :type 'boolean)
567
568 (defcustom gnus-summary-dummy-line-format
569   "  %(:                          :%) %S\n"
570   "*The format specification for the dummy roots in the summary buffer.
571 It works along the same lines as a normal formatting string,
572 with some simple extensions.
573
574 %S  The subject"
575   :group 'gnus-threading
576   :type 'string)
577
578 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
579   "*The format specification for the summary mode line.
580 It works along the same lines as a normal formatting string,
581 with some simple extensions:
582
583 %G  Group name
584 %p  Unprefixed group name
585 %A  Current article number
586 %z  Current article score
587 %V  Gnus version
588 %U  Number of unread articles in the group
589 %e  Number of unselected articles in the group
590 %Z  A string with unread/unselected article counts
591 %g  Shortish group name
592 %S  Subject of the current article
593 %u  User-defined spec
594 %s  Current score file name
595 %d  Number of dormant articles
596 %r  Number of articles that have been marked as read in this session
597 %E  Number of articles expunged by the score files"
598   :group 'gnus-summary-format
599   :type 'string)
600
601 (defcustom gnus-list-identifiers nil
602   "Regexp that matches list identifiers to be removed from subject.
603 This can also be a list of regexps."
604   :version "21.1"
605   :group 'gnus-summary-format
606   :group 'gnus-article-hiding
607   :type '(choice (const :tag "none" nil)
608                  (regexp :value ".*")
609                  (repeat :value (".*") regexp)))
610
611 (defcustom gnus-summary-mark-below 0
612   "*Mark all articles with a score below this variable as read.
613 This variable is local to each summary buffer and usually set by the
614 score file."
615   :group 'gnus-score-default
616   :type 'integer)
617
618 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
619   "*List of functions used for sorting articles in the summary buffer.
620
621 Each function takes two articles and returns non-nil if the first
622 article should be sorted before the other.  If you use more than one
623 function, the primary sort function should be the last.  You should
624 probably always include `gnus-article-sort-by-number' in the list of
625 sorting functions -- preferably first.  Also note that sorting by date
626 is often much slower than sorting by number, and the sorting order is
627 very similar.  (Sorting by date means sorting by the time the message
628 was sent, sorting by number means sorting by arrival time.)
629
630 Ready-made functions include `gnus-article-sort-by-number',
631 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
632 `gnus-article-sort-by-date' and `gnus-article-sort-by-score'.
633
634 When threading is turned on, the variable `gnus-thread-sort-functions'
635 controls how articles are sorted."
636   :group 'gnus-summary-sort
637   :type '(repeat (choice (function-item gnus-article-sort-by-number)
638                          (function-item gnus-article-sort-by-author)
639                          (function-item gnus-article-sort-by-subject)
640                          (function-item gnus-article-sort-by-date)
641                          (function-item gnus-article-sort-by-score)
642                          (function :tag "other"))))
643
644 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
645   "*List of functions used for sorting threads in the summary buffer.
646 By default, threads are sorted by article number.
647
648 Each function takes two threads and returns non-nil if the first
649 thread should be sorted before the other.  If you use more than one
650 function, the primary sort function should be the last.  You should
651 probably always include `gnus-thread-sort-by-number' in the list of
652 sorting functions -- preferably first.  Also note that sorting by date
653 is often much slower than sorting by number, and the sorting order is
654 very similar.  (Sorting by date means sorting by the time the message
655 was sent, sorting by number means sorting by arrival time.)
656
657 Ready-made functions include `gnus-thread-sort-by-number',
658 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
659 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
660 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
661
662 When threading is turned off, the variable
663 `gnus-article-sort-functions' controls how articles are sorted."
664   :group 'gnus-summary-sort
665   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
666                          (function-item gnus-thread-sort-by-author)
667                          (function-item gnus-thread-sort-by-subject)
668                          (function-item gnus-thread-sort-by-date)
669                          (function-item gnus-thread-sort-by-score)
670                          (function-item gnus-thread-sort-by-total-score)
671                          (function :tag "other"))))
672
673 (defcustom gnus-thread-score-function '+
674   "*Function used for calculating the total score of a thread.
675
676 The function is called with the scores of the article and each
677 subthread and should then return the score of the thread.
678
679 Some functions you can use are `+', `max', or `min'."
680   :group 'gnus-summary-sort
681   :type 'function)
682
683 (defcustom gnus-summary-expunge-below nil
684   "All articles that have a score less than this variable will be expunged.
685 This variable is local to the summary buffers."
686   :group 'gnus-score-default
687   :type '(choice (const :tag "off" nil)
688                  integer))
689
690 (defcustom gnus-thread-expunge-below nil
691   "All threads that have a total score less than this variable will be expunged.
692 See `gnus-thread-score-function' for en explanation of what a
693 \"thread score\" is.
694
695 This variable is local to the summary buffers."
696   :group 'gnus-threading
697   :group 'gnus-score-default
698   :type '(choice (const :tag "off" nil)
699                  integer))
700
701 (defcustom gnus-summary-mode-hook nil
702   "*A hook for Gnus summary mode.
703 This hook is run before any variables are set in the summary buffer."
704   :options '(turn-on-gnus-mailing-list-mode)
705   :group 'gnus-summary-various
706   :type 'hook)
707
708 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
709 (when (featurep 'xemacs)
710   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
711   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
712   (add-hook 'gnus-summary-mode-hook
713             'gnus-xmas-switch-horizontal-scrollbar-off))
714
715 (defcustom gnus-summary-menu-hook nil
716   "*Hook run after the creation of the summary mode menu."
717   :group 'gnus-summary-visual
718   :type 'hook)
719
720 (defcustom gnus-summary-exit-hook nil
721   "*A hook called on exit from the summary buffer.
722 It will be called with point in the group buffer."
723   :group 'gnus-summary-exit
724   :type 'hook)
725
726 (defcustom gnus-summary-prepare-hook nil
727   "*A hook called after the summary buffer has been generated.
728 If you want to modify the summary buffer, you can use this hook."
729   :group 'gnus-summary-various
730   :type 'hook)
731
732 (defcustom gnus-summary-prepared-hook nil
733   "*A hook called as the last thing after the summary buffer has been generated."
734   :group 'gnus-summary-various
735   :type 'hook)
736
737 (defcustom gnus-summary-generate-hook nil
738   "*A hook run just before generating the summary buffer.
739 This hook is commonly used to customize threading variables and the
740 like."
741   :group 'gnus-summary-various
742   :type 'hook)
743
744 (defcustom gnus-select-group-hook nil
745   "*A hook called when a newsgroup is selected.
746
747 If you'd like to simplify subjects like the
748 `gnus-summary-next-same-subject' command does, you can use the
749 following hook:
750
751  (setq gnus-select-group-hook
752       (list
753         (lambda ()
754           (mapcar (lambda (header)
755                      (mail-header-set-subject
756                       header
757                       (gnus-simplify-subject
758                        (mail-header-subject header) 're-only)))
759                   gnus-newsgroup-headers))))"
760   :group 'gnus-group-select
761   :type 'hook)
762
763 (defcustom gnus-select-article-hook nil
764   "*A hook called when an article is selected."
765   :group 'gnus-summary-choose
766   :type 'hook)
767
768 (defcustom gnus-visual-mark-article-hook
769   (list 'gnus-highlight-selected-summary)
770   "*Hook run after selecting an article in the summary buffer.
771 It is meant to be used for highlighting the article in some way.  It
772 is not run if `gnus-visual' is nil."
773   :group 'gnus-summary-visual
774   :type 'hook)
775
776 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
777   "*A hook called before parsing the headers."
778   :group 'gnus-various
779   :type 'hook)
780
781 (defcustom gnus-exit-group-hook nil
782   "*A hook called when exiting summary mode.
783 This hook is not called from the non-updating exit commands like `Q'."
784   :group 'gnus-various
785   :type 'hook)
786
787 (defcustom gnus-summary-update-hook
788   (list 'gnus-summary-highlight-line)
789   "*A hook called when a summary line is changed.
790 The hook will not be called if `gnus-visual' is nil.
791
792 The default function `gnus-summary-highlight-line' will
793 highlight the line according to the `gnus-summary-highlight'
794 variable."
795   :group 'gnus-summary-visual
796   :type 'hook)
797
798 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
799   "*A hook called when an article is selected for the first time.
800 The hook is intended to mark an article as read (or unread)
801 automatically when it is selected."
802   :group 'gnus-summary-choose
803   :type 'hook)
804
805 (defcustom gnus-group-no-more-groups-hook nil
806   "*A hook run when returning to group mode having no more (unread) groups."
807   :group 'gnus-group-select
808   :type 'hook)
809
810 (defcustom gnus-ps-print-hook nil
811   "*A hook run before ps-printing something from Gnus."
812   :group 'gnus-summary
813   :type 'hook)
814
815 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
816   "Face used for highlighting the current article in the summary buffer."
817   :group 'gnus-summary-visual
818   :type 'face)
819
820 (defcustom gnus-summary-highlight
821   '(((= mark gnus-canceled-mark)
822      . gnus-summary-cancelled-face)
823     ((and (> score default)
824           (or (= mark gnus-dormant-mark)
825               (= mark gnus-ticked-mark)))
826      . gnus-summary-high-ticked-face)
827     ((and (< score default)
828           (or (= mark gnus-dormant-mark)
829               (= mark gnus-ticked-mark)))
830      . gnus-summary-low-ticked-face)
831     ((or (= mark gnus-dormant-mark)
832          (= mark gnus-ticked-mark))
833      . gnus-summary-normal-ticked-face)
834     ((and (> score default) (= mark gnus-ancient-mark))
835      . gnus-summary-high-ancient-face)
836     ((and (< score default) (= mark gnus-ancient-mark))
837      . gnus-summary-low-ancient-face)
838     ((= mark gnus-ancient-mark)
839      . gnus-summary-normal-ancient-face)
840     ((and (> score default) (= mark gnus-unread-mark))
841      . gnus-summary-high-unread-face)
842     ((and (< score default) (= mark gnus-unread-mark))
843      . gnus-summary-low-unread-face)
844     ((and (memq article gnus-newsgroup-incorporated)
845           (= mark gnus-unread-mark))
846      . gnus-summary-incorporated-face)
847     ((= mark gnus-unread-mark)
848      . gnus-summary-normal-unread-face)
849     ((and (> score default) (memq mark (list gnus-downloadable-mark
850                                              gnus-undownloaded-mark)))
851      . gnus-summary-high-unread-face)
852     ((and (< score default) (memq mark (list gnus-downloadable-mark
853                                              gnus-undownloaded-mark)))
854      . gnus-summary-low-unread-face)
855     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
856      . gnus-summary-normal-unread-face)
857     ((> score default)
858      . gnus-summary-high-read-face)
859     ((< score default)
860      . gnus-summary-low-read-face)
861     (t
862      . gnus-summary-normal-read-face))
863   "*Controls the highlighting of summary buffer lines.
864
865 A list of (FORM . FACE) pairs.  When deciding how a a particular
866 summary line should be displayed, each form is evaluated.  The content
867 of the face field after the first true form is used.  You can change
868 how those summary lines are displayed, by editing the face field.
869
870 You can use the following variables in the FORM field.
871
872 score:   The articles score
873 default: The default article score.
874 below:   The score below which articles are automatically marked as read.
875 mark:    The articles mark."
876   :group 'gnus-summary-visual
877   :type '(repeat (cons (sexp :tag "Form" nil)
878                        face)))
879
880 (defcustom gnus-alter-header-function nil
881   "Function called to allow alteration of article header structures.
882 The function is called with one parameter, the article header vector,
883 which it may alter in any way.")
884
885 (defvar gnus-decode-encoded-word-function
886   (mime-find-field-decoder 'From 'nov)
887   "Variable that says which function should be used to decode a string with encoded words.")
888
889 (defcustom gnus-extra-headers nil
890   "*Extra headers to parse."
891   :version "21.1"
892   :group 'gnus-summary
893   :type '(repeat symbol))
894
895 (defcustom gnus-ignored-from-addresses
896   (and user-mail-address (regexp-quote user-mail-address))
897   "*Regexp of From headers that may be suppressed in favor of To headers."
898   :version "21.1"
899   :group 'gnus-summary
900   :type 'regexp)
901
902 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
903   "List of charsets that should be ignored.
904 When these charsets are used in the \"charset\" parameter, the
905 default charset will be used instead."
906   :version "21.1"
907   :type '(repeat symbol)
908   :group 'gnus-charset)
909
910 (gnus-define-group-parameter
911  ignored-charsets
912  :type list
913  :function-document
914  "Return the ignored charsets of GROUP."
915  :variable gnus-group-ignored-charsets-alist
916  :variable-default 
917  '(("alt\\.chinese\\.text" iso-8859-1))
918  :variable-document
919  "Alist of regexps (to match group names) and charsets that should be ignored.
920 When these charsets are used in the \"charset\" parameter, the
921 default charset will be used instead."
922  :variable-group gnus-charset
923  :variable-type '(repeat (cons (regexp :tag "Group")
924                                (repeat symbol)))
925  :parameter-type '(choice :tag "Ignored charsets" 
926                           :value nil
927                           (repeat (symbol)))
928  :parameter-document       "\
929 List of charsets that should be ignored.
930
931 When these charsets are used in the \"charset\" parameter, the
932 default charset will be used instead.")
933
934 (defcustom gnus-group-highlight-words-alist nil
935   "Alist of group regexps and highlight regexps.
936 This variable uses the same syntax as `gnus-emphasis-alist'."
937   :version "21.1"
938   :type '(repeat (cons (regexp :tag "Group")
939                        (repeat (list (regexp :tag "Highlight regexp")
940                                      (number :tag "Group for entire word" 0)
941                                      (number :tag "Group for displayed part" 0)
942                                      (symbol :tag "Face"
943                                              gnus-emphasis-highlight-words)))))
944   :group 'gnus-summary-visual)
945
946 (defcustom gnus-use-wheel nil
947   "Use Intelli-mouse on summary movement"
948   :type 'boolean
949   :group 'gnus-summary-maneuvering)
950
951 (defcustom gnus-wheel-scroll-amount '(5 . 1)
952   "Amount to scroll messages by spinning the mouse wheel.
953 This is actually a cons cell, where the first item is the amount to scroll
954 on a normal wheel event, and the second is the amount to scroll when the
955 wheel is moved with the shift key depressed."
956   :type '(cons (integer :tag "Shift") integer)
957   :group 'gnus-summary-maneuvering)
958
959 (defcustom gnus-wheel-edge-resistance 2
960   "How hard it should be to change the current article
961 by moving the mouse over the edge of the article window."
962   :type 'integer
963   :group 'gnus-summary-maneuvering)
964
965 (defcustom gnus-summary-show-article-charset-alist
966   nil
967   "Alist of number and charset.
968 The article will be shown with the charset corresponding to the
969 numbered argument.
970 For example: ((1 . cn-gb-2312) (2 . big5))."
971   :version "21.1"
972   :type '(repeat (cons (number :tag "Argument" 1)
973                        (symbol :tag "Charset")))
974   :group 'gnus-charset)
975
976 (defcustom gnus-preserve-marks t
977   "Whether marks are preserved when moving, copying and respooling messages."
978   :version "21.1"
979   :type 'boolean
980   :group 'gnus-summary-marks)
981
982 (defcustom gnus-alter-articles-to-read-function nil
983   "Function to be called to alter the list of articles to be selected."
984   :type '(choice (const nil) function)
985   :group 'gnus-summary)
986
987 (defcustom gnus-orphan-score nil
988   "*All orphans get this score added.  Set in the score file."
989   :group 'gnus-score-default
990   :type '(choice (const nil)
991                  integer))
992
993 (defcustom gnus-summary-save-parts-default-mime "image/.*"
994   "*A regexp to match MIME parts when saving multiple parts of a message
995 with gnus-summary-save-parts (X m). This regexp will be used by default
996 when prompting the user for which type of files to save."
997   :group 'gnus-summary
998   :type 'regexp)
999
1000
1001 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1002   "*A regexp to match MIME parts when saving multiple parts of a message
1003 with gnus-summary-save-parts (X m). This regexp will be used by default
1004 when prompting the user for which type of files to save."
1005   :group 'gnus-summary
1006   :type 'regexp)
1007
1008
1009 ;;; Internal variables
1010
1011 (defvar gnus-article-mime-handles nil)
1012 (defvar gnus-article-decoded-p nil)
1013 (defvar gnus-article-charset nil)
1014 (defvar gnus-article-ignored-charsets nil)
1015 (defvar gnus-scores-exclude-files nil)
1016 (defvar gnus-page-broken nil)
1017 (defvar gnus-inhibit-mime-unbuttonizing nil)
1018
1019 (defvar gnus-original-article nil)
1020 (defvar gnus-article-internal-prepare-hook nil)
1021 (defvar gnus-newsgroup-process-stack nil)
1022
1023 (defvar gnus-thread-indent-array nil)
1024 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1025 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1026   "Function called to sort the articles within a thread after it has been gathered together.")
1027
1028 (defvar gnus-summary-save-parts-type-history nil)
1029 (defvar gnus-summary-save-parts-last-directory nil)
1030
1031 (defvar gnus-summary-save-parts-type-history nil)
1032 (defvar gnus-summary-save-parts-last-directory nil)
1033
1034 ;; Avoid highlighting in kill files.
1035 (defvar gnus-summary-inhibit-highlight nil)
1036 (defvar gnus-newsgroup-selected-overlay nil)
1037 (defvar gnus-inhibit-limiting nil)
1038 (defvar gnus-newsgroup-adaptive-score-file nil)
1039 (defvar gnus-current-score-file nil)
1040 (defvar gnus-current-move-group nil)
1041 (defvar gnus-current-copy-group nil)
1042 (defvar gnus-current-crosspost-group nil)
1043
1044 (defvar gnus-newsgroup-dependencies nil)
1045 (defvar gnus-newsgroup-adaptive nil)
1046 (defvar gnus-summary-display-article-function nil)
1047 (defvar gnus-summary-highlight-line-function nil
1048   "Function called after highlighting a summary line.")
1049
1050 (defvar gnus-summary-line-format-alist
1051   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1052     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1053     (?s gnus-tmp-subject-or-nil ?s)
1054     (?n gnus-tmp-name ?s)
1055     (?A (std11-address-string
1056          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1057     (?a (or (std11-full-name-string
1058              (car (mime-entity-read-field gnus-tmp-header 'From)))
1059             gnus-tmp-from) ?s)
1060     (?F gnus-tmp-from ?s)
1061     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1062     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1063     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1064     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1065     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1066     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1067     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1068     (?L gnus-tmp-lines ?s)
1069     (?I gnus-tmp-indentation ?s)
1070     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1071     (?R gnus-tmp-replied ?c)
1072     (?\[ gnus-tmp-opening-bracket ?c)
1073     (?\] gnus-tmp-closing-bracket ?c)
1074     (?\> (make-string gnus-tmp-level ? ) ?s)
1075     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1076     (?i gnus-tmp-score ?d)
1077     (?z gnus-tmp-score-char ?c)
1078     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1079     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1080     (?U gnus-tmp-unread ?c)
1081     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1082     (?t (gnus-summary-number-of-articles-in-thread
1083          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1084         ?d)
1085     (?e (gnus-summary-number-of-articles-in-thread
1086          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1087         ?c)
1088     (?u gnus-tmp-user-defined ?s)
1089     (?P (gnus-pick-line-number) ?d))
1090   "An alist of format specifications that can appear in summary lines.
1091 These are paired with what variables they correspond with, along with
1092 the type of the variable (string, integer, character, etc).")
1093
1094 (defvar gnus-summary-dummy-line-format-alist
1095   `((?S gnus-tmp-subject ?s)
1096     (?N gnus-tmp-number ?d)
1097     (?u gnus-tmp-user-defined ?s)))
1098
1099 (defvar gnus-summary-mode-line-format-alist
1100   `((?G gnus-tmp-group-name ?s)
1101     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1102     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1103     (?A gnus-tmp-article-number ?d)
1104     (?Z gnus-tmp-unread-and-unselected ?s)
1105     (?V gnus-version ?s)
1106     (?U gnus-tmp-unread-and-unticked ?d)
1107     (?S gnus-tmp-subject ?s)
1108     (?e gnus-tmp-unselected ?d)
1109     (?u gnus-tmp-user-defined ?s)
1110     (?d (length gnus-newsgroup-dormant) ?d)
1111     (?t (length gnus-newsgroup-marked) ?d)
1112     (?r (length gnus-newsgroup-reads) ?d)
1113     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1114     (?E gnus-newsgroup-expunged-tally ?d)
1115     (?s (gnus-current-score-file-nondirectory) ?s)))
1116
1117 (defvar gnus-last-search-regexp nil
1118   "Default regexp for article search command.")
1119
1120 (defvar gnus-summary-search-article-matched-data nil
1121   "Last matched data of article search command.  It is the local variable
1122 in `gnus-article-buffer' which consists of the list of start position,
1123 end position and text.")
1124
1125 (defvar gnus-last-shell-command nil
1126   "Default shell command on article.")
1127
1128 (defvar gnus-newsgroup-begin nil)
1129 (defvar gnus-newsgroup-end nil)
1130 (defvar gnus-newsgroup-last-rmail nil)
1131 (defvar gnus-newsgroup-last-mail nil)
1132 (defvar gnus-newsgroup-last-folder nil)
1133 (defvar gnus-newsgroup-last-file nil)
1134 (defvar gnus-newsgroup-auto-expire nil)
1135 (defvar gnus-newsgroup-active nil)
1136
1137 (defvar gnus-newsgroup-data nil)
1138 (defvar gnus-newsgroup-data-reverse nil)
1139 (defvar gnus-newsgroup-limit nil)
1140 (defvar gnus-newsgroup-limits nil)
1141
1142 (defvar gnus-newsgroup-unreads nil
1143   "List of unread articles in the current newsgroup.")
1144
1145 (defvar gnus-newsgroup-unselected nil
1146   "List of unselected unread articles in the current newsgroup.")
1147
1148 (defvar gnus-newsgroup-reads nil
1149   "Alist of read articles and article marks in the current newsgroup.")
1150
1151 (defvar gnus-newsgroup-expunged-tally nil)
1152
1153 (defvar gnus-newsgroup-marked nil
1154   "List of ticked articles in the current newsgroup (a subset of unread art).")
1155
1156 (defvar gnus-newsgroup-killed nil
1157   "List of ranges of articles that have been through the scoring process.")
1158
1159 (defvar gnus-newsgroup-cached nil
1160   "List of articles that come from the article cache.")
1161
1162 (defvar gnus-newsgroup-saved nil
1163   "List of articles that have been saved.")
1164
1165 (defvar gnus-newsgroup-kill-headers nil)
1166
1167 (defvar gnus-newsgroup-replied nil
1168   "List of articles that have been replied to in the current newsgroup.")
1169
1170 (defvar gnus-newsgroup-forwarded nil
1171   "List of articles that have been forwarded in the current newsgroup.")
1172
1173 (defvar gnus-newsgroup-expirable nil
1174   "List of articles in the current newsgroup that can be expired.")
1175
1176 (defvar gnus-newsgroup-processable nil
1177   "List of articles in the current newsgroup that can be processed.")
1178
1179 (defvar gnus-newsgroup-downloadable nil
1180   "List of articles in the current newsgroup that can be processed.")
1181
1182 (defvar gnus-newsgroup-undownloaded nil
1183   "List of articles in the current newsgroup that haven't been downloaded..")
1184
1185 (defvar gnus-newsgroup-unsendable nil
1186   "List of articles in the current newsgroup that won't be sent.")
1187
1188 (defvar gnus-newsgroup-bookmarks nil
1189   "List of articles in the current newsgroup that have bookmarks.")
1190
1191 (defvar gnus-newsgroup-dormant nil
1192   "List of dormant articles in the current newsgroup.")
1193
1194 (defvar gnus-newsgroup-scored nil
1195   "List of scored articles in the current newsgroup.")
1196
1197 (defvar gnus-newsgroup-incorporated nil
1198   "List of incorporated articles in the current newsgroup.")
1199
1200 (defvar gnus-newsgroup-headers nil
1201   "List of article headers in the current newsgroup.")
1202
1203 (defvar gnus-newsgroup-threads nil)
1204
1205 (defvar gnus-newsgroup-prepared nil
1206   "Whether the current group has been prepared properly.")
1207
1208 (defvar gnus-newsgroup-ancient nil
1209   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1210
1211 (defvar gnus-newsgroup-sparse nil)
1212
1213 (defvar gnus-current-article nil)
1214 (defvar gnus-article-current nil)
1215 (defvar gnus-current-headers nil)
1216 (defvar gnus-have-all-headers nil)
1217 (defvar gnus-last-article nil)
1218 (defvar gnus-newsgroup-history nil)
1219 (defvar gnus-newsgroup-charset nil)
1220 (defvar gnus-newsgroup-ephemeral-charset nil)
1221 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1222
1223 (defvar gnus-article-before-search nil)
1224
1225 (defconst gnus-summary-local-variables
1226   '(gnus-newsgroup-name
1227     gnus-newsgroup-begin gnus-newsgroup-end
1228     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1229     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1230     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1231     gnus-newsgroup-unselected gnus-newsgroup-marked
1232     gnus-newsgroup-reads gnus-newsgroup-saved
1233     gnus-newsgroup-replied gnus-newsgroup-forwarded
1234     gnus-newsgroup-expirable
1235     gnus-newsgroup-processable gnus-newsgroup-killed
1236     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1237     gnus-newsgroup-unsendable
1238     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1239     gnus-newsgroup-headers gnus-newsgroup-threads
1240     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1241     gnus-current-article gnus-current-headers gnus-have-all-headers
1242     gnus-last-article gnus-article-internal-prepare-hook
1243     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1244     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1245     gnus-thread-expunge-below
1246     gnus-score-alist gnus-current-score-file
1247     (gnus-summary-expunge-below . global)
1248     (gnus-summary-mark-below . global)
1249     (gnus-orphan-score . global)
1250     gnus-newsgroup-active gnus-scores-exclude-files
1251     gnus-newsgroup-history gnus-newsgroup-ancient
1252     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1253     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1254     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1255     (gnus-newsgroup-expunged-tally . 0)
1256     gnus-cache-removable-articles gnus-newsgroup-cached
1257     gnus-newsgroup-data gnus-newsgroup-data-reverse
1258     gnus-newsgroup-limit gnus-newsgroup-limits
1259     gnus-newsgroup-charset
1260     gnus-newsgroup-incorporated)
1261   "Variables that are buffer-local to the summary buffers.")
1262
1263 (defvar gnus-newsgroup-variables nil
1264   "Variables that have separate values in the newsgroups.")
1265
1266 ;; Byte-compiler warning.
1267 (eval-when-compile (defvar gnus-article-mode-map))
1268
1269 ;; Subject simplification.
1270
1271 (defun gnus-simplify-whitespace (str)
1272   "Remove excessive whitespace from STR."
1273   (let ((mystr str))
1274     ;; Multiple spaces.
1275     (while (string-match "[ \t][ \t]+" mystr)
1276       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1277                           " "
1278                           (substring mystr (match-end 0)))))
1279     ;; Leading spaces.
1280     (when (string-match "^[ \t]+" mystr)
1281       (setq mystr (substring mystr (match-end 0))))
1282     ;; Trailing spaces.
1283     (when (string-match "[ \t]+$" mystr)
1284       (setq mystr (substring mystr 0 (match-beginning 0))))
1285     mystr))
1286
1287 (defsubst gnus-simplify-subject-re (subject)
1288   "Remove \"Re:\" from subject lines."
1289   (if (string-match message-subject-re-regexp subject)
1290       (substring subject (match-end 0))
1291     subject))
1292
1293 (defun gnus-simplify-subject (subject &optional re-only)
1294   "Remove `Re:' and words in parentheses.
1295 If RE-ONLY is non-nil, strip leading `Re:'s only."
1296   (let ((case-fold-search t))           ;Ignore case.
1297     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1298     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1299       (setq subject (substring subject (match-end 0))))
1300     ;; Remove uninteresting prefixes.
1301     (when (and (not re-only)
1302                gnus-simplify-ignored-prefixes
1303                (string-match gnus-simplify-ignored-prefixes subject))
1304       (setq subject (substring subject (match-end 0))))
1305     ;; Remove words in parentheses from end.
1306     (unless re-only
1307       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1308         (setq subject (substring subject 0 (match-beginning 0)))))
1309     ;; Return subject string.
1310     subject))
1311
1312 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1313 ;; all whitespace.
1314 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1315   (goto-char (point-min))
1316   (while (re-search-forward regexp nil t)
1317     (replace-match (or newtext ""))))
1318
1319 (defun gnus-simplify-buffer-fuzzy ()
1320   "Simplify string in the buffer fuzzily.
1321 The string in the accessible portion of the current buffer is simplified.
1322 It is assumed to be a single-line subject.
1323 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1324 matter is removed.  Additional things can be deleted by setting
1325 `gnus-simplify-subject-fuzzy-regexp'."
1326   (let ((case-fold-search t)
1327         (modified-tick))
1328     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1329
1330     (while (not (eq modified-tick (buffer-modified-tick)))
1331       (setq modified-tick (buffer-modified-tick))
1332       (cond
1333        ((listp gnus-simplify-subject-fuzzy-regexp)
1334         (mapcar 'gnus-simplify-buffer-fuzzy-step
1335                 gnus-simplify-subject-fuzzy-regexp))
1336        (gnus-simplify-subject-fuzzy-regexp
1337         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1338       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1339       (gnus-simplify-buffer-fuzzy-step
1340        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1341       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1342
1343     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1344     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1345     (gnus-simplify-buffer-fuzzy-step " $")
1346     (gnus-simplify-buffer-fuzzy-step "^ +")))
1347
1348 (defun gnus-simplify-subject-fuzzy (subject)
1349   "Simplify a subject string fuzzily.
1350 See `gnus-simplify-buffer-fuzzy' for details."
1351   (save-excursion
1352     (gnus-set-work-buffer)
1353     (let ((case-fold-search t))
1354       ;; Remove uninteresting prefixes.
1355       (when (and gnus-simplify-ignored-prefixes
1356                  (string-match gnus-simplify-ignored-prefixes subject))
1357         (setq subject (substring subject (match-end 0))))
1358       (insert subject)
1359       (inline (gnus-simplify-buffer-fuzzy))
1360       (buffer-string))))
1361
1362 (defsubst gnus-simplify-subject-fully (subject)
1363   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1364   (cond
1365    (gnus-simplify-subject-functions
1366     (gnus-map-function gnus-simplify-subject-functions subject))
1367    ((null gnus-summary-gather-subject-limit)
1368     (gnus-simplify-subject-re subject))
1369    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1370     (gnus-simplify-subject-fuzzy subject))
1371    ((numberp gnus-summary-gather-subject-limit)
1372     (gnus-limit-string (gnus-simplify-subject-re subject)
1373                        gnus-summary-gather-subject-limit))
1374    (t
1375     subject)))
1376
1377 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1378   "Check whether two subjects are equal.
1379 If optional argument simple-first is t, first argument is already
1380 simplified."
1381   (cond
1382    ((null simple-first)
1383     (equal (gnus-simplify-subject-fully s1)
1384            (gnus-simplify-subject-fully s2)))
1385    (t
1386     (equal s1
1387            (gnus-simplify-subject-fully s2)))))
1388
1389 (defun gnus-summary-bubble-group ()
1390   "Increase the score of the current group.
1391 This is a handy function to add to `gnus-summary-exit-hook' to
1392 increase the score of each group you read."
1393   (gnus-group-add-score gnus-newsgroup-name))
1394
1395 \f
1396 ;;;
1397 ;;; Gnus summary mode
1398 ;;;
1399
1400 (put 'gnus-summary-mode 'mode-class 'special)
1401
1402 (defvar gnus-article-commands-menu)
1403
1404 (when t
1405   ;; Non-orthogonal keys
1406
1407   (gnus-define-keys gnus-summary-mode-map
1408     " " gnus-summary-next-page
1409     "\177" gnus-summary-prev-page
1410     [delete] gnus-summary-prev-page
1411     [backspace] gnus-summary-prev-page
1412     "\r" gnus-summary-scroll-up
1413     "\M-\r" gnus-summary-scroll-down
1414     "n" gnus-summary-next-unread-article
1415     "p" gnus-summary-prev-unread-article
1416     "N" gnus-summary-next-article
1417     "P" gnus-summary-prev-article
1418     "\M-\C-n" gnus-summary-next-same-subject
1419     "\M-\C-p" gnus-summary-prev-same-subject
1420     "\M-n" gnus-summary-next-unread-subject
1421     "\M-p" gnus-summary-prev-unread-subject
1422     "." gnus-summary-first-unread-article
1423     "," gnus-summary-best-unread-article
1424     "\M-s" gnus-summary-search-article-forward
1425     "\M-r" gnus-summary-search-article-backward
1426     "<" gnus-summary-beginning-of-article
1427     ">" gnus-summary-end-of-article
1428     "j" gnus-summary-goto-article
1429     "^" gnus-summary-refer-parent-article
1430     "\M-^" gnus-summary-refer-article
1431     "u" gnus-summary-tick-article-forward
1432     "!" gnus-summary-tick-article-forward
1433     "U" gnus-summary-tick-article-backward
1434     "d" gnus-summary-mark-as-read-forward
1435     "D" gnus-summary-mark-as-read-backward
1436     "E" gnus-summary-mark-as-expirable
1437     "\M-u" gnus-summary-clear-mark-forward
1438     "\M-U" gnus-summary-clear-mark-backward
1439     "k" gnus-summary-kill-same-subject-and-select
1440     "\C-k" gnus-summary-kill-same-subject
1441     "\M-\C-k" gnus-summary-kill-thread
1442     "\M-\C-l" gnus-summary-lower-thread
1443     "e" gnus-summary-edit-article
1444     "#" gnus-summary-mark-as-processable
1445     "\M-#" gnus-summary-unmark-as-processable
1446     "\M-\C-t" gnus-summary-toggle-threads
1447     "\M-\C-s" gnus-summary-show-thread
1448     "\M-\C-h" gnus-summary-hide-thread
1449     "\M-\C-f" gnus-summary-next-thread
1450     "\M-\C-b" gnus-summary-prev-thread
1451     [(meta down)] gnus-summary-next-thread
1452     [(meta up)] gnus-summary-prev-thread
1453     "\M-\C-u" gnus-summary-up-thread
1454     "\M-\C-d" gnus-summary-down-thread
1455     "&" gnus-summary-execute-command
1456     "c" gnus-summary-catchup-and-exit
1457     "\C-w" gnus-summary-mark-region-as-read
1458     "\C-t" gnus-summary-toggle-truncation
1459     "?" gnus-summary-mark-as-dormant
1460     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1461     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1462     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1463     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1464     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1465     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1466     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1467     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1468     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1469     "=" gnus-summary-expand-window
1470     "\C-x\C-s" gnus-summary-reselect-current-group
1471     "\M-g" gnus-summary-rescan-group
1472     "w" gnus-summary-stop-page-breaking
1473     "\C-c\C-r" gnus-summary-caesar-message
1474     "\M-t" gnus-summary-toggle-mime
1475     "f" gnus-summary-followup
1476     "F" gnus-summary-followup-with-original
1477     "C" gnus-summary-cancel-article
1478     "r" gnus-summary-reply
1479     "R" gnus-summary-reply-with-original
1480     "\C-c\C-f" gnus-summary-mail-forward
1481     "o" gnus-summary-save-article
1482     "\C-o" gnus-summary-save-article-mail
1483     "|" gnus-summary-pipe-output
1484     "\M-k" gnus-summary-edit-local-kill
1485     "\M-K" gnus-summary-edit-global-kill
1486     ;; "V" gnus-version
1487     "\C-c\C-d" gnus-summary-describe-group
1488     "q" gnus-summary-exit
1489     "Q" gnus-summary-exit-no-update
1490     "\C-c\C-i" gnus-info-find-node
1491     gnus-mouse-2 gnus-mouse-pick-article
1492     "m" gnus-summary-mail-other-window
1493     "a" gnus-summary-post-news
1494     "x" gnus-summary-limit-to-unread
1495     "s" gnus-summary-isearch-article
1496     "t" gnus-article-toggle-headers
1497     "g" gnus-summary-show-article
1498     "l" gnus-summary-goto-last-article
1499     "v" gnus-summary-preview-mime-message
1500     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1501     "\C-d" gnus-summary-enter-digest-group
1502     "\M-\C-d" gnus-summary-read-document
1503     "\M-\C-e" gnus-summary-edit-parameters
1504     "\M-\C-a" gnus-summary-customize-parameters
1505     "\C-c\C-b" gnus-bug
1506     "*" gnus-cache-enter-article
1507     "\M-*" gnus-cache-remove-article
1508     "\M-&" gnus-summary-universal-argument
1509     "\C-l" gnus-recenter
1510     "I" gnus-summary-increase-score
1511     "L" gnus-summary-lower-score
1512     "\M-i" gnus-symbolic-argument
1513     "h" gnus-summary-select-article-buffer
1514
1515     "V" gnus-summary-score-map
1516     "X" gnus-uu-extract-map
1517     "S" gnus-summary-send-map)
1518
1519   ;; Sort of orthogonal keymap
1520   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1521     "t" gnus-summary-tick-article-forward
1522     "!" gnus-summary-tick-article-forward
1523     "d" gnus-summary-mark-as-read-forward
1524     "r" gnus-summary-mark-as-read-forward
1525     "c" gnus-summary-clear-mark-forward
1526     " " gnus-summary-clear-mark-forward
1527     "e" gnus-summary-mark-as-expirable
1528     "x" gnus-summary-mark-as-expirable
1529     "?" gnus-summary-mark-as-dormant
1530     "b" gnus-summary-set-bookmark
1531     "B" gnus-summary-remove-bookmark
1532     "#" gnus-summary-mark-as-processable
1533     "\M-#" gnus-summary-unmark-as-processable
1534     "S" gnus-summary-limit-include-expunged
1535     "C" gnus-summary-catchup
1536     "H" gnus-summary-catchup-to-here
1537     "\C-c" gnus-summary-catchup-all
1538     "k" gnus-summary-kill-same-subject-and-select
1539     "K" gnus-summary-kill-same-subject
1540     "P" gnus-uu-mark-map)
1541
1542   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1543     "c" gnus-summary-clear-above
1544     "u" gnus-summary-tick-above
1545     "m" gnus-summary-mark-above
1546     "k" gnus-summary-kill-below)
1547
1548   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1549     "/" gnus-summary-limit-to-subject
1550     "n" gnus-summary-limit-to-articles
1551     "w" gnus-summary-pop-limit
1552     "s" gnus-summary-limit-to-subject
1553     "a" gnus-summary-limit-to-author
1554     "u" gnus-summary-limit-to-unread
1555     "m" gnus-summary-limit-to-marks
1556     "M" gnus-summary-limit-exclude-marks
1557     "v" gnus-summary-limit-to-score
1558     "*" gnus-summary-limit-include-cached
1559     "D" gnus-summary-limit-include-dormant
1560     "T" gnus-summary-limit-include-thread
1561     "d" gnus-summary-limit-exclude-dormant
1562     "t" gnus-summary-limit-to-age
1563     "x" gnus-summary-limit-to-extra
1564     "E" gnus-summary-limit-include-expunged
1565     "c" gnus-summary-limit-exclude-childless-dormant
1566     "C" gnus-summary-limit-mark-excluded-as-read
1567     "o" gnus-summary-insert-old-articles
1568     "N" gnus-summary-insert-new-articles)
1569
1570   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1571     "n" gnus-summary-next-unread-article
1572     "p" gnus-summary-prev-unread-article
1573     "N" gnus-summary-next-article
1574     "P" gnus-summary-prev-article
1575     "\C-n" gnus-summary-next-same-subject
1576     "\C-p" gnus-summary-prev-same-subject
1577     "\M-n" gnus-summary-next-unread-subject
1578     "\M-p" gnus-summary-prev-unread-subject
1579     "f" gnus-summary-first-unread-article
1580     "b" gnus-summary-best-unread-article
1581     "j" gnus-summary-goto-article
1582     "g" gnus-summary-goto-subject
1583     "l" gnus-summary-goto-last-article
1584     "o" gnus-summary-pop-article)
1585
1586   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1587     "k" gnus-summary-kill-thread
1588     "l" gnus-summary-lower-thread
1589     "i" gnus-summary-raise-thread
1590     "T" gnus-summary-toggle-threads
1591     "t" gnus-summary-rethread-current
1592     "^" gnus-summary-reparent-thread
1593     "s" gnus-summary-show-thread
1594     "S" gnus-summary-show-all-threads
1595     "h" gnus-summary-hide-thread
1596     "H" gnus-summary-hide-all-threads
1597     "n" gnus-summary-next-thread
1598     "p" gnus-summary-prev-thread
1599     "u" gnus-summary-up-thread
1600     "o" gnus-summary-top-thread
1601     "d" gnus-summary-down-thread
1602     "#" gnus-uu-mark-thread
1603     "\M-#" gnus-uu-unmark-thread)
1604
1605   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1606     "g" gnus-summary-prepare
1607     "c" gnus-summary-insert-cached-articles)
1608
1609   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1610     "c" gnus-summary-catchup-and-exit
1611     "C" gnus-summary-catchup-all-and-exit
1612     "E" gnus-summary-exit-no-update
1613     "J" gnus-summary-jump-to-other-group
1614     "Q" gnus-summary-exit
1615     "Z" gnus-summary-exit
1616     "n" gnus-summary-catchup-and-goto-next-group
1617     "R" gnus-summary-reselect-current-group
1618     "G" gnus-summary-rescan-group
1619     "N" gnus-summary-next-group
1620     "s" gnus-summary-save-newsrc
1621     "P" gnus-summary-prev-group)
1622
1623   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1624     " " gnus-summary-next-page
1625     "n" gnus-summary-next-page
1626     "\177" gnus-summary-prev-page
1627     [delete] gnus-summary-prev-page
1628     "p" gnus-summary-prev-page
1629     "\r" gnus-summary-scroll-up
1630     "\M-\r" gnus-summary-scroll-down
1631     "<" gnus-summary-beginning-of-article
1632     ">" gnus-summary-end-of-article
1633     "b" gnus-summary-beginning-of-article
1634     "e" gnus-summary-end-of-article
1635     "^" gnus-summary-refer-parent-article
1636     "r" gnus-summary-refer-parent-article
1637     "D" gnus-summary-enter-digest-group
1638     "R" gnus-summary-refer-references
1639     "T" gnus-summary-refer-thread
1640     "g" gnus-summary-show-article
1641     "s" gnus-summary-isearch-article
1642     "P" gnus-summary-print-article
1643     "M" gnus-mailing-list-insinuate
1644     "t" gnus-article-babel)
1645
1646   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1647     "b" gnus-article-add-buttons
1648     "B" gnus-article-add-buttons-to-head
1649     "o" gnus-article-treat-overstrike
1650     "e" gnus-article-emphasize
1651     "w" gnus-article-fill-cited-article
1652     "Q" gnus-article-fill-long-lines
1653     "C" gnus-article-capitalize-sentences
1654     "c" gnus-article-remove-cr
1655     "Z" gnus-article-decode-HZ
1656     "f" gnus-article-display-x-face
1657     "l" gnus-summary-stop-page-breaking
1658     "r" gnus-summary-caesar-message
1659     "t" gnus-article-toggle-headers
1660     "v" gnus-summary-verbose-headers
1661     "m" gnus-summary-toggle-mime
1662     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1663     "p" gnus-article-verify-x-pgp-sig
1664     "d" gnus-article-treat-dumbquotes
1665     "s" gnus-smiley-display)
1666
1667   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1668     "a" gnus-article-hide
1669     "h" gnus-article-toggle-headers
1670     "b" gnus-article-hide-boring-headers
1671     "s" gnus-article-hide-signature
1672     "c" gnus-article-hide-citation
1673     "C" gnus-article-hide-citation-in-followups
1674     "l" gnus-article-hide-list-identifiers
1675     "p" gnus-article-hide-pgp
1676     "B" gnus-article-strip-banner
1677     "P" gnus-article-hide-pem
1678     "\C-c" gnus-article-hide-citation-maybe)
1679
1680   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1681     "a" gnus-article-highlight
1682     "h" gnus-article-highlight-headers
1683     "c" gnus-article-highlight-citation
1684     "s" gnus-article-highlight-signature)
1685
1686   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1687     "z" gnus-article-date-ut
1688     "u" gnus-article-date-ut
1689     "l" gnus-article-date-local
1690     "p" gnus-article-date-english
1691     "e" gnus-article-date-lapsed
1692     "o" gnus-article-date-original
1693     "i" gnus-article-date-iso8601
1694     "s" gnus-article-date-user)
1695
1696   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1697     "t" gnus-article-remove-trailing-blank-lines
1698     "l" gnus-article-strip-leading-blank-lines
1699     "m" gnus-article-strip-multiple-blank-lines
1700     "a" gnus-article-strip-blank-lines
1701     "A" gnus-article-strip-all-blank-lines
1702     "s" gnus-article-strip-leading-space
1703     "e" gnus-article-strip-trailing-space
1704     "w" gnus-article-remove-leading-whitespace)
1705
1706   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1707     "v" gnus-version
1708     "f" gnus-summary-fetch-faq
1709     "d" gnus-summary-describe-group
1710     "h" gnus-summary-describe-briefly
1711     "i" gnus-info-find-node)
1712
1713   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1714     "e" gnus-summary-expire-articles
1715     "\M-\C-e" gnus-summary-expire-articles-now
1716     "\177" gnus-summary-delete-article
1717     [delete] gnus-summary-delete-article
1718     [backspace] gnus-summary-delete-article
1719     "m" gnus-summary-move-article
1720     "r" gnus-summary-respool-article
1721     "w" gnus-summary-edit-article
1722     "c" gnus-summary-copy-article
1723     "B" gnus-summary-crosspost-article
1724     "q" gnus-summary-respool-query
1725     "t" gnus-summary-respool-trace
1726     "i" gnus-summary-import-article
1727     "I" gnus-summary-create-article
1728     "p" gnus-summary-article-posted-p)
1729
1730   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1731     "o" gnus-summary-save-article
1732     "m" gnus-summary-save-article-mail
1733     "F" gnus-summary-write-article-file
1734     "r" gnus-summary-save-article-rmail
1735     "f" gnus-summary-save-article-file
1736     "b" gnus-summary-save-article-body-file
1737     "h" gnus-summary-save-article-folder
1738     "v" gnus-summary-save-article-vm
1739     "p" gnus-summary-pipe-output
1740     "s" gnus-soup-add-article)
1741
1742   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1743     "b" gnus-summary-display-buttonized
1744     "m" gnus-summary-repair-multipart
1745     "v" gnus-article-view-part
1746     "o" gnus-article-save-part
1747     "c" gnus-article-copy-part
1748     "C" gnus-article-view-part-as-charset
1749     "e" gnus-article-externalize-part
1750     "E" gnus-article-encrypt-body
1751     "i" gnus-article-inline-part
1752     "|" gnus-article-pipe-part))
1753
1754 (defvar gnus-article-post-menu nil)
1755
1756 (defun gnus-summary-make-menu-bar ()
1757   (gnus-turn-off-edit-menu 'summary)
1758
1759   (unless (boundp 'gnus-summary-misc-menu)
1760
1761     (easy-menu-define
1762      gnus-summary-kill-menu gnus-summary-mode-map ""
1763      (cons
1764       "Score"
1765       (nconc
1766        (list
1767         ["Customize" gnus-score-customize t])
1768        (gnus-make-score-map 'increase)
1769        (gnus-make-score-map 'lower)
1770        '(("Mark"
1771           ["Kill below" gnus-summary-kill-below t]
1772           ["Mark above" gnus-summary-mark-above t]
1773           ["Tick above" gnus-summary-tick-above t]
1774           ["Clear above" gnus-summary-clear-above t])
1775          ["Current score" gnus-summary-current-score t]
1776          ["Set score" gnus-summary-set-score t]
1777          ["Switch current score file..." gnus-score-change-score-file t]
1778          ["Set mark below..." gnus-score-set-mark-below t]
1779          ["Set expunge below..." gnus-score-set-expunge-below t]
1780          ["Edit current score file" gnus-score-edit-current-scores t]
1781          ["Edit score file" gnus-score-edit-file t]
1782          ["Trace score" gnus-score-find-trace t]
1783          ["Find words" gnus-score-find-favourite-words t]
1784          ["Rescore buffer" gnus-summary-rescore t]
1785          ["Increase score..." gnus-summary-increase-score t]
1786          ["Lower score..." gnus-summary-lower-score t]))))
1787
1788     ;; Define both the Article menu in the summary buffer and the equivalent
1789     ;; Commands menu in the article buffer here for consistency.
1790     (let ((innards
1791            `(("Hide"
1792               ["All" gnus-article-hide t]
1793               ["Headers" gnus-article-toggle-headers t]
1794               ["Signature" gnus-article-hide-signature t]
1795               ["Citation" gnus-article-hide-citation t]
1796               ["List identifiers" gnus-article-hide-list-identifiers t]
1797               ["PGP" gnus-article-hide-pgp t]
1798               ["Banner" gnus-article-strip-banner t]
1799               ["Boring headers" gnus-article-hide-boring-headers t])
1800              ("Highlight"
1801               ["All" gnus-article-highlight t]
1802               ["Headers" gnus-article-highlight-headers t]
1803               ["Signature" gnus-article-highlight-signature t]
1804               ["Citation" gnus-article-highlight-citation t])
1805              ("Date"
1806               ["Local" gnus-article-date-local t]
1807               ["ISO8601" gnus-article-date-iso8601 t]
1808               ["UT" gnus-article-date-ut t]
1809               ["Original" gnus-article-date-original t]
1810               ["Lapsed" gnus-article-date-lapsed t]
1811               ["User-defined" gnus-article-date-user t])
1812              ("Washing"
1813               ("Remove Blanks"
1814                ["Leading" gnus-article-strip-leading-blank-lines t]
1815                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1816                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1817                ["All of the above" gnus-article-strip-blank-lines t]
1818                ["All" gnus-article-strip-all-blank-lines t]
1819                ["Leading space" gnus-article-strip-leading-space t]
1820                ["Trailing space" gnus-article-strip-trailing-space t]
1821                ["Leading space in headers" 
1822                 gnus-article-remove-leading-whitespace t])
1823               ["Overstrike" gnus-article-treat-overstrike t]
1824               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1825               ["Emphasis" gnus-article-emphasize t]
1826               ["Word wrap" gnus-article-fill-cited-article t]
1827               ["Fill long lines" gnus-article-fill-long-lines t]
1828               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1829               ["CR" gnus-article-remove-cr t]
1830               ["Show X-Face" gnus-article-display-x-face t]
1831               ["Rot 13" gnus-summary-caesar-message
1832                ,@(if (featurep 'xemacs) '(t)
1833                    '(:help "\"Caesar rotate\" article by 13"))]
1834               ["Unix pipe" gnus-summary-pipe-message t]
1835               ["Add buttons" gnus-article-add-buttons t]
1836               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1837               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1838               ["Toggle MIME" gnus-summary-toggle-mime t]
1839               ["Verbose header" gnus-summary-verbose-headers t]
1840               ["Toggle header" gnus-summary-toggle-header t]
1841               ["Toggle smileys" gnus-smiley-display t]
1842               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1843               ["HZ" gnus-article-decode-HZ t])
1844              ("Output"
1845               ["Save in default format" gnus-summary-save-article
1846                ,@(if (featurep 'xemacs) '(t)
1847                    '(:help "Save article using default method"))]
1848               ["Save in file" gnus-summary-save-article-file
1849                ,@(if (featurep 'xemacs) '(t)
1850                    '(:help "Save article in file"))]
1851               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1852               ["Save in MH folder" gnus-summary-save-article-folder t]
1853               ["Save in VM folder" gnus-summary-save-article-vm t]
1854               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1855               ["Save body in file" gnus-summary-save-article-body-file t]
1856               ["Pipe through a filter" gnus-summary-pipe-output t]
1857               ["Add to SOUP packet" gnus-soup-add-article t]
1858               ["Print" gnus-summary-print-article t])
1859              ("Backend"
1860               ["Respool article..." gnus-summary-respool-article t]
1861               ["Move article..." gnus-summary-move-article
1862                (gnus-check-backend-function
1863                 'request-move-article gnus-newsgroup-name)]
1864               ["Copy article..." gnus-summary-copy-article t]
1865               ["Crosspost article..." gnus-summary-crosspost-article
1866                (gnus-check-backend-function
1867                 'request-replace-article gnus-newsgroup-name)]
1868               ["Import file..." gnus-summary-import-article t]
1869               ["Create article..." gnus-summary-create-article t]
1870               ["Check if posted" gnus-summary-article-posted-p t]
1871               ["Edit article" gnus-summary-edit-article
1872                (not (gnus-group-read-only-p))]
1873               ["Delete article" gnus-summary-delete-article
1874                (gnus-check-backend-function
1875                 'request-expire-articles gnus-newsgroup-name)]
1876               ["Query respool" gnus-summary-respool-query t]
1877               ["Trace respool" gnus-summary-respool-trace t]
1878               ["Delete expirable articles" gnus-summary-expire-articles-now
1879                (gnus-check-backend-function
1880                 'request-expire-articles gnus-newsgroup-name)])
1881              ("Extract"
1882               ["Uudecode" gnus-uu-decode-uu
1883                ,@(if (featurep 'xemacs) '(t)
1884                    '(:help "Decode uuencoded article(s)"))]
1885               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1886               ["Unshar" gnus-uu-decode-unshar t]
1887               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1888               ["Save" gnus-uu-decode-save t]
1889               ["Binhex" gnus-uu-decode-binhex t]
1890               ["Postscript" gnus-uu-decode-postscript t])
1891              ("Cache"
1892               ["Enter article" gnus-cache-enter-article t]
1893               ["Remove article" gnus-cache-remove-article t])
1894              ["Translate" gnus-article-babel t]
1895              ["Select article buffer" gnus-summary-select-article-buffer t]
1896              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1897              ["Isearch article..." gnus-summary-isearch-article t]
1898              ["Beginning of the article" gnus-summary-beginning-of-article t]
1899              ["End of the article" gnus-summary-end-of-article t]
1900              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1901              ["Fetch referenced articles" gnus-summary-refer-references t]
1902              ["Fetch current thread" gnus-summary-refer-thread t]
1903              ["Fetch article with id..." gnus-summary-refer-article t]
1904              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1905              ["Redisplay" gnus-summary-show-article t])))
1906       (easy-menu-define
1907        gnus-summary-article-menu gnus-summary-mode-map ""
1908        (cons "Article" innards))
1909
1910       (if (not (keymapp gnus-summary-article-menu))
1911           (easy-menu-define
1912             gnus-article-commands-menu gnus-article-mode-map ""
1913             (cons "Commands" innards))
1914         ;; in Emacs, don't share menu.
1915         (setq gnus-article-commands-menu 
1916               (copy-keymap gnus-summary-article-menu))
1917         (define-key gnus-article-mode-map [menu-bar commands]
1918           (cons "Commands" gnus-article-commands-menu))))
1919
1920     (easy-menu-define
1921      gnus-summary-thread-menu gnus-summary-mode-map ""
1922      '("Threads"
1923        ["Toggle threading" gnus-summary-toggle-threads t]
1924        ["Hide threads" gnus-summary-hide-all-threads t]
1925        ["Show threads" gnus-summary-show-all-threads t]
1926        ["Hide thread" gnus-summary-hide-thread t]
1927        ["Show thread" gnus-summary-show-thread t]
1928        ["Go to next thread" gnus-summary-next-thread t]
1929        ["Go to previous thread" gnus-summary-prev-thread t]
1930        ["Go down thread" gnus-summary-down-thread t]
1931        ["Go up thread" gnus-summary-up-thread t]
1932        ["Top of thread" gnus-summary-top-thread t]
1933        ["Mark thread as read" gnus-summary-kill-thread t]
1934        ["Lower thread score" gnus-summary-lower-thread t]
1935        ["Raise thread score" gnus-summary-raise-thread t]
1936        ["Rethread current" gnus-summary-rethread-current t]))
1937
1938     (easy-menu-define
1939      gnus-summary-post-menu gnus-summary-mode-map ""
1940      `("Post"
1941        ["Post an article" gnus-summary-post-news
1942         ,@(if (featurep 'xemacs) '(t)
1943             '(:help "Post an article"))]
1944        ["Followup" gnus-summary-followup
1945         ,@(if (featurep 'xemacs) '(t)
1946             '(:help "Post followup to this article"))]
1947        ["Followup and yank" gnus-summary-followup-with-original
1948         ,@(if (featurep 'xemacs) '(t)
1949             '(:help "Post followup to this article, quoting its contents"))]
1950        ["Supersede article" gnus-summary-supersede-article t]
1951        ["Cancel article" gnus-summary-cancel-article
1952         ,@(if (featurep 'xemacs) '(t)
1953             '(:help "Cancel an article you posted"))]
1954        ["Reply" gnus-summary-reply t]
1955        ["Reply and yank" gnus-summary-reply-with-original t]
1956        ["Wide reply" gnus-summary-wide-reply t]
1957        ["Wide reply and yank" gnus-summary-wide-reply-with-original
1958         ,@(if (featurep 'xemacs) '(t)
1959             '(:help "Mail a reply, quoting this article"))]
1960        ["Mail forward" gnus-summary-mail-forward t]
1961        ["Post forward" gnus-summary-post-forward t]
1962        ["Digest and mail" gnus-summary-digest-mail-forward t]
1963        ["Digest and post" gnus-summary-digest-post-forward t]
1964        ["Resend message" gnus-summary-resend-message t]
1965        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1966        ["Send a mail" gnus-summary-mail-other-window t]
1967        ["Uuencode and post" gnus-uu-post-news
1968         ,@(if (featurep 'xemacs) '(t)
1969             '(:help "Post a uuencoded article"))]
1970        ["Followup via news" gnus-summary-followup-to-mail t]
1971        ["Followup via news and yank"
1972         gnus-summary-followup-to-mail-with-original t]
1973        ;;("Draft"
1974        ;;["Send" gnus-summary-send-draft t]
1975        ;;["Send bounced" gnus-resend-bounced-mail t])
1976        ))
1977
1978     (cond 
1979      ((not (keymapp gnus-summary-post-menu))
1980       (setq gnus-article-post-menu gnus-summary-post-menu))
1981      ((not gnus-article-post-menu)
1982       ;; Don't share post menu.
1983       (setq gnus-article-post-menu
1984             (copy-keymap gnus-summary-post-menu))))
1985     (define-key gnus-article-mode-map [menu-bar post]
1986       (cons "Post" gnus-article-post-menu))
1987
1988     (easy-menu-define
1989      gnus-summary-misc-menu gnus-summary-mode-map ""
1990      `("Misc"
1991        ("Mark Read"
1992         ["Mark as read" gnus-summary-mark-as-read-forward t]
1993         ["Mark same subject and select"
1994          gnus-summary-kill-same-subject-and-select t]
1995         ["Mark same subject" gnus-summary-kill-same-subject t]
1996         ["Catchup" gnus-summary-catchup
1997          ,@(if (featurep 'xemacs) '(t)
1998              '(:help "Mark unread articles in this group as read"))]
1999         ["Catchup all" gnus-summary-catchup-all t]
2000         ["Catchup to here" gnus-summary-catchup-to-here t]
2001         ["Catchup region" gnus-summary-mark-region-as-read t]
2002         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2003        ("Mark Various"
2004         ["Tick" gnus-summary-tick-article-forward t]
2005         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2006         ["Remove marks" gnus-summary-clear-mark-forward t]
2007         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2008         ["Set bookmark" gnus-summary-set-bookmark t]
2009         ["Remove bookmark" gnus-summary-remove-bookmark t])
2010        ("Mark Limit"
2011         ["Marks..." gnus-summary-limit-to-marks t]
2012         ["Subject..." gnus-summary-limit-to-subject t]
2013         ["Author..." gnus-summary-limit-to-author t]
2014         ["Age..." gnus-summary-limit-to-age t]
2015         ["Extra..." gnus-summary-limit-to-extra t]
2016         ["Score" gnus-summary-limit-to-score t]
2017         ["Unread" gnus-summary-limit-to-unread t]
2018         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2019         ["Articles" gnus-summary-limit-to-articles t]
2020         ["Pop limit" gnus-summary-pop-limit t]
2021         ["Show dormant" gnus-summary-limit-include-dormant t]
2022         ["Hide childless dormant"
2023          gnus-summary-limit-exclude-childless-dormant t]
2024         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2025         ["Hide marked" gnus-summary-limit-exclude-marks t]
2026         ["Show expunged" gnus-summary-limit-include-expunged t])
2027        ("Process Mark"
2028         ["Set mark" gnus-summary-mark-as-processable t]
2029         ["Remove mark" gnus-summary-unmark-as-processable t]
2030         ["Remove all marks" gnus-summary-unmark-all-processable t]
2031         ["Mark above" gnus-uu-mark-over t]
2032         ["Mark series" gnus-uu-mark-series t]
2033         ["Mark region" gnus-uu-mark-region t]
2034         ["Unmark region" gnus-uu-unmark-region t]
2035         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2036         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2037         ["Mark all" gnus-uu-mark-all t]
2038         ["Mark buffer" gnus-uu-mark-buffer t]
2039         ["Mark sparse" gnus-uu-mark-sparse t]
2040         ["Mark thread" gnus-uu-mark-thread t]
2041         ["Unmark thread" gnus-uu-unmark-thread t]
2042         ("Process Mark Sets"
2043          ["Kill" gnus-summary-kill-process-mark t]
2044          ["Yank" gnus-summary-yank-process-mark
2045           gnus-newsgroup-process-stack]
2046          ["Save" gnus-summary-save-process-mark t]))
2047        ("Scroll article"
2048         ["Page forward" gnus-summary-next-page
2049          ,@(if (featurep 'xemacs) '(t)
2050              '(:help "Show next page of article"))]
2051         ["Page backward" gnus-summary-prev-page
2052          ,@(if (featurep 'xemacs) '(t)
2053              '(:help "Show previous page of article"))]
2054         ["Line forward" gnus-summary-scroll-up t])
2055        ("Move"
2056         ["Next unread article" gnus-summary-next-unread-article t]
2057         ["Previous unread article" gnus-summary-prev-unread-article t]
2058         ["Next article" gnus-summary-next-article t]
2059         ["Previous article" gnus-summary-prev-article t]
2060         ["Next unread subject" gnus-summary-next-unread-subject t]
2061         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2062         ["Next article same subject" gnus-summary-next-same-subject t]
2063         ["Previous article same subject" gnus-summary-prev-same-subject t]
2064         ["First unread article" gnus-summary-first-unread-article t]
2065         ["Best unread article" gnus-summary-best-unread-article t]
2066         ["Go to subject number..." gnus-summary-goto-subject t]
2067         ["Go to article number..." gnus-summary-goto-article t]
2068         ["Go to the last article" gnus-summary-goto-last-article t]
2069         ["Pop article off history" gnus-summary-pop-article t])
2070        ("Sort"
2071         ["Sort by number" gnus-summary-sort-by-number t]
2072         ["Sort by author" gnus-summary-sort-by-author t]
2073         ["Sort by subject" gnus-summary-sort-by-subject t]
2074         ["Sort by date" gnus-summary-sort-by-date t]
2075         ["Sort by score" gnus-summary-sort-by-score t]
2076         ["Sort by lines" gnus-summary-sort-by-lines t]
2077         ["Sort by characters" gnus-summary-sort-by-chars t]
2078         ["Original sort" gnus-summary-sort-by-original t])
2079        ("Help"
2080         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2081         ["Describe group" gnus-summary-describe-group t]
2082         ["Read manual" gnus-info-find-node t])
2083        ("Modes"
2084         ["Pick and read" gnus-pick-mode t]
2085         ["Binary" gnus-binary-mode t])
2086        ("Regeneration"
2087         ["Regenerate" gnus-summary-prepare t]
2088         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2089         ["Toggle threading" gnus-summary-toggle-threads t])
2090        ["See old articles" gnus-summary-insert-old-articles t]
2091        ["See new articles" gnus-summary-insert-new-articles t]
2092        ["Filter articles..." gnus-summary-execute-command t]
2093        ["Run command on subjects..." gnus-summary-universal-argument t]
2094        ["Search articles forward..." gnus-summary-search-article-forward t]
2095        ["Search articles backward..." gnus-summary-search-article-backward t]
2096        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2097        ["Expand window" gnus-summary-expand-window t]
2098        ["Expire expirable articles" gnus-summary-expire-articles
2099         (gnus-check-backend-function
2100          'request-expire-articles gnus-newsgroup-name)]
2101        ["Edit local kill file" gnus-summary-edit-local-kill t]
2102        ["Edit main kill file" gnus-summary-edit-global-kill t]
2103        ["Edit group parameters" gnus-summary-edit-parameters t]
2104        ["Customize group parameters" gnus-summary-customize-parameters t]
2105        ["Send a bug report" gnus-bug t]
2106        ("Exit"
2107         ["Catchup and exit" gnus-summary-catchup-and-exit
2108          ,@(if (featurep 'xemacs) '(t)
2109              '(:help "Mark unread articles in this group as read, then exit"))]
2110         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2111         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2112         ["Exit group" gnus-summary-exit
2113          ,@(if (featurep 'xemacs) '(t)
2114              '(:help "Exit current group, return to group selection mode"))]
2115         ["Exit group without updating" gnus-summary-exit-no-update t]
2116         ["Exit and goto next group" gnus-summary-next-group t]
2117         ["Exit and goto prev group" gnus-summary-prev-group t]
2118         ["Reselect group" gnus-summary-reselect-current-group t]
2119         ["Rescan group" gnus-summary-rescan-group t]
2120         ["Update dribble" gnus-summary-save-newsrc t])))
2121
2122     (gnus-run-hooks 'gnus-summary-menu-hook)))
2123
2124 (defvar gnus-summary-tool-bar-map nil)
2125
2126 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2127 (defun gnus-summary-make-tool-bar ()
2128   (if (and (fboundp 'tool-bar-add-item-from-menu)
2129            (default-value 'tool-bar-mode)
2130            (not gnus-summary-tool-bar-map))
2131       (setq gnus-summary-tool-bar-map
2132             (let ((tool-bar-map (make-sparse-keymap))
2133                   (load-path (mm-image-load-path)))
2134               (tool-bar-add-item-from-menu
2135                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2136               (tool-bar-add-item-from-menu
2137                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2138               (tool-bar-add-item-from-menu
2139                'gnus-summary-post-news "post" gnus-summary-mode-map)
2140               (tool-bar-add-item-from-menu
2141                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2142               (tool-bar-add-item-from-menu
2143                'gnus-summary-followup "followup" gnus-summary-mode-map)
2144               (tool-bar-add-item-from-menu
2145                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2146               (tool-bar-add-item-from-menu
2147                'gnus-summary-reply "reply" gnus-summary-mode-map)
2148               (tool-bar-add-item-from-menu
2149                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2150               (tool-bar-add-item-from-menu
2151                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2152               (tool-bar-add-item-from-menu
2153                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2154               (tool-bar-add-item-from-menu
2155                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2156               (tool-bar-add-item-from-menu
2157                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2158               (tool-bar-add-item-from-menu
2159                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2160               (tool-bar-add-item-from-menu
2161                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2162               (tool-bar-add-item-from-menu
2163                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2164               tool-bar-map)))
2165   (if gnus-summary-tool-bar-map
2166       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2167
2168 (defun gnus-score-set-default (var value)
2169   "A version of set that updates the GNU Emacs menu-bar."
2170   (set var value)
2171   ;; It is the message that forces the active status to be updated.
2172   (message ""))
2173
2174 (defun gnus-make-score-map (type)
2175   "Make a summary score map of type TYPE."
2176   (if t
2177       nil
2178     (let ((headers '(("author" "from" string)
2179                      ("subject" "subject" string)
2180                      ("article body" "body" string)
2181                      ("article head" "head" string)
2182                      ("xref" "xref" string)
2183                      ("extra header" "extra" string)
2184                      ("lines" "lines" number)
2185                      ("followups to author" "followup" string)))
2186           (types '((number ("less than" <)
2187                            ("greater than" >)
2188                            ("equal" =))
2189                    (string ("substring" s)
2190                            ("exact string" e)
2191                            ("fuzzy string" f)
2192                            ("regexp" r))))
2193           (perms '(("temporary" (current-time-string))
2194                    ("permanent" nil)
2195                    ("immediate" now)))
2196           header)
2197       (list
2198        (apply
2199         'nconc
2200         (list
2201          (if (eq type 'lower)
2202              "Lower score"
2203            "Increase score"))
2204         (let (outh)
2205           (while headers
2206             (setq header (car headers))
2207             (setq outh
2208                   (cons
2209                    (apply
2210                     'nconc
2211                     (list (car header))
2212                     (let ((ts (cdr (assoc (nth 2 header) types)))
2213                           outt)
2214                       (while ts
2215                         (setq outt
2216                               (cons
2217                                (apply
2218                                 'nconc
2219                                 (list (caar ts))
2220                                 (let ((ps perms)
2221                                       outp)
2222                                   (while ps
2223                                     (setq outp
2224                                           (cons
2225                                            (vector
2226                                             (caar ps)
2227                                             (list
2228                                              'gnus-summary-score-entry
2229                                              (nth 1 header)
2230                                              (if (or (string= (nth 1 header)
2231                                                               "head")
2232                                                      (string= (nth 1 header)
2233                                                               "body"))
2234                                                  ""
2235                                                (list 'gnus-summary-header
2236                                                      (nth 1 header)))
2237                                              (list 'quote (nth 1 (car ts)))
2238                                              (list 'gnus-score-delta-default
2239                                                    nil)
2240                                              (nth 1 (car ps))
2241                                              t)
2242                                             t)
2243                                            outp))
2244                                     (setq ps (cdr ps)))
2245                                   (list (nreverse outp))))
2246                                outt))
2247                         (setq ts (cdr ts)))
2248                       (list (nreverse outt))))
2249                    outh))
2250             (setq headers (cdr headers)))
2251           (list (nreverse outh))))))))
2252
2253 \f
2254
2255 (defun gnus-summary-mode (&optional group)
2256   "Major mode for reading articles.
2257
2258 All normal editing commands are switched off.
2259 \\<gnus-summary-mode-map>
2260 Each line in this buffer represents one article.  To read an
2261 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2262 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2263 respectively.
2264
2265 You can also post articles and send mail from this buffer.  To
2266 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2267 of an article, type `\\[gnus-summary-reply]'.
2268
2269 There are approx. one gazillion commands you can execute in this
2270 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2271
2272 The following commands are available:
2273
2274 \\{gnus-summary-mode-map}"
2275   (interactive)
2276   (kill-all-local-variables)
2277   (when (gnus-visual-p 'summary-menu 'menu)
2278     (gnus-summary-make-menu-bar)
2279     (gnus-summary-make-tool-bar))
2280   (gnus-summary-make-local-variables)
2281   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2282     (gnus-summary-make-local-variables))
2283   (gnus-make-thread-indent-array)
2284   (gnus-simplify-mode-line)
2285   (setq major-mode 'gnus-summary-mode)
2286   (setq mode-name "Summary")
2287   (make-local-variable 'minor-mode-alist)
2288   (use-local-map gnus-summary-mode-map)
2289   (buffer-disable-undo)
2290   (setq buffer-read-only t)             ;Disable modification
2291   (setq truncate-lines t)
2292   (setq selective-display t)
2293   (setq selective-display-ellipses t)   ;Display `...'
2294   (gnus-summary-set-display-table)
2295   (gnus-set-default-directory)
2296   (setq gnus-newsgroup-name group)
2297   (unless (gnus-news-group-p group)
2298     (setq gnus-newsgroup-incorporated
2299           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2300   (make-local-variable 'gnus-summary-line-format)
2301   (make-local-variable 'gnus-summary-line-format-spec)
2302   (make-local-variable 'gnus-summary-dummy-line-format)
2303   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2304   (make-local-variable 'gnus-summary-mark-positions)
2305   (make-local-hook 'pre-command-hook)
2306   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2307   (gnus-run-hooks 'gnus-summary-mode-hook)
2308   (turn-on-gnus-mailing-list-mode)
2309   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2310   (gnus-update-summary-mark-positions))
2311
2312 (defun gnus-summary-make-local-variables ()
2313   "Make all the local summary buffer variables."
2314   (let (global)
2315     (dolist (local gnus-summary-local-variables)
2316       (if (consp local)
2317           (progn
2318             (if (eq (cdr local) 'global)
2319                 ;; Copy the global value of the variable.
2320                 (setq global (symbol-value (car local)))
2321               ;; Use the value from the list.
2322               (setq global (eval (cdr local))))
2323             (set (make-local-variable (car local)) global))
2324         ;; Simple nil-valued local variable.
2325         (set (make-local-variable local) nil)))))
2326
2327 (defun gnus-summary-clear-local-variables ()
2328   (let ((locals gnus-summary-local-variables))
2329     (while locals
2330       (if (consp (car locals))
2331           (and (vectorp (caar locals))
2332                (set (caar locals) nil))
2333         (and (vectorp (car locals))
2334              (set (car locals) nil)))
2335       (setq locals (cdr locals)))))
2336
2337 ;; Summary data functions.
2338
2339 (defmacro gnus-data-number (data)
2340   `(car ,data))
2341
2342 (defmacro gnus-data-set-number (data number)
2343   `(setcar ,data ,number))
2344
2345 (defmacro gnus-data-mark (data)
2346   `(nth 1 ,data))
2347
2348 (defmacro gnus-data-set-mark (data mark)
2349   `(setcar (nthcdr 1 ,data) ,mark))
2350
2351 (defmacro gnus-data-pos (data)
2352   `(nth 2 ,data))
2353
2354 (defmacro gnus-data-set-pos (data pos)
2355   `(setcar (nthcdr 2 ,data) ,pos))
2356
2357 (defmacro gnus-data-header (data)
2358   `(nth 3 ,data))
2359
2360 (defmacro gnus-data-set-header (data header)
2361   `(setcar (nthcdr 3 ,data) ,header))
2362
2363 (defmacro gnus-data-level (data)
2364   `(nth 4 ,data))
2365
2366 (defmacro gnus-data-unread-p (data)
2367   `(= (nth 1 ,data) gnus-unread-mark))
2368
2369 (defmacro gnus-data-read-p (data)
2370   `(/= (nth 1 ,data) gnus-unread-mark))
2371
2372 (defmacro gnus-data-pseudo-p (data)
2373   `(consp (nth 3 ,data)))
2374
2375 (defmacro gnus-data-find (number)
2376   `(assq ,number gnus-newsgroup-data))
2377
2378 (defmacro gnus-data-find-list (number &optional data)
2379   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2380      (memq (assq ,number bdata)
2381            bdata)))
2382
2383 (defmacro gnus-data-make (number mark pos header level)
2384   `(list ,number ,mark ,pos ,header ,level))
2385
2386 (defun gnus-data-enter (after-article number mark pos header level offset)
2387   (let ((data (gnus-data-find-list after-article)))
2388     (unless data
2389       (error "No such article: %d" after-article))
2390     (setcdr data (cons (gnus-data-make number mark pos header level)
2391                        (cdr data)))
2392     (setq gnus-newsgroup-data-reverse nil)
2393     (gnus-data-update-list (cddr data) offset)))
2394
2395 (defun gnus-data-enter-list (after-article list &optional offset)
2396   (when list
2397     (let ((data (and after-article (gnus-data-find-list after-article)))
2398           (ilist list))
2399       (if (not (or data
2400                    after-article))
2401           (let ((odata gnus-newsgroup-data))
2402             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2403             (when offset
2404               (gnus-data-update-list odata offset)))
2405         ;; Find the last element in the list to be spliced into the main
2406         ;; list.
2407         (while (cdr list)
2408           (setq list (cdr list)))
2409         (if (not data)
2410             (progn
2411               (setcdr list gnus-newsgroup-data)
2412               (setq gnus-newsgroup-data ilist)
2413               (when offset
2414                 (gnus-data-update-list (cdr list) offset)))
2415           (setcdr list (cdr data))
2416           (setcdr data ilist)
2417           (when offset
2418             (gnus-data-update-list (cdr list) offset))))
2419       (setq gnus-newsgroup-data-reverse nil))))
2420
2421 (defun gnus-data-remove (article &optional offset)
2422   (let ((data gnus-newsgroup-data))
2423     (if (= (gnus-data-number (car data)) article)
2424         (progn
2425           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2426                 gnus-newsgroup-data-reverse nil)
2427           (when offset
2428             (gnus-data-update-list gnus-newsgroup-data offset)))
2429       (while (cdr data)
2430         (when (= (gnus-data-number (cadr data)) article)
2431           (setcdr data (cddr data))
2432           (when offset
2433             (gnus-data-update-list (cdr data) offset))
2434           (setq data nil
2435                 gnus-newsgroup-data-reverse nil))
2436         (setq data (cdr data))))))
2437
2438 (defmacro gnus-data-list (backward)
2439   `(if ,backward
2440        (or gnus-newsgroup-data-reverse
2441            (setq gnus-newsgroup-data-reverse
2442                  (reverse gnus-newsgroup-data)))
2443      gnus-newsgroup-data))
2444
2445 (defun gnus-data-update-list (data offset)
2446   "Add OFFSET to the POS of all data entries in DATA."
2447   (setq gnus-newsgroup-data-reverse nil)
2448   (while data
2449     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2450     (setq data (cdr data))))
2451
2452 (defun gnus-summary-article-pseudo-p (article)
2453   "Say whether this article is a pseudo article or not."
2454   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2455
2456 (defmacro gnus-summary-article-sparse-p (article)
2457   "Say whether this article is a sparse article or not."
2458   `(memq ,article gnus-newsgroup-sparse))
2459
2460 (defmacro gnus-summary-article-ancient-p (article)
2461   "Say whether this article is a sparse article or not."
2462   `(memq ,article gnus-newsgroup-ancient))
2463
2464 (defun gnus-article-parent-p (number)
2465   "Say whether this article is a parent or not."
2466   (let ((data (gnus-data-find-list number)))
2467     (and (cdr data)                     ; There has to be an article after...
2468          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2469             (gnus-data-level (nth 1 data))))))
2470
2471 (defun gnus-article-children (number)
2472   "Return a list of all children to NUMBER."
2473   (let* ((data (gnus-data-find-list number))
2474          (level (gnus-data-level (car data)))
2475          children)
2476     (setq data (cdr data))
2477     (while (and data
2478                 (= (gnus-data-level (car data)) (1+ level)))
2479       (push (gnus-data-number (car data)) children)
2480       (setq data (cdr data)))
2481     children))
2482
2483 (defmacro gnus-summary-skip-intangible ()
2484   "If the current article is intangible, then jump to a different article."
2485   '(let ((to (get-text-property (point) 'gnus-intangible)))
2486      (and to (gnus-summary-goto-subject to))))
2487
2488 (defmacro gnus-summary-article-intangible-p ()
2489   "Say whether this article is intangible or not."
2490   '(get-text-property (point) 'gnus-intangible))
2491
2492 (defun gnus-article-read-p (article)
2493   "Say whether ARTICLE is read or not."
2494   (not (or (memq article gnus-newsgroup-marked)
2495            (memq article gnus-newsgroup-unreads)
2496            (memq article gnus-newsgroup-unselected)
2497            (memq article gnus-newsgroup-dormant))))
2498
2499 ;; Some summary mode macros.
2500
2501 (defmacro gnus-summary-article-number ()
2502   "The article number of the article on the current line.
2503 If there isn's an article number here, then we return the current
2504 article number."
2505   '(progn
2506      (gnus-summary-skip-intangible)
2507      (or (get-text-property (point) 'gnus-number)
2508          (gnus-summary-last-subject))))
2509
2510 (defmacro gnus-summary-article-header (&optional number)
2511   "Return the header of article NUMBER."
2512   `(gnus-data-header (gnus-data-find
2513                       ,(or number '(gnus-summary-article-number)))))
2514
2515 (defmacro gnus-summary-thread-level (&optional number)
2516   "Return the level of thread that starts with article NUMBER."
2517   `(if (and (eq gnus-summary-make-false-root 'dummy)
2518             (get-text-property (point) 'gnus-intangible))
2519        0
2520      (gnus-data-level (gnus-data-find
2521                        ,(or number '(gnus-summary-article-number))))))
2522
2523 (defmacro gnus-summary-article-mark (&optional number)
2524   "Return the mark of article NUMBER."
2525   `(gnus-data-mark (gnus-data-find
2526                     ,(or number '(gnus-summary-article-number)))))
2527
2528 (defmacro gnus-summary-article-pos (&optional number)
2529   "Return the position of the line of article NUMBER."
2530   `(gnus-data-pos (gnus-data-find
2531                    ,(or number '(gnus-summary-article-number)))))
2532
2533 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2534 (defmacro gnus-summary-article-subject (&optional number)
2535   "Return current subject string or nil if nothing."
2536   `(let ((headers
2537           ,(if number
2538                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2539              '(gnus-data-header (assq (gnus-summary-article-number)
2540                                       gnus-newsgroup-data)))))
2541      (and headers
2542           (vectorp headers)
2543           (mail-header-subject headers))))
2544
2545 (defmacro gnus-summary-article-score (&optional number)
2546   "Return current article score."
2547   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2548                   gnus-newsgroup-scored))
2549        gnus-summary-default-score 0))
2550
2551 (defun gnus-summary-article-children (&optional number)
2552   "Return a list of article numbers that are children of article NUMBER."
2553   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2554          (level (gnus-data-level (car data)))
2555          l children)
2556     (while (and (setq data (cdr data))
2557                 (> (setq l (gnus-data-level (car data))) level))
2558       (and (= (1+ level) l)
2559            (push (gnus-data-number (car data))
2560                  children)))
2561     (nreverse children)))
2562
2563 (defun gnus-summary-article-parent (&optional number)
2564   "Return the article number of the parent of article NUMBER."
2565   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2566                                     (gnus-data-list t)))
2567          (level (gnus-data-level (car data))))
2568     (if (zerop level)
2569         ()                              ; This is a root.
2570       ;; We search until we find an article with a level less than
2571       ;; this one.  That function has to be the parent.
2572       (while (and (setq data (cdr data))
2573                   (not (< (gnus-data-level (car data)) level))))
2574       (and data (gnus-data-number (car data))))))
2575
2576 (defun gnus-unread-mark-p (mark)
2577   "Say whether MARK is the unread mark."
2578   (= mark gnus-unread-mark))
2579
2580 (defun gnus-read-mark-p (mark)
2581   "Say whether MARK is one of the marks that mark as read.
2582 This is all marks except unread, ticked, dormant, and expirable."
2583   (not (or (= mark gnus-unread-mark)
2584            (= mark gnus-ticked-mark)
2585            (= mark gnus-dormant-mark)
2586            (= mark gnus-expirable-mark))))
2587
2588 (defmacro gnus-article-mark (number)
2589   "Return the MARK of article NUMBER.
2590 This macro should only be used when computing the mark the \"first\"
2591 time; i.e., when generating the summary lines.  After that,
2592 `gnus-summary-article-mark' should be used to examine the
2593 marks of articles."
2594   `(cond
2595     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2596     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2597     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2598     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2599     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2600     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2601     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2602     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2603            gnus-ancient-mark))))
2604
2605 ;; Saving hidden threads.
2606
2607 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2608 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2609
2610 (defmacro gnus-save-hidden-threads (&rest forms)
2611   "Save hidden threads, eval FORMS, and restore the hidden threads."
2612   (let ((config (make-symbol "config")))
2613     `(let ((,config (gnus-hidden-threads-configuration)))
2614        (unwind-protect
2615            (save-excursion
2616              ,@forms)
2617          (gnus-restore-hidden-threads-configuration ,config)))))
2618
2619 (defun gnus-data-compute-positions ()
2620   "Compute the positions of all articles."
2621   (setq gnus-newsgroup-data-reverse nil)
2622   (let ((data gnus-newsgroup-data))
2623     (save-excursion
2624       (gnus-save-hidden-threads
2625         (gnus-summary-show-all-threads)
2626         (goto-char (point-min))
2627         (while data
2628           (while (get-text-property (point) 'gnus-intangible)
2629             (forward-line 1))
2630           (gnus-data-set-pos (car data) (+ (point) 3))
2631           (setq data (cdr data))
2632           (forward-line 1))))))
2633
2634 (defun gnus-hidden-threads-configuration ()
2635   "Return the current hidden threads configuration."
2636   (save-excursion
2637     (let (config)
2638       (goto-char (point-min))
2639       (while (search-forward "\r" nil t)
2640         (push (1- (point)) config))
2641       config)))
2642
2643 (defun gnus-restore-hidden-threads-configuration (config)
2644   "Restore hidden threads configuration from CONFIG."
2645   (save-excursion
2646     (let (point buffer-read-only)
2647       (while (setq point (pop config))
2648         (when (and (< point (point-max))
2649                    (goto-char point)
2650                    (eq (char-after) ?\n))
2651           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2652
2653 ;; Various summary mode internalish functions.
2654
2655 (defun gnus-mouse-pick-article (e)
2656   (interactive "e")
2657   (mouse-set-point e)
2658   (gnus-summary-next-page nil t))
2659
2660 (defun gnus-summary-set-display-table ()
2661   "Change the display table.
2662 Odd characters have a tendency to mess
2663 up nicely formatted displays - we make all possible glyphs
2664 display only a single character."
2665
2666   ;; We start from the standard display table, if any.
2667   (let ((table (or (copy-sequence standard-display-table)
2668                    (make-display-table)))
2669         (i 32))
2670     ;; Nix out all the control chars...
2671     (while (>= (setq i (1- i)) 0)
2672       (aset table i [??]))
2673     ;; ... but not newline and cr, of course.  (cr is necessary for the
2674     ;; selective display).
2675     (aset table ?\n nil)
2676     (aset table ?\r nil)
2677     ;; We keep TAB as well.
2678     (aset table ?\t nil)
2679     ;; We nix out any glyphs over 126 that are not set already.
2680     (let ((i 256))
2681       (while (>= (setq i (1- i)) 127)
2682         ;; Only modify if the entry is nil.
2683         (unless (aref table i)
2684           (aset table i [??]))))
2685     (setq buffer-display-table table)))
2686
2687 (defun gnus-summary-buffer-name (group)
2688   "Return the summary buffer name of GROUP."
2689   (concat "*Summary " group "*"))
2690
2691 (defun gnus-summary-setup-buffer (group)
2692   "Initialize summary buffer."
2693   (let ((buffer (gnus-summary-buffer-name group)))
2694     (if (get-buffer buffer)
2695         (progn
2696           (set-buffer buffer)
2697           (setq gnus-summary-buffer (current-buffer))
2698           (not gnus-newsgroup-prepared))
2699       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2700       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2701       (gnus-summary-mode group)
2702       (when gnus-carpal
2703         (gnus-carpal-setup-buffer 'summary))
2704       (unless gnus-single-article-buffer
2705         (make-local-variable 'gnus-article-buffer)
2706         (make-local-variable 'gnus-article-current)
2707         (make-local-variable 'gnus-original-article-buffer))
2708       (setq gnus-newsgroup-name group)
2709       ;; Set any local variables in the group parameters.
2710       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2711       t)))
2712
2713 (defun gnus-set-global-variables ()
2714   "Set the global equivalents of the buffer-local variables.
2715 They are set to the latest values they had.  These reflect the summary
2716 buffer that was in action when the last article was fetched."
2717   (when (eq major-mode 'gnus-summary-mode)
2718     (setq gnus-summary-buffer (current-buffer))
2719     (let ((name gnus-newsgroup-name)
2720           (marked gnus-newsgroup-marked)
2721           (unread gnus-newsgroup-unreads)
2722           (headers gnus-current-headers)
2723           (data gnus-newsgroup-data)
2724           (summary gnus-summary-buffer)
2725           (article-buffer gnus-article-buffer)
2726           (original gnus-original-article-buffer)
2727           (gac gnus-article-current)
2728           (reffed gnus-reffed-article-number)
2729           (score-file gnus-current-score-file)
2730           (default-charset gnus-newsgroup-charset)
2731           vlist)
2732       (let ((locals gnus-newsgroup-variables))
2733         (while locals
2734           (if (consp (car locals))
2735               (push (eval (caar locals)) vlist)
2736             (push (eval (car locals)) vlist))
2737           (setq locals (cdr locals)))
2738         (setq vlist (nreverse vlist)))
2739       (save-excursion
2740         (set-buffer gnus-group-buffer)
2741         (setq gnus-newsgroup-name name
2742               gnus-newsgroup-marked marked
2743               gnus-newsgroup-unreads unread
2744               gnus-current-headers headers
2745               gnus-newsgroup-data data
2746               gnus-article-current gac
2747               gnus-summary-buffer summary
2748               gnus-article-buffer article-buffer
2749               gnus-original-article-buffer original
2750               gnus-reffed-article-number reffed
2751               gnus-current-score-file score-file
2752               gnus-newsgroup-charset default-charset)
2753         (let ((locals gnus-newsgroup-variables))
2754           (while locals
2755             (if (consp (car locals))
2756                 (set (caar locals) (pop vlist))
2757               (set (car locals) (pop vlist)))
2758             (setq locals (cdr locals))))
2759         ;; The article buffer also has local variables.
2760         (when (gnus-buffer-live-p gnus-article-buffer)
2761           (set-buffer gnus-article-buffer)
2762           (setq gnus-summary-buffer summary))))))
2763
2764 (defun gnus-summary-article-unread-p (article)
2765   "Say whether ARTICLE is unread or not."
2766   (memq article gnus-newsgroup-unreads))
2767
2768 (defun gnus-summary-first-article-p (&optional article)
2769   "Return whether ARTICLE is the first article in the buffer."
2770   (if (not (setq article (or article (gnus-summary-article-number))))
2771       nil
2772     (eq article (caar gnus-newsgroup-data))))
2773
2774 (defun gnus-summary-last-article-p (&optional article)
2775   "Return whether ARTICLE is the last article in the buffer."
2776   (if (not (setq article (or article (gnus-summary-article-number))))
2777       ;; All non-existent numbers are the last article.  :-)
2778       t
2779     (not (cdr (gnus-data-find-list article)))))
2780
2781 (defun gnus-make-thread-indent-array ()
2782   (let ((n 200))
2783     (unless (and gnus-thread-indent-array
2784                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2785       (setq gnus-thread-indent-array (make-vector 201 "")
2786             gnus-thread-indent-array-level gnus-thread-indent-level)
2787       (while (>= n 0)
2788         (aset gnus-thread-indent-array n
2789               (make-string (* n gnus-thread-indent-level) ? ))
2790         (setq n (1- n))))))
2791
2792 (defun gnus-update-summary-mark-positions ()
2793   "Compute where the summary marks are to go."
2794   (save-excursion
2795     (when (gnus-buffer-exists-p gnus-summary-buffer)
2796       (set-buffer gnus-summary-buffer))
2797     (let ((gnus-replied-mark 129)
2798           (gnus-score-below-mark 130)
2799           (gnus-score-over-mark 130)
2800           (gnus-download-mark 131)
2801           (spec gnus-summary-line-format-spec)
2802           gnus-visual pos)
2803       (save-excursion
2804         (gnus-set-work-buffer)
2805         (let ((gnus-summary-line-format-spec spec)
2806               (gnus-newsgroup-downloadable '((0 . t))))
2807           (gnus-summary-insert-line
2808            (make-full-mail-header 0 "" "nobody"
2809                                   "05 Apr 2001 23:33:09 +0400"
2810                                   "" "" 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 -1))
2905     (when (= gnus-tmp-lines -1)
2906       (setq gnus-tmp-lines "?"))
2907     (gnus-put-text-property-excluding-characters-with-faces
2908      (point)
2909      (progn (eval gnus-summary-line-format-spec) (point))
2910      'gnus-number gnus-tmp-number)
2911     (when (gnus-visual-p 'summary-highlight 'highlight)
2912       (forward-line -1)
2913       (gnus-run-hooks 'gnus-summary-update-hook)
2914       (forward-line 1))))
2915
2916 (defun gnus-summary-update-line (&optional dont-update)
2917   "Update summary line after change."
2918   (when (and gnus-summary-default-score
2919              (not gnus-summary-inhibit-highlight))
2920     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2921            (article (gnus-summary-article-number))
2922            (score (gnus-summary-article-score article)))
2923       (unless dont-update
2924         (if (and gnus-summary-mark-below
2925                  (< (gnus-summary-article-score)
2926                     gnus-summary-mark-below))
2927             ;; This article has a low score, so we mark it as read.
2928             (when (memq article gnus-newsgroup-unreads)
2929               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2930           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2931             ;; This article was previously marked as read on account
2932             ;; of a low score, but now it has risen, so we mark it as
2933             ;; unread.
2934             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2935         (gnus-summary-update-mark
2936          (if (or (null gnus-summary-default-score)
2937                  (<= (abs (- score gnus-summary-default-score))
2938                      gnus-summary-zcore-fuzz))
2939              ?  ;Whitespace
2940            (if (< score gnus-summary-default-score)
2941                gnus-score-below-mark gnus-score-over-mark))
2942          'score))
2943       ;; Do visual highlighting.
2944       (when (gnus-visual-p 'summary-highlight 'highlight)
2945         (gnus-run-hooks 'gnus-summary-update-hook)))))
2946
2947 (defvar gnus-tmp-new-adopts nil)
2948
2949 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2950   "Return the number of articles in THREAD.
2951 This may be 0 in some cases -- if none of the articles in
2952 the thread are to be displayed."
2953   (let* ((number
2954           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2955           (cond
2956            ((not (listp thread))
2957             1)
2958            ((and (consp thread) (cdr thread))
2959             (apply
2960              '+ 1 (mapcar
2961                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2962            ((null thread)
2963             1)
2964            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2965             1)
2966            (t 0))))
2967     (when (and level (zerop level) gnus-tmp-new-adopts)
2968       (incf number
2969             (apply '+ (mapcar
2970                        'gnus-summary-number-of-articles-in-thread
2971                        gnus-tmp-new-adopts))))
2972     (if char
2973         (if (> number 1) gnus-not-empty-thread-mark
2974           gnus-empty-thread-mark)
2975       number)))
2976
2977 (defun gnus-summary-set-local-parameters (group)
2978   "Go through the local params of GROUP and set all variable specs in that list."
2979   (let ((params (gnus-group-find-parameter group))
2980         elem)
2981     (while params
2982       (setq elem (car params)
2983             params (cdr params))
2984       (and (consp elem)                 ; Has to be a cons.
2985            (consp (cdr elem))           ; The cdr has to be a list.
2986            (symbolp (car elem))         ; Has to be a symbol in there.
2987            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2988            (ignore-errors               ; So we set it.
2989              (make-local-variable (car elem))
2990              (set (car elem) (eval (nth 1 elem))))))))
2991
2992 (defun gnus-summary-read-group (group &optional show-all no-article
2993                                       kill-buffer no-display backward
2994                                       select-articles)
2995   "Start reading news in newsgroup GROUP.
2996 If SHOW-ALL is non-nil, already read articles are also listed.
2997 If NO-ARTICLE is non-nil, no article is selected initially.
2998 If NO-DISPLAY, don't generate a summary buffer."
2999   (let (result)
3000     (while (and group
3001                 (null (setq result
3002                             (let ((gnus-auto-select-next nil))
3003                               (or (gnus-summary-read-group-1
3004                                    group show-all no-article
3005                                    kill-buffer no-display
3006                                    select-articles)
3007                                   (setq show-all nil
3008                                         select-articles nil)))))
3009                 (eq gnus-auto-select-next 'quietly))
3010       (set-buffer gnus-group-buffer)
3011       ;; The entry function called above goes to the next
3012       ;; group automatically, so we go two groups back
3013       ;; if we are searching for the previous group.
3014       (when backward
3015         (gnus-group-prev-unread-group 2))
3016       (if (not (equal group (gnus-group-group-name)))
3017           (setq group (gnus-group-group-name))
3018         (setq group nil)))
3019     result))
3020
3021 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3022   "Directly jump to the other GROUP from summary buffer.
3023 If SHOW-ALL is non-nil, already read articles are also listed."
3024   (interactive
3025    (if (eq gnus-summary-buffer (current-buffer))
3026        (list (completing-read
3027               "Group: " gnus-active-hashtb nil t
3028               (when (and gnus-newsgroup-name
3029                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3030                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3031               'gnus-group-history)
3032              current-prefix-arg)
3033      (error "%s must be invoked from a gnus summary buffer." this-command)))
3034   (unless (or (zerop (length group))
3035               (and gnus-newsgroup-name
3036                    (string-equal gnus-newsgroup-name group)))
3037     (gnus-summary-exit)
3038     (gnus-summary-read-group group show-all
3039                              gnus-dont-select-after-jump-to-other-group)))
3040
3041 (defun gnus-summary-read-group-1 (group show-all no-article
3042                                         kill-buffer no-display
3043                                         &optional select-articles)
3044   ;; Killed foreign groups can't be entered.
3045   ;;  (when (and (not (gnus-group-native-p group))
3046   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3047   ;;    (error "Dead non-native groups can't be entered"))
3048   (gnus-message 5 "Retrieving newsgroup: %s..."
3049                 (gnus-group-decoded-name group))
3050   (let* ((new-group (gnus-summary-setup-buffer group))
3051          (quit-config (gnus-group-quit-config group))
3052          (did-select (and new-group (gnus-select-newsgroup
3053                                      group show-all select-articles))))
3054     (cond
3055      ;; This summary buffer exists already, so we just select it.
3056      ((not new-group)
3057       (gnus-set-global-variables)
3058       (when kill-buffer
3059         (gnus-kill-or-deaden-summary kill-buffer))
3060       (gnus-configure-windows 'summary 'force)
3061       (gnus-set-mode-line 'summary)
3062       (gnus-summary-position-point)
3063       (message "")
3064       t)
3065      ;; We couldn't select this group.
3066      ((null did-select)
3067       (when (and (eq major-mode 'gnus-summary-mode)
3068                  (not (equal (current-buffer) kill-buffer)))
3069         (kill-buffer (current-buffer))
3070         (if (not quit-config)
3071             (progn
3072               ;; Update the info -- marks might need to be removed,
3073               ;; for instance.
3074               (gnus-summary-update-info)
3075               (set-buffer gnus-group-buffer)
3076               (gnus-group-jump-to-group group)
3077               (gnus-group-next-unread-group 1))
3078           (gnus-handle-ephemeral-exit quit-config)))
3079       (let ((grpinfo (gnus-get-info group)))
3080         (if (null (gnus-info-read grpinfo))
3081             (gnus-message 3 "Group %s contains no messages" 
3082                           (gnus-group-decoded-name group))
3083           (gnus-message 3 "Can't select group")))
3084       nil)
3085      ;; The user did a `C-g' while prompting for number of articles,
3086      ;; so we exit this group.
3087      ((eq did-select 'quit)
3088       (and (eq major-mode 'gnus-summary-mode)
3089            (not (equal (current-buffer) kill-buffer))
3090            (kill-buffer (current-buffer)))
3091       (when kill-buffer
3092         (gnus-kill-or-deaden-summary kill-buffer))
3093       (if (not quit-config)
3094           (progn
3095             (set-buffer gnus-group-buffer)
3096             (gnus-group-jump-to-group group)
3097             (gnus-group-next-unread-group 1)
3098             (gnus-configure-windows 'group 'force))
3099         (gnus-handle-ephemeral-exit quit-config))
3100       ;; Finally signal the quit.
3101       (signal 'quit nil))
3102      ;; The group was successfully selected.
3103      (t
3104       (gnus-set-global-variables)
3105       ;; Save the active value in effect when the group was entered.
3106       (setq gnus-newsgroup-active
3107             (gnus-copy-sequence
3108              (gnus-active gnus-newsgroup-name)))
3109       ;; You can change the summary buffer in some way with this hook.
3110       (gnus-run-hooks 'gnus-select-group-hook)
3111       (gnus-update-format-specifications
3112        nil 'summary 'summary-mode 'summary-dummy)
3113       (gnus-update-summary-mark-positions)
3114       ;; Do score processing.
3115       (when gnus-use-scoring
3116         (gnus-possibly-score-headers))
3117       ;; Check whether to fill in the gaps in the threads.
3118       (when gnus-build-sparse-threads
3119         (gnus-build-sparse-threads))
3120       ;; Find the initial limit.
3121       (if gnus-show-threads
3122           (if show-all
3123               (let ((gnus-newsgroup-dormant nil))
3124                 (gnus-summary-initial-limit show-all))
3125             (gnus-summary-initial-limit show-all))
3126         ;; When untreaded, all articles are always shown.
3127         (setq gnus-newsgroup-limit
3128               (mapcar
3129                (lambda (header) (mail-header-number header))
3130                gnus-newsgroup-headers)))
3131       ;; Generate the summary buffer.
3132       (unless no-display
3133         (gnus-summary-prepare))
3134       (when gnus-use-trees
3135         (gnus-tree-open group)
3136         (setq gnus-summary-highlight-line-function
3137               'gnus-tree-highlight-article))
3138       ;; If the summary buffer is empty, but there are some low-scored
3139       ;; articles or some excluded dormants, we include these in the
3140       ;; buffer.
3141       (when (and (zerop (buffer-size))
3142                  (not no-display))
3143         (cond (gnus-newsgroup-dormant
3144                (gnus-summary-limit-include-dormant))
3145               ((and gnus-newsgroup-scored show-all)
3146                (gnus-summary-limit-include-expunged t))))
3147       ;; Function `gnus-apply-kill-file' must be called in this hook.
3148       (gnus-run-hooks 'gnus-apply-kill-hook)
3149       (if (and (zerop (buffer-size))
3150                (not no-display))
3151           (progn
3152             ;; This newsgroup is empty.
3153             (gnus-summary-catchup-and-exit nil t)
3154             (gnus-message 6 "No unread news")
3155             (when kill-buffer
3156               (gnus-kill-or-deaden-summary kill-buffer))
3157             ;; Return nil from this function.
3158             nil)
3159         ;; Hide conversation thread subtrees.  We cannot do this in
3160         ;; gnus-summary-prepare-hook since kill processing may not
3161         ;; work with hidden articles.
3162         (and gnus-show-threads
3163              gnus-thread-hide-subtree
3164              (gnus-summary-hide-all-threads))
3165         (when kill-buffer
3166           (gnus-kill-or-deaden-summary kill-buffer))
3167         ;; Show first unread article if requested.
3168         (if (and (not no-article)
3169                  (not no-display)
3170                  gnus-newsgroup-unreads
3171                  gnus-auto-select-first)
3172             (progn
3173               (gnus-configure-windows 'summary)
3174               (cond
3175                ((eq gnus-auto-select-first 'best)
3176                 (gnus-summary-best-unread-article))
3177                ((eq gnus-auto-select-first t)
3178                 (gnus-summary-first-unread-article))
3179                ((gnus-functionp gnus-auto-select-first)
3180                 (funcall gnus-auto-select-first))))
3181           ;; Don't select any articles, just move point to the first
3182           ;; article in the group.
3183           (goto-char (point-min))
3184           (gnus-summary-position-point)
3185           (gnus-configure-windows 'summary 'force)
3186           (gnus-set-mode-line 'summary))
3187         (when (get-buffer-window gnus-group-buffer t)
3188           ;; Gotta use windows, because recenter does weird stuff if
3189           ;; the current buffer ain't the displayed window.
3190           (let ((owin (selected-window)))
3191             (select-window (get-buffer-window gnus-group-buffer t))
3192             (when (gnus-group-goto-group group)
3193               (recenter))
3194             (select-window owin)))
3195         ;; Mark this buffer as "prepared".
3196         (setq gnus-newsgroup-prepared t)
3197         (gnus-run-hooks 'gnus-summary-prepared-hook)
3198         t)))))
3199
3200 (defun gnus-summary-prepare ()
3201   "Generate the summary buffer."
3202   (interactive)
3203   (let ((buffer-read-only nil))
3204     (erase-buffer)
3205     (setq gnus-newsgroup-data nil
3206           gnus-newsgroup-data-reverse nil)
3207     (gnus-run-hooks 'gnus-summary-generate-hook)
3208     ;; Generate the buffer, either with threads or without.
3209     (when gnus-newsgroup-headers
3210       (gnus-summary-prepare-threads
3211        (if gnus-show-threads
3212            (gnus-sort-gathered-threads
3213             (funcall gnus-summary-thread-gathering-function
3214                      (gnus-sort-threads
3215                       (gnus-cut-threads (gnus-make-threads)))))
3216          ;; Unthreaded display.
3217          (gnus-sort-articles gnus-newsgroup-headers))))
3218     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3219     ;; Call hooks for modifying summary buffer.
3220     (goto-char (point-min))
3221     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3222
3223 (defsubst gnus-general-simplify-subject (subject)
3224   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3225   (setq subject
3226         (cond
3227          ;; Truncate the subject.
3228          (gnus-simplify-subject-functions
3229           (gnus-map-function gnus-simplify-subject-functions subject))
3230          ((numberp gnus-summary-gather-subject-limit)
3231           (setq subject (gnus-simplify-subject-re subject))
3232           (if (> (length subject) gnus-summary-gather-subject-limit)
3233               (substring subject 0 gnus-summary-gather-subject-limit)
3234             subject))
3235          ;; Fuzzily simplify it.
3236          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3237           (gnus-simplify-subject-fuzzy subject))
3238          ;; Just remove the leading "Re:".
3239          (t
3240           (gnus-simplify-subject-re subject))))
3241
3242   (if (and gnus-summary-gather-exclude-subject
3243            (string-match gnus-summary-gather-exclude-subject subject))
3244       nil                               ; This article shouldn't be gathered
3245     subject))
3246
3247 (defun gnus-summary-simplify-subject-query ()
3248   "Query where the respool algorithm would put this article."
3249   (interactive)
3250   (gnus-summary-select-article)
3251   (message "%s"
3252            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3253
3254 (defun gnus-gather-threads-by-subject (threads)
3255   "Gather threads by looking at Subject headers."
3256   (if (not gnus-summary-make-false-root)
3257       threads
3258     (let ((hashtb (gnus-make-hashtable 1024))
3259           (prev threads)
3260           (result threads)
3261           subject hthread whole-subject)
3262       (while threads
3263         (setq subject (gnus-general-simplify-subject
3264                        (setq whole-subject (mail-header-subject
3265                                             (caar threads)))))
3266         (when subject
3267           (if (setq hthread (gnus-gethash subject hashtb))
3268               (progn
3269                 ;; We enter a dummy root into the thread, if we
3270                 ;; haven't done that already.
3271                 (unless (stringp (caar hthread))
3272                   (setcar hthread (list whole-subject (car hthread))))
3273                 ;; We add this new gathered thread to this gathered
3274                 ;; thread.
3275                 (setcdr (car hthread)
3276                         (nconc (cdar hthread) (list (car threads))))
3277                 ;; Remove it from the list of threads.
3278                 (setcdr prev (cdr threads))
3279                 (setq threads prev))
3280             ;; Enter this thread into the hash table.
3281             (gnus-sethash subject threads hashtb)))
3282         (setq prev threads)
3283         (setq threads (cdr threads)))
3284       result)))
3285
3286 (defun gnus-gather-threads-by-references (threads)
3287   "Gather threads by looking at References headers."
3288   (let ((idhashtb (gnus-make-hashtable 1024))
3289         (thhashtb (gnus-make-hashtable 1024))
3290         (prev threads)
3291         (result threads)
3292         ids references id gthread gid entered ref)
3293     (while threads
3294       (when (setq references (mail-header-references (caar threads)))
3295         (setq id (mail-header-id (caar threads))
3296               ids (gnus-split-references references)
3297               entered nil)
3298         (while (setq ref (pop ids))
3299           (setq ids (delete ref ids))
3300           (if (not (setq gid (gnus-gethash ref idhashtb)))
3301               (progn
3302                 (gnus-sethash ref id idhashtb)
3303                 (gnus-sethash id threads thhashtb))
3304             (setq gthread (gnus-gethash gid thhashtb))
3305             (unless entered
3306               ;; We enter a dummy root into the thread, if we
3307               ;; haven't done that already.
3308               (unless (stringp (caar gthread))
3309                 (setcar gthread (list (mail-header-subject (caar gthread))
3310                                       (car gthread))))
3311               ;; We add this new gathered thread to this gathered
3312               ;; thread.
3313               (setcdr (car gthread)
3314                       (nconc (cdar gthread) (list (car threads)))))
3315             ;; Add it into the thread hash table.
3316             (gnus-sethash id gthread thhashtb)
3317             (setq entered t)
3318             ;; Remove it from the list of threads.
3319             (setcdr prev (cdr threads))
3320             (setq threads prev))))
3321       (setq prev threads)
3322       (setq threads (cdr threads)))
3323     result))
3324
3325 (defun gnus-sort-gathered-threads (threads)
3326   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3327   (let ((result threads))
3328     (while threads
3329       (when (stringp (caar threads))
3330         (setcdr (car threads)
3331                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3332       (setq threads (cdr threads)))
3333     result))
3334
3335 (defun gnus-thread-loop-p (root thread)
3336   "Say whether ROOT is in THREAD."
3337   (let ((stack (list thread))
3338         (infloop 0)
3339         th)
3340     (while (setq thread (pop stack))
3341       (setq th (cdr thread))
3342       (while (and th
3343                   (not (eq (caar th) root)))
3344         (pop th))
3345       (if th
3346           ;; We have found a loop.
3347           (let (ref-dep)
3348             (setcdr thread (delq (car th) (cdr thread)))
3349             (if (boundp (setq ref-dep (intern "none"
3350                                               gnus-newsgroup-dependencies)))
3351                 (setcdr (symbol-value ref-dep)
3352                         (nconc (cdr (symbol-value ref-dep))
3353                                (list (car th))))
3354               (set ref-dep (list nil (car th))))
3355             (setq infloop 1
3356                   stack nil))
3357         ;; Push all the subthreads onto the stack.
3358         (push (cdr thread) stack)))
3359     infloop))
3360
3361 (defun gnus-make-threads ()
3362   "Go through the dependency hashtb and find the roots.  Return all threads."
3363   (let (threads)
3364     (while (catch 'infloop
3365              (mapatoms
3366               (lambda (refs)
3367                 ;; Deal with self-referencing References loops.
3368                 (when (and (car (symbol-value refs))
3369                            (not (zerop
3370                                  (apply
3371                                   '+
3372                                   (mapcar
3373                                    (lambda (thread)
3374                                      (gnus-thread-loop-p
3375                                       (car (symbol-value refs)) thread))
3376                                    (cdr (symbol-value refs)))))))
3377                   (setq threads nil)
3378                   (throw 'infloop t))
3379                 (unless (car (symbol-value refs))
3380                   ;; These threads do not refer back to any other articles,
3381                   ;; so they're roots.
3382                   (setq threads (append (cdr (symbol-value refs)) threads))))
3383               gnus-newsgroup-dependencies)))
3384     threads))
3385
3386 ;; Build the thread tree.
3387 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3388   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3389
3390 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3391 if it was already present.
3392
3393 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3394 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3395 Message-IDs will be renamed be renamed to a unique Message-ID before
3396 being entered.
3397
3398 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3399   (let* ((id (mail-header-id header))
3400          (id-dep (and id (intern id dependencies)))
3401          ref ref-dep ref-header)
3402     ;; Enter this `header' in the `dependencies' table.
3403     (cond
3404      ((not id-dep)
3405       (setq header nil))
3406      ;; The first two cases do the normal part: enter a new `header'
3407      ;; in the `dependencies' table.
3408      ((not (boundp id-dep))
3409       (set id-dep (list header)))
3410      ((null (car (symbol-value id-dep)))
3411       (setcar (symbol-value id-dep) header))
3412
3413      ;; From here the `header' was already present in the
3414      ;; `dependencies' table.
3415      (force-new
3416       ;; Overrides an existing entry;
3417       ;; just set the header part of the entry.
3418       (setcar (symbol-value id-dep) header))
3419
3420      ;; Renames the existing `header' to a unique Message-ID.
3421      ((not gnus-summary-ignore-duplicates)
3422       ;; An article with this Message-ID has already been seen.
3423       ;; We rename the Message-ID.
3424       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3425            (list header))
3426       (mail-header-set-id header id))
3427
3428      ;; The last case ignores an existing entry, except it adds any
3429      ;; additional Xrefs (in case the two articles came from different
3430      ;; servers.
3431      ;; Also sets `header' to `nil' meaning that the `dependencies'
3432      ;; table was *not* modified.
3433      (t
3434       (mail-header-set-xref
3435        (car (symbol-value id-dep))
3436        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3437                    "")
3438                (or (mail-header-xref header) "")))
3439       (setq header nil)))
3440
3441     (when header
3442       ;; First check if that we are not creating a References loop.
3443       (setq ref (gnus-parent-id (mail-header-references header)))
3444       (while (and ref
3445                   (setq ref-dep (intern-soft ref dependencies))
3446                   (boundp ref-dep)
3447                   (setq ref-header (car (symbol-value ref-dep))))
3448         (if (string= id ref)
3449             ;; Yuk!  This is a reference loop.  Make the article be a
3450             ;; root article.
3451             (progn
3452               (mail-header-set-references (car (symbol-value id-dep)) "none")
3453               (setq ref nil))
3454           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3455       (setq ref (gnus-parent-id (mail-header-references header)))
3456       (setq ref-dep (intern (or ref "none") dependencies))
3457       (if (boundp ref-dep)
3458           (setcdr (symbol-value ref-dep)
3459                   (nconc (cdr (symbol-value ref-dep))
3460                          (list (symbol-value id-dep))))
3461         (set ref-dep (list nil (symbol-value id-dep)))))
3462     header))
3463
3464 (defun gnus-build-sparse-threads ()
3465   (let ((headers gnus-newsgroup-headers)
3466         (mail-parse-charset gnus-newsgroup-charset)
3467         (gnus-summary-ignore-duplicates t)
3468         header references generation relations
3469         subject child end new-child date)
3470     ;; First we create an alist of generations/relations, where
3471     ;; generations is how much we trust the relation, and the relation
3472     ;; is parent/child.
3473     (gnus-message 7 "Making sparse threads...")
3474     (save-excursion
3475       (nnheader-set-temp-buffer " *gnus sparse threads*")
3476       (while (setq header (pop headers))
3477         (when (and (setq references (mail-header-references header))
3478                    (not (string= references "")))
3479           (insert references)
3480           (setq child (mail-header-id header)
3481                 subject (mail-header-subject header)
3482                 date (mail-header-date header)
3483                 generation 0)
3484           (while (search-backward ">" nil t)
3485             (setq end (1+ (point)))
3486             (when (search-backward "<" nil t)
3487               (setq new-child (buffer-substring (point) end))
3488               (push (list (incf generation)
3489                           child (setq child new-child)
3490                           subject date)
3491                     relations)))
3492           (when child
3493             (push (list (1+ generation) child nil subject) relations))
3494           (erase-buffer)))
3495       (kill-buffer (current-buffer)))
3496     ;; Sort over trustworthiness.
3497     (mapcar
3498      (lambda (relation)
3499        (when (gnus-dependencies-add-header
3500               (make-full-mail-header-from-decoded-header
3501                gnus-reffed-article-number
3502                (nth 3 relation) "" (or (nth 4 relation) "")
3503                (nth 1 relation)
3504                (or (nth 2 relation) "") 0 0 "")
3505               gnus-newsgroup-dependencies nil)
3506          (push gnus-reffed-article-number gnus-newsgroup-limit)
3507          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3508          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3509                gnus-newsgroup-reads)
3510          (decf gnus-reffed-article-number)))
3511      (sort relations 'car-less-than-car))
3512     (gnus-message 7 "Making sparse threads...done")))
3513
3514 (defun gnus-build-old-threads ()
3515   ;; Look at all the articles that refer back to old articles, and
3516   ;; fetch the headers for the articles that aren't there.  This will
3517   ;; build complete threads - if the roots haven't been expired by the
3518   ;; server, that is.
3519   (let ((mail-parse-charset gnus-newsgroup-charset)
3520         id heads)
3521     (mapatoms
3522      (lambda (refs)
3523        (when (not (car (symbol-value refs)))
3524          (setq heads (cdr (symbol-value refs)))
3525          (while heads
3526            (if (memq (mail-header-number (caar heads))
3527                      gnus-newsgroup-dormant)
3528                (setq heads (cdr heads))
3529              (setq id (symbol-name refs))
3530              (while (and (setq id (gnus-build-get-header id))
3531                          (not (car (gnus-id-to-thread id)))))
3532              (setq heads nil)))))
3533      gnus-newsgroup-dependencies)))
3534
3535 ;; This function has to be called with point after the article number
3536 ;; on the beginning of the line.
3537 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3538   (let ((eol (gnus-point-at-eol))
3539         (buffer (current-buffer))
3540         header)
3541
3542     ;; overview: [num subject from date id refs chars lines misc]
3543     (unwind-protect
3544         (progn
3545           (narrow-to-region (point) eol)
3546           (unless (eobp)
3547             (forward-char))
3548
3549           (setq header
3550                 (make-full-mail-header
3551                  number                         ; number
3552                  (nnheader-nov-field)           ; subject
3553                  (nnheader-nov-field)           ; from
3554                  (nnheader-nov-field)           ; date
3555                  (nnheader-nov-read-message-id) ; id
3556                  (nnheader-nov-field)           ; refs
3557                  (nnheader-nov-read-integer)    ; chars
3558                  (nnheader-nov-read-integer)    ; lines
3559                  (unless (eobp)
3560                    (if (looking-at "Xref: ")
3561                        (goto-char (match-end 0)))
3562                    (nnheader-nov-field))        ; Xref
3563                  (nnheader-nov-parse-extra))))  ; extra
3564
3565       (widen))
3566
3567     (when gnus-alter-header-function
3568       (funcall gnus-alter-header-function header))
3569     (gnus-dependencies-add-header header dependencies force-new)))
3570
3571 (defun gnus-build-get-header (id)
3572   "Look through the buffer of NOV lines and find the header to ID.
3573 Enter this line into the dependencies hash table, and return
3574 the id of the parent article (if any)."
3575   (let ((deps gnus-newsgroup-dependencies)
3576         found header)
3577     (prog1
3578         (save-excursion
3579           (set-buffer nntp-server-buffer)
3580           (let ((case-fold-search nil))
3581             (goto-char (point-min))
3582             (while (and (not found)
3583                         (search-forward id nil t))
3584               (beginning-of-line)
3585               (setq found (looking-at
3586                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3587                                    (regexp-quote id))))
3588               (or found (beginning-of-line 2)))
3589             (when found
3590               (beginning-of-line)
3591               (and
3592                (setq header (gnus-nov-parse-line
3593                              (read (current-buffer)) deps))
3594                (gnus-parent-id (mail-header-references header))))))
3595       (when header
3596         (let ((number (mail-header-number header)))
3597           (push number gnus-newsgroup-limit)
3598           (push header gnus-newsgroup-headers)
3599           (if (memq number gnus-newsgroup-unselected)
3600               (progn
3601                 (push number gnus-newsgroup-unreads)
3602                 (setq gnus-newsgroup-unselected
3603                       (delq number gnus-newsgroup-unselected)))
3604             (push number gnus-newsgroup-ancient)))))))
3605
3606 (defun gnus-build-all-threads ()
3607   "Read all the headers."
3608   (let ((gnus-summary-ignore-duplicates t)
3609         (mail-parse-charset gnus-newsgroup-charset)
3610         (dependencies gnus-newsgroup-dependencies)
3611         header article)
3612     (save-excursion
3613       (set-buffer nntp-server-buffer)
3614       (let ((case-fold-search nil))
3615         (goto-char (point-min))
3616         (while (not (eobp))
3617           (ignore-errors
3618             (setq article (read (current-buffer))
3619                   header (gnus-nov-parse-line article dependencies)))
3620           (when header
3621             (save-excursion
3622               (set-buffer gnus-summary-buffer)
3623               (push header gnus-newsgroup-headers)
3624               (if (memq (setq article (mail-header-number header))
3625                         gnus-newsgroup-unselected)
3626                   (progn
3627                     (push article gnus-newsgroup-unreads)
3628                     (setq gnus-newsgroup-unselected
3629                           (delq article gnus-newsgroup-unselected)))
3630                 (push article gnus-newsgroup-ancient)))
3631             (forward-line 1)))))))
3632
3633 (defun gnus-summary-update-article-line (article header)
3634   "Update the line for ARTICLE using HEADERS."
3635   (let* ((id (mail-header-id header))
3636          (thread (gnus-id-to-thread id)))
3637     (unless thread
3638       (error "Article in no thread"))
3639     ;; Update the thread.
3640     (setcar thread header)
3641     (gnus-summary-goto-subject article)
3642     (let* ((datal (gnus-data-find-list article))
3643            (data (car datal))
3644            (length (when (cdr datal)
3645                      (- (gnus-data-pos data)
3646                         (gnus-data-pos (cadr datal)))))
3647            (buffer-read-only nil)
3648            (level (gnus-summary-thread-level)))
3649       (gnus-delete-line)
3650       (gnus-summary-insert-line
3651        header level nil (gnus-article-mark article)
3652        (memq article gnus-newsgroup-replied)
3653        (memq article gnus-newsgroup-expirable)
3654        ;; Only insert the Subject string when it's different
3655        ;; from the previous Subject string.
3656        (if (and
3657             gnus-show-threads
3658             (gnus-subject-equal
3659              (condition-case ()
3660                  (mail-header-subject
3661                   (gnus-data-header
3662                    (cadr
3663                     (gnus-data-find-list
3664                      article
3665                      (gnus-data-list t)))))
3666                ;; Error on the side of excessive subjects.
3667                (error ""))
3668              (mail-header-subject header)))
3669            ""
3670          (mail-header-subject header))
3671        nil (cdr (assq article gnus-newsgroup-scored))
3672        (memq article gnus-newsgroup-processable))
3673       (when length
3674         (gnus-data-update-list
3675          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3676
3677 (defun gnus-summary-update-article (article &optional iheader)
3678   "Update ARTICLE in the summary buffer."
3679   (set-buffer gnus-summary-buffer)
3680   (let* ((header (gnus-summary-article-header article))
3681          (id (mail-header-id header))
3682          (data (gnus-data-find article))
3683          (thread (gnus-id-to-thread id))
3684          (references (mail-header-references header))
3685          (parent
3686           (gnus-id-to-thread
3687            (or (gnus-parent-id
3688                 (when (and references
3689                            (not (equal "" references)))
3690                   references))
3691                "none")))
3692          (buffer-read-only nil)
3693          (old (car thread)))
3694     (when thread
3695       (unless iheader
3696         (setcar thread nil)
3697         (when parent
3698           (delq thread parent)))
3699       (if (gnus-summary-insert-subject id header)
3700           ;; Set the (possibly) new article number in the data structure.
3701           (gnus-data-set-number data (gnus-id-to-article id))
3702         (setcar thread old)
3703         nil))))
3704
3705 (defun gnus-rebuild-thread (id &optional line)
3706   "Rebuild the thread containing ID.
3707 If LINE, insert the rebuilt thread starting on line LINE."
3708   (let ((buffer-read-only nil)
3709         old-pos current thread data)
3710     (if (not gnus-show-threads)
3711         (setq thread (list (car (gnus-id-to-thread id))))
3712       ;; Get the thread this article is part of.
3713       (setq thread (gnus-remove-thread id)))
3714     (setq old-pos (gnus-point-at-bol))
3715     (setq current (save-excursion
3716                     (and (zerop (forward-line -1))
3717                          (gnus-summary-article-number))))
3718     ;; If this is a gathered thread, we have to go some re-gathering.
3719     (when (stringp (car thread))
3720       (let ((subject (car thread))
3721             roots thr)
3722         (setq thread (cdr thread))
3723         (while thread
3724           (unless (memq (setq thr (gnus-id-to-thread
3725                                    (gnus-root-id
3726                                     (mail-header-id (caar thread)))))
3727                         roots)
3728             (push thr roots))
3729           (setq thread (cdr thread)))
3730         ;; We now have all (unique) roots.
3731         (if (= (length roots) 1)
3732             ;; All the loose roots are now one solid root.
3733             (setq thread (car roots))
3734           (setq thread (cons subject (gnus-sort-threads roots))))))
3735     (let (threads)
3736       ;; We then insert this thread into the summary buffer.
3737       (when line
3738         (goto-char (point-min))
3739         (forward-line (1- line)))
3740       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3741         (if gnus-show-threads
3742             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3743           (gnus-summary-prepare-unthreaded thread))
3744         (setq data (nreverse gnus-newsgroup-data))
3745         (setq threads gnus-newsgroup-threads))
3746       ;; We splice the new data into the data structure.
3747       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3748       ;;!!! then we want to insert at the beginning of the buffer.
3749       ;;!!! That happens to be true with Gnus now, but that may
3750       ;;!!! change in the future.  Perhaps.
3751       (gnus-data-enter-list
3752        (if line nil current) data (- (point) old-pos))
3753       (setq gnus-newsgroup-threads
3754             (nconc threads gnus-newsgroup-threads))
3755       (gnus-data-compute-positions))))
3756
3757 (defun gnus-number-to-header (number)
3758   "Return the header for article NUMBER."
3759   (let ((headers gnus-newsgroup-headers))
3760     (while (and headers
3761                 (not (= number (mail-header-number (car headers)))))
3762       (pop headers))
3763     (when headers
3764       (car headers))))
3765
3766 (defun gnus-parent-headers (in-headers &optional generation)
3767   "Return the headers of the GENERATIONeth parent of HEADERS."
3768   (unless generation
3769     (setq generation 1))
3770   (let ((parent t)
3771         (headers in-headers)
3772         references)
3773     (while (and parent
3774                 (not (zerop generation))
3775                 (setq references (mail-header-references headers)))
3776       (setq headers (if (and references
3777                              (setq parent (gnus-parent-id references)))
3778                         (car (gnus-id-to-thread parent))
3779                       nil))
3780       (decf generation))
3781     (and (not (eq headers in-headers))
3782          headers)))
3783
3784 (defun gnus-id-to-thread (id)
3785   "Return the (sub-)thread where ID appears."
3786   (gnus-gethash id gnus-newsgroup-dependencies))
3787
3788 (defun gnus-id-to-article (id)
3789   "Return the article number of ID."
3790   (let ((thread (gnus-id-to-thread id)))
3791     (when (and thread
3792                (car thread))
3793       (mail-header-number (car thread)))))
3794
3795 (defun gnus-id-to-header (id)
3796   "Return the article headers of ID."
3797   (car (gnus-id-to-thread id)))
3798
3799 (defun gnus-article-displayed-root-p (article)
3800   "Say whether ARTICLE is a root(ish) article."
3801   (let ((level (gnus-summary-thread-level article))
3802         (refs (mail-header-references  (gnus-summary-article-header article)))
3803         particle)
3804     (cond
3805      ((null level) nil)
3806      ((zerop level) t)
3807      ((null refs) t)
3808      ((null (gnus-parent-id refs)) t)
3809      ((and (= 1 level)
3810            (null (setq particle (gnus-id-to-article
3811                                  (gnus-parent-id refs))))
3812            (null (gnus-summary-thread-level particle)))))))
3813
3814 (defun gnus-root-id (id)
3815   "Return the id of the root of the thread where ID appears."
3816   (let (last-id prev)
3817     (while (and id (setq prev (car (gnus-id-to-thread id))))
3818       (setq last-id id
3819             id (gnus-parent-id (mail-header-references prev))))
3820     last-id))
3821
3822 (defun gnus-articles-in-thread (thread)
3823   "Return the list of articles in THREAD."
3824   (cons (mail-header-number (car thread))
3825         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3826
3827 (defun gnus-remove-thread (id &optional dont-remove)
3828   "Remove the thread that has ID in it."
3829   (let (headers thread last-id)
3830     ;; First go up in this thread until we find the root.
3831     (setq last-id (gnus-root-id id)
3832           headers (message-flatten-list (gnus-id-to-thread last-id)))
3833     ;; We have now found the real root of this thread.  It might have
3834     ;; been gathered into some loose thread, so we have to search
3835     ;; through the threads to find the thread we wanted.
3836     (let ((threads gnus-newsgroup-threads)
3837           sub)
3838       (while threads
3839         (setq sub (car threads))
3840         (if (stringp (car sub))
3841             ;; This is a gathered thread, so we look at the roots
3842             ;; below it to find whether this article is in this
3843             ;; gathered root.
3844             (progn
3845               (setq sub (cdr sub))
3846               (while sub
3847                 (when (member (caar sub) headers)
3848                   (setq thread (car threads)
3849                         threads nil
3850                         sub nil))
3851                 (setq sub (cdr sub))))
3852           ;; It's an ordinary thread, so we check it.
3853           (when (eq (car sub) (car headers))
3854             (setq thread sub
3855                   threads nil)))
3856         (setq threads (cdr threads)))
3857       ;; If this article is in no thread, then it's a root.
3858       (if thread
3859           (unless dont-remove
3860             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3861         (setq thread (gnus-id-to-thread last-id)))
3862       (when thread
3863         (prog1
3864             thread                      ; We return this thread.
3865           (unless dont-remove
3866             (if (stringp (car thread))
3867                 (progn
3868                   ;; If we use dummy roots, then we have to remove the
3869                   ;; dummy root as well.
3870                   (when (eq gnus-summary-make-false-root 'dummy)
3871                     ;; We go to the dummy root by going to
3872                     ;; the first sub-"thread", and then one line up.
3873                     (gnus-summary-goto-article
3874                      (mail-header-number (caadr thread)))
3875                     (forward-line -1)
3876                     (gnus-delete-line)
3877                     (gnus-data-compute-positions))
3878                   (setq thread (cdr thread))
3879                   (while thread
3880                     (gnus-remove-thread-1 (car thread))
3881                     (setq thread (cdr thread))))
3882               (gnus-remove-thread-1 thread))))))))
3883
3884 (defun gnus-remove-thread-1 (thread)
3885   "Remove the thread THREAD recursively."
3886   (let ((number (mail-header-number (pop thread)))
3887         d)
3888     (setq thread (reverse thread))
3889     (while thread
3890       (gnus-remove-thread-1 (pop thread)))
3891     (when (setq d (gnus-data-find number))
3892       (goto-char (gnus-data-pos d))
3893       (gnus-summary-show-thread)
3894       (gnus-data-remove
3895        number
3896        (- (gnus-point-at-bol)
3897           (prog1
3898               (1+ (gnus-point-at-eol))
3899             (gnus-delete-line)))))))
3900
3901 (defun gnus-sort-threads-1 (threads func)
3902   (sort (mapcar (lambda (thread)
3903                   (cons (car thread)
3904                         (and (cdr thread)
3905                              (gnus-sort-threads-1 (cdr thread) func))))
3906                 threads) func))
3907
3908 (defun gnus-sort-threads (threads)
3909   "Sort THREADS."
3910   (if (not gnus-thread-sort-functions)
3911       threads
3912     (gnus-message 8 "Sorting threads...")
3913     (prog1
3914         (gnus-sort-threads-1
3915          threads
3916          (gnus-make-sort-function gnus-thread-sort-functions))
3917       (gnus-message 8 "Sorting threads...done"))))
3918
3919 (defun gnus-sort-articles (articles)
3920   "Sort ARTICLES."
3921   (when gnus-article-sort-functions
3922     (gnus-message 7 "Sorting articles...")
3923     (prog1
3924         (setq gnus-newsgroup-headers
3925               (sort articles (gnus-make-sort-function
3926                               gnus-article-sort-functions)))
3927       (gnus-message 7 "Sorting articles...done"))))
3928
3929 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3930 (defmacro gnus-thread-header (thread)
3931   "Return header of first article in THREAD.
3932 Note that THREAD must never, ever be anything else than a variable -
3933 using some other form will lead to serious barfage."
3934   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3935   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3936   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3937         (vector thread) 2))
3938
3939 (defsubst gnus-article-sort-by-number (h1 h2)
3940   "Sort articles by article number."
3941   (< (mail-header-number h1)
3942      (mail-header-number h2)))
3943
3944 (defun gnus-thread-sort-by-number (h1 h2)
3945   "Sort threads by root article number."
3946   (gnus-article-sort-by-number
3947    (gnus-thread-header h1) (gnus-thread-header h2)))
3948
3949 (defsubst gnus-article-sort-by-lines (h1 h2)
3950   "Sort articles by article Lines header."
3951   (< (mail-header-lines h1)
3952      (mail-header-lines h2)))
3953
3954 (defun gnus-thread-sort-by-lines (h1 h2)
3955   "Sort threads by root article Lines header."
3956   (gnus-article-sort-by-lines
3957    (gnus-thread-header h1) (gnus-thread-header h2)))
3958
3959 (defsubst gnus-article-sort-by-chars (h1 h2)
3960   "Sort articles by octet length."
3961   (< (mail-header-chars h1)
3962      (mail-header-chars h2)))
3963
3964 (defun gnus-thread-sort-by-chars (h1 h2)
3965   "Sort threads by root article octet length."
3966   (gnus-article-sort-by-chars
3967    (gnus-thread-header h1) (gnus-thread-header h2)))
3968
3969 (defsubst gnus-article-sort-by-author (h1 h2)
3970   "Sort articles by root author."
3971   (string-lessp
3972    (let ((addr (car (mime-entity-read-field h1 'From))))
3973      (or (std11-full-name-string addr)
3974          (std11-address-string addr)
3975          ""))
3976    (let ((addr (car (mime-entity-read-field h2 'From))))
3977      (or (std11-full-name-string addr)
3978          (std11-address-string addr)
3979          ""))
3980    ))
3981
3982 (defun gnus-thread-sort-by-author (h1 h2)
3983   "Sort threads by root author."
3984   (gnus-article-sort-by-author
3985    (gnus-thread-header h1)  (gnus-thread-header h2)))
3986
3987 (defsubst gnus-article-sort-by-subject (h1 h2)
3988   "Sort articles by root subject."
3989   (string-lessp
3990    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3991    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3992
3993 (defun gnus-thread-sort-by-subject (h1 h2)
3994   "Sort threads by root subject."
3995   (gnus-article-sort-by-subject
3996    (gnus-thread-header h1) (gnus-thread-header h2)))
3997
3998 (defsubst gnus-article-sort-by-date (h1 h2)
3999   "Sort articles by root article date."
4000   (time-less-p
4001    (gnus-date-get-time (mail-header-date h1))
4002    (gnus-date-get-time (mail-header-date h2))))
4003
4004 (defun gnus-thread-sort-by-date (h1 h2)
4005   "Sort threads by root article date."
4006   (gnus-article-sort-by-date
4007    (gnus-thread-header h1) (gnus-thread-header h2)))
4008
4009 (defsubst gnus-article-sort-by-score (h1 h2)
4010   "Sort articles by root article score.
4011 Unscored articles will be counted as having a score of zero."
4012   (> (or (cdr (assq (mail-header-number h1)
4013                     gnus-newsgroup-scored))
4014          gnus-summary-default-score 0)
4015      (or (cdr (assq (mail-header-number h2)
4016                     gnus-newsgroup-scored))
4017          gnus-summary-default-score 0)))
4018
4019 (defun gnus-thread-sort-by-score (h1 h2)
4020   "Sort threads by root article score."
4021   (gnus-article-sort-by-score
4022    (gnus-thread-header h1) (gnus-thread-header h2)))
4023
4024 (defun gnus-thread-sort-by-total-score (h1 h2)
4025   "Sort threads by the sum of all scores in the thread.
4026 Unscored articles will be counted as having a score of zero."
4027   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4028
4029 (defun gnus-thread-total-score (thread)
4030   ;; This function find the total score of THREAD.
4031   (cond ((null thread)
4032          0)
4033         ((consp thread)
4034          (if (stringp (car thread))
4035              (apply gnus-thread-score-function 0
4036                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4037            (gnus-thread-total-score-1 thread)))
4038         (t
4039          (gnus-thread-total-score-1 (list thread)))))
4040
4041 (defun gnus-thread-total-score-1 (root)
4042   ;; This function find the total score of the thread below ROOT.
4043   (setq root (car root))
4044   (apply gnus-thread-score-function
4045          (or (append
4046               (mapcar 'gnus-thread-total-score
4047                       (cdr (gnus-id-to-thread (mail-header-id root))))
4048               (when (> (mail-header-number root) 0)
4049                 (list (or (cdr (assq (mail-header-number root)
4050                                      gnus-newsgroup-scored))
4051                           gnus-summary-default-score 0))))
4052              (list gnus-summary-default-score)
4053              '(0))))
4054
4055 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4056 (defvar gnus-tmp-prev-subject nil)
4057 (defvar gnus-tmp-false-parent nil)
4058 (defvar gnus-tmp-root-expunged nil)
4059 (defvar gnus-tmp-dummy-line nil)
4060
4061 (eval-when-compile (defvar gnus-tmp-header))
4062 (defun gnus-extra-header (type &optional header)
4063   "Return the extra header of TYPE."
4064   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4065       ""))
4066
4067 (defun gnus-summary-prepare-threads (threads)
4068   "Prepare summary buffer from THREADS and indentation LEVEL.
4069 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4070 or a straight list of headers."
4071   (gnus-message 7 "Generating summary...")
4072
4073   (setq gnus-newsgroup-threads threads)
4074   (beginning-of-line)
4075
4076   (let ((gnus-tmp-level 0)
4077         (default-score (or gnus-summary-default-score 0))
4078         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4079         thread number subject stack state gnus-tmp-gathered beg-match
4080         new-roots gnus-tmp-new-adopts thread-end
4081         gnus-tmp-header gnus-tmp-unread
4082         gnus-tmp-replied gnus-tmp-subject-or-nil
4083         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4084         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4085         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
4086
4087     (setq gnus-tmp-prev-subject nil)
4088
4089     (if (vectorp (car threads))
4090         ;; If this is a straight (sic) list of headers, then a
4091         ;; threaded summary display isn't required, so we just create
4092         ;; an unthreaded one.
4093         (gnus-summary-prepare-unthreaded threads)
4094
4095       ;; Do the threaded display.
4096
4097       (while (or threads stack gnus-tmp-new-adopts new-roots)
4098
4099         (if (and (= gnus-tmp-level 0)
4100                  (or (not stack)
4101                      (= (caar stack) 0))
4102                  (not gnus-tmp-false-parent)
4103                  (or gnus-tmp-new-adopts new-roots))
4104             (if gnus-tmp-new-adopts
4105                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4106                       thread (list (car gnus-tmp-new-adopts))
4107                       gnus-tmp-header (caar thread)
4108                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4109               (when new-roots
4110                 (setq thread (list (car new-roots))
4111                       gnus-tmp-header (caar thread)
4112                       new-roots (cdr new-roots))))
4113
4114           (if threads
4115               ;; If there are some threads, we do them before the
4116               ;; threads on the stack.
4117               (setq thread threads
4118                     gnus-tmp-header (caar thread))
4119             ;; There were no current threads, so we pop something off
4120             ;; the stack.
4121             (setq state (car stack)
4122                   gnus-tmp-level (car state)
4123                   thread (cdr state)
4124                   stack (cdr stack)
4125                   gnus-tmp-header (caar thread))))
4126
4127         (setq gnus-tmp-false-parent nil)
4128         (setq gnus-tmp-root-expunged nil)
4129         (setq thread-end nil)
4130
4131         (if (stringp gnus-tmp-header)
4132             ;; The header is a dummy root.
4133             (cond
4134              ((eq gnus-summary-make-false-root 'adopt)
4135               ;; We let the first article adopt the rest.
4136               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4137                                                (cddar thread)))
4138               (setq gnus-tmp-gathered
4139                     (nconc (mapcar
4140                             (lambda (h) (mail-header-number (car h)))
4141                             (cddar thread))
4142                            gnus-tmp-gathered))
4143               (setq thread (cons (list (caar thread)
4144                                        (cadar thread))
4145                                  (cdr thread)))
4146               (setq gnus-tmp-level -1
4147                     gnus-tmp-false-parent t))
4148              ((eq gnus-summary-make-false-root 'empty)
4149               ;; We print adopted articles with empty subject fields.
4150               (setq gnus-tmp-gathered
4151                     (nconc (mapcar
4152                             (lambda (h) (mail-header-number (car h)))
4153                             (cddar thread))
4154                            gnus-tmp-gathered))
4155               (setq gnus-tmp-level -1))
4156              ((eq gnus-summary-make-false-root 'dummy)
4157               ;; We remember that we probably want to output a dummy
4158               ;; root.
4159               (setq gnus-tmp-dummy-line gnus-tmp-header)
4160               (setq gnus-tmp-prev-subject gnus-tmp-header))
4161              (t
4162               ;; We do not make a root for the gathered
4163               ;; sub-threads at all.
4164               (setq gnus-tmp-level -1)))
4165
4166           (setq number (mail-header-number gnus-tmp-header)
4167                 subject (mail-header-subject gnus-tmp-header))
4168
4169           (cond
4170            ;; If the thread has changed subject, we might want to make
4171            ;; this subthread into a root.
4172            ((and (null gnus-thread-ignore-subject)
4173                  (not (zerop gnus-tmp-level))
4174                  gnus-tmp-prev-subject
4175                  (not (inline
4176                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4177             (setq new-roots (nconc new-roots (list (car thread)))
4178                   thread-end t
4179                   gnus-tmp-header nil))
4180            ;; If the article lies outside the current limit,
4181            ;; then we do not display it.
4182            ((not (memq number gnus-newsgroup-limit))
4183             (setq gnus-tmp-gathered
4184                   (nconc (mapcar
4185                           (lambda (h) (mail-header-number (car h)))
4186                           (cdar thread))
4187                          gnus-tmp-gathered))
4188             (setq gnus-tmp-new-adopts (if (cdar thread)
4189                                           (append gnus-tmp-new-adopts
4190                                                   (cdar thread))
4191                                         gnus-tmp-new-adopts)
4192                   thread-end t
4193                   gnus-tmp-header nil)
4194             (when (zerop gnus-tmp-level)
4195               (setq gnus-tmp-root-expunged t)))
4196            ;; Perhaps this article is to be marked as read?
4197            ((and gnus-summary-mark-below
4198                  (< (or (cdr (assq number gnus-newsgroup-scored))
4199                         default-score)
4200                     gnus-summary-mark-below)
4201                  ;; Don't touch sparse articles.
4202                  (not (gnus-summary-article-sparse-p number))
4203                  (not (gnus-summary-article-ancient-p number)))
4204             (setq gnus-newsgroup-unreads
4205                   (delq number gnus-newsgroup-unreads))
4206             (if gnus-newsgroup-auto-expire
4207                 (push number gnus-newsgroup-expirable)
4208               (push (cons number gnus-low-score-mark)
4209                     gnus-newsgroup-reads))))
4210
4211           (when gnus-tmp-header
4212             ;; We may have an old dummy line to output before this
4213             ;; article.
4214             (when (and gnus-tmp-dummy-line
4215                        (gnus-subject-equal
4216                         gnus-tmp-dummy-line
4217                         (mail-header-subject gnus-tmp-header)))
4218               (gnus-summary-insert-dummy-line
4219                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4220               (setq gnus-tmp-dummy-line nil))
4221
4222             ;; Compute the mark.
4223             (setq gnus-tmp-unread (gnus-article-mark number))
4224
4225             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4226                                   gnus-tmp-header gnus-tmp-level)
4227                   gnus-newsgroup-data)
4228
4229             ;; Actually insert the line.
4230             (setq
4231              gnus-tmp-subject-or-nil
4232              (cond
4233               ((and gnus-thread-ignore-subject
4234                     gnus-tmp-prev-subject
4235                     (not (inline (gnus-subject-equal
4236                                   gnus-tmp-prev-subject subject))))
4237                subject)
4238               ((zerop gnus-tmp-level)
4239                (if (and (eq gnus-summary-make-false-root 'empty)
4240                         (memq number gnus-tmp-gathered)
4241                         gnus-tmp-prev-subject
4242                         (inline (gnus-subject-equal
4243                                  gnus-tmp-prev-subject subject)))
4244                    gnus-summary-same-subject
4245                  subject))
4246               (t gnus-summary-same-subject)))
4247             (if (and (eq gnus-summary-make-false-root 'adopt)
4248                      (= gnus-tmp-level 1)
4249                      (memq number gnus-tmp-gathered))
4250                 (setq gnus-tmp-opening-bracket ?\<
4251                       gnus-tmp-closing-bracket ?\>)
4252               (setq gnus-tmp-opening-bracket ?\[
4253                     gnus-tmp-closing-bracket ?\]))
4254             (setq
4255              gnus-tmp-indentation
4256              (aref gnus-thread-indent-array gnus-tmp-level)
4257              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4258              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4259                                 gnus-summary-default-score 0)
4260              gnus-tmp-score-char
4261              (if (or (null gnus-summary-default-score)
4262                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4263                          gnus-summary-zcore-fuzz))
4264                  ?  ;Whitespace
4265                (if (< gnus-tmp-score gnus-summary-default-score)
4266                    gnus-score-below-mark gnus-score-over-mark))
4267              gnus-tmp-replied
4268              (cond ((memq number gnus-newsgroup-processable)
4269                     gnus-process-mark)
4270                    ((memq number gnus-newsgroup-cached)
4271                     gnus-cached-mark)
4272                    ((memq number gnus-newsgroup-replied)
4273                     gnus-replied-mark)
4274                    ((memq number gnus-newsgroup-forwarded)
4275                     gnus-forwarded-mark)
4276                    ((memq number gnus-newsgroup-saved)
4277                     gnus-saved-mark)
4278                    (t gnus-no-mark))
4279              gnus-tmp-from (mail-header-from gnus-tmp-header)
4280              gnus-tmp-name
4281              (cond
4282               ((string-match "<[^>]+> *$" gnus-tmp-from)
4283                (setq beg-match (match-beginning 0))
4284                (or (and (string-match "^\".+\"" gnus-tmp-from)
4285                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4286                    (substring gnus-tmp-from 0 beg-match)))
4287               ((string-match "(.+)" gnus-tmp-from)
4288                (substring gnus-tmp-from
4289                           (1+ (match-beginning 0)) (1- (match-end 0))))
4290               (t gnus-tmp-from)))
4291             (when (string= gnus-tmp-name "")
4292               (setq gnus-tmp-name gnus-tmp-from))
4293             (unless (numberp gnus-tmp-lines)
4294               (setq gnus-tmp-lines -1))
4295             (when (= gnus-tmp-lines -1)
4296               (setq gnus-tmp-lines "?"))
4297             (gnus-put-text-property
4298              (point)
4299              (progn (eval gnus-summary-line-format-spec) (point))
4300              'gnus-number number)
4301             (when gnus-visual-p
4302               (forward-line -1)
4303               (gnus-run-hooks 'gnus-summary-update-hook)
4304               (forward-line 1))
4305
4306             (setq gnus-tmp-prev-subject subject)))
4307
4308         (when (nth 1 thread)
4309           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4310         (incf gnus-tmp-level)
4311         (setq threads (if thread-end nil (cdar thread)))
4312         (unless threads
4313           (setq gnus-tmp-level 0)))))
4314   (gnus-message 7 "Generating summary...done"))
4315
4316 (defun gnus-summary-prepare-unthreaded (headers)
4317   "Generate an unthreaded summary buffer based on HEADERS."
4318   (let (header number mark)
4319
4320     (beginning-of-line)
4321
4322     (while headers
4323       ;; We may have to root out some bad articles...
4324       (when (memq (setq number (mail-header-number
4325                                 (setq header (pop headers))))
4326                   gnus-newsgroup-limit)
4327         ;; Mark article as read when it has a low score.
4328         (when (and gnus-summary-mark-below
4329                    (< (or (cdr (assq number gnus-newsgroup-scored))
4330                           gnus-summary-default-score 0)
4331                       gnus-summary-mark-below)
4332                    (not (gnus-summary-article-ancient-p number)))
4333           (setq gnus-newsgroup-unreads
4334                 (delq number gnus-newsgroup-unreads))
4335           (if gnus-newsgroup-auto-expire
4336               (push number gnus-newsgroup-expirable)
4337             (push (cons number gnus-low-score-mark)
4338                   gnus-newsgroup-reads)))
4339
4340         (setq mark (gnus-article-mark number))
4341         (push (gnus-data-make number mark (1+ (point)) header 0)
4342               gnus-newsgroup-data)
4343         (gnus-summary-insert-line
4344          header 0 number
4345          mark (memq number gnus-newsgroup-replied)
4346          (memq number gnus-newsgroup-expirable)
4347          (mail-header-subject header) nil
4348          (cdr (assq number gnus-newsgroup-scored))
4349          (memq number gnus-newsgroup-processable))))))
4350
4351 (defun gnus-summary-remove-list-identifiers ()
4352   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4353   (let ((regexp (if (consp gnus-list-identifiers)
4354                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4355                   gnus-list-identifiers))
4356         changed subject)
4357     (when regexp
4358       (dolist (header gnus-newsgroup-headers)
4359         (setq subject (mail-header-subject header)
4360               changed nil)
4361         (while (string-match
4362                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4363                 subject)
4364           (setq subject
4365                 (concat (substring subject 0 (match-beginning 2))
4366                         (substring subject (match-end 0)))
4367                 changed t))
4368         (when (and changed
4369                    (string-match
4370                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4371           (setq subject
4372                 (concat (substring subject 0 (match-beginning 1))
4373                         (substring subject (match-end 1)))))
4374         (when changed
4375           (mail-header-set-subject header subject))))))
4376
4377 (defun gnus-fetch-headers (articles)
4378   "Fetch headers of ARTICLES."
4379   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4380     (gnus-message 5 "Fetching headers for %s..." name)
4381     (prog1
4382         ;;;!!! FIXME: temporary fix for an infloop on nnimap.
4383         (if (eq 'nnimap (car (gnus-find-method-for-group name)))
4384             (if (eq 'nov
4385                     (setq gnus-headers-retrieved-by
4386                           (gnus-retrieve-headers
4387                            articles gnus-newsgroup-name
4388                            ;; We might want to fetch old headers, but
4389                            ;; not if there is only 1 article.
4390                            (and (or (and
4391                                      (not (eq gnus-fetch-old-headers 'some))
4392                                      (not (numberp gnus-fetch-old-headers)))
4393                                     (> (length articles) 1))
4394                                 gnus-fetch-old-headers))))
4395                 (gnus-get-newsgroup-headers-xover
4396                  articles nil nil gnus-newsgroup-name t)
4397               (gnus-get-newsgroup-headers))
4398           (gnus-retrieve-parsed-headers
4399            articles gnus-newsgroup-name
4400            ;; We might want to fetch old headers, but
4401            ;; not if there is only 1 article.
4402            (and (or (and (not (eq gnus-fetch-old-headers 'some))
4403                          (not (numberp gnus-fetch-old-headers)))
4404                     (> (length articles) 1))
4405                 gnus-fetch-old-headers)))
4406       (gnus-message 5 "Fetching headers for %s...done" name))))
4407
4408 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4409   "Select newsgroup GROUP.
4410 If READ-ALL is non-nil, all articles in the group are selected.
4411 If SELECT-ARTICLES, only select those articles from GROUP."
4412   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4413          ;;!!! Dirty hack; should be removed.
4414          (gnus-summary-ignore-duplicates
4415           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4416               t
4417             gnus-summary-ignore-duplicates))
4418          (info (nth 2 entry))
4419          articles fetched-articles cached)
4420
4421     (unless (gnus-check-server
4422              (setq gnus-current-select-method
4423                    (gnus-find-method-for-group group)))
4424       (error "Couldn't open server"))
4425
4426     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4427         (gnus-activate-group group)     ; Or we can activate it...
4428         (progn                          ; Or we bug out.
4429           (when (equal major-mode 'gnus-summary-mode)
4430             (kill-buffer (current-buffer)))
4431           (error "Couldn't activate group %s: %s"
4432                  group (gnus-status-message group))))
4433
4434     (unless (gnus-request-group group t)
4435       (when (equal major-mode 'gnus-summary-mode)
4436         (kill-buffer (current-buffer)))
4437       (error "Couldn't request group %s: %s"
4438              group (gnus-status-message group)))
4439
4440     (setq gnus-newsgroup-name group)
4441     (setq gnus-newsgroup-unselected nil)
4442     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4443     (gnus-summary-setup-default-charset)
4444
4445     ;; Adjust and set lists of article marks.
4446     (when info
4447       (gnus-adjust-marked-articles info))
4448
4449     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4450     (setq cached
4451           (if (gnus-virtual-group-p group)
4452               gnus-newsgroup-cached
4453             (gnus-cache-articles-in-group group)))
4454
4455     (setq gnus-newsgroup-unreads
4456           (gnus-set-difference
4457            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4458            gnus-newsgroup-dormant))
4459
4460     (setq gnus-newsgroup-processable nil)
4461
4462     (gnus-update-read-articles group gnus-newsgroup-unreads)
4463
4464     (if (setq articles select-articles)
4465         (setq gnus-newsgroup-unselected
4466               (gnus-sorted-intersection
4467                gnus-newsgroup-unreads
4468                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4469       (setq articles (gnus-articles-to-read group read-all)))
4470
4471     (cond
4472      ((null articles)
4473       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4474       'quit)
4475      ((eq articles 0) nil)
4476      (t
4477       ;; Init the dependencies hash table.
4478       (setq gnus-newsgroup-dependencies
4479             (gnus-make-hashtable (length articles)))
4480       (gnus-set-global-variables)
4481       ;; Retrieve the headers and read them in.
4482       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4483
4484       ;; Suppress duplicates?
4485       (when gnus-suppress-duplicates
4486         (gnus-dup-suppress-articles))
4487
4488       ;; Set the initial limit.
4489       (setq gnus-newsgroup-limit (copy-sequence articles))
4490       ;; Remove canceled articles from the list of unread articles.
4491       (setq gnus-newsgroup-unreads
4492             (gnus-set-sorted-intersection
4493              gnus-newsgroup-unreads
4494              (setq fetched-articles
4495                    (mapcar (lambda (headers) (mail-header-number headers))
4496                            gnus-newsgroup-headers))))
4497       ;; Removed marked articles that do not exist.
4498       (gnus-update-missing-marks
4499        (gnus-sorted-complement fetched-articles articles))
4500
4501       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4502       (when cached
4503         (setq gnus-newsgroup-cached cached))
4504
4505       ;; We might want to build some more threads first.
4506       (when (and gnus-fetch-old-headers
4507                  (eq gnus-headers-retrieved-by 'nov))
4508         (if (eq gnus-fetch-old-headers 'invisible)
4509             (gnus-build-all-threads)
4510           (gnus-build-old-threads)))
4511       ;; Let the Gnus agent mark articles as read.
4512       (when gnus-agent
4513         (gnus-agent-get-undownloaded-list))
4514       ;; Remove list identifiers from subject
4515       (when gnus-list-identifiers
4516         (gnus-summary-remove-list-identifiers))
4517       ;; Check whether auto-expire is to be done in this group.
4518       (setq gnus-newsgroup-auto-expire
4519             (gnus-group-auto-expirable-p group))
4520       ;; Set up the article buffer now, if necessary.
4521       (unless gnus-single-article-buffer
4522         (gnus-article-setup-buffer))
4523       ;; First and last article in this newsgroup.
4524       (when gnus-newsgroup-headers
4525         (setq gnus-newsgroup-begin
4526               (mail-header-number (car gnus-newsgroup-headers))
4527               gnus-newsgroup-end
4528               (mail-header-number
4529                (gnus-last-element gnus-newsgroup-headers))))
4530       ;; GROUP is successfully selected.
4531       (or gnus-newsgroup-headers t)))))
4532
4533 (defun gnus-articles-to-read (group &optional read-all)
4534   "Find out what articles the user wants to read."
4535   (let* ((articles
4536           ;; Select all articles if `read-all' is non-nil, or if there
4537           ;; are no unread articles.
4538           (if (or read-all
4539                   (and (zerop (length gnus-newsgroup-marked))
4540                        (zerop (length gnus-newsgroup-unreads)))
4541                   (eq (gnus-group-find-parameter group 'display)
4542                       'all))
4543               (or
4544                (gnus-uncompress-range (gnus-active group))
4545                (gnus-cache-articles-in-group group))
4546             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4547                           (copy-sequence gnus-newsgroup-unreads))
4548                   '<)))
4549          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4550          (scored (length scored-list))
4551          (number (length articles))
4552          (marked (+ (length gnus-newsgroup-marked)
4553                     (length gnus-newsgroup-dormant)))
4554          (select
4555           (cond
4556            ((numberp read-all)
4557             read-all)
4558            (t
4559             (condition-case ()
4560                 (cond
4561                  ((and (or (<= scored marked) (= scored number))
4562                        (natnump gnus-large-newsgroup)
4563                        (> number gnus-large-newsgroup))
4564                   (let* ((cursor-in-echo-area nil)
4565                          (input (read-from-minibuffer
4566                                  (format
4567                                   "How many articles from %s (max %d): "
4568                                   (gnus-limit-string gnus-newsgroup-name 35)
4569                                   number)
4570                                  (cons (number-to-string gnus-large-newsgroup)
4571                                        0))))
4572                     (if (string-match "^[ \t]*$" input)
4573                         number
4574                       input)))
4575                  ((and (> scored marked) (< scored number)
4576                        (> (- scored number) 20))
4577                   (let ((input
4578                          (read-string
4579                           (format "%s %s (%d scored, %d total): "
4580                                   "How many articles from"
4581                                   group scored number))))
4582                     (if (string-match "^[ \t]*$" input)
4583                         number input)))
4584                  (t number))
4585               (quit
4586                (message "Quit getting the articles to read")
4587                nil))))))
4588     (setq select (if (stringp select) (string-to-number select) select))
4589     (if (or (null select) (zerop select))
4590         select
4591       (if (and (not (zerop scored)) (<= (abs select) scored))
4592           (progn
4593             (setq articles (sort scored-list '<))
4594             (setq number (length articles)))
4595         (setq articles (copy-sequence articles)))
4596
4597       (when (< (abs select) number)
4598         (if (< select 0)
4599             ;; Select the N oldest articles.
4600             (setcdr (nthcdr (1- (abs select)) articles) nil)
4601           ;; Select the N most recent articles.
4602           (setq articles (nthcdr (- number select) articles))))
4603       (setq gnus-newsgroup-unselected
4604             (gnus-sorted-intersection
4605              gnus-newsgroup-unreads
4606              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4607       (when gnus-alter-articles-to-read-function
4608         (setq gnus-newsgroup-unreads
4609               (sort
4610                (funcall gnus-alter-articles-to-read-function
4611                         gnus-newsgroup-name gnus-newsgroup-unreads)
4612                '<)))
4613       articles)))
4614
4615 (defun gnus-killed-articles (killed articles)
4616   (let (out)
4617     (while articles
4618       (when (inline (gnus-member-of-range (car articles) killed))
4619         (push (car articles) out))
4620       (setq articles (cdr articles)))
4621     out))
4622
4623 (defun gnus-uncompress-marks (marks)
4624   "Uncompress the mark ranges in MARKS."
4625   (let ((uncompressed '(score bookmark))
4626         out)
4627     (while marks
4628       (if (memq (caar marks) uncompressed)
4629           (push (car marks) out)
4630         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4631       (setq marks (cdr marks)))
4632     out))
4633
4634 (defun gnus-adjust-marked-articles (info)
4635   "Set all article lists and remove all marks that are no longer valid."
4636   (let* ((marked-lists (gnus-info-marks info))
4637          (active (gnus-active (gnus-info-group info)))
4638          (min (car active))
4639          (max (cdr active))
4640          (types gnus-article-mark-lists)
4641          (uncompressed '(score bookmark killed))
4642          marks var articles article mark)
4643
4644     (while marked-lists
4645       (setq marks (pop marked-lists))
4646       (set (setq var (intern (format "gnus-newsgroup-%s"
4647                                      (car (rassq (setq mark (car marks))
4648                                                  types)))))
4649            (if (memq (car marks) uncompressed) (cdr marks)
4650              (gnus-uncompress-range (cdr marks))))
4651
4652       (setq articles (symbol-value var))
4653
4654       ;; All articles have to be subsets of the active articles.
4655       (cond
4656        ;; Adjust "simple" lists.
4657        ((memq mark '(tick dormant expire reply save))
4658         (while articles
4659           (when (or (< (setq article (pop articles)) min) (> article max))
4660             (set var (delq article (symbol-value var))))))
4661        ;; Adjust assocs.
4662        ((memq mark uncompressed)
4663         (when (not (listp (cdr (symbol-value var))))
4664           (set var (list (symbol-value var))))
4665         (when (not (listp (cdr articles)))
4666           (setq articles (list articles)))
4667         (while articles
4668           (when (or (not (consp (setq article (pop articles))))
4669                     (< (car article) min)
4670                     (> (car article) max))
4671             (set var (delq article (symbol-value var))))))))))
4672
4673 (defun gnus-update-missing-marks (missing)
4674   "Go through the list of MISSING articles and remove them from the mark lists."
4675   (when missing
4676     (let ((types gnus-article-mark-lists)
4677           var m)
4678       ;; Go through all types.
4679       (while types
4680         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4681         (when (symbol-value var)
4682           ;; This list has articles.  So we delete all missing articles
4683           ;; from it.
4684           (setq m missing)
4685           (while m
4686             (set var (delq (pop m) (symbol-value var)))))))))
4687
4688 (defun gnus-update-marks ()
4689   "Enter the various lists of marked articles into the newsgroup info list."
4690   (let ((types gnus-article-mark-lists)
4691         (info (gnus-get-info gnus-newsgroup-name))
4692         (uncompressed '(score bookmark killed))
4693         type list newmarked symbol delta-marks)
4694     (when info
4695       ;; Add all marks lists to the list of marks lists.
4696       (while (setq type (pop types))
4697         (setq list (symbol-value
4698                     (setq symbol
4699                           (intern (format "gnus-newsgroup-%s"
4700                                           (car type))))))
4701
4702         (when list
4703           ;; Get rid of the entries of the articles that have the
4704           ;; default score.
4705           (when (and (eq (cdr type) 'score)
4706                      gnus-save-score
4707                      list)
4708             (let* ((arts list)
4709                    (prev (cons nil list))
4710                    (all prev))
4711               (while arts
4712                 (if (or (not (consp (car arts)))
4713                         (= (cdar arts) gnus-summary-default-score))
4714                     (setcdr prev (cdr arts))
4715                   (setq prev arts))
4716                 (setq arts (cdr arts)))
4717               (setq list (cdr all)))))
4718
4719         (unless (memq (cdr type) uncompressed)
4720           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4721
4722         (when (gnus-check-backend-function
4723                'request-set-mark gnus-newsgroup-name)
4724           ;; propagate flags to server, with the following exceptions:
4725           ;; uncompressed:s are not proper flags (they are cons cells)
4726           ;; cache is a internal gnus flag
4727           ;; download are local to one gnus installation (well)
4728           ;; unsend are for nndraft groups only
4729           ;; xxx: generality of this?  this suits nnimap anyway
4730           (unless (memq (cdr type) (append '(cache download unsend)
4731                                            uncompressed))
4732             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4733                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4734                    (add (gnus-remove-from-range
4735                          (gnus-copy-sequence list) old)))
4736               (when add
4737                 (push (list add 'add (list (cdr type))) delta-marks))
4738               (when del
4739                 (push (list del 'del (list (cdr type))) delta-marks)))))
4740
4741         (when list
4742           (push (cons (cdr type) list) newmarked)))
4743
4744       (when delta-marks
4745         (unless (gnus-check-group gnus-newsgroup-name)
4746           (error "Can't open server for %s" gnus-newsgroup-name))
4747         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4748
4749       ;; Enter these new marks into the info of the group.
4750       (if (nthcdr 3 info)
4751           (setcar (nthcdr 3 info) newmarked)
4752         ;; Add the marks lists to the end of the info.
4753         (when newmarked
4754           (setcdr (nthcdr 2 info) (list newmarked))))
4755
4756       ;; Cut off the end of the info if there's nothing else there.
4757       (let ((i 5))
4758         (while (and (> i 2)
4759                     (not (nth i info)))
4760           (when (nthcdr (decf i) info)
4761             (setcdr (nthcdr i info) nil)))))))
4762
4763 (defun gnus-set-mode-line (where)
4764   "Set the mode line of the article or summary buffers.
4765 If WHERE is `summary', the summary mode line format will be used."
4766   ;; Is this mode line one we keep updated?
4767   (when (and (memq where gnus-updated-mode-lines)
4768              (symbol-value
4769               (intern (format "gnus-%s-mode-line-format-spec" where))))
4770     (let (mode-string)
4771       (save-excursion
4772         ;; We evaluate this in the summary buffer since these
4773         ;; variables are buffer-local to that buffer.
4774         (set-buffer gnus-summary-buffer)
4775         ;; We bind all these variables that are used in the `eval' form
4776         ;; below.
4777         (let* ((mformat (symbol-value
4778                          (intern
4779                           (format "gnus-%s-mode-line-format-spec" where))))
4780                (gnus-tmp-group-name (gnus-group-decoded-name 
4781                                      gnus-newsgroup-name))
4782                (gnus-tmp-article-number (or gnus-current-article 0))
4783                (gnus-tmp-unread gnus-newsgroup-unreads)
4784                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4785                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4786                (gnus-tmp-unread-and-unselected
4787                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4788                             (zerop gnus-tmp-unselected))
4789                        "")
4790                       ((zerop gnus-tmp-unselected)
4791                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4792                       (t (format "{%d(+%d) more}"
4793                                  gnus-tmp-unread-and-unticked
4794                                  gnus-tmp-unselected))))
4795                (gnus-tmp-subject
4796                 (if (and gnus-current-headers
4797                          (vectorp gnus-current-headers))
4798                     (gnus-mode-string-quote
4799                      (mail-header-subject gnus-current-headers))
4800                   ""))
4801                bufname-length max-len
4802                gnus-tmp-header);; passed as argument to any user-format-funcs
4803           (setq mode-string (eval mformat))
4804           (setq bufname-length (if (string-match "%b" mode-string)
4805                                    (- (length
4806                                        (buffer-name
4807                                         (if (eq where 'summary)
4808                                             nil
4809                                           (get-buffer gnus-article-buffer))))
4810                                       2)
4811                                  0))
4812           (setq max-len (max 4 (if gnus-mode-non-string-length
4813                                    (- (window-width)
4814                                       gnus-mode-non-string-length
4815                                       bufname-length)
4816                                  (length mode-string))))
4817           ;; We might have to chop a bit of the string off...
4818           (when (> (length mode-string) max-len)
4819             (setq mode-string
4820                   (concat (gnus-truncate-string mode-string (- max-len 3))
4821                           "...")))
4822           ;; Pad the mode string a bit.
4823           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4824       ;; Update the mode line.
4825       (setq mode-line-buffer-identification
4826             (gnus-mode-line-buffer-identification (list mode-string)))
4827       (set-buffer-modified-p t))))
4828
4829 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4830   "Go through the HEADERS list and add all Xrefs to a hash table.
4831 The resulting hash table is returned, or nil if no Xrefs were found."
4832   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4833          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4834          (xref-hashtb (gnus-make-hashtable))
4835          start group entry number xrefs header)
4836     (while headers
4837       (setq header (pop headers))
4838       (when (and (setq xrefs (mail-header-xref header))
4839                  (not (memq (setq number (mail-header-number header))
4840                             unreads)))
4841         (setq start 0)
4842         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4843           (setq start (match-end 0))
4844           (setq group (if prefix
4845                           (concat prefix (substring xrefs (match-beginning 1)
4846                                                     (match-end 1)))
4847                         (substring xrefs (match-beginning 1) (match-end 1))))
4848           (setq number
4849                 (string-to-int (substring xrefs (match-beginning 2)
4850                                           (match-end 2))))
4851           (if (setq entry (gnus-gethash group xref-hashtb))
4852               (setcdr entry (cons number (cdr entry)))
4853             (gnus-sethash group (cons number nil) xref-hashtb)))))
4854     (and start xref-hashtb)))
4855
4856 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4857   "Look through all the headers and mark the Xrefs as read."
4858   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4859         name entry info xref-hashtb idlist method nth4)
4860     (save-excursion
4861       (set-buffer gnus-group-buffer)
4862       (when (setq xref-hashtb
4863                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4864         (mapatoms
4865          (lambda (group)
4866            (unless (string= from-newsgroup (setq name (symbol-name group)))
4867              (setq idlist (symbol-value group))
4868              ;; Dead groups are not updated.
4869              (and (prog1
4870                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4871                             info (nth 2 entry))
4872                     (when (stringp (setq nth4 (gnus-info-method info)))
4873                       (setq nth4 (gnus-server-to-method nth4))))
4874                   ;; Only do the xrefs if the group has the same
4875                   ;; select method as the group we have just read.
4876                   (or (gnus-methods-equal-p
4877                        nth4 (gnus-find-method-for-group from-newsgroup))
4878                       virtual
4879                       (equal nth4 (setq method (gnus-find-method-for-group
4880                                                 from-newsgroup)))
4881                       (and (equal (car nth4) (car method))
4882                            (equal (nth 1 nth4) (nth 1 method))))
4883                   gnus-use-cross-reference
4884                   (or (not (eq gnus-use-cross-reference t))
4885                       virtual
4886                       ;; Only do cross-references on subscribed
4887                       ;; groups, if that is what is wanted.
4888                       (<= (gnus-info-level info) gnus-level-subscribed))
4889                   (gnus-group-make-articles-read name idlist))))
4890          xref-hashtb)))))
4891
4892 (defun gnus-compute-read-articles (group articles)
4893   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4894          (info (nth 2 entry))
4895          (active (gnus-active group))
4896          ninfo)
4897     (when entry
4898       ;; First peel off all invalid article numbers.
4899       (when active
4900         (let ((ids articles)
4901               id first)
4902           (while (setq id (pop ids))
4903             (when (and first (> id (cdr active)))
4904               ;; We'll end up in this situation in one particular
4905               ;; obscure situation.  If you re-scan a group and get
4906               ;; a new article that is cross-posted to a different
4907               ;; group that has not been re-scanned, you might get
4908               ;; crossposted article that has a higher number than
4909               ;; Gnus believes possible.  So we re-activate this
4910               ;; group as well.  This might mean doing the
4911               ;; crossposting thingy will *increase* the number
4912               ;; of articles in some groups.  Tsk, tsk.
4913               (setq active (or (gnus-activate-group group) active)))
4914             (when (or (> id (cdr active))
4915                       (< id (car active)))
4916               (setq articles (delq id articles))))))
4917       ;; If the read list is nil, we init it.
4918       (if (and active
4919                (null (gnus-info-read info))
4920                (> (car active) 1))
4921           (setq ninfo (cons 1 (1- (car active))))
4922         (setq ninfo (gnus-info-read info)))
4923       ;; Then we add the read articles to the range.
4924       (gnus-add-to-range
4925        ninfo (setq articles (sort articles '<))))))
4926
4927 (defun gnus-group-make-articles-read (group articles)
4928   "Update the info of GROUP to say that ARTICLES are read."
4929   (let* ((num 0)
4930          (entry (gnus-gethash group gnus-newsrc-hashtb))
4931          (info (nth 2 entry))
4932          (active (gnus-active group))
4933          range)
4934     (when entry
4935       (setq range (gnus-compute-read-articles group articles))
4936       (save-excursion
4937         (set-buffer gnus-group-buffer)
4938         (gnus-undo-register
4939           `(progn
4940              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4941              (gnus-info-set-read ',info ',(gnus-info-read info))
4942              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4943              (gnus-group-update-group ,group t))))
4944       ;; Add the read articles to the range.
4945       (gnus-info-set-read info range)
4946       ;; Then we have to re-compute how many unread
4947       ;; articles there are in this group.
4948       (when active
4949         (cond
4950          ((not range)
4951           (setq num (- (1+ (cdr active)) (car active))))
4952          ((not (listp (cdr range)))
4953           (setq num (- (cdr active) (- (1+ (cdr range))
4954                                        (car range)))))
4955          (t
4956           (while range
4957             (if (numberp (car range))
4958                 (setq num (1+ num))
4959               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4960             (setq range (cdr range)))
4961           (setq num (- (cdr active) num))))
4962         ;; Update the number of unread articles.
4963         (setcar entry num)
4964         ;; Update the group buffer.
4965         (gnus-group-update-group group t)))))
4966
4967 (defvar gnus-newsgroup-none-id 0)
4968
4969 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4970   (let ((cur nntp-server-buffer)
4971         (dependencies
4972          (or dependencies
4973              (save-excursion (set-buffer gnus-summary-buffer)
4974                              gnus-newsgroup-dependencies)))
4975         headers id end ref
4976         (mail-parse-charset gnus-newsgroup-charset)
4977         (mail-parse-ignored-charsets
4978          (save-excursion (condition-case nil
4979                              (set-buffer gnus-summary-buffer)
4980                            (error))
4981                          gnus-newsgroup-ignored-charsets)))
4982     (save-excursion
4983       (set-buffer nntp-server-buffer)
4984       ;; Translate all TAB characters into SPACE characters.
4985       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4986       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4987       (gnus-run-hooks 'gnus-parse-headers-hook)
4988       (let ((case-fold-search t)
4989             in-reply-to header p lines chars ctype)
4990         (goto-char (point-min))
4991         ;; Search to the beginning of the next header.  Error messages
4992         ;; do not begin with 2 or 3.
4993         (while (re-search-forward "^[23][0-9]+ " nil t)
4994           (setq id nil
4995                 ref nil)
4996           ;; This implementation of this function, with nine
4997           ;; search-forwards instead of the one re-search-forward and
4998           ;; a case (which basically was the old function) is actually
4999           ;; about twice as fast, even though it looks messier.  You
5000           ;; can't have everything, I guess.  Speed and elegance
5001           ;; doesn't always go hand in hand.
5002           (setq
5003            header
5004            (make-full-mail-header
5005             ;; Number.
5006             (prog1
5007                 (read cur)
5008               (end-of-line)
5009               (setq p (point))
5010               (narrow-to-region (point)
5011                                 (or (and (search-forward "\n.\n" nil t)
5012                                          (- (point) 2))
5013                                     (point))))
5014             ;; Subject.
5015             (progn
5016               (goto-char p)
5017               (if (search-forward "\nsubject: " nil t)
5018                   (nnheader-header-value)
5019                 "(none)"))
5020             ;; From.
5021             (progn
5022               (goto-char p)
5023               (if (or (search-forward "\nfrom: " nil t)
5024                       (search-forward "\nfrom:" nil t))
5025                   (nnheader-header-value)
5026                 "(nobody)"))
5027             ;; Date.
5028             (progn
5029               (goto-char p)
5030               (if (search-forward "\ndate: " nil t)
5031                   (nnheader-header-value)
5032                 ""))
5033             ;; Message-ID.
5034             (progn
5035               (goto-char p)
5036               (setq id (if (re-search-forward
5037                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5038                            ;; We do it this way to make sure the Message-ID
5039                            ;; is (somewhat) syntactically valid.
5040                            (buffer-substring (match-beginning 1)
5041                                              (match-end 1))
5042                          ;; If there was no message-id, we just fake one
5043                          ;; to make subsequent routines simpler.
5044                          (nnheader-generate-fake-message-id))))
5045             ;; References.
5046             (progn
5047               (goto-char p)
5048               (if (search-forward "\nreferences: " nil t)
5049                   (progn
5050                     (setq end (point))
5051                     (prog1
5052                         (nnheader-header-value)
5053                       (setq ref
5054                             (buffer-substring
5055                              (progn
5056                                ;; (end-of-line)
5057                                (search-backward ">" end t)
5058                                (1+ (point)))
5059                              (progn
5060                                (search-backward "<" end t)
5061                                (point))))))
5062                 ;; Get the references from the in-reply-to header if there
5063                 ;; were no references and the in-reply-to header looks
5064                 ;; promising.
5065                 (if (and (search-forward "\nin-reply-to: " nil t)
5066                          (setq in-reply-to (nnheader-header-value))
5067                          (string-match "<[^>]+>" in-reply-to))
5068                     (let (ref2)
5069                       (setq ref (substring in-reply-to (match-beginning 0)
5070                                            (match-end 0)))
5071                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5072                         (setq ref2 (substring in-reply-to (match-beginning 0)
5073                                               (match-end 0)))
5074                         (when (> (length ref2) (length ref))
5075                           (setq ref ref2)))
5076                       ref)
5077                   (setq ref nil))))
5078             ;; Chars.
5079             (progn
5080               (goto-char p)
5081               (if (search-forward "\nchars: " nil t)
5082                   (if (numberp (setq chars (ignore-errors (read cur))))
5083                       chars -1)
5084                 -1))
5085             ;; Lines.
5086             (progn
5087               (goto-char p)
5088               (if (search-forward "\nlines: " nil t)
5089                   (if (numberp (setq lines (ignore-errors (read cur))))
5090                       lines -1)
5091                 -1))
5092             ;; Xref.
5093             (progn
5094               (goto-char p)
5095               (and (search-forward "\nxref: " nil t)
5096                    (nnheader-header-value)))
5097             ;; Extra.
5098             (when gnus-extra-headers
5099               (let ((extra gnus-extra-headers)
5100                     out)
5101                 (while extra
5102                   (goto-char p)
5103                   (when (search-forward
5104                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
5105                     (push (cons (car extra) (nnheader-header-value)) out))
5106                   (pop extra))
5107                 out))))
5108           (goto-char p)
5109           (if (and (search-forward "\ncontent-type: " nil t)
5110                    (setq ctype (nnheader-header-value)))
5111               (mime-entity-set-content-type-internal
5112                header (mime-parse-Content-Type ctype)))
5113           (when (equal id ref)
5114             (setq ref nil))
5115
5116           (when gnus-alter-header-function
5117             (funcall gnus-alter-header-function header)
5118             (setq id (mail-header-id header)
5119                   ref (gnus-parent-id (mail-header-references header))))
5120
5121           (when (setq header
5122                       (gnus-dependencies-add-header
5123                        header dependencies force-new))
5124             (push header headers))
5125           (goto-char (point-max))
5126           (widen))
5127         (nreverse headers)))))
5128
5129 ;; Goes through the xover lines and returns a list of vectors
5130 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5131                                                   force-new dependencies
5132                                                   group also-fetch-heads)
5133   "Parse the news overview data in the server buffer.
5134 Return a list of headers that match SEQUENCE (see
5135 `nntp-retrieve-headers')."
5136   ;; Get the Xref when the users reads the articles since most/some
5137   ;; NNTP servers do not include Xrefs when using XOVER.
5138   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5139   (let ((mail-parse-charset gnus-newsgroup-charset)
5140         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5141         (cur nntp-server-buffer)
5142         (dependencies (or dependencies gnus-newsgroup-dependencies))
5143         number headers header)
5144     (save-excursion
5145       (set-buffer nntp-server-buffer)
5146       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5147       ;; Allow the user to mangle the headers before parsing them.
5148       (gnus-run-hooks 'gnus-parse-headers-hook)
5149       (goto-char (point-min))
5150       (while (not (eobp))
5151         (condition-case ()
5152             (while (and sequence (not (eobp)))
5153               (setq number (read cur))
5154               (while (and sequence
5155                           (< (car sequence) number))
5156                 (setq sequence (cdr sequence)))
5157               (and sequence
5158                    (eq number (car sequence))
5159                    (progn
5160                      (setq sequence (cdr sequence))
5161                      (setq header (inline
5162                                     (gnus-nov-parse-line
5163                                      number dependencies force-new))))
5164                    (push header headers))
5165               (forward-line 1))
5166           (error
5167            (gnus-error 4 "Strange nov line (%d)"
5168                        (count-lines (point-min) (point)))))
5169         (forward-line 1))
5170       ;; A common bug in inn is that if you have posted an article and
5171       ;; then retrieves the active file, it will answer correctly --
5172       ;; the new article is included.  However, a NOV entry for the
5173       ;; article may not have been generated yet, so this may fail.
5174       ;; We work around this problem by retrieving the last few
5175       ;; headers using HEAD.
5176       (if (or (not also-fetch-heads)
5177               (not sequence))
5178           ;; We (probably) got all the headers.
5179           (nreverse headers)
5180         (let ((gnus-nov-is-evil t))
5181           (nconc
5182            (nreverse headers)
5183            ;;;!!! FIXME: temporary fix for an infloop on nnimap.
5184            (if (eq 'nnimap (car (gnus-find-method-for-group group)))
5185                (when (gnus-retrieve-headers sequence group)
5186                  (gnus-get-newsgroup-headers))
5187              (gnus-retrieve-parsed-headers sequence group))))))))
5188
5189 (defun gnus-article-get-xrefs ()
5190   "Fill in the Xref value in `gnus-current-headers', if necessary.
5191 This is meant to be called in `gnus-article-internal-prepare-hook'."
5192   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5193                                  gnus-current-headers)))
5194     (or (not gnus-use-cross-reference)
5195         (not headers)
5196         (and (mail-header-xref headers)
5197              (not (string= (mail-header-xref headers) "")))
5198         (let ((case-fold-search t)
5199               xref)
5200           (save-restriction
5201             (nnheader-narrow-to-headers)
5202             (goto-char (point-min))
5203             (when (or (and (not (eobp))
5204                            (eq (downcase (char-after)) ?x)
5205                            (looking-at "Xref:"))
5206                       (search-forward "\nXref:" nil t))
5207               (goto-char (1+ (match-end 0)))
5208               (setq xref (buffer-substring (point)
5209                                            (progn (end-of-line) (point))))
5210               (mail-header-set-xref headers xref)))))))
5211
5212 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5213   "Find article ID and insert the summary line for that article.
5214 OLD-HEADER can either be a header or a line number to insert
5215 the subject line on."
5216   (let* ((line (and (numberp old-header) old-header))
5217          (old-header (and (vectorp old-header) old-header))
5218          (header (cond ((and old-header use-old-header)
5219                         old-header)
5220                        ((and (numberp id)
5221                              (gnus-number-to-header id))
5222                         (gnus-number-to-header id))
5223                        (t
5224                         (gnus-read-header id))))
5225          (number (and (numberp id) id))
5226          d)
5227     (when header
5228       ;; Rebuild the thread that this article is part of and go to the
5229       ;; article we have fetched.
5230       (when (and (not gnus-show-threads)
5231                  old-header)
5232         (when (and number
5233                    (setq d (gnus-data-find (mail-header-number old-header))))
5234           (goto-char (gnus-data-pos d))
5235           (gnus-data-remove
5236            number
5237            (- (gnus-point-at-bol)
5238               (prog1
5239                   (1+ (gnus-point-at-eol))
5240                 (gnus-delete-line))))))
5241       (when old-header
5242         (mail-header-set-number header (mail-header-number old-header)))
5243       (setq gnus-newsgroup-sparse
5244             (delq (setq number (mail-header-number header))
5245                   gnus-newsgroup-sparse))
5246       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5247       (push number gnus-newsgroup-limit)
5248       (gnus-rebuild-thread (mail-header-id header) line)
5249       (gnus-summary-goto-subject number nil t))
5250     (when (and (numberp number)
5251                (> number 0))
5252       ;; We have to update the boundaries even if we can't fetch the
5253       ;; article if ID is a number -- so that the next `P' or `N'
5254       ;; command will fetch the previous (or next) article even
5255       ;; if the one we tried to fetch this time has been canceled.
5256       (when (> number gnus-newsgroup-end)
5257         (setq gnus-newsgroup-end number))
5258       (when (< number gnus-newsgroup-begin)
5259         (setq gnus-newsgroup-begin number))
5260       (setq gnus-newsgroup-unselected
5261             (delq number gnus-newsgroup-unselected)))
5262     ;; Report back a success?
5263     (and header (mail-header-number header))))
5264
5265 ;;; Process/prefix in the summary buffer
5266
5267 (defun gnus-summary-work-articles (n)
5268   "Return a list of articles to be worked upon.
5269 The prefix argument, the list of process marked articles, and the
5270 current article will be taken into consideration."
5271   (save-excursion
5272     (set-buffer gnus-summary-buffer)
5273     (cond
5274      (n
5275       ;; A numerical prefix has been given.
5276       (setq n (prefix-numeric-value n))
5277       (let ((backward (< n 0))
5278             (n (abs (prefix-numeric-value n)))
5279             articles article)
5280         (save-excursion
5281           (while
5282               (and (> n 0)
5283                    (push (setq article (gnus-summary-article-number))
5284                          articles)
5285                    (if backward
5286                        (gnus-summary-find-prev nil article)
5287                      (gnus-summary-find-next nil article)))
5288             (decf n)))
5289         (nreverse articles)))
5290      ((and (gnus-region-active-p) (mark))
5291       (message "region active")
5292       ;; Work on the region between point and mark.
5293       (let ((max (max (point) (mark)))
5294             articles article)
5295         (save-excursion
5296           (goto-char (min (point) (mark)))
5297           (while
5298               (and
5299                (push (setq article (gnus-summary-article-number)) articles)
5300                (gnus-summary-find-next nil article)
5301                (< (point) max)))
5302           (nreverse articles))))
5303      (gnus-newsgroup-processable
5304       ;; There are process-marked articles present.
5305       ;; Save current state.
5306       (gnus-summary-save-process-mark)
5307       ;; Return the list.
5308       (reverse gnus-newsgroup-processable))
5309      (t
5310       ;; Just return the current article.
5311       (list (gnus-summary-article-number))))))
5312
5313 (defmacro gnus-summary-iterate (arg &rest forms)
5314   "Iterate over the process/prefixed articles and do FORMS.
5315 ARG is the interactive prefix given to the command.  FORMS will be
5316 executed with point over the summary line of the articles."
5317   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5318     `(let ((,articles (gnus-summary-work-articles ,arg)))
5319        (while ,articles
5320          (gnus-summary-goto-subject (car ,articles))
5321          ,@forms
5322          (pop ,articles)))))
5323
5324 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5325 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5326
5327 (defun gnus-summary-save-process-mark ()
5328   "Push the current set of process marked articles on the stack."
5329   (interactive)
5330   (push (copy-sequence gnus-newsgroup-processable)
5331         gnus-newsgroup-process-stack))
5332
5333 (defun gnus-summary-kill-process-mark ()
5334   "Push the current set of process marked articles on the stack and unmark."
5335   (interactive)
5336   (gnus-summary-save-process-mark)
5337   (gnus-summary-unmark-all-processable))
5338
5339 (defun gnus-summary-yank-process-mark ()
5340   "Pop the last process mark state off the stack and restore it."
5341   (interactive)
5342   (unless gnus-newsgroup-process-stack
5343     (error "Empty mark stack"))
5344   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5345
5346 (defun gnus-summary-process-mark-set (set)
5347   "Make SET into the current process marked articles."
5348   (gnus-summary-unmark-all-processable)
5349   (while set
5350     (gnus-summary-set-process-mark (pop set))))
5351
5352 ;;; Searching and stuff
5353
5354 (defun gnus-summary-search-group (&optional backward use-level)
5355   "Search for next unread newsgroup.
5356 If optional argument BACKWARD is non-nil, search backward instead."
5357   (save-excursion
5358     (set-buffer gnus-group-buffer)
5359     (when (gnus-group-search-forward
5360            backward nil (if use-level (gnus-group-group-level) nil))
5361       (gnus-group-group-name))))
5362
5363 (defun gnus-summary-best-group (&optional exclude-group)
5364   "Find the name of the best unread group.
5365 If EXCLUDE-GROUP, do not go to this group."
5366   (save-excursion
5367     (set-buffer gnus-group-buffer)
5368     (save-excursion
5369       (gnus-group-best-unread-group exclude-group))))
5370
5371 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5372   (if backward (gnus-summary-find-prev)
5373     (let* ((dummy (gnus-summary-article-intangible-p))
5374            (article (or article (gnus-summary-article-number)))
5375            (arts (gnus-data-find-list article))
5376            result)
5377       (when (and (not dummy)
5378                  (or (not gnus-summary-check-current)
5379                      (not unread)
5380                      (not (gnus-data-unread-p (car arts)))))
5381         (setq arts (cdr arts)))
5382       (when (setq result
5383                   (if unread
5384                       (progn
5385                         (while arts
5386                           (when (or (and undownloaded
5387                                          (eq gnus-undownloaded-mark
5388                                              (gnus-data-mark (car arts))))
5389                                     (gnus-data-unread-p (car arts)))
5390                             (setq result (car arts)
5391                                   arts nil))
5392                           (setq arts (cdr arts)))
5393                         result)
5394                     (car arts)))
5395         (goto-char (gnus-data-pos result))
5396         (gnus-data-number result)))))
5397
5398 (defun gnus-summary-find-prev (&optional unread article)
5399   (let* ((eobp (eobp))
5400          (article (or article (gnus-summary-article-number)))
5401          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5402          result)
5403     (when (and (not eobp)
5404                (or (not gnus-summary-check-current)
5405                    (not unread)
5406                    (not (gnus-data-unread-p (car arts)))))
5407       (setq arts (cdr arts)))
5408     (when (setq result
5409                 (if unread
5410                     (progn
5411                       (while arts
5412                         (when (gnus-data-unread-p (car arts))
5413                           (setq result (car arts)
5414                                 arts nil))
5415                         (setq arts (cdr arts)))
5416                       result)
5417                   (car arts)))
5418       (goto-char (gnus-data-pos result))
5419       (gnus-data-number result))))
5420
5421 (defun gnus-summary-find-subject (subject &optional unread backward article)
5422   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5423          (article (or article (gnus-summary-article-number)))
5424          (articles (gnus-data-list backward))
5425          (arts (gnus-data-find-list article articles))
5426          result)
5427     (when (or (not gnus-summary-check-current)
5428               (not unread)
5429               (not (gnus-data-unread-p (car arts))))
5430       (setq arts (cdr arts)))
5431     (while arts
5432       (and (or (not unread)
5433                (gnus-data-unread-p (car arts)))
5434            (vectorp (gnus-data-header (car arts)))
5435            (gnus-subject-equal
5436             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5437            (setq result (car arts)
5438                  arts nil))
5439       (setq arts (cdr arts)))
5440     (and result
5441          (goto-char (gnus-data-pos result))
5442          (gnus-data-number result))))
5443
5444 (defun gnus-summary-search-forward (&optional unread subject backward)
5445   "Search forward for an article.
5446 If UNREAD, look for unread articles.  If SUBJECT, look for
5447 articles with that subject.  If BACKWARD, search backward instead."
5448   (cond (subject (gnus-summary-find-subject subject unread backward))
5449         (backward (gnus-summary-find-prev unread))
5450         (t (gnus-summary-find-next unread))))
5451
5452 (defun gnus-recenter (&optional n)
5453   "Center point in window and redisplay frame.
5454 Also do horizontal recentering."
5455   (interactive "P")
5456   (when (and gnus-auto-center-summary
5457              (not (eq gnus-auto-center-summary 'vertical)))
5458     (gnus-horizontal-recenter))
5459   (recenter n))
5460
5461 (defun gnus-summary-recenter ()
5462   "Center point in the summary window.
5463 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5464 displayed, no centering will be performed."
5465   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5466   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5467   (interactive)
5468   (let* ((top (cond ((< (window-height) 4) 0)
5469                     ((< (window-height) 7) 1)
5470                     (t (if (numberp gnus-auto-center-summary)
5471                            gnus-auto-center-summary
5472                          2))))
5473          (height (1- (window-height)))
5474          (bottom (save-excursion (goto-char (point-max))
5475                                  (forward-line (- height))
5476                                  (point)))
5477          (window (get-buffer-window (current-buffer))))
5478     ;; The user has to want it.
5479     (when gnus-auto-center-summary
5480       (when (get-buffer-window gnus-article-buffer)
5481         ;; Only do recentering when the article buffer is displayed,
5482         ;; Set the window start to either `bottom', which is the biggest
5483         ;; possible valid number, or the second line from the top,
5484         ;; whichever is the least.
5485         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5486           (if (> bottom top-pos)
5487               ;; Keep the second line from the top visible
5488               (set-window-start window top-pos t)
5489             ;; Try to keep the bottom line visible; if it's partially
5490             ;; obscured, either scroll one more line to make it fully
5491             ;; visible, or revert to using TOP-POS.
5492             (save-excursion
5493               (goto-char (point-max))
5494               (forward-line -1)
5495               (let ((last-line-start (point)))
5496                 (goto-char bottom)
5497                 (set-window-start window (point) t)
5498                 (when (not (pos-visible-in-window-p last-line-start window))
5499                   (forward-line 1)
5500                   (set-window-start window (min (point) top-pos) t)))))))
5501       ;; Do horizontal recentering while we're at it.
5502       (when (and (get-buffer-window (current-buffer) t)
5503                  (not (eq gnus-auto-center-summary 'vertical)))
5504         (let ((selected (selected-window)))
5505           (select-window (get-buffer-window (current-buffer) t))
5506           (gnus-summary-position-point)
5507           (gnus-horizontal-recenter)
5508           (select-window selected))))))
5509
5510 (defun gnus-summary-jump-to-group (newsgroup)
5511   "Move point to NEWSGROUP in group mode buffer."
5512   ;; Keep update point of group mode buffer if visible.
5513   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5514       (save-window-excursion
5515         ;; Take care of tree window mode.
5516         (when (get-buffer-window gnus-group-buffer)
5517           (pop-to-buffer gnus-group-buffer))
5518         (gnus-group-jump-to-group newsgroup))
5519     (save-excursion
5520       ;; Take care of tree window mode.
5521       (if (get-buffer-window gnus-group-buffer)
5522           (pop-to-buffer gnus-group-buffer)
5523         (set-buffer gnus-group-buffer))
5524       (gnus-group-jump-to-group newsgroup))))
5525
5526 ;; This function returns a list of article numbers based on the
5527 ;; difference between the ranges of read articles in this group and
5528 ;; the range of active articles.
5529 (defun gnus-list-of-unread-articles (group)
5530   (let* ((read (gnus-info-read (gnus-get-info group)))
5531          (active (or (gnus-active group) (gnus-activate-group group)))
5532          (last (cdr active))
5533          first nlast unread)
5534     ;; If none are read, then all are unread.
5535     (if (not read)
5536         (setq first (car active))
5537       ;; If the range of read articles is a single range, then the
5538       ;; first unread article is the article after the last read
5539       ;; article.  Sounds logical, doesn't it?
5540       (if (and (not (listp (cdr read)))
5541                (or (< (car read) (car active))
5542                    (progn (setq read (list read))
5543                           nil)))
5544           (setq first (max (car active) (1+ (cdr read))))
5545         ;; `read' is a list of ranges.
5546         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5547                                   (caar read)))
5548                   1)
5549           (setq first (car active)))
5550         (while read
5551           (when first
5552             (while (< first nlast)
5553               (push first unread)
5554               (setq first (1+ first))))
5555           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5556           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5557           (setq read (cdr read)))))
5558     ;; And add the last unread articles.
5559     (while (<= first last)
5560       (push first unread)
5561       (setq first (1+ first)))
5562     ;; Return the list of unread articles.
5563     (delq 0 (nreverse unread))))
5564
5565 (defun gnus-list-of-read-articles (group)
5566   "Return a list of unread, unticked and non-dormant articles."
5567   (let* ((info (gnus-get-info group))
5568          (marked (gnus-info-marks info))
5569          (active (gnus-active group)))
5570     (and info active
5571          (gnus-set-difference
5572           (gnus-sorted-complement
5573            (gnus-uncompress-range active)
5574            (gnus-list-of-unread-articles group))
5575           (append
5576            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5577            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5578
5579 ;; Various summary commands
5580
5581 (defun gnus-summary-select-article-buffer ()
5582   "Reconfigure windows to show article buffer."
5583   (interactive)
5584   (if (not (gnus-buffer-live-p gnus-article-buffer))
5585       (error "There is no article buffer for this summary buffer")
5586     (gnus-configure-windows 'article)
5587     (select-window (get-buffer-window gnus-article-buffer))))
5588
5589 (defun gnus-summary-universal-argument (arg)
5590   "Perform any operation on all articles that are process/prefixed."
5591   (interactive "P")
5592   (let ((articles (gnus-summary-work-articles arg))
5593         func article)
5594     (if (eq
5595          (setq
5596           func
5597           (key-binding
5598            (read-key-sequence
5599             (substitute-command-keys
5600              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5601          'undefined)
5602         (gnus-error 1 "Undefined key")
5603       (save-excursion
5604         (while articles
5605           (gnus-summary-goto-subject (setq article (pop articles)))
5606           (let (gnus-newsgroup-processable)
5607             (command-execute func))
5608           (gnus-summary-remove-process-mark article)))))
5609   (gnus-summary-position-point))
5610
5611 (defun gnus-summary-toggle-truncation (&optional arg)
5612   "Toggle truncation of summary lines.
5613 With arg, turn line truncation on iff arg is positive."
5614   (interactive "P")
5615   (setq truncate-lines
5616         (if (null arg) (not truncate-lines)
5617           (> (prefix-numeric-value arg) 0)))
5618   (redraw-display))
5619
5620 (defun gnus-summary-reselect-current-group (&optional all rescan)
5621   "Exit and then reselect the current newsgroup.
5622 The prefix argument ALL means to select all articles."
5623   (interactive "P")
5624   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5625     (error "Ephemeral groups can't be reselected"))
5626   (let ((current-subject (gnus-summary-article-number))
5627         (group gnus-newsgroup-name))
5628     (setq gnus-newsgroup-begin nil)
5629     (gnus-summary-exit)
5630     ;; We have to adjust the point of group mode buffer because
5631     ;; point was moved to the next unread newsgroup by exiting.
5632     (gnus-summary-jump-to-group group)
5633     (when rescan
5634       (save-excursion
5635         (save-window-excursion
5636           ;; Don't show group contents.
5637           (set-window-start (selected-window) (point-max))
5638           (gnus-group-get-new-news-this-group 1))))
5639     (gnus-group-read-group all t)
5640     (gnus-summary-goto-subject current-subject nil t)))
5641
5642 (defun gnus-summary-rescan-group (&optional all)
5643   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5644   (interactive "P")
5645   (gnus-summary-reselect-current-group all t))
5646
5647 (defun gnus-summary-update-info (&optional non-destructive)
5648   (save-excursion
5649     (let ((group gnus-newsgroup-name))
5650       (when group
5651         (when gnus-newsgroup-kill-headers
5652           (setq gnus-newsgroup-killed
5653                 (gnus-compress-sequence
5654                  (nconc
5655                   (gnus-set-sorted-intersection
5656                    (gnus-uncompress-range gnus-newsgroup-killed)
5657                    (setq gnus-newsgroup-unselected
5658                          (sort gnus-newsgroup-unselected '<)))
5659                   (setq gnus-newsgroup-unreads
5660                         (sort gnus-newsgroup-unreads '<)))
5661                  t)))
5662         (unless (listp (cdr gnus-newsgroup-killed))
5663           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5664         (let ((headers gnus-newsgroup-headers))
5665           ;; Set the new ranges of read articles.
5666           (save-excursion
5667             (set-buffer gnus-group-buffer)
5668             (gnus-undo-force-boundary))
5669           (gnus-update-read-articles
5670            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5671           ;; Set the current article marks.
5672           (let ((gnus-newsgroup-scored
5673                  (if (and (not gnus-save-score)
5674                           (not non-destructive))
5675                      nil
5676                    gnus-newsgroup-scored)))
5677             (save-excursion
5678               (gnus-update-marks)))
5679           ;; Do the cross-ref thing.
5680           (when gnus-use-cross-reference
5681             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5682           ;; Do not switch windows but change the buffer to work.
5683           (set-buffer gnus-group-buffer)
5684           (unless (gnus-ephemeral-group-p group)
5685             (gnus-group-update-group group)))))))
5686
5687 (defun gnus-summary-save-newsrc (&optional force)
5688   "Save the current number of read/marked articles in the dribble buffer.
5689 The dribble buffer will then be saved.
5690 If FORCE (the prefix), also save the .newsrc file(s)."
5691   (interactive "P")
5692   (gnus-summary-update-info t)
5693   (if force
5694       (gnus-save-newsrc-file)
5695     (gnus-dribble-save)))
5696
5697 (defun gnus-summary-exit (&optional temporary)
5698   "Exit reading current newsgroup, and then return to group selection mode.
5699 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5700   (interactive)
5701   (gnus-set-global-variables)
5702   (gnus-kill-save-kill-buffer)
5703   (gnus-async-halt-prefetch)
5704   (let* ((group gnus-newsgroup-name)
5705          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5706          (mode major-mode)
5707          (group-point nil)
5708          (buf (current-buffer)))
5709     (unless quit-config
5710       ;; Do adaptive scoring, and possibly save score files.
5711       (when gnus-newsgroup-adaptive
5712         (gnus-score-adaptive))
5713       (when gnus-use-scoring
5714         (gnus-score-save)))
5715     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5716     ;; If we have several article buffers, we kill them at exit.
5717     (unless gnus-single-article-buffer
5718       (gnus-kill-buffer gnus-original-article-buffer)
5719       (setq gnus-article-current nil))
5720     (when gnus-use-cache
5721       (gnus-cache-possibly-remove-articles)
5722       (gnus-cache-save-buffers))
5723     (gnus-async-prefetch-remove-group group)
5724     (when gnus-suppress-duplicates
5725       (gnus-dup-enter-articles))
5726     (when gnus-use-trees
5727       (gnus-tree-close group))
5728     (when gnus-use-cache
5729       (gnus-cache-write-active))
5730     ;; Remove entries for this group.
5731     (nnmail-purge-split-history (gnus-group-real-name group))
5732     ;; Make all changes in this group permanent.
5733     (unless quit-config
5734       (gnus-run-hooks 'gnus-exit-group-hook)
5735       (gnus-summary-update-info))
5736     (gnus-close-group group)
5737     ;; Make sure where we were, and go to next newsgroup.
5738     (set-buffer gnus-group-buffer)
5739     (unless quit-config
5740       (gnus-group-jump-to-group group))
5741     (gnus-run-hooks 'gnus-summary-exit-hook)
5742     (unless (or quit-config
5743                 ;; If this group has disappeared from the summary
5744                 ;; buffer, don't skip forwards.
5745                 (not (string= group (gnus-group-group-name))))
5746       (gnus-group-next-unread-group 1))
5747     (setq group-point (point))
5748     (if temporary
5749         nil                             ;Nothing to do.
5750       ;; If we have several article buffers, we kill them at exit.
5751       (unless gnus-single-article-buffer
5752         (gnus-kill-buffer gnus-article-buffer)
5753         (gnus-kill-buffer gnus-original-article-buffer)
5754         (setq gnus-article-current nil))
5755       (set-buffer buf)
5756       (if (not gnus-kill-summary-on-exit)
5757           (progn
5758             (gnus-deaden-summary)
5759             (setq mode nil))
5760         ;; We set all buffer-local variables to nil.  It is unclear why
5761         ;; this is needed, but if we don't, buffer-local variables are
5762         ;; not garbage-collected, it seems.  This would the lead to en
5763         ;; ever-growing Emacs.
5764         (gnus-summary-clear-local-variables)
5765         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5766           (gnus-summary-clear-local-variables))
5767         (when (get-buffer gnus-article-buffer)
5768           (bury-buffer gnus-article-buffer))
5769         ;; We clear the global counterparts of the buffer-local
5770         ;; variables as well, just to be on the safe side.
5771         (set-buffer gnus-group-buffer)
5772         (gnus-summary-clear-local-variables)
5773         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5774           (gnus-summary-clear-local-variables)))
5775       (setq gnus-current-select-method gnus-select-method)
5776       (pop-to-buffer gnus-group-buffer)
5777       (if (not quit-config)
5778           (progn
5779             (goto-char group-point)
5780             (gnus-configure-windows 'group 'force)
5781             (unless (pos-visible-in-window-p)
5782               (forward-line (/ (static-if (featurep 'xemacs)
5783                                    (window-displayed-height)
5784                                  (1- (window-height)))
5785                                -2))
5786               (set-window-start (selected-window) (point))
5787               (goto-char group-point)))
5788         (gnus-handle-ephemeral-exit quit-config))
5789       ;; Return to group mode buffer.
5790       (when (eq mode 'gnus-summary-mode)
5791         (gnus-kill-buffer buf))
5792       ;; Clear the current group name.
5793       (unless quit-config
5794         (setq gnus-newsgroup-name nil)))))
5795
5796 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5797 (defun gnus-summary-exit-no-update (&optional no-questions)
5798   "Quit reading current newsgroup without updating read article info."
5799   (interactive)
5800   (let* ((group gnus-newsgroup-name)
5801          (quit-config (gnus-group-quit-config group)))
5802     (when (or no-questions
5803               gnus-expert-user
5804               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5805       (gnus-async-halt-prefetch)
5806       (mapcar 'funcall
5807               (delq 'gnus-summary-expire-articles
5808                     (copy-sequence gnus-summary-prepare-exit-hook)))
5809       ;; If we have several article buffers, we kill them at exit.
5810       (unless gnus-single-article-buffer
5811         (gnus-kill-buffer gnus-article-buffer)
5812         (gnus-kill-buffer gnus-original-article-buffer)
5813         (setq gnus-article-current nil))
5814       (if (not gnus-kill-summary-on-exit)
5815           (gnus-deaden-summary)
5816         (gnus-close-group group)
5817         (gnus-summary-clear-local-variables)
5818         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5819           (gnus-summary-clear-local-variables))
5820         (set-buffer gnus-group-buffer)
5821         (gnus-summary-clear-local-variables)
5822         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5823           (gnus-summary-clear-local-variables))
5824         (when (get-buffer gnus-summary-buffer)
5825           (kill-buffer gnus-summary-buffer)))
5826       (unless gnus-single-article-buffer
5827         (setq gnus-article-current nil))
5828       (when gnus-use-trees
5829         (gnus-tree-close group))
5830       (gnus-async-prefetch-remove-group group)
5831       (when (get-buffer gnus-article-buffer)
5832         (bury-buffer gnus-article-buffer))
5833       ;; Return to the group buffer.
5834       (gnus-configure-windows 'group 'force)
5835       ;; Clear the current group name.
5836       (setq gnus-newsgroup-name nil)
5837       (when (equal (gnus-group-group-name) group)
5838         (gnus-group-next-unread-group 1))
5839       (when quit-config
5840         (gnus-handle-ephemeral-exit quit-config)))))
5841
5842 (defun gnus-handle-ephemeral-exit (quit-config)
5843   "Handle movement when leaving an ephemeral group.
5844 The state which existed when entering the ephemeral is reset."
5845   (if (not (buffer-name (car quit-config)))
5846       (gnus-configure-windows 'group 'force)
5847     (set-buffer (car quit-config))
5848     (cond ((eq major-mode 'gnus-summary-mode)
5849            (gnus-set-global-variables))
5850           ((eq major-mode 'gnus-article-mode)
5851            (save-excursion
5852              ;; The `gnus-summary-buffer' variable may point
5853              ;; to the old summary buffer when using a single
5854              ;; article buffer.
5855              (unless (gnus-buffer-live-p gnus-summary-buffer)
5856                (set-buffer gnus-group-buffer))
5857              (set-buffer gnus-summary-buffer)
5858              (gnus-set-global-variables))))
5859     (if (or (eq (cdr quit-config) 'article)
5860             (eq (cdr quit-config) 'pick))
5861         (progn
5862           ;; The current article may be from the ephemeral group
5863           ;; thus it is best that we reload this article
5864           (gnus-summary-show-article)
5865           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5866               (gnus-configure-windows 'pick 'force)
5867             (gnus-configure-windows (cdr quit-config) 'force)))
5868       (gnus-configure-windows (cdr quit-config) 'force))
5869     (when (eq major-mode 'gnus-summary-mode)
5870       (gnus-summary-next-subject 1 nil t)
5871       (gnus-summary-recenter)
5872       (gnus-summary-position-point))))
5873
5874 (defun gnus-summary-preview-mime-message ()
5875   "MIME decode and play this message."
5876   (interactive)
5877   (let ((gnus-break-pages nil)
5878         (gnus-show-mime t))
5879     (gnus-summary-select-article gnus-show-all-headers t))
5880   (select-window (get-buffer-window gnus-article-buffer)))
5881
5882 ;;; Dead summaries.
5883
5884 (defvar gnus-dead-summary-mode-map nil)
5885
5886 (unless gnus-dead-summary-mode-map
5887   (setq gnus-dead-summary-mode-map (make-keymap))
5888   (suppress-keymap gnus-dead-summary-mode-map)
5889   (substitute-key-definition
5890    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5891   (let ((keys '("\C-d" "\r" "\177" [delete])))
5892     (while keys
5893       (define-key gnus-dead-summary-mode-map
5894         (pop keys) 'gnus-summary-wake-up-the-dead))))
5895
5896 (defvar gnus-dead-summary-mode nil
5897   "Minor mode for Gnus summary buffers.")
5898
5899 (defun gnus-dead-summary-mode (&optional arg)
5900   "Minor mode for Gnus summary buffers."
5901   (interactive "P")
5902   (when (eq major-mode 'gnus-summary-mode)
5903     (make-local-variable 'gnus-dead-summary-mode)
5904     (setq gnus-dead-summary-mode
5905           (if (null arg) (not gnus-dead-summary-mode)
5906             (> (prefix-numeric-value arg) 0)))
5907     (when gnus-dead-summary-mode
5908       (gnus-add-minor-mode
5909        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5910
5911 (defun gnus-deaden-summary ()
5912   "Make the current summary buffer into a dead summary buffer."
5913   ;; Kill any previous dead summary buffer.
5914   (when (and gnus-dead-summary
5915              (buffer-name gnus-dead-summary))
5916     (save-excursion
5917       (set-buffer gnus-dead-summary)
5918       (when gnus-dead-summary-mode
5919         (kill-buffer (current-buffer)))))
5920   ;; Make this the current dead summary.
5921   (setq gnus-dead-summary (current-buffer))
5922   (gnus-dead-summary-mode 1)
5923   (let ((name (buffer-name)))
5924     (when (string-match "Summary" name)
5925       (rename-buffer
5926        (concat (substring name 0 (match-beginning 0)) "Dead "
5927                (substring name (match-beginning 0)))
5928        t)
5929       (bury-buffer))))
5930
5931 (defun gnus-kill-or-deaden-summary (buffer)
5932   "Kill or deaden the summary BUFFER."
5933   (save-excursion
5934     (when (and (buffer-name buffer)
5935                (not gnus-single-article-buffer))
5936       (save-excursion
5937         (set-buffer buffer)
5938         (gnus-kill-buffer gnus-article-buffer)
5939         (gnus-kill-buffer gnus-original-article-buffer)))
5940     (cond (gnus-kill-summary-on-exit
5941            (when (and gnus-use-trees
5942                       (gnus-buffer-exists-p buffer))
5943              (save-excursion
5944                (set-buffer buffer)
5945                (gnus-tree-close gnus-newsgroup-name)))
5946            (gnus-kill-buffer buffer))
5947           ((gnus-buffer-exists-p buffer)
5948            (save-excursion
5949              (set-buffer buffer)
5950              (gnus-deaden-summary))))))
5951
5952 (defun gnus-summary-wake-up-the-dead (&rest args)
5953   "Wake up the dead summary buffer."
5954   (interactive)
5955   (gnus-dead-summary-mode -1)
5956   (let ((name (buffer-name)))
5957     (when (string-match "Dead " name)
5958       (rename-buffer
5959        (concat (substring name 0 (match-beginning 0))
5960                (substring name (match-end 0)))
5961        t)))
5962   (gnus-message 3 "This dead summary is now alive again"))
5963
5964 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5965 (defun gnus-summary-fetch-faq (&optional faq-dir)
5966   "Fetch the FAQ for the current group.
5967 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5968 in."
5969   (interactive
5970    (list
5971     (when current-prefix-arg
5972       (completing-read
5973        "Faq dir: " (and (listp gnus-group-faq-directory)
5974                         (mapcar (lambda (file) (list file))
5975                                 gnus-group-faq-directory))))))
5976   (let (gnus-faq-buffer)
5977     (when (setq gnus-faq-buffer
5978                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5979       (gnus-configure-windows 'summary-faq))))
5980
5981 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5982 (defun gnus-summary-describe-group (&optional force)
5983   "Describe the current newsgroup."
5984   (interactive "P")
5985   (gnus-group-describe-group force gnus-newsgroup-name))
5986
5987 (defun gnus-summary-describe-briefly ()
5988   "Describe summary mode commands briefly."
5989   (interactive)
5990   (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")))
5991
5992 ;; Walking around group mode buffer from summary mode.
5993
5994 (defun gnus-summary-next-group (&optional no-article target-group backward)
5995   "Exit current newsgroup and then select next unread newsgroup.
5996 If prefix argument NO-ARTICLE is non-nil, no article is selected
5997 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5998 previous group instead."
5999   (interactive "P")
6000   ;; Stop pre-fetching.
6001   (gnus-async-halt-prefetch)
6002   (let ((current-group gnus-newsgroup-name)
6003         (current-buffer (current-buffer))
6004         entered)
6005     ;; First we semi-exit this group to update Xrefs and all variables.
6006     ;; We can't do a real exit, because the window conf must remain
6007     ;; the same in case the user is prompted for info, and we don't
6008     ;; want the window conf to change before that...
6009     (gnus-summary-exit t)
6010     (while (not entered)
6011       ;; Then we find what group we are supposed to enter.
6012       (set-buffer gnus-group-buffer)
6013       (gnus-group-jump-to-group current-group)
6014       (setq target-group
6015             (or target-group
6016                 (if (eq gnus-keep-same-level 'best)
6017                     (gnus-summary-best-group gnus-newsgroup-name)
6018                   (gnus-summary-search-group backward gnus-keep-same-level))))
6019       (if (not target-group)
6020           ;; There are no further groups, so we return to the group
6021           ;; buffer.
6022           (progn
6023             (gnus-message 5 "Returning to the group buffer")
6024             (setq entered t)
6025             (when (gnus-buffer-live-p current-buffer)
6026               (set-buffer current-buffer)
6027               (gnus-summary-exit))
6028             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6029         ;; We try to enter the target group.
6030         (gnus-group-jump-to-group target-group)
6031         (let ((unreads (gnus-group-group-unread)))
6032           (if (and (or (eq t unreads)
6033                        (and unreads (not (zerop unreads))))
6034                    (gnus-summary-read-group
6035                     target-group nil no-article
6036                     (and (buffer-name current-buffer) current-buffer)
6037                     nil backward))
6038               (setq entered t)
6039             (setq current-group target-group
6040                   target-group nil)))))))
6041
6042 (defun gnus-summary-prev-group (&optional no-article)
6043   "Exit current newsgroup and then select previous unread newsgroup.
6044 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6045   (interactive "P")
6046   (gnus-summary-next-group no-article nil t))
6047
6048 ;; Walking around summary lines.
6049
6050 (defun gnus-summary-first-subject (&optional unread undownloaded)
6051   "Go to the first unread subject.
6052 If UNREAD is non-nil, go to the first unread article.
6053 Returns the article selected or nil if there are no unread articles."
6054   (interactive "P")
6055   (prog1
6056       (cond
6057        ;; Empty summary.
6058        ((null gnus-newsgroup-data)
6059         (gnus-message 3 "No articles in the group")
6060         nil)
6061        ;; Pick the first article.
6062        ((not unread)
6063         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6064         (gnus-data-number (car gnus-newsgroup-data)))
6065        ;; No unread articles.
6066        ((null gnus-newsgroup-unreads)
6067         (gnus-message 3 "No more unread articles")
6068         nil)
6069        ;; Find the first unread article.
6070        (t
6071         (let ((data gnus-newsgroup-data))
6072           (while (and data
6073                       (and (not (and undownloaded
6074                                      (eq gnus-undownloaded-mark
6075                                          (gnus-data-mark (car data)))))
6076                            (not (gnus-data-unread-p (car data)))))
6077             (setq data (cdr data)))
6078           (when data
6079             (goto-char (gnus-data-pos (car data)))
6080             (gnus-data-number (car data))))))
6081     (gnus-summary-position-point)))
6082
6083 (defun gnus-summary-next-subject (n &optional unread dont-display)
6084   "Go to next N'th summary line.
6085 If N is negative, go to the previous N'th subject line.
6086 If UNREAD is non-nil, only unread articles are selected.
6087 The difference between N and the actual number of steps taken is
6088 returned."
6089   (interactive "p")
6090   (let ((backward (< n 0))
6091         (n (abs n)))
6092     (while (and (> n 0)
6093                 (if backward
6094                     (gnus-summary-find-prev unread)
6095                   (gnus-summary-find-next unread)))
6096       (unless (zerop (setq n (1- n)))
6097         (gnus-summary-show-thread)))
6098     (when (/= 0 n)
6099       (gnus-message 7 "No more%s articles"
6100                     (if unread " unread" "")))
6101     (unless dont-display
6102       (gnus-summary-recenter)
6103       (gnus-summary-position-point))
6104     n))
6105
6106 (defun gnus-summary-next-unread-subject (n)
6107   "Go to next N'th unread summary line."
6108   (interactive "p")
6109   (gnus-summary-next-subject n t))
6110
6111 (defun gnus-summary-prev-subject (n &optional unread)
6112   "Go to previous N'th summary line.
6113 If optional argument UNREAD is non-nil, only unread article is selected."
6114   (interactive "p")
6115   (gnus-summary-next-subject (- n) unread))
6116
6117 (defun gnus-summary-prev-unread-subject (n)
6118   "Go to previous N'th unread summary line."
6119   (interactive "p")
6120   (gnus-summary-next-subject (- n) t))
6121
6122 (defun gnus-summary-goto-subject (article &optional force silent)
6123   "Go the subject line of ARTICLE.
6124 If FORCE, also allow jumping to articles not currently shown."
6125   (interactive "nArticle number: ")
6126   (let ((b (point))
6127         (data (gnus-data-find article)))
6128     ;; We read in the article if we have to.
6129     (and (not data)
6130          force
6131          (gnus-summary-insert-subject
6132           article
6133           (if (or (numberp force) (vectorp force)) force)
6134           t)
6135          (setq data (gnus-data-find article)))
6136     (goto-char b)
6137     (if (not data)
6138         (progn
6139           (unless silent
6140             (gnus-message 3 "Can't find article %d" article))
6141           nil)
6142       (goto-char (gnus-data-pos data))
6143       (gnus-summary-position-point)
6144       article)))
6145
6146 ;; Walking around summary lines with displaying articles.
6147
6148 (defun gnus-summary-expand-window (&optional arg)
6149   "Make the summary buffer take up the entire Emacs frame.
6150 Given a prefix, will force an `article' buffer configuration."
6151   (interactive "P")
6152   (if arg
6153       (gnus-configure-windows 'article 'force)
6154     (gnus-configure-windows 'summary 'force)))
6155
6156 (defun gnus-summary-display-article (article &optional all-header)
6157   "Display ARTICLE in article buffer."
6158   (when (gnus-buffer-live-p gnus-article-buffer)
6159     (with-current-buffer gnus-article-buffer
6160       (set-buffer-multibyte t)))
6161   (gnus-set-global-variables)
6162   (when (gnus-buffer-live-p gnus-article-buffer)
6163     (with-current-buffer gnus-article-buffer
6164       (setq gnus-article-charset gnus-newsgroup-charset)
6165       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)))
6166   (if (null article)
6167       nil
6168     (prog1
6169         (if gnus-summary-display-article-function
6170             (funcall gnus-summary-display-article-function article all-header)
6171           (gnus-article-prepare article all-header))
6172       (with-current-buffer gnus-article-buffer
6173         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6174              nil))
6175       (gnus-run-hooks 'gnus-select-article-hook)
6176       (when (and gnus-current-article
6177                  (not (zerop gnus-current-article)))
6178         (gnus-summary-goto-subject gnus-current-article))
6179       (gnus-summary-recenter)
6180       (when (and gnus-use-trees gnus-show-threads)
6181         (gnus-possibly-generate-tree article)
6182         (gnus-highlight-selected-tree article))
6183       ;; Successfully display article.
6184       (gnus-article-set-window-start
6185        (cdr (assq article gnus-newsgroup-bookmarks))))))
6186
6187 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6188   "Select the current article.
6189 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6190 non-nil, the article will be re-fetched even if it already present in
6191 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6192 be displayed."
6193   ;; Make sure we are in the summary buffer to work around bbdb bug.
6194   (unless (eq major-mode 'gnus-summary-mode)
6195     (set-buffer gnus-summary-buffer))
6196   (let ((article (or article (gnus-summary-article-number)))
6197         (all-headers (not (not all-headers))) ;Must be T or NIL.
6198         gnus-summary-display-article-function)
6199     (and (not pseudo)
6200          (gnus-summary-article-pseudo-p article)
6201          (error "This is a pseudo-article"))
6202     (save-excursion
6203       (set-buffer gnus-summary-buffer)
6204       (if (or (and gnus-single-article-buffer
6205                    (or (null gnus-current-article)
6206                        (null gnus-article-current)
6207                        (null (get-buffer gnus-article-buffer))
6208                        (not (eq article (cdr gnus-article-current)))
6209                        (not (equal (car gnus-article-current)
6210                                    gnus-newsgroup-name))))
6211               (and (not gnus-single-article-buffer)
6212                    (or (null gnus-current-article)
6213                        (not (eq gnus-current-article article))))
6214               force)
6215           ;; The requested article is different from the current article.
6216           (progn
6217             (gnus-summary-display-article article all-headers)
6218             (when (or all-headers gnus-show-all-headers)
6219               (gnus-article-show-all-headers))
6220             (gnus-article-set-window-start
6221              (cdr (assq article gnus-newsgroup-bookmarks)))
6222             article)
6223         (when (or all-headers gnus-show-all-headers)
6224           (gnus-article-show-all-headers))
6225         'old))))
6226
6227 (defun gnus-summary-force-verify-and-decrypt ()
6228   (interactive)
6229   (let ((mm-verify-option 'known)
6230         (mm-decrypt-option 'known))
6231     (gnus-summary-select-article nil 'force)))
6232
6233 (defun gnus-summary-set-current-mark (&optional current-mark)
6234   "Obsolete function."
6235   nil)
6236
6237 (defun gnus-summary-next-article (&optional unread subject backward push)
6238   "Select the next article.
6239 If UNREAD, only unread articles are selected.
6240 If SUBJECT, only articles with SUBJECT are selected.
6241 If BACKWARD, the previous article is selected instead of the next."
6242   (interactive "P")
6243   (cond
6244    ;; Is there such an article?
6245    ((and (gnus-summary-search-forward unread subject backward)
6246          (or (gnus-summary-display-article (gnus-summary-article-number))
6247              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6248     (gnus-summary-position-point))
6249    ;; If not, we try the first unread, if that is wanted.
6250    ((and subject
6251          gnus-auto-select-same
6252          (gnus-summary-first-unread-article))
6253     (gnus-summary-position-point)
6254     (gnus-message 6 "Wrapped"))
6255    ;; Try to get next/previous article not displayed in this group.
6256    ((and gnus-auto-extend-newsgroup
6257          (not unread) (not subject))
6258     (gnus-summary-goto-article
6259      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6260      nil (count-lines (point-min) (point))))
6261    ;; Go to next/previous group.
6262    (t
6263     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6264       (gnus-summary-jump-to-group gnus-newsgroup-name))
6265     (let ((cmd last-command-char)
6266           (point
6267            (save-excursion
6268              (set-buffer gnus-group-buffer)
6269              (point)))
6270           (group
6271            (if (eq gnus-keep-same-level 'best)
6272                (gnus-summary-best-group gnus-newsgroup-name)
6273              (gnus-summary-search-group backward gnus-keep-same-level))))
6274       ;; For some reason, the group window gets selected.  We change
6275       ;; it back.
6276       (select-window (get-buffer-window (current-buffer)))
6277       ;; Select next unread newsgroup automagically.
6278       (cond
6279        ((or (not gnus-auto-select-next)
6280             (not cmd))
6281         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6282        ((or (eq gnus-auto-select-next 'quietly)
6283             (and (eq gnus-auto-select-next 'slightly-quietly)
6284                  push)
6285             (and (eq gnus-auto-select-next 'almost-quietly)
6286                  (gnus-summary-last-article-p)))
6287         ;; Select quietly.
6288         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6289             (gnus-summary-exit)
6290           (gnus-message 7 "No more%s articles (%s)..."
6291                         (if unread " unread" "")
6292                         (if group (concat "selecting " group)
6293                           "exiting"))
6294           (gnus-summary-next-group nil group backward)))
6295        (t
6296         (when (gnus-key-press-event-p last-input-event)
6297           (gnus-summary-walk-group-buffer
6298            gnus-newsgroup-name cmd unread backward point))))))))
6299
6300 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6301   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6302                       (?\C-p (gnus-group-prev-unread-group 1))))
6303         (cursor-in-echo-area t)
6304         keve key group ended)
6305     (save-excursion
6306       (set-buffer gnus-group-buffer)
6307       (goto-char start)
6308       (setq group
6309             (if (eq gnus-keep-same-level 'best)
6310                 (gnus-summary-best-group gnus-newsgroup-name)
6311               (gnus-summary-search-group backward gnus-keep-same-level))))
6312     (while (not ended)
6313       (gnus-message
6314        5 "No more%s articles%s" (if unread " unread" "")
6315        (if (and group
6316                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6317            (format " (Type %s for %s [%s])"
6318                    (single-key-description cmd) group
6319                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6320          (format " (Type %s to exit %s)"
6321                  (single-key-description cmd)
6322                  gnus-newsgroup-name)))
6323       ;; Confirm auto selection.
6324       (setq key (car (setq keve (gnus-read-event-char))))
6325       (setq ended t)
6326       (cond
6327        ((assq key keystrokes)
6328         (let ((obuf (current-buffer)))
6329           (switch-to-buffer gnus-group-buffer)
6330           (when group
6331             (gnus-group-jump-to-group group))
6332           (eval (cadr (assq key keystrokes)))
6333           (setq group (gnus-group-group-name))
6334           (switch-to-buffer obuf))
6335         (setq ended nil))
6336        ((equal key cmd)
6337         (if (or (not group)
6338                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6339             (gnus-summary-exit)
6340           (gnus-summary-next-group nil group backward)))
6341        (t
6342         (push (cdr keve) unread-command-events))))))
6343
6344 (defun gnus-summary-next-unread-article ()
6345   "Select unread article after current one."
6346   (interactive)
6347   (gnus-summary-next-article
6348    (or (not (eq gnus-summary-goto-unread 'never))
6349        (gnus-summary-last-article-p (gnus-summary-article-number)))
6350    (and gnus-auto-select-same
6351         (gnus-summary-article-subject))))
6352
6353 (defun gnus-summary-prev-article (&optional unread subject)
6354   "Select the article after the current one.
6355 If UNREAD is non-nil, only unread articles are selected."
6356   (interactive "P")
6357   (gnus-summary-next-article unread subject t))
6358
6359 (defun gnus-summary-prev-unread-article ()
6360   "Select unread article before current one."
6361   (interactive)
6362   (gnus-summary-prev-article
6363    (or (not (eq gnus-summary-goto-unread 'never))
6364        (gnus-summary-first-article-p (gnus-summary-article-number)))
6365    (and gnus-auto-select-same
6366         (gnus-summary-article-subject))))
6367
6368 (defun gnus-summary-next-page (&optional lines circular)
6369   "Show next page of the selected article.
6370 If at the end of the current article, select the next article.
6371 LINES says how many lines should be scrolled up.
6372
6373 If CIRCULAR is non-nil, go to the start of the article instead of
6374 selecting the next article when reaching the end of the current
6375 article."
6376   (interactive "P")
6377   (setq gnus-summary-buffer (current-buffer))
6378   (gnus-set-global-variables)
6379   (let ((article (gnus-summary-article-number))
6380         (article-window (get-buffer-window gnus-article-buffer t))
6381         endp)
6382     ;; If the buffer is empty, we have no article.
6383     (unless article
6384       (error "No article to select"))
6385     (gnus-configure-windows 'article)
6386     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6387         (if (and (eq gnus-summary-goto-unread 'never)
6388                  (not (gnus-summary-last-article-p article)))
6389             (gnus-summary-next-article)
6390           (gnus-summary-next-unread-article))
6391       (if (or (null gnus-current-article)
6392               (null gnus-article-current)
6393               (/= article (cdr gnus-article-current))
6394               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6395           ;; Selected subject is different from current article's.
6396           (gnus-summary-display-article article)
6397         (when article-window
6398           (gnus-eval-in-buffer-window gnus-article-buffer
6399             (setq endp (gnus-article-next-page lines)))
6400           (when endp
6401             (cond (circular
6402                    (gnus-summary-beginning-of-article))
6403                   (lines
6404                    (gnus-message 3 "End of message"))
6405                   ((null lines)
6406                    (if (and (eq gnus-summary-goto-unread 'never)
6407                             (not (gnus-summary-last-article-p article)))
6408                        (gnus-summary-next-article)
6409                      (gnus-summary-next-unread-article))))))))
6410     (gnus-summary-recenter)
6411     (gnus-summary-position-point)))
6412
6413 (defun gnus-summary-prev-page (&optional lines move)
6414   "Show previous page of selected article.
6415 Argument LINES specifies lines to be scrolled down.
6416 If MOVE, move to the previous unread article if point is at
6417 the beginning of the buffer."
6418   (interactive "P")
6419   (let ((article (gnus-summary-article-number))
6420         (article-window (get-buffer-window gnus-article-buffer t))
6421         endp)
6422     (gnus-configure-windows 'article)
6423     (if (or (null gnus-current-article)
6424             (null gnus-article-current)
6425             (/= article (cdr gnus-article-current))
6426             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6427         ;; Selected subject is different from current article's.
6428         (gnus-summary-display-article article)
6429       (gnus-summary-recenter)
6430       (when article-window
6431         (gnus-eval-in-buffer-window gnus-article-buffer
6432           (setq endp (gnus-article-prev-page lines)))
6433         (when (and move endp)
6434           (cond (lines
6435                  (gnus-message 3 "Beginning of message"))
6436                 ((null lines)
6437                  (if (and (eq gnus-summary-goto-unread 'never)
6438                           (not (gnus-summary-first-article-p article)))
6439                      (gnus-summary-prev-article)
6440                    (gnus-summary-prev-unread-article))))))))
6441   (gnus-summary-position-point))
6442
6443 (defun gnus-summary-prev-page-or-article (&optional lines)
6444   "Show previous page of selected article.
6445 Argument LINES specifies lines to be scrolled down.
6446 If at the beginning of the article, go to the next article."
6447   (interactive "P")
6448   (gnus-summary-prev-page lines t))
6449
6450 (defun gnus-summary-scroll-up (lines)
6451   "Scroll up (or down) one line current article.
6452 Argument LINES specifies lines to be scrolled up (or down if negative)."
6453   (interactive "p")
6454   (gnus-configure-windows 'article)
6455   (gnus-summary-show-thread)
6456   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6457     (gnus-eval-in-buffer-window gnus-article-buffer
6458       (cond ((> lines 0)
6459              (when (gnus-article-next-page lines)
6460                (gnus-message 3 "End of message")))
6461             ((< lines 0)
6462              (gnus-article-prev-page (- lines))))))
6463   (gnus-summary-recenter)
6464   (gnus-summary-position-point))
6465
6466 (defun gnus-summary-scroll-down (lines)
6467   "Scroll down (or up) one line current article.
6468 Argument LINES specifies lines to be scrolled down (or up if negative)."
6469   (interactive "p")
6470   (gnus-summary-scroll-up (- lines)))
6471
6472 (defun gnus-summary-next-same-subject ()
6473   "Select next article which has the same subject as current one."
6474   (interactive)
6475   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6476
6477 (defun gnus-summary-prev-same-subject ()
6478   "Select previous article which has the same subject as current one."
6479   (interactive)
6480   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6481
6482 (defun gnus-summary-next-unread-same-subject ()
6483   "Select next unread article which has the same subject as current one."
6484   (interactive)
6485   (gnus-summary-next-article t (gnus-summary-article-subject)))
6486
6487 (defun gnus-summary-prev-unread-same-subject ()
6488   "Select previous unread article which has the same subject as current one."
6489   (interactive)
6490   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6491
6492 (defun gnus-summary-first-unread-article ()
6493   "Select the first unread article.
6494 Return nil if there are no unread articles."
6495   (interactive)
6496   (prog1
6497       (when (gnus-summary-first-subject t)
6498         (gnus-summary-show-thread)
6499         (gnus-summary-first-subject t)
6500         (gnus-summary-display-article (gnus-summary-article-number)))
6501     (gnus-summary-position-point)))
6502
6503 (defun gnus-summary-first-unread-subject ()
6504   "Place the point on the subject line of the first unread article.
6505 Return nil if there are no unread articles."
6506   (interactive)
6507   (prog1
6508       (when (gnus-summary-first-subject t)
6509         (gnus-summary-show-thread)
6510         (gnus-summary-first-subject t))
6511     (gnus-summary-position-point)))
6512
6513 (defun gnus-summary-first-article ()
6514   "Select the first article.
6515 Return nil if there are no articles."
6516   (interactive)
6517   (prog1
6518       (when (gnus-summary-first-subject)
6519         (gnus-summary-show-thread)
6520         (gnus-summary-first-subject)
6521         (gnus-summary-display-article (gnus-summary-article-number)))
6522     (gnus-summary-position-point)))
6523
6524 (defun gnus-summary-best-unread-article ()
6525   "Select the unread article with the highest score."
6526   (interactive)
6527   (let ((best -1000000)
6528         (data gnus-newsgroup-data)
6529         article score)
6530     (while data
6531       (and (gnus-data-unread-p (car data))
6532            (> (setq score
6533                     (gnus-summary-article-score (gnus-data-number (car data))))
6534               best)
6535            (setq best score
6536                  article (gnus-data-number (car data))))
6537       (setq data (cdr data)))
6538     (prog1
6539         (if article
6540             (gnus-summary-goto-article article)
6541           (error "No unread articles"))
6542       (gnus-summary-position-point))))
6543
6544 (defun gnus-summary-last-subject ()
6545   "Go to the last displayed subject line in the group."
6546   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6547     (when article
6548       (gnus-summary-goto-subject article))))
6549
6550 (defun gnus-summary-goto-article (article &optional all-headers force)
6551   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6552 If ALL-HEADERS is non-nil, no header lines are hidden.
6553 If FORCE, go to the article even if it isn't displayed.  If FORCE
6554 is a number, it is the line the article is to be displayed on."
6555   (interactive
6556    (list
6557     (completing-read
6558      "Article number or Message-ID: "
6559      (mapcar (lambda (number) (list (int-to-string number)))
6560              gnus-newsgroup-limit))
6561     current-prefix-arg
6562     t))
6563   (prog1
6564       (if (and (stringp article)
6565                (string-match "@" article))
6566           (gnus-summary-refer-article article)
6567         (when (stringp article)
6568           (setq article (string-to-number article)))
6569         (if (gnus-summary-goto-subject article force)
6570             (gnus-summary-display-article article all-headers)
6571           (gnus-message 4 "Couldn't go to article %s" article) nil))
6572     (gnus-summary-position-point)))
6573
6574 (defun gnus-summary-goto-last-article ()
6575   "Go to the previously read article."
6576   (interactive)
6577   (prog1
6578       (when gnus-last-article
6579         (gnus-summary-goto-article gnus-last-article nil t))
6580     (gnus-summary-position-point)))
6581
6582 (defun gnus-summary-pop-article (number)
6583   "Pop one article off the history and go to the previous.
6584 NUMBER articles will be popped off."
6585   (interactive "p")
6586   (let (to)
6587     (setq gnus-newsgroup-history
6588           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6589     (if to
6590         (gnus-summary-goto-article (car to) nil t)
6591       (error "Article history empty")))
6592   (gnus-summary-position-point))
6593
6594 ;; Summary commands and functions for limiting the summary buffer.
6595
6596 (defun gnus-summary-limit-to-articles (n)
6597   "Limit the summary buffer to the next N articles.
6598 If not given a prefix, use the process marked articles instead."
6599   (interactive "P")
6600   (prog1
6601       (let ((articles (gnus-summary-work-articles n)))
6602         (setq gnus-newsgroup-processable nil)
6603         (gnus-summary-limit articles))
6604     (gnus-summary-position-point)))
6605
6606 (defun gnus-summary-pop-limit (&optional total)
6607   "Restore the previous limit.
6608 If given a prefix, remove all limits."
6609   (interactive "P")
6610   (when total
6611     (setq gnus-newsgroup-limits
6612           (list (mapcar (lambda (h) (mail-header-number h))
6613                         gnus-newsgroup-headers))))
6614   (unless gnus-newsgroup-limits
6615     (error "No limit to pop"))
6616   (prog1
6617       (gnus-summary-limit nil 'pop)
6618     (gnus-summary-position-point)))
6619
6620 (defun gnus-summary-limit-to-subject (subject &optional header)
6621   "Limit the summary buffer to articles that have subjects that match a regexp."
6622   (interactive "sLimit to subject (regexp): ")
6623   (unless header
6624     (setq header "subject"))
6625   (when (not (equal "" subject))
6626     (prog1
6627         (let ((articles (gnus-summary-find-matching
6628                          (or header "subject") subject 'all)))
6629           (unless articles
6630             (error "Found no matches for \"%s\"" subject))
6631           (gnus-summary-limit articles))
6632       (gnus-summary-position-point))))
6633
6634 (defun gnus-summary-limit-to-author (from)
6635   "Limit the summary buffer to articles that have authors that match a regexp."
6636   (interactive "sLimit to author (regexp): ")
6637   (gnus-summary-limit-to-subject from "from"))
6638
6639 (defun gnus-summary-limit-to-age (age &optional younger-p)
6640   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6641 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6642 articles that are younger than AGE days."
6643   (interactive
6644    (let ((younger current-prefix-arg)
6645          (days-got nil)
6646          days)
6647      (while (not days-got)
6648        (setq days (if younger
6649                       (read-string "Limit to articles within (in days): ")
6650                     (read-string "Limit to articles older than (in days): ")))
6651        (when (> (length days) 0)
6652          (setq days (read days)))
6653        (if (numberp days)
6654            (setq days-got t)
6655          (message "Please enter a number.")
6656          (sleep-for 1)))
6657      (list days younger)))
6658   (prog1
6659       (let ((data gnus-newsgroup-data)
6660             (cutoff (days-to-time age))
6661             articles d date is-younger)
6662         (while (setq d (pop data))
6663           (when (and (vectorp (gnus-data-header d))
6664                      (setq date (mail-header-date (gnus-data-header d))))
6665             (setq is-younger (time-less-p
6666                               (time-since (condition-case ()
6667                                               (date-to-time date)
6668                                             (error '(0 0))))
6669                               cutoff))
6670             (when (if younger-p
6671                       is-younger
6672                     (not is-younger))
6673               (push (gnus-data-number d) articles))))
6674         (gnus-summary-limit (nreverse articles)))
6675     (gnus-summary-position-point)))
6676
6677 (defun gnus-summary-limit-to-extra (header regexp)
6678   "Limit the summary buffer to articles that match an 'extra' header."
6679   (interactive
6680    (let ((header
6681           (intern
6682            (gnus-completing-read
6683             (symbol-name (car gnus-extra-headers))
6684             "Limit extra header:"
6685             (mapcar (lambda (x)
6686                       (cons (symbol-name x) x))
6687                     gnus-extra-headers)
6688             nil
6689             t))))
6690      (list header
6691            (read-string (format "Limit to header %s (regexp): " header)))))
6692   (when (not (equal "" regexp))
6693     (prog1
6694         (let ((articles (gnus-summary-find-matching
6695                          (cons 'extra header) regexp 'all)))
6696           (unless articles
6697             (error "Found no matches for \"%s\"" regexp))
6698           (gnus-summary-limit articles))
6699       (gnus-summary-position-point))))
6700
6701 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6702 (make-obsolete
6703  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6704
6705 (defun gnus-summary-limit-to-unread (&optional all)
6706   "Limit the summary buffer to articles that are not marked as read.
6707 If ALL is non-nil, limit strictly to unread articles."
6708   (interactive "P")
6709   (if all
6710       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6711     (gnus-summary-limit-to-marks
6712      ;; Concat all the marks that say that an article is read and have
6713      ;; those removed.
6714      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6715            gnus-killed-mark gnus-kill-file-mark
6716            gnus-low-score-mark gnus-expirable-mark
6717            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6718            gnus-duplicate-mark gnus-souped-mark)
6719      'reverse)))
6720
6721 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6722 (make-obsolete 'gnus-summary-delete-marked-with
6723                'gnus-summary-limit-exlude-marks)
6724
6725 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6726   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6727 If REVERSE, limit the summary buffer to articles that are marked
6728 with MARKS.  MARKS can either be a string of marks or a list of marks.
6729 Returns how many articles were removed."
6730   (interactive "sMarks: ")
6731   (gnus-summary-limit-to-marks marks t))
6732
6733 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6734   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6735 If REVERSE (the prefix), limit the summary buffer to articles that are
6736 not marked with MARKS.  MARKS can either be a string of marks or a
6737 list of marks.
6738 Returns how many articles were removed."
6739   (interactive "sMarks: \nP")
6740   (prog1
6741       (let ((data gnus-newsgroup-data)
6742             (marks (if (listp marks) marks
6743                      (append marks nil))) ; Transform to list.
6744             articles)
6745         (while data
6746           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6747                   (memq (gnus-data-mark (car data)) marks))
6748             (push (gnus-data-number (car data)) articles))
6749           (setq data (cdr data)))
6750         (gnus-summary-limit articles))
6751     (gnus-summary-position-point)))
6752
6753 (defun gnus-summary-limit-to-score (&optional score)
6754   "Limit to articles with score at or above SCORE."
6755   (interactive "P")
6756   (setq score (if score
6757                   (prefix-numeric-value score)
6758                 (or gnus-summary-default-score 0)))
6759   (let ((data gnus-newsgroup-data)
6760         articles)
6761     (while data
6762       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6763                 score)
6764         (push (gnus-data-number (car data)) articles))
6765       (setq data (cdr data)))
6766     (prog1
6767         (gnus-summary-limit articles)
6768       (gnus-summary-position-point))))
6769
6770 (defun gnus-summary-limit-include-thread (id)
6771   "Display all the hidden articles that in the current thread."
6772   (interactive (list (mail-header-id (gnus-summary-article-header))))
6773   (let ((articles (gnus-articles-in-thread
6774                    (gnus-id-to-thread (gnus-root-id id)))))
6775     (prog1
6776         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6777       (gnus-summary-position-point))))
6778
6779 (defun gnus-summary-limit-include-dormant ()
6780   "Display all the hidden articles that are marked as dormant.
6781 Note that this command only works on a subset of the articles currently
6782 fetched for this group."
6783   (interactive)
6784   (unless gnus-newsgroup-dormant
6785     (error "There are no dormant articles in this group"))
6786   (prog1
6787       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6788     (gnus-summary-position-point)))
6789
6790 (defun gnus-summary-limit-exclude-dormant ()
6791   "Hide all dormant articles."
6792   (interactive)
6793   (prog1
6794       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6795     (gnus-summary-position-point)))
6796
6797 (defun gnus-summary-limit-exclude-childless-dormant ()
6798   "Hide all dormant articles that have no children."
6799   (interactive)
6800   (let ((data (gnus-data-list t))
6801         articles d children)
6802     ;; Find all articles that are either not dormant or have
6803     ;; children.
6804     (while (setq d (pop data))
6805       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6806                 (and (setq children
6807                            (gnus-article-children (gnus-data-number d)))
6808                      (let (found)
6809                        (while children
6810                          (when (memq (car children) articles)
6811                            (setq children nil
6812                                  found t))
6813                          (pop children))
6814                        found)))
6815         (push (gnus-data-number d) articles)))
6816     ;; Do the limiting.
6817     (prog1
6818         (gnus-summary-limit articles)
6819       (gnus-summary-position-point))))
6820
6821 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6822   "Mark all unread excluded articles as read.
6823 If ALL, mark even excluded ticked and dormants as read."
6824   (interactive "P")
6825   (let ((articles (gnus-sorted-complement
6826                    (sort
6827                     (mapcar (lambda (h) (mail-header-number h))
6828                             gnus-newsgroup-headers)
6829                     '<)
6830                    (sort gnus-newsgroup-limit '<)))
6831         article)
6832     (setq gnus-newsgroup-unreads
6833           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6834     (if all
6835         (setq gnus-newsgroup-dormant nil
6836               gnus-newsgroup-marked nil
6837               gnus-newsgroup-reads
6838               (nconc
6839                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6840                gnus-newsgroup-reads))
6841       (while (setq article (pop articles))
6842         (unless (or (memq article gnus-newsgroup-dormant)
6843                     (memq article gnus-newsgroup-marked))
6844           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6845
6846 (defun gnus-summary-limit (articles &optional pop)
6847   (if pop
6848       ;; We pop the previous limit off the stack and use that.
6849       (setq articles (car gnus-newsgroup-limits)
6850             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6851     ;; We use the new limit, so we push the old limit on the stack.
6852     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6853   ;; Set the limit.
6854   (setq gnus-newsgroup-limit articles)
6855   (let ((total (length gnus-newsgroup-data))
6856         (data (gnus-data-find-list (gnus-summary-article-number)))
6857         (gnus-summary-mark-below nil)   ; Inhibit this.
6858         found)
6859     ;; This will do all the work of generating the new summary buffer
6860     ;; according to the new limit.
6861     (gnus-summary-prepare)
6862     ;; Hide any threads, possibly.
6863     (and gnus-show-threads
6864          gnus-thread-hide-subtree
6865          (gnus-summary-hide-all-threads))
6866     ;; Try to return to the article you were at, or one in the
6867     ;; neighborhood.
6868     (when data
6869       ;; We try to find some article after the current one.
6870       (while data
6871         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6872           (setq data nil
6873                 found t))
6874         (setq data (cdr data))))
6875     (unless found
6876       ;; If there is no data, that means that we were after the last
6877       ;; article.  The same goes when we can't find any articles
6878       ;; after the current one.
6879       (goto-char (point-max))
6880       (gnus-summary-find-prev))
6881     (gnus-set-mode-line 'summary)
6882     ;; We return how many articles were removed from the summary
6883     ;; buffer as a result of the new limit.
6884     (- total (length gnus-newsgroup-data))))
6885
6886 (defsubst gnus-invisible-cut-children (threads)
6887   (let ((num 0))
6888     (while threads
6889       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6890         (incf num))
6891       (pop threads))
6892     (< num 2)))
6893
6894 (defsubst gnus-cut-thread (thread)
6895   "Go forwards in the thread until we find an article that we want to display."
6896   (when (or (eq gnus-fetch-old-headers 'some)
6897             (eq gnus-fetch-old-headers 'invisible)
6898             (numberp gnus-fetch-old-headers)
6899             (eq gnus-build-sparse-threads 'some)
6900             (eq gnus-build-sparse-threads 'more))
6901     ;; Deal with old-fetched headers and sparse threads.
6902     (while (and
6903             thread
6904             (or
6905              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6906              (gnus-summary-article-ancient-p
6907               (mail-header-number (car thread))))
6908             (if (or (<= (length (cdr thread)) 1)
6909                     (eq gnus-fetch-old-headers 'invisible))
6910                 (setq gnus-newsgroup-limit
6911                       (delq (mail-header-number (car thread))
6912                             gnus-newsgroup-limit)
6913                       thread (cadr thread))
6914               (when (gnus-invisible-cut-children (cdr thread))
6915                 (let ((th (cdr thread)))
6916                   (while th
6917                     (if (memq (mail-header-number (caar th))
6918                               gnus-newsgroup-limit)
6919                         (setq thread (car th)
6920                               th nil)
6921                       (setq th (cdr th))))))))))
6922   thread)
6923
6924 (defun gnus-cut-threads (threads)
6925   "Cut off all uninteresting articles from the beginning of threads."
6926   (when (or (eq gnus-fetch-old-headers 'some)
6927             (eq gnus-fetch-old-headers 'invisible)
6928             (numberp gnus-fetch-old-headers)
6929             (eq gnus-build-sparse-threads 'some)
6930             (eq gnus-build-sparse-threads 'more))
6931     (let ((th threads))
6932       (while th
6933         (setcar th (gnus-cut-thread (car th)))
6934         (setq th (cdr th)))))
6935   ;; Remove nixed out threads.
6936   (delq nil threads))
6937
6938 (defun gnus-summary-initial-limit (&optional show-if-empty)
6939   "Figure out what the initial limit is supposed to be on group entry.
6940 This entails weeding out unwanted dormants, low-scored articles,
6941 fetch-old-headers verbiage, and so on."
6942   ;; Most groups have nothing to remove.
6943   (if (or gnus-inhibit-limiting
6944           (and (null gnus-newsgroup-dormant)
6945                (not (eq gnus-fetch-old-headers 'some))
6946                (not (numberp gnus-fetch-old-headers))
6947                (not (eq gnus-fetch-old-headers 'invisible))
6948                (null gnus-summary-expunge-below)
6949                (not (eq gnus-build-sparse-threads 'some))
6950                (not (eq gnus-build-sparse-threads 'more))
6951                (null gnus-thread-expunge-below)
6952                (not gnus-use-nocem)))
6953       ()                                ; Do nothing.
6954     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6955     (setq gnus-newsgroup-limit nil)
6956     (mapatoms
6957      (lambda (node)
6958        (unless (car (symbol-value node))
6959          ;; These threads have no parents -- they are roots.
6960          (let ((nodes (cdr (symbol-value node)))
6961                thread)
6962            (while nodes
6963              (if (and gnus-thread-expunge-below
6964                       (< (gnus-thread-total-score (car nodes))
6965                          gnus-thread-expunge-below))
6966                  (gnus-expunge-thread (pop nodes))
6967                (setq thread (pop nodes))
6968                (gnus-summary-limit-children thread))))))
6969      gnus-newsgroup-dependencies)
6970     ;; If this limitation resulted in an empty group, we might
6971     ;; pop the previous limit and use it instead.
6972     (when (and (not gnus-newsgroup-limit)
6973                show-if-empty)
6974       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6975     gnus-newsgroup-limit))
6976
6977 (defun gnus-summary-limit-children (thread)
6978   "Return 1 if this subthread is visible and 0 if it is not."
6979   ;; First we get the number of visible children to this thread.  This
6980   ;; is done by recursing down the thread using this function, so this
6981   ;; will really go down to a leaf article first, before slowly
6982   ;; working its way up towards the root.
6983   (when thread
6984     (let ((children
6985            (if (cdr thread)
6986                (apply '+ (mapcar 'gnus-summary-limit-children
6987                                  (cdr thread)))
6988              0))
6989           (number (mail-header-number (car thread)))
6990           score)
6991       (if (and
6992            (not (memq number gnus-newsgroup-marked))
6993            (or
6994             ;; If this article is dormant and has absolutely no visible
6995             ;; children, then this article isn't visible.
6996             (and (memq number gnus-newsgroup-dormant)
6997                  (zerop children))
6998             ;; If this is "fetch-old-headered" and there is no
6999             ;; visible children, then we don't want this article.
7000             (and (or (eq gnus-fetch-old-headers 'some)
7001                      (numberp gnus-fetch-old-headers))
7002                  (gnus-summary-article-ancient-p number)
7003                  (zerop children))
7004             ;; If this is "fetch-old-headered" and `invisible', then
7005             ;; we don't want this article.
7006             (and (eq gnus-fetch-old-headers 'invisible)
7007                  (gnus-summary-article-ancient-p number))
7008             ;; If this is a sparsely inserted article with no children,
7009             ;; we don't want it.
7010             (and (eq gnus-build-sparse-threads 'some)
7011                  (gnus-summary-article-sparse-p number)
7012                  (zerop children))
7013             ;; If we use expunging, and this article is really
7014             ;; low-scored, then we don't want this article.
7015             (when (and gnus-summary-expunge-below
7016                        (< (setq score
7017                                 (or (cdr (assq number gnus-newsgroup-scored))
7018                                     gnus-summary-default-score))
7019                           gnus-summary-expunge-below))
7020               ;; We increase the expunge-tally here, but that has
7021               ;; nothing to do with the limits, really.
7022               (incf gnus-newsgroup-expunged-tally)
7023               ;; We also mark as read here, if that's wanted.
7024               (when (and gnus-summary-mark-below
7025                          (< score gnus-summary-mark-below))
7026                 (setq gnus-newsgroup-unreads
7027                       (delq number gnus-newsgroup-unreads))
7028                 (if gnus-newsgroup-auto-expire
7029                     (push number gnus-newsgroup-expirable)
7030                   (push (cons number gnus-low-score-mark)
7031                         gnus-newsgroup-reads)))
7032               t)
7033             ;; Check NoCeM things.
7034             (if (and gnus-use-nocem
7035                      (gnus-nocem-unwanted-article-p
7036                       (mail-header-id (car thread))))
7037                 (progn
7038                   (setq gnus-newsgroup-unreads
7039                         (delq number gnus-newsgroup-unreads))
7040                   t))))
7041           ;; Nope, invisible article.
7042           0
7043         ;; Ok, this article is to be visible, so we add it to the limit
7044         ;; and return 1.
7045         (push number gnus-newsgroup-limit)
7046         1))))
7047
7048 (defun gnus-expunge-thread (thread)
7049   "Mark all articles in THREAD as read."
7050   (let* ((number (mail-header-number (car thread))))
7051     (incf gnus-newsgroup-expunged-tally)
7052     ;; We also mark as read here, if that's wanted.
7053     (setq gnus-newsgroup-unreads
7054           (delq number gnus-newsgroup-unreads))
7055     (if gnus-newsgroup-auto-expire
7056         (push number gnus-newsgroup-expirable)
7057       (push (cons number gnus-low-score-mark)
7058             gnus-newsgroup-reads)))
7059   ;; Go recursively through all subthreads.
7060   (mapcar 'gnus-expunge-thread (cdr thread)))
7061
7062 ;; Summary article oriented commands
7063
7064 (defun gnus-summary-refer-parent-article (n)
7065   "Refer parent article N times.
7066 If N is negative, go to ancestor -N instead.
7067 The difference between N and the number of articles fetched is returned."
7068   (interactive "p")
7069   (let ((skip 1)
7070         error header ref)
7071     (when (not (natnump n))
7072       (setq skip (abs n)
7073             n 1))
7074     (while (and (> n 0)
7075                 (not error))
7076       (setq header (gnus-summary-article-header))
7077       (if (and (eq (mail-header-number header)
7078                    (cdr gnus-article-current))
7079                (equal gnus-newsgroup-name
7080                       (car gnus-article-current)))
7081           ;; If we try to find the parent of the currently
7082           ;; displayed article, then we take a look at the actual
7083           ;; References header, since this is slightly more
7084           ;; reliable than the References field we got from the
7085           ;; server.
7086           (save-excursion
7087             (set-buffer gnus-original-article-buffer)
7088             (nnheader-narrow-to-headers)
7089             (unless (setq ref (message-fetch-field "references"))
7090               (setq ref (message-fetch-field "in-reply-to")))
7091             (widen))
7092         (setq ref
7093               ;; It's not the current article, so we take a bet on
7094               ;; the value we got from the server.
7095               (mail-header-references header)))
7096       (if (and ref
7097                (not (equal ref "")))
7098           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7099             (gnus-message 1 "Couldn't find parent"))
7100         (gnus-message 1 "No references in article %d"
7101                       (gnus-summary-article-number))
7102         (setq error t))
7103       (decf n))
7104     (gnus-summary-position-point)
7105     n))
7106
7107 (defun gnus-summary-refer-references ()
7108   "Fetch all articles mentioned in the References header.
7109 Return the number of articles fetched."
7110   (interactive)
7111   (let ((ref (mail-header-references (gnus-summary-article-header)))
7112         (current (gnus-summary-article-number))
7113         (n 0))
7114     (if (or (not ref)
7115             (equal ref ""))
7116         (error "No References in the current article")
7117       ;; For each Message-ID in the References header...
7118       (while (string-match "<[^>]*>" ref)
7119         (incf n)
7120         ;; ... fetch that article.
7121         (gnus-summary-refer-article
7122          (prog1 (match-string 0 ref)
7123            (setq ref (substring ref (match-end 0))))))
7124       (gnus-summary-goto-subject current)
7125       (gnus-summary-position-point)
7126       n)))
7127
7128 (defun gnus-summary-refer-thread (&optional limit)
7129   "Fetch all articles in the current thread.
7130 If LIMIT (the numerical prefix), fetch that many old headers instead
7131 of what's specified by the `gnus-refer-thread-limit' variable."
7132   (interactive "P")
7133   (let ((id (mail-header-id (gnus-summary-article-header)))
7134         (limit (if limit (prefix-numeric-value limit)
7135                  gnus-refer-thread-limit)))
7136     ;; We want to fetch LIMIT *old* headers, but we also have to
7137     ;; re-fetch all the headers in the current buffer, because many of
7138     ;; them may be undisplayed.  So we adjust LIMIT.
7139     (when (numberp limit)
7140       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7141     (unless (eq gnus-fetch-old-headers 'invisible)
7142       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7143       ;; Retrieve the headers and read them in.
7144       (if (eq (gnus-retrieve-headers
7145                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7146               'nov)
7147           (gnus-build-all-threads)
7148         (error "Can't fetch thread from backends that don't support NOV"))
7149       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7150     (gnus-summary-limit-include-thread id)))
7151
7152 (defun gnus-summary-refer-article (message-id)
7153   "Fetch an article specified by MESSAGE-ID."
7154   (interactive "sMessage-ID: ")
7155   (when (and (stringp message-id)
7156              (not (zerop (length message-id))))
7157     ;; Construct the correct Message-ID if necessary.
7158     ;; Suggested by tale@pawl.rpi.edu.
7159     (unless (string-match "^<" message-id)
7160       (setq message-id (concat "<" message-id)))
7161     (unless (string-match ">$" message-id)
7162       (setq message-id (concat message-id ">")))
7163     (let* ((header (gnus-id-to-header message-id))
7164            (sparse (and header
7165                         (gnus-summary-article-sparse-p
7166                          (mail-header-number header))
7167                         (memq (mail-header-number header)
7168                               gnus-newsgroup-limit)))
7169            number)
7170       (cond
7171        ;; If the article is present in the buffer we just go to it.
7172        ((and header
7173              (or (not (gnus-summary-article-sparse-p
7174                        (mail-header-number header)))
7175                  sparse))
7176         (prog1
7177             (gnus-summary-goto-article
7178              (mail-header-number header) nil t)
7179           (when sparse
7180             (gnus-summary-update-article (mail-header-number header)))))
7181        (t
7182         ;; We fetch the article.
7183         (catch 'found
7184           (dolist (gnus-override-method (gnus-refer-article-methods))
7185             (gnus-check-server gnus-override-method)
7186             ;; Fetch the header, and display the article.
7187             (when (setq number (gnus-summary-insert-subject message-id))
7188               (gnus-summary-select-article nil nil nil number)
7189               (throw 'found t)))
7190           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7191
7192 (defun gnus-refer-article-methods ()
7193   "Return a list of referrable methods."
7194   (cond
7195    ;; No method, so we default to current and native.
7196    ((null gnus-refer-article-method)
7197     (list gnus-current-select-method gnus-select-method))
7198    ;; Current.
7199    ((eq 'current gnus-refer-article-method)
7200     (list gnus-current-select-method))
7201    ;; List of select methods.
7202    ((not (and (symbolp (car gnus-refer-article-method))
7203               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7204     (let (out)
7205       (dolist (method gnus-refer-article-method)
7206         (push (if (eq 'current method)
7207                   gnus-current-select-method
7208                 method)
7209               out))
7210       (nreverse out)))
7211    ;; One single select method.
7212    (t
7213     (list gnus-refer-article-method))))
7214
7215 (defun gnus-summary-edit-parameters ()
7216   "Edit the group parameters of the current group."
7217   (interactive)
7218   (gnus-group-edit-group gnus-newsgroup-name 'params))
7219
7220 (defun gnus-summary-customize-parameters ()
7221   "Customize the group parameters of the current group."
7222   (interactive)
7223   (gnus-group-customize gnus-newsgroup-name))
7224
7225 (defun gnus-summary-enter-digest-group (&optional force)
7226   "Enter an nndoc group based on the current article.
7227 If FORCE, force a digest interpretation.  If not, try
7228 to guess what the document format is."
7229   (interactive "P")
7230   (let ((conf gnus-current-window-configuration))
7231     (save-excursion
7232       (gnus-summary-select-article))
7233     (setq gnus-current-window-configuration conf)
7234     (let* ((name (format "%s-%d"
7235                          (gnus-group-prefixed-name
7236                           gnus-newsgroup-name (list 'nndoc ""))
7237                          (save-excursion
7238                            (set-buffer gnus-summary-buffer)
7239                            gnus-current-article)))
7240            (ogroup gnus-newsgroup-name)
7241            (params (append (gnus-info-params (gnus-get-info ogroup))
7242                            (list (cons 'to-group ogroup))
7243                            (list (cons 'save-article-group ogroup))))
7244            (case-fold-search t)
7245            (buf (current-buffer))
7246            dig to-address)
7247       (save-excursion
7248         (set-buffer gnus-original-article-buffer)
7249         ;; Have the digest group inherit the main mail address of
7250         ;; the parent article.
7251         (when (setq to-address (or (message-fetch-field "reply-to")
7252                                    (message-fetch-field "from")))
7253           (setq params (append
7254                         (list (cons 'to-address
7255                                     (funcall gnus-decode-encoded-word-function
7256                                              to-address))))))
7257         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7258         (insert-buffer-substring gnus-original-article-buffer)
7259         ;; Remove lines that may lead nndoc to misinterpret the
7260         ;; document type.
7261         (narrow-to-region
7262          (goto-char (point-min))
7263          (or (search-forward "\n\n" nil t) (point)))
7264         (goto-char (point-min))
7265         (delete-matching-lines "^Path:\\|^From ")
7266         (widen))
7267       (unwind-protect
7268           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7269                     (gnus-newsgroup-ephemeral-ignored-charsets
7270                      gnus-newsgroup-ignored-charsets))
7271                 (gnus-group-read-ephemeral-group
7272                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7273                               (nndoc-article-type
7274                                ,(if force 'mbox 'guess))) t))
7275               ;; Make all postings to this group go to the parent group.
7276               (nconc (gnus-info-params (gnus-get-info name))
7277                      params)
7278             ;; Couldn't select this doc group.
7279             (switch-to-buffer buf)
7280             (gnus-set-global-variables)
7281             (gnus-configure-windows 'summary)
7282             (gnus-message 3 "Article couldn't be entered?"))
7283         (kill-buffer dig)))))
7284
7285 (defun gnus-summary-read-document (n)
7286   "Open a new group based on the current article(s).
7287 This will allow you to read digests and other similar
7288 documents as newsgroups.
7289 Obeys the standard process/prefix convention."
7290   (interactive "P")
7291   (let* ((articles (gnus-summary-work-articles n))
7292          (ogroup gnus-newsgroup-name)
7293          (params (append (gnus-info-params (gnus-get-info ogroup))
7294                          (list (cons 'to-group ogroup))))
7295          article group egroup groups vgroup)
7296     (while (setq article (pop articles))
7297       (setq group (format "%s-%d" gnus-newsgroup-name article))
7298       (gnus-summary-remove-process-mark article)
7299       (when (gnus-summary-display-article article)
7300         (save-excursion
7301           (with-temp-buffer
7302             (insert-buffer-substring gnus-original-article-buffer)
7303             ;; Remove some headers that may lead nndoc to make
7304             ;; the wrong guess.
7305             (message-narrow-to-head)
7306             (goto-char (point-min))
7307             (delete-matching-lines "^\\(Path\\):\\|^From ")
7308             (widen)
7309             (if (setq egroup
7310                       (gnus-group-read-ephemeral-group
7311                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7312                                      (nndoc-article-type guess))
7313                        t nil t))
7314                 (progn
7315                   ;; Make all postings to this group go to the parent group.
7316                   (nconc (gnus-info-params (gnus-get-info egroup))
7317                          params)
7318                   (push egroup groups))
7319               ;; Couldn't select this doc group.
7320               (gnus-error 3 "Article couldn't be entered"))))))
7321     ;; Now we have selected all the documents.
7322     (cond
7323      ((not groups)
7324       (error "None of the articles could be interpreted as documents"))
7325      ((gnus-group-read-ephemeral-group
7326        (setq vgroup (format
7327                      "nnvirtual:%s-%s" gnus-newsgroup-name
7328                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7329        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7330        t
7331        (cons (current-buffer) 'summary)))
7332      (t
7333       (error "Couldn't select virtual nndoc group")))))
7334
7335 (defun gnus-summary-isearch-article (&optional regexp-p)
7336   "Do incremental search forward on the current article.
7337 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7338   (interactive "P")
7339   (let* ((gnus-inhibit-treatment t)
7340          (old (gnus-summary-select-article)))
7341     (gnus-configure-windows 'article)
7342     (gnus-eval-in-buffer-window gnus-article-buffer
7343       (save-restriction
7344         (widen)
7345         (when (eq 'old old)
7346           (gnus-article-show-all-headers))
7347         (goto-char (point-min))
7348         (isearch-forward regexp-p)))))
7349
7350 (defun gnus-summary-search-article-forward (regexp &optional backward)
7351   "Search for an article containing REGEXP forward.
7352 If BACKWARD, search backward instead."
7353   (interactive
7354    (list (read-string
7355           (format "Search article %s (regexp%s): "
7356                   (if current-prefix-arg "backward" "forward")
7357                   (if gnus-last-search-regexp
7358                       (concat ", default " gnus-last-search-regexp)
7359                     "")))
7360          current-prefix-arg))
7361   (if (string-equal regexp "")
7362       (setq regexp (or gnus-last-search-regexp ""))
7363     (setq gnus-last-search-regexp regexp)
7364     (setq gnus-article-before-search gnus-current-article))
7365   ;; Intentionally set gnus-last-article.
7366   (setq gnus-last-article gnus-article-before-search)
7367   (let ((gnus-last-article gnus-last-article))
7368     (if (gnus-summary-search-article regexp backward)
7369         (gnus-summary-show-thread)
7370       (error "Search failed: \"%s\"" regexp))))
7371
7372 (defun gnus-summary-search-article-backward (regexp)
7373   "Search for an article containing REGEXP backward."
7374   (interactive
7375    (list (read-string
7376           (format "Search article backward (regexp%s): "
7377                   (if gnus-last-search-regexp
7378                       (concat ", default " gnus-last-search-regexp)
7379                     "")))))
7380   (gnus-summary-search-article-forward regexp 'backward))
7381
7382 (eval-when-compile
7383   (defmacro gnus-summary-search-article-position-point (regexp backward)
7384     "Dehighlight the last matched text and goto the beginning position."
7385     (` (if (and gnus-summary-search-article-matched-data
7386                 (let ((text (caddr gnus-summary-search-article-matched-data))
7387                       (inhibit-read-only t)
7388                       buffer-read-only)
7389                   (delete-region
7390                    (goto-char (car gnus-summary-search-article-matched-data))
7391                    (cadr gnus-summary-search-article-matched-data))
7392                   (insert text)
7393                   (string-match (, regexp) text)))
7394            (if (, backward) (beginning-of-line) (end-of-line))
7395          (goto-char (if (, backward) (point-max) (point-min))))))
7396
7397   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7398     "Place point where X-Face image is displayed."
7399     (if (featurep 'xemacs)
7400         (` (let ((end (if (search-forward "\n\n" nil t)
7401                           (goto-char (1- (point)))
7402                         (point-min)))
7403                  extent)
7404              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7405              (unless (and (re-search-forward "^From:" end t)
7406                           (setq extent (extent-at (point)))
7407                           (extent-begin-glyph extent))
7408                (goto-char (, opoint)))))
7409       (` (let ((end (if (search-forward "\n\n" nil t)
7410                         (goto-char (1- (point)))
7411                       (point-min)))
7412                (start (or (search-backward "\n\n" nil t) (point-min))))
7413            (goto-char
7414             (or (text-property-any start end 'x-face-image t);; x-face-e21
7415                 (text-property-any start end 'x-face-mule-bitmap-image t)
7416                 (, opoint)))))))
7417
7418   (defmacro gnus-summary-search-article-highlight-matched-text
7419     (backward treated x-face)
7420     "Highlight matched text in the function `gnus-summary-search-article'."
7421     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7422              (end (set-marker (make-marker) (match-end 0)))
7423              (inhibit-read-only t)
7424              buffer-read-only)
7425          (unless treated
7426            (let ((,@
7427                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7428                     (mapcar
7429                      (lambda (item) (setq items (delq item items)))
7430                      '(gnus-treat-buttonize
7431                        gnus-treat-fill-article
7432                        gnus-treat-fill-long-lines
7433                        gnus-treat-emphasize
7434                        gnus-treat-highlight-headers
7435                        gnus-treat-highlight-citation
7436                        gnus-treat-highlight-signature
7437                        gnus-treat-overstrike
7438                        gnus-treat-display-xface
7439                        gnus-treat-buttonize-head
7440                        gnus-treat-decode-article-as-default-mime-charset))
7441                     (static-if (featurep 'xemacs)
7442                         items
7443                       (cons '(x-face-mule-delete-x-face-field
7444                               (quote never))
7445                             items))))
7446                  (gnus-treat-display-xface
7447                   (when (, x-face) gnus-treat-display-xface)))
7448              (gnus-article-prepare-mime-display)))
7449          (goto-char (if (, backward) start end))
7450          (when (, x-face)
7451            (gnus-summary-search-article-highlight-goto-x-face (point)))
7452          (setq gnus-summary-search-article-matched-data
7453                (list start end (buffer-substring start end)))
7454          (unless (eq start end);; matched text has been deleted. :-<
7455            (put-text-property start end 'face
7456                               (or (find-face 'isearch)
7457                                   'secondary-selection))))))
7458   )
7459
7460 (defun gnus-summary-search-article (regexp &optional backward)
7461   "Search for an article containing REGEXP.
7462 Optional argument BACKWARD means do search for backward.
7463 `gnus-select-article-hook' is not called during the search."
7464   ;; We have to require this here to make sure that the following
7465   ;; dynamic binding isn't shadowed by autoloading.
7466   (require 'gnus-async)
7467   (require 'gnus-art)
7468   (let ((gnus-select-article-hook nil)  ;Disable hook.
7469         (gnus-article-prepare-hook nil)
7470         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7471         (gnus-use-article-prefetch nil)
7472         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7473         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7474         (sum (current-buffer))
7475         (found nil)
7476         point treated)
7477     (gnus-save-hidden-threads
7478       (static-if (featurep 'xemacs)
7479           (let ((gnus-inhibit-treatment t))
7480             (setq treated (eq 'old (gnus-summary-select-article)))
7481             (when (and treated
7482                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7483                                  (window-live-p (get-buffer-window
7484                                                  gnus-article-buffer t)))))
7485               (gnus-summary-select-article nil t)
7486               (setq treated nil)))
7487         (let ((gnus-inhibit-treatment t)
7488               (x-face-mule-delete-x-face-field 'never))
7489           (setq treated (eq 'old (gnus-summary-select-article)))
7490           (when (and treated
7491                      (not
7492                       (and (gnus-buffer-live-p gnus-article-buffer)
7493                            (window-live-p (get-buffer-window
7494                                            gnus-article-buffer t))
7495                            (or (not (string-match "^\\^X-Face:" regexp))
7496                                (with-current-buffer gnus-article-buffer
7497                                  gnus-summary-search-article-matched-data)))))
7498             (gnus-summary-select-article nil t)
7499             (setq treated nil))))
7500       (set-buffer gnus-article-buffer)
7501       (widen)
7502       (if treated
7503           (progn
7504             (gnus-article-show-all-headers)
7505             (gnus-summary-search-article-position-point regexp backward))
7506         (goto-char (if backward (point-max) (point-min))))
7507       (while (not found)
7508         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7509         (if (if backward
7510                 (re-search-backward regexp nil t)
7511               (re-search-forward regexp nil t))
7512             ;; We found the regexp.
7513             (progn
7514               (gnus-summary-search-article-highlight-matched-text
7515                backward treated (string-match "^\\^X-Face:" regexp))
7516               (setq found 'found)
7517               (forward-line
7518                (/ (- 2 (window-height
7519                         (get-buffer-window gnus-article-buffer t)))
7520                   2))
7521               (set-window-start
7522                (get-buffer-window (current-buffer))
7523                (point))
7524               (set-buffer sum)
7525               (setq point (point)))
7526           ;; We didn't find it, so we go to the next article.
7527           (set-buffer sum)
7528           (setq found 'not)
7529           (while (eq found 'not)
7530             (if (not (if backward (gnus-summary-find-prev)
7531                        (gnus-summary-find-next)))
7532                 ;; No more articles.
7533                 (setq found t)
7534               ;; Select the next article and adjust point.
7535               (unless (gnus-summary-article-sparse-p
7536                        (gnus-summary-article-number))
7537                 (setq found nil)
7538                 (let ((gnus-inhibit-treatment t))
7539                   (gnus-summary-select-article))
7540                 (setq treated nil)
7541                 (set-buffer gnus-article-buffer)
7542                 (widen)
7543                 (goto-char (if backward (point-max) (point-min))))))))
7544       (gnus-message 7 ""))
7545     ;; Return whether we found the regexp.
7546     (when (eq found 'found)
7547       (goto-char point)
7548       (gnus-summary-show-thread)
7549       (gnus-summary-goto-subject gnus-current-article)
7550       (gnus-summary-position-point)
7551       t)))
7552
7553 (defun gnus-summary-find-matching (header regexp &optional backward unread
7554                                           not-case-fold)
7555   "Return a list of all articles that match REGEXP on HEADER.
7556 The search stars on the current article and goes forwards unless
7557 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7558 If UNREAD is non-nil, only unread articles will
7559 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7560 in the comparisons."
7561   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7562                 (gnus-data-find-list
7563                  (gnus-summary-article-number) (gnus-data-list backward))))
7564         (case-fold-search (not not-case-fold))
7565         articles d func)
7566     (if (consp header)
7567         (if (eq (car header) 'extra)
7568             (setq func
7569                   `(lambda (h)
7570                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7571                          "")))
7572           (error "%s is an invalid header" header))
7573       (unless (fboundp (intern (concat "mail-header-" header)))
7574         (error "%s is not a valid header" header))
7575       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7576     (while data
7577       (setq d (car data))
7578       (and (or (not unread)             ; We want all articles...
7579                (gnus-data-unread-p d))  ; Or just unreads.
7580            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7581            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7582            (push (gnus-data-number d) articles)) ; Success!
7583       (setq data (cdr data)))
7584     (nreverse articles)))
7585
7586 (defun gnus-summary-execute-command (header regexp command &optional backward)
7587   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7588 If HEADER is an empty string (or nil), the match is done on the entire
7589 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7590   (interactive
7591    (list (let ((completion-ignore-case t))
7592            (completing-read
7593             "Header name: "
7594             (mapcar (lambda (header) (list (format "%s" header)))
7595                     (append
7596                      '("Number" "Subject" "From" "Lines" "Date"
7597                        "Message-ID" "Xref" "References" "Body")
7598                      gnus-extra-headers))
7599             nil 'require-match))
7600          (read-string "Regexp: ")
7601          (read-key-sequence "Command: ")
7602          current-prefix-arg))
7603   (when (equal header "Body")
7604     (setq header ""))
7605   ;; Hidden thread subtrees must be searched as well.
7606   (gnus-summary-show-all-threads)
7607   ;; We don't want to change current point nor window configuration.
7608   (save-excursion
7609     (save-window-excursion
7610       (gnus-message 6 "Executing %s..." (key-description command))
7611       ;; We'd like to execute COMMAND interactively so as to give arguments.
7612       (gnus-execute header regexp
7613                     `(call-interactively ',(key-binding command))
7614                     backward)
7615       (gnus-message 6 "Executing %s...done" (key-description command)))))
7616
7617 (defun gnus-summary-beginning-of-article ()
7618   "Scroll the article back to the beginning."
7619   (interactive)
7620   (gnus-summary-select-article)
7621   (gnus-configure-windows 'article)
7622   (gnus-eval-in-buffer-window gnus-article-buffer
7623     (widen)
7624     (goto-char (point-min))
7625     (when gnus-page-broken
7626       (gnus-narrow-to-page))))
7627
7628 (defun gnus-summary-end-of-article ()
7629   "Scroll to the end of the article."
7630   (interactive)
7631   (gnus-summary-select-article)
7632   (gnus-configure-windows 'article)
7633   (gnus-eval-in-buffer-window gnus-article-buffer
7634     (widen)
7635     (goto-char (point-max))
7636     (recenter -3)
7637     (when gnus-page-broken
7638       (gnus-narrow-to-page))))
7639
7640 (defun gnus-summary-print-article (&optional filename n)
7641   "Generate and print a PostScript image of the N next (mail) articles.
7642
7643 If N is negative, print the N previous articles.  If N is nil and articles
7644 have been marked with the process mark, print these instead.
7645
7646 If the optional first argument FILENAME is nil, send the image to the
7647 printer.  If FILENAME is a string, save the PostScript image in a file with
7648 that name.  If FILENAME is a number, prompt the user for the name of the file
7649 to save in."
7650   (interactive (list (ps-print-preprint current-prefix-arg)))
7651   (dolist (article (gnus-summary-work-articles n))
7652     (gnus-summary-select-article nil nil 'pseudo article)
7653     (gnus-eval-in-buffer-window gnus-article-buffer
7654       (let ((buffer (generate-new-buffer " *print*")))
7655         (unwind-protect
7656             (progn
7657               (copy-to-buffer buffer (point-min) (point-max))
7658               (set-buffer buffer)
7659               (gnus-article-delete-invisible-text)
7660               (when (gnus-visual-p 'article-highlight 'highlight)
7661                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7662                 ;; highlight.
7663                 (let ((gnus-article-buffer buffer))
7664                   (gnus-article-highlight-citation t)
7665                   (gnus-article-highlight-signature)))
7666               (let ((ps-left-header
7667                      (list
7668                       (concat "("
7669                               (mail-header-subject gnus-current-headers) ")")
7670                       (concat "("
7671                               (mail-header-from gnus-current-headers) ")")))
7672                     (ps-right-header
7673                      (list
7674                       "/pagenumberstring load"
7675                       (concat "("
7676                               (mail-header-date gnus-current-headers) ")"))))
7677                 (gnus-run-hooks 'gnus-ps-print-hook)
7678                 (save-excursion
7679                   (if window-system
7680                       (ps-spool-buffer-with-faces)
7681                     (ps-spool-buffer)))))
7682           (kill-buffer buffer))))
7683     (gnus-summary-remove-process-mark article))
7684   (ps-despool filename))
7685
7686 (defun gnus-summary-show-article (&optional arg)
7687   "Force re-fetching of the current article.
7688 If ARG (the prefix) is a number, show the article with the charset
7689 defined in `gnus-summary-show-article-charset-alist', or the charset
7690 inputed.
7691 If ARG (the prefix) is non-nil and not a number, show the raw article
7692 without any article massaging functions being run."
7693   (interactive "P")
7694   (cond
7695    ((numberp arg)
7696     (let ((gnus-newsgroup-charset
7697            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7698                (read-coding-system "Charset: ")))
7699           (gnus-newsgroup-ignored-charsets 'gnus-all))
7700       (gnus-summary-select-article nil 'force)
7701       (let ((deps gnus-newsgroup-dependencies)
7702             head header)
7703         (save-excursion
7704           (set-buffer gnus-original-article-buffer)
7705           (save-restriction
7706             (message-narrow-to-head)
7707             (setq head (buffer-string)))
7708           (with-temp-buffer
7709             (insert (format "211 %d Article retrieved.\n"
7710                             (cdr gnus-article-current)))
7711             (insert head)
7712             (insert ".\n")
7713             (let ((nntp-server-buffer (current-buffer)))
7714               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7715         (gnus-data-set-header
7716          (gnus-data-find (cdr gnus-article-current))
7717          header)
7718         (gnus-summary-update-article-line
7719          (cdr gnus-article-current) header))))
7720    ((not arg)
7721     ;; Select the article the normal way.
7722     (gnus-summary-select-article nil 'force))
7723    (t
7724     ;; We have to require this here to make sure that the following
7725     ;; dynamic binding isn't shadowed by autoloading.
7726     (require 'gnus-async)
7727     (require 'gnus-art)
7728     ;; Bind the article treatment functions to nil.
7729     (let ((gnus-have-all-headers t)
7730           gnus-article-prepare-hook
7731           gnus-article-decode-hook
7732           gnus-break-pages
7733           gnus-show-mime
7734           (gnus-inhibit-treatment t))
7735       (gnus-summary-select-article nil 'force))))
7736   (gnus-summary-goto-subject gnus-current-article)
7737   (gnus-summary-position-point))
7738
7739 (defun gnus-summary-verbose-headers (&optional arg)
7740   "Toggle permanent full header display.
7741 If ARG is a positive number, turn header display on.
7742 If ARG is a negative number, turn header display off."
7743   (interactive "P")
7744   (setq gnus-show-all-headers
7745         (cond ((or (not (numberp arg))
7746                    (zerop arg))
7747                (not gnus-show-all-headers))
7748               ((natnump arg)
7749                t)))
7750   (gnus-summary-show-article))
7751
7752 (defun gnus-summary-toggle-header (&optional arg)
7753   "Show the headers if they are hidden, or hide them if they are shown.
7754 If ARG is a positive number, show the entire header.
7755 If ARG is a negative number, hide the unwanted header lines."
7756   (interactive "P")
7757   (save-excursion
7758     (set-buffer gnus-article-buffer)
7759     (save-restriction
7760       (let* ((buffer-read-only nil)
7761              (inhibit-point-motion-hooks t)
7762              hidden e)
7763         (setq hidden
7764               (if (numberp arg)
7765                   (>= arg 0)
7766                 (save-restriction
7767                   (article-narrow-to-head)
7768                   (gnus-article-hidden-text-p 'headers))))
7769         (goto-char (point-min))
7770         (when (search-forward "\n\n" nil t)
7771           (delete-region (point-min) (1- (point))))
7772         (goto-char (point-min))
7773         (save-excursion
7774           (set-buffer gnus-original-article-buffer)
7775           (goto-char (point-min))
7776           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7777         (insert-buffer-substring gnus-original-article-buffer 1 e)
7778         (save-restriction
7779           (narrow-to-region (point-min) (point))
7780           (article-decode-encoded-words)
7781           (if  hidden
7782               (let ((gnus-treat-hide-headers nil)
7783                     (gnus-treat-hide-boring-headers nil))
7784                 (setq gnus-article-wash-types
7785                       (delq 'headers gnus-article-wash-types))
7786                 (gnus-treat-article 'head))
7787             (gnus-treat-article 'head)))
7788         (gnus-set-mode-line 'article)))))
7789
7790 (defun gnus-summary-show-all-headers ()
7791   "Make all header lines visible."
7792   (interactive)
7793   (gnus-summary-toggle-header 1))
7794
7795 (defun gnus-summary-toggle-mime (&optional arg)
7796   "Toggle MIME processing.
7797 If ARG is a positive number, turn MIME processing on."
7798   (interactive "P")
7799   (setq gnus-show-mime
7800         (if (null arg)
7801             (not gnus-show-mime)
7802           (> (prefix-numeric-value arg) 0)))
7803   (gnus-summary-select-article t 'force))
7804
7805 (defun gnus-summary-caesar-message (&optional arg)
7806   "Caesar rotate the current article by 13.
7807 The numerical prefix specifies how many places to rotate each letter
7808 forward."
7809   (interactive "P")
7810   (gnus-summary-select-article)
7811   (let ((mail-header-separator ""))
7812     (gnus-eval-in-buffer-window gnus-article-buffer
7813       (save-restriction
7814         (widen)
7815         (let ((start (window-start))
7816               buffer-read-only)
7817           (message-caesar-buffer-body arg)
7818           (set-window-start (get-buffer-window (current-buffer)) start))))))
7819
7820 (defun gnus-summary-stop-page-breaking ()
7821   "Stop page breaking in the current article."
7822   (interactive)
7823   (gnus-summary-select-article)
7824   (gnus-eval-in-buffer-window gnus-article-buffer
7825     (widen)
7826     (when (gnus-visual-p 'page-marker)
7827       (let ((buffer-read-only nil))
7828         (gnus-remove-text-with-property 'gnus-prev)
7829         (gnus-remove-text-with-property 'gnus-next))
7830       (setq gnus-page-broken nil))))
7831
7832 (defun gnus-summary-move-article (&optional n to-newsgroup
7833                                             select-method action)
7834   "Move the current article to a different newsgroup.
7835 If N is a positive number, move the N next articles.
7836 If N is a negative number, move the N previous articles.
7837 If N is nil and any articles have been marked with the process mark,
7838 move those articles instead.
7839 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7840 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7841 re-spool using this method.
7842
7843 For this function to work, both the current newsgroup and the
7844 newsgroup that you want to move to have to support the `request-move'
7845 and `request-accept' functions.
7846
7847 ACTION can be either `move' (the default), `crosspost' or `copy'."
7848   (interactive "P")
7849   (unless action
7850     (setq action 'move))
7851   ;; Disable marking as read.
7852   (let (gnus-mark-article-hook)
7853     (save-window-excursion
7854       (gnus-summary-select-article)))
7855   ;; Check whether the source group supports the required functions.
7856   (cond ((and (eq action 'move)
7857               (not (gnus-check-backend-function
7858                     'request-move-article gnus-newsgroup-name)))
7859          (error "The current group does not support article moving"))
7860         ((and (eq action 'crosspost)
7861               (not (gnus-check-backend-function
7862                     'request-replace-article gnus-newsgroup-name)))
7863          (error "The current group does not support article editing")))
7864   (let ((articles (gnus-summary-work-articles n))
7865         (prefix (if (gnus-check-backend-function
7866                     'request-move-article gnus-newsgroup-name)
7867                     (gnus-group-real-prefix gnus-newsgroup-name)
7868                   ""))
7869         (names '((move "Move" "Moving")
7870                  (copy "Copy" "Copying")
7871                  (crosspost "Crosspost" "Crossposting")))
7872         (copy-buf (save-excursion
7873                     (nnheader-set-temp-buffer " *copy article*")))
7874         (default-marks gnus-article-mark-lists)
7875         (no-expire-marks (delete '(expirable . expire)
7876                                  (copy-sequence gnus-article-mark-lists)))
7877         art-group to-method new-xref article to-groups)
7878     (unless (assq action names)
7879       (error "Unknown action %s" action))
7880     ;; Read the newsgroup name.
7881     (when (and (not to-newsgroup)
7882                (not select-method))
7883       (setq to-newsgroup
7884             (gnus-read-move-group-name
7885              (cadr (assq action names))
7886              (symbol-value (intern (format "gnus-current-%s-group" action)))
7887              articles prefix))
7888       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7889     (setq to-method (or select-method
7890                         (gnus-server-to-method
7891                          (gnus-group-method to-newsgroup))))
7892     ;; Check the method we are to move this article to...
7893     (unless (gnus-check-backend-function
7894              'request-accept-article (car to-method))
7895       (error "%s does not support article copying" (car to-method)))
7896     (unless (gnus-check-server to-method)
7897       (error "Can't open server %s" (car to-method)))
7898     (gnus-message 6 "%s to %s: %s..."
7899                   (caddr (assq action names))
7900                   (or (car select-method) to-newsgroup) articles)
7901     (while articles
7902       (setq article (pop articles))
7903       (setq
7904        art-group
7905        (cond
7906         ;; Move the article.
7907         ((eq action 'move)
7908          ;; Remove this article from future suppression.
7909          (gnus-dup-unsuppress-article article)
7910          (gnus-request-move-article
7911           article                       ; Article to move
7912           gnus-newsgroup-name           ; From newsgroup
7913           (nth 1 (gnus-find-method-for-group
7914                   gnus-newsgroup-name)) ; Server
7915           (list 'gnus-request-accept-article
7916                 to-newsgroup (list 'quote select-method)
7917                 (not articles) t)       ; Accept form
7918           (not articles)))              ; Only save nov last time
7919         ;; Copy the article.
7920         ((eq action 'copy)
7921          (save-excursion
7922            (set-buffer copy-buf)
7923            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7924              (gnus-request-accept-article
7925               to-newsgroup select-method (not articles) t))))
7926         ;; Crosspost the article.
7927         ((eq action 'crosspost)
7928          (let ((xref (message-tokenize-header
7929                       (mail-header-xref (gnus-summary-article-header article))
7930                       " ")))
7931            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7932                                   ":" article))
7933            (unless xref
7934              (setq xref (list (system-name))))
7935            (setq new-xref
7936                  (concat
7937                   (mapconcat 'identity
7938                              (delete "Xref:" (delete new-xref xref))
7939                              " ")
7940                   " " new-xref))
7941            (save-excursion
7942              (set-buffer copy-buf)
7943              ;; First put the article in the destination group.
7944              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7945              (when (consp (setq art-group
7946                                 (gnus-request-accept-article
7947                                  to-newsgroup select-method (not articles))))
7948                (setq new-xref (concat new-xref " " (car art-group)
7949                                       ":" (cdr art-group)))
7950                ;; Now we have the new Xrefs header, so we insert
7951                ;; it and replace the new article.
7952                (nnheader-replace-header "Xref" new-xref)
7953                (gnus-request-replace-article
7954                 (cdr art-group) to-newsgroup (current-buffer))
7955                art-group))))))
7956       (cond
7957        ((not art-group)
7958         (gnus-message 1 "Couldn't %s article %s: %s"
7959                       (cadr (assq action names)) article
7960                       (nnheader-get-report (car to-method))))
7961        ((eq art-group 'junk)
7962         (when (eq action 'move)
7963           (gnus-summary-mark-article article gnus-canceled-mark)
7964           (gnus-message 4 "Deleted article %s" article)))
7965        (t
7966         (let* ((pto-group (gnus-group-prefixed-name
7967                            (car art-group) to-method))
7968                (entry
7969                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7970                (info (nth 2 entry))
7971                (to-group (gnus-info-group info))
7972                to-marks)
7973           ;; Update the group that has been moved to.
7974           (when (and info
7975                      (memq action '(move copy)))
7976             (unless (member to-group to-groups)
7977               (push to-group to-groups))
7978
7979             (unless (memq article gnus-newsgroup-unreads)
7980               (push 'read to-marks)
7981               (gnus-info-set-read
7982                info (gnus-add-to-range (gnus-info-read info)
7983                                        (list (cdr art-group)))))
7984
7985             ;; See whether the article is to be put in the cache.
7986             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7987                              default-marks
7988                            no-expire-marks))
7989                   (to-article (cdr art-group)))
7990
7991               ;; Enter the article into the cache in the new group,
7992               ;; if that is required.
7993               (when gnus-use-cache
7994                 (gnus-cache-possibly-enter-article
7995                  to-group to-article
7996                  (let ((header (copy-sequence
7997                                 (gnus-summary-article-header article))))
7998                    (mail-header-set-number header to-article)
7999                    header)
8000                  (memq article gnus-newsgroup-marked)
8001                  (memq article gnus-newsgroup-dormant)
8002                  (memq article gnus-newsgroup-unreads)))
8003
8004               (when gnus-preserve-marks
8005                 ;; Copy any marks over to the new group.
8006                 (when (and (equal to-group gnus-newsgroup-name)
8007                            (not (memq article gnus-newsgroup-unreads)))
8008                   ;; Mark this article as read in this group.
8009                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8010                   (setcdr (gnus-active to-group) to-article)
8011                   (setcdr gnus-newsgroup-active to-article))
8012
8013                 (while marks
8014                   (when (memq article (symbol-value
8015                                        (intern (format "gnus-newsgroup-%s"
8016                                                        (caar marks)))))
8017                     (push (cdar marks) to-marks)
8018                     ;; If the other group is the same as this group,
8019                     ;; then we have to add the mark to the list.
8020                     (when (equal to-group gnus-newsgroup-name)
8021                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8022                            (cons to-article
8023                                  (symbol-value
8024                                   (intern (format "gnus-newsgroup-%s"
8025                                                   (caar marks)))))))
8026                     ;; Copy the marks to other group.
8027                     (gnus-add-marked-articles
8028                      to-group (cdar marks) (list to-article) info))
8029                   (setq marks (cdr marks)))
8030
8031                 (gnus-request-set-mark to-group (list (list (list to-article)
8032                                                             'set
8033                                                             to-marks))))
8034
8035               (gnus-dribble-enter
8036                (concat "(gnus-group-set-info '"
8037                        (gnus-prin1-to-string (gnus-get-info to-group))
8038                        ")"))))
8039
8040           ;; Update the Xref header in this article to point to
8041           ;; the new crossposted article we have just created.
8042           (when (eq action 'crosspost)
8043             (save-excursion
8044               (set-buffer copy-buf)
8045               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8046               (nnheader-replace-header "Xref" new-xref)
8047               (gnus-request-replace-article
8048                article gnus-newsgroup-name (current-buffer)))))
8049
8050         ;;;!!!Why is this necessary?
8051         (set-buffer gnus-summary-buffer)
8052
8053         (gnus-summary-goto-subject article)
8054         (when (eq action 'move)
8055           (gnus-summary-mark-article article gnus-canceled-mark))))
8056       (gnus-summary-remove-process-mark article))
8057     ;; Re-activate all groups that have been moved to.
8058     (while to-groups
8059       (save-excursion
8060         (set-buffer gnus-group-buffer)
8061         (when (gnus-group-goto-group (car to-groups) t)
8062           (gnus-group-get-new-news-this-group 1 t))
8063         (pop to-groups)))
8064
8065     (gnus-kill-buffer copy-buf)
8066     (gnus-summary-position-point)
8067     (gnus-set-mode-line 'summary)))
8068
8069 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8070   "Move the current article to a different newsgroup.
8071 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8072 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8073 re-spool using this method."
8074   (interactive "P")
8075   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8076
8077 (defun gnus-summary-crosspost-article (&optional n)
8078   "Crosspost the current article to some other group."
8079   (interactive "P")
8080   (gnus-summary-move-article n nil nil 'crosspost))
8081
8082 (defcustom gnus-summary-respool-default-method nil
8083   "Default method for respooling an article.
8084 If nil, use to the current newsgroup method."
8085   :type '(choice (gnus-select-method :value (nnml ""))
8086                  (const nil))
8087   :group 'gnus-summary-mail)
8088
8089 (defun gnus-summary-respool-article (&optional n method)
8090   "Respool the current article.
8091 The article will be squeezed through the mail spooling process again,
8092 which means that it will be put in some mail newsgroup or other
8093 depending on `nnmail-split-methods'.
8094 If N is a positive number, respool the N next articles.
8095 If N is a negative number, respool the N previous articles.
8096 If N is nil and any articles have been marked with the process mark,
8097 respool those articles instead.
8098
8099 Respooling can be done both from mail groups and \"real\" newsgroups.
8100 In the former case, the articles in question will be moved from the
8101 current group into whatever groups they are destined to.  In the
8102 latter case, they will be copied into the relevant groups."
8103   (interactive
8104    (list current-prefix-arg
8105          (let* ((methods (gnus-methods-using 'respool))
8106                 (methname
8107                  (symbol-name (or gnus-summary-respool-default-method
8108                                   (car (gnus-find-method-for-group
8109                                         gnus-newsgroup-name)))))
8110                 (method
8111                  (gnus-completing-read
8112                   methname "What backend do you want to use when respooling?"
8113                   methods nil t nil 'gnus-mail-method-history))
8114                 ms)
8115            (cond
8116             ((zerop (length (setq ms (gnus-servers-using-backend
8117                                       (intern method)))))
8118              (list (intern method) ""))
8119             ((= 1 (length ms))
8120              (car ms))
8121             (t
8122              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8123                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8124                            ms-alist))))))))
8125   (unless method
8126     (error "No method given for respooling"))
8127   (if (assoc (symbol-name
8128               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8129              (gnus-methods-using 'respool))
8130       (gnus-summary-move-article n nil method)
8131     (gnus-summary-copy-article n nil method)))
8132
8133 (defun gnus-summary-import-article (file &optional edit)
8134   "Import an arbitrary file into a mail newsgroup."
8135   (interactive "fImport file: \nP")
8136   (let ((group gnus-newsgroup-name)
8137         (now (current-time))
8138         atts lines group-art)
8139     (unless (gnus-check-backend-function 'request-accept-article group)
8140       (error "%s does not support article importing" group))
8141     (or (file-readable-p file)
8142         (not (file-regular-p file))
8143         (error "Can't read %s" file))
8144     (save-excursion
8145       (set-buffer (gnus-get-buffer-create " *import file*"))
8146       (erase-buffer)
8147       (nnheader-insert-file-contents file)
8148       (goto-char (point-min))
8149       (unless (nnheader-article-p)
8150         ;; This doesn't look like an article, so we fudge some headers.
8151         (setq atts (file-attributes file)
8152               lines (count-lines (point-min) (point-max)))
8153         (insert "From: " (read-string "From: ") "\n"
8154                 "Subject: " (read-string "Subject: ") "\n"
8155                 "Date: " (message-make-date (nth 5 atts)) "\n"
8156                 "Message-ID: " (message-make-message-id) "\n"
8157                 "Lines: " (int-to-string lines) "\n"
8158                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8159       (setq group-art (gnus-request-accept-article group nil t))
8160       (kill-buffer (current-buffer)))
8161     (setq gnus-newsgroup-active (gnus-activate-group group))
8162     (forward-line 1)
8163     (gnus-summary-goto-article (cdr group-art) nil t)
8164     (when edit
8165       (gnus-summary-edit-article))))
8166
8167 (defun gnus-summary-create-article ()
8168   "Create an article in a mail newsgroup."
8169   (interactive)
8170   (let ((group gnus-newsgroup-name)
8171         (now (current-time))
8172         group-art)
8173     (unless (gnus-check-backend-function 'request-accept-article group)
8174       (error "%s does not support article importing" group))
8175     (save-excursion
8176       (set-buffer (gnus-get-buffer-create " *import file*"))
8177       (erase-buffer)
8178       (goto-char (point-min))
8179       ;; This doesn't look like an article, so we fudge some headers.
8180       (insert "From: " (read-string "From: ") "\n"
8181               "Subject: " (read-string "Subject: ") "\n"
8182               "Date: " (message-make-date now) "\n"
8183               "Message-ID: " (message-make-message-id) "\n")
8184       (setq group-art (gnus-request-accept-article group nil t))
8185       (kill-buffer (current-buffer)))
8186     (setq gnus-newsgroup-active (gnus-activate-group group))
8187     (forward-line 1)
8188     (gnus-summary-goto-article (cdr group-art) nil t)
8189     (gnus-summary-edit-article)))
8190
8191 (defun gnus-summary-article-posted-p ()
8192   "Say whether the current (mail) article is available from news as well.
8193 This will be the case if the article has both been mailed and posted."
8194   (interactive)
8195   (let ((id (mail-header-references (gnus-summary-article-header)))
8196         (gnus-override-method (car (gnus-refer-article-methods))))
8197     (if (gnus-request-head id "")
8198         (gnus-message 2 "The current message was found on %s"
8199                       gnus-override-method)
8200       (gnus-message 2 "The current message couldn't be found on %s"
8201                     gnus-override-method)
8202       nil)))
8203
8204 (defun gnus-summary-expire-articles (&optional now)
8205   "Expire all articles that are marked as expirable in the current group."
8206   (interactive)
8207   (when (gnus-check-backend-function
8208          'request-expire-articles gnus-newsgroup-name)
8209     ;; This backend supports expiry.
8210     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8211            (expirable (if total
8212                           (progn
8213                             ;; We need to update the info for
8214                             ;; this group for `gnus-list-of-read-articles'
8215                             ;; to give us the right answer.
8216                             (gnus-run-hooks 'gnus-exit-group-hook)
8217                             (gnus-summary-update-info)
8218                             (gnus-list-of-read-articles gnus-newsgroup-name))
8219                         (setq gnus-newsgroup-expirable
8220                               (sort gnus-newsgroup-expirable '<))))
8221            (expiry-wait (if now 'immediate
8222                           (gnus-group-find-parameter
8223                            gnus-newsgroup-name 'expiry-wait)))
8224            (nnmail-expiry-target
8225             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8226                 nnmail-expiry-target))
8227            es)
8228       (when expirable
8229         ;; There are expirable articles in this group, so we run them
8230         ;; through the expiry process.
8231         (gnus-message 6 "Expiring articles...")
8232         (unless (gnus-check-group gnus-newsgroup-name)
8233           (error "Can't open server for %s" gnus-newsgroup-name))
8234         ;; The list of articles that weren't expired is returned.
8235         (save-excursion
8236           (if expiry-wait
8237               (let ((nnmail-expiry-wait-function nil)
8238                     (nnmail-expiry-wait expiry-wait))
8239                 (setq es (gnus-request-expire-articles
8240                           expirable gnus-newsgroup-name)))
8241             (setq es (gnus-request-expire-articles
8242                       expirable gnus-newsgroup-name)))
8243           (unless total
8244             (setq gnus-newsgroup-expirable es))
8245           ;; We go through the old list of expirable, and mark all
8246           ;; really expired articles as nonexistent.
8247           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8248             (let ((gnus-use-cache nil))
8249               (while expirable
8250                 (unless (memq (car expirable) es)
8251                   (when (gnus-data-find (car expirable))
8252                     (gnus-summary-mark-article
8253                      (car expirable) gnus-canceled-mark)))
8254                 (setq expirable (cdr expirable))))))
8255         (gnus-message 6 "Expiring articles...done")))))
8256
8257 (defun gnus-summary-expire-articles-now ()
8258   "Expunge all expirable articles in the current group.
8259 This means that *all* articles that are marked as expirable will be
8260 deleted forever, right now."
8261   (interactive)
8262   (or gnus-expert-user
8263       (gnus-yes-or-no-p
8264        "Are you really, really, really sure you want to delete all these messages? ")
8265       (error "Phew!"))
8266   (gnus-summary-expire-articles t))
8267
8268 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8269 (defun gnus-summary-delete-article (&optional n)
8270   "Delete the N next (mail) articles.
8271 This command actually deletes articles.  This is not a marking
8272 command.  The article will disappear forever from your life, never to
8273 return.
8274 If N is negative, delete backwards.
8275 If N is nil and articles have been marked with the process mark,
8276 delete these instead."
8277   (interactive "P")
8278   (unless (gnus-check-backend-function 'request-expire-articles
8279                                        gnus-newsgroup-name)
8280     (error "The current newsgroup does not support article deletion"))
8281   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8282     (error "Couldn't open server"))
8283   ;; Compute the list of articles to delete.
8284   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8285         not-deleted)
8286     (if (and gnus-novice-user
8287              (not (gnus-yes-or-no-p
8288                    (format "Do you really want to delete %s forever? "
8289                            (if (> (length articles) 1)
8290                                (format "these %s articles" (length articles))
8291                              "this article")))))
8292         ()
8293       ;; Delete the articles.
8294       (setq not-deleted (gnus-request-expire-articles
8295                          articles gnus-newsgroup-name 'force))
8296       (while articles
8297         (gnus-summary-remove-process-mark (car articles))
8298         ;; The backend might not have been able to delete the article
8299         ;; after all.
8300         (unless (memq (car articles) not-deleted)
8301           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8302         (setq articles (cdr articles)))
8303       (when not-deleted
8304         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8305     (gnus-summary-position-point)
8306     (gnus-set-mode-line 'summary)
8307     not-deleted))
8308
8309 (defun gnus-summary-edit-article (&optional force)
8310   "Edit the current article.
8311 This will have permanent effect only in mail groups.
8312 If FORCE is non-nil, allow editing of articles even in read-only
8313 groups."
8314   (interactive "P")
8315   (save-excursion
8316     (set-buffer gnus-summary-buffer)
8317     (let ((mail-parse-charset gnus-newsgroup-charset)
8318           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8319       (gnus-set-global-variables)
8320       (when (and (not force)
8321                  (gnus-group-read-only-p))
8322         (error "The current newsgroup does not support article editing"))
8323       (gnus-summary-show-article t)
8324       (gnus-article-edit-article
8325        'ignore
8326        `(lambda (no-highlight)
8327           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8328                 (message-options message-options)
8329                 (message-options-set-recipient)
8330                 (mail-parse-ignored-charsets
8331                  ',gnus-newsgroup-ignored-charsets))
8332             (gnus-summary-edit-article-done
8333              ,(or (mail-header-references gnus-current-headers) "")
8334              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8335
8336 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8337
8338 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8339                                                  no-highlight)
8340   "Make edits to the current article permanent."
8341   (interactive)
8342   (save-excursion
8343     ;; The buffer restriction contains the entire article if it exists.
8344     (when (article-goto-body)
8345       (let ((lines (count-lines (point) (point-max)))
8346             (length (- (point-max) (point)))
8347             (case-fold-search t)
8348             (body (copy-marker (point))))
8349         (goto-char (point-min))
8350         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8351           (delete-region (match-beginning 1) (match-end 1))
8352           (insert (number-to-string length)))
8353         (goto-char (point-min))
8354         (when (re-search-forward
8355                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8356           (delete-region (match-beginning 1) (match-end 1))
8357           (insert (number-to-string length)))
8358         (goto-char (point-min))
8359         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8360           (delete-region (match-beginning 1) (match-end 1))
8361           (insert (number-to-string lines))))))
8362   ;; Replace the article.
8363   (let ((buf (current-buffer)))
8364     (with-temp-buffer
8365       (insert-buffer-substring buf)
8366
8367       (if (and (not read-only)
8368                (not (gnus-request-replace-article
8369                      (cdr gnus-article-current) (car gnus-article-current)
8370                      (current-buffer) t)))
8371           (error "Couldn't replace article")
8372         ;; Update the summary buffer.
8373         (if (and references
8374                  (equal (message-tokenize-header references " ")
8375                         (message-tokenize-header
8376                          (or (message-fetch-field "references") "") " ")))
8377             ;; We only have to update this line.
8378             (save-excursion
8379               (save-restriction
8380                 (message-narrow-to-head)
8381                 (let ((head (buffer-string))
8382                       header)
8383                   (with-temp-buffer
8384                     (insert (format "211 %d Article retrieved.\n"
8385                                     (cdr gnus-article-current)))
8386                     (insert head)
8387                     (insert ".\n")
8388                     (let ((nntp-server-buffer (current-buffer)))
8389                       (setq header (car (gnus-get-newsgroup-headers
8390                                          (save-excursion
8391                                            (set-buffer gnus-summary-buffer)
8392                                            gnus-newsgroup-dependencies)
8393                                          t))))
8394                     (save-excursion
8395                       (set-buffer gnus-summary-buffer)
8396                       (gnus-data-set-header
8397                        (gnus-data-find (cdr gnus-article-current))
8398                        header)
8399                       (gnus-summary-update-article-line
8400                        (cdr gnus-article-current) header))))))
8401           ;; Update threads.
8402           (set-buffer (or buffer gnus-summary-buffer))
8403           (gnus-summary-update-article (cdr gnus-article-current)))
8404         ;; Prettify the article buffer again.
8405         (unless no-highlight
8406           (save-excursion
8407             (set-buffer gnus-article-buffer)
8408             ;;;!!! Fix this -- article should be rehighlighted.
8409             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8410             (set-buffer gnus-original-article-buffer)
8411             (gnus-request-article
8412              (cdr gnus-article-current)
8413              (car gnus-article-current) (current-buffer))))
8414         ;; Prettify the summary buffer line.
8415         (when (gnus-visual-p 'summary-highlight 'highlight)
8416           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8417
8418 (defun gnus-summary-edit-wash (key)
8419   "Perform editing command KEY in the article buffer."
8420   (interactive
8421    (list
8422     (progn
8423       (message "%s" (concat (this-command-keys) "- "))
8424       (read-char))))
8425   (message "")
8426   (gnus-summary-edit-article)
8427   (execute-kbd-macro (concat (this-command-keys) key))
8428   (gnus-article-edit-done))
8429
8430 ;;; Respooling
8431
8432 (defun gnus-summary-respool-query (&optional silent trace)
8433   "Query where the respool algorithm would put this article."
8434   (interactive)
8435   (let (gnus-mark-article-hook)
8436     (gnus-summary-select-article)
8437     (save-excursion
8438       (set-buffer gnus-original-article-buffer)
8439       (save-restriction
8440         (message-narrow-to-head)
8441         (let ((groups (nnmail-article-group 'identity trace)))
8442           (unless silent
8443             (if groups
8444                 (message "This message would go to %s"
8445                          (mapconcat 'car groups ", "))
8446               (message "This message would go to no groups"))
8447             groups))))))
8448
8449 (defun gnus-summary-respool-trace ()
8450   "Trace where the respool algorithm would put this article.
8451 Display a buffer showing all fancy splitting patterns which matched."
8452   (interactive)
8453   (gnus-summary-respool-query nil t))
8454
8455 ;; Summary marking commands.
8456
8457 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8458   "Mark articles which has the same subject as read, and then select the next.
8459 If UNMARK is positive, remove any kind of mark.
8460 If UNMARK is negative, tick articles."
8461   (interactive "P")
8462   (when unmark
8463     (setq unmark (prefix-numeric-value unmark)))
8464   (let ((count
8465          (gnus-summary-mark-same-subject
8466           (gnus-summary-article-subject) unmark)))
8467     ;; Select next unread article.  If auto-select-same mode, should
8468     ;; select the first unread article.
8469     (gnus-summary-next-article t (and gnus-auto-select-same
8470                                       (gnus-summary-article-subject)))
8471     (gnus-message 7 "%d article%s marked as %s"
8472                   count (if (= count 1) " is" "s are")
8473                   (if unmark "unread" "read"))))
8474
8475 (defun gnus-summary-kill-same-subject (&optional unmark)
8476   "Mark articles which has the same subject as read.
8477 If UNMARK is positive, remove any kind of mark.
8478 If UNMARK is negative, tick articles."
8479   (interactive "P")
8480   (when unmark
8481     (setq unmark (prefix-numeric-value unmark)))
8482   (let ((count
8483          (gnus-summary-mark-same-subject
8484           (gnus-summary-article-subject) unmark)))
8485     ;; If marked as read, go to next unread subject.
8486     (when (null unmark)
8487       ;; Go to next unread subject.
8488       (gnus-summary-next-subject 1 t))
8489     (gnus-message 7 "%d articles are marked as %s"
8490                   count (if unmark "unread" "read"))))
8491
8492 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8493   "Mark articles with same SUBJECT as read, and return marked number.
8494 If optional argument UNMARK is positive, remove any kinds of marks.
8495 If optional argument UNMARK is negative, mark articles as unread instead."
8496   (let ((count 1))
8497     (save-excursion
8498       (cond
8499        ((null unmark)                   ; Mark as read.
8500         (while (and
8501                 (progn
8502                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8503                   (gnus-summary-show-thread) t)
8504                 (gnus-summary-find-subject subject))
8505           (setq count (1+ count))))
8506        ((> unmark 0)                    ; Tick.
8507         (while (and
8508                 (progn
8509                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8510                   (gnus-summary-show-thread) t)
8511                 (gnus-summary-find-subject subject))
8512           (setq count (1+ count))))
8513        (t                               ; Mark as unread.
8514         (while (and
8515                 (progn
8516                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8517                   (gnus-summary-show-thread) t)
8518                 (gnus-summary-find-subject subject))
8519           (setq count (1+ count)))))
8520       (gnus-set-mode-line 'summary)
8521       ;; Return the number of marked articles.
8522       count)))
8523
8524 (defun gnus-summary-mark-as-processable (n &optional unmark)
8525   "Set the process mark on the next N articles.
8526 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8527 the process mark instead.  The difference between N and the actual
8528 number of articles marked is returned."
8529   (interactive "P")
8530   (if (and (null n) (gnus-region-active-p))
8531       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8532     (setq n (prefix-numeric-value n))
8533     (let ((backward (< n 0))
8534           (n (abs n)))
8535       (while (and
8536               (> n 0)
8537               (if unmark
8538                 (gnus-summary-remove-process-mark
8539                  (gnus-summary-article-number))
8540                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8541               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8542         (setq n (1- n)))
8543       (when (/= 0 n)
8544         (gnus-message 7 "No more articles"))
8545       (gnus-summary-recenter)
8546       (gnus-summary-position-point)
8547       n)))
8548
8549 (defun gnus-summary-unmark-as-processable (n)
8550   "Remove the process mark from the next N articles.
8551 If N is negative, unmark backward instead.  The difference between N and
8552 the actual number of articles unmarked is returned."
8553   (interactive "P")
8554   (gnus-summary-mark-as-processable n t))
8555
8556 (defun gnus-summary-unmark-all-processable ()
8557   "Remove the process mark from all articles."
8558   (interactive)
8559   (save-excursion
8560     (while gnus-newsgroup-processable
8561       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8562   (gnus-summary-position-point))
8563
8564 (defun gnus-summary-add-mark (article type)
8565   "Mark ARTICLE with a mark of TYPE."
8566   (let ((vtype (car (assq type gnus-article-mark-lists)))
8567         var)
8568     (if (not vtype)
8569         (error "No such mark type: %s" type)
8570       (setq var (intern (format "gnus-newsgroup-%s" type)))
8571       (set var (cons article (symbol-value var)))
8572       (if (memq type '(processable cached replied forwarded saved))
8573           (gnus-summary-update-secondary-mark article)
8574         ;;; !!! This is bobus.  We should find out what primary
8575         ;;; !!! mark we want to set.
8576         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8577
8578 (defun gnus-summary-mark-as-expirable (n)
8579   "Mark N articles forward as expirable.
8580 If N is negative, mark backward instead.  The difference between N and
8581 the actual number of articles marked is returned."
8582   (interactive "p")
8583   (gnus-summary-mark-forward n gnus-expirable-mark))
8584
8585 (defun gnus-summary-mark-article-as-replied (article)
8586   "Mark ARTICLE as replied to and update the summary line.
8587 ARTICLE can also be a list of articles."
8588   (interactive (list (gnus-summary-article-number)))
8589   (let ((articles (if (listp article) article (list article))))
8590     (dolist (article articles)
8591       (push article gnus-newsgroup-replied)
8592       (let ((buffer-read-only nil))
8593         (when (gnus-summary-goto-subject article nil t)
8594           (gnus-summary-update-secondary-mark article))))))
8595
8596 (defun gnus-summary-mark-article-as-forwarded (article)
8597   "Mark ARTICLE as forwarded and update the summary line.
8598 ARTICLE can also be a list of articles."
8599   (let ((articles (if (listp article) article (list article))))
8600     (dolist (article articles)
8601       (push article gnus-newsgroup-forwarded)
8602       (let ((buffer-read-only nil))
8603         (when (gnus-summary-goto-subject article nil t)
8604           (gnus-summary-update-secondary-mark article))))))
8605
8606 (defun gnus-summary-set-bookmark (article)
8607   "Set a bookmark in current article."
8608   (interactive (list (gnus-summary-article-number)))
8609   (when (or (not (get-buffer gnus-article-buffer))
8610             (not gnus-current-article)
8611             (not gnus-article-current)
8612             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8613     (error "No current article selected"))
8614   ;; Remove old bookmark, if one exists.
8615   (let ((old (assq article gnus-newsgroup-bookmarks)))
8616     (when old
8617       (setq gnus-newsgroup-bookmarks
8618             (delq old gnus-newsgroup-bookmarks))))
8619   ;; Set the new bookmark, which is on the form
8620   ;; (article-number . line-number-in-body).
8621   (push
8622    (cons article
8623          (save-excursion
8624            (set-buffer gnus-article-buffer)
8625            (count-lines
8626             (min (point)
8627                  (save-excursion
8628                    (goto-char (point-min))
8629                    (search-forward "\n\n" nil t)
8630                    (point)))
8631             (point))))
8632    gnus-newsgroup-bookmarks)
8633   (gnus-message 6 "A bookmark has been added to the current article."))
8634
8635 (defun gnus-summary-remove-bookmark (article)
8636   "Remove the bookmark from the current article."
8637   (interactive (list (gnus-summary-article-number)))
8638   ;; Remove old bookmark, if one exists.
8639   (let ((old (assq article gnus-newsgroup-bookmarks)))
8640     (if old
8641         (progn
8642           (setq gnus-newsgroup-bookmarks
8643                 (delq old gnus-newsgroup-bookmarks))
8644           (gnus-message 6 "Removed bookmark."))
8645       (gnus-message 6 "No bookmark in current article."))))
8646
8647 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8648 (defun gnus-summary-mark-as-dormant (n)
8649   "Mark N articles forward as dormant.
8650 If N is negative, mark backward instead.  The difference between N and
8651 the actual number of articles marked is returned."
8652   (interactive "p")
8653   (gnus-summary-mark-forward n gnus-dormant-mark))
8654
8655 (defun gnus-summary-set-process-mark (article)
8656   "Set the process mark on ARTICLE and update the summary line."
8657   (setq gnus-newsgroup-processable
8658         (cons article
8659               (delq article gnus-newsgroup-processable)))
8660   (when (gnus-summary-goto-subject article)
8661     (gnus-summary-show-thread)
8662     (gnus-summary-goto-subject article)
8663     (gnus-summary-update-secondary-mark article)))
8664
8665 (defun gnus-summary-remove-process-mark (article)
8666   "Remove the process mark from ARTICLE and update the summary line."
8667   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8668   (when (gnus-summary-goto-subject article)
8669     (gnus-summary-show-thread)
8670     (gnus-summary-goto-subject article)
8671     (gnus-summary-update-secondary-mark article)))
8672
8673 (defun gnus-summary-set-saved-mark (article)
8674   "Set the process mark on ARTICLE and update the summary line."
8675   (push article gnus-newsgroup-saved)
8676   (when (gnus-summary-goto-subject article)
8677     (gnus-summary-update-secondary-mark article)))
8678
8679 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8680   "Mark N articles as read forwards.
8681 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8682 The difference between N and the actual number of articles marked is
8683 returned.
8684 Iff NO-EXPIRE, auto-expiry will be inhibited."
8685   (interactive "p")
8686   (gnus-summary-show-thread)
8687   (let ((backward (< n 0))
8688         (gnus-summary-goto-unread
8689          (and gnus-summary-goto-unread
8690               (not (eq gnus-summary-goto-unread 'never))
8691               (not (memq mark (list gnus-unread-mark
8692                                     gnus-ticked-mark gnus-dormant-mark)))))
8693         (n (abs n))
8694         (mark (or mark gnus-del-mark)))
8695     (while (and (> n 0)
8696                 (gnus-summary-mark-article nil mark no-expire)
8697                 (zerop (gnus-summary-next-subject
8698                         (if backward -1 1)
8699                         (and gnus-summary-goto-unread
8700                              (not (eq gnus-summary-goto-unread 'never)))
8701                         t)))
8702       (setq n (1- n)))
8703     (when (/= 0 n)
8704       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8705     (gnus-summary-recenter)
8706     (gnus-summary-position-point)
8707     (gnus-set-mode-line 'summary)
8708     n))
8709
8710 (defun gnus-summary-mark-article-as-read (mark)
8711   "Mark the current article quickly as read with MARK."
8712   (let ((article (gnus-summary-article-number)))
8713     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8714     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8715     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8716     (push (cons article mark) gnus-newsgroup-reads)
8717     ;; Possibly remove from cache, if that is used.
8718     (when gnus-use-cache
8719       (gnus-cache-enter-remove-article article))
8720     ;; Allow the backend to change the mark.
8721     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8722     ;; Check for auto-expiry.
8723     (when (and gnus-newsgroup-auto-expire
8724                (memq mark gnus-auto-expirable-marks))
8725       (setq mark gnus-expirable-mark)
8726       ;; Let the backend know about the mark change.
8727       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8728       (push article gnus-newsgroup-expirable))
8729     ;; Set the mark in the buffer.
8730     (gnus-summary-update-mark mark 'unread)
8731     t))
8732
8733 (defun gnus-summary-mark-article-as-unread (mark)
8734   "Mark the current article quickly as unread with MARK."
8735   (let* ((article (gnus-summary-article-number))
8736          (old-mark (gnus-summary-article-mark article)))
8737     ;; Allow the backend to change the mark.
8738     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8739     (if (eq mark old-mark)
8740         t
8741       (if (<= article 0)
8742           (progn
8743             (gnus-error 1 "Can't mark negative article numbers")
8744             nil)
8745         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8746         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8747         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8748         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8749         (cond ((= mark gnus-ticked-mark)
8750                (push article gnus-newsgroup-marked))
8751               ((= mark gnus-dormant-mark)
8752                (push article gnus-newsgroup-dormant))
8753               (t
8754                (push article gnus-newsgroup-unreads)))
8755         (gnus-pull article gnus-newsgroup-reads)
8756
8757         ;; See whether the article is to be put in the cache.
8758         (and gnus-use-cache
8759              (vectorp (gnus-summary-article-header article))
8760              (save-excursion
8761                (gnus-cache-possibly-enter-article
8762                 gnus-newsgroup-name article
8763                 (gnus-summary-article-header article)
8764                 (= mark gnus-ticked-mark)
8765                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8766
8767         ;; Fix the mark.
8768         (gnus-summary-update-mark mark 'unread)
8769         t))))
8770
8771 (defun gnus-summary-mark-article (&optional article mark no-expire)
8772   "Mark ARTICLE with MARK.  MARK can be any character.
8773 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8774 `??' (dormant) and `?E' (expirable).
8775 If MARK is nil, then the default character `?r' is used.
8776 If ARTICLE is nil, then the article on the current line will be
8777 marked.
8778 Iff NO-EXPIRE, auto-expiry will be inhibited."
8779   ;; The mark might be a string.
8780   (when (stringp mark)
8781     (setq mark (aref mark 0)))
8782   ;; If no mark is given, then we check auto-expiring.
8783   (when (null mark)
8784     (setq mark gnus-del-mark))
8785   (when (and (not no-expire)
8786              gnus-newsgroup-auto-expire
8787              (memq mark gnus-auto-expirable-marks))
8788     (setq mark gnus-expirable-mark))
8789   (let ((article (or article (gnus-summary-article-number)))
8790         (old-mark (gnus-summary-article-mark article)))
8791     ;; Allow the backend to change the mark.
8792     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8793     (if (eq mark old-mark)
8794         t
8795       (unless article
8796         (error "No article on current line"))
8797       (if (not (if (or (= mark gnus-unread-mark)
8798                        (= mark gnus-ticked-mark)
8799                        (= mark gnus-dormant-mark))
8800                    (gnus-mark-article-as-unread article mark)
8801                  (gnus-mark-article-as-read article mark)))
8802           t
8803         ;; See whether the article is to be put in the cache.
8804         (and gnus-use-cache
8805              (not (= mark gnus-canceled-mark))
8806              (vectorp (gnus-summary-article-header article))
8807              (save-excursion
8808                (gnus-cache-possibly-enter-article
8809                 gnus-newsgroup-name article
8810                 (gnus-summary-article-header article)
8811                 (= mark gnus-ticked-mark)
8812                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8813
8814         (when (gnus-summary-goto-subject article nil t)
8815           (let ((buffer-read-only nil))
8816             (gnus-summary-show-thread)
8817             ;; Fix the mark.
8818             (gnus-summary-update-mark mark 'unread)
8819             t))))))
8820
8821 (defun gnus-summary-update-secondary-mark (article)
8822   "Update the secondary (read, process, cache) mark."
8823   (gnus-summary-update-mark
8824    (cond ((memq article gnus-newsgroup-processable)
8825           gnus-process-mark)
8826          ((memq article gnus-newsgroup-cached)
8827           gnus-cached-mark)
8828          ((memq article gnus-newsgroup-replied)
8829           gnus-replied-mark)
8830          ((memq article gnus-newsgroup-forwarded)
8831           gnus-forwarded-mark)
8832          ((memq article gnus-newsgroup-saved)
8833           gnus-saved-mark)
8834          (t gnus-no-mark))
8835    'replied)
8836   (when (gnus-visual-p 'summary-highlight 'highlight)
8837     (gnus-run-hooks 'gnus-summary-update-hook))
8838   t)
8839
8840 (defun gnus-summary-update-mark (mark type)
8841   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8842         (buffer-read-only nil))
8843     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8844     (when forward
8845       (when (looking-at "\r")
8846         (incf forward))
8847       (when (<= (+ forward (point)) (point-max))
8848         ;; Go to the right position on the line.
8849         (goto-char (+ forward (point)))
8850         ;; Replace the old mark with the new mark.
8851         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8852         ;; Optionally update the marks by some user rule.
8853         (when (eq type 'unread)
8854           (gnus-data-set-mark
8855            (gnus-data-find (gnus-summary-article-number)) mark)
8856           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8857
8858 (defun gnus-mark-article-as-read (article &optional mark)
8859   "Enter ARTICLE in the pertinent lists and remove it from others."
8860   ;; Make the article expirable.
8861   (let ((mark (or mark gnus-del-mark)))
8862     (if (= mark gnus-expirable-mark)
8863         (push article gnus-newsgroup-expirable)
8864       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8865     ;; Remove from unread and marked lists.
8866     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8867     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8868     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8869     (push (cons article mark) gnus-newsgroup-reads)
8870     ;; Possibly remove from cache, if that is used.
8871     (when gnus-use-cache
8872       (gnus-cache-enter-remove-article article))
8873     t))
8874
8875 (defun gnus-mark-article-as-unread (article &optional mark)
8876   "Enter ARTICLE in the pertinent lists and remove it from others."
8877   (let ((mark (or mark gnus-ticked-mark)))
8878     (if (<= article 0)
8879         (progn
8880           (gnus-error 1 "Can't mark negative article numbers")
8881           nil)
8882       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8883             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8884             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8885             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8886
8887       ;; Unsuppress duplicates?
8888       (when gnus-suppress-duplicates
8889         (gnus-dup-unsuppress-article article))
8890
8891       (cond ((= mark gnus-ticked-mark)
8892              (push article gnus-newsgroup-marked))
8893             ((= mark gnus-dormant-mark)
8894              (push article gnus-newsgroup-dormant))
8895             (t
8896              (push article gnus-newsgroup-unreads)))
8897       (gnus-pull article gnus-newsgroup-reads)
8898       t)))
8899
8900 (defalias 'gnus-summary-mark-as-unread-forward
8901   'gnus-summary-tick-article-forward)
8902 (make-obsolete 'gnus-summary-mark-as-unread-forward
8903                'gnus-summary-tick-article-forward)
8904 (defun gnus-summary-tick-article-forward (n)
8905   "Tick N articles forwards.
8906 If N is negative, tick backwards instead.
8907 The difference between N and the number of articles ticked is returned."
8908   (interactive "p")
8909   (gnus-summary-mark-forward n gnus-ticked-mark))
8910
8911 (defalias 'gnus-summary-mark-as-unread-backward
8912   'gnus-summary-tick-article-backward)
8913 (make-obsolete 'gnus-summary-mark-as-unread-backward
8914                'gnus-summary-tick-article-backward)
8915 (defun gnus-summary-tick-article-backward (n)
8916   "Tick N articles backwards.
8917 The difference between N and the number of articles ticked is returned."
8918   (interactive "p")
8919   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8920
8921 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8922 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8923 (defun gnus-summary-tick-article (&optional article clear-mark)
8924   "Mark current article as unread.
8925 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8926 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8927   (interactive)
8928   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8929                                        gnus-ticked-mark)))
8930
8931 (defun gnus-summary-mark-as-read-forward (n)
8932   "Mark N articles as read forwards.
8933 If N is negative, mark backwards instead.
8934 The difference between N and the actual number of articles marked is
8935 returned."
8936   (interactive "p")
8937   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8938
8939 (defun gnus-summary-mark-as-read-backward (n)
8940   "Mark the N articles as read backwards.
8941 The difference between N and the actual number of articles marked is
8942 returned."
8943   (interactive "p")
8944   (gnus-summary-mark-forward
8945    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8946
8947 (defun gnus-summary-mark-as-read (&optional article mark)
8948   "Mark current article as read.
8949 ARTICLE specifies the article to be marked as read.
8950 MARK specifies a string to be inserted at the beginning of the line."
8951   (gnus-summary-mark-article article mark))
8952
8953 (defun gnus-summary-clear-mark-forward (n)
8954   "Clear marks from N articles forward.
8955 If N is negative, clear backward instead.
8956 The difference between N and the number of marks cleared is returned."
8957   (interactive "p")
8958   (gnus-summary-mark-forward n gnus-unread-mark))
8959
8960 (defun gnus-summary-clear-mark-backward (n)
8961   "Clear marks from N articles backward.
8962 The difference between N and the number of marks cleared is returned."
8963   (interactive "p")
8964   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8965
8966 (defun gnus-summary-mark-unread-as-read ()
8967   "Intended to be used by `gnus-summary-mark-article-hook'."
8968   (when (memq gnus-current-article gnus-newsgroup-unreads)
8969     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8970
8971 (defun gnus-summary-mark-read-and-unread-as-read ()
8972   "Intended to be used by `gnus-summary-mark-article-hook'."
8973   (let ((mark (gnus-summary-article-mark)))
8974     (when (or (gnus-unread-mark-p mark)
8975               (gnus-read-mark-p mark))
8976       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8977
8978 (defun gnus-summary-mark-unread-as-ticked ()
8979    "Intended to be used by `gnus-summary-mark-article-hook'."
8980   (when (memq gnus-current-article gnus-newsgroup-unreads)
8981     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
8982
8983 (defun gnus-summary-mark-region-as-read (point mark all)
8984   "Mark all unread articles between point and mark as read.
8985 If given a prefix, mark all articles between point and mark as read,
8986 even ticked and dormant ones."
8987   (interactive "r\nP")
8988   (save-excursion
8989     (let (article)
8990       (goto-char point)
8991       (beginning-of-line)
8992       (while (and
8993               (< (point) mark)
8994               (progn
8995                 (when (or all
8996                           (memq (setq article (gnus-summary-article-number))
8997                                 gnus-newsgroup-unreads))
8998                   (gnus-summary-mark-article article gnus-del-mark))
8999                 t)
9000               (gnus-summary-find-next))))))
9001
9002 (defun gnus-summary-mark-below (score mark)
9003   "Mark articles with score less than SCORE with MARK."
9004   (interactive "P\ncMark: ")
9005   (setq score (if score
9006                   (prefix-numeric-value score)
9007                 (or gnus-summary-default-score 0)))
9008   (save-excursion
9009     (set-buffer gnus-summary-buffer)
9010     (goto-char (point-min))
9011     (while
9012         (progn
9013           (and (< (gnus-summary-article-score) score)
9014                (gnus-summary-mark-article nil mark))
9015           (gnus-summary-find-next)))))
9016
9017 (defun gnus-summary-kill-below (&optional score)
9018   "Mark articles with score below SCORE as read."
9019   (interactive "P")
9020   (gnus-summary-mark-below score gnus-killed-mark))
9021
9022 (defun gnus-summary-clear-above (&optional score)
9023   "Clear all marks from articles with score above SCORE."
9024   (interactive "P")
9025   (gnus-summary-mark-above score gnus-unread-mark))
9026
9027 (defun gnus-summary-tick-above (&optional score)
9028   "Tick all articles with score above SCORE."
9029   (interactive "P")
9030   (gnus-summary-mark-above score gnus-ticked-mark))
9031
9032 (defun gnus-summary-mark-above (score mark)
9033   "Mark articles with score over SCORE with MARK."
9034   (interactive "P\ncMark: ")
9035   (setq score (if score
9036                   (prefix-numeric-value score)
9037                 (or gnus-summary-default-score 0)))
9038   (save-excursion
9039     (set-buffer gnus-summary-buffer)
9040     (goto-char (point-min))
9041     (while (and (progn
9042                   (when (> (gnus-summary-article-score) score)
9043                     (gnus-summary-mark-article nil mark))
9044                   t)
9045                 (gnus-summary-find-next)))))
9046
9047 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9048 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9049 (defun gnus-summary-limit-include-expunged (&optional no-error)
9050   "Display all the hidden articles that were expunged for low scores."
9051   (interactive)
9052   (let ((buffer-read-only nil))
9053     (let ((scored gnus-newsgroup-scored)
9054           headers h)
9055       (while scored
9056         (unless (gnus-summary-article-header (caar scored))
9057           (and (setq h (gnus-number-to-header (caar scored)))
9058                (< (cdar scored) gnus-summary-expunge-below)
9059                (push h headers)))
9060         (setq scored (cdr scored)))
9061       (if (not headers)
9062           (when (not no-error)
9063             (error "No expunged articles hidden"))
9064         (goto-char (point-min))
9065         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9066         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9067         (mapcar (lambda (x) (push (mail-header-number x) 
9068                                   gnus-newsgroup-limit))
9069                 headers)
9070         (gnus-summary-prepare-unthreaded (nreverse headers))
9071         (goto-char (point-min))
9072         (gnus-summary-position-point)
9073         t))))
9074
9075 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
9076   "Mark all unread articles in this newsgroup as read.
9077 If prefix argument ALL is non-nil, ticked and dormant articles will
9078 also be marked as read.
9079 If QUIETLY is non-nil, no questions will be asked.
9080 If TO-HERE is non-nil, it should be a point in the buffer.  All
9081 articles before this point will be marked as read.
9082 Note that this function will only catch up the unread article
9083 in the current summary buffer limitation.
9084 The number of articles marked as read is returned."
9085   (interactive "P")
9086   (prog1
9087       (save-excursion
9088         (when (or quietly
9089                   (not gnus-interactive-catchup) ;Without confirmation?
9090                   gnus-expert-user
9091                   (gnus-y-or-n-p
9092                    (if all
9093                        "Mark absolutely all articles as read? "
9094                      "Mark all unread articles as read? ")))
9095           (if (and not-mark
9096                    (not gnus-newsgroup-adaptive)
9097                    (not gnus-newsgroup-auto-expire)
9098                    (not gnus-suppress-duplicates)
9099                    (or (not gnus-use-cache)
9100                        (eq gnus-use-cache 'passive)))
9101               (progn
9102                 (when all
9103                   (setq gnus-newsgroup-marked nil
9104                         gnus-newsgroup-dormant nil))
9105                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9106             ;; We actually mark all articles as canceled, which we
9107             ;; have to do when using auto-expiry or adaptive scoring.
9108             (gnus-summary-show-all-threads)
9109             (when (gnus-summary-first-subject (not all) t)
9110               (while (and
9111                       (if to-here (< (point) to-here) t)
9112                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
9113                       (gnus-summary-find-next (not all) nil nil t))))
9114             (gnus-set-mode-line 'summary))
9115           t))
9116     (gnus-summary-position-point)))
9117
9118 (defun gnus-summary-catchup-to-here (&optional all)
9119   "Mark all unticked articles before the current one as read.
9120 If ALL is non-nil, also mark ticked and dormant articles as read."
9121   (interactive "P")
9122   (save-excursion
9123     (gnus-save-hidden-threads
9124       (let ((beg (point)))
9125         ;; We check that there are unread articles.
9126         (when (or all (gnus-summary-find-prev))
9127           (gnus-summary-catchup all t beg)))))
9128   (gnus-summary-position-point))
9129
9130 (defun gnus-summary-catchup-all (&optional quietly)
9131   "Mark all articles in this newsgroup as read."
9132   (interactive "P")
9133   (gnus-summary-catchup t quietly))
9134
9135 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9136   "Mark all unread articles in this group as read, then exit.
9137 If prefix argument ALL is non-nil, all articles are marked as read.
9138 If QUIETLY is non-nil, no questions will be asked."
9139   (interactive "P")
9140   (when (gnus-summary-catchup all quietly nil 'fast)
9141     ;; Select next newsgroup or exit.
9142     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9143              (eq gnus-auto-select-next 'quietly))
9144         (gnus-summary-next-group nil)
9145       (gnus-summary-exit))))
9146
9147 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9148   "Mark all articles in this newsgroup as read, and then exit."
9149   (interactive "P")
9150   (gnus-summary-catchup-and-exit t quietly))
9151
9152 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9153   "Mark all articles in this group as read and select the next group.
9154 If given a prefix, mark all articles, unread as well as ticked, as
9155 read."
9156   (interactive "P")
9157   (save-excursion
9158     (gnus-summary-catchup all))
9159   (gnus-summary-next-group))
9160
9161 ;;;
9162 ;;; with article
9163 ;;;
9164
9165 (defmacro gnus-with-article (article &rest forms)
9166   "Select ARTICLE and perform FORMS in the original article buffer.
9167 Then replace the article with the result."
9168   `(progn
9169      ;; We don't want the article to be marked as read.
9170      (let (gnus-mark-article-hook)
9171        (gnus-summary-select-article t t nil ,article))
9172      (set-buffer gnus-original-article-buffer)
9173      ,@forms
9174      (if (not (gnus-check-backend-function
9175                'request-replace-article (car gnus-article-current)))
9176          (gnus-message 5 "Read-only group; not replacing")
9177        (unless (gnus-request-replace-article
9178                 ,article (car gnus-article-current)
9179                 (current-buffer) t)
9180          (error "Couldn't replace article")))
9181      ;; The cache and backlog have to be flushed somewhat.
9182      (when gnus-keep-backlog
9183        (gnus-backlog-remove-article
9184         (car gnus-article-current) (cdr gnus-article-current)))
9185      (when gnus-use-cache
9186        (gnus-cache-update-article
9187         (car gnus-article-current) (cdr gnus-article-current)))))
9188
9189 (put 'gnus-with-article 'lisp-indent-function 1)
9190 (put 'gnus-with-article 'edebug-form-spec '(form body))
9191
9192 ;; Thread-based commands.
9193
9194 (defun gnus-summary-articles-in-thread (&optional article)
9195   "Return a list of all articles in the current thread.
9196 If ARTICLE is non-nil, return all articles in the thread that starts
9197 with that article."
9198   (let* ((article (or article (gnus-summary-article-number)))
9199          (data (gnus-data-find-list article))
9200          (top-level (gnus-data-level (car data)))
9201          (top-subject
9202           (cond ((null gnus-thread-operation-ignore-subject)
9203                  (gnus-simplify-subject-re
9204                   (mail-header-subject (gnus-data-header (car data)))))
9205                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9206                  (gnus-simplify-subject-fuzzy
9207                   (mail-header-subject (gnus-data-header (car data)))))
9208                 (t nil)))
9209          (end-point (save-excursion
9210                       (if (gnus-summary-go-to-next-thread)
9211                           (point) (point-max))))
9212          articles)
9213     (while (and data
9214                 (< (gnus-data-pos (car data)) end-point))
9215       (when (or (not top-subject)
9216                 (string= top-subject
9217                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9218                              (gnus-simplify-subject-fuzzy
9219                               (mail-header-subject
9220                                (gnus-data-header (car data))))
9221                            (gnus-simplify-subject-re
9222                             (mail-header-subject
9223                              (gnus-data-header (car data)))))))
9224         (push (gnus-data-number (car data)) articles))
9225       (unless (and (setq data (cdr data))
9226                    (> (gnus-data-level (car data)) top-level))
9227         (setq data nil)))
9228     ;; Return the list of articles.
9229     (nreverse articles)))
9230
9231 (defun gnus-summary-rethread-current ()
9232   "Rethread the thread the current article is part of."
9233   (interactive)
9234   (let* ((gnus-show-threads t)
9235          (article (gnus-summary-article-number))
9236          (id (mail-header-id (gnus-summary-article-header)))
9237          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9238     (unless id
9239       (error "No article on the current line"))
9240     (gnus-rebuild-thread id)
9241     (gnus-summary-goto-subject article)))
9242
9243 (defun gnus-summary-reparent-thread ()
9244   "Make the current article child of the marked (or previous) article.
9245
9246 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9247 is non-nil or the Subject: of both articles are the same."
9248   (interactive)
9249   (unless (not (gnus-group-read-only-p))
9250     (error "The current newsgroup does not support article editing"))
9251   (unless (<= (length gnus-newsgroup-processable) 1)
9252     (error "No more than one article may be marked"))
9253   (save-window-excursion
9254     (let ((gnus-article-buffer " *reparent*")
9255           (current-article (gnus-summary-article-number))
9256           ;; First grab the marked article, otherwise one line up.
9257           (parent-article (if (not (null gnus-newsgroup-processable))
9258                               (car gnus-newsgroup-processable)
9259                             (save-excursion
9260                               (if (eq (forward-line -1) 0)
9261                                   (gnus-summary-article-number)
9262                                 (error "Beginning of summary buffer"))))))
9263       (unless (not (eq current-article parent-article))
9264         (error "An article may not be self-referential"))
9265       (let ((message-id (mail-header-id
9266                          (gnus-summary-article-header parent-article))))
9267         (unless (and message-id (not (equal message-id "")))
9268           (error "No message-id in desired parent"))
9269         (gnus-with-article current-article
9270           (save-restriction
9271             (goto-char (point-min))
9272             (message-narrow-to-head)
9273             (if (re-search-forward "^References: " nil t)
9274                 (progn
9275                   (re-search-forward "^[^ \t]" nil t)
9276                   (forward-line -1)
9277                   (end-of-line)
9278                   (insert " " message-id))
9279               (insert "References: " message-id "\n"))))
9280         (set-buffer gnus-summary-buffer)
9281         (gnus-summary-unmark-all-processable)
9282         (gnus-summary-update-article current-article)
9283         (gnus-summary-rethread-current)
9284         (gnus-message 3 "Article %d is now the child of article %d"
9285                       current-article parent-article)))))
9286
9287 (defun gnus-summary-toggle-threads (&optional arg)
9288   "Toggle showing conversation threads.
9289 If ARG is positive number, turn showing conversation threads on."
9290   (interactive "P")
9291   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9292     (setq gnus-show-threads
9293           (if (null arg) (not gnus-show-threads)
9294             (> (prefix-numeric-value arg) 0)))
9295     (gnus-summary-prepare)
9296     (gnus-summary-goto-subject current)
9297     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9298     (gnus-summary-position-point)))
9299
9300 (defun gnus-summary-show-all-threads ()
9301   "Show all threads."
9302   (interactive)
9303   (save-excursion
9304     (let ((buffer-read-only nil))
9305       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9306   (gnus-summary-position-point))
9307
9308 (defun gnus-summary-show-thread ()
9309   "Show thread subtrees.
9310 Returns nil if no thread was there to be shown."
9311   (interactive)
9312   (let ((buffer-read-only nil)
9313         (orig (point))
9314         ;; first goto end then to beg, to have point at beg after let
9315         (end (progn (end-of-line) (point)))
9316         (beg (progn (beginning-of-line) (point))))
9317     (prog1
9318         ;; Any hidden lines here?
9319         (search-forward "\r" end t)
9320       (subst-char-in-region beg end ?\^M ?\n t)
9321       (goto-char orig)
9322       (gnus-summary-position-point))))
9323
9324 (defun gnus-summary-hide-all-threads ()
9325   "Hide all thread subtrees."
9326   (interactive)
9327   (save-excursion
9328     (goto-char (point-min))
9329     (gnus-summary-hide-thread)
9330     (while (zerop (gnus-summary-next-thread 1 t))
9331       (gnus-summary-hide-thread)))
9332   (gnus-summary-position-point))
9333
9334 (defun gnus-summary-hide-thread ()
9335   "Hide thread subtrees.
9336 Returns nil if no threads were there to be hidden."
9337   (interactive)
9338   (let ((buffer-read-only nil)
9339         (start (point))
9340         (article (gnus-summary-article-number)))
9341     (goto-char start)
9342     ;; Go forward until either the buffer ends or the subthread
9343     ;; ends.
9344     (when (and (not (eobp))
9345                (or (zerop (gnus-summary-next-thread 1 t))
9346                    (goto-char (point-max))))
9347       (prog1
9348           (if (and (> (point) start)
9349                    (search-backward "\n" start t))
9350               (progn
9351                 (subst-char-in-region start (point) ?\n ?\^M)
9352                 (gnus-summary-goto-subject article))
9353             (goto-char start)
9354             nil)))))
9355
9356 (defun gnus-summary-go-to-next-thread (&optional previous)
9357   "Go to the same level (or less) next thread.
9358 If PREVIOUS is non-nil, go to previous thread instead.
9359 Return the article number moved to, or nil if moving was impossible."
9360   (let ((level (gnus-summary-thread-level))
9361         (way (if previous -1 1))
9362         (beg (point)))
9363     (forward-line way)
9364     (while (and (not (eobp))
9365                 (< level (gnus-summary-thread-level)))
9366       (forward-line way))
9367     (if (eobp)
9368         (progn
9369           (goto-char beg)
9370           nil)
9371       (setq beg (point))
9372       (prog1
9373           (gnus-summary-article-number)
9374         (goto-char beg)))))
9375
9376 (defun gnus-summary-next-thread (n &optional silent)
9377   "Go to the same level next N'th thread.
9378 If N is negative, search backward instead.
9379 Returns the difference between N and the number of skips actually
9380 done.
9381
9382 If SILENT, don't output messages."
9383   (interactive "p")
9384   (let ((backward (< n 0))
9385         (n (abs n)))
9386     (while (and (> n 0)
9387                 (gnus-summary-go-to-next-thread backward))
9388       (decf n))
9389     (unless silent
9390       (gnus-summary-position-point))
9391     (when (and (not silent) (/= 0 n))
9392       (gnus-message 7 "No more threads"))
9393     n))
9394
9395 (defun gnus-summary-prev-thread (n)
9396   "Go to the same level previous N'th thread.
9397 Returns the difference between N and the number of skips actually
9398 done."
9399   (interactive "p")
9400   (gnus-summary-next-thread (- n)))
9401
9402 (defun gnus-summary-go-down-thread ()
9403   "Go down one level in the current thread."
9404   (let ((children (gnus-summary-article-children)))
9405     (when children
9406       (gnus-summary-goto-subject (car children)))))
9407
9408 (defun gnus-summary-go-up-thread ()
9409   "Go up one level in the current thread."
9410   (let ((parent (gnus-summary-article-parent)))
9411     (when parent
9412       (gnus-summary-goto-subject parent))))
9413
9414 (defun gnus-summary-down-thread (n)
9415   "Go down thread N steps.
9416 If N is negative, go up instead.
9417 Returns the difference between N and how many steps down that were
9418 taken."
9419   (interactive "p")
9420   (let ((up (< n 0))
9421         (n (abs n)))
9422     (while (and (> n 0)
9423                 (if up (gnus-summary-go-up-thread)
9424                   (gnus-summary-go-down-thread)))
9425       (setq n (1- n)))
9426     (gnus-summary-position-point)
9427     (when (/= 0 n)
9428       (gnus-message 7 "Can't go further"))
9429     n))
9430
9431 (defun gnus-summary-up-thread (n)
9432   "Go up thread N steps.
9433 If N is negative, go down instead.
9434 Returns the difference between N and how many steps down that were
9435 taken."
9436   (interactive "p")
9437   (gnus-summary-down-thread (- n)))
9438
9439 (defun gnus-summary-top-thread ()
9440   "Go to the top of the thread."
9441   (interactive)
9442   (while (gnus-summary-go-up-thread))
9443   (gnus-summary-article-number))
9444
9445 (defun gnus-summary-kill-thread (&optional unmark)
9446   "Mark articles under current thread as read.
9447 If the prefix argument is positive, remove any kinds of marks.
9448 If the prefix argument is negative, tick articles instead."
9449   (interactive "P")
9450   (when unmark
9451     (setq unmark (prefix-numeric-value unmark)))
9452   (let ((articles (gnus-summary-articles-in-thread)))
9453     (save-excursion
9454       ;; Expand the thread.
9455       (gnus-summary-show-thread)
9456       ;; Mark all the articles.
9457       (while articles
9458         (gnus-summary-goto-subject (car articles))
9459         (cond ((null unmark)
9460                (gnus-summary-mark-article-as-read gnus-killed-mark))
9461               ((> unmark 0)
9462                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9463               (t
9464                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9465         (setq articles (cdr articles))))
9466     ;; Hide killed subtrees.
9467     (and (null unmark)
9468          gnus-thread-hide-killed
9469          (gnus-summary-hide-thread))
9470     ;; If marked as read, go to next unread subject.
9471     (when (null unmark)
9472       ;; Go to next unread subject.
9473       (gnus-summary-next-subject 1 t)))
9474   (gnus-set-mode-line 'summary))
9475
9476 ;; Summary sorting commands
9477
9478 (defun gnus-summary-sort-by-number (&optional reverse)
9479   "Sort the summary buffer by article number.
9480 Argument REVERSE means reverse order."
9481   (interactive "P")
9482   (gnus-summary-sort 'number reverse))
9483
9484 (defun gnus-summary-sort-by-author (&optional reverse)
9485   "Sort the summary buffer by author name alphabetically.
9486 If `case-fold-search' is non-nil, case of letters is ignored.
9487 Argument REVERSE means reverse order."
9488   (interactive "P")
9489   (gnus-summary-sort 'author reverse))
9490
9491 (defun gnus-summary-sort-by-subject (&optional reverse)
9492   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9493 If `case-fold-search' is non-nil, case of letters is ignored.
9494 Argument REVERSE means reverse order."
9495   (interactive "P")
9496   (gnus-summary-sort 'subject reverse))
9497
9498 (defun gnus-summary-sort-by-date (&optional reverse)
9499   "Sort the summary buffer by date.
9500 Argument REVERSE means reverse order."
9501   (interactive "P")
9502   (gnus-summary-sort 'date reverse))
9503
9504 (defun gnus-summary-sort-by-score (&optional reverse)
9505   "Sort the summary buffer by score.
9506 Argument REVERSE means reverse order."
9507   (interactive "P")
9508   (gnus-summary-sort 'score reverse))
9509
9510 (defun gnus-summary-sort-by-lines (&optional reverse)
9511   "Sort the summary buffer by the number of lines.
9512 Argument REVERSE means reverse order."
9513   (interactive "P")
9514   (gnus-summary-sort 'lines reverse))
9515
9516 (defun gnus-summary-sort-by-chars (&optional reverse)
9517   "Sort the summary buffer by article length.
9518 Argument REVERSE means reverse order."
9519   (interactive "P")
9520   (gnus-summary-sort 'chars reverse))
9521
9522 (defun gnus-summary-sort-by-original (&optional reverse)
9523   "Sort the summary buffer using the default sorting method.
9524 Argument REVERSE means reverse order."
9525   (interactive "P")
9526   (let* ((buffer-read-only)
9527          (gnus-summary-prepare-hook nil))
9528     ;; We do the sorting by regenerating the threads.
9529     (gnus-summary-prepare)
9530     ;; Hide subthreads if needed.
9531     (when (and gnus-show-threads gnus-thread-hide-subtree)
9532       (gnus-summary-hide-all-threads))))
9533
9534 (defun gnus-summary-sort (predicate reverse)
9535   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9536   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9537          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9538          (gnus-thread-sort-functions
9539           (if (not reverse)
9540               thread
9541             `(lambda (t1 t2)
9542                (,thread t2 t1))))
9543          (gnus-sort-gathered-threads-function
9544           gnus-thread-sort-functions)
9545          (gnus-article-sort-functions
9546           (if (not reverse)
9547               article
9548             `(lambda (t1 t2)
9549                (,article t2 t1))))
9550          (buffer-read-only)
9551          (gnus-summary-prepare-hook nil))
9552     ;; We do the sorting by regenerating the threads.
9553     (gnus-summary-prepare)
9554     ;; Hide subthreads if needed.
9555     (when (and gnus-show-threads gnus-thread-hide-subtree)
9556       (gnus-summary-hide-all-threads))))
9557
9558 ;; Summary saving commands.
9559
9560 (defun gnus-summary-save-article (&optional n not-saved)
9561   "Save the current article using the default saver function.
9562 If N is a positive number, save the N next articles.
9563 If N is a negative number, save the N previous articles.
9564 If N is nil and any articles have been marked with the process mark,
9565 save those articles instead.
9566 The variable `gnus-default-article-saver' specifies the saver function."
9567   (interactive "P")
9568   (let* ((articles (gnus-summary-work-articles n))
9569          (save-buffer (save-excursion
9570                         (nnheader-set-temp-buffer " *Gnus Save*")))
9571          (num (length articles))
9572          header file)
9573     (dolist (article articles)
9574       (setq header (gnus-summary-article-header article))
9575       (if (not (vectorp header))
9576           ;; This is a pseudo-article.
9577           (if (assq 'name header)
9578               (gnus-copy-file (cdr (assq 'name header)))
9579             (gnus-message 1 "Article %d is unsaveable" article))
9580         ;; This is a real article.
9581         (save-window-excursion
9582           (gnus-summary-select-article t nil nil article))
9583         (save-excursion
9584           (set-buffer save-buffer)
9585           (erase-buffer)
9586           (insert-buffer-substring gnus-original-article-buffer))
9587         (setq file (gnus-article-save save-buffer file num))
9588         (gnus-summary-remove-process-mark article)
9589         (unless not-saved
9590           (gnus-summary-set-saved-mark article))))
9591     (gnus-kill-buffer save-buffer)
9592     (gnus-summary-position-point)
9593     (gnus-set-mode-line 'summary)
9594     n))
9595
9596 (defun gnus-summary-pipe-output (&optional arg)
9597   "Pipe the current article to a subprocess.
9598 If N is a positive number, pipe the N next articles.
9599 If N is a negative number, pipe the N previous articles.
9600 If N is nil and any articles have been marked with the process mark,
9601 pipe those articles instead."
9602   (interactive "P")
9603   (require 'gnus-art)
9604   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9605     (gnus-summary-save-article arg t))
9606   (gnus-configure-windows 'pipe))
9607
9608 (defun gnus-summary-save-article-mail (&optional arg)
9609   "Append the current article to an mail file.
9610 If N is a positive number, save the N next articles.
9611 If N is a negative number, save the N previous articles.
9612 If N is nil and any articles have been marked with the process mark,
9613 save those articles instead."
9614   (interactive "P")
9615   (require 'gnus-art)
9616   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9617     (gnus-summary-save-article arg)))
9618
9619 (defun gnus-summary-save-article-rmail (&optional arg)
9620   "Append the current article to an rmail file.
9621 If N is a positive number, save the N next articles.
9622 If N is a negative number, save the N previous articles.
9623 If N is nil and any articles have been marked with the process mark,
9624 save those articles instead."
9625   (interactive "P")
9626   (require 'gnus-art)
9627   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9628     (gnus-summary-save-article arg)))
9629
9630 (defun gnus-summary-save-article-file (&optional arg)
9631   "Append the current article to a file.
9632 If N is a positive number, save the N next articles.
9633 If N is a negative number, save the N previous articles.
9634 If N is nil and any articles have been marked with the process mark,
9635 save those articles instead."
9636   (interactive "P")
9637   (require 'gnus-art)
9638   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9639     (gnus-summary-save-article arg)))
9640
9641 (defun gnus-summary-write-article-file (&optional arg)
9642   "Write the current article to a file, deleting the previous file.
9643 If N is a positive number, save the N next articles.
9644 If N is a negative number, save the N previous articles.
9645 If N is nil and any articles have been marked with the process mark,
9646 save those articles instead."
9647   (interactive "P")
9648   (require 'gnus-art)
9649   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9650     (gnus-summary-save-article arg)))
9651
9652 (defun gnus-summary-save-article-body-file (&optional arg)
9653   "Append the current article body to a file.
9654 If N is a positive number, save the N next articles.
9655 If N is a negative number, save the N previous articles.
9656 If N is nil and any articles have been marked with the process mark,
9657 save those articles instead."
9658   (interactive "P")
9659   (require 'gnus-art)
9660   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9661     (gnus-summary-save-article arg)))
9662
9663 (defun gnus-summary-pipe-message (program)
9664   "Pipe the current article through PROGRAM."
9665   (interactive "sProgram: ")
9666   (gnus-summary-select-article)
9667   (let ((mail-header-separator ""))
9668     (gnus-eval-in-buffer-window gnus-article-buffer
9669       (save-restriction
9670         (widen)
9671         (let ((start (window-start))
9672               buffer-read-only)
9673           (message-pipe-buffer-body program)
9674           (set-window-start (get-buffer-window (current-buffer)) start))))))
9675
9676 (defun gnus-get-split-value (methods)
9677   "Return a value based on the split METHODS."
9678   (let (split-name method result match)
9679     (when methods
9680       (save-excursion
9681         (set-buffer gnus-original-article-buffer)
9682         (save-restriction
9683           (nnheader-narrow-to-headers)
9684           (while (and methods (not split-name))
9685             (goto-char (point-min))
9686             (setq method (pop methods))
9687             (setq match (car method))
9688             (when (cond
9689                    ((stringp match)
9690                     ;; Regular expression.
9691                     (ignore-errors
9692                       (re-search-forward match nil t)))
9693                    ((gnus-functionp match)
9694                     ;; Function.
9695                     (save-restriction
9696                       (widen)
9697                       (setq result (funcall match gnus-newsgroup-name))))
9698                    ((consp match)
9699                     ;; Form.
9700                     (save-restriction
9701                       (widen)
9702                       (setq result (eval match)))))
9703               (setq split-name (cdr method))
9704               (cond ((stringp result)
9705                      (push (expand-file-name
9706                             result gnus-article-save-directory)
9707                            split-name))
9708                     ((consp result)
9709                      (setq split-name (append result split-name)))))))))
9710     (nreverse split-name)))
9711
9712 (defun gnus-valid-move-group-p (group)
9713   (and (boundp group)
9714        (symbol-name group)
9715        (symbol-value group)
9716        (gnus-get-function (gnus-find-method-for-group
9717                            (symbol-name group)) 'request-accept-article t)))
9718
9719 (defun gnus-read-move-group-name (prompt default articles prefix)
9720   "Read a group name."
9721   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9722          (minibuffer-confirm-incomplete nil) ; XEmacs
9723          (prom
9724           (format "%s %s to:"
9725                   prompt
9726                   (if (> (length articles) 1)
9727                       (format "these %d articles" (length articles))
9728                     "this article")))
9729          (to-newsgroup
9730           (cond
9731            ((null split-name)
9732             (gnus-completing-read default prom
9733                                   gnus-active-hashtb
9734                                   'gnus-valid-move-group-p
9735                                   nil prefix
9736                                   'gnus-group-history))
9737            ((= 1 (length split-name))
9738             (gnus-completing-read (car split-name) prom
9739                                   gnus-active-hashtb
9740                                   'gnus-valid-move-group-p
9741                                   nil nil
9742                                   'gnus-group-history))
9743            (t
9744             (gnus-completing-read nil prom
9745                                   (mapcar (lambda (el) (list el))
9746                                           (nreverse split-name))
9747                                   nil nil nil
9748                                   'gnus-group-history))))
9749          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9750     (when to-newsgroup
9751       (if (or (string= to-newsgroup "")
9752               (string= to-newsgroup prefix))
9753           (setq to-newsgroup default))
9754       (unless to-newsgroup
9755         (error "No group name entered"))
9756       (or (gnus-active to-newsgroup)
9757           (gnus-activate-group to-newsgroup nil nil to-method)
9758           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9759                                      to-newsgroup))
9760               (or (and (gnus-request-create-group to-newsgroup to-method)
9761                        (gnus-activate-group
9762                         to-newsgroup nil nil to-method)
9763                        (gnus-subscribe-group to-newsgroup))
9764                   (error "Couldn't create group %s" to-newsgroup)))
9765           (error "No such group: %s" to-newsgroup)))
9766     to-newsgroup))
9767
9768 (defun gnus-summary-save-parts (type dir n &optional reverse)
9769   "Save parts matching TYPE to DIR.
9770 If REVERSE, save parts that do not match TYPE."
9771   (interactive
9772    (list (read-string "Save parts of type: "
9773                       (or (car gnus-summary-save-parts-type-history)
9774                           gnus-summary-save-parts-default-mime)
9775                       'gnus-summary-save-parts-type-history)
9776          (setq gnus-summary-save-parts-last-directory
9777                (read-file-name "Save to directory: "
9778                                gnus-summary-save-parts-last-directory
9779                                nil t))
9780          current-prefix-arg))
9781   (gnus-summary-iterate n
9782     (let ((gnus-display-mime-function nil)
9783           (gnus-inhibit-treatment t))
9784       (gnus-summary-select-article))
9785     (save-excursion
9786       (set-buffer gnus-article-buffer)
9787       (let ((handles (or gnus-article-mime-handles
9788                          (mm-dissect-buffer) (mm-uu-dissect))))
9789         (when handles
9790           (gnus-summary-save-parts-1 type dir handles reverse)
9791           (unless gnus-article-mime-handles ;; Don't destroy this case.
9792             (mm-destroy-parts handles)))))))
9793
9794 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9795   (if (stringp (car handle))
9796       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9797               (cdr handle))
9798     (when (if reverse
9799               (not (string-match type (mm-handle-media-type handle)))
9800             (string-match type (mm-handle-media-type handle)))
9801       (let ((file (expand-file-name
9802                    (file-name-nondirectory
9803                     (or
9804                      (mail-content-type-get
9805                       (mm-handle-disposition handle) 'filename)
9806                      (concat gnus-newsgroup-name
9807                              "." (number-to-string
9808                                   (cdr gnus-article-current)))))
9809                    dir)))
9810         (unless (file-exists-p file)
9811           (mm-save-part-to-file handle file))))))
9812
9813 ;; Summary extract commands
9814
9815 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9816   (let ((buffer-read-only nil)
9817         (article (gnus-summary-article-number))
9818         after-article b e)
9819     (unless (gnus-summary-goto-subject article)
9820       (error "No such article: %d" article))
9821     (gnus-summary-position-point)
9822     ;; If all commands are to be bunched up on one line, we collect
9823     ;; them here.
9824     (unless gnus-view-pseudos-separately
9825       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9826             files action)
9827         (while ps
9828           (setq action (cdr (assq 'action (car ps))))
9829           (setq files (list (cdr (assq 'name (car ps)))))
9830           (while (and ps (cdr ps)
9831                       (string= (or action "1")
9832                                (or (cdr (assq 'action (cadr ps))) "2")))
9833             (push (cdr (assq 'name (cadr ps))) files)
9834             (setcdr ps (cddr ps)))
9835           (when files
9836             (when (not (string-match "%s" action))
9837               (push " " files))
9838             (push " " files)
9839             (when (assq 'execute (car ps))
9840               (setcdr (assq 'execute (car ps))
9841                       (funcall (if (string-match "%s" action)
9842                                    'format 'concat)
9843                                action
9844                                (mapconcat
9845                                 (lambda (f)
9846                                   (if (equal f " ")
9847                                       f
9848                                     (gnus-quote-arg-for-sh-or-csh f)))
9849                                 files " ")))))
9850           (setq ps (cdr ps)))))
9851     (if (and gnus-view-pseudos (not not-view))
9852         (while pslist
9853           (when (assq 'execute (car pslist))
9854             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9855                                   (eq gnus-view-pseudos 'not-confirm)))
9856           (setq pslist (cdr pslist)))
9857       (save-excursion
9858         (while pslist
9859           (setq after-article (or (cdr (assq 'article (car pslist)))
9860                                   (gnus-summary-article-number)))
9861           (gnus-summary-goto-subject after-article)
9862           (forward-line 1)
9863           (setq b (point))
9864           (insert "    " (file-name-nondirectory
9865                           (cdr (assq 'name (car pslist))))
9866                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9867           (setq e (point))
9868           (forward-line -1)             ; back to `b'
9869           (gnus-add-text-properties
9870            b (1- e) (list 'gnus-number gnus-reffed-article-number
9871                           gnus-mouse-face-prop gnus-mouse-face))
9872           (gnus-data-enter
9873            after-article gnus-reffed-article-number
9874            gnus-unread-mark b (car pslist) 0 (- e b))
9875           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9876           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9877           (setq pslist (cdr pslist)))))))
9878
9879 (defun gnus-pseudos< (p1 p2)
9880   (let ((c1 (cdr (assq 'action p1)))
9881         (c2 (cdr (assq 'action p2))))
9882     (and c1 c2 (string< c1 c2))))
9883
9884 (defun gnus-request-pseudo-article (props)
9885   (cond ((assq 'execute props)
9886          (gnus-execute-command (cdr (assq 'execute props)))))
9887   (let ((gnus-current-article (gnus-summary-article-number)))
9888     (gnus-run-hooks 'gnus-mark-article-hook)))
9889
9890 (defun gnus-execute-command (command &optional automatic)
9891   (save-excursion
9892     (gnus-article-setup-buffer)
9893     (set-buffer gnus-article-buffer)
9894     (setq buffer-read-only nil)
9895     (let ((command (if automatic command
9896                      (read-string "Command: " (cons command 0)))))
9897       (erase-buffer)
9898       (insert "$ " command "\n\n")
9899       (if gnus-view-pseudo-asynchronously
9900           (start-process "gnus-execute" (current-buffer) shell-file-name
9901                          shell-command-switch command)
9902         (call-process shell-file-name nil t nil
9903                       shell-command-switch command)))))
9904
9905 ;; Summary kill commands.
9906
9907 (defun gnus-summary-edit-global-kill (article)
9908   "Edit the \"global\" kill file."
9909   (interactive (list (gnus-summary-article-number)))
9910   (gnus-group-edit-global-kill article))
9911
9912 (defun gnus-summary-edit-local-kill ()
9913   "Edit a local kill file applied to the current newsgroup."
9914   (interactive)
9915   (setq gnus-current-headers (gnus-summary-article-header))
9916   (gnus-group-edit-local-kill
9917    (gnus-summary-article-number) gnus-newsgroup-name))
9918
9919 ;;; Header reading.
9920
9921 (defun gnus-read-header (id &optional header)
9922   "Read the headers of article ID and enter them into the Gnus system."
9923   (let ((group gnus-newsgroup-name)
9924         (gnus-override-method
9925          (or
9926           gnus-override-method
9927           (and (gnus-news-group-p gnus-newsgroup-name)
9928                (car (gnus-refer-article-methods)))))
9929         where)
9930     ;; First we check to see whether the header in question is already
9931     ;; fetched.
9932     (if (stringp id)
9933         ;; This is a Message-ID.
9934         (setq header (or header (gnus-id-to-header id)))
9935       ;; This is an article number.
9936       (setq header (or header (gnus-summary-article-header id))))
9937     (if (and header
9938              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9939         ;; We have found the header.
9940         header
9941       ;; If this is a sparse article, we have to nix out its
9942       ;; previous entry in the thread hashtb.
9943       (when (and header
9944                  (gnus-summary-article-sparse-p (mail-header-number header)))
9945         (let* ((parent (gnus-parent-id (mail-header-references header)))
9946                (thread (and parent (gnus-id-to-thread parent))))
9947           (when thread
9948             (delq (assq header thread) thread))))
9949       ;; We have to really fetch the header to this article.
9950       (save-excursion
9951         (set-buffer nntp-server-buffer)
9952         (when (setq where (gnus-request-head id group))
9953           (nnheader-fold-continuation-lines)
9954           (goto-char (point-max))
9955           (insert ".\n")
9956           (goto-char (point-min))
9957           (insert "211 ")
9958           (princ (cond
9959                   ((numberp id) id)
9960                   ((cdr where) (cdr where))
9961                   (header (mail-header-number header))
9962                   (t gnus-reffed-article-number))
9963                  (current-buffer))
9964           (insert " Article retrieved.\n"))
9965         (if (or (not where)
9966                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9967             ()                          ; Malformed head.
9968           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9969             (when (and (stringp id)
9970                        (not (string= (gnus-group-real-name group)
9971                                      (car where))))
9972               ;; If we fetched by Message-ID and the article came
9973               ;; from a different group, we fudge some bogus article
9974               ;; numbers for this article.
9975               (mail-header-set-number header gnus-reffed-article-number))
9976             (save-excursion
9977               (set-buffer gnus-summary-buffer)
9978               (decf gnus-reffed-article-number)
9979               (gnus-remove-header (mail-header-number header))
9980               (push header gnus-newsgroup-headers)
9981               (setq gnus-current-headers header)
9982               (push (mail-header-number header) gnus-newsgroup-limit)))
9983           header)))))
9984
9985 (defun gnus-remove-header (number)
9986   "Remove header NUMBER from `gnus-newsgroup-headers'."
9987   (if (and gnus-newsgroup-headers
9988            (= number (mail-header-number (car gnus-newsgroup-headers))))
9989       (pop gnus-newsgroup-headers)
9990     (let ((headers gnus-newsgroup-headers))
9991       (while (and (cdr headers)
9992                   (not (= number (mail-header-number (cadr headers)))))
9993         (pop headers))
9994       (when (cdr headers)
9995         (setcdr headers (cddr headers))))))
9996
9997 ;;;
9998 ;;; summary highlights
9999 ;;;
10000
10001 (defun gnus-highlight-selected-summary ()
10002   "Highlight selected article in summary buffer."
10003   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10004   (when gnus-summary-selected-face
10005     (save-excursion
10006       (let* ((beg (progn (beginning-of-line) (point)))
10007              (end (progn (end-of-line) (point)))
10008              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10009              (from (if (get-text-property beg gnus-mouse-face-prop)
10010                        beg
10011                      (or (next-single-property-change
10012                           beg gnus-mouse-face-prop nil end)
10013                          beg)))
10014              (to
10015               (if (= from end)
10016                   (- from 2)
10017                 (or (next-single-property-change
10018                      from gnus-mouse-face-prop nil end)
10019                     end))))
10020         ;; If no mouse-face prop on line we will have to = from = end,
10021         ;; so we highlight the entire line instead.
10022         (when (= (+ to 2) from)
10023           (setq from beg)
10024           (setq to end))
10025         (if gnus-newsgroup-selected-overlay
10026             ;; Move old overlay.
10027             (gnus-move-overlay
10028              gnus-newsgroup-selected-overlay from to (current-buffer))
10029           ;; Create new overlay.
10030           (gnus-overlay-put
10031            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10032            'face gnus-summary-selected-face))))))
10033
10034 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10035 (defun gnus-summary-highlight-line ()
10036   "Highlight current line according to `gnus-summary-highlight'."
10037   (let* ((list gnus-summary-highlight)
10038          (p (point))
10039          (end (progn (end-of-line) (point)))
10040          ;; now find out where the line starts and leave point there.
10041          (beg (progn (beginning-of-line) (point)))
10042          (article (gnus-summary-article-number))
10043          (score (or (cdr (assq (or article gnus-current-article)
10044                                gnus-newsgroup-scored))
10045                     gnus-summary-default-score 0))
10046          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10047          (inhibit-read-only t))
10048     ;; Eval the cars of the lists until we find a match.
10049     (let ((default gnus-summary-default-score))
10050       (while (and list
10051                   (not (eval (caar list))))
10052         (setq list (cdr list))))
10053     (let ((face (cdar list)))
10054       (unless (eq face (get-text-property beg 'face))
10055         (gnus-put-text-property-excluding-characters-with-faces
10056          beg end 'face
10057          (setq face (if (boundp face) (symbol-value face) face)))
10058         (when gnus-summary-highlight-line-function
10059           (funcall gnus-summary-highlight-line-function article face))))
10060     (goto-char p)))
10061
10062 (defun gnus-update-read-articles (group unread &optional compute)
10063   "Update the list of read articles in GROUP."
10064   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10065          (entry (gnus-gethash group gnus-newsrc-hashtb))
10066          (info (nth 2 entry))
10067          (prev 1)
10068          (unread (sort (copy-sequence unread) '<))
10069          read)
10070     (if (or (not info) (not active))
10071         ;; There is no info on this group if it was, in fact,
10072         ;; killed.  Gnus stores no information on killed groups, so
10073         ;; there's nothing to be done.
10074         ;; One could store the information somewhere temporarily,
10075         ;; perhaps...  Hmmm...
10076         ()
10077       ;; Remove any negative articles numbers.
10078       (while (and unread (< (car unread) 0))
10079         (setq unread (cdr unread)))
10080       ;; Remove any expired article numbers
10081       (while (and unread (< (car unread) (car active)))
10082         (setq unread (cdr unread)))
10083       ;; Compute the ranges of read articles by looking at the list of
10084       ;; unread articles.
10085       (while unread
10086         (when (/= (car unread) prev)
10087           (push (if (= prev (1- (car unread))) prev
10088                   (cons prev (1- (car unread))))
10089                 read))
10090         (setq prev (1+ (car unread)))
10091         (setq unread (cdr unread)))
10092       (when (<= prev (cdr active))
10093         (push (cons prev (cdr active)) read))
10094       (setq read (if (> (length read) 1) (nreverse read) read))
10095       (if compute
10096           read
10097         (save-excursion
10098           (let (setmarkundo)
10099             ;; Propagate the read marks to the backend.
10100             (when (gnus-check-backend-function 'request-set-mark group)
10101               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10102                     (add (gnus-remove-from-range read (gnus-info-read info))))
10103                 (when (or add del)
10104                   (unless (gnus-check-group group)
10105                     (error "Can't open server for %s" group))
10106                   (gnus-request-set-mark
10107                    group (delq nil (list (if add (list add 'add '(read)))
10108                                          (if del (list del 'del '(read))))))
10109                   (setq setmarkundo
10110                         `(gnus-request-set-mark
10111                           ,group
10112                           ',(delq nil (list
10113                                        (if del (list del 'add '(read)))
10114                                        (if add (list add 'del '(read))))))))))
10115             (set-buffer gnus-group-buffer)
10116             (gnus-undo-register
10117               `(progn
10118                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10119                  (gnus-info-set-read ',info ',(gnus-info-read info))
10120                  (gnus-get-unread-articles-in-group ',info
10121                                                     (gnus-active ,group))
10122                  (gnus-group-update-group ,group t)
10123                  ,setmarkundo))))
10124         ;; Enter this list into the group info.
10125         (gnus-info-set-read info read)
10126         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10127         (gnus-get-unread-articles-in-group info (gnus-active group))
10128         t))))
10129
10130 (defun gnus-offer-save-summaries ()
10131   "Offer to save all active summary buffers."
10132   (save-excursion
10133     (let ((buflist (buffer-list))
10134           buffers bufname)
10135       ;; Go through all buffers and find all summaries.
10136       (while buflist
10137         (and (setq bufname (buffer-name (car buflist)))
10138              (string-match "Summary" bufname)
10139              (save-excursion
10140                (set-buffer bufname)
10141                ;; We check that this is, indeed, a summary buffer.
10142                (and (eq major-mode 'gnus-summary-mode)
10143                     ;; Also make sure this isn't bogus.
10144                     gnus-newsgroup-prepared
10145                     ;; Also make sure that this isn't a dead summary buffer.
10146                     (not gnus-dead-summary-mode)))
10147              (push bufname buffers))
10148         (setq buflist (cdr buflist)))
10149       ;; Go through all these summary buffers and offer to save them.
10150       (when buffers
10151         (map-y-or-n-p
10152          "Update summary buffer %s? "
10153          (lambda (buf)
10154            (switch-to-buffer buf)
10155            (gnus-summary-exit))
10156          buffers)))))
10157
10158
10159 ;;; @ for mime-partial
10160 ;;;
10161
10162 (defun gnus-request-partial-message ()
10163   (save-excursion
10164     (let ((number (gnus-summary-article-number))
10165           (group gnus-newsgroup-name)
10166           (mother gnus-article-buffer))
10167       (set-buffer (get-buffer-create " *Partial Article*"))
10168       (erase-buffer)
10169       (setq mime-preview-buffer mother)
10170       (gnus-request-article-this-buffer number group)
10171       (mime-parse-buffer)
10172       )))
10173
10174 (autoload 'mime-combine-message/partial-pieces-automatically
10175   "mime-partial"
10176   "Internal method to combine message/partial messages automatically.")
10177
10178 (mime-add-condition
10179  'action '((type . message)(subtype . partial)
10180            (major-mode . gnus-original-article-mode)
10181            (method . mime-combine-message/partial-pieces-automatically)
10182            (summary-buffer-exp . gnus-summary-buffer)
10183            (request-partial-message-method . gnus-request-partial-message)
10184            ))
10185
10186
10187 ;;; @ for message/rfc822
10188 ;;;
10189
10190 (defun gnus-mime-extract-message/rfc822 (entity situation)
10191   (let (group article num cwin swin cur)
10192     (with-temp-buffer
10193       (mime-insert-entity-content entity)
10194       (setq group (or (cdr (assq 'group situation))
10195                       (completing-read "Group: "
10196                                        gnus-active-hashtb
10197                                        nil
10198                                        (gnus-read-active-file-p)
10199                                        gnus-newsgroup-name))
10200             article (gnus-request-accept-article group)))
10201     (when (and (consp article)
10202                (numberp (setq article (cdr article))))
10203       (setq num (1+ (or (cdr (assq 'number situation)) 0))
10204             cwin (get-buffer-window (current-buffer) t))
10205       (save-window-excursion
10206         (if (setq swin (get-buffer-window gnus-summary-buffer t))
10207             (select-window swin)
10208           (set-buffer gnus-summary-buffer))
10209         (setq cur gnus-current-article)
10210         (forward-line num)
10211         (let (gnus-show-threads)
10212           (gnus-summary-goto-subject article t))
10213         (gnus-summary-clear-mark-forward 1)
10214         (gnus-summary-goto-subject cur))
10215       (when (and cwin (window-frame cwin))
10216         (select-frame (window-frame cwin)))
10217       (when (boundp 'mime-acting-situation-to-override)
10218         (set-alist 'mime-acting-situation-to-override
10219                    'group
10220                    group)
10221         (set-alist 'mime-acting-situation-to-override
10222                    'after-method
10223                    `(progn
10224                       (save-current-buffer
10225                         (set-buffer gnus-group-buffer)
10226                         (gnus-activate-group ,group))
10227                       (gnus-summary-goto-article ,cur
10228                                                  gnus-show-all-headers)))
10229         (set-alist 'mime-acting-situation-to-override
10230                    'number num)))))
10231
10232 (mime-add-condition
10233  'action '((type . message)(subtype . rfc822)
10234            (major-mode . gnus-original-article-mode)
10235            (method . gnus-mime-extract-message/rfc822)
10236            (mode . "extract")
10237            ))
10238
10239 (mime-add-condition
10240  'action '((type . message)(subtype . news)
10241            (major-mode . gnus-original-article-mode)
10242            (method . gnus-mime-extract-message/rfc822)
10243            (mode . "extract")
10244            ))
10245
10246 (defun gnus-mime-extract-multipart (entity situation)
10247   (let ((children (mime-entity-children entity))
10248         mime-acting-situation-to-override
10249         f)
10250     (while children
10251       (mime-play-entity (car children)
10252                         (cons (assq 'mode situation)
10253                               mime-acting-situation-to-override))
10254       (setq children (cdr children)))
10255     (if (setq f (cdr (assq 'after-method
10256                            mime-acting-situation-to-override)))
10257         (eval f)
10258       )))
10259
10260 (mime-add-condition
10261  'action '((type . multipart)
10262            (method . gnus-mime-extract-multipart)
10263            (mode . "extract")
10264            )
10265  'with-default)
10266
10267
10268 ;;; @ end
10269 ;;;
10270
10271 (defun gnus-summary-setup-default-charset ()
10272   "Setup newsgroup default charset."
10273   (if (equal gnus-newsgroup-name "nndraft:drafts")
10274       (setq gnus-newsgroup-charset nil)
10275     (let* ((ignored-charsets
10276             (or gnus-newsgroup-ephemeral-ignored-charsets
10277                 (append
10278                  (and gnus-newsgroup-name
10279                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10280                  gnus-newsgroup-ignored-charsets))))
10281       (setq gnus-newsgroup-charset
10282             (or gnus-newsgroup-ephemeral-charset
10283                 (and gnus-newsgroup-name
10284                      (gnus-parameter-charset gnus-newsgroup-name))
10285                 gnus-default-charset))
10286       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10287            ignored-charsets))))
10288
10289 ;;;
10290 ;;; Mime Commands
10291 ;;;
10292
10293 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10294   "Display the current article buffer fully MIME-buttonized.
10295 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10296 treated as multipart/mixed."
10297   (interactive "P")
10298   (require 'gnus-art)
10299   (let ((gnus-unbuttonized-mime-types nil)
10300         (gnus-mime-display-multipart-as-mixed show-all-parts))
10301     (gnus-summary-show-article)))
10302
10303 (defun gnus-summary-repair-multipart (article)
10304   "Add a Content-Type header to a multipart article without one."
10305   (interactive (list (gnus-summary-article-number)))
10306   (gnus-with-article article
10307     (message-narrow-to-head)
10308     (message-remove-header "Mime-Version")
10309     (goto-char (point-max))
10310     (insert "Mime-Version: 1.0\n")
10311     (widen)
10312     (when (search-forward "\n--" nil t)
10313       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10314         (message-narrow-to-head)
10315         (message-remove-header "Content-Type")
10316         (goto-char (point-max))
10317         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10318                         separator))
10319         (widen))))
10320   (let (gnus-mark-article-hook)
10321     (gnus-summary-select-article t t nil article)))
10322
10323 (defun gnus-summary-toggle-display-buttonized ()
10324   "Toggle the buttonizing of the article buffer."
10325   (interactive)
10326   (require 'gnus-art)
10327   (if (setq gnus-inhibit-mime-unbuttonizing
10328             (not gnus-inhibit-mime-unbuttonizing))
10329       (let ((gnus-unbuttonized-mime-types nil))
10330         (gnus-summary-show-article))
10331     (gnus-summary-show-article)))
10332
10333 ;;;
10334 ;;; Intelli-mouse commmands
10335 ;;;
10336
10337 (defun gnus-wheel-summary-scroll (event)
10338   (interactive "e")
10339   (let ((amount (if (memq 'shift (event-modifiers event))
10340                     (car gnus-wheel-scroll-amount)
10341                   (cdr gnus-wheel-scroll-amount)))
10342         (direction (- (* (static-if (featurep 'xemacs)
10343                              (event-button event)
10344                            (cond ((eq 'mouse-4 (event-basic-type event))
10345                                   4)
10346                                  ((eq 'mouse-5 (event-basic-type event))
10347                                   5)))
10348                          2) 9))
10349         edge)
10350     (gnus-summary-scroll-up (* amount direction))
10351     (when (gnus-eval-in-buffer-window gnus-article-buffer
10352             (save-restriction
10353               (widen)
10354               (and (if (< 0 direction)
10355                        (gnus-article-next-page 0)
10356                      (gnus-article-prev-page 0)
10357                      (bobp))
10358                    (if (setq edge (get-text-property
10359                                    (point-min) 'gnus-wheel-edge))
10360                        (setq edge (* edge direction))
10361                      (setq edge -1))
10362                    (or (plusp edge)
10363                        (let ((buffer-read-only nil)
10364                              (inhibit-read-only t))
10365                          (put-text-property (point-min) (point-max)
10366                                             'gnus-wheel-edge direction)
10367                          nil))
10368                    (or (> edge gnus-wheel-edge-resistance)
10369                        (let ((buffer-read-only nil)
10370                              (inhibit-read-only t))
10371                          (put-text-property (point-min) (point-max)
10372                                             'gnus-wheel-edge
10373                                             (* (1+ edge) direction))
10374                          nil))
10375                    (eq last-command 'gnus-wheel-summary-scroll))))
10376       (gnus-summary-next-article nil nil (minusp direction)))))
10377
10378 (defun gnus-wheel-install ()
10379   "Enable mouse wheel support on summary window."
10380   (when gnus-use-wheel
10381     (let ((keys
10382            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
10383       (dolist (key keys)
10384         (define-key gnus-summary-mode-map key
10385           'gnus-wheel-summary-scroll)))))
10386
10387 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
10388
10389 ;;;
10390 ;;; Traditional PGP commmands
10391 ;;;
10392
10393 (defun gnus-summary-decrypt-article (&optional force)
10394   "Decrypt the current article in traditional PGP way.
10395 This will have permanent effect only in mail groups.
10396 If FORCE is non-nil, allow editing of articles even in read-only
10397 groups."
10398   (interactive "P")
10399   (gnus-summary-select-article t)
10400   (gnus-eval-in-buffer-window gnus-article-buffer
10401     (save-excursion
10402       (save-restriction
10403         (widen)
10404         (goto-char (point-min))
10405         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10406           (error "Not a traditional PGP message!"))
10407         (let ((armor-start (match-beginning 0)))
10408           (if (and (pgg-decrypt-region armor-start (point-max))
10409                    (or force (not (gnus-group-read-only-p))))
10410               (let ((inhibit-read-only t)
10411                     buffer-read-only)
10412                 (delete-region armor-start
10413                                (progn
10414                                  (re-search-forward "^-+END PGP" nil t)
10415                                  (beginning-of-line 2)
10416                                  (point)))
10417                 (insert-buffer-substring pgg-output-buffer))))))))
10418
10419 (defun gnus-summary-verify-article ()
10420   "Verify the current article in traditional PGP way."
10421   (interactive)
10422   (save-excursion
10423     (set-buffer gnus-original-article-buffer)
10424     (goto-char (point-min))
10425     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10426       (error "Not a traditional PGP message!"))
10427     (re-search-forward "^-+END PGP" nil t)
10428     (beginning-of-line 2)
10429     (call-interactively (function pgg-verify-region))))
10430
10431 ;;;
10432 ;;; Generic summary marking commands
10433 ;;;
10434
10435 (defvar gnus-summary-marking-alist
10436   '((read gnus-del-mark "d")
10437     (unread gnus-unread-mark "u")
10438     (ticked gnus-ticked-mark "!")
10439     (dormant gnus-dormant-mark "?")
10440     (expirable gnus-expirable-mark "e"))
10441   "An alist of names/marks/keystrokes.")
10442
10443 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10444 (defvar gnus-summary-mark-map)
10445
10446 (defun gnus-summary-make-all-marking-commands ()
10447   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10448   (dolist (elem gnus-summary-marking-alist)
10449     (apply 'gnus-summary-make-marking-command elem)))
10450
10451 (defun gnus-summary-make-marking-command (name mark keystroke)
10452   (let ((map (make-sparse-keymap)))
10453     (define-key gnus-summary-generic-mark-map keystroke map)
10454     (dolist (lway `((next "next" next nil "n")
10455                     (next-unread "next unread" next t "N")
10456                     (prev "previous" prev nil "p")
10457                     (prev-unread "previous unread" prev t "P")
10458                     (nomove "" nil nil ,keystroke)))
10459       (let ((func (gnus-summary-make-marking-command-1
10460                    mark (car lway) lway name)))
10461         (setq func (eval func))
10462         (define-key map (nth 4 lway) func)))))
10463
10464 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10465   `(defun ,(intern
10466             (format "gnus-summary-put-mark-as-%s%s"
10467                     name (if (eq way 'nomove)
10468                              ""
10469                            (concat "-" (symbol-name way)))))
10470      (n)
10471      ,(format
10472        "Mark the current article as %s%s.
10473 If N, the prefix, then repeat N times.
10474 If N is negative, move in reverse order.
10475 The difference between N and the actual number of articles marked is
10476 returned."
10477        name (car (cdr lway)))
10478      (interactive "p")
10479      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10480
10481 (defun gnus-summary-generic-mark (n mark move unread)
10482   "Mark N articles with MARK."
10483   (unless (eq major-mode 'gnus-summary-mode)
10484     (error "This command can only be used in the summary buffer"))
10485   (gnus-summary-show-thread)
10486   (let ((nummove
10487          (cond
10488           ((eq move 'next) 1)
10489           ((eq move 'prev) -1)
10490           (t 0))))
10491     (if (zerop nummove)
10492         (setq n 1)
10493       (when (< n 0)
10494         (setq n (abs n)
10495               nummove (* -1 nummove))))
10496     (while (and (> n 0)
10497                 (gnus-summary-mark-article nil mark)
10498                 (zerop (gnus-summary-next-subject nummove unread t)))
10499       (setq n (1- n)))
10500     (when (/= 0 n)
10501       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10502     (gnus-summary-recenter)
10503     (gnus-summary-position-point)
10504     (gnus-set-mode-line 'summary)
10505     n))
10506
10507 (defun gnus-summary-insert-articles (articles)
10508   (when (setq articles
10509               (gnus-set-difference articles
10510                                    (mapcar (lambda (h) (mail-header-number h))
10511                                            gnus-newsgroup-headers)))
10512     (setq gnus-newsgroup-headers 
10513           (merge 'list
10514                  gnus-newsgroup-headers
10515                  (gnus-fetch-headers articles)
10516                  'gnus-article-sort-by-number))
10517     ;; Suppress duplicates?
10518     (when gnus-suppress-duplicates
10519       (gnus-dup-suppress-articles))
10520     
10521     ;; We might want to build some more threads first.
10522     (when (and gnus-fetch-old-headers
10523                (eq gnus-headers-retrieved-by 'nov))
10524       (if (eq gnus-fetch-old-headers 'invisible)
10525         (gnus-build-all-threads)
10526         (gnus-build-old-threads)))
10527     ;; Let the Gnus agent mark articles as read.
10528     (when gnus-agent
10529       (gnus-agent-get-undownloaded-list))
10530     ;; Remove list identifiers from subject
10531     (when gnus-list-identifiers
10532       (gnus-summary-remove-list-identifiers))
10533     ;; First and last article in this newsgroup.
10534     (when gnus-newsgroup-headers
10535       (setq gnus-newsgroup-begin
10536             (mail-header-number (car gnus-newsgroup-headers))
10537             gnus-newsgroup-end
10538             (mail-header-number
10539              (gnus-last-element gnus-newsgroup-headers))))
10540     (when gnus-use-scoring
10541       (gnus-possibly-score-headers))))
10542
10543 (defun gnus-summary-insert-old-articles (&optional all)
10544   "Insert all old articles in this group.
10545 If ALL is non-nil, already read articles become readable.
10546 If ALL is a number, fetch this number of articles."
10547   (interactive "P")
10548   (prog1
10549       (let ((old (mapcar 'car gnus-newsgroup-data))
10550             (i (car gnus-newsgroup-active))
10551             older len)
10552         (while (<= i (cdr gnus-newsgroup-active))
10553           (or (memq i old) (push i older))
10554           (incf i))
10555         (setq len (length older))
10556         (cond 
10557          ((null older) nil)
10558          ((numberp all) 
10559           (if (< all len)
10560               (setq older (subseq older 0 all))))
10561          (all nil)
10562          (t
10563           (if (and (numberp gnus-large-newsgroup)
10564                    (> len gnus-large-newsgroup))
10565               (let ((input
10566                      (read-string
10567                       (format
10568                        "How many articles from %s (default %d): "
10569                        (gnus-limit-string 
10570                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10571                        len))))
10572                 (unless (string-match "^[ \t]*$" input) 
10573                   (setq all (string-to-number input))
10574                   (if (< all len)
10575                       (setq older (subseq older 0 all))))))))
10576         (if (not older)
10577             (message "No old news.")
10578           (gnus-summary-insert-articles older)
10579           (gnus-summary-limit (gnus-union older old))))
10580     (gnus-summary-position-point)))
10581
10582 (defun gnus-summary-insert-new-articles ()
10583   "Insert all new articles in this group."
10584   (interactive)
10585   (prog1
10586       (let ((old (mapcar 'car gnus-newsgroup-data))
10587             (old-active gnus-newsgroup-active)
10588             (nnmail-fetched-sources (list t))
10589             i new)
10590         (setq gnus-newsgroup-active 
10591               (gnus-activate-group gnus-newsgroup-name 'scan))
10592         (setq i (1+ (cdr old-active)))
10593         (while (<= i (cdr gnus-newsgroup-active))
10594           (push i new)
10595           (incf i))
10596         (if (not new)
10597             (message "No gnus is bad news.")
10598           (setq new (nreverse new))
10599           (gnus-summary-insert-articles new)
10600           (setq gnus-newsgroup-unreads
10601                 (append gnus-newsgroup-unreads new))
10602           (gnus-summary-limit (gnus-union old new))))
10603     (gnus-summary-position-point)))
10604
10605 (gnus-summary-make-all-marking-commands)
10606
10607 (gnus-ems-redefine)
10608
10609 (provide 'gnus-sum)
10610
10611 (run-hooks 'gnus-sum-load-hook)
10612
10613 ;;; gnus-sum.el ends here