Synch with Oort Gnus v0.02.
[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     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
856           (memq article gnus-newsgroup-unreads))
857      . gnus-summary-normal-unread-face)
858     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
859      . gnus-summary-normal-read-face)
860     ((> score default)
861      . gnus-summary-high-read-face)
862     ((< score default)
863      . gnus-summary-low-read-face)
864     (t
865      . gnus-summary-normal-read-face))
866   "*Controls the highlighting of summary buffer lines.
867
868 A list of (FORM . FACE) pairs.  When deciding how a a particular
869 summary line should be displayed, each form is evaluated.  The content
870 of the face field after the first true form is used.  You can change
871 how those summary lines are displayed, by editing the face field.
872
873 You can use the following variables in the FORM field.
874
875 score:   The articles score
876 default: The default article score.
877 below:   The score below which articles are automatically marked as read.
878 mark:    The articles mark."
879   :group 'gnus-summary-visual
880   :type '(repeat (cons (sexp :tag "Form" nil)
881                        face)))
882
883 (defcustom gnus-alter-header-function nil
884   "Function called to allow alteration of article header structures.
885 The function is called with one parameter, the article header vector,
886 which it may alter in any way.")
887
888 (defvar gnus-decode-encoded-word-function
889   (mime-find-field-decoder 'From 'nov)
890   "Variable that says which function should be used to decode a string with encoded words.")
891
892 (defcustom gnus-extra-headers nil
893   "*Extra headers to parse."
894   :version "21.1"
895   :group 'gnus-summary
896   :type '(repeat symbol))
897
898 (defcustom gnus-ignored-from-addresses
899   (and user-mail-address (regexp-quote user-mail-address))
900   "*Regexp of From headers that may be suppressed in favor of To headers."
901   :version "21.1"
902   :group 'gnus-summary
903   :type 'regexp)
904
905 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
906   "List of charsets that should be ignored.
907 When these charsets are used in the \"charset\" parameter, the
908 default charset will be used instead."
909   :version "21.1"
910   :type '(repeat symbol)
911   :group 'gnus-charset)
912
913 (gnus-define-group-parameter
914  ignored-charsets
915  :type list
916  :function-document
917  "Return the ignored charsets of GROUP."
918  :variable gnus-group-ignored-charsets-alist
919  :variable-default 
920  '(("alt\\.chinese\\.text" iso-8859-1))
921  :variable-document
922  "Alist of regexps (to match group names) and charsets that should be ignored.
923 When these charsets are used in the \"charset\" parameter, the
924 default charset will be used instead."
925  :variable-group gnus-charset
926  :variable-type '(repeat (cons (regexp :tag "Group")
927                                (repeat symbol)))
928  :parameter-type '(choice :tag "Ignored charsets" 
929                           :value nil
930                           (repeat (symbol)))
931  :parameter-document       "\
932 List of charsets that should be ignored.
933
934 When these charsets are used in the \"charset\" parameter, the
935 default charset will be used instead.")
936
937 (defcustom gnus-group-highlight-words-alist nil
938   "Alist of group regexps and highlight regexps.
939 This variable uses the same syntax as `gnus-emphasis-alist'."
940   :version "21.1"
941   :type '(repeat (cons (regexp :tag "Group")
942                        (repeat (list (regexp :tag "Highlight regexp")
943                                      (number :tag "Group for entire word" 0)
944                                      (number :tag "Group for displayed part" 0)
945                                      (symbol :tag "Face"
946                                              gnus-emphasis-highlight-words)))))
947   :group 'gnus-summary-visual)
948
949 (defcustom gnus-use-wheel nil
950   "Use Intelli-mouse on summary movement"
951   :type 'boolean
952   :group 'gnus-summary-maneuvering)
953
954 (defcustom gnus-wheel-scroll-amount '(5 . 1)
955   "Amount to scroll messages by spinning the mouse wheel.
956 This is actually a cons cell, where the first item is the amount to scroll
957 on a normal wheel event, and the second is the amount to scroll when the
958 wheel is moved with the shift key depressed."
959   :type '(cons (integer :tag "Shift") integer)
960   :group 'gnus-summary-maneuvering)
961
962 (defcustom gnus-wheel-edge-resistance 2
963   "How hard it should be to change the current article
964 by moving the mouse over the edge of the article window."
965   :type 'integer
966   :group 'gnus-summary-maneuvering)
967
968 (defcustom gnus-summary-show-article-charset-alist
969   nil
970   "Alist of number and charset.
971 The article will be shown with the charset corresponding to the
972 numbered argument.
973 For example: ((1 . cn-gb-2312) (2 . big5))."
974   :version "21.1"
975   :type '(repeat (cons (number :tag "Argument" 1)
976                        (symbol :tag "Charset")))
977   :group 'gnus-charset)
978
979 (defcustom gnus-preserve-marks t
980   "Whether marks are preserved when moving, copying and respooling messages."
981   :version "21.1"
982   :type 'boolean
983   :group 'gnus-summary-marks)
984
985 (defcustom gnus-alter-articles-to-read-function nil
986   "Function to be called to alter the list of articles to be selected."
987   :type '(choice (const nil) function)
988   :group 'gnus-summary)
989
990 (defcustom gnus-orphan-score nil
991   "*All orphans get this score added.  Set in the score file."
992   :group 'gnus-score-default
993   :type '(choice (const nil)
994                  integer))
995
996 (defcustom gnus-summary-save-parts-default-mime "image/.*"
997   "*A regexp to match MIME parts when saving multiple parts of a message
998 with gnus-summary-save-parts (X m). This regexp will be used by default
999 when prompting the user for which type of files to save."
1000   :group 'gnus-summary
1001   :type 'regexp)
1002
1003
1004 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1005   "*A regexp to match MIME parts when saving multiple parts of a message
1006 with gnus-summary-save-parts (X m). This regexp will be used by default
1007 when prompting the user for which type of files to save."
1008   :group 'gnus-summary
1009   :type 'regexp)
1010
1011
1012 ;;; Internal variables
1013
1014 (defvar gnus-article-mime-handles nil)
1015 (defvar gnus-article-decoded-p nil)
1016 (defvar gnus-article-charset nil)
1017 (defvar gnus-article-ignored-charsets nil)
1018 (defvar gnus-scores-exclude-files nil)
1019 (defvar gnus-page-broken nil)
1020 (defvar gnus-inhibit-mime-unbuttonizing nil)
1021
1022 (defvar gnus-original-article nil)
1023 (defvar gnus-article-internal-prepare-hook nil)
1024 (defvar gnus-newsgroup-process-stack nil)
1025
1026 (defvar gnus-thread-indent-array nil)
1027 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1028 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1029   "Function called to sort the articles within a thread after it has been gathered together.")
1030
1031 (defvar gnus-summary-save-parts-type-history nil)
1032 (defvar gnus-summary-save-parts-last-directory nil)
1033
1034 (defvar gnus-summary-save-parts-type-history nil)
1035 (defvar gnus-summary-save-parts-last-directory nil)
1036
1037 ;; Avoid highlighting in kill files.
1038 (defvar gnus-summary-inhibit-highlight nil)
1039 (defvar gnus-newsgroup-selected-overlay nil)
1040 (defvar gnus-inhibit-limiting nil)
1041 (defvar gnus-newsgroup-adaptive-score-file nil)
1042 (defvar gnus-current-score-file nil)
1043 (defvar gnus-current-move-group nil)
1044 (defvar gnus-current-copy-group nil)
1045 (defvar gnus-current-crosspost-group nil)
1046
1047 (defvar gnus-newsgroup-dependencies nil)
1048 (defvar gnus-newsgroup-adaptive nil)
1049 (defvar gnus-summary-display-article-function nil)
1050 (defvar gnus-summary-highlight-line-function nil
1051   "Function called after highlighting a summary line.")
1052
1053 (defvar gnus-summary-line-format-alist
1054   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1055     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1056     (?s gnus-tmp-subject-or-nil ?s)
1057     (?n gnus-tmp-name ?s)
1058     (?A (std11-address-string
1059          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1060     (?a (or (std11-full-name-string
1061              (car (mime-entity-read-field gnus-tmp-header 'From)))
1062             gnus-tmp-from) ?s)
1063     (?F gnus-tmp-from ?s)
1064     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1065     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1066     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1067     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1068     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1069     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1070     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1071     (?L gnus-tmp-lines ?s)
1072     (?I gnus-tmp-indentation ?s)
1073     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1074     (?R gnus-tmp-replied ?c)
1075     (?\[ gnus-tmp-opening-bracket ?c)
1076     (?\] gnus-tmp-closing-bracket ?c)
1077     (?\> (make-string gnus-tmp-level ? ) ?s)
1078     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1079     (?i gnus-tmp-score ?d)
1080     (?z gnus-tmp-score-char ?c)
1081     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1082     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1083     (?U gnus-tmp-unread ?c)
1084     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1085     (?t (gnus-summary-number-of-articles-in-thread
1086          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1087         ?d)
1088     (?e (gnus-summary-number-of-articles-in-thread
1089          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1090         ?c)
1091     (?u gnus-tmp-user-defined ?s)
1092     (?P (gnus-pick-line-number) ?d))
1093   "An alist of format specifications that can appear in summary lines.
1094 These are paired with what variables they correspond with, along with
1095 the type of the variable (string, integer, character, etc).")
1096
1097 (defvar gnus-summary-dummy-line-format-alist
1098   `((?S gnus-tmp-subject ?s)
1099     (?N gnus-tmp-number ?d)
1100     (?u gnus-tmp-user-defined ?s)))
1101
1102 (defvar gnus-summary-mode-line-format-alist
1103   `((?G gnus-tmp-group-name ?s)
1104     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1105     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1106     (?A gnus-tmp-article-number ?d)
1107     (?Z gnus-tmp-unread-and-unselected ?s)
1108     (?V gnus-version ?s)
1109     (?U gnus-tmp-unread-and-unticked ?d)
1110     (?S gnus-tmp-subject ?s)
1111     (?e gnus-tmp-unselected ?d)
1112     (?u gnus-tmp-user-defined ?s)
1113     (?d (length gnus-newsgroup-dormant) ?d)
1114     (?t (length gnus-newsgroup-marked) ?d)
1115     (?r (length gnus-newsgroup-reads) ?d)
1116     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1117     (?E gnus-newsgroup-expunged-tally ?d)
1118     (?s (gnus-current-score-file-nondirectory) ?s)))
1119
1120 (defvar gnus-last-search-regexp nil
1121   "Default regexp for article search command.")
1122
1123 (defvar gnus-summary-search-article-matched-data nil
1124   "Last matched data of article search command.  It is the local variable
1125 in `gnus-article-buffer' which consists of the list of start position,
1126 end position and text.")
1127
1128 (defvar gnus-last-shell-command nil
1129   "Default shell command on article.")
1130
1131 (defvar gnus-newsgroup-begin nil)
1132 (defvar gnus-newsgroup-end nil)
1133 (defvar gnus-newsgroup-last-rmail nil)
1134 (defvar gnus-newsgroup-last-mail nil)
1135 (defvar gnus-newsgroup-last-folder nil)
1136 (defvar gnus-newsgroup-last-file nil)
1137 (defvar gnus-newsgroup-auto-expire nil)
1138 (defvar gnus-newsgroup-active nil)
1139
1140 (defvar gnus-newsgroup-data nil)
1141 (defvar gnus-newsgroup-data-reverse nil)
1142 (defvar gnus-newsgroup-limit nil)
1143 (defvar gnus-newsgroup-limits nil)
1144
1145 (defvar gnus-newsgroup-unreads nil
1146   "List of unread articles in the current newsgroup.")
1147
1148 (defvar gnus-newsgroup-unselected nil
1149   "List of unselected unread articles in the current newsgroup.")
1150
1151 (defvar gnus-newsgroup-reads nil
1152   "Alist of read articles and article marks in the current newsgroup.")
1153
1154 (defvar gnus-newsgroup-expunged-tally nil)
1155
1156 (defvar gnus-newsgroup-marked nil
1157   "List of ticked articles in the current newsgroup (a subset of unread art).")
1158
1159 (defvar gnus-newsgroup-killed nil
1160   "List of ranges of articles that have been through the scoring process.")
1161
1162 (defvar gnus-newsgroup-cached nil
1163   "List of articles that come from the article cache.")
1164
1165 (defvar gnus-newsgroup-saved nil
1166   "List of articles that have been saved.")
1167
1168 (defvar gnus-newsgroup-kill-headers nil)
1169
1170 (defvar gnus-newsgroup-replied nil
1171   "List of articles that have been replied to in the current newsgroup.")
1172
1173 (defvar gnus-newsgroup-forwarded nil
1174   "List of articles that have been forwarded in the current newsgroup.")
1175
1176 (defvar gnus-newsgroup-expirable nil
1177   "List of articles in the current newsgroup that can be expired.")
1178
1179 (defvar gnus-newsgroup-processable nil
1180   "List of articles in the current newsgroup that can be processed.")
1181
1182 (defvar gnus-newsgroup-downloadable nil
1183   "List of articles in the current newsgroup that can be processed.")
1184
1185 (defvar gnus-newsgroup-undownloaded nil
1186   "List of articles in the current newsgroup that haven't been downloaded..")
1187
1188 (defvar gnus-newsgroup-unsendable nil
1189   "List of articles in the current newsgroup that won't be sent.")
1190
1191 (defvar gnus-newsgroup-bookmarks nil
1192   "List of articles in the current newsgroup that have bookmarks.")
1193
1194 (defvar gnus-newsgroup-dormant nil
1195   "List of dormant articles in the current newsgroup.")
1196
1197 (defvar gnus-newsgroup-scored nil
1198   "List of scored articles in the current newsgroup.")
1199
1200 (defvar gnus-newsgroup-incorporated nil
1201   "List of incorporated articles in the current newsgroup.")
1202
1203 (defvar gnus-newsgroup-headers nil
1204   "List of article headers in the current newsgroup.")
1205
1206 (defvar gnus-newsgroup-threads nil)
1207
1208 (defvar gnus-newsgroup-prepared nil
1209   "Whether the current group has been prepared properly.")
1210
1211 (defvar gnus-newsgroup-ancient nil
1212   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1213
1214 (defvar gnus-newsgroup-sparse nil)
1215
1216 (defvar gnus-current-article nil)
1217 (defvar gnus-article-current nil)
1218 (defvar gnus-current-headers nil)
1219 (defvar gnus-have-all-headers nil)
1220 (defvar gnus-last-article nil)
1221 (defvar gnus-newsgroup-history nil)
1222 (defvar gnus-newsgroup-charset nil)
1223 (defvar gnus-newsgroup-ephemeral-charset nil)
1224 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1225
1226 (defvar gnus-article-before-search nil)
1227
1228 (defconst gnus-summary-local-variables
1229   '(gnus-newsgroup-name
1230     gnus-newsgroup-begin gnus-newsgroup-end
1231     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1232     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1233     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1234     gnus-newsgroup-unselected gnus-newsgroup-marked
1235     gnus-newsgroup-reads gnus-newsgroup-saved
1236     gnus-newsgroup-replied gnus-newsgroup-forwarded
1237     gnus-newsgroup-expirable
1238     gnus-newsgroup-processable gnus-newsgroup-killed
1239     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1240     gnus-newsgroup-unsendable
1241     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1242     gnus-newsgroup-headers gnus-newsgroup-threads
1243     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1244     gnus-current-article gnus-current-headers gnus-have-all-headers
1245     gnus-last-article gnus-article-internal-prepare-hook
1246     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1247     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1248     gnus-thread-expunge-below
1249     gnus-score-alist gnus-current-score-file
1250     (gnus-summary-expunge-below . global)
1251     (gnus-summary-mark-below . global)
1252     (gnus-orphan-score . global)
1253     gnus-newsgroup-active gnus-scores-exclude-files
1254     gnus-newsgroup-history gnus-newsgroup-ancient
1255     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1256     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1257     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1258     (gnus-newsgroup-expunged-tally . 0)
1259     gnus-cache-removable-articles gnus-newsgroup-cached
1260     gnus-newsgroup-data gnus-newsgroup-data-reverse
1261     gnus-newsgroup-limit gnus-newsgroup-limits
1262     gnus-newsgroup-charset
1263     gnus-newsgroup-incorporated)
1264   "Variables that are buffer-local to the summary buffers.")
1265
1266 (defvar gnus-newsgroup-variables nil
1267   "Variables that have separate values in the newsgroups.")
1268
1269 ;; Byte-compiler warning.
1270 (eval-when-compile (defvar gnus-article-mode-map))
1271
1272 ;; Subject simplification.
1273
1274 (defun gnus-simplify-whitespace (str)
1275   "Remove excessive whitespace from STR."
1276   (let ((mystr str))
1277     ;; Multiple spaces.
1278     (while (string-match "[ \t][ \t]+" mystr)
1279       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1280                           " "
1281                           (substring mystr (match-end 0)))))
1282     ;; Leading spaces.
1283     (when (string-match "^[ \t]+" mystr)
1284       (setq mystr (substring mystr (match-end 0))))
1285     ;; Trailing spaces.
1286     (when (string-match "[ \t]+$" mystr)
1287       (setq mystr (substring mystr 0 (match-beginning 0))))
1288     mystr))
1289
1290 (defsubst gnus-simplify-subject-re (subject)
1291   "Remove \"Re:\" from subject lines."
1292   (if (string-match message-subject-re-regexp subject)
1293       (substring subject (match-end 0))
1294     subject))
1295
1296 (defun gnus-simplify-subject (subject &optional re-only)
1297   "Remove `Re:' and words in parentheses.
1298 If RE-ONLY is non-nil, strip leading `Re:'s only."
1299   (let ((case-fold-search t))           ;Ignore case.
1300     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1301     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1302       (setq subject (substring subject (match-end 0))))
1303     ;; Remove uninteresting prefixes.
1304     (when (and (not re-only)
1305                gnus-simplify-ignored-prefixes
1306                (string-match gnus-simplify-ignored-prefixes subject))
1307       (setq subject (substring subject (match-end 0))))
1308     ;; Remove words in parentheses from end.
1309     (unless re-only
1310       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1311         (setq subject (substring subject 0 (match-beginning 0)))))
1312     ;; Return subject string.
1313     subject))
1314
1315 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1316 ;; all whitespace.
1317 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1318   (goto-char (point-min))
1319   (while (re-search-forward regexp nil t)
1320     (replace-match (or newtext ""))))
1321
1322 (defun gnus-simplify-buffer-fuzzy ()
1323   "Simplify string in the buffer fuzzily.
1324 The string in the accessible portion of the current buffer is simplified.
1325 It is assumed to be a single-line subject.
1326 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1327 matter is removed.  Additional things can be deleted by setting
1328 `gnus-simplify-subject-fuzzy-regexp'."
1329   (let ((case-fold-search t)
1330         (modified-tick))
1331     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1332
1333     (while (not (eq modified-tick (buffer-modified-tick)))
1334       (setq modified-tick (buffer-modified-tick))
1335       (cond
1336        ((listp gnus-simplify-subject-fuzzy-regexp)
1337         (mapcar 'gnus-simplify-buffer-fuzzy-step
1338                 gnus-simplify-subject-fuzzy-regexp))
1339        (gnus-simplify-subject-fuzzy-regexp
1340         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1341       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1342       (gnus-simplify-buffer-fuzzy-step
1343        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1344       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1345
1346     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1347     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1348     (gnus-simplify-buffer-fuzzy-step " $")
1349     (gnus-simplify-buffer-fuzzy-step "^ +")))
1350
1351 (defun gnus-simplify-subject-fuzzy (subject)
1352   "Simplify a subject string fuzzily.
1353 See `gnus-simplify-buffer-fuzzy' for details."
1354   (save-excursion
1355     (gnus-set-work-buffer)
1356     (let ((case-fold-search t))
1357       ;; Remove uninteresting prefixes.
1358       (when (and gnus-simplify-ignored-prefixes
1359                  (string-match gnus-simplify-ignored-prefixes subject))
1360         (setq subject (substring subject (match-end 0))))
1361       (insert subject)
1362       (inline (gnus-simplify-buffer-fuzzy))
1363       (buffer-string))))
1364
1365 (defsubst gnus-simplify-subject-fully (subject)
1366   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1367   (cond
1368    (gnus-simplify-subject-functions
1369     (gnus-map-function gnus-simplify-subject-functions subject))
1370    ((null gnus-summary-gather-subject-limit)
1371     (gnus-simplify-subject-re subject))
1372    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1373     (gnus-simplify-subject-fuzzy subject))
1374    ((numberp gnus-summary-gather-subject-limit)
1375     (gnus-limit-string (gnus-simplify-subject-re subject)
1376                        gnus-summary-gather-subject-limit))
1377    (t
1378     subject)))
1379
1380 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1381   "Check whether two subjects are equal.
1382 If optional argument simple-first is t, first argument is already
1383 simplified."
1384   (cond
1385    ((null simple-first)
1386     (equal (gnus-simplify-subject-fully s1)
1387            (gnus-simplify-subject-fully s2)))
1388    (t
1389     (equal s1
1390            (gnus-simplify-subject-fully s2)))))
1391
1392 (defun gnus-summary-bubble-group ()
1393   "Increase the score of the current group.
1394 This is a handy function to add to `gnus-summary-exit-hook' to
1395 increase the score of each group you read."
1396   (gnus-group-add-score gnus-newsgroup-name))
1397
1398 \f
1399 ;;;
1400 ;;; Gnus summary mode
1401 ;;;
1402
1403 (put 'gnus-summary-mode 'mode-class 'special)
1404
1405 (defvar gnus-article-commands-menu)
1406
1407 (when t
1408   ;; Non-orthogonal keys
1409
1410   (gnus-define-keys gnus-summary-mode-map
1411     " " gnus-summary-next-page
1412     "\177" gnus-summary-prev-page
1413     [delete] gnus-summary-prev-page
1414     [backspace] gnus-summary-prev-page
1415     "\r" gnus-summary-scroll-up
1416     "\M-\r" gnus-summary-scroll-down
1417     "n" gnus-summary-next-unread-article
1418     "p" gnus-summary-prev-unread-article
1419     "N" gnus-summary-next-article
1420     "P" gnus-summary-prev-article
1421     "\M-\C-n" gnus-summary-next-same-subject
1422     "\M-\C-p" gnus-summary-prev-same-subject
1423     "\M-n" gnus-summary-next-unread-subject
1424     "\M-p" gnus-summary-prev-unread-subject
1425     "." gnus-summary-first-unread-article
1426     "," gnus-summary-best-unread-article
1427     "\M-s" gnus-summary-search-article-forward
1428     "\M-r" gnus-summary-search-article-backward
1429     "<" gnus-summary-beginning-of-article
1430     ">" gnus-summary-end-of-article
1431     "j" gnus-summary-goto-article
1432     "^" gnus-summary-refer-parent-article
1433     "\M-^" gnus-summary-refer-article
1434     "u" gnus-summary-tick-article-forward
1435     "!" gnus-summary-tick-article-forward
1436     "U" gnus-summary-tick-article-backward
1437     "d" gnus-summary-mark-as-read-forward
1438     "D" gnus-summary-mark-as-read-backward
1439     "E" gnus-summary-mark-as-expirable
1440     "\M-u" gnus-summary-clear-mark-forward
1441     "\M-U" gnus-summary-clear-mark-backward
1442     "k" gnus-summary-kill-same-subject-and-select
1443     "\C-k" gnus-summary-kill-same-subject
1444     "\M-\C-k" gnus-summary-kill-thread
1445     "\M-\C-l" gnus-summary-lower-thread
1446     "e" gnus-summary-edit-article
1447     "#" gnus-summary-mark-as-processable
1448     "\M-#" gnus-summary-unmark-as-processable
1449     "\M-\C-t" gnus-summary-toggle-threads
1450     "\M-\C-s" gnus-summary-show-thread
1451     "\M-\C-h" gnus-summary-hide-thread
1452     "\M-\C-f" gnus-summary-next-thread
1453     "\M-\C-b" gnus-summary-prev-thread
1454     [(meta down)] gnus-summary-next-thread
1455     [(meta up)] gnus-summary-prev-thread
1456     "\M-\C-u" gnus-summary-up-thread
1457     "\M-\C-d" gnus-summary-down-thread
1458     "&" gnus-summary-execute-command
1459     "c" gnus-summary-catchup-and-exit
1460     "\C-w" gnus-summary-mark-region-as-read
1461     "\C-t" gnus-summary-toggle-truncation
1462     "?" gnus-summary-mark-as-dormant
1463     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1464     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1465     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1466     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1467     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1468     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1469     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1470     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1471     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1472     "=" gnus-summary-expand-window
1473     "\C-x\C-s" gnus-summary-reselect-current-group
1474     "\M-g" gnus-summary-rescan-group
1475     "w" gnus-summary-stop-page-breaking
1476     "\C-c\C-r" gnus-summary-caesar-message
1477     "\M-t" gnus-summary-toggle-mime
1478     "f" gnus-summary-followup
1479     "F" gnus-summary-followup-with-original
1480     "C" gnus-summary-cancel-article
1481     "r" gnus-summary-reply
1482     "R" gnus-summary-reply-with-original
1483     "\C-c\C-f" gnus-summary-mail-forward
1484     "o" gnus-summary-save-article
1485     "\C-o" gnus-summary-save-article-mail
1486     "|" gnus-summary-pipe-output
1487     "\M-k" gnus-summary-edit-local-kill
1488     "\M-K" gnus-summary-edit-global-kill
1489     ;; "V" gnus-version
1490     "\C-c\C-d" gnus-summary-describe-group
1491     "q" gnus-summary-exit
1492     "Q" gnus-summary-exit-no-update
1493     "\C-c\C-i" gnus-info-find-node
1494     gnus-mouse-2 gnus-mouse-pick-article
1495     "m" gnus-summary-mail-other-window
1496     "a" gnus-summary-post-news
1497     "x" gnus-summary-limit-to-unread
1498     "s" gnus-summary-isearch-article
1499     "t" gnus-article-toggle-headers
1500     "g" gnus-summary-show-article
1501     "l" gnus-summary-goto-last-article
1502     "v" gnus-summary-preview-mime-message
1503     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1504     "\C-d" gnus-summary-enter-digest-group
1505     "\M-\C-d" gnus-summary-read-document
1506     "\M-\C-e" gnus-summary-edit-parameters
1507     "\M-\C-a" gnus-summary-customize-parameters
1508     "\C-c\C-b" gnus-bug
1509     "*" gnus-cache-enter-article
1510     "\M-*" gnus-cache-remove-article
1511     "\M-&" gnus-summary-universal-argument
1512     "\C-l" gnus-recenter
1513     "I" gnus-summary-increase-score
1514     "L" gnus-summary-lower-score
1515     "\M-i" gnus-symbolic-argument
1516     "h" gnus-summary-select-article-buffer
1517
1518     "V" gnus-summary-score-map
1519     "X" gnus-uu-extract-map
1520     "S" gnus-summary-send-map)
1521
1522   ;; Sort of orthogonal keymap
1523   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1524     "t" gnus-summary-tick-article-forward
1525     "!" gnus-summary-tick-article-forward
1526     "d" gnus-summary-mark-as-read-forward
1527     "r" gnus-summary-mark-as-read-forward
1528     "c" gnus-summary-clear-mark-forward
1529     " " gnus-summary-clear-mark-forward
1530     "e" gnus-summary-mark-as-expirable
1531     "x" gnus-summary-mark-as-expirable
1532     "?" gnus-summary-mark-as-dormant
1533     "b" gnus-summary-set-bookmark
1534     "B" gnus-summary-remove-bookmark
1535     "#" gnus-summary-mark-as-processable
1536     "\M-#" gnus-summary-unmark-as-processable
1537     "S" gnus-summary-limit-include-expunged
1538     "C" gnus-summary-catchup
1539     "H" gnus-summary-catchup-to-here
1540     "\C-c" gnus-summary-catchup-all
1541     "k" gnus-summary-kill-same-subject-and-select
1542     "K" gnus-summary-kill-same-subject
1543     "P" gnus-uu-mark-map)
1544
1545   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1546     "c" gnus-summary-clear-above
1547     "u" gnus-summary-tick-above
1548     "m" gnus-summary-mark-above
1549     "k" gnus-summary-kill-below)
1550
1551   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1552     "/" gnus-summary-limit-to-subject
1553     "n" gnus-summary-limit-to-articles
1554     "w" gnus-summary-pop-limit
1555     "s" gnus-summary-limit-to-subject
1556     "a" gnus-summary-limit-to-author
1557     "u" gnus-summary-limit-to-unread
1558     "m" gnus-summary-limit-to-marks
1559     "M" gnus-summary-limit-exclude-marks
1560     "v" gnus-summary-limit-to-score
1561     "*" gnus-summary-limit-include-cached
1562     "D" gnus-summary-limit-include-dormant
1563     "T" gnus-summary-limit-include-thread
1564     "d" gnus-summary-limit-exclude-dormant
1565     "t" gnus-summary-limit-to-age
1566     "x" gnus-summary-limit-to-extra
1567     "E" gnus-summary-limit-include-expunged
1568     "c" gnus-summary-limit-exclude-childless-dormant
1569     "C" gnus-summary-limit-mark-excluded-as-read
1570     "o" gnus-summary-insert-old-articles
1571     "N" gnus-summary-insert-new-articles)
1572
1573   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1574     "n" gnus-summary-next-unread-article
1575     "p" gnus-summary-prev-unread-article
1576     "N" gnus-summary-next-article
1577     "P" gnus-summary-prev-article
1578     "\C-n" gnus-summary-next-same-subject
1579     "\C-p" gnus-summary-prev-same-subject
1580     "\M-n" gnus-summary-next-unread-subject
1581     "\M-p" gnus-summary-prev-unread-subject
1582     "f" gnus-summary-first-unread-article
1583     "b" gnus-summary-best-unread-article
1584     "j" gnus-summary-goto-article
1585     "g" gnus-summary-goto-subject
1586     "l" gnus-summary-goto-last-article
1587     "o" gnus-summary-pop-article)
1588
1589   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1590     "k" gnus-summary-kill-thread
1591     "l" gnus-summary-lower-thread
1592     "i" gnus-summary-raise-thread
1593     "T" gnus-summary-toggle-threads
1594     "t" gnus-summary-rethread-current
1595     "^" gnus-summary-reparent-thread
1596     "s" gnus-summary-show-thread
1597     "S" gnus-summary-show-all-threads
1598     "h" gnus-summary-hide-thread
1599     "H" gnus-summary-hide-all-threads
1600     "n" gnus-summary-next-thread
1601     "p" gnus-summary-prev-thread
1602     "u" gnus-summary-up-thread
1603     "o" gnus-summary-top-thread
1604     "d" gnus-summary-down-thread
1605     "#" gnus-uu-mark-thread
1606     "\M-#" gnus-uu-unmark-thread)
1607
1608   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1609     "g" gnus-summary-prepare
1610     "c" gnus-summary-insert-cached-articles)
1611
1612   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1613     "c" gnus-summary-catchup-and-exit
1614     "C" gnus-summary-catchup-all-and-exit
1615     "E" gnus-summary-exit-no-update
1616     "J" gnus-summary-jump-to-other-group
1617     "Q" gnus-summary-exit
1618     "Z" gnus-summary-exit
1619     "n" gnus-summary-catchup-and-goto-next-group
1620     "R" gnus-summary-reselect-current-group
1621     "G" gnus-summary-rescan-group
1622     "N" gnus-summary-next-group
1623     "s" gnus-summary-save-newsrc
1624     "P" gnus-summary-prev-group)
1625
1626   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1627     " " gnus-summary-next-page
1628     "n" gnus-summary-next-page
1629     "\177" gnus-summary-prev-page
1630     [delete] gnus-summary-prev-page
1631     "p" gnus-summary-prev-page
1632     "\r" gnus-summary-scroll-up
1633     "\M-\r" gnus-summary-scroll-down
1634     "<" gnus-summary-beginning-of-article
1635     ">" gnus-summary-end-of-article
1636     "b" gnus-summary-beginning-of-article
1637     "e" gnus-summary-end-of-article
1638     "^" gnus-summary-refer-parent-article
1639     "r" gnus-summary-refer-parent-article
1640     "D" gnus-summary-enter-digest-group
1641     "R" gnus-summary-refer-references
1642     "T" gnus-summary-refer-thread
1643     "g" gnus-summary-show-article
1644     "s" gnus-summary-isearch-article
1645     "P" gnus-summary-print-article
1646     "M" gnus-mailing-list-insinuate
1647     "t" gnus-article-babel)
1648
1649   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1650     "b" gnus-article-add-buttons
1651     "B" gnus-article-add-buttons-to-head
1652     "o" gnus-article-treat-overstrike
1653     "e" gnus-article-emphasize
1654     "w" gnus-article-fill-cited-article
1655     "Q" gnus-article-fill-long-lines
1656     "C" gnus-article-capitalize-sentences
1657     "c" gnus-article-remove-cr
1658     "Z" gnus-article-decode-HZ
1659     "f" gnus-article-display-x-face
1660     "l" gnus-summary-stop-page-breaking
1661     "r" gnus-summary-caesar-message
1662     "t" gnus-article-toggle-headers
1663     "v" gnus-summary-verbose-headers
1664     "m" gnus-summary-toggle-mime
1665     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1666     "p" gnus-article-verify-x-pgp-sig
1667     "d" gnus-article-treat-dumbquotes
1668     "s" gnus-smiley-display)
1669
1670   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1671     "a" gnus-article-hide
1672     "h" gnus-article-toggle-headers
1673     "b" gnus-article-hide-boring-headers
1674     "s" gnus-article-hide-signature
1675     "c" gnus-article-hide-citation
1676     "C" gnus-article-hide-citation-in-followups
1677     "l" gnus-article-hide-list-identifiers
1678     "p" gnus-article-hide-pgp
1679     "B" gnus-article-strip-banner
1680     "P" gnus-article-hide-pem
1681     "\C-c" gnus-article-hide-citation-maybe)
1682
1683   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1684     "a" gnus-article-highlight
1685     "h" gnus-article-highlight-headers
1686     "c" gnus-article-highlight-citation
1687     "s" gnus-article-highlight-signature)
1688
1689   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1690     "z" gnus-article-date-ut
1691     "u" gnus-article-date-ut
1692     "l" gnus-article-date-local
1693     "p" gnus-article-date-english
1694     "e" gnus-article-date-lapsed
1695     "o" gnus-article-date-original
1696     "i" gnus-article-date-iso8601
1697     "s" gnus-article-date-user)
1698
1699   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1700     "t" gnus-article-remove-trailing-blank-lines
1701     "l" gnus-article-strip-leading-blank-lines
1702     "m" gnus-article-strip-multiple-blank-lines
1703     "a" gnus-article-strip-blank-lines
1704     "A" gnus-article-strip-all-blank-lines
1705     "s" gnus-article-strip-leading-space
1706     "e" gnus-article-strip-trailing-space
1707     "w" gnus-article-remove-leading-whitespace)
1708
1709   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1710     "v" gnus-version
1711     "f" gnus-summary-fetch-faq
1712     "d" gnus-summary-describe-group
1713     "h" gnus-summary-describe-briefly
1714     "i" gnus-info-find-node)
1715
1716   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1717     "e" gnus-summary-expire-articles
1718     "\M-\C-e" gnus-summary-expire-articles-now
1719     "\177" gnus-summary-delete-article
1720     [delete] gnus-summary-delete-article
1721     [backspace] gnus-summary-delete-article
1722     "m" gnus-summary-move-article
1723     "r" gnus-summary-respool-article
1724     "w" gnus-summary-edit-article
1725     "c" gnus-summary-copy-article
1726     "B" gnus-summary-crosspost-article
1727     "q" gnus-summary-respool-query
1728     "t" gnus-summary-respool-trace
1729     "i" gnus-summary-import-article
1730     "I" gnus-summary-create-article
1731     "p" gnus-summary-article-posted-p)
1732
1733   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1734     "o" gnus-summary-save-article
1735     "m" gnus-summary-save-article-mail
1736     "F" gnus-summary-write-article-file
1737     "r" gnus-summary-save-article-rmail
1738     "f" gnus-summary-save-article-file
1739     "b" gnus-summary-save-article-body-file
1740     "h" gnus-summary-save-article-folder
1741     "v" gnus-summary-save-article-vm
1742     "p" gnus-summary-pipe-output
1743     "s" gnus-soup-add-article)
1744
1745   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1746     "b" gnus-summary-display-buttonized
1747     "m" gnus-summary-repair-multipart
1748     "v" gnus-article-view-part
1749     "o" gnus-article-save-part
1750     "c" gnus-article-copy-part
1751     "C" gnus-article-view-part-as-charset
1752     "e" gnus-article-externalize-part
1753     "E" gnus-article-encrypt-body
1754     "i" gnus-article-inline-part
1755     "|" gnus-article-pipe-part))
1756
1757 (defvar gnus-article-post-menu nil)
1758
1759 (defun gnus-summary-make-menu-bar ()
1760   (gnus-turn-off-edit-menu 'summary)
1761
1762   (unless (boundp 'gnus-summary-misc-menu)
1763
1764     (easy-menu-define
1765      gnus-summary-kill-menu gnus-summary-mode-map ""
1766      (cons
1767       "Score"
1768       (nconc
1769        (list
1770         ["Customize" gnus-score-customize t])
1771        (gnus-make-score-map 'increase)
1772        (gnus-make-score-map 'lower)
1773        '(("Mark"
1774           ["Kill below" gnus-summary-kill-below t]
1775           ["Mark above" gnus-summary-mark-above t]
1776           ["Tick above" gnus-summary-tick-above t]
1777           ["Clear above" gnus-summary-clear-above t])
1778          ["Current score" gnus-summary-current-score t]
1779          ["Set score" gnus-summary-set-score t]
1780          ["Switch current score file..." gnus-score-change-score-file t]
1781          ["Set mark below..." gnus-score-set-mark-below t]
1782          ["Set expunge below..." gnus-score-set-expunge-below t]
1783          ["Edit current score file" gnus-score-edit-current-scores t]
1784          ["Edit score file" gnus-score-edit-file t]
1785          ["Trace score" gnus-score-find-trace t]
1786          ["Find words" gnus-score-find-favourite-words t]
1787          ["Rescore buffer" gnus-summary-rescore t]
1788          ["Increase score..." gnus-summary-increase-score t]
1789          ["Lower score..." gnus-summary-lower-score t]))))
1790
1791     ;; Define both the Article menu in the summary buffer and the equivalent
1792     ;; Commands menu in the article buffer here for consistency.
1793     (let ((innards
1794            `(("Hide"
1795               ["All" gnus-article-hide t]
1796               ["Headers" gnus-article-toggle-headers t]
1797               ["Signature" gnus-article-hide-signature t]
1798               ["Citation" gnus-article-hide-citation t]
1799               ["List identifiers" gnus-article-hide-list-identifiers t]
1800               ["PGP" gnus-article-hide-pgp t]
1801               ["Banner" gnus-article-strip-banner t]
1802               ["Boring headers" gnus-article-hide-boring-headers t])
1803              ("Highlight"
1804               ["All" gnus-article-highlight t]
1805               ["Headers" gnus-article-highlight-headers t]
1806               ["Signature" gnus-article-highlight-signature t]
1807               ["Citation" gnus-article-highlight-citation t])
1808              ("Date"
1809               ["Local" gnus-article-date-local t]
1810               ["ISO8601" gnus-article-date-iso8601 t]
1811               ["UT" gnus-article-date-ut t]
1812               ["Original" gnus-article-date-original t]
1813               ["Lapsed" gnus-article-date-lapsed t]
1814               ["User-defined" gnus-article-date-user t])
1815              ("Washing"
1816               ("Remove Blanks"
1817                ["Leading" gnus-article-strip-leading-blank-lines t]
1818                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1819                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1820                ["All of the above" gnus-article-strip-blank-lines t]
1821                ["All" gnus-article-strip-all-blank-lines t]
1822                ["Leading space" gnus-article-strip-leading-space t]
1823                ["Trailing space" gnus-article-strip-trailing-space t]
1824                ["Leading space in headers" 
1825                 gnus-article-remove-leading-whitespace t])
1826               ["Overstrike" gnus-article-treat-overstrike t]
1827               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1828               ["Emphasis" gnus-article-emphasize t]
1829               ["Word wrap" gnus-article-fill-cited-article t]
1830               ["Fill long lines" gnus-article-fill-long-lines t]
1831               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1832               ["CR" gnus-article-remove-cr t]
1833               ["Show X-Face" gnus-article-display-x-face t]
1834               ["Rot 13" gnus-summary-caesar-message
1835                ,@(if (featurep 'xemacs) '(t)
1836                    '(:help "\"Caesar rotate\" article by 13"))]
1837               ["Unix pipe" gnus-summary-pipe-message t]
1838               ["Add buttons" gnus-article-add-buttons t]
1839               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1840               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1841               ["Toggle MIME" gnus-summary-toggle-mime t]
1842               ["Verbose header" gnus-summary-verbose-headers t]
1843               ["Toggle header" gnus-summary-toggle-header t]
1844               ["Toggle smileys" gnus-smiley-display t]
1845               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1846               ["HZ" gnus-article-decode-HZ t])
1847              ("Output"
1848               ["Save in default format" gnus-summary-save-article
1849                ,@(if (featurep 'xemacs) '(t)
1850                    '(:help "Save article using default method"))]
1851               ["Save in file" gnus-summary-save-article-file
1852                ,@(if (featurep 'xemacs) '(t)
1853                    '(:help "Save article in file"))]
1854               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1855               ["Save in MH folder" gnus-summary-save-article-folder t]
1856               ["Save in VM folder" gnus-summary-save-article-vm t]
1857               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1858               ["Save body in file" gnus-summary-save-article-body-file t]
1859               ["Pipe through a filter" gnus-summary-pipe-output t]
1860               ["Add to SOUP packet" gnus-soup-add-article t]
1861               ["Print" gnus-summary-print-article t])
1862              ("Backend"
1863               ["Respool article..." gnus-summary-respool-article t]
1864               ["Move article..." gnus-summary-move-article
1865                (gnus-check-backend-function
1866                 'request-move-article gnus-newsgroup-name)]
1867               ["Copy article..." gnus-summary-copy-article t]
1868               ["Crosspost article..." gnus-summary-crosspost-article
1869                (gnus-check-backend-function
1870                 'request-replace-article gnus-newsgroup-name)]
1871               ["Import file..." gnus-summary-import-article t]
1872               ["Create article..." gnus-summary-create-article t]
1873               ["Check if posted" gnus-summary-article-posted-p t]
1874               ["Edit article" gnus-summary-edit-article
1875                (not (gnus-group-read-only-p))]
1876               ["Delete article" gnus-summary-delete-article
1877                (gnus-check-backend-function
1878                 'request-expire-articles gnus-newsgroup-name)]
1879               ["Query respool" gnus-summary-respool-query t]
1880               ["Trace respool" gnus-summary-respool-trace t]
1881               ["Delete expirable articles" gnus-summary-expire-articles-now
1882                (gnus-check-backend-function
1883                 'request-expire-articles gnus-newsgroup-name)])
1884              ("Extract"
1885               ["Uudecode" gnus-uu-decode-uu
1886                ,@(if (featurep 'xemacs) '(t)
1887                    '(:help "Decode uuencoded article(s)"))]
1888               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1889               ["Unshar" gnus-uu-decode-unshar t]
1890               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1891               ["Save" gnus-uu-decode-save t]
1892               ["Binhex" gnus-uu-decode-binhex t]
1893               ["Postscript" gnus-uu-decode-postscript t])
1894              ("Cache"
1895               ["Enter article" gnus-cache-enter-article t]
1896               ["Remove article" gnus-cache-remove-article t])
1897              ["Translate" gnus-article-babel t]
1898              ["Select article buffer" gnus-summary-select-article-buffer t]
1899              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1900              ["Isearch article..." gnus-summary-isearch-article t]
1901              ["Beginning of the article" gnus-summary-beginning-of-article t]
1902              ["End of the article" gnus-summary-end-of-article t]
1903              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1904              ["Fetch referenced articles" gnus-summary-refer-references t]
1905              ["Fetch current thread" gnus-summary-refer-thread t]
1906              ["Fetch article with id..." gnus-summary-refer-article t]
1907              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1908              ["Redisplay" gnus-summary-show-article t])))
1909       (easy-menu-define
1910        gnus-summary-article-menu gnus-summary-mode-map ""
1911        (cons "Article" innards))
1912
1913       (if (not (keymapp gnus-summary-article-menu))
1914           (easy-menu-define
1915             gnus-article-commands-menu gnus-article-mode-map ""
1916             (cons "Commands" innards))
1917         ;; in Emacs, don't share menu.
1918         (setq gnus-article-commands-menu 
1919               (copy-keymap gnus-summary-article-menu))
1920         (define-key gnus-article-mode-map [menu-bar commands]
1921           (cons "Commands" gnus-article-commands-menu))))
1922
1923     (easy-menu-define
1924      gnus-summary-thread-menu gnus-summary-mode-map ""
1925      '("Threads"
1926        ["Toggle threading" gnus-summary-toggle-threads t]
1927        ["Hide threads" gnus-summary-hide-all-threads t]
1928        ["Show threads" gnus-summary-show-all-threads t]
1929        ["Hide thread" gnus-summary-hide-thread t]
1930        ["Show thread" gnus-summary-show-thread t]
1931        ["Go to next thread" gnus-summary-next-thread t]
1932        ["Go to previous thread" gnus-summary-prev-thread t]
1933        ["Go down thread" gnus-summary-down-thread t]
1934        ["Go up thread" gnus-summary-up-thread t]
1935        ["Top of thread" gnus-summary-top-thread t]
1936        ["Mark thread as read" gnus-summary-kill-thread t]
1937        ["Lower thread score" gnus-summary-lower-thread t]
1938        ["Raise thread score" gnus-summary-raise-thread t]
1939        ["Rethread current" gnus-summary-rethread-current t]))
1940
1941     (easy-menu-define
1942      gnus-summary-post-menu gnus-summary-mode-map ""
1943      `("Post"
1944        ["Post an article" gnus-summary-post-news
1945         ,@(if (featurep 'xemacs) '(t)
1946             '(:help "Post an article"))]
1947        ["Followup" gnus-summary-followup
1948         ,@(if (featurep 'xemacs) '(t)
1949             '(:help "Post followup to this article"))]
1950        ["Followup and yank" gnus-summary-followup-with-original
1951         ,@(if (featurep 'xemacs) '(t)
1952             '(:help "Post followup to this article, quoting its contents"))]
1953        ["Supersede article" gnus-summary-supersede-article t]
1954        ["Cancel article" gnus-summary-cancel-article
1955         ,@(if (featurep 'xemacs) '(t)
1956             '(:help "Cancel an article you posted"))]
1957        ["Reply" gnus-summary-reply t]
1958        ["Reply and yank" gnus-summary-reply-with-original t]
1959        ["Wide reply" gnus-summary-wide-reply t]
1960        ["Wide reply and yank" gnus-summary-wide-reply-with-original
1961         ,@(if (featurep 'xemacs) '(t)
1962             '(:help "Mail a reply, quoting this article"))]
1963        ["Mail forward" gnus-summary-mail-forward t]
1964        ["Post forward" gnus-summary-post-forward t]
1965        ["Digest and mail" gnus-summary-digest-mail-forward t]
1966        ["Digest and post" gnus-summary-digest-post-forward t]
1967        ["Resend message" gnus-summary-resend-message t]
1968        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1969        ["Send a mail" gnus-summary-mail-other-window t]
1970        ["Uuencode and post" gnus-uu-post-news
1971         ,@(if (featurep 'xemacs) '(t)
1972             '(:help "Post a uuencoded article"))]
1973        ["Followup via news" gnus-summary-followup-to-mail t]
1974        ["Followup via news and yank"
1975         gnus-summary-followup-to-mail-with-original t]
1976        ;;("Draft"
1977        ;;["Send" gnus-summary-send-draft t]
1978        ;;["Send bounced" gnus-resend-bounced-mail t])
1979        ))
1980
1981     (cond 
1982      ((not (keymapp gnus-summary-post-menu))
1983       (setq gnus-article-post-menu gnus-summary-post-menu))
1984      ((not gnus-article-post-menu)
1985       ;; Don't share post menu.
1986       (setq gnus-article-post-menu
1987             (copy-keymap gnus-summary-post-menu))))
1988     (define-key gnus-article-mode-map [menu-bar post]
1989       (cons "Post" gnus-article-post-menu))
1990
1991     (easy-menu-define
1992      gnus-summary-misc-menu gnus-summary-mode-map ""
1993      `("Misc"
1994        ("Mark Read"
1995         ["Mark as read" gnus-summary-mark-as-read-forward t]
1996         ["Mark same subject and select"
1997          gnus-summary-kill-same-subject-and-select t]
1998         ["Mark same subject" gnus-summary-kill-same-subject t]
1999         ["Catchup" gnus-summary-catchup
2000          ,@(if (featurep 'xemacs) '(t)
2001              '(:help "Mark unread articles in this group as read"))]
2002         ["Catchup all" gnus-summary-catchup-all t]
2003         ["Catchup to here" gnus-summary-catchup-to-here t]
2004         ["Catchup region" gnus-summary-mark-region-as-read t]
2005         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2006        ("Mark Various"
2007         ["Tick" gnus-summary-tick-article-forward t]
2008         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2009         ["Remove marks" gnus-summary-clear-mark-forward t]
2010         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2011         ["Set bookmark" gnus-summary-set-bookmark t]
2012         ["Remove bookmark" gnus-summary-remove-bookmark t])
2013        ("Mark Limit"
2014         ["Marks..." gnus-summary-limit-to-marks t]
2015         ["Subject..." gnus-summary-limit-to-subject t]
2016         ["Author..." gnus-summary-limit-to-author t]
2017         ["Age..." gnus-summary-limit-to-age t]
2018         ["Extra..." gnus-summary-limit-to-extra t]
2019         ["Score" gnus-summary-limit-to-score t]
2020         ["Unread" gnus-summary-limit-to-unread t]
2021         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2022         ["Articles" gnus-summary-limit-to-articles t]
2023         ["Pop limit" gnus-summary-pop-limit t]
2024         ["Show dormant" gnus-summary-limit-include-dormant t]
2025         ["Hide childless dormant"
2026          gnus-summary-limit-exclude-childless-dormant t]
2027         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2028         ["Hide marked" gnus-summary-limit-exclude-marks t]
2029         ["Show expunged" gnus-summary-limit-include-expunged t])
2030        ("Process Mark"
2031         ["Set mark" gnus-summary-mark-as-processable t]
2032         ["Remove mark" gnus-summary-unmark-as-processable t]
2033         ["Remove all marks" gnus-summary-unmark-all-processable t]
2034         ["Mark above" gnus-uu-mark-over t]
2035         ["Mark series" gnus-uu-mark-series t]
2036         ["Mark region" gnus-uu-mark-region t]
2037         ["Unmark region" gnus-uu-unmark-region t]
2038         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2039         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2040         ["Mark all" gnus-uu-mark-all t]
2041         ["Mark buffer" gnus-uu-mark-buffer t]
2042         ["Mark sparse" gnus-uu-mark-sparse t]
2043         ["Mark thread" gnus-uu-mark-thread t]
2044         ["Unmark thread" gnus-uu-unmark-thread t]
2045         ("Process Mark Sets"
2046          ["Kill" gnus-summary-kill-process-mark t]
2047          ["Yank" gnus-summary-yank-process-mark
2048           gnus-newsgroup-process-stack]
2049          ["Save" gnus-summary-save-process-mark t]))
2050        ("Scroll article"
2051         ["Page forward" gnus-summary-next-page
2052          ,@(if (featurep 'xemacs) '(t)
2053              '(:help "Show next page of article"))]
2054         ["Page backward" gnus-summary-prev-page
2055          ,@(if (featurep 'xemacs) '(t)
2056              '(:help "Show previous page of article"))]
2057         ["Line forward" gnus-summary-scroll-up t])
2058        ("Move"
2059         ["Next unread article" gnus-summary-next-unread-article t]
2060         ["Previous unread article" gnus-summary-prev-unread-article t]
2061         ["Next article" gnus-summary-next-article t]
2062         ["Previous article" gnus-summary-prev-article t]
2063         ["Next unread subject" gnus-summary-next-unread-subject t]
2064         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2065         ["Next article same subject" gnus-summary-next-same-subject t]
2066         ["Previous article same subject" gnus-summary-prev-same-subject t]
2067         ["First unread article" gnus-summary-first-unread-article t]
2068         ["Best unread article" gnus-summary-best-unread-article t]
2069         ["Go to subject number..." gnus-summary-goto-subject t]
2070         ["Go to article number..." gnus-summary-goto-article t]
2071         ["Go to the last article" gnus-summary-goto-last-article t]
2072         ["Pop article off history" gnus-summary-pop-article t])
2073        ("Sort"
2074         ["Sort by number" gnus-summary-sort-by-number t]
2075         ["Sort by author" gnus-summary-sort-by-author t]
2076         ["Sort by subject" gnus-summary-sort-by-subject t]
2077         ["Sort by date" gnus-summary-sort-by-date t]
2078         ["Sort by score" gnus-summary-sort-by-score t]
2079         ["Sort by lines" gnus-summary-sort-by-lines t]
2080         ["Sort by characters" gnus-summary-sort-by-chars t]
2081         ["Original sort" gnus-summary-sort-by-original t])
2082        ("Help"
2083         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2084         ["Describe group" gnus-summary-describe-group t]
2085         ["Read manual" gnus-info-find-node t])
2086        ("Modes"
2087         ["Pick and read" gnus-pick-mode t]
2088         ["Binary" gnus-binary-mode t])
2089        ("Regeneration"
2090         ["Regenerate" gnus-summary-prepare t]
2091         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2092         ["Toggle threading" gnus-summary-toggle-threads t])
2093        ["See old articles" gnus-summary-insert-old-articles t]
2094        ["See new articles" gnus-summary-insert-new-articles t]
2095        ["Filter articles..." gnus-summary-execute-command t]
2096        ["Run command on subjects..." gnus-summary-universal-argument t]
2097        ["Search articles forward..." gnus-summary-search-article-forward t]
2098        ["Search articles backward..." gnus-summary-search-article-backward t]
2099        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2100        ["Expand window" gnus-summary-expand-window t]
2101        ["Expire expirable articles" gnus-summary-expire-articles
2102         (gnus-check-backend-function
2103          'request-expire-articles gnus-newsgroup-name)]
2104        ["Edit local kill file" gnus-summary-edit-local-kill t]
2105        ["Edit main kill file" gnus-summary-edit-global-kill t]
2106        ["Edit group parameters" gnus-summary-edit-parameters t]
2107        ["Customize group parameters" gnus-summary-customize-parameters t]
2108        ["Send a bug report" gnus-bug t]
2109        ("Exit"
2110         ["Catchup and exit" gnus-summary-catchup-and-exit
2111          ,@(if (featurep 'xemacs) '(t)
2112              '(:help "Mark unread articles in this group as read, then exit"))]
2113         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2114         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2115         ["Exit group" gnus-summary-exit
2116          ,@(if (featurep 'xemacs) '(t)
2117              '(:help "Exit current group, return to group selection mode"))]
2118         ["Exit group without updating" gnus-summary-exit-no-update t]
2119         ["Exit and goto next group" gnus-summary-next-group t]
2120         ["Exit and goto prev group" gnus-summary-prev-group t]
2121         ["Reselect group" gnus-summary-reselect-current-group t]
2122         ["Rescan group" gnus-summary-rescan-group t]
2123         ["Update dribble" gnus-summary-save-newsrc t])))
2124
2125     (gnus-run-hooks 'gnus-summary-menu-hook)))
2126
2127 (defvar gnus-summary-tool-bar-map nil)
2128
2129 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2130 (defun gnus-summary-make-tool-bar ()
2131   (if (and (fboundp 'tool-bar-add-item-from-menu)
2132            (default-value 'tool-bar-mode)
2133            (not gnus-summary-tool-bar-map))
2134       (setq gnus-summary-tool-bar-map
2135             (let ((tool-bar-map (make-sparse-keymap))
2136                   (load-path (mm-image-load-path)))
2137               (tool-bar-add-item-from-menu
2138                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2139               (tool-bar-add-item-from-menu
2140                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2141               (tool-bar-add-item-from-menu
2142                'gnus-summary-post-news "post" gnus-summary-mode-map)
2143               (tool-bar-add-item-from-menu
2144                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2145               (tool-bar-add-item-from-menu
2146                'gnus-summary-followup "followup" gnus-summary-mode-map)
2147               (tool-bar-add-item-from-menu
2148                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2149               (tool-bar-add-item-from-menu
2150                'gnus-summary-reply "reply" gnus-summary-mode-map)
2151               (tool-bar-add-item-from-menu
2152                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2153               (tool-bar-add-item-from-menu
2154                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2155               (tool-bar-add-item-from-menu
2156                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2157               (tool-bar-add-item-from-menu
2158                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2159               (tool-bar-add-item-from-menu
2160                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2161               (tool-bar-add-item-from-menu
2162                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2163               (tool-bar-add-item-from-menu
2164                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2165               (tool-bar-add-item-from-menu
2166                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2167               tool-bar-map)))
2168   (if gnus-summary-tool-bar-map
2169       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2170
2171 (defun gnus-score-set-default (var value)
2172   "A version of set that updates the GNU Emacs menu-bar."
2173   (set var value)
2174   ;; It is the message that forces the active status to be updated.
2175   (message ""))
2176
2177 (defun gnus-make-score-map (type)
2178   "Make a summary score map of type TYPE."
2179   (if t
2180       nil
2181     (let ((headers '(("author" "from" string)
2182                      ("subject" "subject" string)
2183                      ("article body" "body" string)
2184                      ("article head" "head" string)
2185                      ("xref" "xref" string)
2186                      ("extra header" "extra" string)
2187                      ("lines" "lines" number)
2188                      ("followups to author" "followup" string)))
2189           (types '((number ("less than" <)
2190                            ("greater than" >)
2191                            ("equal" =))
2192                    (string ("substring" s)
2193                            ("exact string" e)
2194                            ("fuzzy string" f)
2195                            ("regexp" r))))
2196           (perms '(("temporary" (current-time-string))
2197                    ("permanent" nil)
2198                    ("immediate" now)))
2199           header)
2200       (list
2201        (apply
2202         'nconc
2203         (list
2204          (if (eq type 'lower)
2205              "Lower score"
2206            "Increase score"))
2207         (let (outh)
2208           (while headers
2209             (setq header (car headers))
2210             (setq outh
2211                   (cons
2212                    (apply
2213                     'nconc
2214                     (list (car header))
2215                     (let ((ts (cdr (assoc (nth 2 header) types)))
2216                           outt)
2217                       (while ts
2218                         (setq outt
2219                               (cons
2220                                (apply
2221                                 'nconc
2222                                 (list (caar ts))
2223                                 (let ((ps perms)
2224                                       outp)
2225                                   (while ps
2226                                     (setq outp
2227                                           (cons
2228                                            (vector
2229                                             (caar ps)
2230                                             (list
2231                                              'gnus-summary-score-entry
2232                                              (nth 1 header)
2233                                              (if (or (string= (nth 1 header)
2234                                                               "head")
2235                                                      (string= (nth 1 header)
2236                                                               "body"))
2237                                                  ""
2238                                                (list 'gnus-summary-header
2239                                                      (nth 1 header)))
2240                                              (list 'quote (nth 1 (car ts)))
2241                                              (list 'gnus-score-delta-default
2242                                                    nil)
2243                                              (nth 1 (car ps))
2244                                              t)
2245                                             t)
2246                                            outp))
2247                                     (setq ps (cdr ps)))
2248                                   (list (nreverse outp))))
2249                                outt))
2250                         (setq ts (cdr ts)))
2251                       (list (nreverse outt))))
2252                    outh))
2253             (setq headers (cdr headers)))
2254           (list (nreverse outh))))))))
2255
2256 \f
2257
2258 (defun gnus-summary-mode (&optional group)
2259   "Major mode for reading articles.
2260
2261 All normal editing commands are switched off.
2262 \\<gnus-summary-mode-map>
2263 Each line in this buffer represents one article.  To read an
2264 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2265 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2266 respectively.
2267
2268 You can also post articles and send mail from this buffer.  To
2269 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2270 of an article, type `\\[gnus-summary-reply]'.
2271
2272 There are approx. one gazillion commands you can execute in this
2273 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2274
2275 The following commands are available:
2276
2277 \\{gnus-summary-mode-map}"
2278   (interactive)
2279   (kill-all-local-variables)
2280   (when (gnus-visual-p 'summary-menu 'menu)
2281     (gnus-summary-make-menu-bar)
2282     (gnus-summary-make-tool-bar))
2283   (gnus-summary-make-local-variables)
2284   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2285     (gnus-summary-make-local-variables))
2286   (gnus-make-thread-indent-array)
2287   (gnus-simplify-mode-line)
2288   (setq major-mode 'gnus-summary-mode)
2289   (setq mode-name "Summary")
2290   (make-local-variable 'minor-mode-alist)
2291   (use-local-map gnus-summary-mode-map)
2292   (buffer-disable-undo)
2293   (setq buffer-read-only t)             ;Disable modification
2294   (setq truncate-lines t)
2295   (setq selective-display t)
2296   (setq selective-display-ellipses t)   ;Display `...'
2297   (gnus-summary-set-display-table)
2298   (gnus-set-default-directory)
2299   (setq gnus-newsgroup-name group)
2300   (unless (gnus-news-group-p group)
2301     (setq gnus-newsgroup-incorporated
2302           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2303   (make-local-variable 'gnus-summary-line-format)
2304   (make-local-variable 'gnus-summary-line-format-spec)
2305   (make-local-variable 'gnus-summary-dummy-line-format)
2306   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2307   (make-local-variable 'gnus-summary-mark-positions)
2308   (make-local-hook 'pre-command-hook)
2309   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2310   (gnus-run-hooks 'gnus-summary-mode-hook)
2311   (turn-on-gnus-mailing-list-mode)
2312   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2313   (gnus-update-summary-mark-positions))
2314
2315 (defun gnus-summary-make-local-variables ()
2316   "Make all the local summary buffer variables."
2317   (let (global)
2318     (dolist (local gnus-summary-local-variables)
2319       (if (consp local)
2320           (progn
2321             (if (eq (cdr local) 'global)
2322                 ;; Copy the global value of the variable.
2323                 (setq global (symbol-value (car local)))
2324               ;; Use the value from the list.
2325               (setq global (eval (cdr local))))
2326             (set (make-local-variable (car local)) global))
2327         ;; Simple nil-valued local variable.
2328         (set (make-local-variable local) nil)))))
2329
2330 (defun gnus-summary-clear-local-variables ()
2331   (let ((locals gnus-summary-local-variables))
2332     (while locals
2333       (if (consp (car locals))
2334           (and (vectorp (caar locals))
2335                (set (caar locals) nil))
2336         (and (vectorp (car locals))
2337              (set (car locals) nil)))
2338       (setq locals (cdr locals)))))
2339
2340 ;; Summary data functions.
2341
2342 (defmacro gnus-data-number (data)
2343   `(car ,data))
2344
2345 (defmacro gnus-data-set-number (data number)
2346   `(setcar ,data ,number))
2347
2348 (defmacro gnus-data-mark (data)
2349   `(nth 1 ,data))
2350
2351 (defmacro gnus-data-set-mark (data mark)
2352   `(setcar (nthcdr 1 ,data) ,mark))
2353
2354 (defmacro gnus-data-pos (data)
2355   `(nth 2 ,data))
2356
2357 (defmacro gnus-data-set-pos (data pos)
2358   `(setcar (nthcdr 2 ,data) ,pos))
2359
2360 (defmacro gnus-data-header (data)
2361   `(nth 3 ,data))
2362
2363 (defmacro gnus-data-set-header (data header)
2364   `(setcar (nthcdr 3 ,data) ,header))
2365
2366 (defmacro gnus-data-level (data)
2367   `(nth 4 ,data))
2368
2369 (defmacro gnus-data-unread-p (data)
2370   `(= (nth 1 ,data) gnus-unread-mark))
2371
2372 (defmacro gnus-data-read-p (data)
2373   `(/= (nth 1 ,data) gnus-unread-mark))
2374
2375 (defmacro gnus-data-pseudo-p (data)
2376   `(consp (nth 3 ,data)))
2377
2378 (defmacro gnus-data-find (number)
2379   `(assq ,number gnus-newsgroup-data))
2380
2381 (defmacro gnus-data-find-list (number &optional data)
2382   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2383      (memq (assq ,number bdata)
2384            bdata)))
2385
2386 (defmacro gnus-data-make (number mark pos header level)
2387   `(list ,number ,mark ,pos ,header ,level))
2388
2389 (defun gnus-data-enter (after-article number mark pos header level offset)
2390   (let ((data (gnus-data-find-list after-article)))
2391     (unless data
2392       (error "No such article: %d" after-article))
2393     (setcdr data (cons (gnus-data-make number mark pos header level)
2394                        (cdr data)))
2395     (setq gnus-newsgroup-data-reverse nil)
2396     (gnus-data-update-list (cddr data) offset)))
2397
2398 (defun gnus-data-enter-list (after-article list &optional offset)
2399   (when list
2400     (let ((data (and after-article (gnus-data-find-list after-article)))
2401           (ilist list))
2402       (if (not (or data
2403                    after-article))
2404           (let ((odata gnus-newsgroup-data))
2405             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2406             (when offset
2407               (gnus-data-update-list odata offset)))
2408         ;; Find the last element in the list to be spliced into the main
2409         ;; list.
2410         (while (cdr list)
2411           (setq list (cdr list)))
2412         (if (not data)
2413             (progn
2414               (setcdr list gnus-newsgroup-data)
2415               (setq gnus-newsgroup-data ilist)
2416               (when offset
2417                 (gnus-data-update-list (cdr list) offset)))
2418           (setcdr list (cdr data))
2419           (setcdr data ilist)
2420           (when offset
2421             (gnus-data-update-list (cdr list) offset))))
2422       (setq gnus-newsgroup-data-reverse nil))))
2423
2424 (defun gnus-data-remove (article &optional offset)
2425   (let ((data gnus-newsgroup-data))
2426     (if (= (gnus-data-number (car data)) article)
2427         (progn
2428           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2429                 gnus-newsgroup-data-reverse nil)
2430           (when offset
2431             (gnus-data-update-list gnus-newsgroup-data offset)))
2432       (while (cdr data)
2433         (when (= (gnus-data-number (cadr data)) article)
2434           (setcdr data (cddr data))
2435           (when offset
2436             (gnus-data-update-list (cdr data) offset))
2437           (setq data nil
2438                 gnus-newsgroup-data-reverse nil))
2439         (setq data (cdr data))))))
2440
2441 (defmacro gnus-data-list (backward)
2442   `(if ,backward
2443        (or gnus-newsgroup-data-reverse
2444            (setq gnus-newsgroup-data-reverse
2445                  (reverse gnus-newsgroup-data)))
2446      gnus-newsgroup-data))
2447
2448 (defun gnus-data-update-list (data offset)
2449   "Add OFFSET to the POS of all data entries in DATA."
2450   (setq gnus-newsgroup-data-reverse nil)
2451   (while data
2452     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2453     (setq data (cdr data))))
2454
2455 (defun gnus-summary-article-pseudo-p (article)
2456   "Say whether this article is a pseudo article or not."
2457   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2458
2459 (defmacro gnus-summary-article-sparse-p (article)
2460   "Say whether this article is a sparse article or not."
2461   `(memq ,article gnus-newsgroup-sparse))
2462
2463 (defmacro gnus-summary-article-ancient-p (article)
2464   "Say whether this article is a sparse article or not."
2465   `(memq ,article gnus-newsgroup-ancient))
2466
2467 (defun gnus-article-parent-p (number)
2468   "Say whether this article is a parent or not."
2469   (let ((data (gnus-data-find-list number)))
2470     (and (cdr data)                     ; There has to be an article after...
2471          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2472             (gnus-data-level (nth 1 data))))))
2473
2474 (defun gnus-article-children (number)
2475   "Return a list of all children to NUMBER."
2476   (let* ((data (gnus-data-find-list number))
2477          (level (gnus-data-level (car data)))
2478          children)
2479     (setq data (cdr data))
2480     (while (and data
2481                 (= (gnus-data-level (car data)) (1+ level)))
2482       (push (gnus-data-number (car data)) children)
2483       (setq data (cdr data)))
2484     children))
2485
2486 (defmacro gnus-summary-skip-intangible ()
2487   "If the current article is intangible, then jump to a different article."
2488   '(let ((to (get-text-property (point) 'gnus-intangible)))
2489      (and to (gnus-summary-goto-subject to))))
2490
2491 (defmacro gnus-summary-article-intangible-p ()
2492   "Say whether this article is intangible or not."
2493   '(get-text-property (point) 'gnus-intangible))
2494
2495 (defun gnus-article-read-p (article)
2496   "Say whether ARTICLE is read or not."
2497   (not (or (memq article gnus-newsgroup-marked)
2498            (memq article gnus-newsgroup-unreads)
2499            (memq article gnus-newsgroup-unselected)
2500            (memq article gnus-newsgroup-dormant))))
2501
2502 ;; Some summary mode macros.
2503
2504 (defmacro gnus-summary-article-number ()
2505   "The article number of the article on the current line.
2506 If there isn's an article number here, then we return the current
2507 article number."
2508   '(progn
2509      (gnus-summary-skip-intangible)
2510      (or (get-text-property (point) 'gnus-number)
2511          (gnus-summary-last-subject))))
2512
2513 (defmacro gnus-summary-article-header (&optional number)
2514   "Return the header of article NUMBER."
2515   `(gnus-data-header (gnus-data-find
2516                       ,(or number '(gnus-summary-article-number)))))
2517
2518 (defmacro gnus-summary-thread-level (&optional number)
2519   "Return the level of thread that starts with article NUMBER."
2520   `(if (and (eq gnus-summary-make-false-root 'dummy)
2521             (get-text-property (point) 'gnus-intangible))
2522        0
2523      (gnus-data-level (gnus-data-find
2524                        ,(or number '(gnus-summary-article-number))))))
2525
2526 (defmacro gnus-summary-article-mark (&optional number)
2527   "Return the mark of article NUMBER."
2528   `(gnus-data-mark (gnus-data-find
2529                     ,(or number '(gnus-summary-article-number)))))
2530
2531 (defmacro gnus-summary-article-pos (&optional number)
2532   "Return the position of the line of article NUMBER."
2533   `(gnus-data-pos (gnus-data-find
2534                    ,(or number '(gnus-summary-article-number)))))
2535
2536 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2537 (defmacro gnus-summary-article-subject (&optional number)
2538   "Return current subject string or nil if nothing."
2539   `(let ((headers
2540           ,(if number
2541                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2542              '(gnus-data-header (assq (gnus-summary-article-number)
2543                                       gnus-newsgroup-data)))))
2544      (and headers
2545           (vectorp headers)
2546           (mail-header-subject headers))))
2547
2548 (defmacro gnus-summary-article-score (&optional number)
2549   "Return current article score."
2550   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2551                   gnus-newsgroup-scored))
2552        gnus-summary-default-score 0))
2553
2554 (defun gnus-summary-article-children (&optional number)
2555   "Return a list of article numbers that are children of article NUMBER."
2556   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2557          (level (gnus-data-level (car data)))
2558          l children)
2559     (while (and (setq data (cdr data))
2560                 (> (setq l (gnus-data-level (car data))) level))
2561       (and (= (1+ level) l)
2562            (push (gnus-data-number (car data))
2563                  children)))
2564     (nreverse children)))
2565
2566 (defun gnus-summary-article-parent (&optional number)
2567   "Return the article number of the parent of article NUMBER."
2568   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2569                                     (gnus-data-list t)))
2570          (level (gnus-data-level (car data))))
2571     (if (zerop level)
2572         ()                              ; This is a root.
2573       ;; We search until we find an article with a level less than
2574       ;; this one.  That function has to be the parent.
2575       (while (and (setq data (cdr data))
2576                   (not (< (gnus-data-level (car data)) level))))
2577       (and data (gnus-data-number (car data))))))
2578
2579 (defun gnus-unread-mark-p (mark)
2580   "Say whether MARK is the unread mark."
2581   (= mark gnus-unread-mark))
2582
2583 (defun gnus-read-mark-p (mark)
2584   "Say whether MARK is one of the marks that mark as read.
2585 This is all marks except unread, ticked, dormant, and expirable."
2586   (not (or (= mark gnus-unread-mark)
2587            (= mark gnus-ticked-mark)
2588            (= mark gnus-dormant-mark)
2589            (= mark gnus-expirable-mark))))
2590
2591 (defmacro gnus-article-mark (number)
2592   "Return the MARK of article NUMBER.
2593 This macro should only be used when computing the mark the \"first\"
2594 time; i.e., when generating the summary lines.  After that,
2595 `gnus-summary-article-mark' should be used to examine the
2596 marks of articles."
2597   `(cond
2598     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2599     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2600     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2601     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2602     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2603     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2604     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2605     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2606            gnus-ancient-mark))))
2607
2608 ;; Saving hidden threads.
2609
2610 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2611 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2612
2613 (defmacro gnus-save-hidden-threads (&rest forms)
2614   "Save hidden threads, eval FORMS, and restore the hidden threads."
2615   (let ((config (make-symbol "config")))
2616     `(let ((,config (gnus-hidden-threads-configuration)))
2617        (unwind-protect
2618            (save-excursion
2619              ,@forms)
2620          (gnus-restore-hidden-threads-configuration ,config)))))
2621
2622 (defun gnus-data-compute-positions ()
2623   "Compute the positions of all articles."
2624   (setq gnus-newsgroup-data-reverse nil)
2625   (let ((data gnus-newsgroup-data))
2626     (save-excursion
2627       (gnus-save-hidden-threads
2628         (gnus-summary-show-all-threads)
2629         (goto-char (point-min))
2630         (while data
2631           (while (get-text-property (point) 'gnus-intangible)
2632             (forward-line 1))
2633           (gnus-data-set-pos (car data) (+ (point) 3))
2634           (setq data (cdr data))
2635           (forward-line 1))))))
2636
2637 (defun gnus-hidden-threads-configuration ()
2638   "Return the current hidden threads configuration."
2639   (save-excursion
2640     (let (config)
2641       (goto-char (point-min))
2642       (while (search-forward "\r" nil t)
2643         (push (1- (point)) config))
2644       config)))
2645
2646 (defun gnus-restore-hidden-threads-configuration (config)
2647   "Restore hidden threads configuration from CONFIG."
2648   (save-excursion
2649     (let (point buffer-read-only)
2650       (while (setq point (pop config))
2651         (when (and (< point (point-max))
2652                    (goto-char point)
2653                    (eq (char-after) ?\n))
2654           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2655
2656 ;; Various summary mode internalish functions.
2657
2658 (defun gnus-mouse-pick-article (e)
2659   (interactive "e")
2660   (mouse-set-point e)
2661   (gnus-summary-next-page nil t))
2662
2663 (defun gnus-summary-set-display-table ()
2664   "Change the display table.
2665 Odd characters have a tendency to mess
2666 up nicely formatted displays - we make all possible glyphs
2667 display only a single character."
2668
2669   ;; We start from the standard display table, if any.
2670   (let ((table (or (copy-sequence standard-display-table)
2671                    (make-display-table)))
2672         (i 32))
2673     ;; Nix out all the control chars...
2674     (while (>= (setq i (1- i)) 0)
2675       (aset table i [??]))
2676     ;; ... but not newline and cr, of course.  (cr is necessary for the
2677     ;; selective display).
2678     (aset table ?\n nil)
2679     (aset table ?\r nil)
2680     ;; We keep TAB as well.
2681     (aset table ?\t nil)
2682     ;; We nix out any glyphs over 126 that are not set already.
2683     (let ((i 256))
2684       (while (>= (setq i (1- i)) 127)
2685         ;; Only modify if the entry is nil.
2686         (unless (aref table i)
2687           (aset table i [??]))))
2688     (setq buffer-display-table table)))
2689
2690 (defun gnus-summary-buffer-name (group)
2691   "Return the summary buffer name of GROUP."
2692   (concat "*Summary " group "*"))
2693
2694 (defun gnus-summary-setup-buffer (group)
2695   "Initialize summary buffer."
2696   (let ((buffer (gnus-summary-buffer-name group)))
2697     (if (get-buffer buffer)
2698         (progn
2699           (set-buffer buffer)
2700           (setq gnus-summary-buffer (current-buffer))
2701           (not gnus-newsgroup-prepared))
2702       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2703       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2704       (gnus-summary-mode group)
2705       (when gnus-carpal
2706         (gnus-carpal-setup-buffer 'summary))
2707       (unless gnus-single-article-buffer
2708         (make-local-variable 'gnus-article-buffer)
2709         (make-local-variable 'gnus-article-current)
2710         (make-local-variable 'gnus-original-article-buffer))
2711       (setq gnus-newsgroup-name group)
2712       ;; Set any local variables in the group parameters.
2713       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2714       t)))
2715
2716 (defun gnus-set-global-variables ()
2717   "Set the global equivalents of the buffer-local variables.
2718 They are set to the latest values they had.  These reflect the summary
2719 buffer that was in action when the last article was fetched."
2720   (when (eq major-mode 'gnus-summary-mode)
2721     (setq gnus-summary-buffer (current-buffer))
2722     (let ((name gnus-newsgroup-name)
2723           (marked gnus-newsgroup-marked)
2724           (unread gnus-newsgroup-unreads)
2725           (headers gnus-current-headers)
2726           (data gnus-newsgroup-data)
2727           (summary gnus-summary-buffer)
2728           (article-buffer gnus-article-buffer)
2729           (original gnus-original-article-buffer)
2730           (gac gnus-article-current)
2731           (reffed gnus-reffed-article-number)
2732           (score-file gnus-current-score-file)
2733           (default-charset gnus-newsgroup-charset)
2734           vlist)
2735       (let ((locals gnus-newsgroup-variables))
2736         (while locals
2737           (if (consp (car locals))
2738               (push (eval (caar locals)) vlist)
2739             (push (eval (car locals)) vlist))
2740           (setq locals (cdr locals)))
2741         (setq vlist (nreverse vlist)))
2742       (save-excursion
2743         (set-buffer gnus-group-buffer)
2744         (setq gnus-newsgroup-name name
2745               gnus-newsgroup-marked marked
2746               gnus-newsgroup-unreads unread
2747               gnus-current-headers headers
2748               gnus-newsgroup-data data
2749               gnus-article-current gac
2750               gnus-summary-buffer summary
2751               gnus-article-buffer article-buffer
2752               gnus-original-article-buffer original
2753               gnus-reffed-article-number reffed
2754               gnus-current-score-file score-file
2755               gnus-newsgroup-charset default-charset)
2756         (let ((locals gnus-newsgroup-variables))
2757           (while locals
2758             (if (consp (car locals))
2759                 (set (caar locals) (pop vlist))
2760               (set (car locals) (pop vlist)))
2761             (setq locals (cdr locals))))
2762         ;; The article buffer also has local variables.
2763         (when (gnus-buffer-live-p gnus-article-buffer)
2764           (set-buffer gnus-article-buffer)
2765           (setq gnus-summary-buffer summary))))))
2766
2767 (defun gnus-summary-article-unread-p (article)
2768   "Say whether ARTICLE is unread or not."
2769   (memq article gnus-newsgroup-unreads))
2770
2771 (defun gnus-summary-first-article-p (&optional article)
2772   "Return whether ARTICLE is the first article in the buffer."
2773   (if (not (setq article (or article (gnus-summary-article-number))))
2774       nil
2775     (eq article (caar gnus-newsgroup-data))))
2776
2777 (defun gnus-summary-last-article-p (&optional article)
2778   "Return whether ARTICLE is the last article in the buffer."
2779   (if (not (setq article (or article (gnus-summary-article-number))))
2780       ;; All non-existent numbers are the last article.  :-)
2781       t
2782     (not (cdr (gnus-data-find-list article)))))
2783
2784 (defun gnus-make-thread-indent-array ()
2785   (let ((n 200))
2786     (unless (and gnus-thread-indent-array
2787                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2788       (setq gnus-thread-indent-array (make-vector 201 "")
2789             gnus-thread-indent-array-level gnus-thread-indent-level)
2790       (while (>= n 0)
2791         (aset gnus-thread-indent-array n
2792               (make-string (* n gnus-thread-indent-level) ? ))
2793         (setq n (1- n))))))
2794
2795 (defun gnus-update-summary-mark-positions ()
2796   "Compute where the summary marks are to go."
2797   (save-excursion
2798     (when (gnus-buffer-exists-p gnus-summary-buffer)
2799       (set-buffer gnus-summary-buffer))
2800     (let ((gnus-replied-mark 129)
2801           (gnus-score-below-mark 130)
2802           (gnus-score-over-mark 130)
2803           (gnus-download-mark 131)
2804           (spec gnus-summary-line-format-spec)
2805           gnus-visual pos)
2806       (save-excursion
2807         (gnus-set-work-buffer)
2808         (let ((gnus-summary-line-format-spec spec)
2809               (gnus-newsgroup-downloadable '((0 . t))))
2810           (gnus-summary-insert-line
2811            (make-full-mail-header 0 "" "nobody"
2812                                   "05 Apr 2001 23:33:09 +0400"
2813                                   "" "" 0 0 "" nil)
2814            0 nil 128 t nil "" nil 1)
2815           (goto-char (point-min))
2816           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2817                                              (- (point) 2)))))
2818           (goto-char (point-min))
2819           (push (cons 'replied (and (search-forward "\201" nil t)
2820                                     (- (point) 2)))
2821                 pos)
2822           (goto-char (point-min))
2823           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2824                 pos)
2825           (goto-char (point-min))
2826           (push (cons 'download
2827                       (and (search-forward "\203" nil t) (- (point) 2)))
2828                 pos)))
2829       (setq gnus-summary-mark-positions pos))))
2830
2831 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2832   "Insert a dummy root in the summary buffer."
2833   (beginning-of-line)
2834   (gnus-add-text-properties
2835    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2836    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2837
2838 (defun gnus-summary-from-or-to-or-newsgroups (header)
2839   (let ((to (cdr (assq 'To (mail-header-extra header))))
2840         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2841         (default-mime-charset (with-current-buffer gnus-summary-buffer
2842                                 default-mime-charset)))
2843     (cond
2844      ((and to
2845            gnus-ignored-from-addresses
2846            (string-match gnus-ignored-from-addresses
2847                          (mail-header-from header)))
2848       (concat "-> "
2849               (or (car (funcall gnus-extract-address-components
2850                                 (funcall
2851                                  gnus-decode-encoded-word-function to)))
2852                   (funcall gnus-decode-encoded-word-function to))))
2853      ((and newsgroups
2854            gnus-ignored-from-addresses
2855            (string-match gnus-ignored-from-addresses
2856                          (mail-header-from header)))
2857       (concat "=> " newsgroups))
2858      (t
2859       (or (car (funcall gnus-extract-address-components
2860                         (mail-header-from header)))
2861           (mail-header-from header))))))
2862
2863 (defun gnus-summary-insert-line (gnus-tmp-header
2864                                  gnus-tmp-level gnus-tmp-current
2865                                  gnus-tmp-unread gnus-tmp-replied
2866                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2867                                  &optional gnus-tmp-dummy gnus-tmp-score
2868                                  gnus-tmp-process)
2869   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2870          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2871          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2872          (gnus-tmp-score-char
2873           (if (or (null gnus-summary-default-score)
2874                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2875                       gnus-summary-zcore-fuzz))
2876               ?  ;Whitespace
2877             (if (< gnus-tmp-score gnus-summary-default-score)
2878                 gnus-score-below-mark gnus-score-over-mark)))
2879          (gnus-tmp-replied
2880           (cond (gnus-tmp-process gnus-process-mark)
2881                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2882                  gnus-cached-mark)
2883                 (gnus-tmp-replied gnus-replied-mark)
2884                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2885                  gnus-saved-mark)
2886                 (t gnus-no-mark)))
2887          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2888          (gnus-tmp-name
2889           (cond
2890            ((string-match "<[^>]+> *$" gnus-tmp-from)
2891             (let ((beg (match-beginning 0)))
2892               (or (and (string-match "^\".+\"" gnus-tmp-from)
2893                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2894                   (substring gnus-tmp-from 0 beg))))
2895            ((string-match "(.+)" gnus-tmp-from)
2896             (substring gnus-tmp-from
2897                        (1+ (match-beginning 0)) (1- (match-end 0))))
2898            (t gnus-tmp-from)))
2899          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2900          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2901          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2902          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2903          (buffer-read-only nil))
2904     (when (string= gnus-tmp-name "")
2905       (setq gnus-tmp-name gnus-tmp-from))
2906     (unless (numberp gnus-tmp-lines)
2907       (setq gnus-tmp-lines -1))
2908     (when (= gnus-tmp-lines -1)
2909       (setq gnus-tmp-lines "?"))
2910     (gnus-put-text-property-excluding-characters-with-faces
2911      (point)
2912      (progn (eval gnus-summary-line-format-spec) (point))
2913      'gnus-number gnus-tmp-number)
2914     (when (gnus-visual-p 'summary-highlight 'highlight)
2915       (forward-line -1)
2916       (gnus-run-hooks 'gnus-summary-update-hook)
2917       (forward-line 1))))
2918
2919 (defun gnus-summary-update-line (&optional dont-update)
2920   "Update summary line after change."
2921   (when (and gnus-summary-default-score
2922              (not gnus-summary-inhibit-highlight))
2923     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2924            (article (gnus-summary-article-number))
2925            (score (gnus-summary-article-score article)))
2926       (unless dont-update
2927         (if (and gnus-summary-mark-below
2928                  (< (gnus-summary-article-score)
2929                     gnus-summary-mark-below))
2930             ;; This article has a low score, so we mark it as read.
2931             (when (memq article gnus-newsgroup-unreads)
2932               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2933           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2934             ;; This article was previously marked as read on account
2935             ;; of a low score, but now it has risen, so we mark it as
2936             ;; unread.
2937             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2938         (gnus-summary-update-mark
2939          (if (or (null gnus-summary-default-score)
2940                  (<= (abs (- score gnus-summary-default-score))
2941                      gnus-summary-zcore-fuzz))
2942              ?  ;Whitespace
2943            (if (< score gnus-summary-default-score)
2944                gnus-score-below-mark gnus-score-over-mark))
2945          'score))
2946       ;; Do visual highlighting.
2947       (when (gnus-visual-p 'summary-highlight 'highlight)
2948         (gnus-run-hooks 'gnus-summary-update-hook)))))
2949
2950 (defvar gnus-tmp-new-adopts nil)
2951
2952 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2953   "Return the number of articles in THREAD.
2954 This may be 0 in some cases -- if none of the articles in
2955 the thread are to be displayed."
2956   (let* ((number
2957           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2958           (cond
2959            ((not (listp thread))
2960             1)
2961            ((and (consp thread) (cdr thread))
2962             (apply
2963              '+ 1 (mapcar
2964                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2965            ((null thread)
2966             1)
2967            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2968             1)
2969            (t 0))))
2970     (when (and level (zerop level) gnus-tmp-new-adopts)
2971       (incf number
2972             (apply '+ (mapcar
2973                        'gnus-summary-number-of-articles-in-thread
2974                        gnus-tmp-new-adopts))))
2975     (if char
2976         (if (> number 1) gnus-not-empty-thread-mark
2977           gnus-empty-thread-mark)
2978       number)))
2979
2980 (defun gnus-summary-set-local-parameters (group)
2981   "Go through the local params of GROUP and set all variable specs in that list."
2982   (let ((params (gnus-group-find-parameter group))
2983         elem)
2984     (while params
2985       (setq elem (car params)
2986             params (cdr params))
2987       (and (consp elem)                 ; Has to be a cons.
2988            (consp (cdr elem))           ; The cdr has to be a list.
2989            (symbolp (car elem))         ; Has to be a symbol in there.
2990            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2991            (ignore-errors               ; So we set it.
2992              (make-local-variable (car elem))
2993              (set (car elem) (eval (nth 1 elem))))))))
2994
2995 (defun gnus-summary-read-group (group &optional show-all no-article
2996                                       kill-buffer no-display backward
2997                                       select-articles)
2998   "Start reading news in newsgroup GROUP.
2999 If SHOW-ALL is non-nil, already read articles are also listed.
3000 If NO-ARTICLE is non-nil, no article is selected initially.
3001 If NO-DISPLAY, don't generate a summary buffer."
3002   (let (result)
3003     (while (and group
3004                 (null (setq result
3005                             (let ((gnus-auto-select-next nil))
3006                               (or (gnus-summary-read-group-1
3007                                    group show-all no-article
3008                                    kill-buffer no-display
3009                                    select-articles)
3010                                   (setq show-all nil
3011                                         select-articles nil)))))
3012                 (eq gnus-auto-select-next 'quietly))
3013       (set-buffer gnus-group-buffer)
3014       ;; The entry function called above goes to the next
3015       ;; group automatically, so we go two groups back
3016       ;; if we are searching for the previous group.
3017       (when backward
3018         (gnus-group-prev-unread-group 2))
3019       (if (not (equal group (gnus-group-group-name)))
3020           (setq group (gnus-group-group-name))
3021         (setq group nil)))
3022     result))
3023
3024 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3025   "Directly jump to the other GROUP from summary buffer.
3026 If SHOW-ALL is non-nil, already read articles are also listed."
3027   (interactive
3028    (if (eq gnus-summary-buffer (current-buffer))
3029        (list (completing-read
3030               "Group: " gnus-active-hashtb nil t
3031               (when (and gnus-newsgroup-name
3032                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3033                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3034               'gnus-group-history)
3035              current-prefix-arg)
3036      (error "%s must be invoked from a gnus summary buffer." this-command)))
3037   (unless (or (zerop (length group))
3038               (and gnus-newsgroup-name
3039                    (string-equal gnus-newsgroup-name group)))
3040     (gnus-summary-exit)
3041     (gnus-summary-read-group group show-all
3042                              gnus-dont-select-after-jump-to-other-group)))
3043
3044 (defun gnus-summary-read-group-1 (group show-all no-article
3045                                         kill-buffer no-display
3046                                         &optional select-articles)
3047   ;; Killed foreign groups can't be entered.
3048   ;;  (when (and (not (gnus-group-native-p group))
3049   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3050   ;;    (error "Dead non-native groups can't be entered"))
3051   (gnus-message 5 "Retrieving newsgroup: %s..."
3052                 (gnus-group-decoded-name group))
3053   (let* ((new-group (gnus-summary-setup-buffer group))
3054          (quit-config (gnus-group-quit-config group))
3055          (did-select (and new-group (gnus-select-newsgroup
3056                                      group show-all select-articles))))
3057     (cond
3058      ;; This summary buffer exists already, so we just select it.
3059      ((not new-group)
3060       (gnus-set-global-variables)
3061       (when kill-buffer
3062         (gnus-kill-or-deaden-summary kill-buffer))
3063       (gnus-configure-windows 'summary 'force)
3064       (gnus-set-mode-line 'summary)
3065       (gnus-summary-position-point)
3066       (message "")
3067       t)
3068      ;; We couldn't select this group.
3069      ((null did-select)
3070       (when (and (eq major-mode 'gnus-summary-mode)
3071                  (not (equal (current-buffer) kill-buffer)))
3072         (kill-buffer (current-buffer))
3073         (if (not quit-config)
3074             (progn
3075               ;; Update the info -- marks might need to be removed,
3076               ;; for instance.
3077               (gnus-summary-update-info)
3078               (set-buffer gnus-group-buffer)
3079               (gnus-group-jump-to-group group)
3080               (gnus-group-next-unread-group 1))
3081           (gnus-handle-ephemeral-exit quit-config)))
3082       (let ((grpinfo (gnus-get-info group)))
3083         (if (null (gnus-info-read grpinfo))
3084             (gnus-message 3 "Group %s contains no messages" 
3085                           (gnus-group-decoded-name group))
3086           (gnus-message 3 "Can't select group")))
3087       nil)
3088      ;; The user did a `C-g' while prompting for number of articles,
3089      ;; so we exit this group.
3090      ((eq did-select 'quit)
3091       (and (eq major-mode 'gnus-summary-mode)
3092            (not (equal (current-buffer) kill-buffer))
3093            (kill-buffer (current-buffer)))
3094       (when kill-buffer
3095         (gnus-kill-or-deaden-summary kill-buffer))
3096       (if (not quit-config)
3097           (progn
3098             (set-buffer gnus-group-buffer)
3099             (gnus-group-jump-to-group group)
3100             (gnus-group-next-unread-group 1)
3101             (gnus-configure-windows 'group 'force))
3102         (gnus-handle-ephemeral-exit quit-config))
3103       ;; Finally signal the quit.
3104       (signal 'quit nil))
3105      ;; The group was successfully selected.
3106      (t
3107       (gnus-set-global-variables)
3108       ;; Save the active value in effect when the group was entered.
3109       (setq gnus-newsgroup-active
3110             (gnus-copy-sequence
3111              (gnus-active gnus-newsgroup-name)))
3112       ;; You can change the summary buffer in some way with this hook.
3113       (gnus-run-hooks 'gnus-select-group-hook)
3114       (gnus-update-format-specifications
3115        nil 'summary 'summary-mode 'summary-dummy)
3116       (gnus-update-summary-mark-positions)
3117       ;; Do score processing.
3118       (when gnus-use-scoring
3119         (gnus-possibly-score-headers))
3120       ;; Check whether to fill in the gaps in the threads.
3121       (when gnus-build-sparse-threads
3122         (gnus-build-sparse-threads))
3123       ;; Find the initial limit.
3124       (if gnus-show-threads
3125           (if show-all
3126               (let ((gnus-newsgroup-dormant nil))
3127                 (gnus-summary-initial-limit show-all))
3128             (gnus-summary-initial-limit show-all))
3129         ;; When untreaded, all articles are always shown.
3130         (setq gnus-newsgroup-limit
3131               (mapcar
3132                (lambda (header) (mail-header-number header))
3133                gnus-newsgroup-headers)))
3134       ;; Generate the summary buffer.
3135       (unless no-display
3136         (gnus-summary-prepare))
3137       (when gnus-use-trees
3138         (gnus-tree-open group)
3139         (setq gnus-summary-highlight-line-function
3140               'gnus-tree-highlight-article))
3141       ;; If the summary buffer is empty, but there are some low-scored
3142       ;; articles or some excluded dormants, we include these in the
3143       ;; buffer.
3144       (when (and (zerop (buffer-size))
3145                  (not no-display))
3146         (cond (gnus-newsgroup-dormant
3147                (gnus-summary-limit-include-dormant))
3148               ((and gnus-newsgroup-scored show-all)
3149                (gnus-summary-limit-include-expunged t))))
3150       ;; Function `gnus-apply-kill-file' must be called in this hook.
3151       (gnus-run-hooks 'gnus-apply-kill-hook)
3152       (if (and (zerop (buffer-size))
3153                (not no-display))
3154           (progn
3155             ;; This newsgroup is empty.
3156             (gnus-summary-catchup-and-exit nil t)
3157             (gnus-message 6 "No unread news")
3158             (when kill-buffer
3159               (gnus-kill-or-deaden-summary kill-buffer))
3160             ;; Return nil from this function.
3161             nil)
3162         ;; Hide conversation thread subtrees.  We cannot do this in
3163         ;; gnus-summary-prepare-hook since kill processing may not
3164         ;; work with hidden articles.
3165         (and gnus-show-threads
3166              gnus-thread-hide-subtree
3167              (gnus-summary-hide-all-threads))
3168         (when kill-buffer
3169           (gnus-kill-or-deaden-summary kill-buffer))
3170         ;; Show first unread article if requested.
3171         (if (and (not no-article)
3172                  (not no-display)
3173                  gnus-newsgroup-unreads
3174                  gnus-auto-select-first)
3175             (progn
3176               (gnus-configure-windows 'summary)
3177               (cond
3178                ((eq gnus-auto-select-first 'best)
3179                 (gnus-summary-best-unread-article))
3180                ((eq gnus-auto-select-first t)
3181                 (gnus-summary-first-unread-article))
3182                ((gnus-functionp gnus-auto-select-first)
3183                 (funcall gnus-auto-select-first))))
3184           ;; Don't select any articles, just move point to the first
3185           ;; article in the group.
3186           (goto-char (point-min))
3187           (gnus-summary-position-point)
3188           (gnus-configure-windows 'summary 'force)
3189           (gnus-set-mode-line 'summary))
3190         (when (get-buffer-window gnus-group-buffer t)
3191           ;; Gotta use windows, because recenter does weird stuff if
3192           ;; the current buffer ain't the displayed window.
3193           (let ((owin (selected-window)))
3194             (select-window (get-buffer-window gnus-group-buffer t))
3195             (when (gnus-group-goto-group group)
3196               (recenter))
3197             (select-window owin)))
3198         ;; Mark this buffer as "prepared".
3199         (setq gnus-newsgroup-prepared t)
3200         (gnus-run-hooks 'gnus-summary-prepared-hook)
3201         t)))))
3202
3203 (defun gnus-summary-prepare ()
3204   "Generate the summary buffer."
3205   (interactive)
3206   (let ((buffer-read-only nil))
3207     (erase-buffer)
3208     (setq gnus-newsgroup-data nil
3209           gnus-newsgroup-data-reverse nil)
3210     (gnus-run-hooks 'gnus-summary-generate-hook)
3211     ;; Generate the buffer, either with threads or without.
3212     (when gnus-newsgroup-headers
3213       (gnus-summary-prepare-threads
3214        (if gnus-show-threads
3215            (gnus-sort-gathered-threads
3216             (funcall gnus-summary-thread-gathering-function
3217                      (gnus-sort-threads
3218                       (gnus-cut-threads (gnus-make-threads)))))
3219          ;; Unthreaded display.
3220          (gnus-sort-articles gnus-newsgroup-headers))))
3221     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3222     ;; Call hooks for modifying summary buffer.
3223     (goto-char (point-min))
3224     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3225
3226 (defsubst gnus-general-simplify-subject (subject)
3227   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3228   (setq subject
3229         (cond
3230          ;; Truncate the subject.
3231          (gnus-simplify-subject-functions
3232           (gnus-map-function gnus-simplify-subject-functions subject))
3233          ((numberp gnus-summary-gather-subject-limit)
3234           (setq subject (gnus-simplify-subject-re subject))
3235           (if (> (length subject) gnus-summary-gather-subject-limit)
3236               (substring subject 0 gnus-summary-gather-subject-limit)
3237             subject))
3238          ;; Fuzzily simplify it.
3239          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3240           (gnus-simplify-subject-fuzzy subject))
3241          ;; Just remove the leading "Re:".
3242          (t
3243           (gnus-simplify-subject-re subject))))
3244
3245   (if (and gnus-summary-gather-exclude-subject
3246            (string-match gnus-summary-gather-exclude-subject subject))
3247       nil                               ; This article shouldn't be gathered
3248     subject))
3249
3250 (defun gnus-summary-simplify-subject-query ()
3251   "Query where the respool algorithm would put this article."
3252   (interactive)
3253   (gnus-summary-select-article)
3254   (message "%s"
3255            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3256
3257 (defun gnus-gather-threads-by-subject (threads)
3258   "Gather threads by looking at Subject headers."
3259   (if (not gnus-summary-make-false-root)
3260       threads
3261     (let ((hashtb (gnus-make-hashtable 1024))
3262           (prev threads)
3263           (result threads)
3264           subject hthread whole-subject)
3265       (while threads
3266         (setq subject (gnus-general-simplify-subject
3267                        (setq whole-subject (mail-header-subject
3268                                             (caar threads)))))
3269         (when subject
3270           (if (setq hthread (gnus-gethash subject hashtb))
3271               (progn
3272                 ;; We enter a dummy root into the thread, if we
3273                 ;; haven't done that already.
3274                 (unless (stringp (caar hthread))
3275                   (setcar hthread (list whole-subject (car hthread))))
3276                 ;; We add this new gathered thread to this gathered
3277                 ;; thread.
3278                 (setcdr (car hthread)
3279                         (nconc (cdar hthread) (list (car threads))))
3280                 ;; Remove it from the list of threads.
3281                 (setcdr prev (cdr threads))
3282                 (setq threads prev))
3283             ;; Enter this thread into the hash table.
3284             (gnus-sethash subject threads hashtb)))
3285         (setq prev threads)
3286         (setq threads (cdr threads)))
3287       result)))
3288
3289 (defun gnus-gather-threads-by-references (threads)
3290   "Gather threads by looking at References headers."
3291   (let ((idhashtb (gnus-make-hashtable 1024))
3292         (thhashtb (gnus-make-hashtable 1024))
3293         (prev threads)
3294         (result threads)
3295         ids references id gthread gid entered ref)
3296     (while threads
3297       (when (setq references (mail-header-references (caar threads)))
3298         (setq id (mail-header-id (caar threads))
3299               ids (gnus-split-references references)
3300               entered nil)
3301         (while (setq ref (pop ids))
3302           (setq ids (delete ref ids))
3303           (if (not (setq gid (gnus-gethash ref idhashtb)))
3304               (progn
3305                 (gnus-sethash ref id idhashtb)
3306                 (gnus-sethash id threads thhashtb))
3307             (setq gthread (gnus-gethash gid thhashtb))
3308             (unless entered
3309               ;; We enter a dummy root into the thread, if we
3310               ;; haven't done that already.
3311               (unless (stringp (caar gthread))
3312                 (setcar gthread (list (mail-header-subject (caar gthread))
3313                                       (car gthread))))
3314               ;; We add this new gathered thread to this gathered
3315               ;; thread.
3316               (setcdr (car gthread)
3317                       (nconc (cdar gthread) (list (car threads)))))
3318             ;; Add it into the thread hash table.
3319             (gnus-sethash id gthread thhashtb)
3320             (setq entered t)
3321             ;; Remove it from the list of threads.
3322             (setcdr prev (cdr threads))
3323             (setq threads prev))))
3324       (setq prev threads)
3325       (setq threads (cdr threads)))
3326     result))
3327
3328 (defun gnus-sort-gathered-threads (threads)
3329   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3330   (let ((result threads))
3331     (while threads
3332       (when (stringp (caar threads))
3333         (setcdr (car threads)
3334                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3335       (setq threads (cdr threads)))
3336     result))
3337
3338 (defun gnus-thread-loop-p (root thread)
3339   "Say whether ROOT is in THREAD."
3340   (let ((stack (list thread))
3341         (infloop 0)
3342         th)
3343     (while (setq thread (pop stack))
3344       (setq th (cdr thread))
3345       (while (and th
3346                   (not (eq (caar th) root)))
3347         (pop th))
3348       (if th
3349           ;; We have found a loop.
3350           (let (ref-dep)
3351             (setcdr thread (delq (car th) (cdr thread)))
3352             (if (boundp (setq ref-dep (intern "none"
3353                                               gnus-newsgroup-dependencies)))
3354                 (setcdr (symbol-value ref-dep)
3355                         (nconc (cdr (symbol-value ref-dep))
3356                                (list (car th))))
3357               (set ref-dep (list nil (car th))))
3358             (setq infloop 1
3359                   stack nil))
3360         ;; Push all the subthreads onto the stack.
3361         (push (cdr thread) stack)))
3362     infloop))
3363
3364 (defun gnus-make-threads ()
3365   "Go through the dependency hashtb and find the roots.  Return all threads."
3366   (let (threads)
3367     (while (catch 'infloop
3368              (mapatoms
3369               (lambda (refs)
3370                 ;; Deal with self-referencing References loops.
3371                 (when (and (car (symbol-value refs))
3372                            (not (zerop
3373                                  (apply
3374                                   '+
3375                                   (mapcar
3376                                    (lambda (thread)
3377                                      (gnus-thread-loop-p
3378                                       (car (symbol-value refs)) thread))
3379                                    (cdr (symbol-value refs)))))))
3380                   (setq threads nil)
3381                   (throw 'infloop t))
3382                 (unless (car (symbol-value refs))
3383                   ;; These threads do not refer back to any other articles,
3384                   ;; so they're roots.
3385                   (setq threads (append (cdr (symbol-value refs)) threads))))
3386               gnus-newsgroup-dependencies)))
3387     threads))
3388
3389 ;; Build the thread tree.
3390 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3391   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3392
3393 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3394 if it was already present.
3395
3396 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3397 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3398 Message-IDs will be renamed be renamed to a unique Message-ID before
3399 being entered.
3400
3401 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3402   (let* ((id (mail-header-id header))
3403          (id-dep (and id (intern id dependencies)))
3404          ref ref-dep ref-header)
3405     ;; Enter this `header' in the `dependencies' table.
3406     (cond
3407      ((not id-dep)
3408       (setq header nil))
3409      ;; The first two cases do the normal part: enter a new `header'
3410      ;; in the `dependencies' table.
3411      ((not (boundp id-dep))
3412       (set id-dep (list header)))
3413      ((null (car (symbol-value id-dep)))
3414       (setcar (symbol-value id-dep) header))
3415
3416      ;; From here the `header' was already present in the
3417      ;; `dependencies' table.
3418      (force-new
3419       ;; Overrides an existing entry;
3420       ;; just set the header part of the entry.
3421       (setcar (symbol-value id-dep) header))
3422
3423      ;; Renames the existing `header' to a unique Message-ID.
3424      ((not gnus-summary-ignore-duplicates)
3425       ;; An article with this Message-ID has already been seen.
3426       ;; We rename the Message-ID.
3427       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3428            (list header))
3429       (mail-header-set-id header id))
3430
3431      ;; The last case ignores an existing entry, except it adds any
3432      ;; additional Xrefs (in case the two articles came from different
3433      ;; servers.
3434      ;; Also sets `header' to `nil' meaning that the `dependencies'
3435      ;; table was *not* modified.
3436      (t
3437       (mail-header-set-xref
3438        (car (symbol-value id-dep))
3439        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3440                    "")
3441                (or (mail-header-xref header) "")))
3442       (setq header nil)))
3443
3444     (when header
3445       ;; First check if that we are not creating a References loop.
3446       (setq ref (gnus-parent-id (mail-header-references header)))
3447       (while (and ref
3448                   (setq ref-dep (intern-soft ref dependencies))
3449                   (boundp ref-dep)
3450                   (setq ref-header (car (symbol-value ref-dep))))
3451         (if (string= id ref)
3452             ;; Yuk!  This is a reference loop.  Make the article be a
3453             ;; root article.
3454             (progn
3455               (mail-header-set-references (car (symbol-value id-dep)) "none")
3456               (setq ref nil))
3457           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3458       (setq ref (gnus-parent-id (mail-header-references header)))
3459       (setq ref-dep (intern (or ref "none") dependencies))
3460       (if (boundp ref-dep)
3461           (setcdr (symbol-value ref-dep)
3462                   (nconc (cdr (symbol-value ref-dep))
3463                          (list (symbol-value id-dep))))
3464         (set ref-dep (list nil (symbol-value id-dep)))))
3465     header))
3466
3467 (defun gnus-build-sparse-threads ()
3468   (let ((headers gnus-newsgroup-headers)
3469         (mail-parse-charset gnus-newsgroup-charset)
3470         (gnus-summary-ignore-duplicates t)
3471         header references generation relations
3472         subject child end new-child date)
3473     ;; First we create an alist of generations/relations, where
3474     ;; generations is how much we trust the relation, and the relation
3475     ;; is parent/child.
3476     (gnus-message 7 "Making sparse threads...")
3477     (save-excursion
3478       (nnheader-set-temp-buffer " *gnus sparse threads*")
3479       (while (setq header (pop headers))
3480         (when (and (setq references (mail-header-references header))
3481                    (not (string= references "")))
3482           (insert references)
3483           (setq child (mail-header-id header)
3484                 subject (mail-header-subject header)
3485                 date (mail-header-date header)
3486                 generation 0)
3487           (while (search-backward ">" nil t)
3488             (setq end (1+ (point)))
3489             (when (search-backward "<" nil t)
3490               (setq new-child (buffer-substring (point) end))
3491               (push (list (incf generation)
3492                           child (setq child new-child)
3493                           subject date)
3494                     relations)))
3495           (when child
3496             (push (list (1+ generation) child nil subject) relations))
3497           (erase-buffer)))
3498       (kill-buffer (current-buffer)))
3499     ;; Sort over trustworthiness.
3500     (mapcar
3501      (lambda (relation)
3502        (when (gnus-dependencies-add-header
3503               (make-full-mail-header-from-decoded-header
3504                gnus-reffed-article-number
3505                (nth 3 relation) "" (or (nth 4 relation) "")
3506                (nth 1 relation)
3507                (or (nth 2 relation) "") 0 0 "")
3508               gnus-newsgroup-dependencies nil)
3509          (push gnus-reffed-article-number gnus-newsgroup-limit)
3510          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3511          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3512                gnus-newsgroup-reads)
3513          (decf gnus-reffed-article-number)))
3514      (sort relations 'car-less-than-car))
3515     (gnus-message 7 "Making sparse threads...done")))
3516
3517 (defun gnus-build-old-threads ()
3518   ;; Look at all the articles that refer back to old articles, and
3519   ;; fetch the headers for the articles that aren't there.  This will
3520   ;; build complete threads - if the roots haven't been expired by the
3521   ;; server, that is.
3522   (let ((mail-parse-charset gnus-newsgroup-charset)
3523         id heads)
3524     (mapatoms
3525      (lambda (refs)
3526        (when (not (car (symbol-value refs)))
3527          (setq heads (cdr (symbol-value refs)))
3528          (while heads
3529            (if (memq (mail-header-number (caar heads))
3530                      gnus-newsgroup-dormant)
3531                (setq heads (cdr heads))
3532              (setq id (symbol-name refs))
3533              (while (and (setq id (gnus-build-get-header id))
3534                          (not (car (gnus-id-to-thread id)))))
3535              (setq heads nil)))))
3536      gnus-newsgroup-dependencies)))
3537
3538 ;; This function has to be called with point after the article number
3539 ;; on the beginning of the line.
3540 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3541   (let ((eol (gnus-point-at-eol))
3542         (buffer (current-buffer))
3543         header)
3544
3545     ;; overview: [num subject from date id refs chars lines misc]
3546     (unwind-protect
3547         (progn
3548           (narrow-to-region (point) eol)
3549           (unless (eobp)
3550             (forward-char))
3551
3552           (setq header
3553                 (make-full-mail-header
3554                  number                         ; number
3555                  (nnheader-nov-field)           ; subject
3556                  (nnheader-nov-field)           ; from
3557                  (nnheader-nov-field)           ; date
3558                  (nnheader-nov-read-message-id) ; id
3559                  (nnheader-nov-field)           ; refs
3560                  (nnheader-nov-read-integer)    ; chars
3561                  (nnheader-nov-read-integer)    ; lines
3562                  (unless (eobp)
3563                    (if (looking-at "Xref: ")
3564                        (goto-char (match-end 0)))
3565                    (nnheader-nov-field))        ; Xref
3566                  (nnheader-nov-parse-extra))))  ; extra
3567
3568       (widen))
3569
3570     (when gnus-alter-header-function
3571       (funcall gnus-alter-header-function header))
3572     (gnus-dependencies-add-header header dependencies force-new)))
3573
3574 (defun gnus-build-get-header (id)
3575   "Look through the buffer of NOV lines and find the header to ID.
3576 Enter this line into the dependencies hash table, and return
3577 the id of the parent article (if any)."
3578   (let ((deps gnus-newsgroup-dependencies)
3579         found header)
3580     (prog1
3581         (save-excursion
3582           (set-buffer nntp-server-buffer)
3583           (let ((case-fold-search nil))
3584             (goto-char (point-min))
3585             (while (and (not found)
3586                         (search-forward id nil t))
3587               (beginning-of-line)
3588               (setq found (looking-at
3589                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3590                                    (regexp-quote id))))
3591               (or found (beginning-of-line 2)))
3592             (when found
3593               (beginning-of-line)
3594               (and
3595                (setq header (gnus-nov-parse-line
3596                              (read (current-buffer)) deps))
3597                (gnus-parent-id (mail-header-references header))))))
3598       (when header
3599         (let ((number (mail-header-number header)))
3600           (push number gnus-newsgroup-limit)
3601           (push header gnus-newsgroup-headers)
3602           (if (memq number gnus-newsgroup-unselected)
3603               (progn
3604                 (push number gnus-newsgroup-unreads)
3605                 (setq gnus-newsgroup-unselected
3606                       (delq number gnus-newsgroup-unselected)))
3607             (push number gnus-newsgroup-ancient)))))))
3608
3609 (defun gnus-build-all-threads ()
3610   "Read all the headers."
3611   (let ((gnus-summary-ignore-duplicates t)
3612         (mail-parse-charset gnus-newsgroup-charset)
3613         (dependencies gnus-newsgroup-dependencies)
3614         header article)
3615     (save-excursion
3616       (set-buffer nntp-server-buffer)
3617       (let ((case-fold-search nil))
3618         (goto-char (point-min))
3619         (while (not (eobp))
3620           (ignore-errors
3621             (setq article (read (current-buffer))
3622                   header (gnus-nov-parse-line article dependencies)))
3623           (when header
3624             (save-excursion
3625               (set-buffer gnus-summary-buffer)
3626               (push header gnus-newsgroup-headers)
3627               (if (memq (setq article (mail-header-number header))
3628                         gnus-newsgroup-unselected)
3629                   (progn
3630                     (push article gnus-newsgroup-unreads)
3631                     (setq gnus-newsgroup-unselected
3632                           (delq article gnus-newsgroup-unselected)))
3633                 (push article gnus-newsgroup-ancient)))
3634             (forward-line 1)))))))
3635
3636 (defun gnus-summary-update-article-line (article header)
3637   "Update the line for ARTICLE using HEADERS."
3638   (let* ((id (mail-header-id header))
3639          (thread (gnus-id-to-thread id)))
3640     (unless thread
3641       (error "Article in no thread"))
3642     ;; Update the thread.
3643     (setcar thread header)
3644     (gnus-summary-goto-subject article)
3645     (let* ((datal (gnus-data-find-list article))
3646            (data (car datal))
3647            (length (when (cdr datal)
3648                      (- (gnus-data-pos data)
3649                         (gnus-data-pos (cadr datal)))))
3650            (buffer-read-only nil)
3651            (level (gnus-summary-thread-level)))
3652       (gnus-delete-line)
3653       (gnus-summary-insert-line
3654        header level nil (gnus-article-mark article)
3655        (memq article gnus-newsgroup-replied)
3656        (memq article gnus-newsgroup-expirable)
3657        ;; Only insert the Subject string when it's different
3658        ;; from the previous Subject string.
3659        (if (and
3660             gnus-show-threads
3661             (gnus-subject-equal
3662              (condition-case ()
3663                  (mail-header-subject
3664                   (gnus-data-header
3665                    (cadr
3666                     (gnus-data-find-list
3667                      article
3668                      (gnus-data-list t)))))
3669                ;; Error on the side of excessive subjects.
3670                (error ""))
3671              (mail-header-subject header)))
3672            ""
3673          (mail-header-subject header))
3674        nil (cdr (assq article gnus-newsgroup-scored))
3675        (memq article gnus-newsgroup-processable))
3676       (when length
3677         (gnus-data-update-list
3678          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3679
3680 (defun gnus-summary-update-article (article &optional iheader)
3681   "Update ARTICLE in the summary buffer."
3682   (set-buffer gnus-summary-buffer)
3683   (let* ((header (gnus-summary-article-header article))
3684          (id (mail-header-id header))
3685          (data (gnus-data-find article))
3686          (thread (gnus-id-to-thread id))
3687          (references (mail-header-references header))
3688          (parent
3689           (gnus-id-to-thread
3690            (or (gnus-parent-id
3691                 (when (and references
3692                            (not (equal "" references)))
3693                   references))
3694                "none")))
3695          (buffer-read-only nil)
3696          (old (car thread)))
3697     (when thread
3698       (unless iheader
3699         (setcar thread nil)
3700         (when parent
3701           (delq thread parent)))
3702       (if (gnus-summary-insert-subject id header)
3703           ;; Set the (possibly) new article number in the data structure.
3704           (gnus-data-set-number data (gnus-id-to-article id))
3705         (setcar thread old)
3706         nil))))
3707
3708 (defun gnus-rebuild-thread (id &optional line)
3709   "Rebuild the thread containing ID.
3710 If LINE, insert the rebuilt thread starting on line LINE."
3711   (let ((buffer-read-only nil)
3712         old-pos current thread data)
3713     (if (not gnus-show-threads)
3714         (setq thread (list (car (gnus-id-to-thread id))))
3715       ;; Get the thread this article is part of.
3716       (setq thread (gnus-remove-thread id)))
3717     (setq old-pos (gnus-point-at-bol))
3718     (setq current (save-excursion
3719                     (and (zerop (forward-line -1))
3720                          (gnus-summary-article-number))))
3721     ;; If this is a gathered thread, we have to go some re-gathering.
3722     (when (stringp (car thread))
3723       (let ((subject (car thread))
3724             roots thr)
3725         (setq thread (cdr thread))
3726         (while thread
3727           (unless (memq (setq thr (gnus-id-to-thread
3728                                    (gnus-root-id
3729                                     (mail-header-id (caar thread)))))
3730                         roots)
3731             (push thr roots))
3732           (setq thread (cdr thread)))
3733         ;; We now have all (unique) roots.
3734         (if (= (length roots) 1)
3735             ;; All the loose roots are now one solid root.
3736             (setq thread (car roots))
3737           (setq thread (cons subject (gnus-sort-threads roots))))))
3738     (let (threads)
3739       ;; We then insert this thread into the summary buffer.
3740       (when line
3741         (goto-char (point-min))
3742         (forward-line (1- line)))
3743       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3744         (if gnus-show-threads
3745             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3746           (gnus-summary-prepare-unthreaded thread))
3747         (setq data (nreverse gnus-newsgroup-data))
3748         (setq threads gnus-newsgroup-threads))
3749       ;; We splice the new data into the data structure.
3750       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3751       ;;!!! then we want to insert at the beginning of the buffer.
3752       ;;!!! That happens to be true with Gnus now, but that may
3753       ;;!!! change in the future.  Perhaps.
3754       (gnus-data-enter-list
3755        (if line nil current) data (- (point) old-pos))
3756       (setq gnus-newsgroup-threads
3757             (nconc threads gnus-newsgroup-threads))
3758       (gnus-data-compute-positions))))
3759
3760 (defun gnus-number-to-header (number)
3761   "Return the header for article NUMBER."
3762   (let ((headers gnus-newsgroup-headers))
3763     (while (and headers
3764                 (not (= number (mail-header-number (car headers)))))
3765       (pop headers))
3766     (when headers
3767       (car headers))))
3768
3769 (defun gnus-parent-headers (in-headers &optional generation)
3770   "Return the headers of the GENERATIONeth parent of HEADERS."
3771   (unless generation
3772     (setq generation 1))
3773   (let ((parent t)
3774         (headers in-headers)
3775         references)
3776     (while (and parent
3777                 (not (zerop generation))
3778                 (setq references (mail-header-references headers)))
3779       (setq headers (if (and references
3780                              (setq parent (gnus-parent-id references)))
3781                         (car (gnus-id-to-thread parent))
3782                       nil))
3783       (decf generation))
3784     (and (not (eq headers in-headers))
3785          headers)))
3786
3787 (defun gnus-id-to-thread (id)
3788   "Return the (sub-)thread where ID appears."
3789   (gnus-gethash id gnus-newsgroup-dependencies))
3790
3791 (defun gnus-id-to-article (id)
3792   "Return the article number of ID."
3793   (let ((thread (gnus-id-to-thread id)))
3794     (when (and thread
3795                (car thread))
3796       (mail-header-number (car thread)))))
3797
3798 (defun gnus-id-to-header (id)
3799   "Return the article headers of ID."
3800   (car (gnus-id-to-thread id)))
3801
3802 (defun gnus-article-displayed-root-p (article)
3803   "Say whether ARTICLE is a root(ish) article."
3804   (let ((level (gnus-summary-thread-level article))
3805         (refs (mail-header-references  (gnus-summary-article-header article)))
3806         particle)
3807     (cond
3808      ((null level) nil)
3809      ((zerop level) t)
3810      ((null refs) t)
3811      ((null (gnus-parent-id refs)) t)
3812      ((and (= 1 level)
3813            (null (setq particle (gnus-id-to-article
3814                                  (gnus-parent-id refs))))
3815            (null (gnus-summary-thread-level particle)))))))
3816
3817 (defun gnus-root-id (id)
3818   "Return the id of the root of the thread where ID appears."
3819   (let (last-id prev)
3820     (while (and id (setq prev (car (gnus-id-to-thread id))))
3821       (setq last-id id
3822             id (gnus-parent-id (mail-header-references prev))))
3823     last-id))
3824
3825 (defun gnus-articles-in-thread (thread)
3826   "Return the list of articles in THREAD."
3827   (cons (mail-header-number (car thread))
3828         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3829
3830 (defun gnus-remove-thread (id &optional dont-remove)
3831   "Remove the thread that has ID in it."
3832   (let (headers thread last-id)
3833     ;; First go up in this thread until we find the root.
3834     (setq last-id (gnus-root-id id)
3835           headers (message-flatten-list (gnus-id-to-thread last-id)))
3836     ;; We have now found the real root of this thread.  It might have
3837     ;; been gathered into some loose thread, so we have to search
3838     ;; through the threads to find the thread we wanted.
3839     (let ((threads gnus-newsgroup-threads)
3840           sub)
3841       (while threads
3842         (setq sub (car threads))
3843         (if (stringp (car sub))
3844             ;; This is a gathered thread, so we look at the roots
3845             ;; below it to find whether this article is in this
3846             ;; gathered root.
3847             (progn
3848               (setq sub (cdr sub))
3849               (while sub
3850                 (when (member (caar sub) headers)
3851                   (setq thread (car threads)
3852                         threads nil
3853                         sub nil))
3854                 (setq sub (cdr sub))))
3855           ;; It's an ordinary thread, so we check it.
3856           (when (eq (car sub) (car headers))
3857             (setq thread sub
3858                   threads nil)))
3859         (setq threads (cdr threads)))
3860       ;; If this article is in no thread, then it's a root.
3861       (if thread
3862           (unless dont-remove
3863             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3864         (setq thread (gnus-id-to-thread last-id)))
3865       (when thread
3866         (prog1
3867             thread                      ; We return this thread.
3868           (unless dont-remove
3869             (if (stringp (car thread))
3870                 (progn
3871                   ;; If we use dummy roots, then we have to remove the
3872                   ;; dummy root as well.
3873                   (when (eq gnus-summary-make-false-root 'dummy)
3874                     ;; We go to the dummy root by going to
3875                     ;; the first sub-"thread", and then one line up.
3876                     (gnus-summary-goto-article
3877                      (mail-header-number (caadr thread)))
3878                     (forward-line -1)
3879                     (gnus-delete-line)
3880                     (gnus-data-compute-positions))
3881                   (setq thread (cdr thread))
3882                   (while thread
3883                     (gnus-remove-thread-1 (car thread))
3884                     (setq thread (cdr thread))))
3885               (gnus-remove-thread-1 thread))))))))
3886
3887 (defun gnus-remove-thread-1 (thread)
3888   "Remove the thread THREAD recursively."
3889   (let ((number (mail-header-number (pop thread)))
3890         d)
3891     (setq thread (reverse thread))
3892     (while thread
3893       (gnus-remove-thread-1 (pop thread)))
3894     (when (setq d (gnus-data-find number))
3895       (goto-char (gnus-data-pos d))
3896       (gnus-summary-show-thread)
3897       (gnus-data-remove
3898        number
3899        (- (gnus-point-at-bol)
3900           (prog1
3901               (1+ (gnus-point-at-eol))
3902             (gnus-delete-line)))))))
3903
3904 (defun gnus-sort-threads-1 (threads func)
3905   (sort (mapcar (lambda (thread)
3906                   (cons (car thread)
3907                         (and (cdr thread)
3908                              (gnus-sort-threads-1 (cdr thread) func))))
3909                 threads) func))
3910
3911 (defun gnus-sort-threads (threads)
3912   "Sort THREADS."
3913   (if (not gnus-thread-sort-functions)
3914       threads
3915     (gnus-message 8 "Sorting threads...")
3916     (prog1
3917         (gnus-sort-threads-1
3918          threads
3919          (gnus-make-sort-function gnus-thread-sort-functions))
3920       (gnus-message 8 "Sorting threads...done"))))
3921
3922 (defun gnus-sort-articles (articles)
3923   "Sort ARTICLES."
3924   (when gnus-article-sort-functions
3925     (gnus-message 7 "Sorting articles...")
3926     (prog1
3927         (setq gnus-newsgroup-headers
3928               (sort articles (gnus-make-sort-function
3929                               gnus-article-sort-functions)))
3930       (gnus-message 7 "Sorting articles...done"))))
3931
3932 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3933 (defmacro gnus-thread-header (thread)
3934   "Return header of first article in THREAD.
3935 Note that THREAD must never, ever be anything else than a variable -
3936 using some other form will lead to serious barfage."
3937   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3938   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3939   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3940         (vector thread) 2))
3941
3942 (defsubst gnus-article-sort-by-number (h1 h2)
3943   "Sort articles by article number."
3944   (< (mail-header-number h1)
3945      (mail-header-number h2)))
3946
3947 (defun gnus-thread-sort-by-number (h1 h2)
3948   "Sort threads by root article number."
3949   (gnus-article-sort-by-number
3950    (gnus-thread-header h1) (gnus-thread-header h2)))
3951
3952 (defsubst gnus-article-sort-by-lines (h1 h2)
3953   "Sort articles by article Lines header."
3954   (< (mail-header-lines h1)
3955      (mail-header-lines h2)))
3956
3957 (defun gnus-thread-sort-by-lines (h1 h2)
3958   "Sort threads by root article Lines header."
3959   (gnus-article-sort-by-lines
3960    (gnus-thread-header h1) (gnus-thread-header h2)))
3961
3962 (defsubst gnus-article-sort-by-chars (h1 h2)
3963   "Sort articles by octet length."
3964   (< (mail-header-chars h1)
3965      (mail-header-chars h2)))
3966
3967 (defun gnus-thread-sort-by-chars (h1 h2)
3968   "Sort threads by root article octet length."
3969   (gnus-article-sort-by-chars
3970    (gnus-thread-header h1) (gnus-thread-header h2)))
3971
3972 (defsubst gnus-article-sort-by-author (h1 h2)
3973   "Sort articles by root author."
3974   (string-lessp
3975    (let ((addr (car (mime-entity-read-field h1 'From))))
3976      (or (std11-full-name-string addr)
3977          (std11-address-string addr)
3978          ""))
3979    (let ((addr (car (mime-entity-read-field h2 'From))))
3980      (or (std11-full-name-string addr)
3981          (std11-address-string addr)
3982          ""))
3983    ))
3984
3985 (defun gnus-thread-sort-by-author (h1 h2)
3986   "Sort threads by root author."
3987   (gnus-article-sort-by-author
3988    (gnus-thread-header h1)  (gnus-thread-header h2)))
3989
3990 (defsubst gnus-article-sort-by-subject (h1 h2)
3991   "Sort articles by root subject."
3992   (string-lessp
3993    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3994    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3995
3996 (defun gnus-thread-sort-by-subject (h1 h2)
3997   "Sort threads by root subject."
3998   (gnus-article-sort-by-subject
3999    (gnus-thread-header h1) (gnus-thread-header h2)))
4000
4001 (defsubst gnus-article-sort-by-date (h1 h2)
4002   "Sort articles by root article date."
4003   (time-less-p
4004    (gnus-date-get-time (mail-header-date h1))
4005    (gnus-date-get-time (mail-header-date h2))))
4006
4007 (defun gnus-thread-sort-by-date (h1 h2)
4008   "Sort threads by root article date."
4009   (gnus-article-sort-by-date
4010    (gnus-thread-header h1) (gnus-thread-header h2)))
4011
4012 (defsubst gnus-article-sort-by-score (h1 h2)
4013   "Sort articles by root article score.
4014 Unscored articles will be counted as having a score of zero."
4015   (> (or (cdr (assq (mail-header-number h1)
4016                     gnus-newsgroup-scored))
4017          gnus-summary-default-score 0)
4018      (or (cdr (assq (mail-header-number h2)
4019                     gnus-newsgroup-scored))
4020          gnus-summary-default-score 0)))
4021
4022 (defun gnus-thread-sort-by-score (h1 h2)
4023   "Sort threads by root article score."
4024   (gnus-article-sort-by-score
4025    (gnus-thread-header h1) (gnus-thread-header h2)))
4026
4027 (defun gnus-thread-sort-by-total-score (h1 h2)
4028   "Sort threads by the sum of all scores in the thread.
4029 Unscored articles will be counted as having a score of zero."
4030   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4031
4032 (defun gnus-thread-total-score (thread)
4033   ;; This function find the total score of THREAD.
4034   (cond ((null thread)
4035          0)
4036         ((consp thread)
4037          (if (stringp (car thread))
4038              (apply gnus-thread-score-function 0
4039                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4040            (gnus-thread-total-score-1 thread)))
4041         (t
4042          (gnus-thread-total-score-1 (list thread)))))
4043
4044 (defun gnus-thread-total-score-1 (root)
4045   ;; This function find the total score of the thread below ROOT.
4046   (setq root (car root))
4047   (apply gnus-thread-score-function
4048          (or (append
4049               (mapcar 'gnus-thread-total-score
4050                       (cdr (gnus-id-to-thread (mail-header-id root))))
4051               (when (> (mail-header-number root) 0)
4052                 (list (or (cdr (assq (mail-header-number root)
4053                                      gnus-newsgroup-scored))
4054                           gnus-summary-default-score 0))))
4055              (list gnus-summary-default-score)
4056              '(0))))
4057
4058 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4059 (defvar gnus-tmp-prev-subject nil)
4060 (defvar gnus-tmp-false-parent nil)
4061 (defvar gnus-tmp-root-expunged nil)
4062 (defvar gnus-tmp-dummy-line nil)
4063
4064 (eval-when-compile (defvar gnus-tmp-header))
4065 (defun gnus-extra-header (type &optional header)
4066   "Return the extra header of TYPE."
4067   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4068       ""))
4069
4070 (defun gnus-summary-prepare-threads (threads)
4071   "Prepare summary buffer from THREADS and indentation LEVEL.
4072 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4073 or a straight list of headers."
4074   (gnus-message 7 "Generating summary...")
4075
4076   (setq gnus-newsgroup-threads threads)
4077   (beginning-of-line)
4078
4079   (let ((gnus-tmp-level 0)
4080         (default-score (or gnus-summary-default-score 0))
4081         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4082         thread number subject stack state gnus-tmp-gathered beg-match
4083         new-roots gnus-tmp-new-adopts thread-end
4084         gnus-tmp-header gnus-tmp-unread
4085         gnus-tmp-replied gnus-tmp-subject-or-nil
4086         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4087         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4088         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
4089
4090     (setq gnus-tmp-prev-subject nil)
4091
4092     (if (vectorp (car threads))
4093         ;; If this is a straight (sic) list of headers, then a
4094         ;; threaded summary display isn't required, so we just create
4095         ;; an unthreaded one.
4096         (gnus-summary-prepare-unthreaded threads)
4097
4098       ;; Do the threaded display.
4099
4100       (while (or threads stack gnus-tmp-new-adopts new-roots)
4101
4102         (if (and (= gnus-tmp-level 0)
4103                  (or (not stack)
4104                      (= (caar stack) 0))
4105                  (not gnus-tmp-false-parent)
4106                  (or gnus-tmp-new-adopts new-roots))
4107             (if gnus-tmp-new-adopts
4108                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4109                       thread (list (car gnus-tmp-new-adopts))
4110                       gnus-tmp-header (caar thread)
4111                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4112               (when new-roots
4113                 (setq thread (list (car new-roots))
4114                       gnus-tmp-header (caar thread)
4115                       new-roots (cdr new-roots))))
4116
4117           (if threads
4118               ;; If there are some threads, we do them before the
4119               ;; threads on the stack.
4120               (setq thread threads
4121                     gnus-tmp-header (caar thread))
4122             ;; There were no current threads, so we pop something off
4123             ;; the stack.
4124             (setq state (car stack)
4125                   gnus-tmp-level (car state)
4126                   thread (cdr state)
4127                   stack (cdr stack)
4128                   gnus-tmp-header (caar thread))))
4129
4130         (setq gnus-tmp-false-parent nil)
4131         (setq gnus-tmp-root-expunged nil)
4132         (setq thread-end nil)
4133
4134         (if (stringp gnus-tmp-header)
4135             ;; The header is a dummy root.
4136             (cond
4137              ((eq gnus-summary-make-false-root 'adopt)
4138               ;; We let the first article adopt the rest.
4139               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4140                                                (cddar thread)))
4141               (setq gnus-tmp-gathered
4142                     (nconc (mapcar
4143                             (lambda (h) (mail-header-number (car h)))
4144                             (cddar thread))
4145                            gnus-tmp-gathered))
4146               (setq thread (cons (list (caar thread)
4147                                        (cadar thread))
4148                                  (cdr thread)))
4149               (setq gnus-tmp-level -1
4150                     gnus-tmp-false-parent t))
4151              ((eq gnus-summary-make-false-root 'empty)
4152               ;; We print adopted articles with empty subject fields.
4153               (setq gnus-tmp-gathered
4154                     (nconc (mapcar
4155                             (lambda (h) (mail-header-number (car h)))
4156                             (cddar thread))
4157                            gnus-tmp-gathered))
4158               (setq gnus-tmp-level -1))
4159              ((eq gnus-summary-make-false-root 'dummy)
4160               ;; We remember that we probably want to output a dummy
4161               ;; root.
4162               (setq gnus-tmp-dummy-line gnus-tmp-header)
4163               (setq gnus-tmp-prev-subject gnus-tmp-header))
4164              (t
4165               ;; We do not make a root for the gathered
4166               ;; sub-threads at all.
4167               (setq gnus-tmp-level -1)))
4168
4169           (setq number (mail-header-number gnus-tmp-header)
4170                 subject (mail-header-subject gnus-tmp-header))
4171
4172           (cond
4173            ;; If the thread has changed subject, we might want to make
4174            ;; this subthread into a root.
4175            ((and (null gnus-thread-ignore-subject)
4176                  (not (zerop gnus-tmp-level))
4177                  gnus-tmp-prev-subject
4178                  (not (inline
4179                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4180             (setq new-roots (nconc new-roots (list (car thread)))
4181                   thread-end t
4182                   gnus-tmp-header nil))
4183            ;; If the article lies outside the current limit,
4184            ;; then we do not display it.
4185            ((not (memq number gnus-newsgroup-limit))
4186             (setq gnus-tmp-gathered
4187                   (nconc (mapcar
4188                           (lambda (h) (mail-header-number (car h)))
4189                           (cdar thread))
4190                          gnus-tmp-gathered))
4191             (setq gnus-tmp-new-adopts (if (cdar thread)
4192                                           (append gnus-tmp-new-adopts
4193                                                   (cdar thread))
4194                                         gnus-tmp-new-adopts)
4195                   thread-end t
4196                   gnus-tmp-header nil)
4197             (when (zerop gnus-tmp-level)
4198               (setq gnus-tmp-root-expunged t)))
4199            ;; Perhaps this article is to be marked as read?
4200            ((and gnus-summary-mark-below
4201                  (< (or (cdr (assq number gnus-newsgroup-scored))
4202                         default-score)
4203                     gnus-summary-mark-below)
4204                  ;; Don't touch sparse articles.
4205                  (not (gnus-summary-article-sparse-p number))
4206                  (not (gnus-summary-article-ancient-p number)))
4207             (setq gnus-newsgroup-unreads
4208                   (delq number gnus-newsgroup-unreads))
4209             (if gnus-newsgroup-auto-expire
4210                 (push number gnus-newsgroup-expirable)
4211               (push (cons number gnus-low-score-mark)
4212                     gnus-newsgroup-reads))))
4213
4214           (when gnus-tmp-header
4215             ;; We may have an old dummy line to output before this
4216             ;; article.
4217             (when (and gnus-tmp-dummy-line
4218                        (gnus-subject-equal
4219                         gnus-tmp-dummy-line
4220                         (mail-header-subject gnus-tmp-header)))
4221               (gnus-summary-insert-dummy-line
4222                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4223               (setq gnus-tmp-dummy-line nil))
4224
4225             ;; Compute the mark.
4226             (setq gnus-tmp-unread (gnus-article-mark number))
4227
4228             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4229                                   gnus-tmp-header gnus-tmp-level)
4230                   gnus-newsgroup-data)
4231
4232             ;; Actually insert the line.
4233             (setq
4234              gnus-tmp-subject-or-nil
4235              (cond
4236               ((and gnus-thread-ignore-subject
4237                     gnus-tmp-prev-subject
4238                     (not (inline (gnus-subject-equal
4239                                   gnus-tmp-prev-subject subject))))
4240                subject)
4241               ((zerop gnus-tmp-level)
4242                (if (and (eq gnus-summary-make-false-root 'empty)
4243                         (memq number gnus-tmp-gathered)
4244                         gnus-tmp-prev-subject
4245                         (inline (gnus-subject-equal
4246                                  gnus-tmp-prev-subject subject)))
4247                    gnus-summary-same-subject
4248                  subject))
4249               (t gnus-summary-same-subject)))
4250             (if (and (eq gnus-summary-make-false-root 'adopt)
4251                      (= gnus-tmp-level 1)
4252                      (memq number gnus-tmp-gathered))
4253                 (setq gnus-tmp-opening-bracket ?\<
4254                       gnus-tmp-closing-bracket ?\>)
4255               (setq gnus-tmp-opening-bracket ?\[
4256                     gnus-tmp-closing-bracket ?\]))
4257             (setq
4258              gnus-tmp-indentation
4259              (aref gnus-thread-indent-array gnus-tmp-level)
4260              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4261              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4262                                 gnus-summary-default-score 0)
4263              gnus-tmp-score-char
4264              (if (or (null gnus-summary-default-score)
4265                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4266                          gnus-summary-zcore-fuzz))
4267                  ?  ;Whitespace
4268                (if (< gnus-tmp-score gnus-summary-default-score)
4269                    gnus-score-below-mark gnus-score-over-mark))
4270              gnus-tmp-replied
4271              (cond ((memq number gnus-newsgroup-processable)
4272                     gnus-process-mark)
4273                    ((memq number gnus-newsgroup-cached)
4274                     gnus-cached-mark)
4275                    ((memq number gnus-newsgroup-replied)
4276                     gnus-replied-mark)
4277                    ((memq number gnus-newsgroup-forwarded)
4278                     gnus-forwarded-mark)
4279                    ((memq number gnus-newsgroup-saved)
4280                     gnus-saved-mark)
4281                    (t gnus-no-mark))
4282              gnus-tmp-from (mail-header-from gnus-tmp-header)
4283              gnus-tmp-name
4284              (cond
4285               ((string-match "<[^>]+> *$" gnus-tmp-from)
4286                (setq beg-match (match-beginning 0))
4287                (or (and (string-match "^\".+\"" gnus-tmp-from)
4288                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4289                    (substring gnus-tmp-from 0 beg-match)))
4290               ((string-match "(.+)" gnus-tmp-from)
4291                (substring gnus-tmp-from
4292                           (1+ (match-beginning 0)) (1- (match-end 0))))
4293               (t gnus-tmp-from)))
4294             (when (string= gnus-tmp-name "")
4295               (setq gnus-tmp-name gnus-tmp-from))
4296             (unless (numberp gnus-tmp-lines)
4297               (setq gnus-tmp-lines -1))
4298             (when (= gnus-tmp-lines -1)
4299               (setq gnus-tmp-lines "?"))
4300             (gnus-put-text-property
4301              (point)
4302              (progn (eval gnus-summary-line-format-spec) (point))
4303              'gnus-number number)
4304             (when gnus-visual-p
4305               (forward-line -1)
4306               (gnus-run-hooks 'gnus-summary-update-hook)
4307               (forward-line 1))
4308
4309             (setq gnus-tmp-prev-subject subject)))
4310
4311         (when (nth 1 thread)
4312           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4313         (incf gnus-tmp-level)
4314         (setq threads (if thread-end nil (cdar thread)))
4315         (unless threads
4316           (setq gnus-tmp-level 0)))))
4317   (gnus-message 7 "Generating summary...done"))
4318
4319 (defun gnus-summary-prepare-unthreaded (headers)
4320   "Generate an unthreaded summary buffer based on HEADERS."
4321   (let (header number mark)
4322
4323     (beginning-of-line)
4324
4325     (while headers
4326       ;; We may have to root out some bad articles...
4327       (when (memq (setq number (mail-header-number
4328                                 (setq header (pop headers))))
4329                   gnus-newsgroup-limit)
4330         ;; Mark article as read when it has a low score.
4331         (when (and gnus-summary-mark-below
4332                    (< (or (cdr (assq number gnus-newsgroup-scored))
4333                           gnus-summary-default-score 0)
4334                       gnus-summary-mark-below)
4335                    (not (gnus-summary-article-ancient-p number)))
4336           (setq gnus-newsgroup-unreads
4337                 (delq number gnus-newsgroup-unreads))
4338           (if gnus-newsgroup-auto-expire
4339               (push number gnus-newsgroup-expirable)
4340             (push (cons number gnus-low-score-mark)
4341                   gnus-newsgroup-reads)))
4342
4343         (setq mark (gnus-article-mark number))
4344         (push (gnus-data-make number mark (1+ (point)) header 0)
4345               gnus-newsgroup-data)
4346         (gnus-summary-insert-line
4347          header 0 number
4348          mark (memq number gnus-newsgroup-replied)
4349          (memq number gnus-newsgroup-expirable)
4350          (mail-header-subject header) nil
4351          (cdr (assq number gnus-newsgroup-scored))
4352          (memq number gnus-newsgroup-processable))))))
4353
4354 (defun gnus-summary-remove-list-identifiers ()
4355   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4356   (let ((regexp (if (consp gnus-list-identifiers)
4357                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4358                   gnus-list-identifiers))
4359         changed subject)
4360     (when regexp
4361       (dolist (header gnus-newsgroup-headers)
4362         (setq subject (mail-header-subject header)
4363               changed nil)
4364         (while (string-match
4365                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4366                 subject)
4367           (setq subject
4368                 (concat (substring subject 0 (match-beginning 2))
4369                         (substring subject (match-end 0)))
4370                 changed t))
4371         (when (and changed
4372                    (string-match
4373                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4374           (setq subject
4375                 (concat (substring subject 0 (match-beginning 1))
4376                         (substring subject (match-end 1)))))
4377         (when changed
4378           (mail-header-set-subject header subject))))))
4379
4380 (defun gnus-fetch-headers (articles)
4381   "Fetch headers of ARTICLES."
4382   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4383     (gnus-message 5 "Fetching headers for %s..." name)
4384     (prog1
4385         ;;;!!! FIXME: temporary fix for an infloop on nnimap.
4386         (if (eq 'nnimap (car (gnus-find-method-for-group name)))
4387             (if (eq 'nov
4388                     (setq gnus-headers-retrieved-by
4389                           (gnus-retrieve-headers
4390                            articles gnus-newsgroup-name
4391                            ;; We might want to fetch old headers, but
4392                            ;; not if there is only 1 article.
4393                            (and (or (and
4394                                      (not (eq gnus-fetch-old-headers 'some))
4395                                      (not (numberp gnus-fetch-old-headers)))
4396                                     (> (length articles) 1))
4397                                 gnus-fetch-old-headers))))
4398                 (gnus-get-newsgroup-headers-xover
4399                  articles nil nil gnus-newsgroup-name t)
4400               (gnus-get-newsgroup-headers))
4401           (gnus-retrieve-parsed-headers
4402            articles gnus-newsgroup-name
4403            ;; We might want to fetch old headers, but
4404            ;; not if there is only 1 article.
4405            (and (or (and (not (eq gnus-fetch-old-headers 'some))
4406                          (not (numberp gnus-fetch-old-headers)))
4407                     (> (length articles) 1))
4408                 gnus-fetch-old-headers)))
4409       (gnus-message 5 "Fetching headers for %s...done" name))))
4410
4411 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4412   "Select newsgroup GROUP.
4413 If READ-ALL is non-nil, all articles in the group are selected.
4414 If SELECT-ARTICLES, only select those articles from GROUP."
4415   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4416          ;;!!! Dirty hack; should be removed.
4417          (gnus-summary-ignore-duplicates
4418           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4419               t
4420             gnus-summary-ignore-duplicates))
4421          (info (nth 2 entry))
4422          articles fetched-articles cached)
4423
4424     (unless (gnus-check-server
4425              (setq gnus-current-select-method
4426                    (gnus-find-method-for-group group)))
4427       (error "Couldn't open server"))
4428
4429     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4430         (gnus-activate-group group)     ; Or we can activate it...
4431         (progn                          ; Or we bug out.
4432           (when (equal major-mode 'gnus-summary-mode)
4433             (kill-buffer (current-buffer)))
4434           (error "Couldn't activate group %s: %s"
4435                  group (gnus-status-message group))))
4436
4437     (unless (gnus-request-group group t)
4438       (when (equal major-mode 'gnus-summary-mode)
4439         (kill-buffer (current-buffer)))
4440       (error "Couldn't request group %s: %s"
4441              group (gnus-status-message group)))
4442
4443     (setq gnus-newsgroup-name group)
4444     (setq gnus-newsgroup-unselected nil)
4445     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4446     (gnus-summary-setup-default-charset)
4447
4448     ;; Adjust and set lists of article marks.
4449     (when info
4450       (gnus-adjust-marked-articles info))
4451
4452     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4453     (setq cached
4454           (if (gnus-virtual-group-p group)
4455               gnus-newsgroup-cached
4456             (gnus-cache-articles-in-group group)))
4457
4458     (setq gnus-newsgroup-unreads
4459           (gnus-set-difference
4460            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4461            gnus-newsgroup-dormant))
4462
4463     (setq gnus-newsgroup-processable nil)
4464
4465     (gnus-update-read-articles group gnus-newsgroup-unreads)
4466
4467     (if (setq articles select-articles)
4468         (setq gnus-newsgroup-unselected
4469               (gnus-sorted-intersection
4470                gnus-newsgroup-unreads
4471                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4472       (setq articles (gnus-articles-to-read group read-all)))
4473
4474     (cond
4475      ((null articles)
4476       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4477       'quit)
4478      ((eq articles 0) nil)
4479      (t
4480       ;; Init the dependencies hash table.
4481       (setq gnus-newsgroup-dependencies
4482             (gnus-make-hashtable (length articles)))
4483       (gnus-set-global-variables)
4484       ;; Retrieve the headers and read them in.
4485       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4486
4487       ;; Suppress duplicates?
4488       (when gnus-suppress-duplicates
4489         (gnus-dup-suppress-articles))
4490
4491       ;; Set the initial limit.
4492       (setq gnus-newsgroup-limit (copy-sequence articles))
4493       ;; Remove canceled articles from the list of unread articles.
4494       (setq gnus-newsgroup-unreads
4495             (gnus-set-sorted-intersection
4496              gnus-newsgroup-unreads
4497              (setq fetched-articles
4498                    (mapcar (lambda (headers) (mail-header-number headers))
4499                            gnus-newsgroup-headers))))
4500       ;; Removed marked articles that do not exist.
4501       (gnus-update-missing-marks
4502        (gnus-sorted-complement fetched-articles articles))
4503
4504       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4505       (when cached
4506         (setq gnus-newsgroup-cached cached))
4507
4508       ;; We might want to build some more threads first.
4509       (when (and gnus-fetch-old-headers
4510                  (eq gnus-headers-retrieved-by 'nov))
4511         (if (eq gnus-fetch-old-headers 'invisible)
4512             (gnus-build-all-threads)
4513           (gnus-build-old-threads)))
4514       ;; Let the Gnus agent mark articles as read.
4515       (when gnus-agent
4516         (gnus-agent-get-undownloaded-list))
4517       ;; Remove list identifiers from subject
4518       (when gnus-list-identifiers
4519         (gnus-summary-remove-list-identifiers))
4520       ;; Check whether auto-expire is to be done in this group.
4521       (setq gnus-newsgroup-auto-expire
4522             (gnus-group-auto-expirable-p group))
4523       ;; Set up the article buffer now, if necessary.
4524       (unless gnus-single-article-buffer
4525         (gnus-article-setup-buffer))
4526       ;; First and last article in this newsgroup.
4527       (when gnus-newsgroup-headers
4528         (setq gnus-newsgroup-begin
4529               (mail-header-number (car gnus-newsgroup-headers))
4530               gnus-newsgroup-end
4531               (mail-header-number
4532                (gnus-last-element gnus-newsgroup-headers))))
4533       ;; GROUP is successfully selected.
4534       (or gnus-newsgroup-headers t)))))
4535
4536 (defun gnus-articles-to-read (group &optional read-all)
4537   "Find out what articles the user wants to read."
4538   (let* ((articles
4539           ;; Select all articles if `read-all' is non-nil, or if there
4540           ;; are no unread articles.
4541           (if (or read-all
4542                   (and (zerop (length gnus-newsgroup-marked))
4543                        (zerop (length gnus-newsgroup-unreads)))
4544                   (eq (gnus-group-find-parameter group 'display)
4545                       'all))
4546               (or
4547                (gnus-uncompress-range (gnus-active group))
4548                (gnus-cache-articles-in-group group))
4549             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4550                           (copy-sequence gnus-newsgroup-unreads))
4551                   '<)))
4552          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4553          (scored (length scored-list))
4554          (number (length articles))
4555          (marked (+ (length gnus-newsgroup-marked)
4556                     (length gnus-newsgroup-dormant)))
4557          (select
4558           (cond
4559            ((numberp read-all)
4560             read-all)
4561            (t
4562             (condition-case ()
4563                 (cond
4564                  ((and (or (<= scored marked) (= scored number))
4565                        (natnump gnus-large-newsgroup)
4566                        (> number gnus-large-newsgroup))
4567                   (let* ((cursor-in-echo-area nil)
4568                          (input (read-from-minibuffer
4569                                  (format
4570                                   "How many articles from %s (max %d): "
4571                                   (gnus-limit-string gnus-newsgroup-name 35)
4572                                   number)
4573                                  (cons (number-to-string gnus-large-newsgroup)
4574                                        0))))
4575                     (if (string-match "^[ \t]*$" input)
4576                         number
4577                       input)))
4578                  ((and (> scored marked) (< scored number)
4579                        (> (- scored number) 20))
4580                   (let ((input
4581                          (read-string
4582                           (format "%s %s (%d scored, %d total): "
4583                                   "How many articles from"
4584                                   group scored number))))
4585                     (if (string-match "^[ \t]*$" input)
4586                         number input)))
4587                  (t number))
4588               (quit
4589                (message "Quit getting the articles to read")
4590                nil))))))
4591     (setq select (if (stringp select) (string-to-number select) select))
4592     (if (or (null select) (zerop select))
4593         select
4594       (if (and (not (zerop scored)) (<= (abs select) scored))
4595           (progn
4596             (setq articles (sort scored-list '<))
4597             (setq number (length articles)))
4598         (setq articles (copy-sequence articles)))
4599
4600       (when (< (abs select) number)
4601         (if (< select 0)
4602             ;; Select the N oldest articles.
4603             (setcdr (nthcdr (1- (abs select)) articles) nil)
4604           ;; Select the N most recent articles.
4605           (setq articles (nthcdr (- number select) articles))))
4606       (setq gnus-newsgroup-unselected
4607             (gnus-sorted-intersection
4608              gnus-newsgroup-unreads
4609              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4610       (when gnus-alter-articles-to-read-function
4611         (setq gnus-newsgroup-unreads
4612               (sort
4613                (funcall gnus-alter-articles-to-read-function
4614                         gnus-newsgroup-name gnus-newsgroup-unreads)
4615                '<)))
4616       articles)))
4617
4618 (defun gnus-killed-articles (killed articles)
4619   (let (out)
4620     (while articles
4621       (when (inline (gnus-member-of-range (car articles) killed))
4622         (push (car articles) out))
4623       (setq articles (cdr articles)))
4624     out))
4625
4626 (defun gnus-uncompress-marks (marks)
4627   "Uncompress the mark ranges in MARKS."
4628   (let ((uncompressed '(score bookmark))
4629         out)
4630     (while marks
4631       (if (memq (caar marks) uncompressed)
4632           (push (car marks) out)
4633         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4634       (setq marks (cdr marks)))
4635     out))
4636
4637 (defun gnus-adjust-marked-articles (info)
4638   "Set all article lists and remove all marks that are no longer valid."
4639   (let* ((marked-lists (gnus-info-marks info))
4640          (active (gnus-active (gnus-info-group info)))
4641          (min (car active))
4642          (max (cdr active))
4643          (types gnus-article-mark-lists)
4644          (uncompressed '(score bookmark killed))
4645          marks var articles article mark)
4646
4647     (while marked-lists
4648       (setq marks (pop marked-lists))
4649       (set (setq var (intern (format "gnus-newsgroup-%s"
4650                                      (car (rassq (setq mark (car marks))
4651                                                  types)))))
4652            (if (memq (car marks) uncompressed) (cdr marks)
4653              (gnus-uncompress-range (cdr marks))))
4654
4655       (setq articles (symbol-value var))
4656
4657       ;; All articles have to be subsets of the active articles.
4658       (cond
4659        ;; Adjust "simple" lists.
4660        ((memq mark '(tick dormant expire reply save))
4661         (while articles
4662           (when (or (< (setq article (pop articles)) min) (> article max))
4663             (set var (delq article (symbol-value var))))))
4664        ;; Adjust assocs.
4665        ((memq mark uncompressed)
4666         (when (not (listp (cdr (symbol-value var))))
4667           (set var (list (symbol-value var))))
4668         (when (not (listp (cdr articles)))
4669           (setq articles (list articles)))
4670         (while articles
4671           (when (or (not (consp (setq article (pop articles))))
4672                     (< (car article) min)
4673                     (> (car article) max))
4674             (set var (delq article (symbol-value var))))))))))
4675
4676 (defun gnus-update-missing-marks (missing)
4677   "Go through the list of MISSING articles and remove them from the mark lists."
4678   (when missing
4679     (let ((types gnus-article-mark-lists)
4680           var m)
4681       ;; Go through all types.
4682       (while types
4683         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4684         (when (symbol-value var)
4685           ;; This list has articles.  So we delete all missing articles
4686           ;; from it.
4687           (setq m missing)
4688           (while m
4689             (set var (delq (pop m) (symbol-value var)))))))))
4690
4691 (defun gnus-update-marks ()
4692   "Enter the various lists of marked articles into the newsgroup info list."
4693   (let ((types gnus-article-mark-lists)
4694         (info (gnus-get-info gnus-newsgroup-name))
4695         (uncompressed '(score bookmark killed))
4696         type list newmarked symbol delta-marks)
4697     (when info
4698       ;; Add all marks lists to the list of marks lists.
4699       (while (setq type (pop types))
4700         (setq list (symbol-value
4701                     (setq symbol
4702                           (intern (format "gnus-newsgroup-%s"
4703                                           (car type))))))
4704
4705         (when list
4706           ;; Get rid of the entries of the articles that have the
4707           ;; default score.
4708           (when (and (eq (cdr type) 'score)
4709                      gnus-save-score
4710                      list)
4711             (let* ((arts list)
4712                    (prev (cons nil list))
4713                    (all prev))
4714               (while arts
4715                 (if (or (not (consp (car arts)))
4716                         (= (cdar arts) gnus-summary-default-score))
4717                     (setcdr prev (cdr arts))
4718                   (setq prev arts))
4719                 (setq arts (cdr arts)))
4720               (setq list (cdr all)))))
4721
4722         (unless (memq (cdr type) uncompressed)
4723           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4724
4725         (when (gnus-check-backend-function
4726                'request-set-mark gnus-newsgroup-name)
4727           ;; propagate flags to server, with the following exceptions:
4728           ;; uncompressed:s are not proper flags (they are cons cells)
4729           ;; cache is a internal gnus flag
4730           ;; download are local to one gnus installation (well)
4731           ;; unsend are for nndraft groups only
4732           ;; xxx: generality of this?  this suits nnimap anyway
4733           (unless (memq (cdr type) (append '(cache download unsend)
4734                                            uncompressed))
4735             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4736                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4737                    (add (gnus-remove-from-range
4738                          (gnus-copy-sequence list) old)))
4739               (when add
4740                 (push (list add 'add (list (cdr type))) delta-marks))
4741               (when del
4742                 (push (list del 'del (list (cdr type))) delta-marks)))))
4743
4744         (when list
4745           (push (cons (cdr type) list) newmarked)))
4746
4747       (when delta-marks
4748         (unless (gnus-check-group gnus-newsgroup-name)
4749           (error "Can't open server for %s" gnus-newsgroup-name))
4750         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4751
4752       ;; Enter these new marks into the info of the group.
4753       (if (nthcdr 3 info)
4754           (setcar (nthcdr 3 info) newmarked)
4755         ;; Add the marks lists to the end of the info.
4756         (when newmarked
4757           (setcdr (nthcdr 2 info) (list newmarked))))
4758
4759       ;; Cut off the end of the info if there's nothing else there.
4760       (let ((i 5))
4761         (while (and (> i 2)
4762                     (not (nth i info)))
4763           (when (nthcdr (decf i) info)
4764             (setcdr (nthcdr i info) nil)))))))
4765
4766 (defun gnus-set-mode-line (where)
4767   "Set the mode line of the article or summary buffers.
4768 If WHERE is `summary', the summary mode line format will be used."
4769   ;; Is this mode line one we keep updated?
4770   (when (and (memq where gnus-updated-mode-lines)
4771              (symbol-value
4772               (intern (format "gnus-%s-mode-line-format-spec" where))))
4773     (let (mode-string)
4774       (save-excursion
4775         ;; We evaluate this in the summary buffer since these
4776         ;; variables are buffer-local to that buffer.
4777         (set-buffer gnus-summary-buffer)
4778         ;; We bind all these variables that are used in the `eval' form
4779         ;; below.
4780         (let* ((mformat (symbol-value
4781                          (intern
4782                           (format "gnus-%s-mode-line-format-spec" where))))
4783                (gnus-tmp-group-name (gnus-group-decoded-name 
4784                                      gnus-newsgroup-name))
4785                (gnus-tmp-article-number (or gnus-current-article 0))
4786                (gnus-tmp-unread gnus-newsgroup-unreads)
4787                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4788                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4789                (gnus-tmp-unread-and-unselected
4790                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4791                             (zerop gnus-tmp-unselected))
4792                        "")
4793                       ((zerop gnus-tmp-unselected)
4794                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4795                       (t (format "{%d(+%d) more}"
4796                                  gnus-tmp-unread-and-unticked
4797                                  gnus-tmp-unselected))))
4798                (gnus-tmp-subject
4799                 (if (and gnus-current-headers
4800                          (vectorp gnus-current-headers))
4801                     (gnus-mode-string-quote
4802                      (mail-header-subject gnus-current-headers))
4803                   ""))
4804                bufname-length max-len
4805                gnus-tmp-header);; passed as argument to any user-format-funcs
4806           (setq mode-string (eval mformat))
4807           (setq bufname-length (if (string-match "%b" mode-string)
4808                                    (- (length
4809                                        (buffer-name
4810                                         (if (eq where 'summary)
4811                                             nil
4812                                           (get-buffer gnus-article-buffer))))
4813                                       2)
4814                                  0))
4815           (setq max-len (max 4 (if gnus-mode-non-string-length
4816                                    (- (window-width)
4817                                       gnus-mode-non-string-length
4818                                       bufname-length)
4819                                  (length mode-string))))
4820           ;; We might have to chop a bit of the string off...
4821           (when (> (length mode-string) max-len)
4822             (setq mode-string
4823                   (concat (gnus-truncate-string mode-string (- max-len 3))
4824                           "...")))
4825           ;; Pad the mode string a bit.
4826           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4827       ;; Update the mode line.
4828       (setq mode-line-buffer-identification
4829             (gnus-mode-line-buffer-identification (list mode-string)))
4830       (set-buffer-modified-p t))))
4831
4832 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4833   "Go through the HEADERS list and add all Xrefs to a hash table.
4834 The resulting hash table is returned, or nil if no Xrefs were found."
4835   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4836          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4837          (xref-hashtb (gnus-make-hashtable))
4838          start group entry number xrefs header)
4839     (while headers
4840       (setq header (pop headers))
4841       (when (and (setq xrefs (mail-header-xref header))
4842                  (not (memq (setq number (mail-header-number header))
4843                             unreads)))
4844         (setq start 0)
4845         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4846           (setq start (match-end 0))
4847           (setq group (if prefix
4848                           (concat prefix (substring xrefs (match-beginning 1)
4849                                                     (match-end 1)))
4850                         (substring xrefs (match-beginning 1) (match-end 1))))
4851           (setq number
4852                 (string-to-int (substring xrefs (match-beginning 2)
4853                                           (match-end 2))))
4854           (if (setq entry (gnus-gethash group xref-hashtb))
4855               (setcdr entry (cons number (cdr entry)))
4856             (gnus-sethash group (cons number nil) xref-hashtb)))))
4857     (and start xref-hashtb)))
4858
4859 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4860   "Look through all the headers and mark the Xrefs as read."
4861   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4862         name entry info xref-hashtb idlist method nth4)
4863     (save-excursion
4864       (set-buffer gnus-group-buffer)
4865       (when (setq xref-hashtb
4866                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4867         (mapatoms
4868          (lambda (group)
4869            (unless (string= from-newsgroup (setq name (symbol-name group)))
4870              (setq idlist (symbol-value group))
4871              ;; Dead groups are not updated.
4872              (and (prog1
4873                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4874                             info (nth 2 entry))
4875                     (when (stringp (setq nth4 (gnus-info-method info)))
4876                       (setq nth4 (gnus-server-to-method nth4))))
4877                   ;; Only do the xrefs if the group has the same
4878                   ;; select method as the group we have just read.
4879                   (or (gnus-methods-equal-p
4880                        nth4 (gnus-find-method-for-group from-newsgroup))
4881                       virtual
4882                       (equal nth4 (setq method (gnus-find-method-for-group
4883                                                 from-newsgroup)))
4884                       (and (equal (car nth4) (car method))
4885                            (equal (nth 1 nth4) (nth 1 method))))
4886                   gnus-use-cross-reference
4887                   (or (not (eq gnus-use-cross-reference t))
4888                       virtual
4889                       ;; Only do cross-references on subscribed
4890                       ;; groups, if that is what is wanted.
4891                       (<= (gnus-info-level info) gnus-level-subscribed))
4892                   (gnus-group-make-articles-read name idlist))))
4893          xref-hashtb)))))
4894
4895 (defun gnus-compute-read-articles (group articles)
4896   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4897          (info (nth 2 entry))
4898          (active (gnus-active group))
4899          ninfo)
4900     (when entry
4901       ;; First peel off all invalid article numbers.
4902       (when active
4903         (let ((ids articles)
4904               id first)
4905           (while (setq id (pop ids))
4906             (when (and first (> id (cdr active)))
4907               ;; We'll end up in this situation in one particular
4908               ;; obscure situation.  If you re-scan a group and get
4909               ;; a new article that is cross-posted to a different
4910               ;; group that has not been re-scanned, you might get
4911               ;; crossposted article that has a higher number than
4912               ;; Gnus believes possible.  So we re-activate this
4913               ;; group as well.  This might mean doing the
4914               ;; crossposting thingy will *increase* the number
4915               ;; of articles in some groups.  Tsk, tsk.
4916               (setq active (or (gnus-activate-group group) active)))
4917             (when (or (> id (cdr active))
4918                       (< id (car active)))
4919               (setq articles (delq id articles))))))
4920       ;; If the read list is nil, we init it.
4921       (if (and active
4922                (null (gnus-info-read info))
4923                (> (car active) 1))
4924           (setq ninfo (cons 1 (1- (car active))))
4925         (setq ninfo (gnus-info-read info)))
4926       ;; Then we add the read articles to the range.
4927       (gnus-add-to-range
4928        ninfo (setq articles (sort articles '<))))))
4929
4930 (defun gnus-group-make-articles-read (group articles)
4931   "Update the info of GROUP to say that ARTICLES are read."
4932   (let* ((num 0)
4933          (entry (gnus-gethash group gnus-newsrc-hashtb))
4934          (info (nth 2 entry))
4935          (active (gnus-active group))
4936          range)
4937     (when entry
4938       (setq range (gnus-compute-read-articles group articles))
4939       (save-excursion
4940         (set-buffer gnus-group-buffer)
4941         (gnus-undo-register
4942           `(progn
4943              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4944              (gnus-info-set-read ',info ',(gnus-info-read info))
4945              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4946              (gnus-group-update-group ,group t))))
4947       ;; Add the read articles to the range.
4948       (gnus-info-set-read info range)
4949       ;; Then we have to re-compute how many unread
4950       ;; articles there are in this group.
4951       (when active
4952         (cond
4953          ((not range)
4954           (setq num (- (1+ (cdr active)) (car active))))
4955          ((not (listp (cdr range)))
4956           (setq num (- (cdr active) (- (1+ (cdr range))
4957                                        (car range)))))
4958          (t
4959           (while range
4960             (if (numberp (car range))
4961                 (setq num (1+ num))
4962               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4963             (setq range (cdr range)))
4964           (setq num (- (cdr active) num))))
4965         ;; Update the number of unread articles.
4966         (setcar entry num)
4967         ;; Update the group buffer.
4968         (gnus-group-update-group group t)))))
4969
4970 (defvar gnus-newsgroup-none-id 0)
4971
4972 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4973   (let ((cur nntp-server-buffer)
4974         (dependencies
4975          (or dependencies
4976              (save-excursion (set-buffer gnus-summary-buffer)
4977                              gnus-newsgroup-dependencies)))
4978         headers id end ref
4979         (mail-parse-charset gnus-newsgroup-charset)
4980         (mail-parse-ignored-charsets
4981          (save-excursion (condition-case nil
4982                              (set-buffer gnus-summary-buffer)
4983                            (error))
4984                          gnus-newsgroup-ignored-charsets)))
4985     (save-excursion
4986       (set-buffer nntp-server-buffer)
4987       ;; Translate all TAB characters into SPACE characters.
4988       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4989       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4990       (gnus-run-hooks 'gnus-parse-headers-hook)
4991       (let ((case-fold-search t)
4992             in-reply-to header p lines chars ctype)
4993         (goto-char (point-min))
4994         ;; Search to the beginning of the next header.  Error messages
4995         ;; do not begin with 2 or 3.
4996         (while (re-search-forward "^[23][0-9]+ " nil t)
4997           (setq id nil
4998                 ref nil)
4999           ;; This implementation of this function, with nine
5000           ;; search-forwards instead of the one re-search-forward and
5001           ;; a case (which basically was the old function) is actually
5002           ;; about twice as fast, even though it looks messier.  You
5003           ;; can't have everything, I guess.  Speed and elegance
5004           ;; doesn't always go hand in hand.
5005           (setq
5006            header
5007            (make-full-mail-header
5008             ;; Number.
5009             (prog1
5010                 (read cur)
5011               (end-of-line)
5012               (setq p (point))
5013               (narrow-to-region (point)
5014                                 (or (and (search-forward "\n.\n" nil t)
5015                                          (- (point) 2))
5016                                     (point))))
5017             ;; Subject.
5018             (progn
5019               (goto-char p)
5020               (if (search-forward "\nsubject: " nil t)
5021                   (nnheader-header-value)
5022                 "(none)"))
5023             ;; From.
5024             (progn
5025               (goto-char p)
5026               (if (or (search-forward "\nfrom: " nil t)
5027                       (search-forward "\nfrom:" nil t))
5028                   (nnheader-header-value)
5029                 "(nobody)"))
5030             ;; Date.
5031             (progn
5032               (goto-char p)
5033               (if (search-forward "\ndate: " nil t)
5034                   (nnheader-header-value)
5035                 ""))
5036             ;; Message-ID.
5037             (progn
5038               (goto-char p)
5039               (setq id (if (re-search-forward
5040                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5041                            ;; We do it this way to make sure the Message-ID
5042                            ;; is (somewhat) syntactically valid.
5043                            (buffer-substring (match-beginning 1)
5044                                              (match-end 1))
5045                          ;; If there was no message-id, we just fake one
5046                          ;; to make subsequent routines simpler.
5047                          (nnheader-generate-fake-message-id))))
5048             ;; References.
5049             (progn
5050               (goto-char p)
5051               (if (search-forward "\nreferences: " nil t)
5052                   (progn
5053                     (setq end (point))
5054                     (prog1
5055                         (nnheader-header-value)
5056                       (setq ref
5057                             (buffer-substring
5058                              (progn
5059                                ;; (end-of-line)
5060                                (search-backward ">" end t)
5061                                (1+ (point)))
5062                              (progn
5063                                (search-backward "<" end t)
5064                                (point))))))
5065                 ;; Get the references from the in-reply-to header if there
5066                 ;; were no references and the in-reply-to header looks
5067                 ;; promising.
5068                 (if (and (search-forward "\nin-reply-to: " nil t)
5069                          (setq in-reply-to (nnheader-header-value))
5070                          (string-match "<[^>]+>" in-reply-to))
5071                     (let (ref2)
5072                       (setq ref (substring in-reply-to (match-beginning 0)
5073                                            (match-end 0)))
5074                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5075                         (setq ref2 (substring in-reply-to (match-beginning 0)
5076                                               (match-end 0)))
5077                         (when (> (length ref2) (length ref))
5078                           (setq ref ref2)))
5079                       ref)
5080                   (setq ref nil))))
5081             ;; Chars.
5082             (progn
5083               (goto-char p)
5084               (if (search-forward "\nchars: " nil t)
5085                   (if (numberp (setq chars (ignore-errors (read cur))))
5086                       chars -1)
5087                 -1))
5088             ;; Lines.
5089             (progn
5090               (goto-char p)
5091               (if (search-forward "\nlines: " nil t)
5092                   (if (numberp (setq lines (ignore-errors (read cur))))
5093                       lines -1)
5094                 -1))
5095             ;; Xref.
5096             (progn
5097               (goto-char p)
5098               (and (search-forward "\nxref: " nil t)
5099                    (nnheader-header-value)))
5100             ;; Extra.
5101             (when gnus-extra-headers
5102               (let ((extra gnus-extra-headers)
5103                     out)
5104                 (while extra
5105                   (goto-char p)
5106                   (when (search-forward
5107                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
5108                     (push (cons (car extra) (nnheader-header-value)) out))
5109                   (pop extra))
5110                 out))))
5111           (goto-char p)
5112           (if (and (search-forward "\ncontent-type: " nil t)
5113                    (setq ctype (nnheader-header-value)))
5114               (mime-entity-set-content-type-internal
5115                header (mime-parse-Content-Type ctype)))
5116           (when (equal id ref)
5117             (setq ref nil))
5118
5119           (when gnus-alter-header-function
5120             (funcall gnus-alter-header-function header)
5121             (setq id (mail-header-id header)
5122                   ref (gnus-parent-id (mail-header-references header))))
5123
5124           (when (setq header
5125                       (gnus-dependencies-add-header
5126                        header dependencies force-new))
5127             (push header headers))
5128           (goto-char (point-max))
5129           (widen))
5130         (nreverse headers)))))
5131
5132 ;; Goes through the xover lines and returns a list of vectors
5133 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5134                                                   force-new dependencies
5135                                                   group also-fetch-heads)
5136   "Parse the news overview data in the server buffer.
5137 Return a list of headers that match SEQUENCE (see
5138 `nntp-retrieve-headers')."
5139   ;; Get the Xref when the users reads the articles since most/some
5140   ;; NNTP servers do not include Xrefs when using XOVER.
5141   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5142   (let ((mail-parse-charset gnus-newsgroup-charset)
5143         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5144         (cur nntp-server-buffer)
5145         (dependencies (or dependencies gnus-newsgroup-dependencies))
5146         number headers header)
5147     (save-excursion
5148       (set-buffer nntp-server-buffer)
5149       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5150       ;; Allow the user to mangle the headers before parsing them.
5151       (gnus-run-hooks 'gnus-parse-headers-hook)
5152       (goto-char (point-min))
5153       (while (not (eobp))
5154         (condition-case ()
5155             (while (and sequence (not (eobp)))
5156               (setq number (read cur))
5157               (while (and sequence
5158                           (< (car sequence) number))
5159                 (setq sequence (cdr sequence)))
5160               (and sequence
5161                    (eq number (car sequence))
5162                    (progn
5163                      (setq sequence (cdr sequence))
5164                      (setq header (inline
5165                                     (gnus-nov-parse-line
5166                                      number dependencies force-new))))
5167                    (push header headers))
5168               (forward-line 1))
5169           (error
5170            (gnus-error 4 "Strange nov line (%d)"
5171                        (count-lines (point-min) (point)))))
5172         (forward-line 1))
5173       ;; A common bug in inn is that if you have posted an article and
5174       ;; then retrieves the active file, it will answer correctly --
5175       ;; the new article is included.  However, a NOV entry for the
5176       ;; article may not have been generated yet, so this may fail.
5177       ;; We work around this problem by retrieving the last few
5178       ;; headers using HEAD.
5179       (if (or (not also-fetch-heads)
5180               (not sequence))
5181           ;; We (probably) got all the headers.
5182           (nreverse headers)
5183         (let ((gnus-nov-is-evil t))
5184           (nconc
5185            (nreverse headers)
5186            ;;;!!! FIXME: temporary fix for an infloop on nnimap.
5187            (if (eq 'nnimap (car (gnus-find-method-for-group group)))
5188                (when (gnus-retrieve-headers sequence group)
5189                  (gnus-get-newsgroup-headers))
5190              (gnus-retrieve-parsed-headers sequence group))))))))
5191
5192 (defun gnus-article-get-xrefs ()
5193   "Fill in the Xref value in `gnus-current-headers', if necessary.
5194 This is meant to be called in `gnus-article-internal-prepare-hook'."
5195   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5196                                  gnus-current-headers)))
5197     (or (not gnus-use-cross-reference)
5198         (not headers)
5199         (and (mail-header-xref headers)
5200              (not (string= (mail-header-xref headers) "")))
5201         (let ((case-fold-search t)
5202               xref)
5203           (save-restriction
5204             (nnheader-narrow-to-headers)
5205             (goto-char (point-min))
5206             (when (or (and (not (eobp))
5207                            (eq (downcase (char-after)) ?x)
5208                            (looking-at "Xref:"))
5209                       (search-forward "\nXref:" nil t))
5210               (goto-char (1+ (match-end 0)))
5211               (setq xref (buffer-substring (point)
5212                                            (progn (end-of-line) (point))))
5213               (mail-header-set-xref headers xref)))))))
5214
5215 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5216   "Find article ID and insert the summary line for that article.
5217 OLD-HEADER can either be a header or a line number to insert
5218 the subject line on."
5219   (let* ((line (and (numberp old-header) old-header))
5220          (old-header (and (vectorp old-header) old-header))
5221          (header (cond ((and old-header use-old-header)
5222                         old-header)
5223                        ((and (numberp id)
5224                              (gnus-number-to-header id))
5225                         (gnus-number-to-header id))
5226                        (t
5227                         (gnus-read-header id))))
5228          (number (and (numberp id) id))
5229          d)
5230     (when header
5231       ;; Rebuild the thread that this article is part of and go to the
5232       ;; article we have fetched.
5233       (when (and (not gnus-show-threads)
5234                  old-header)
5235         (when (and number
5236                    (setq d (gnus-data-find (mail-header-number old-header))))
5237           (goto-char (gnus-data-pos d))
5238           (gnus-data-remove
5239            number
5240            (- (gnus-point-at-bol)
5241               (prog1
5242                   (1+ (gnus-point-at-eol))
5243                 (gnus-delete-line))))))
5244       (when old-header
5245         (mail-header-set-number header (mail-header-number old-header)))
5246       (setq gnus-newsgroup-sparse
5247             (delq (setq number (mail-header-number header))
5248                   gnus-newsgroup-sparse))
5249       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5250       (push number gnus-newsgroup-limit)
5251       (gnus-rebuild-thread (mail-header-id header) line)
5252       (gnus-summary-goto-subject number nil t))
5253     (when (and (numberp number)
5254                (> number 0))
5255       ;; We have to update the boundaries even if we can't fetch the
5256       ;; article if ID is a number -- so that the next `P' or `N'
5257       ;; command will fetch the previous (or next) article even
5258       ;; if the one we tried to fetch this time has been canceled.
5259       (when (> number gnus-newsgroup-end)
5260         (setq gnus-newsgroup-end number))
5261       (when (< number gnus-newsgroup-begin)
5262         (setq gnus-newsgroup-begin number))
5263       (setq gnus-newsgroup-unselected
5264             (delq number gnus-newsgroup-unselected)))
5265     ;; Report back a success?
5266     (and header (mail-header-number header))))
5267
5268 ;;; Process/prefix in the summary buffer
5269
5270 (defun gnus-summary-work-articles (n)
5271   "Return a list of articles to be worked upon.
5272 The prefix argument, the list of process marked articles, and the
5273 current article will be taken into consideration."
5274   (save-excursion
5275     (set-buffer gnus-summary-buffer)
5276     (cond
5277      (n
5278       ;; A numerical prefix has been given.
5279       (setq n (prefix-numeric-value n))
5280       (let ((backward (< n 0))
5281             (n (abs (prefix-numeric-value n)))
5282             articles article)
5283         (save-excursion
5284           (while
5285               (and (> n 0)
5286                    (push (setq article (gnus-summary-article-number))
5287                          articles)
5288                    (if backward
5289                        (gnus-summary-find-prev nil article)
5290                      (gnus-summary-find-next nil article)))
5291             (decf n)))
5292         (nreverse articles)))
5293      ((and (gnus-region-active-p) (mark))
5294       (message "region active")
5295       ;; Work on the region between point and mark.
5296       (let ((max (max (point) (mark)))
5297             articles article)
5298         (save-excursion
5299           (goto-char (min (point) (mark)))
5300           (while
5301               (and
5302                (push (setq article (gnus-summary-article-number)) articles)
5303                (gnus-summary-find-next nil article)
5304                (< (point) max)))
5305           (nreverse articles))))
5306      (gnus-newsgroup-processable
5307       ;; There are process-marked articles present.
5308       ;; Save current state.
5309       (gnus-summary-save-process-mark)
5310       ;; Return the list.
5311       (reverse gnus-newsgroup-processable))
5312      (t
5313       ;; Just return the current article.
5314       (list (gnus-summary-article-number))))))
5315
5316 (defmacro gnus-summary-iterate (arg &rest forms)
5317   "Iterate over the process/prefixed articles and do FORMS.
5318 ARG is the interactive prefix given to the command.  FORMS will be
5319 executed with point over the summary line of the articles."
5320   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5321     `(let ((,articles (gnus-summary-work-articles ,arg)))
5322        (while ,articles
5323          (gnus-summary-goto-subject (car ,articles))
5324          ,@forms
5325          (pop ,articles)))))
5326
5327 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5328 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5329
5330 (defun gnus-summary-save-process-mark ()
5331   "Push the current set of process marked articles on the stack."
5332   (interactive)
5333   (push (copy-sequence gnus-newsgroup-processable)
5334         gnus-newsgroup-process-stack))
5335
5336 (defun gnus-summary-kill-process-mark ()
5337   "Push the current set of process marked articles on the stack and unmark."
5338   (interactive)
5339   (gnus-summary-save-process-mark)
5340   (gnus-summary-unmark-all-processable))
5341
5342 (defun gnus-summary-yank-process-mark ()
5343   "Pop the last process mark state off the stack and restore it."
5344   (interactive)
5345   (unless gnus-newsgroup-process-stack
5346     (error "Empty mark stack"))
5347   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5348
5349 (defun gnus-summary-process-mark-set (set)
5350   "Make SET into the current process marked articles."
5351   (gnus-summary-unmark-all-processable)
5352   (while set
5353     (gnus-summary-set-process-mark (pop set))))
5354
5355 ;;; Searching and stuff
5356
5357 (defun gnus-summary-search-group (&optional backward use-level)
5358   "Search for next unread newsgroup.
5359 If optional argument BACKWARD is non-nil, search backward instead."
5360   (save-excursion
5361     (set-buffer gnus-group-buffer)
5362     (when (gnus-group-search-forward
5363            backward nil (if use-level (gnus-group-group-level) nil))
5364       (gnus-group-group-name))))
5365
5366 (defun gnus-summary-best-group (&optional exclude-group)
5367   "Find the name of the best unread group.
5368 If EXCLUDE-GROUP, do not go to this group."
5369   (save-excursion
5370     (set-buffer gnus-group-buffer)
5371     (save-excursion
5372       (gnus-group-best-unread-group exclude-group))))
5373
5374 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5375   (if backward (gnus-summary-find-prev)
5376     (let* ((dummy (gnus-summary-article-intangible-p))
5377            (article (or article (gnus-summary-article-number)))
5378            (arts (gnus-data-find-list article))
5379            result)
5380       (when (and (not dummy)
5381                  (or (not gnus-summary-check-current)
5382                      (not unread)
5383                      (not (gnus-data-unread-p (car arts)))))
5384         (setq arts (cdr arts)))
5385       (when (setq result
5386                   (if unread
5387                       (progn
5388                         (while arts
5389                           (when (or (and undownloaded
5390                                          (eq gnus-undownloaded-mark
5391                                              (gnus-data-mark (car arts))))
5392                                     (gnus-data-unread-p (car arts)))
5393                             (setq result (car arts)
5394                                   arts nil))
5395                           (setq arts (cdr arts)))
5396                         result)
5397                     (car arts)))
5398         (goto-char (gnus-data-pos result))
5399         (gnus-data-number result)))))
5400
5401 (defun gnus-summary-find-prev (&optional unread article)
5402   (let* ((eobp (eobp))
5403          (article (or article (gnus-summary-article-number)))
5404          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5405          result)
5406     (when (and (not eobp)
5407                (or (not gnus-summary-check-current)
5408                    (not unread)
5409                    (not (gnus-data-unread-p (car arts)))))
5410       (setq arts (cdr arts)))
5411     (when (setq result
5412                 (if unread
5413                     (progn
5414                       (while arts
5415                         (when (gnus-data-unread-p (car arts))
5416                           (setq result (car arts)
5417                                 arts nil))
5418                         (setq arts (cdr arts)))
5419                       result)
5420                   (car arts)))
5421       (goto-char (gnus-data-pos result))
5422       (gnus-data-number result))))
5423
5424 (defun gnus-summary-find-subject (subject &optional unread backward article)
5425   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5426          (article (or article (gnus-summary-article-number)))
5427          (articles (gnus-data-list backward))
5428          (arts (gnus-data-find-list article articles))
5429          result)
5430     (when (or (not gnus-summary-check-current)
5431               (not unread)
5432               (not (gnus-data-unread-p (car arts))))
5433       (setq arts (cdr arts)))
5434     (while arts
5435       (and (or (not unread)
5436                (gnus-data-unread-p (car arts)))
5437            (vectorp (gnus-data-header (car arts)))
5438            (gnus-subject-equal
5439             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5440            (setq result (car arts)
5441                  arts nil))
5442       (setq arts (cdr arts)))
5443     (and result
5444          (goto-char (gnus-data-pos result))
5445          (gnus-data-number result))))
5446
5447 (defun gnus-summary-search-forward (&optional unread subject backward)
5448   "Search forward for an article.
5449 If UNREAD, look for unread articles.  If SUBJECT, look for
5450 articles with that subject.  If BACKWARD, search backward instead."
5451   (cond (subject (gnus-summary-find-subject subject unread backward))
5452         (backward (gnus-summary-find-prev unread))
5453         (t (gnus-summary-find-next unread))))
5454
5455 (defun gnus-recenter (&optional n)
5456   "Center point in window and redisplay frame.
5457 Also do horizontal recentering."
5458   (interactive "P")
5459   (when (and gnus-auto-center-summary
5460              (not (eq gnus-auto-center-summary 'vertical)))
5461     (gnus-horizontal-recenter))
5462   (recenter n))
5463
5464 (defun gnus-summary-recenter ()
5465   "Center point in the summary window.
5466 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5467 displayed, no centering will be performed."
5468   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5469   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5470   (interactive)
5471   (let* ((top (cond ((< (window-height) 4) 0)
5472                     ((< (window-height) 7) 1)
5473                     (t (if (numberp gnus-auto-center-summary)
5474                            gnus-auto-center-summary
5475                          2))))
5476          (height (1- (window-height)))
5477          (bottom (save-excursion (goto-char (point-max))
5478                                  (forward-line (- height))
5479                                  (point)))
5480          (window (get-buffer-window (current-buffer))))
5481     ;; The user has to want it.
5482     (when gnus-auto-center-summary
5483       (when (get-buffer-window gnus-article-buffer)
5484         ;; Only do recentering when the article buffer is displayed,
5485         ;; Set the window start to either `bottom', which is the biggest
5486         ;; possible valid number, or the second line from the top,
5487         ;; whichever is the least.
5488         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5489           (if (> bottom top-pos)
5490               ;; Keep the second line from the top visible
5491               (set-window-start window top-pos t)
5492             ;; Try to keep the bottom line visible; if it's partially
5493             ;; obscured, either scroll one more line to make it fully
5494             ;; visible, or revert to using TOP-POS.
5495             (save-excursion
5496               (goto-char (point-max))
5497               (forward-line -1)
5498               (let ((last-line-start (point)))
5499                 (goto-char bottom)
5500                 (set-window-start window (point) t)
5501                 (when (not (pos-visible-in-window-p last-line-start window))
5502                   (forward-line 1)
5503                   (set-window-start window (min (point) top-pos) t)))))))
5504       ;; Do horizontal recentering while we're at it.
5505       (when (and (get-buffer-window (current-buffer) t)
5506                  (not (eq gnus-auto-center-summary 'vertical)))
5507         (let ((selected (selected-window)))
5508           (select-window (get-buffer-window (current-buffer) t))
5509           (gnus-summary-position-point)
5510           (gnus-horizontal-recenter)
5511           (select-window selected))))))
5512
5513 (defun gnus-summary-jump-to-group (newsgroup)
5514   "Move point to NEWSGROUP in group mode buffer."
5515   ;; Keep update point of group mode buffer if visible.
5516   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5517       (save-window-excursion
5518         ;; Take care of tree window mode.
5519         (when (get-buffer-window gnus-group-buffer)
5520           (pop-to-buffer gnus-group-buffer))
5521         (gnus-group-jump-to-group newsgroup))
5522     (save-excursion
5523       ;; Take care of tree window mode.
5524       (if (get-buffer-window gnus-group-buffer)
5525           (pop-to-buffer gnus-group-buffer)
5526         (set-buffer gnus-group-buffer))
5527       (gnus-group-jump-to-group newsgroup))))
5528
5529 ;; This function returns a list of article numbers based on the
5530 ;; difference between the ranges of read articles in this group and
5531 ;; the range of active articles.
5532 (defun gnus-list-of-unread-articles (group)
5533   (let* ((read (gnus-info-read (gnus-get-info group)))
5534          (active (or (gnus-active group) (gnus-activate-group group)))
5535          (last (cdr active))
5536          first nlast unread)
5537     ;; If none are read, then all are unread.
5538     (if (not read)
5539         (setq first (car active))
5540       ;; If the range of read articles is a single range, then the
5541       ;; first unread article is the article after the last read
5542       ;; article.  Sounds logical, doesn't it?
5543       (if (and (not (listp (cdr read)))
5544                (or (< (car read) (car active))
5545                    (progn (setq read (list read))
5546                           nil)))
5547           (setq first (max (car active) (1+ (cdr read))))
5548         ;; `read' is a list of ranges.
5549         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5550                                   (caar read)))
5551                   1)
5552           (setq first (car active)))
5553         (while read
5554           (when first
5555             (while (< first nlast)
5556               (push first unread)
5557               (setq first (1+ first))))
5558           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5559           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5560           (setq read (cdr read)))))
5561     ;; And add the last unread articles.
5562     (while (<= first last)
5563       (push first unread)
5564       (setq first (1+ first)))
5565     ;; Return the list of unread articles.
5566     (delq 0 (nreverse unread))))
5567
5568 (defun gnus-list-of-read-articles (group)
5569   "Return a list of unread, unticked and non-dormant articles."
5570   (let* ((info (gnus-get-info group))
5571          (marked (gnus-info-marks info))
5572          (active (gnus-active group)))
5573     (and info active
5574          (gnus-set-difference
5575           (gnus-sorted-complement
5576            (gnus-uncompress-range active)
5577            (gnus-list-of-unread-articles group))
5578           (append
5579            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5580            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5581
5582 ;; Various summary commands
5583
5584 (defun gnus-summary-select-article-buffer ()
5585   "Reconfigure windows to show article buffer."
5586   (interactive)
5587   (if (not (gnus-buffer-live-p gnus-article-buffer))
5588       (error "There is no article buffer for this summary buffer")
5589     (gnus-configure-windows 'article)
5590     (select-window (get-buffer-window gnus-article-buffer))))
5591
5592 (defun gnus-summary-universal-argument (arg)
5593   "Perform any operation on all articles that are process/prefixed."
5594   (interactive "P")
5595   (let ((articles (gnus-summary-work-articles arg))
5596         func article)
5597     (if (eq
5598          (setq
5599           func
5600           (key-binding
5601            (read-key-sequence
5602             (substitute-command-keys
5603              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5604          'undefined)
5605         (gnus-error 1 "Undefined key")
5606       (save-excursion
5607         (while articles
5608           (gnus-summary-goto-subject (setq article (pop articles)))
5609           (let (gnus-newsgroup-processable)
5610             (command-execute func))
5611           (gnus-summary-remove-process-mark article)))))
5612   (gnus-summary-position-point))
5613
5614 (defun gnus-summary-toggle-truncation (&optional arg)
5615   "Toggle truncation of summary lines.
5616 With arg, turn line truncation on iff arg is positive."
5617   (interactive "P")
5618   (setq truncate-lines
5619         (if (null arg) (not truncate-lines)
5620           (> (prefix-numeric-value arg) 0)))
5621   (redraw-display))
5622
5623 (defun gnus-summary-reselect-current-group (&optional all rescan)
5624   "Exit and then reselect the current newsgroup.
5625 The prefix argument ALL means to select all articles."
5626   (interactive "P")
5627   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5628     (error "Ephemeral groups can't be reselected"))
5629   (let ((current-subject (gnus-summary-article-number))
5630         (group gnus-newsgroup-name))
5631     (setq gnus-newsgroup-begin nil)
5632     (gnus-summary-exit)
5633     ;; We have to adjust the point of group mode buffer because
5634     ;; point was moved to the next unread newsgroup by exiting.
5635     (gnus-summary-jump-to-group group)
5636     (when rescan
5637       (save-excursion
5638         (save-window-excursion
5639           ;; Don't show group contents.
5640           (set-window-start (selected-window) (point-max))
5641           (gnus-group-get-new-news-this-group 1))))
5642     (gnus-group-read-group all t)
5643     (gnus-summary-goto-subject current-subject nil t)))
5644
5645 (defun gnus-summary-rescan-group (&optional all)
5646   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5647   (interactive "P")
5648   (gnus-summary-reselect-current-group all t))
5649
5650 (defun gnus-summary-update-info (&optional non-destructive)
5651   (save-excursion
5652     (let ((group gnus-newsgroup-name))
5653       (when group
5654         (when gnus-newsgroup-kill-headers
5655           (setq gnus-newsgroup-killed
5656                 (gnus-compress-sequence
5657                  (nconc
5658                   (gnus-set-sorted-intersection
5659                    (gnus-uncompress-range gnus-newsgroup-killed)
5660                    (setq gnus-newsgroup-unselected
5661                          (sort gnus-newsgroup-unselected '<)))
5662                   (setq gnus-newsgroup-unreads
5663                         (sort gnus-newsgroup-unreads '<)))
5664                  t)))
5665         (unless (listp (cdr gnus-newsgroup-killed))
5666           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5667         (let ((headers gnus-newsgroup-headers))
5668           ;; Set the new ranges of read articles.
5669           (save-excursion
5670             (set-buffer gnus-group-buffer)
5671             (gnus-undo-force-boundary))
5672           (gnus-update-read-articles
5673            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5674           ;; Set the current article marks.
5675           (let ((gnus-newsgroup-scored
5676                  (if (and (not gnus-save-score)
5677                           (not non-destructive))
5678                      nil
5679                    gnus-newsgroup-scored)))
5680             (save-excursion
5681               (gnus-update-marks)))
5682           ;; Do the cross-ref thing.
5683           (when gnus-use-cross-reference
5684             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5685           ;; Do not switch windows but change the buffer to work.
5686           (set-buffer gnus-group-buffer)
5687           (unless (gnus-ephemeral-group-p group)
5688             (gnus-group-update-group group)))))))
5689
5690 (defun gnus-summary-save-newsrc (&optional force)
5691   "Save the current number of read/marked articles in the dribble buffer.
5692 The dribble buffer will then be saved.
5693 If FORCE (the prefix), also save the .newsrc file(s)."
5694   (interactive "P")
5695   (gnus-summary-update-info t)
5696   (if force
5697       (gnus-save-newsrc-file)
5698     (gnus-dribble-save)))
5699
5700 (defun gnus-summary-exit (&optional temporary)
5701   "Exit reading current newsgroup, and then return to group selection mode.
5702 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5703   (interactive)
5704   (gnus-set-global-variables)
5705   (gnus-kill-save-kill-buffer)
5706   (gnus-async-halt-prefetch)
5707   (let* ((group gnus-newsgroup-name)
5708          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5709          (mode major-mode)
5710          (group-point nil)
5711          (buf (current-buffer)))
5712     (unless quit-config
5713       ;; Do adaptive scoring, and possibly save score files.
5714       (when gnus-newsgroup-adaptive
5715         (gnus-score-adaptive))
5716       (when gnus-use-scoring
5717         (gnus-score-save)))
5718     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5719     ;; If we have several article buffers, we kill them at exit.
5720     (unless gnus-single-article-buffer
5721       (gnus-kill-buffer gnus-original-article-buffer)
5722       (setq gnus-article-current nil))
5723     (when gnus-use-cache
5724       (gnus-cache-possibly-remove-articles)
5725       (gnus-cache-save-buffers))
5726     (gnus-async-prefetch-remove-group group)
5727     (when gnus-suppress-duplicates
5728       (gnus-dup-enter-articles))
5729     (when gnus-use-trees
5730       (gnus-tree-close group))
5731     (when gnus-use-cache
5732       (gnus-cache-write-active))
5733     ;; Remove entries for this group.
5734     (nnmail-purge-split-history (gnus-group-real-name group))
5735     ;; Make all changes in this group permanent.
5736     (unless quit-config
5737       (gnus-run-hooks 'gnus-exit-group-hook)
5738       (gnus-summary-update-info))
5739     (gnus-close-group group)
5740     ;; Make sure where we were, and go to next newsgroup.
5741     (set-buffer gnus-group-buffer)
5742     (unless quit-config
5743       (gnus-group-jump-to-group group))
5744     (gnus-run-hooks 'gnus-summary-exit-hook)
5745     (unless (or quit-config
5746                 ;; If this group has disappeared from the summary
5747                 ;; buffer, don't skip forwards.
5748                 (not (string= group (gnus-group-group-name))))
5749       (gnus-group-next-unread-group 1))
5750     (setq group-point (point))
5751     (if temporary
5752         nil                             ;Nothing to do.
5753       ;; If we have several article buffers, we kill them at exit.
5754       (unless gnus-single-article-buffer
5755         (gnus-kill-buffer gnus-article-buffer)
5756         (gnus-kill-buffer gnus-original-article-buffer)
5757         (setq gnus-article-current nil))
5758       (set-buffer buf)
5759       (if (not gnus-kill-summary-on-exit)
5760           (progn
5761             (gnus-deaden-summary)
5762             (setq mode nil))
5763         ;; We set all buffer-local variables to nil.  It is unclear why
5764         ;; this is needed, but if we don't, buffer-local variables are
5765         ;; not garbage-collected, it seems.  This would the lead to en
5766         ;; ever-growing Emacs.
5767         (gnus-summary-clear-local-variables)
5768         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5769           (gnus-summary-clear-local-variables))
5770         (when (get-buffer gnus-article-buffer)
5771           (bury-buffer gnus-article-buffer))
5772         ;; We clear the global counterparts of the buffer-local
5773         ;; variables as well, just to be on the safe side.
5774         (set-buffer gnus-group-buffer)
5775         (gnus-summary-clear-local-variables)
5776         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5777           (gnus-summary-clear-local-variables)))
5778       (setq gnus-current-select-method gnus-select-method)
5779       (pop-to-buffer gnus-group-buffer)
5780       (if (not quit-config)
5781           (progn
5782             (goto-char group-point)
5783             (gnus-configure-windows 'group 'force)
5784             (unless (pos-visible-in-window-p)
5785               (forward-line (/ (static-if (featurep 'xemacs)
5786                                    (window-displayed-height)
5787                                  (1- (window-height)))
5788                                -2))
5789               (set-window-start (selected-window) (point))
5790               (goto-char group-point)))
5791         (gnus-handle-ephemeral-exit quit-config))
5792       ;; Return to group mode buffer.
5793       (when (eq mode 'gnus-summary-mode)
5794         (gnus-kill-buffer buf))
5795       ;; Clear the current group name.
5796       (unless quit-config
5797         (setq gnus-newsgroup-name nil)))))
5798
5799 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5800 (defun gnus-summary-exit-no-update (&optional no-questions)
5801   "Quit reading current newsgroup without updating read article info."
5802   (interactive)
5803   (let* ((group gnus-newsgroup-name)
5804          (quit-config (gnus-group-quit-config group)))
5805     (when (or no-questions
5806               gnus-expert-user
5807               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5808       (gnus-async-halt-prefetch)
5809       (mapcar 'funcall
5810               (delq 'gnus-summary-expire-articles
5811                     (copy-sequence gnus-summary-prepare-exit-hook)))
5812       ;; If we have several article buffers, we kill them at exit.
5813       (unless gnus-single-article-buffer
5814         (gnus-kill-buffer gnus-article-buffer)
5815         (gnus-kill-buffer gnus-original-article-buffer)
5816         (setq gnus-article-current nil))
5817       (if (not gnus-kill-summary-on-exit)
5818           (gnus-deaden-summary)
5819         (gnus-close-group group)
5820         (gnus-summary-clear-local-variables)
5821         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5822           (gnus-summary-clear-local-variables))
5823         (set-buffer gnus-group-buffer)
5824         (gnus-summary-clear-local-variables)
5825         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5826           (gnus-summary-clear-local-variables))
5827         (when (get-buffer gnus-summary-buffer)
5828           (kill-buffer gnus-summary-buffer)))
5829       (unless gnus-single-article-buffer
5830         (setq gnus-article-current nil))
5831       (when gnus-use-trees
5832         (gnus-tree-close group))
5833       (gnus-async-prefetch-remove-group group)
5834       (when (get-buffer gnus-article-buffer)
5835         (bury-buffer gnus-article-buffer))
5836       ;; Return to the group buffer.
5837       (gnus-configure-windows 'group 'force)
5838       ;; Clear the current group name.
5839       (setq gnus-newsgroup-name nil)
5840       (when (equal (gnus-group-group-name) group)
5841         (gnus-group-next-unread-group 1))
5842       (when quit-config
5843         (gnus-handle-ephemeral-exit quit-config)))))
5844
5845 (defun gnus-handle-ephemeral-exit (quit-config)
5846   "Handle movement when leaving an ephemeral group.
5847 The state which existed when entering the ephemeral is reset."
5848   (if (not (buffer-name (car quit-config)))
5849       (gnus-configure-windows 'group 'force)
5850     (set-buffer (car quit-config))
5851     (cond ((eq major-mode 'gnus-summary-mode)
5852            (gnus-set-global-variables))
5853           ((eq major-mode 'gnus-article-mode)
5854            (save-excursion
5855              ;; The `gnus-summary-buffer' variable may point
5856              ;; to the old summary buffer when using a single
5857              ;; article buffer.
5858              (unless (gnus-buffer-live-p gnus-summary-buffer)
5859                (set-buffer gnus-group-buffer))
5860              (set-buffer gnus-summary-buffer)
5861              (gnus-set-global-variables))))
5862     (if (or (eq (cdr quit-config) 'article)
5863             (eq (cdr quit-config) 'pick))
5864         (progn
5865           ;; The current article may be from the ephemeral group
5866           ;; thus it is best that we reload this article
5867           (gnus-summary-show-article)
5868           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5869               (gnus-configure-windows 'pick 'force)
5870             (gnus-configure-windows (cdr quit-config) 'force)))
5871       (gnus-configure-windows (cdr quit-config) 'force))
5872     (when (eq major-mode 'gnus-summary-mode)
5873       (gnus-summary-next-subject 1 nil t)
5874       (gnus-summary-recenter)
5875       (gnus-summary-position-point))))
5876
5877 (defun gnus-summary-preview-mime-message ()
5878   "MIME decode and play this message."
5879   (interactive)
5880   (let ((gnus-break-pages nil)
5881         (gnus-show-mime t))
5882     (gnus-summary-select-article gnus-show-all-headers t))
5883   (select-window (get-buffer-window gnus-article-buffer)))
5884
5885 ;;; Dead summaries.
5886
5887 (defvar gnus-dead-summary-mode-map nil)
5888
5889 (unless gnus-dead-summary-mode-map
5890   (setq gnus-dead-summary-mode-map (make-keymap))
5891   (suppress-keymap gnus-dead-summary-mode-map)
5892   (substitute-key-definition
5893    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5894   (let ((keys '("\C-d" "\r" "\177" [delete])))
5895     (while keys
5896       (define-key gnus-dead-summary-mode-map
5897         (pop keys) 'gnus-summary-wake-up-the-dead))))
5898
5899 (defvar gnus-dead-summary-mode nil
5900   "Minor mode for Gnus summary buffers.")
5901
5902 (defun gnus-dead-summary-mode (&optional arg)
5903   "Minor mode for Gnus summary buffers."
5904   (interactive "P")
5905   (when (eq major-mode 'gnus-summary-mode)
5906     (make-local-variable 'gnus-dead-summary-mode)
5907     (setq gnus-dead-summary-mode
5908           (if (null arg) (not gnus-dead-summary-mode)
5909             (> (prefix-numeric-value arg) 0)))
5910     (when gnus-dead-summary-mode
5911       (gnus-add-minor-mode
5912        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5913
5914 (defun gnus-deaden-summary ()
5915   "Make the current summary buffer into a dead summary buffer."
5916   ;; Kill any previous dead summary buffer.
5917   (when (and gnus-dead-summary
5918              (buffer-name gnus-dead-summary))
5919     (save-excursion
5920       (set-buffer gnus-dead-summary)
5921       (when gnus-dead-summary-mode
5922         (kill-buffer (current-buffer)))))
5923   ;; Make this the current dead summary.
5924   (setq gnus-dead-summary (current-buffer))
5925   (gnus-dead-summary-mode 1)
5926   (let ((name (buffer-name)))
5927     (when (string-match "Summary" name)
5928       (rename-buffer
5929        (concat (substring name 0 (match-beginning 0)) "Dead "
5930                (substring name (match-beginning 0)))
5931        t)
5932       (bury-buffer))))
5933
5934 (defun gnus-kill-or-deaden-summary (buffer)
5935   "Kill or deaden the summary BUFFER."
5936   (save-excursion
5937     (when (and (buffer-name buffer)
5938                (not gnus-single-article-buffer))
5939       (save-excursion
5940         (set-buffer buffer)
5941         (gnus-kill-buffer gnus-article-buffer)
5942         (gnus-kill-buffer gnus-original-article-buffer)))
5943     (cond (gnus-kill-summary-on-exit
5944            (when (and gnus-use-trees
5945                       (gnus-buffer-exists-p buffer))
5946              (save-excursion
5947                (set-buffer buffer)
5948                (gnus-tree-close gnus-newsgroup-name)))
5949            (gnus-kill-buffer buffer))
5950           ((gnus-buffer-exists-p buffer)
5951            (save-excursion
5952              (set-buffer buffer)
5953              (gnus-deaden-summary))))))
5954
5955 (defun gnus-summary-wake-up-the-dead (&rest args)
5956   "Wake up the dead summary buffer."
5957   (interactive)
5958   (gnus-dead-summary-mode -1)
5959   (let ((name (buffer-name)))
5960     (when (string-match "Dead " name)
5961       (rename-buffer
5962        (concat (substring name 0 (match-beginning 0))
5963                (substring name (match-end 0)))
5964        t)))
5965   (gnus-message 3 "This dead summary is now alive again"))
5966
5967 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5968 (defun gnus-summary-fetch-faq (&optional faq-dir)
5969   "Fetch the FAQ for the current group.
5970 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5971 in."
5972   (interactive
5973    (list
5974     (when current-prefix-arg
5975       (completing-read
5976        "Faq dir: " (and (listp gnus-group-faq-directory)
5977                         (mapcar (lambda (file) (list file))
5978                                 gnus-group-faq-directory))))))
5979   (let (gnus-faq-buffer)
5980     (when (setq gnus-faq-buffer
5981                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5982       (gnus-configure-windows 'summary-faq))))
5983
5984 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5985 (defun gnus-summary-describe-group (&optional force)
5986   "Describe the current newsgroup."
5987   (interactive "P")
5988   (gnus-group-describe-group force gnus-newsgroup-name))
5989
5990 (defun gnus-summary-describe-briefly ()
5991   "Describe summary mode commands briefly."
5992   (interactive)
5993   (gnus-message 6 (substitute-command-keys "\\<gnus-summary-mode-map>\\[gnus-summary-next-page]:Select  \\[gnus-summary-next-unread-article]:Forward  \\[gnus-summary-prev-unread-article]:Backward  \\[gnus-summary-exit]:Exit  \\[gnus-info-find-node]:Run Info        \\[gnus-summary-describe-briefly]:This help")))
5994
5995 ;; Walking around group mode buffer from summary mode.
5996
5997 (defun gnus-summary-next-group (&optional no-article target-group backward)
5998   "Exit current newsgroup and then select next unread newsgroup.
5999 If prefix argument NO-ARTICLE is non-nil, no article is selected
6000 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6001 previous group instead."
6002   (interactive "P")
6003   ;; Stop pre-fetching.
6004   (gnus-async-halt-prefetch)
6005   (let ((current-group gnus-newsgroup-name)
6006         (current-buffer (current-buffer))
6007         entered)
6008     ;; First we semi-exit this group to update Xrefs and all variables.
6009     ;; We can't do a real exit, because the window conf must remain
6010     ;; the same in case the user is prompted for info, and we don't
6011     ;; want the window conf to change before that...
6012     (gnus-summary-exit t)
6013     (while (not entered)
6014       ;; Then we find what group we are supposed to enter.
6015       (set-buffer gnus-group-buffer)
6016       (gnus-group-jump-to-group current-group)
6017       (setq target-group
6018             (or target-group
6019                 (if (eq gnus-keep-same-level 'best)
6020                     (gnus-summary-best-group gnus-newsgroup-name)
6021                   (gnus-summary-search-group backward gnus-keep-same-level))))
6022       (if (not target-group)
6023           ;; There are no further groups, so we return to the group
6024           ;; buffer.
6025           (progn
6026             (gnus-message 5 "Returning to the group buffer")
6027             (setq entered t)
6028             (when (gnus-buffer-live-p current-buffer)
6029               (set-buffer current-buffer)
6030               (gnus-summary-exit))
6031             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6032         ;; We try to enter the target group.
6033         (gnus-group-jump-to-group target-group)
6034         (let ((unreads (gnus-group-group-unread)))
6035           (if (and (or (eq t unreads)
6036                        (and unreads (not (zerop unreads))))
6037                    (gnus-summary-read-group
6038                     target-group nil no-article
6039                     (and (buffer-name current-buffer) current-buffer)
6040                     nil backward))
6041               (setq entered t)
6042             (setq current-group target-group
6043                   target-group nil)))))))
6044
6045 (defun gnus-summary-prev-group (&optional no-article)
6046   "Exit current newsgroup and then select previous unread newsgroup.
6047 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6048   (interactive "P")
6049   (gnus-summary-next-group no-article nil t))
6050
6051 ;; Walking around summary lines.
6052
6053 (defun gnus-summary-first-subject (&optional unread undownloaded)
6054   "Go to the first unread subject.
6055 If UNREAD is non-nil, go to the first unread article.
6056 Returns the article selected or nil if there are no unread articles."
6057   (interactive "P")
6058   (prog1
6059       (cond
6060        ;; Empty summary.
6061        ((null gnus-newsgroup-data)
6062         (gnus-message 3 "No articles in the group")
6063         nil)
6064        ;; Pick the first article.
6065        ((not unread)
6066         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6067         (gnus-data-number (car gnus-newsgroup-data)))
6068        ;; No unread articles.
6069        ((null gnus-newsgroup-unreads)
6070         (gnus-message 3 "No more unread articles")
6071         nil)
6072        ;; Find the first unread article.
6073        (t
6074         (let ((data gnus-newsgroup-data))
6075           (while (and data
6076                       (and (not (and undownloaded
6077                                      (eq gnus-undownloaded-mark
6078                                          (gnus-data-mark (car data)))))
6079                            (not (gnus-data-unread-p (car data)))))
6080             (setq data (cdr data)))
6081           (when data
6082             (goto-char (gnus-data-pos (car data)))
6083             (gnus-data-number (car data))))))
6084     (gnus-summary-position-point)))
6085
6086 (defun gnus-summary-next-subject (n &optional unread dont-display)
6087   "Go to next N'th summary line.
6088 If N is negative, go to the previous N'th subject line.
6089 If UNREAD is non-nil, only unread articles are selected.
6090 The difference between N and the actual number of steps taken is
6091 returned."
6092   (interactive "p")
6093   (let ((backward (< n 0))
6094         (n (abs n)))
6095     (while (and (> n 0)
6096                 (if backward
6097                     (gnus-summary-find-prev unread)
6098                   (gnus-summary-find-next unread)))
6099       (unless (zerop (setq n (1- n)))
6100         (gnus-summary-show-thread)))
6101     (when (/= 0 n)
6102       (gnus-message 7 "No more%s articles"
6103                     (if unread " unread" "")))
6104     (unless dont-display
6105       (gnus-summary-recenter)
6106       (gnus-summary-position-point))
6107     n))
6108
6109 (defun gnus-summary-next-unread-subject (n)
6110   "Go to next N'th unread summary line."
6111   (interactive "p")
6112   (gnus-summary-next-subject n t))
6113
6114 (defun gnus-summary-prev-subject (n &optional unread)
6115   "Go to previous N'th summary line.
6116 If optional argument UNREAD is non-nil, only unread article is selected."
6117   (interactive "p")
6118   (gnus-summary-next-subject (- n) unread))
6119
6120 (defun gnus-summary-prev-unread-subject (n)
6121   "Go to previous N'th unread summary line."
6122   (interactive "p")
6123   (gnus-summary-next-subject (- n) t))
6124
6125 (defun gnus-summary-goto-subject (article &optional force silent)
6126   "Go the subject line of ARTICLE.
6127 If FORCE, also allow jumping to articles not currently shown."
6128   (interactive "nArticle number: ")
6129   (let ((b (point))
6130         (data (gnus-data-find article)))
6131     ;; We read in the article if we have to.
6132     (and (not data)
6133          force
6134          (gnus-summary-insert-subject
6135           article
6136           (if (or (numberp force) (vectorp force)) force)
6137           t)
6138          (setq data (gnus-data-find article)))
6139     (goto-char b)
6140     (if (not data)
6141         (progn
6142           (unless silent
6143             (gnus-message 3 "Can't find article %d" article))
6144           nil)
6145       (goto-char (gnus-data-pos data))
6146       (gnus-summary-position-point)
6147       article)))
6148
6149 ;; Walking around summary lines with displaying articles.
6150
6151 (defun gnus-summary-expand-window (&optional arg)
6152   "Make the summary buffer take up the entire Emacs frame.
6153 Given a prefix, will force an `article' buffer configuration."
6154   (interactive "P")
6155   (if arg
6156       (gnus-configure-windows 'article 'force)
6157     (gnus-configure-windows 'summary 'force)))
6158
6159 (defun gnus-summary-display-article (article &optional all-header)
6160   "Display ARTICLE in article buffer."
6161   (when (gnus-buffer-live-p gnus-article-buffer)
6162     (with-current-buffer gnus-article-buffer
6163       (set-buffer-multibyte t)))
6164   (gnus-set-global-variables)
6165   (when (gnus-buffer-live-p gnus-article-buffer)
6166     (with-current-buffer gnus-article-buffer
6167       (setq gnus-article-charset gnus-newsgroup-charset)
6168       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)))
6169   (if (null article)
6170       nil
6171     (prog1
6172         (if gnus-summary-display-article-function
6173             (funcall gnus-summary-display-article-function article all-header)
6174           (gnus-article-prepare article all-header))
6175       (with-current-buffer gnus-article-buffer
6176         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6177              nil))
6178       (gnus-run-hooks 'gnus-select-article-hook)
6179       (when (and gnus-current-article
6180                  (not (zerop gnus-current-article)))
6181         (gnus-summary-goto-subject gnus-current-article))
6182       (gnus-summary-recenter)
6183       (when (and gnus-use-trees gnus-show-threads)
6184         (gnus-possibly-generate-tree article)
6185         (gnus-highlight-selected-tree article))
6186       ;; Successfully display article.
6187       (gnus-article-set-window-start
6188        (cdr (assq article gnus-newsgroup-bookmarks))))))
6189
6190 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6191   "Select the current article.
6192 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6193 non-nil, the article will be re-fetched even if it already present in
6194 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6195 be displayed."
6196   ;; Make sure we are in the summary buffer to work around bbdb bug.
6197   (unless (eq major-mode 'gnus-summary-mode)
6198     (set-buffer gnus-summary-buffer))
6199   (let ((article (or article (gnus-summary-article-number)))
6200         (all-headers (not (not all-headers))) ;Must be T or NIL.
6201         gnus-summary-display-article-function)
6202     (and (not pseudo)
6203          (gnus-summary-article-pseudo-p article)
6204          (error "This is a pseudo-article"))
6205     (save-excursion
6206       (set-buffer gnus-summary-buffer)
6207       (if (or (and gnus-single-article-buffer
6208                    (or (null gnus-current-article)
6209                        (null gnus-article-current)
6210                        (null (get-buffer gnus-article-buffer))
6211                        (not (eq article (cdr gnus-article-current)))
6212                        (not (equal (car gnus-article-current)
6213                                    gnus-newsgroup-name))))
6214               (and (not gnus-single-article-buffer)
6215                    (or (null gnus-current-article)
6216                        (not (eq gnus-current-article article))))
6217               force)
6218           ;; The requested article is different from the current article.
6219           (progn
6220             (gnus-summary-display-article article all-headers)
6221             (when (or all-headers gnus-show-all-headers)
6222               (gnus-article-show-all-headers))
6223             (gnus-article-set-window-start
6224              (cdr (assq article gnus-newsgroup-bookmarks)))
6225             article)
6226         (when (or all-headers gnus-show-all-headers)
6227           (gnus-article-show-all-headers))
6228         'old))))
6229
6230 (defun gnus-summary-force-verify-and-decrypt ()
6231   (interactive)
6232   (let ((mm-verify-option 'known)
6233         (mm-decrypt-option 'known))
6234     (gnus-summary-select-article nil 'force)))
6235
6236 (defun gnus-summary-set-current-mark (&optional current-mark)
6237   "Obsolete function."
6238   nil)
6239
6240 (defun gnus-summary-next-article (&optional unread subject backward push)
6241   "Select the next article.
6242 If UNREAD, only unread articles are selected.
6243 If SUBJECT, only articles with SUBJECT are selected.
6244 If BACKWARD, the previous article is selected instead of the next."
6245   (interactive "P")
6246   (cond
6247    ;; Is there such an article?
6248    ((and (gnus-summary-search-forward unread subject backward)
6249          (or (gnus-summary-display-article (gnus-summary-article-number))
6250              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6251     (gnus-summary-position-point))
6252    ;; If not, we try the first unread, if that is wanted.
6253    ((and subject
6254          gnus-auto-select-same
6255          (gnus-summary-first-unread-article))
6256     (gnus-summary-position-point)
6257     (gnus-message 6 "Wrapped"))
6258    ;; Try to get next/previous article not displayed in this group.
6259    ((and gnus-auto-extend-newsgroup
6260          (not unread) (not subject))
6261     (gnus-summary-goto-article
6262      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6263      nil (count-lines (point-min) (point))))
6264    ;; Go to next/previous group.
6265    (t
6266     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6267       (gnus-summary-jump-to-group gnus-newsgroup-name))
6268     (let ((cmd last-command-char)
6269           (point
6270            (save-excursion
6271              (set-buffer gnus-group-buffer)
6272              (point)))
6273           (group
6274            (if (eq gnus-keep-same-level 'best)
6275                (gnus-summary-best-group gnus-newsgroup-name)
6276              (gnus-summary-search-group backward gnus-keep-same-level))))
6277       ;; For some reason, the group window gets selected.  We change
6278       ;; it back.
6279       (select-window (get-buffer-window (current-buffer)))
6280       ;; Select next unread newsgroup automagically.
6281       (cond
6282        ((or (not gnus-auto-select-next)
6283             (not cmd))
6284         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6285        ((or (eq gnus-auto-select-next 'quietly)
6286             (and (eq gnus-auto-select-next 'slightly-quietly)
6287                  push)
6288             (and (eq gnus-auto-select-next 'almost-quietly)
6289                  (gnus-summary-last-article-p)))
6290         ;; Select quietly.
6291         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6292             (gnus-summary-exit)
6293           (gnus-message 7 "No more%s articles (%s)..."
6294                         (if unread " unread" "")
6295                         (if group (concat "selecting " group)
6296                           "exiting"))
6297           (gnus-summary-next-group nil group backward)))
6298        (t
6299         (when (gnus-key-press-event-p last-input-event)
6300           (gnus-summary-walk-group-buffer
6301            gnus-newsgroup-name cmd unread backward point))))))))
6302
6303 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6304   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6305                       (?\C-p (gnus-group-prev-unread-group 1))))
6306         (cursor-in-echo-area t)
6307         keve key group ended)
6308     (save-excursion
6309       (set-buffer gnus-group-buffer)
6310       (goto-char start)
6311       (setq group
6312             (if (eq gnus-keep-same-level 'best)
6313                 (gnus-summary-best-group gnus-newsgroup-name)
6314               (gnus-summary-search-group backward gnus-keep-same-level))))
6315     (while (not ended)
6316       (gnus-message
6317        5 "No more%s articles%s" (if unread " unread" "")
6318        (if (and group
6319                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6320            (format " (Type %s for %s [%s])"
6321                    (single-key-description cmd) group
6322                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6323          (format " (Type %s to exit %s)"
6324                  (single-key-description cmd)
6325                  gnus-newsgroup-name)))
6326       ;; Confirm auto selection.
6327       (setq key (car (setq keve (gnus-read-event-char))))
6328       (setq ended t)
6329       (cond
6330        ((assq key keystrokes)
6331         (let ((obuf (current-buffer)))
6332           (switch-to-buffer gnus-group-buffer)
6333           (when group
6334             (gnus-group-jump-to-group group))
6335           (eval (cadr (assq key keystrokes)))
6336           (setq group (gnus-group-group-name))
6337           (switch-to-buffer obuf))
6338         (setq ended nil))
6339        ((equal key cmd)
6340         (if (or (not group)
6341                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6342             (gnus-summary-exit)
6343           (gnus-summary-next-group nil group backward)))
6344        (t
6345         (push (cdr keve) unread-command-events))))))
6346
6347 (defun gnus-summary-next-unread-article ()
6348   "Select unread article after current one."
6349   (interactive)
6350   (gnus-summary-next-article
6351    (or (not (eq gnus-summary-goto-unread 'never))
6352        (gnus-summary-last-article-p (gnus-summary-article-number)))
6353    (and gnus-auto-select-same
6354         (gnus-summary-article-subject))))
6355
6356 (defun gnus-summary-prev-article (&optional unread subject)
6357   "Select the article after the current one.
6358 If UNREAD is non-nil, only unread articles are selected."
6359   (interactive "P")
6360   (gnus-summary-next-article unread subject t))
6361
6362 (defun gnus-summary-prev-unread-article ()
6363   "Select unread article before current one."
6364   (interactive)
6365   (gnus-summary-prev-article
6366    (or (not (eq gnus-summary-goto-unread 'never))
6367        (gnus-summary-first-article-p (gnus-summary-article-number)))
6368    (and gnus-auto-select-same
6369         (gnus-summary-article-subject))))
6370
6371 (defun gnus-summary-next-page (&optional lines circular)
6372   "Show next page of the selected article.
6373 If at the end of the current article, select the next article.
6374 LINES says how many lines should be scrolled up.
6375
6376 If CIRCULAR is non-nil, go to the start of the article instead of
6377 selecting the next article when reaching the end of the current
6378 article."
6379   (interactive "P")
6380   (setq gnus-summary-buffer (current-buffer))
6381   (gnus-set-global-variables)
6382   (let ((article (gnus-summary-article-number))
6383         (article-window (get-buffer-window gnus-article-buffer t))
6384         endp)
6385     ;; If the buffer is empty, we have no article.
6386     (unless article
6387       (error "No article to select"))
6388     (gnus-configure-windows 'article)
6389     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6390         (if (and (eq gnus-summary-goto-unread 'never)
6391                  (not (gnus-summary-last-article-p article)))
6392             (gnus-summary-next-article)
6393           (gnus-summary-next-unread-article))
6394       (if (or (null gnus-current-article)
6395               (null gnus-article-current)
6396               (/= article (cdr gnus-article-current))
6397               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6398           ;; Selected subject is different from current article's.
6399           (gnus-summary-display-article article)
6400         (when article-window
6401           (gnus-eval-in-buffer-window gnus-article-buffer
6402             (setq endp (gnus-article-next-page lines)))
6403           (when endp
6404             (cond (circular
6405                    (gnus-summary-beginning-of-article))
6406                   (lines
6407                    (gnus-message 3 "End of message"))
6408                   ((null lines)
6409                    (if (and (eq gnus-summary-goto-unread 'never)
6410                             (not (gnus-summary-last-article-p article)))
6411                        (gnus-summary-next-article)
6412                      (gnus-summary-next-unread-article))))))))
6413     (gnus-summary-recenter)
6414     (gnus-summary-position-point)))
6415
6416 (defun gnus-summary-prev-page (&optional lines move)
6417   "Show previous page of selected article.
6418 Argument LINES specifies lines to be scrolled down.
6419 If MOVE, move to the previous unread article if point is at
6420 the beginning of the buffer."
6421   (interactive "P")
6422   (let ((article (gnus-summary-article-number))
6423         (article-window (get-buffer-window gnus-article-buffer t))
6424         endp)
6425     (gnus-configure-windows 'article)
6426     (if (or (null gnus-current-article)
6427             (null gnus-article-current)
6428             (/= article (cdr gnus-article-current))
6429             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6430         ;; Selected subject is different from current article's.
6431         (gnus-summary-display-article article)
6432       (gnus-summary-recenter)
6433       (when article-window
6434         (gnus-eval-in-buffer-window gnus-article-buffer
6435           (setq endp (gnus-article-prev-page lines)))
6436         (when (and move endp)
6437           (cond (lines
6438                  (gnus-message 3 "Beginning of message"))
6439                 ((null lines)
6440                  (if (and (eq gnus-summary-goto-unread 'never)
6441                           (not (gnus-summary-first-article-p article)))
6442                      (gnus-summary-prev-article)
6443                    (gnus-summary-prev-unread-article))))))))
6444   (gnus-summary-position-point))
6445
6446 (defun gnus-summary-prev-page-or-article (&optional lines)
6447   "Show previous page of selected article.
6448 Argument LINES specifies lines to be scrolled down.
6449 If at the beginning of the article, go to the next article."
6450   (interactive "P")
6451   (gnus-summary-prev-page lines t))
6452
6453 (defun gnus-summary-scroll-up (lines)
6454   "Scroll up (or down) one line current article.
6455 Argument LINES specifies lines to be scrolled up (or down if negative)."
6456   (interactive "p")
6457   (gnus-configure-windows 'article)
6458   (gnus-summary-show-thread)
6459   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6460     (gnus-eval-in-buffer-window gnus-article-buffer
6461       (cond ((> lines 0)
6462              (when (gnus-article-next-page lines)
6463                (gnus-message 3 "End of message")))
6464             ((< lines 0)
6465              (gnus-article-prev-page (- lines))))))
6466   (gnus-summary-recenter)
6467   (gnus-summary-position-point))
6468
6469 (defun gnus-summary-scroll-down (lines)
6470   "Scroll down (or up) one line current article.
6471 Argument LINES specifies lines to be scrolled down (or up if negative)."
6472   (interactive "p")
6473   (gnus-summary-scroll-up (- lines)))
6474
6475 (defun gnus-summary-next-same-subject ()
6476   "Select next article which has the same subject as current one."
6477   (interactive)
6478   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6479
6480 (defun gnus-summary-prev-same-subject ()
6481   "Select previous article which has the same subject as current one."
6482   (interactive)
6483   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6484
6485 (defun gnus-summary-next-unread-same-subject ()
6486   "Select next unread article which has the same subject as current one."
6487   (interactive)
6488   (gnus-summary-next-article t (gnus-summary-article-subject)))
6489
6490 (defun gnus-summary-prev-unread-same-subject ()
6491   "Select previous unread article which has the same subject as current one."
6492   (interactive)
6493   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6494
6495 (defun gnus-summary-first-unread-article ()
6496   "Select the first unread article.
6497 Return nil if there are no unread articles."
6498   (interactive)
6499   (prog1
6500       (when (gnus-summary-first-subject t)
6501         (gnus-summary-show-thread)
6502         (gnus-summary-first-subject t)
6503         (gnus-summary-display-article (gnus-summary-article-number)))
6504     (gnus-summary-position-point)))
6505
6506 (defun gnus-summary-first-unread-subject ()
6507   "Place the point on the subject line of the first unread article.
6508 Return nil if there are no unread articles."
6509   (interactive)
6510   (prog1
6511       (when (gnus-summary-first-subject t)
6512         (gnus-summary-show-thread)
6513         (gnus-summary-first-subject t))
6514     (gnus-summary-position-point)))
6515
6516 (defun gnus-summary-first-article ()
6517   "Select the first article.
6518 Return nil if there are no articles."
6519   (interactive)
6520   (prog1
6521       (when (gnus-summary-first-subject)
6522         (gnus-summary-show-thread)
6523         (gnus-summary-first-subject)
6524         (gnus-summary-display-article (gnus-summary-article-number)))
6525     (gnus-summary-position-point)))
6526
6527 (defun gnus-summary-best-unread-article ()
6528   "Select the unread article with the highest score."
6529   (interactive)
6530   (let ((best -1000000)
6531         (data gnus-newsgroup-data)
6532         article score)
6533     (while data
6534       (and (gnus-data-unread-p (car data))
6535            (> (setq score
6536                     (gnus-summary-article-score (gnus-data-number (car data))))
6537               best)
6538            (setq best score
6539                  article (gnus-data-number (car data))))
6540       (setq data (cdr data)))
6541     (prog1
6542         (if article
6543             (gnus-summary-goto-article article)
6544           (error "No unread articles"))
6545       (gnus-summary-position-point))))
6546
6547 (defun gnus-summary-last-subject ()
6548   "Go to the last displayed subject line in the group."
6549   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6550     (when article
6551       (gnus-summary-goto-subject article))))
6552
6553 (defun gnus-summary-goto-article (article &optional all-headers force)
6554   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6555 If ALL-HEADERS is non-nil, no header lines are hidden.
6556 If FORCE, go to the article even if it isn't displayed.  If FORCE
6557 is a number, it is the line the article is to be displayed on."
6558   (interactive
6559    (list
6560     (completing-read
6561      "Article number or Message-ID: "
6562      (mapcar (lambda (number) (list (int-to-string number)))
6563              gnus-newsgroup-limit))
6564     current-prefix-arg
6565     t))
6566   (prog1
6567       (if (and (stringp article)
6568                (string-match "@" article))
6569           (gnus-summary-refer-article article)
6570         (when (stringp article)
6571           (setq article (string-to-number article)))
6572         (if (gnus-summary-goto-subject article force)
6573             (gnus-summary-display-article article all-headers)
6574           (gnus-message 4 "Couldn't go to article %s" article) nil))
6575     (gnus-summary-position-point)))
6576
6577 (defun gnus-summary-goto-last-article ()
6578   "Go to the previously read article."
6579   (interactive)
6580   (prog1
6581       (when gnus-last-article
6582         (gnus-summary-goto-article gnus-last-article nil t))
6583     (gnus-summary-position-point)))
6584
6585 (defun gnus-summary-pop-article (number)
6586   "Pop one article off the history and go to the previous.
6587 NUMBER articles will be popped off."
6588   (interactive "p")
6589   (let (to)
6590     (setq gnus-newsgroup-history
6591           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6592     (if to
6593         (gnus-summary-goto-article (car to) nil t)
6594       (error "Article history empty")))
6595   (gnus-summary-position-point))
6596
6597 ;; Summary commands and functions for limiting the summary buffer.
6598
6599 (defun gnus-summary-limit-to-articles (n)
6600   "Limit the summary buffer to the next N articles.
6601 If not given a prefix, use the process marked articles instead."
6602   (interactive "P")
6603   (prog1
6604       (let ((articles (gnus-summary-work-articles n)))
6605         (setq gnus-newsgroup-processable nil)
6606         (gnus-summary-limit articles))
6607     (gnus-summary-position-point)))
6608
6609 (defun gnus-summary-pop-limit (&optional total)
6610   "Restore the previous limit.
6611 If given a prefix, remove all limits."
6612   (interactive "P")
6613   (when total
6614     (setq gnus-newsgroup-limits
6615           (list (mapcar (lambda (h) (mail-header-number h))
6616                         gnus-newsgroup-headers))))
6617   (unless gnus-newsgroup-limits
6618     (error "No limit to pop"))
6619   (prog1
6620       (gnus-summary-limit nil 'pop)
6621     (gnus-summary-position-point)))
6622
6623 (defun gnus-summary-limit-to-subject (subject &optional header)
6624   "Limit the summary buffer to articles that have subjects that match a regexp."
6625   (interactive "sLimit to subject (regexp): ")
6626   (unless header
6627     (setq header "subject"))
6628   (when (not (equal "" subject))
6629     (prog1
6630         (let ((articles (gnus-summary-find-matching
6631                          (or header "subject") subject 'all)))
6632           (unless articles
6633             (error "Found no matches for \"%s\"" subject))
6634           (gnus-summary-limit articles))
6635       (gnus-summary-position-point))))
6636
6637 (defun gnus-summary-limit-to-author (from)
6638   "Limit the summary buffer to articles that have authors that match a regexp."
6639   (interactive "sLimit to author (regexp): ")
6640   (gnus-summary-limit-to-subject from "from"))
6641
6642 (defun gnus-summary-limit-to-age (age &optional younger-p)
6643   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6644 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6645 articles that are younger than AGE days."
6646   (interactive
6647    (let ((younger current-prefix-arg)
6648          (days-got nil)
6649          days)
6650      (while (not days-got)
6651        (setq days (if younger
6652                       (read-string "Limit to articles within (in days): ")
6653                     (read-string "Limit to articles older than (in days): ")))
6654        (when (> (length days) 0)
6655          (setq days (read days)))
6656        (if (numberp days)
6657            (setq days-got t)
6658          (message "Please enter a number.")
6659          (sleep-for 1)))
6660      (list days younger)))
6661   (prog1
6662       (let ((data gnus-newsgroup-data)
6663             (cutoff (days-to-time age))
6664             articles d date is-younger)
6665         (while (setq d (pop data))
6666           (when (and (vectorp (gnus-data-header d))
6667                      (setq date (mail-header-date (gnus-data-header d))))
6668             (setq is-younger (time-less-p
6669                               (time-since (condition-case ()
6670                                               (date-to-time date)
6671                                             (error '(0 0))))
6672                               cutoff))
6673             (when (if younger-p
6674                       is-younger
6675                     (not is-younger))
6676               (push (gnus-data-number d) articles))))
6677         (gnus-summary-limit (nreverse articles)))
6678     (gnus-summary-position-point)))
6679
6680 (defun gnus-summary-limit-to-extra (header regexp)
6681   "Limit the summary buffer to articles that match an 'extra' header."
6682   (interactive
6683    (let ((header
6684           (intern
6685            (gnus-completing-read
6686             (symbol-name (car gnus-extra-headers))
6687             "Limit extra header:"
6688             (mapcar (lambda (x)
6689                       (cons (symbol-name x) x))
6690                     gnus-extra-headers)
6691             nil
6692             t))))
6693      (list header
6694            (read-string (format "Limit to header %s (regexp): " header)))))
6695   (when (not (equal "" regexp))
6696     (prog1
6697         (let ((articles (gnus-summary-find-matching
6698                          (cons 'extra header) regexp 'all)))
6699           (unless articles
6700             (error "Found no matches for \"%s\"" regexp))
6701           (gnus-summary-limit articles))
6702       (gnus-summary-position-point))))
6703
6704 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6705 (make-obsolete
6706  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6707
6708 (defun gnus-summary-limit-to-unread (&optional all)
6709   "Limit the summary buffer to articles that are not marked as read.
6710 If ALL is non-nil, limit strictly to unread articles."
6711   (interactive "P")
6712   (if all
6713       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6714     (gnus-summary-limit-to-marks
6715      ;; Concat all the marks that say that an article is read and have
6716      ;; those removed.
6717      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6718            gnus-killed-mark gnus-kill-file-mark
6719            gnus-low-score-mark gnus-expirable-mark
6720            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6721            gnus-duplicate-mark gnus-souped-mark)
6722      'reverse)))
6723
6724 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6725 (make-obsolete 'gnus-summary-delete-marked-with
6726                'gnus-summary-limit-exlude-marks)
6727
6728 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6729   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6730 If REVERSE, limit the summary buffer to articles that are marked
6731 with MARKS.  MARKS can either be a string of marks or a list of marks.
6732 Returns how many articles were removed."
6733   (interactive "sMarks: ")
6734   (gnus-summary-limit-to-marks marks t))
6735
6736 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6737   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6738 If REVERSE (the prefix), limit the summary buffer to articles that are
6739 not marked with MARKS.  MARKS can either be a string of marks or a
6740 list of marks.
6741 Returns how many articles were removed."
6742   (interactive "sMarks: \nP")
6743   (prog1
6744       (let ((data gnus-newsgroup-data)
6745             (marks (if (listp marks) marks
6746                      (append marks nil))) ; Transform to list.
6747             articles)
6748         (while data
6749           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6750                   (memq (gnus-data-mark (car data)) marks))
6751             (push (gnus-data-number (car data)) articles))
6752           (setq data (cdr data)))
6753         (gnus-summary-limit articles))
6754     (gnus-summary-position-point)))
6755
6756 (defun gnus-summary-limit-to-score (&optional score)
6757   "Limit to articles with score at or above SCORE."
6758   (interactive "P")
6759   (setq score (if score
6760                   (prefix-numeric-value score)
6761                 (or gnus-summary-default-score 0)))
6762   (let ((data gnus-newsgroup-data)
6763         articles)
6764     (while data
6765       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6766                 score)
6767         (push (gnus-data-number (car data)) articles))
6768       (setq data (cdr data)))
6769     (prog1
6770         (gnus-summary-limit articles)
6771       (gnus-summary-position-point))))
6772
6773 (defun gnus-summary-limit-include-thread (id)
6774   "Display all the hidden articles that is in the thread with ID in it.
6775 When called interactively, ID is the Message-ID of the current
6776 article."
6777   (interactive (list (mail-header-id (gnus-summary-article-header))))
6778   (let ((articles (gnus-articles-in-thread
6779                    (gnus-id-to-thread (gnus-root-id id)))))
6780     (prog1
6781         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6782         (gnus-summary-limit-include-matching-articles
6783          "subject"
6784          (regexp-quote (gnus-simplify-subject-re
6785                         (mail-header-subject (gnus-id-to-header id)))))
6786       (gnus-summary-position-point))))
6787
6788 (defun gnus-summary-limit-include-matching-articles (header regexp)
6789   "Display all the hidden articles that have HEADERs that match REGEXP."
6790   (interactive (list (read-string "Match on header: ")
6791                      (read-string "Regexp: ")))
6792   (let ((articles (gnus-find-matching-articles header regexp)))
6793     (prog1
6794         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6795       (gnus-summary-position-point))))
6796
6797 (defun gnus-summary-limit-include-dormant ()
6798   "Display all the hidden articles that are marked as dormant.
6799 Note that this command only works on a subset of the articles currently
6800 fetched for this group."
6801   (interactive)
6802   (unless gnus-newsgroup-dormant
6803     (error "There are no dormant articles in this group"))
6804   (prog1
6805       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6806     (gnus-summary-position-point)))
6807
6808 (defun gnus-summary-limit-exclude-dormant ()
6809   "Hide all dormant articles."
6810   (interactive)
6811   (prog1
6812       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6813     (gnus-summary-position-point)))
6814
6815 (defun gnus-summary-limit-exclude-childless-dormant ()
6816   "Hide all dormant articles that have no children."
6817   (interactive)
6818   (let ((data (gnus-data-list t))
6819         articles d children)
6820     ;; Find all articles that are either not dormant or have
6821     ;; children.
6822     (while (setq d (pop data))
6823       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6824                 (and (setq children
6825                            (gnus-article-children (gnus-data-number d)))
6826                      (let (found)
6827                        (while children
6828                          (when (memq (car children) articles)
6829                            (setq children nil
6830                                  found t))
6831                          (pop children))
6832                        found)))
6833         (push (gnus-data-number d) articles)))
6834     ;; Do the limiting.
6835     (prog1
6836         (gnus-summary-limit articles)
6837       (gnus-summary-position-point))))
6838
6839 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6840   "Mark all unread excluded articles as read.
6841 If ALL, mark even excluded ticked and dormants as read."
6842   (interactive "P")
6843   (let ((articles (gnus-sorted-complement
6844                    (sort
6845                     (mapcar (lambda (h) (mail-header-number h))
6846                             gnus-newsgroup-headers)
6847                     '<)
6848                    (sort gnus-newsgroup-limit '<)))
6849         article)
6850     (setq gnus-newsgroup-unreads
6851           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6852     (if all
6853         (setq gnus-newsgroup-dormant nil
6854               gnus-newsgroup-marked nil
6855               gnus-newsgroup-reads
6856               (nconc
6857                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6858                gnus-newsgroup-reads))
6859       (while (setq article (pop articles))
6860         (unless (or (memq article gnus-newsgroup-dormant)
6861                     (memq article gnus-newsgroup-marked))
6862           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6863
6864 (defun gnus-summary-limit (articles &optional pop)
6865   (if pop
6866       ;; We pop the previous limit off the stack and use that.
6867       (setq articles (car gnus-newsgroup-limits)
6868             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6869     ;; We use the new limit, so we push the old limit on the stack.
6870     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6871   ;; Set the limit.
6872   (setq gnus-newsgroup-limit articles)
6873   (let ((total (length gnus-newsgroup-data))
6874         (data (gnus-data-find-list (gnus-summary-article-number)))
6875         (gnus-summary-mark-below nil)   ; Inhibit this.
6876         found)
6877     ;; This will do all the work of generating the new summary buffer
6878     ;; according to the new limit.
6879     (gnus-summary-prepare)
6880     ;; Hide any threads, possibly.
6881     (and gnus-show-threads
6882          gnus-thread-hide-subtree
6883          (gnus-summary-hide-all-threads))
6884     ;; Try to return to the article you were at, or one in the
6885     ;; neighborhood.
6886     (when data
6887       ;; We try to find some article after the current one.
6888       (while data
6889         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6890           (setq data nil
6891                 found t))
6892         (setq data (cdr data))))
6893     (unless found
6894       ;; If there is no data, that means that we were after the last
6895       ;; article.  The same goes when we can't find any articles
6896       ;; after the current one.
6897       (goto-char (point-max))
6898       (gnus-summary-find-prev))
6899     (gnus-set-mode-line 'summary)
6900     ;; We return how many articles were removed from the summary
6901     ;; buffer as a result of the new limit.
6902     (- total (length gnus-newsgroup-data))))
6903
6904 (defsubst gnus-invisible-cut-children (threads)
6905   (let ((num 0))
6906     (while threads
6907       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6908         (incf num))
6909       (pop threads))
6910     (< num 2)))
6911
6912 (defsubst gnus-cut-thread (thread)
6913   "Go forwards in the thread until we find an article that we want to display."
6914   (when (or (eq gnus-fetch-old-headers 'some)
6915             (eq gnus-fetch-old-headers 'invisible)
6916             (numberp gnus-fetch-old-headers)
6917             (eq gnus-build-sparse-threads 'some)
6918             (eq gnus-build-sparse-threads 'more))
6919     ;; Deal with old-fetched headers and sparse threads.
6920     (while (and
6921             thread
6922             (or
6923              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6924              (gnus-summary-article-ancient-p
6925               (mail-header-number (car thread))))
6926             (if (or (<= (length (cdr thread)) 1)
6927                     (eq gnus-fetch-old-headers 'invisible))
6928                 (setq gnus-newsgroup-limit
6929                       (delq (mail-header-number (car thread))
6930                             gnus-newsgroup-limit)
6931                       thread (cadr thread))
6932               (when (gnus-invisible-cut-children (cdr thread))
6933                 (let ((th (cdr thread)))
6934                   (while th
6935                     (if (memq (mail-header-number (caar th))
6936                               gnus-newsgroup-limit)
6937                         (setq thread (car th)
6938                               th nil)
6939                       (setq th (cdr th))))))))))
6940   thread)
6941
6942 (defun gnus-cut-threads (threads)
6943   "Cut off all uninteresting articles from the beginning of threads."
6944   (when (or (eq gnus-fetch-old-headers 'some)
6945             (eq gnus-fetch-old-headers 'invisible)
6946             (numberp gnus-fetch-old-headers)
6947             (eq gnus-build-sparse-threads 'some)
6948             (eq gnus-build-sparse-threads 'more))
6949     (let ((th threads))
6950       (while th
6951         (setcar th (gnus-cut-thread (car th)))
6952         (setq th (cdr th)))))
6953   ;; Remove nixed out threads.
6954   (delq nil threads))
6955
6956 (defun gnus-summary-initial-limit (&optional show-if-empty)
6957   "Figure out what the initial limit is supposed to be on group entry.
6958 This entails weeding out unwanted dormants, low-scored articles,
6959 fetch-old-headers verbiage, and so on."
6960   ;; Most groups have nothing to remove.
6961   (if (or gnus-inhibit-limiting
6962           (and (null gnus-newsgroup-dormant)
6963                (not (eq gnus-fetch-old-headers 'some))
6964                (not (numberp gnus-fetch-old-headers))
6965                (not (eq gnus-fetch-old-headers 'invisible))
6966                (null gnus-summary-expunge-below)
6967                (not (eq gnus-build-sparse-threads 'some))
6968                (not (eq gnus-build-sparse-threads 'more))
6969                (null gnus-thread-expunge-below)
6970                (not gnus-use-nocem)))
6971       ()                                ; Do nothing.
6972     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6973     (setq gnus-newsgroup-limit nil)
6974     (mapatoms
6975      (lambda (node)
6976        (unless (car (symbol-value node))
6977          ;; These threads have no parents -- they are roots.
6978          (let ((nodes (cdr (symbol-value node)))
6979                thread)
6980            (while nodes
6981              (if (and gnus-thread-expunge-below
6982                       (< (gnus-thread-total-score (car nodes))
6983                          gnus-thread-expunge-below))
6984                  (gnus-expunge-thread (pop nodes))
6985                (setq thread (pop nodes))
6986                (gnus-summary-limit-children thread))))))
6987      gnus-newsgroup-dependencies)
6988     ;; If this limitation resulted in an empty group, we might
6989     ;; pop the previous limit and use it instead.
6990     (when (and (not gnus-newsgroup-limit)
6991                show-if-empty)
6992       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6993     gnus-newsgroup-limit))
6994
6995 (defun gnus-summary-limit-children (thread)
6996   "Return 1 if this subthread is visible and 0 if it is not."
6997   ;; First we get the number of visible children to this thread.  This
6998   ;; is done by recursing down the thread using this function, so this
6999   ;; will really go down to a leaf article first, before slowly
7000   ;; working its way up towards the root.
7001   (when thread
7002     (let ((children
7003            (if (cdr thread)
7004                (apply '+ (mapcar 'gnus-summary-limit-children
7005                                  (cdr thread)))
7006              0))
7007           (number (mail-header-number (car thread)))
7008           score)
7009       (if (and
7010            (not (memq number gnus-newsgroup-marked))
7011            (or
7012             ;; If this article is dormant and has absolutely no visible
7013             ;; children, then this article isn't visible.
7014             (and (memq number gnus-newsgroup-dormant)
7015                  (zerop children))
7016             ;; If this is "fetch-old-headered" and there is no
7017             ;; visible children, then we don't want this article.
7018             (and (or (eq gnus-fetch-old-headers 'some)
7019                      (numberp gnus-fetch-old-headers))
7020                  (gnus-summary-article-ancient-p number)
7021                  (zerop children))
7022             ;; If this is "fetch-old-headered" and `invisible', then
7023             ;; we don't want this article.
7024             (and (eq gnus-fetch-old-headers 'invisible)
7025                  (gnus-summary-article-ancient-p number))
7026             ;; If this is a sparsely inserted article with no children,
7027             ;; we don't want it.
7028             (and (eq gnus-build-sparse-threads 'some)
7029                  (gnus-summary-article-sparse-p number)
7030                  (zerop children))
7031             ;; If we use expunging, and this article is really
7032             ;; low-scored, then we don't want this article.
7033             (when (and gnus-summary-expunge-below
7034                        (< (setq score
7035                                 (or (cdr (assq number gnus-newsgroup-scored))
7036                                     gnus-summary-default-score))
7037                           gnus-summary-expunge-below))
7038               ;; We increase the expunge-tally here, but that has
7039               ;; nothing to do with the limits, really.
7040               (incf gnus-newsgroup-expunged-tally)
7041               ;; We also mark as read here, if that's wanted.
7042               (when (and gnus-summary-mark-below
7043                          (< score gnus-summary-mark-below))
7044                 (setq gnus-newsgroup-unreads
7045                       (delq number gnus-newsgroup-unreads))
7046                 (if gnus-newsgroup-auto-expire
7047                     (push number gnus-newsgroup-expirable)
7048                   (push (cons number gnus-low-score-mark)
7049                         gnus-newsgroup-reads)))
7050               t)
7051             ;; Check NoCeM things.
7052             (if (and gnus-use-nocem
7053                      (gnus-nocem-unwanted-article-p
7054                       (mail-header-id (car thread))))
7055                 (progn
7056                   (setq gnus-newsgroup-unreads
7057                         (delq number gnus-newsgroup-unreads))
7058                   t))))
7059           ;; Nope, invisible article.
7060           0
7061         ;; Ok, this article is to be visible, so we add it to the limit
7062         ;; and return 1.
7063         (push number gnus-newsgroup-limit)
7064         1))))
7065
7066 (defun gnus-expunge-thread (thread)
7067   "Mark all articles in THREAD as read."
7068   (let* ((number (mail-header-number (car thread))))
7069     (incf gnus-newsgroup-expunged-tally)
7070     ;; We also mark as read here, if that's wanted.
7071     (setq gnus-newsgroup-unreads
7072           (delq number gnus-newsgroup-unreads))
7073     (if gnus-newsgroup-auto-expire
7074         (push number gnus-newsgroup-expirable)
7075       (push (cons number gnus-low-score-mark)
7076             gnus-newsgroup-reads)))
7077   ;; Go recursively through all subthreads.
7078   (mapcar 'gnus-expunge-thread (cdr thread)))
7079
7080 ;; Summary article oriented commands
7081
7082 (defun gnus-summary-refer-parent-article (n)
7083   "Refer parent article N times.
7084 If N is negative, go to ancestor -N instead.
7085 The difference between N and the number of articles fetched is returned."
7086   (interactive "p")
7087   (let ((skip 1)
7088         error header ref)
7089     (when (not (natnump n))
7090       (setq skip (abs n)
7091             n 1))
7092     (while (and (> n 0)
7093                 (not error))
7094       (setq header (gnus-summary-article-header))
7095       (if (and (eq (mail-header-number header)
7096                    (cdr gnus-article-current))
7097                (equal gnus-newsgroup-name
7098                       (car gnus-article-current)))
7099           ;; If we try to find the parent of the currently
7100           ;; displayed article, then we take a look at the actual
7101           ;; References header, since this is slightly more
7102           ;; reliable than the References field we got from the
7103           ;; server.
7104           (save-excursion
7105             (set-buffer gnus-original-article-buffer)
7106             (nnheader-narrow-to-headers)
7107             (unless (setq ref (message-fetch-field "references"))
7108               (setq ref (message-fetch-field "in-reply-to")))
7109             (widen))
7110         (setq ref
7111               ;; It's not the current article, so we take a bet on
7112               ;; the value we got from the server.
7113               (mail-header-references header)))
7114       (if (and ref
7115                (not (equal ref "")))
7116           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7117             (gnus-message 1 "Couldn't find parent"))
7118         (gnus-message 1 "No references in article %d"
7119                       (gnus-summary-article-number))
7120         (setq error t))
7121       (decf n))
7122     (gnus-summary-position-point)
7123     n))
7124
7125 (defun gnus-summary-refer-references ()
7126   "Fetch all articles mentioned in the References header.
7127 Return the number of articles fetched."
7128   (interactive)
7129   (let ((ref (mail-header-references (gnus-summary-article-header)))
7130         (current (gnus-summary-article-number))
7131         (n 0))
7132     (if (or (not ref)
7133             (equal ref ""))
7134         (error "No References in the current article")
7135       ;; For each Message-ID in the References header...
7136       (while (string-match "<[^>]*>" ref)
7137         (incf n)
7138         ;; ... fetch that article.
7139         (gnus-summary-refer-article
7140          (prog1 (match-string 0 ref)
7141            (setq ref (substring ref (match-end 0))))))
7142       (gnus-summary-goto-subject current)
7143       (gnus-summary-position-point)
7144       n)))
7145
7146 (defun gnus-summary-refer-thread (&optional limit)
7147   "Fetch all articles in the current thread.
7148 If LIMIT (the numerical prefix), fetch that many old headers instead
7149 of what's specified by the `gnus-refer-thread-limit' variable."
7150   (interactive "P")
7151   (let ((id (mail-header-id (gnus-summary-article-header)))
7152         (limit (if limit (prefix-numeric-value limit)
7153                  gnus-refer-thread-limit)))
7154     ;; We want to fetch LIMIT *old* headers, but we also have to
7155     ;; re-fetch all the headers in the current buffer, because many of
7156     ;; them may be undisplayed.  So we adjust LIMIT.
7157     (when (numberp limit)
7158       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7159     (unless (eq gnus-fetch-old-headers 'invisible)
7160       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7161       ;; Retrieve the headers and read them in.
7162       (if (eq (gnus-retrieve-headers
7163                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7164               'nov)
7165           (gnus-build-all-threads)
7166         (error "Can't fetch thread from backends that don't support NOV"))
7167       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7168     (gnus-summary-limit-include-thread id)))
7169
7170 (defun gnus-summary-refer-article (message-id)
7171   "Fetch an article specified by MESSAGE-ID."
7172   (interactive "sMessage-ID: ")
7173   (when (and (stringp message-id)
7174              (not (zerop (length message-id))))
7175     ;; Construct the correct Message-ID if necessary.
7176     ;; Suggested by tale@pawl.rpi.edu.
7177     (unless (string-match "^<" message-id)
7178       (setq message-id (concat "<" message-id)))
7179     (unless (string-match ">$" message-id)
7180       (setq message-id (concat message-id ">")))
7181     (let* ((header (gnus-id-to-header message-id))
7182            (sparse (and header
7183                         (gnus-summary-article-sparse-p
7184                          (mail-header-number header))
7185                         (memq (mail-header-number header)
7186                               gnus-newsgroup-limit)))
7187            number)
7188       (cond
7189        ;; If the article is present in the buffer we just go to it.
7190        ((and header
7191              (or (not (gnus-summary-article-sparse-p
7192                        (mail-header-number header)))
7193                  sparse))
7194         (prog1
7195             (gnus-summary-goto-article
7196              (mail-header-number header) nil t)
7197           (when sparse
7198             (gnus-summary-update-article (mail-header-number header)))))
7199        (t
7200         ;; We fetch the article.
7201         (catch 'found
7202           (dolist (gnus-override-method (gnus-refer-article-methods))
7203             (gnus-check-server gnus-override-method)
7204             ;; Fetch the header, and display the article.
7205             (when (setq number (gnus-summary-insert-subject message-id))
7206               (gnus-summary-select-article nil nil nil number)
7207               (throw 'found t)))
7208           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7209
7210 (defun gnus-refer-article-methods ()
7211   "Return a list of referrable methods."
7212   (cond
7213    ;; No method, so we default to current and native.
7214    ((null gnus-refer-article-method)
7215     (list gnus-current-select-method gnus-select-method))
7216    ;; Current.
7217    ((eq 'current gnus-refer-article-method)
7218     (list gnus-current-select-method))
7219    ;; List of select methods.
7220    ((not (and (symbolp (car gnus-refer-article-method))
7221               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7222     (let (out)
7223       (dolist (method gnus-refer-article-method)
7224         (push (if (eq 'current method)
7225                   gnus-current-select-method
7226                 method)
7227               out))
7228       (nreverse out)))
7229    ;; One single select method.
7230    (t
7231     (list gnus-refer-article-method))))
7232
7233 (defun gnus-summary-edit-parameters ()
7234   "Edit the group parameters of the current group."
7235   (interactive)
7236   (gnus-group-edit-group gnus-newsgroup-name 'params))
7237
7238 (defun gnus-summary-customize-parameters ()
7239   "Customize the group parameters of the current group."
7240   (interactive)
7241   (gnus-group-customize gnus-newsgroup-name))
7242
7243 (defun gnus-summary-enter-digest-group (&optional force)
7244   "Enter an nndoc group based on the current article.
7245 If FORCE, force a digest interpretation.  If not, try
7246 to guess what the document format is."
7247   (interactive "P")
7248   (let ((conf gnus-current-window-configuration))
7249     (save-excursion
7250       (gnus-summary-select-article))
7251     (setq gnus-current-window-configuration conf)
7252     (let* ((name (format "%s-%d"
7253                          (gnus-group-prefixed-name
7254                           gnus-newsgroup-name (list 'nndoc ""))
7255                          (save-excursion
7256                            (set-buffer gnus-summary-buffer)
7257                            gnus-current-article)))
7258            (ogroup gnus-newsgroup-name)
7259            (params (append (gnus-info-params (gnus-get-info ogroup))
7260                            (list (cons 'to-group ogroup))
7261                            (list (cons 'save-article-group ogroup))))
7262            (case-fold-search t)
7263            (buf (current-buffer))
7264            dig to-address)
7265       (save-excursion
7266         (set-buffer gnus-original-article-buffer)
7267         ;; Have the digest group inherit the main mail address of
7268         ;; the parent article.
7269         (when (setq to-address (or (message-fetch-field "reply-to")
7270                                    (message-fetch-field "from")))
7271           (setq params (append
7272                         (list (cons 'to-address
7273                                     (funcall gnus-decode-encoded-word-function
7274                                              to-address))))))
7275         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7276         (insert-buffer-substring gnus-original-article-buffer)
7277         ;; Remove lines that may lead nndoc to misinterpret the
7278         ;; document type.
7279         (narrow-to-region
7280          (goto-char (point-min))
7281          (or (search-forward "\n\n" nil t) (point)))
7282         (goto-char (point-min))
7283         (delete-matching-lines "^Path:\\|^From ")
7284         (widen))
7285       (unwind-protect
7286           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7287                     (gnus-newsgroup-ephemeral-ignored-charsets
7288                      gnus-newsgroup-ignored-charsets))
7289                 (gnus-group-read-ephemeral-group
7290                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7291                               (nndoc-article-type
7292                                ,(if force 'mbox 'guess))) t))
7293               ;; Make all postings to this group go to the parent group.
7294               (nconc (gnus-info-params (gnus-get-info name))
7295                      params)
7296             ;; Couldn't select this doc group.
7297             (switch-to-buffer buf)
7298             (gnus-set-global-variables)
7299             (gnus-configure-windows 'summary)
7300             (gnus-message 3 "Article couldn't be entered?"))
7301         (kill-buffer dig)))))
7302
7303 (defun gnus-summary-read-document (n)
7304   "Open a new group based on the current article(s).
7305 This will allow you to read digests and other similar
7306 documents as newsgroups.
7307 Obeys the standard process/prefix convention."
7308   (interactive "P")
7309   (let* ((articles (gnus-summary-work-articles n))
7310          (ogroup gnus-newsgroup-name)
7311          (params (append (gnus-info-params (gnus-get-info ogroup))
7312                          (list (cons 'to-group ogroup))))
7313          article group egroup groups vgroup)
7314     (while (setq article (pop articles))
7315       (setq group (format "%s-%d" gnus-newsgroup-name article))
7316       (gnus-summary-remove-process-mark article)
7317       (when (gnus-summary-display-article article)
7318         (save-excursion
7319           (with-temp-buffer
7320             (insert-buffer-substring gnus-original-article-buffer)
7321             ;; Remove some headers that may lead nndoc to make
7322             ;; the wrong guess.
7323             (message-narrow-to-head)
7324             (goto-char (point-min))
7325             (delete-matching-lines "^\\(Path\\):\\|^From ")
7326             (widen)
7327             (if (setq egroup
7328                       (gnus-group-read-ephemeral-group
7329                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7330                                      (nndoc-article-type guess))
7331                        t nil t))
7332                 (progn
7333                   ;; Make all postings to this group go to the parent group.
7334                   (nconc (gnus-info-params (gnus-get-info egroup))
7335                          params)
7336                   (push egroup groups))
7337               ;; Couldn't select this doc group.
7338               (gnus-error 3 "Article couldn't be entered"))))))
7339     ;; Now we have selected all the documents.
7340     (cond
7341      ((not groups)
7342       (error "None of the articles could be interpreted as documents"))
7343      ((gnus-group-read-ephemeral-group
7344        (setq vgroup (format
7345                      "nnvirtual:%s-%s" gnus-newsgroup-name
7346                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7347        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7348        t
7349        (cons (current-buffer) 'summary)))
7350      (t
7351       (error "Couldn't select virtual nndoc group")))))
7352
7353 (defun gnus-summary-isearch-article (&optional regexp-p)
7354   "Do incremental search forward on the current article.
7355 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7356   (interactive "P")
7357   (let* ((gnus-inhibit-treatment t)
7358          (old (gnus-summary-select-article)))
7359     (gnus-configure-windows 'article)
7360     (gnus-eval-in-buffer-window gnus-article-buffer
7361       (save-restriction
7362         (widen)
7363         (when (eq 'old old)
7364           (gnus-article-show-all-headers))
7365         (goto-char (point-min))
7366         (isearch-forward regexp-p)))))
7367
7368 (defun gnus-summary-search-article-forward (regexp &optional backward)
7369   "Search for an article containing REGEXP forward.
7370 If BACKWARD, search backward instead."
7371   (interactive
7372    (list (read-string
7373           (format "Search article %s (regexp%s): "
7374                   (if current-prefix-arg "backward" "forward")
7375                   (if gnus-last-search-regexp
7376                       (concat ", default " gnus-last-search-regexp)
7377                     "")))
7378          current-prefix-arg))
7379   (if (string-equal regexp "")
7380       (setq regexp (or gnus-last-search-regexp ""))
7381     (setq gnus-last-search-regexp regexp)
7382     (setq gnus-article-before-search gnus-current-article))
7383   ;; Intentionally set gnus-last-article.
7384   (setq gnus-last-article gnus-article-before-search)
7385   (let ((gnus-last-article gnus-last-article))
7386     (if (gnus-summary-search-article regexp backward)
7387         (gnus-summary-show-thread)
7388       (error "Search failed: \"%s\"" regexp))))
7389
7390 (defun gnus-summary-search-article-backward (regexp)
7391   "Search for an article containing REGEXP backward."
7392   (interactive
7393    (list (read-string
7394           (format "Search article backward (regexp%s): "
7395                   (if gnus-last-search-regexp
7396                       (concat ", default " gnus-last-search-regexp)
7397                     "")))))
7398   (gnus-summary-search-article-forward regexp 'backward))
7399
7400 (eval-when-compile
7401   (defmacro gnus-summary-search-article-position-point (regexp backward)
7402     "Dehighlight the last matched text and goto the beginning position."
7403     (` (if (and gnus-summary-search-article-matched-data
7404                 (let ((text (caddr gnus-summary-search-article-matched-data))
7405                       (inhibit-read-only t)
7406                       buffer-read-only)
7407                   (delete-region
7408                    (goto-char (car gnus-summary-search-article-matched-data))
7409                    (cadr gnus-summary-search-article-matched-data))
7410                   (insert text)
7411                   (string-match (, regexp) text)))
7412            (if (, backward) (beginning-of-line) (end-of-line))
7413          (goto-char (if (, backward) (point-max) (point-min))))))
7414
7415   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7416     "Place point where X-Face image is displayed."
7417     (if (featurep 'xemacs)
7418         (` (let ((end (if (search-forward "\n\n" nil t)
7419                           (goto-char (1- (point)))
7420                         (point-min)))
7421                  extent)
7422              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7423              (unless (and (re-search-forward "^From:" end t)
7424                           (setq extent (extent-at (point)))
7425                           (extent-begin-glyph extent))
7426                (goto-char (, opoint)))))
7427       (` (let ((end (if (search-forward "\n\n" nil t)
7428                         (goto-char (1- (point)))
7429                       (point-min)))
7430                (start (or (search-backward "\n\n" nil t) (point-min))))
7431            (goto-char
7432             (or (text-property-any start end 'x-face-image t);; x-face-e21
7433                 (text-property-any start end 'x-face-mule-bitmap-image t)
7434                 (, opoint)))))))
7435
7436   (defmacro gnus-summary-search-article-highlight-matched-text
7437     (backward treated x-face)
7438     "Highlight matched text in the function `gnus-summary-search-article'."
7439     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7440              (end (set-marker (make-marker) (match-end 0)))
7441              (inhibit-read-only t)
7442              buffer-read-only)
7443          (unless treated
7444            (let ((,@
7445                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7446                     (mapcar
7447                      (lambda (item) (setq items (delq item items)))
7448                      '(gnus-treat-buttonize
7449                        gnus-treat-fill-article
7450                        gnus-treat-fill-long-lines
7451                        gnus-treat-emphasize
7452                        gnus-treat-highlight-headers
7453                        gnus-treat-highlight-citation
7454                        gnus-treat-highlight-signature
7455                        gnus-treat-overstrike
7456                        gnus-treat-display-xface
7457                        gnus-treat-buttonize-head
7458                        gnus-treat-decode-article-as-default-mime-charset))
7459                     (static-if (featurep 'xemacs)
7460                         items
7461                       (cons '(x-face-mule-delete-x-face-field
7462                               (quote never))
7463                             items))))
7464                  (gnus-treat-display-xface
7465                   (when (, x-face) gnus-treat-display-xface)))
7466              (gnus-article-prepare-mime-display)))
7467          (goto-char (if (, backward) start end))
7468          (when (, x-face)
7469            (gnus-summary-search-article-highlight-goto-x-face (point)))
7470          (setq gnus-summary-search-article-matched-data
7471                (list start end (buffer-substring start end)))
7472          (unless (eq start end);; matched text has been deleted. :-<
7473            (put-text-property start end 'face
7474                               (or (find-face 'isearch)
7475                                   'secondary-selection))))))
7476   )
7477
7478 (defun gnus-summary-search-article (regexp &optional backward)
7479   "Search for an article containing REGEXP.
7480 Optional argument BACKWARD means do search for backward.
7481 `gnus-select-article-hook' is not called during the search."
7482   ;; We have to require this here to make sure that the following
7483   ;; dynamic binding isn't shadowed by autoloading.
7484   (require 'gnus-async)
7485   (require 'gnus-art)
7486   (let ((gnus-select-article-hook nil)  ;Disable hook.
7487         (gnus-article-prepare-hook nil)
7488         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7489         (gnus-use-article-prefetch nil)
7490         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7491         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7492         (sum (current-buffer))
7493         (found nil)
7494         point treated)
7495     (gnus-save-hidden-threads
7496       (static-if (featurep 'xemacs)
7497           (let ((gnus-inhibit-treatment t))
7498             (setq treated (eq 'old (gnus-summary-select-article)))
7499             (when (and treated
7500                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7501                                  (window-live-p (get-buffer-window
7502                                                  gnus-article-buffer t)))))
7503               (gnus-summary-select-article nil t)
7504               (setq treated nil)))
7505         (let ((gnus-inhibit-treatment t)
7506               (x-face-mule-delete-x-face-field 'never))
7507           (setq treated (eq 'old (gnus-summary-select-article)))
7508           (when (and treated
7509                      (not
7510                       (and (gnus-buffer-live-p gnus-article-buffer)
7511                            (window-live-p (get-buffer-window
7512                                            gnus-article-buffer t))
7513                            (or (not (string-match "^\\^X-Face:" regexp))
7514                                (with-current-buffer gnus-article-buffer
7515                                  gnus-summary-search-article-matched-data)))))
7516             (gnus-summary-select-article nil t)
7517             (setq treated nil))))
7518       (set-buffer gnus-article-buffer)
7519       (widen)
7520       (if treated
7521           (progn
7522             (gnus-article-show-all-headers)
7523             (gnus-summary-search-article-position-point regexp backward))
7524         (goto-char (if backward (point-max) (point-min))))
7525       (while (not found)
7526         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7527         (if (if backward
7528                 (re-search-backward regexp nil t)
7529               (re-search-forward regexp nil t))
7530             ;; We found the regexp.
7531             (progn
7532               (gnus-summary-search-article-highlight-matched-text
7533                backward treated (string-match "^\\^X-Face:" regexp))
7534               (setq found 'found)
7535               (forward-line
7536                (/ (- 2 (window-height
7537                         (get-buffer-window gnus-article-buffer t)))
7538                   2))
7539               (set-window-start
7540                (get-buffer-window (current-buffer))
7541                (point))
7542               (set-buffer sum)
7543               (setq point (point)))
7544           ;; We didn't find it, so we go to the next article.
7545           (set-buffer sum)
7546           (setq found 'not)
7547           (while (eq found 'not)
7548             (if (not (if backward (gnus-summary-find-prev)
7549                        (gnus-summary-find-next)))
7550                 ;; No more articles.
7551                 (setq found t)
7552               ;; Select the next article and adjust point.
7553               (unless (gnus-summary-article-sparse-p
7554                        (gnus-summary-article-number))
7555                 (setq found nil)
7556                 (let ((gnus-inhibit-treatment t))
7557                   (gnus-summary-select-article))
7558                 (setq treated nil)
7559                 (set-buffer gnus-article-buffer)
7560                 (widen)
7561                 (goto-char (if backward (point-max) (point-min))))))))
7562       (gnus-message 7 ""))
7563     ;; Return whether we found the regexp.
7564     (when (eq found 'found)
7565       (goto-char point)
7566       (gnus-summary-show-thread)
7567       (gnus-summary-goto-subject gnus-current-article)
7568       (gnus-summary-position-point)
7569       t)))
7570
7571 (defun gnus-find-matching-articles (header regexp)
7572   "Return a list of all articles that match REGEXP on HEADER.
7573 This search includes all articles in the current group that Gnus has
7574 fetched headers for, whether they are displayed or not."
7575   (let ((articles nil)
7576         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7577         (case-fold-search t))
7578     (dolist (header gnus-newsgroup-headers)
7579       (when (string-match regexp (funcall func header))
7580         (push (mail-header-number header) articles)))
7581     (nreverse articles)))
7582
7583 (defun gnus-summary-find-matching (header regexp &optional backward unread
7584                                           not-case-fold)
7585   "Return a list of all articles that match REGEXP on HEADER.
7586 The search stars on the current article and goes forwards unless
7587 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7588 If UNREAD is non-nil, only unread articles will
7589 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7590 in the comparisons."
7591   (let ((case-fold-search (not not-case-fold))
7592         articles d func)
7593     (if (consp header)
7594         (if (eq (car header) 'extra)
7595             (setq func
7596                   `(lambda (h)
7597                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7598                          "")))
7599           (error "%s is an invalid header" header))
7600       (unless (fboundp (intern (concat "mail-header-" header)))
7601         (error "%s is not a valid header" header))
7602       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7603     (dolist (d (if (eq backward 'all)
7604                    gnus-newsgroup-data
7605                  (gnus-data-find-list
7606                   (gnus-summary-article-number)
7607                   (gnus-data-list backward))))
7608       (when (and (or (not unread)       ; We want all articles...
7609                      (gnus-data-unread-p d)) ; Or just unreads.
7610                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7611                  (string-match regexp
7612                                (funcall func (gnus-data-header d)))) ; Match.
7613         (push (gnus-data-number d) articles))) ; Success!
7614     (nreverse articles)))
7615
7616 (defun gnus-summary-execute-command (header regexp command &optional backward)
7617   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7618 If HEADER is an empty string (or nil), the match is done on the entire
7619 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7620   (interactive
7621    (list (let ((completion-ignore-case t))
7622            (completing-read
7623             "Header name: "
7624             (mapcar (lambda (header) (list (format "%s" header)))
7625                     (append
7626                      '("Number" "Subject" "From" "Lines" "Date"
7627                        "Message-ID" "Xref" "References" "Body")
7628                      gnus-extra-headers))
7629             nil 'require-match))
7630          (read-string "Regexp: ")
7631          (read-key-sequence "Command: ")
7632          current-prefix-arg))
7633   (when (equal header "Body")
7634     (setq header ""))
7635   ;; Hidden thread subtrees must be searched as well.
7636   (gnus-summary-show-all-threads)
7637   ;; We don't want to change current point nor window configuration.
7638   (save-excursion
7639     (save-window-excursion
7640       (gnus-message 6 "Executing %s..." (key-description command))
7641       ;; We'd like to execute COMMAND interactively so as to give arguments.
7642       (gnus-execute header regexp
7643                     `(call-interactively ',(key-binding command))
7644                     backward)
7645       (gnus-message 6 "Executing %s...done" (key-description command)))))
7646
7647 (defun gnus-summary-beginning-of-article ()
7648   "Scroll the article back to the beginning."
7649   (interactive)
7650   (gnus-summary-select-article)
7651   (gnus-configure-windows 'article)
7652   (gnus-eval-in-buffer-window gnus-article-buffer
7653     (widen)
7654     (goto-char (point-min))
7655     (when gnus-page-broken
7656       (gnus-narrow-to-page))))
7657
7658 (defun gnus-summary-end-of-article ()
7659   "Scroll to the end of the article."
7660   (interactive)
7661   (gnus-summary-select-article)
7662   (gnus-configure-windows 'article)
7663   (gnus-eval-in-buffer-window gnus-article-buffer
7664     (widen)
7665     (goto-char (point-max))
7666     (recenter -3)
7667     (when gnus-page-broken
7668       (gnus-narrow-to-page))))
7669
7670 (defun gnus-summary-print-article (&optional filename n)
7671   "Generate and print a PostScript image of the N next (mail) articles.
7672
7673 If N is negative, print the N previous articles.  If N is nil and articles
7674 have been marked with the process mark, print these instead.
7675
7676 If the optional first argument FILENAME is nil, send the image to the
7677 printer.  If FILENAME is a string, save the PostScript image in a file with
7678 that name.  If FILENAME is a number, prompt the user for the name of the file
7679 to save in."
7680   (interactive (list (ps-print-preprint current-prefix-arg)))
7681   (dolist (article (gnus-summary-work-articles n))
7682     (gnus-summary-select-article nil nil 'pseudo article)
7683     (gnus-eval-in-buffer-window gnus-article-buffer
7684       (let ((buffer (generate-new-buffer " *print*")))
7685         (unwind-protect
7686             (progn
7687               (copy-to-buffer buffer (point-min) (point-max))
7688               (set-buffer buffer)
7689               (gnus-article-delete-invisible-text)
7690               (when (gnus-visual-p 'article-highlight 'highlight)
7691                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7692                 ;; highlight.
7693                 (let ((gnus-article-buffer buffer))
7694                   (gnus-article-highlight-citation t)
7695                   (gnus-article-highlight-signature)))
7696               (let ((ps-left-header
7697                      (list
7698                       (concat "("
7699                               (mail-header-subject gnus-current-headers) ")")
7700                       (concat "("
7701                               (mail-header-from gnus-current-headers) ")")))
7702                     (ps-right-header
7703                      (list
7704                       "/pagenumberstring load"
7705                       (concat "("
7706                               (mail-header-date gnus-current-headers) ")"))))
7707                 (gnus-run-hooks 'gnus-ps-print-hook)
7708                 (save-excursion
7709                   (if window-system
7710                       (ps-spool-buffer-with-faces)
7711                     (ps-spool-buffer)))))
7712           (kill-buffer buffer))))
7713     (gnus-summary-remove-process-mark article))
7714   (ps-despool filename))
7715
7716 (defun gnus-summary-show-article (&optional arg)
7717   "Force re-fetching of the current article.
7718 If ARG (the prefix) is a number, show the article with the charset
7719 defined in `gnus-summary-show-article-charset-alist', or the charset
7720 inputed.
7721 If ARG (the prefix) is non-nil and not a number, show the raw article
7722 without any article massaging functions being run."
7723   (interactive "P")
7724   (cond
7725    ((numberp arg)
7726     (let ((gnus-newsgroup-charset
7727            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7728                (read-coding-system "Charset: ")))
7729           (gnus-newsgroup-ignored-charsets 'gnus-all))
7730       (gnus-summary-select-article nil 'force)
7731       (let ((deps gnus-newsgroup-dependencies)
7732             head header)
7733         (save-excursion
7734           (set-buffer gnus-original-article-buffer)
7735           (save-restriction
7736             (message-narrow-to-head)
7737             (setq head (buffer-string)))
7738           (with-temp-buffer
7739             (insert (format "211 %d Article retrieved.\n"
7740                             (cdr gnus-article-current)))
7741             (insert head)
7742             (insert ".\n")
7743             (let ((nntp-server-buffer (current-buffer)))
7744               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7745         (gnus-data-set-header
7746          (gnus-data-find (cdr gnus-article-current))
7747          header)
7748         (gnus-summary-update-article-line
7749          (cdr gnus-article-current) header))))
7750    ((not arg)
7751     ;; Select the article the normal way.
7752     (gnus-summary-select-article nil 'force))
7753    (t
7754     ;; We have to require this here to make sure that the following
7755     ;; dynamic binding isn't shadowed by autoloading.
7756     (require 'gnus-async)
7757     (require 'gnus-art)
7758     ;; Bind the article treatment functions to nil.
7759     (let ((gnus-have-all-headers t)
7760           gnus-article-prepare-hook
7761           gnus-article-decode-hook
7762           gnus-break-pages
7763           gnus-show-mime
7764           (gnus-inhibit-treatment t))
7765       (gnus-summary-select-article nil 'force))))
7766   (gnus-summary-goto-subject gnus-current-article)
7767   (gnus-summary-position-point))
7768
7769 (defun gnus-summary-verbose-headers (&optional arg)
7770   "Toggle permanent full header display.
7771 If ARG is a positive number, turn header display on.
7772 If ARG is a negative number, turn header display off."
7773   (interactive "P")
7774   (setq gnus-show-all-headers
7775         (cond ((or (not (numberp arg))
7776                    (zerop arg))
7777                (not gnus-show-all-headers))
7778               ((natnump arg)
7779                t)))
7780   (gnus-summary-show-article))
7781
7782 (defun gnus-summary-toggle-header (&optional arg)
7783   "Show the headers if they are hidden, or hide them if they are shown.
7784 If ARG is a positive number, show the entire header.
7785 If ARG is a negative number, hide the unwanted header lines."
7786   (interactive "P")
7787   (save-excursion
7788     (set-buffer gnus-article-buffer)
7789     (save-restriction
7790       (let* ((buffer-read-only nil)
7791              (inhibit-point-motion-hooks t)
7792              hidden e)
7793         (setq hidden
7794               (if (numberp arg)
7795                   (>= arg 0)
7796                 (save-restriction
7797                   (article-narrow-to-head)
7798                   (gnus-article-hidden-text-p 'headers))))
7799         (goto-char (point-min))
7800         (when (search-forward "\n\n" nil t)
7801           (delete-region (point-min) (1- (point))))
7802         (goto-char (point-min))
7803         (save-excursion
7804           (set-buffer gnus-original-article-buffer)
7805           (goto-char (point-min))
7806           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7807         (insert-buffer-substring gnus-original-article-buffer 1 e)
7808         (save-restriction
7809           (narrow-to-region (point-min) (point))
7810           (article-decode-encoded-words)
7811           (if  hidden
7812               (let ((gnus-treat-hide-headers nil)
7813                     (gnus-treat-hide-boring-headers nil))
7814                 (setq gnus-article-wash-types
7815                       (delq 'headers gnus-article-wash-types))
7816                 (gnus-treat-article 'head))
7817             (gnus-treat-article 'head)))
7818         (gnus-set-mode-line 'article)))))
7819
7820 (defun gnus-summary-show-all-headers ()
7821   "Make all header lines visible."
7822   (interactive)
7823   (gnus-summary-toggle-header 1))
7824
7825 (defun gnus-summary-toggle-mime (&optional arg)
7826   "Toggle MIME processing.
7827 If ARG is a positive number, turn MIME processing on."
7828   (interactive "P")
7829   (setq gnus-show-mime
7830         (if (null arg)
7831             (not gnus-show-mime)
7832           (> (prefix-numeric-value arg) 0)))
7833   (gnus-summary-select-article t 'force))
7834
7835 (defun gnus-summary-caesar-message (&optional arg)
7836   "Caesar rotate the current article by 13.
7837 The numerical prefix specifies how many places to rotate each letter
7838 forward."
7839   (interactive "P")
7840   (gnus-summary-select-article)
7841   (let ((mail-header-separator ""))
7842     (gnus-eval-in-buffer-window gnus-article-buffer
7843       (save-restriction
7844         (widen)
7845         (let ((start (window-start))
7846               buffer-read-only)
7847           (message-caesar-buffer-body arg)
7848           (set-window-start (get-buffer-window (current-buffer)) start))))))
7849
7850 (defun gnus-summary-stop-page-breaking ()
7851   "Stop page breaking in the current article."
7852   (interactive)
7853   (gnus-summary-select-article)
7854   (gnus-eval-in-buffer-window gnus-article-buffer
7855     (widen)
7856     (when (gnus-visual-p 'page-marker)
7857       (let ((buffer-read-only nil))
7858         (gnus-remove-text-with-property 'gnus-prev)
7859         (gnus-remove-text-with-property 'gnus-next))
7860       (setq gnus-page-broken nil))))
7861
7862 (defun gnus-summary-move-article (&optional n to-newsgroup
7863                                             select-method action)
7864   "Move the current article to a different newsgroup.
7865 If N is a positive number, move the N next articles.
7866 If N is a negative number, move the N previous articles.
7867 If N is nil and any articles have been marked with the process mark,
7868 move those articles instead.
7869 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7870 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7871 re-spool using this method.
7872
7873 For this function to work, both the current newsgroup and the
7874 newsgroup that you want to move to have to support the `request-move'
7875 and `request-accept' functions.
7876
7877 ACTION can be either `move' (the default), `crosspost' or `copy'."
7878   (interactive "P")
7879   (unless action
7880     (setq action 'move))
7881   ;; Disable marking as read.
7882   (let (gnus-mark-article-hook)
7883     (save-window-excursion
7884       (gnus-summary-select-article)))
7885   ;; Check whether the source group supports the required functions.
7886   (cond ((and (eq action 'move)
7887               (not (gnus-check-backend-function
7888                     'request-move-article gnus-newsgroup-name)))
7889          (error "The current group does not support article moving"))
7890         ((and (eq action 'crosspost)
7891               (not (gnus-check-backend-function
7892                     'request-replace-article gnus-newsgroup-name)))
7893          (error "The current group does not support article editing")))
7894   (let ((articles (gnus-summary-work-articles n))
7895         (prefix (if (gnus-check-backend-function
7896                     'request-move-article gnus-newsgroup-name)
7897                     (gnus-group-real-prefix gnus-newsgroup-name)
7898                   ""))
7899         (names '((move "Move" "Moving")
7900                  (copy "Copy" "Copying")
7901                  (crosspost "Crosspost" "Crossposting")))
7902         (copy-buf (save-excursion
7903                     (nnheader-set-temp-buffer " *copy article*")))
7904         (default-marks gnus-article-mark-lists)
7905         (no-expire-marks (delete '(expirable . expire)
7906                                  (copy-sequence gnus-article-mark-lists)))
7907         art-group to-method new-xref article to-groups)
7908     (unless (assq action names)
7909       (error "Unknown action %s" action))
7910     ;; Read the newsgroup name.
7911     (when (and (not to-newsgroup)
7912                (not select-method))
7913       (setq to-newsgroup
7914             (gnus-read-move-group-name
7915              (cadr (assq action names))
7916              (symbol-value (intern (format "gnus-current-%s-group" action)))
7917              articles prefix))
7918       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7919     (setq to-method (or select-method
7920                         (gnus-server-to-method
7921                          (gnus-group-method to-newsgroup))))
7922     ;; Check the method we are to move this article to...
7923     (unless (gnus-check-backend-function
7924              'request-accept-article (car to-method))
7925       (error "%s does not support article copying" (car to-method)))
7926     (unless (gnus-check-server to-method)
7927       (error "Can't open server %s" (car to-method)))
7928     (gnus-message 6 "%s to %s: %s..."
7929                   (caddr (assq action names))
7930                   (or (car select-method) to-newsgroup) articles)
7931     (while articles
7932       (setq article (pop articles))
7933       (setq
7934        art-group
7935        (cond
7936         ;; Move the article.
7937         ((eq action 'move)
7938          ;; Remove this article from future suppression.
7939          (gnus-dup-unsuppress-article article)
7940          (gnus-request-move-article
7941           article                       ; Article to move
7942           gnus-newsgroup-name           ; From newsgroup
7943           (nth 1 (gnus-find-method-for-group
7944                   gnus-newsgroup-name)) ; Server
7945           (list 'gnus-request-accept-article
7946                 to-newsgroup (list 'quote select-method)
7947                 (not articles) t)       ; Accept form
7948           (not articles)))              ; Only save nov last time
7949         ;; Copy the article.
7950         ((eq action 'copy)
7951          (save-excursion
7952            (set-buffer copy-buf)
7953            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7954              (gnus-request-accept-article
7955               to-newsgroup select-method (not articles) t))))
7956         ;; Crosspost the article.
7957         ((eq action 'crosspost)
7958          (let ((xref (message-tokenize-header
7959                       (mail-header-xref (gnus-summary-article-header article))
7960                       " ")))
7961            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7962                                   ":" article))
7963            (unless xref
7964              (setq xref (list (system-name))))
7965            (setq new-xref
7966                  (concat
7967                   (mapconcat 'identity
7968                              (delete "Xref:" (delete new-xref xref))
7969                              " ")
7970                   " " new-xref))
7971            (save-excursion
7972              (set-buffer copy-buf)
7973              ;; First put the article in the destination group.
7974              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7975              (when (consp (setq art-group
7976                                 (gnus-request-accept-article
7977                                  to-newsgroup select-method (not articles))))
7978                (setq new-xref (concat new-xref " " (car art-group)
7979                                       ":" (cdr art-group)))
7980                ;; Now we have the new Xrefs header, so we insert
7981                ;; it and replace the new article.
7982                (nnheader-replace-header "Xref" new-xref)
7983                (gnus-request-replace-article
7984                 (cdr art-group) to-newsgroup (current-buffer))
7985                art-group))))))
7986       (cond
7987        ((not art-group)
7988         (gnus-message 1 "Couldn't %s article %s: %s"
7989                       (cadr (assq action names)) article
7990                       (nnheader-get-report (car to-method))))
7991        ((eq art-group 'junk)
7992         (when (eq action 'move)
7993           (gnus-summary-mark-article article gnus-canceled-mark)
7994           (gnus-message 4 "Deleted article %s" article)))
7995        (t
7996         (let* ((pto-group (gnus-group-prefixed-name
7997                            (car art-group) to-method))
7998                (entry
7999                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8000                (info (nth 2 entry))
8001                (to-group (gnus-info-group info))
8002                to-marks)
8003           ;; Update the group that has been moved to.
8004           (when (and info
8005                      (memq action '(move copy)))
8006             (unless (member to-group to-groups)
8007               (push to-group to-groups))
8008
8009             (unless (memq article gnus-newsgroup-unreads)
8010               (push 'read to-marks)
8011               (gnus-info-set-read
8012                info (gnus-add-to-range (gnus-info-read info)
8013                                        (list (cdr art-group)))))
8014
8015             ;; See whether the article is to be put in the cache.
8016             (let ((marks (if (gnus-group-auto-expirable-p to-group)
8017                              default-marks
8018                            no-expire-marks))
8019                   (to-article (cdr art-group)))
8020
8021               ;; Enter the article into the cache in the new group,
8022               ;; if that is required.
8023               (when gnus-use-cache
8024                 (gnus-cache-possibly-enter-article
8025                  to-group to-article
8026                  (let ((header (copy-sequence
8027                                 (gnus-summary-article-header article))))
8028                    (mail-header-set-number header to-article)
8029                    header)
8030                  (memq article gnus-newsgroup-marked)
8031                  (memq article gnus-newsgroup-dormant)
8032                  (memq article gnus-newsgroup-unreads)))
8033
8034               (when gnus-preserve-marks
8035                 ;; Copy any marks over to the new group.
8036                 (when (and (equal to-group gnus-newsgroup-name)
8037                            (not (memq article gnus-newsgroup-unreads)))
8038                   ;; Mark this article as read in this group.
8039                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8040                   (setcdr (gnus-active to-group) to-article)
8041                   (setcdr gnus-newsgroup-active to-article))
8042
8043                 (while marks
8044                   (when (memq article (symbol-value
8045                                        (intern (format "gnus-newsgroup-%s"
8046                                                        (caar marks)))))
8047                     (push (cdar marks) to-marks)
8048                     ;; If the other group is the same as this group,
8049                     ;; then we have to add the mark to the list.
8050                     (when (equal to-group gnus-newsgroup-name)
8051                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8052                            (cons to-article
8053                                  (symbol-value
8054                                   (intern (format "gnus-newsgroup-%s"
8055                                                   (caar marks)))))))
8056                     ;; Copy the marks to other group.
8057                     (gnus-add-marked-articles
8058                      to-group (cdar marks) (list to-article) info))
8059                   (setq marks (cdr marks)))
8060
8061                 (gnus-request-set-mark to-group (list (list (list to-article)
8062                                                             'set
8063                                                             to-marks))))
8064
8065               (gnus-dribble-enter
8066                (concat "(gnus-group-set-info '"
8067                        (gnus-prin1-to-string (gnus-get-info to-group))
8068                        ")"))))
8069
8070           ;; Update the Xref header in this article to point to
8071           ;; the new crossposted article we have just created.
8072           (when (eq action 'crosspost)
8073             (save-excursion
8074               (set-buffer copy-buf)
8075               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8076               (nnheader-replace-header "Xref" new-xref)
8077               (gnus-request-replace-article
8078                article gnus-newsgroup-name (current-buffer)))))
8079
8080         ;;;!!!Why is this necessary?
8081         (set-buffer gnus-summary-buffer)
8082
8083         (gnus-summary-goto-subject article)
8084         (when (eq action 'move)
8085           (gnus-summary-mark-article article gnus-canceled-mark))))
8086       (gnus-summary-remove-process-mark article))
8087     ;; Re-activate all groups that have been moved to.
8088     (while to-groups
8089       (save-excursion
8090         (set-buffer gnus-group-buffer)
8091         (when (gnus-group-goto-group (car to-groups) t)
8092           (gnus-group-get-new-news-this-group 1 t))
8093         (pop to-groups)))
8094
8095     (gnus-kill-buffer copy-buf)
8096     (gnus-summary-position-point)
8097     (gnus-set-mode-line 'summary)))
8098
8099 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8100   "Move the current article to a different newsgroup.
8101 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8102 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8103 re-spool using this method."
8104   (interactive "P")
8105   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8106
8107 (defun gnus-summary-crosspost-article (&optional n)
8108   "Crosspost the current article to some other group."
8109   (interactive "P")
8110   (gnus-summary-move-article n nil nil 'crosspost))
8111
8112 (defcustom gnus-summary-respool-default-method nil
8113   "Default method for respooling an article.
8114 If nil, use to the current newsgroup method."
8115   :type '(choice (gnus-select-method :value (nnml ""))
8116                  (const nil))
8117   :group 'gnus-summary-mail)
8118
8119 (defun gnus-summary-respool-article (&optional n method)
8120   "Respool the current article.
8121 The article will be squeezed through the mail spooling process again,
8122 which means that it will be put in some mail newsgroup or other
8123 depending on `nnmail-split-methods'.
8124 If N is a positive number, respool the N next articles.
8125 If N is a negative number, respool the N previous articles.
8126 If N is nil and any articles have been marked with the process mark,
8127 respool those articles instead.
8128
8129 Respooling can be done both from mail groups and \"real\" newsgroups.
8130 In the former case, the articles in question will be moved from the
8131 current group into whatever groups they are destined to.  In the
8132 latter case, they will be copied into the relevant groups."
8133   (interactive
8134    (list current-prefix-arg
8135          (let* ((methods (gnus-methods-using 'respool))
8136                 (methname
8137                  (symbol-name (or gnus-summary-respool-default-method
8138                                   (car (gnus-find-method-for-group
8139                                         gnus-newsgroup-name)))))
8140                 (method
8141                  (gnus-completing-read
8142                   methname "What backend do you want to use when respooling?"
8143                   methods nil t nil 'gnus-mail-method-history))
8144                 ms)
8145            (cond
8146             ((zerop (length (setq ms (gnus-servers-using-backend
8147                                       (intern method)))))
8148              (list (intern method) ""))
8149             ((= 1 (length ms))
8150              (car ms))
8151             (t
8152              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8153                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8154                            ms-alist))))))))
8155   (unless method
8156     (error "No method given for respooling"))
8157   (if (assoc (symbol-name
8158               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8159              (gnus-methods-using 'respool))
8160       (gnus-summary-move-article n nil method)
8161     (gnus-summary-copy-article n nil method)))
8162
8163 (defun gnus-summary-import-article (file &optional edit)
8164   "Import an arbitrary file into a mail newsgroup."
8165   (interactive "fImport file: \nP")
8166   (let ((group gnus-newsgroup-name)
8167         (now (current-time))
8168         atts lines group-art)
8169     (unless (gnus-check-backend-function 'request-accept-article group)
8170       (error "%s does not support article importing" group))
8171     (or (file-readable-p file)
8172         (not (file-regular-p file))
8173         (error "Can't read %s" file))
8174     (save-excursion
8175       (set-buffer (gnus-get-buffer-create " *import file*"))
8176       (erase-buffer)
8177       (nnheader-insert-file-contents file)
8178       (goto-char (point-min))
8179       (unless (nnheader-article-p)
8180         ;; This doesn't look like an article, so we fudge some headers.
8181         (setq atts (file-attributes file)
8182               lines (count-lines (point-min) (point-max)))
8183         (insert "From: " (read-string "From: ") "\n"
8184                 "Subject: " (read-string "Subject: ") "\n"
8185                 "Date: " (message-make-date (nth 5 atts)) "\n"
8186                 "Message-ID: " (message-make-message-id) "\n"
8187                 "Lines: " (int-to-string lines) "\n"
8188                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8189       (setq group-art (gnus-request-accept-article group nil t))
8190       (kill-buffer (current-buffer)))
8191     (setq gnus-newsgroup-active (gnus-activate-group group))
8192     (forward-line 1)
8193     (gnus-summary-goto-article (cdr group-art) nil t)
8194     (when edit
8195       (gnus-summary-edit-article))))
8196
8197 (defun gnus-summary-create-article ()
8198   "Create an article in a mail newsgroup."
8199   (interactive)
8200   (let ((group gnus-newsgroup-name)
8201         (now (current-time))
8202         group-art)
8203     (unless (gnus-check-backend-function 'request-accept-article group)
8204       (error "%s does not support article importing" group))
8205     (save-excursion
8206       (set-buffer (gnus-get-buffer-create " *import file*"))
8207       (erase-buffer)
8208       (goto-char (point-min))
8209       ;; This doesn't look like an article, so we fudge some headers.
8210       (insert "From: " (read-string "From: ") "\n"
8211               "Subject: " (read-string "Subject: ") "\n"
8212               "Date: " (message-make-date now) "\n"
8213               "Message-ID: " (message-make-message-id) "\n")
8214       (setq group-art (gnus-request-accept-article group nil t))
8215       (kill-buffer (current-buffer)))
8216     (setq gnus-newsgroup-active (gnus-activate-group group))
8217     (forward-line 1)
8218     (gnus-summary-goto-article (cdr group-art) nil t)
8219     (gnus-summary-edit-article)))
8220
8221 (defun gnus-summary-article-posted-p ()
8222   "Say whether the current (mail) article is available from news as well.
8223 This will be the case if the article has both been mailed and posted."
8224   (interactive)
8225   (let ((id (mail-header-references (gnus-summary-article-header)))
8226         (gnus-override-method (car (gnus-refer-article-methods))))
8227     (if (gnus-request-head id "")
8228         (gnus-message 2 "The current message was found on %s"
8229                       gnus-override-method)
8230       (gnus-message 2 "The current message couldn't be found on %s"
8231                     gnus-override-method)
8232       nil)))
8233
8234 (defun gnus-summary-expire-articles (&optional now)
8235   "Expire all articles that are marked as expirable in the current group."
8236   (interactive)
8237   (when (gnus-check-backend-function
8238          'request-expire-articles gnus-newsgroup-name)
8239     ;; This backend supports expiry.
8240     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8241            (expirable (if total
8242                           (progn
8243                             ;; We need to update the info for
8244                             ;; this group for `gnus-list-of-read-articles'
8245                             ;; to give us the right answer.
8246                             (gnus-run-hooks 'gnus-exit-group-hook)
8247                             (gnus-summary-update-info)
8248                             (gnus-list-of-read-articles gnus-newsgroup-name))
8249                         (setq gnus-newsgroup-expirable
8250                               (sort gnus-newsgroup-expirable '<))))
8251            (expiry-wait (if now 'immediate
8252                           (gnus-group-find-parameter
8253                            gnus-newsgroup-name 'expiry-wait)))
8254            (nnmail-expiry-target
8255             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8256                 nnmail-expiry-target))
8257            es)
8258       (when expirable
8259         ;; There are expirable articles in this group, so we run them
8260         ;; through the expiry process.
8261         (gnus-message 6 "Expiring articles...")
8262         (unless (gnus-check-group gnus-newsgroup-name)
8263           (error "Can't open server for %s" gnus-newsgroup-name))
8264         ;; The list of articles that weren't expired is returned.
8265         (save-excursion
8266           (if expiry-wait
8267               (let ((nnmail-expiry-wait-function nil)
8268                     (nnmail-expiry-wait expiry-wait))
8269                 (setq es (gnus-request-expire-articles
8270                           expirable gnus-newsgroup-name)))
8271             (setq es (gnus-request-expire-articles
8272                       expirable gnus-newsgroup-name)))
8273           (unless total
8274             (setq gnus-newsgroup-expirable es))
8275           ;; We go through the old list of expirable, and mark all
8276           ;; really expired articles as nonexistent.
8277           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8278             (let ((gnus-use-cache nil))
8279               (while expirable
8280                 (unless (memq (car expirable) es)
8281                   (when (gnus-data-find (car expirable))
8282                     (gnus-summary-mark-article
8283                      (car expirable) gnus-canceled-mark)))
8284                 (setq expirable (cdr expirable))))))
8285         (gnus-message 6 "Expiring articles...done")))))
8286
8287 (defun gnus-summary-expire-articles-now ()
8288   "Expunge all expirable articles in the current group.
8289 This means that *all* articles that are marked as expirable will be
8290 deleted forever, right now."
8291   (interactive)
8292   (or gnus-expert-user
8293       (gnus-yes-or-no-p
8294        "Are you really, really, really sure you want to delete all these messages? ")
8295       (error "Phew!"))
8296   (gnus-summary-expire-articles t))
8297
8298 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8299 (defun gnus-summary-delete-article (&optional n)
8300   "Delete the N next (mail) articles.
8301 This command actually deletes articles.  This is not a marking
8302 command.  The article will disappear forever from your life, never to
8303 return.
8304 If N is negative, delete backwards.
8305 If N is nil and articles have been marked with the process mark,
8306 delete these instead."
8307   (interactive "P")
8308   (unless (gnus-check-backend-function 'request-expire-articles
8309                                        gnus-newsgroup-name)
8310     (error "The current newsgroup does not support article deletion"))
8311   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8312     (error "Couldn't open server"))
8313   ;; Compute the list of articles to delete.
8314   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8315         not-deleted)
8316     (if (and gnus-novice-user
8317              (not (gnus-yes-or-no-p
8318                    (format "Do you really want to delete %s forever? "
8319                            (if (> (length articles) 1)
8320                                (format "these %s articles" (length articles))
8321                              "this article")))))
8322         ()
8323       ;; Delete the articles.
8324       (setq not-deleted (gnus-request-expire-articles
8325                          articles gnus-newsgroup-name 'force))
8326       (while articles
8327         (gnus-summary-remove-process-mark (car articles))
8328         ;; The backend might not have been able to delete the article
8329         ;; after all.
8330         (unless (memq (car articles) not-deleted)
8331           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8332         (setq articles (cdr articles)))
8333       (when not-deleted
8334         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8335     (gnus-summary-position-point)
8336     (gnus-set-mode-line 'summary)
8337     not-deleted))
8338
8339 (defun gnus-summary-edit-article (&optional force)
8340   "Edit the current article.
8341 This will have permanent effect only in mail groups.
8342 If FORCE is non-nil, allow editing of articles even in read-only
8343 groups."
8344   (interactive "P")
8345   (save-excursion
8346     (set-buffer gnus-summary-buffer)
8347     (let ((mail-parse-charset gnus-newsgroup-charset)
8348           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8349       (gnus-set-global-variables)
8350       (when (and (not force)
8351                  (gnus-group-read-only-p))
8352         (error "The current newsgroup does not support article editing"))
8353       (gnus-summary-show-article t)
8354       (gnus-article-edit-article
8355        'ignore
8356        `(lambda (no-highlight)
8357           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8358                 (message-options message-options)
8359                 (message-options-set-recipient)
8360                 (mail-parse-ignored-charsets
8361                  ',gnus-newsgroup-ignored-charsets))
8362             (gnus-summary-edit-article-done
8363              ,(or (mail-header-references gnus-current-headers) "")
8364              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8365
8366 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8367
8368 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8369                                                  no-highlight)
8370   "Make edits to the current article permanent."
8371   (interactive)
8372   (save-excursion
8373     ;; The buffer restriction contains the entire article if it exists.
8374     (when (article-goto-body)
8375       (let ((lines (count-lines (point) (point-max)))
8376             (length (- (point-max) (point)))
8377             (case-fold-search t)
8378             (body (copy-marker (point))))
8379         (goto-char (point-min))
8380         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8381           (delete-region (match-beginning 1) (match-end 1))
8382           (insert (number-to-string length)))
8383         (goto-char (point-min))
8384         (when (re-search-forward
8385                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8386           (delete-region (match-beginning 1) (match-end 1))
8387           (insert (number-to-string length)))
8388         (goto-char (point-min))
8389         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8390           (delete-region (match-beginning 1) (match-end 1))
8391           (insert (number-to-string lines))))))
8392   ;; Replace the article.
8393   (let ((buf (current-buffer)))
8394     (with-temp-buffer
8395       (insert-buffer-substring buf)
8396
8397       (if (and (not read-only)
8398                (not (gnus-request-replace-article
8399                      (cdr gnus-article-current) (car gnus-article-current)
8400                      (current-buffer) t)))
8401           (error "Couldn't replace article")
8402         ;; Update the summary buffer.
8403         (if (and references
8404                  (equal (message-tokenize-header references " ")
8405                         (message-tokenize-header
8406                          (or (message-fetch-field "references") "") " ")))
8407             ;; We only have to update this line.
8408             (save-excursion
8409               (save-restriction
8410                 (message-narrow-to-head)
8411                 (let ((head (buffer-string))
8412                       header)
8413                   (with-temp-buffer
8414                     (insert (format "211 %d Article retrieved.\n"
8415                                     (cdr gnus-article-current)))
8416                     (insert head)
8417                     (insert ".\n")
8418                     (let ((nntp-server-buffer (current-buffer)))
8419                       (setq header (car (gnus-get-newsgroup-headers
8420                                          (save-excursion
8421                                            (set-buffer gnus-summary-buffer)
8422                                            gnus-newsgroup-dependencies)
8423                                          t))))
8424                     (save-excursion
8425                       (set-buffer gnus-summary-buffer)
8426                       (gnus-data-set-header
8427                        (gnus-data-find (cdr gnus-article-current))
8428                        header)
8429                       (gnus-summary-update-article-line
8430                        (cdr gnus-article-current) header))))))
8431           ;; Update threads.
8432           (set-buffer (or buffer gnus-summary-buffer))
8433           (gnus-summary-update-article (cdr gnus-article-current)))
8434         ;; Prettify the article buffer again.
8435         (unless no-highlight
8436           (save-excursion
8437             (set-buffer gnus-article-buffer)
8438             ;;;!!! Fix this -- article should be rehighlighted.
8439             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8440             (set-buffer gnus-original-article-buffer)
8441             (gnus-request-article
8442              (cdr gnus-article-current)
8443              (car gnus-article-current) (current-buffer))))
8444         ;; Prettify the summary buffer line.
8445         (when (gnus-visual-p 'summary-highlight 'highlight)
8446           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8447
8448 (defun gnus-summary-edit-wash (key)
8449   "Perform editing command KEY in the article buffer."
8450   (interactive
8451    (list
8452     (progn
8453       (message "%s" (concat (this-command-keys) "- "))
8454       (read-char))))
8455   (message "")
8456   (gnus-summary-edit-article)
8457   (execute-kbd-macro (concat (this-command-keys) key))
8458   (gnus-article-edit-done))
8459
8460 ;;; Respooling
8461
8462 (defun gnus-summary-respool-query (&optional silent trace)
8463   "Query where the respool algorithm would put this article."
8464   (interactive)
8465   (let (gnus-mark-article-hook)
8466     (gnus-summary-select-article)
8467     (save-excursion
8468       (set-buffer gnus-original-article-buffer)
8469       (save-restriction
8470         (message-narrow-to-head)
8471         (let ((groups (nnmail-article-group 'identity trace)))
8472           (unless silent
8473             (if groups
8474                 (message "This message would go to %s"
8475                          (mapconcat 'car groups ", "))
8476               (message "This message would go to no groups"))
8477             groups))))))
8478
8479 (defun gnus-summary-respool-trace ()
8480   "Trace where the respool algorithm would put this article.
8481 Display a buffer showing all fancy splitting patterns which matched."
8482   (interactive)
8483   (gnus-summary-respool-query nil t))
8484
8485 ;; Summary marking commands.
8486
8487 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8488   "Mark articles which has the same subject as read, and then select the next.
8489 If UNMARK is positive, remove any kind of mark.
8490 If UNMARK is negative, tick articles."
8491   (interactive "P")
8492   (when unmark
8493     (setq unmark (prefix-numeric-value unmark)))
8494   (let ((count
8495          (gnus-summary-mark-same-subject
8496           (gnus-summary-article-subject) unmark)))
8497     ;; Select next unread article.  If auto-select-same mode, should
8498     ;; select the first unread article.
8499     (gnus-summary-next-article t (and gnus-auto-select-same
8500                                       (gnus-summary-article-subject)))
8501     (gnus-message 7 "%d article%s marked as %s"
8502                   count (if (= count 1) " is" "s are")
8503                   (if unmark "unread" "read"))))
8504
8505 (defun gnus-summary-kill-same-subject (&optional unmark)
8506   "Mark articles which has the same subject as read.
8507 If UNMARK is positive, remove any kind of mark.
8508 If UNMARK is negative, tick articles."
8509   (interactive "P")
8510   (when unmark
8511     (setq unmark (prefix-numeric-value unmark)))
8512   (let ((count
8513          (gnus-summary-mark-same-subject
8514           (gnus-summary-article-subject) unmark)))
8515     ;; If marked as read, go to next unread subject.
8516     (when (null unmark)
8517       ;; Go to next unread subject.
8518       (gnus-summary-next-subject 1 t))
8519     (gnus-message 7 "%d articles are marked as %s"
8520                   count (if unmark "unread" "read"))))
8521
8522 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8523   "Mark articles with same SUBJECT as read, and return marked number.
8524 If optional argument UNMARK is positive, remove any kinds of marks.
8525 If optional argument UNMARK is negative, mark articles as unread instead."
8526   (let ((count 1))
8527     (save-excursion
8528       (cond
8529        ((null unmark)                   ; Mark as read.
8530         (while (and
8531                 (progn
8532                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8533                   (gnus-summary-show-thread) t)
8534                 (gnus-summary-find-subject subject))
8535           (setq count (1+ count))))
8536        ((> unmark 0)                    ; Tick.
8537         (while (and
8538                 (progn
8539                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8540                   (gnus-summary-show-thread) t)
8541                 (gnus-summary-find-subject subject))
8542           (setq count (1+ count))))
8543        (t                               ; Mark as unread.
8544         (while (and
8545                 (progn
8546                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8547                   (gnus-summary-show-thread) t)
8548                 (gnus-summary-find-subject subject))
8549           (setq count (1+ count)))))
8550       (gnus-set-mode-line 'summary)
8551       ;; Return the number of marked articles.
8552       count)))
8553
8554 (defun gnus-summary-mark-as-processable (n &optional unmark)
8555   "Set the process mark on the next N articles.
8556 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8557 the process mark instead.  The difference between N and the actual
8558 number of articles marked is returned."
8559   (interactive "P")
8560   (if (and (null n) (gnus-region-active-p))
8561       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8562     (setq n (prefix-numeric-value n))
8563     (let ((backward (< n 0))
8564           (n (abs n)))
8565       (while (and
8566               (> n 0)
8567               (if unmark
8568                 (gnus-summary-remove-process-mark
8569                  (gnus-summary-article-number))
8570                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8571               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8572         (setq n (1- n)))
8573       (when (/= 0 n)
8574         (gnus-message 7 "No more articles"))
8575       (gnus-summary-recenter)
8576       (gnus-summary-position-point)
8577       n)))
8578
8579 (defun gnus-summary-unmark-as-processable (n)
8580   "Remove the process mark from the next N articles.
8581 If N is negative, unmark backward instead.  The difference between N and
8582 the actual number of articles unmarked is returned."
8583   (interactive "P")
8584   (gnus-summary-mark-as-processable n t))
8585
8586 (defun gnus-summary-unmark-all-processable ()
8587   "Remove the process mark from all articles."
8588   (interactive)
8589   (save-excursion
8590     (while gnus-newsgroup-processable
8591       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8592   (gnus-summary-position-point))
8593
8594 (defun gnus-summary-add-mark (article type)
8595   "Mark ARTICLE with a mark of TYPE."
8596   (let ((vtype (car (assq type gnus-article-mark-lists)))
8597         var)
8598     (if (not vtype)
8599         (error "No such mark type: %s" type)
8600       (setq var (intern (format "gnus-newsgroup-%s" type)))
8601       (set var (cons article (symbol-value var)))
8602       (if (memq type '(processable cached replied forwarded saved))
8603           (gnus-summary-update-secondary-mark article)
8604         ;;; !!! This is bobus.  We should find out what primary
8605         ;;; !!! mark we want to set.
8606         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8607
8608 (defun gnus-summary-mark-as-expirable (n)
8609   "Mark N articles forward as expirable.
8610 If N is negative, mark backward instead.  The difference between N and
8611 the actual number of articles marked is returned."
8612   (interactive "p")
8613   (gnus-summary-mark-forward n gnus-expirable-mark))
8614
8615 (defun gnus-summary-mark-article-as-replied (article)
8616   "Mark ARTICLE as replied to and update the summary line.
8617 ARTICLE can also be a list of articles."
8618   (interactive (list (gnus-summary-article-number)))
8619   (let ((articles (if (listp article) article (list article))))
8620     (dolist (article articles)
8621       (push article gnus-newsgroup-replied)
8622       (let ((buffer-read-only nil))
8623         (when (gnus-summary-goto-subject article nil t)
8624           (gnus-summary-update-secondary-mark article))))))
8625
8626 (defun gnus-summary-mark-article-as-forwarded (article)
8627   "Mark ARTICLE as forwarded and update the summary line.
8628 ARTICLE can also be a list of articles."
8629   (let ((articles (if (listp article) article (list article))))
8630     (dolist (article articles)
8631       (push article gnus-newsgroup-forwarded)
8632       (let ((buffer-read-only nil))
8633         (when (gnus-summary-goto-subject article nil t)
8634           (gnus-summary-update-secondary-mark article))))))
8635
8636 (defun gnus-summary-set-bookmark (article)
8637   "Set a bookmark in current article."
8638   (interactive (list (gnus-summary-article-number)))
8639   (when (or (not (get-buffer gnus-article-buffer))
8640             (not gnus-current-article)
8641             (not gnus-article-current)
8642             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8643     (error "No current article selected"))
8644   ;; Remove old bookmark, if one exists.
8645   (let ((old (assq article gnus-newsgroup-bookmarks)))
8646     (when old
8647       (setq gnus-newsgroup-bookmarks
8648             (delq old gnus-newsgroup-bookmarks))))
8649   ;; Set the new bookmark, which is on the form
8650   ;; (article-number . line-number-in-body).
8651   (push
8652    (cons article
8653          (save-excursion
8654            (set-buffer gnus-article-buffer)
8655            (count-lines
8656             (min (point)
8657                  (save-excursion
8658                    (goto-char (point-min))
8659                    (search-forward "\n\n" nil t)
8660                    (point)))
8661             (point))))
8662    gnus-newsgroup-bookmarks)
8663   (gnus-message 6 "A bookmark has been added to the current article."))
8664
8665 (defun gnus-summary-remove-bookmark (article)
8666   "Remove the bookmark from the current article."
8667   (interactive (list (gnus-summary-article-number)))
8668   ;; Remove old bookmark, if one exists.
8669   (let ((old (assq article gnus-newsgroup-bookmarks)))
8670     (if old
8671         (progn
8672           (setq gnus-newsgroup-bookmarks
8673                 (delq old gnus-newsgroup-bookmarks))
8674           (gnus-message 6 "Removed bookmark."))
8675       (gnus-message 6 "No bookmark in current article."))))
8676
8677 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8678 (defun gnus-summary-mark-as-dormant (n)
8679   "Mark N articles forward as dormant.
8680 If N is negative, mark backward instead.  The difference between N and
8681 the actual number of articles marked is returned."
8682   (interactive "p")
8683   (gnus-summary-mark-forward n gnus-dormant-mark))
8684
8685 (defun gnus-summary-set-process-mark (article)
8686   "Set the process mark on ARTICLE and update the summary line."
8687   (setq gnus-newsgroup-processable
8688         (cons article
8689               (delq article gnus-newsgroup-processable)))
8690   (when (gnus-summary-goto-subject article)
8691     (gnus-summary-show-thread)
8692     (gnus-summary-goto-subject article)
8693     (gnus-summary-update-secondary-mark article)))
8694
8695 (defun gnus-summary-remove-process-mark (article)
8696   "Remove the process mark from ARTICLE and update the summary line."
8697   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8698   (when (gnus-summary-goto-subject article)
8699     (gnus-summary-show-thread)
8700     (gnus-summary-goto-subject article)
8701     (gnus-summary-update-secondary-mark article)))
8702
8703 (defun gnus-summary-set-saved-mark (article)
8704   "Set the process mark on ARTICLE and update the summary line."
8705   (push article gnus-newsgroup-saved)
8706   (when (gnus-summary-goto-subject article)
8707     (gnus-summary-update-secondary-mark article)))
8708
8709 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8710   "Mark N articles as read forwards.
8711 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8712 The difference between N and the actual number of articles marked is
8713 returned.
8714 Iff NO-EXPIRE, auto-expiry will be inhibited."
8715   (interactive "p")
8716   (gnus-summary-show-thread)
8717   (let ((backward (< n 0))
8718         (gnus-summary-goto-unread
8719          (and gnus-summary-goto-unread
8720               (not (eq gnus-summary-goto-unread 'never))
8721               (not (memq mark (list gnus-unread-mark
8722                                     gnus-ticked-mark gnus-dormant-mark)))))
8723         (n (abs n))
8724         (mark (or mark gnus-del-mark)))
8725     (while (and (> n 0)
8726                 (gnus-summary-mark-article nil mark no-expire)
8727                 (zerop (gnus-summary-next-subject
8728                         (if backward -1 1)
8729                         (and gnus-summary-goto-unread
8730                              (not (eq gnus-summary-goto-unread 'never)))
8731                         t)))
8732       (setq n (1- n)))
8733     (when (/= 0 n)
8734       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8735     (gnus-summary-recenter)
8736     (gnus-summary-position-point)
8737     (gnus-set-mode-line 'summary)
8738     n))
8739
8740 (defun gnus-summary-mark-article-as-read (mark)
8741   "Mark the current article quickly as read with MARK."
8742   (let ((article (gnus-summary-article-number)))
8743     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8744     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8745     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8746     (push (cons article mark) gnus-newsgroup-reads)
8747     ;; Possibly remove from cache, if that is used.
8748     (when gnus-use-cache
8749       (gnus-cache-enter-remove-article article))
8750     ;; Allow the backend to change the mark.
8751     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8752     ;; Check for auto-expiry.
8753     (when (and gnus-newsgroup-auto-expire
8754                (memq mark gnus-auto-expirable-marks))
8755       (setq mark gnus-expirable-mark)
8756       ;; Let the backend know about the mark change.
8757       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8758       (push article gnus-newsgroup-expirable))
8759     ;; Set the mark in the buffer.
8760     (gnus-summary-update-mark mark 'unread)
8761     t))
8762
8763 (defun gnus-summary-mark-article-as-unread (mark)
8764   "Mark the current article quickly as unread with MARK."
8765   (let* ((article (gnus-summary-article-number))
8766          (old-mark (gnus-summary-article-mark article)))
8767     ;; Allow the backend to change the mark.
8768     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8769     (if (eq mark old-mark)
8770         t
8771       (if (<= article 0)
8772           (progn
8773             (gnus-error 1 "Can't mark negative article numbers")
8774             nil)
8775         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8776         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8777         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8778         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8779         (cond ((= mark gnus-ticked-mark)
8780                (push article gnus-newsgroup-marked))
8781               ((= mark gnus-dormant-mark)
8782                (push article gnus-newsgroup-dormant))
8783               (t
8784                (push article gnus-newsgroup-unreads)))
8785         (gnus-pull article gnus-newsgroup-reads)
8786
8787         ;; See whether the article is to be put in the cache.
8788         (and gnus-use-cache
8789              (vectorp (gnus-summary-article-header article))
8790              (save-excursion
8791                (gnus-cache-possibly-enter-article
8792                 gnus-newsgroup-name article
8793                 (gnus-summary-article-header article)
8794                 (= mark gnus-ticked-mark)
8795                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8796
8797         ;; Fix the mark.
8798         (gnus-summary-update-mark mark 'unread)
8799         t))))
8800
8801 (defun gnus-summary-mark-article (&optional article mark no-expire)
8802   "Mark ARTICLE with MARK.  MARK can be any character.
8803 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8804 `??' (dormant) and `?E' (expirable).
8805 If MARK is nil, then the default character `?r' is used.
8806 If ARTICLE is nil, then the article on the current line will be
8807 marked.
8808 Iff NO-EXPIRE, auto-expiry will be inhibited."
8809   ;; The mark might be a string.
8810   (when (stringp mark)
8811     (setq mark (aref mark 0)))
8812   ;; If no mark is given, then we check auto-expiring.
8813   (when (null mark)
8814     (setq mark gnus-del-mark))
8815   (when (and (not no-expire)
8816              gnus-newsgroup-auto-expire
8817              (memq mark gnus-auto-expirable-marks))
8818     (setq mark gnus-expirable-mark))
8819   (let ((article (or article (gnus-summary-article-number)))
8820         (old-mark (gnus-summary-article-mark article)))
8821     ;; Allow the backend to change the mark.
8822     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8823     (if (eq mark old-mark)
8824         t
8825       (unless article
8826         (error "No article on current line"))
8827       (if (not (if (or (= mark gnus-unread-mark)
8828                        (= mark gnus-ticked-mark)
8829                        (= mark gnus-dormant-mark))
8830                    (gnus-mark-article-as-unread article mark)
8831                  (gnus-mark-article-as-read article mark)))
8832           t
8833         ;; See whether the article is to be put in the cache.
8834         (and gnus-use-cache
8835              (not (= mark gnus-canceled-mark))
8836              (vectorp (gnus-summary-article-header article))
8837              (save-excursion
8838                (gnus-cache-possibly-enter-article
8839                 gnus-newsgroup-name article
8840                 (gnus-summary-article-header article)
8841                 (= mark gnus-ticked-mark)
8842                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8843
8844         (when (gnus-summary-goto-subject article nil t)
8845           (let ((buffer-read-only nil))
8846             (gnus-summary-show-thread)
8847             ;; Fix the mark.
8848             (gnus-summary-update-mark mark 'unread)
8849             t))))))
8850
8851 (defun gnus-summary-update-secondary-mark (article)
8852   "Update the secondary (read, process, cache) mark."
8853   (gnus-summary-update-mark
8854    (cond ((memq article gnus-newsgroup-processable)
8855           gnus-process-mark)
8856          ((memq article gnus-newsgroup-cached)
8857           gnus-cached-mark)
8858          ((memq article gnus-newsgroup-replied)
8859           gnus-replied-mark)
8860          ((memq article gnus-newsgroup-forwarded)
8861           gnus-forwarded-mark)
8862          ((memq article gnus-newsgroup-saved)
8863           gnus-saved-mark)
8864          (t gnus-no-mark))
8865    'replied)
8866   (when (gnus-visual-p 'summary-highlight 'highlight)
8867     (gnus-run-hooks 'gnus-summary-update-hook))
8868   t)
8869
8870 (defun gnus-summary-update-mark (mark type)
8871   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8872         (buffer-read-only nil))
8873     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8874     (when forward
8875       (when (looking-at "\r")
8876         (incf forward))
8877       (when (<= (+ forward (point)) (point-max))
8878         ;; Go to the right position on the line.
8879         (goto-char (+ forward (point)))
8880         ;; Replace the old mark with the new mark.
8881         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8882         ;; Optionally update the marks by some user rule.
8883         (when (eq type 'unread)
8884           (gnus-data-set-mark
8885            (gnus-data-find (gnus-summary-article-number)) mark)
8886           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8887
8888 (defun gnus-mark-article-as-read (article &optional mark)
8889   "Enter ARTICLE in the pertinent lists and remove it from others."
8890   ;; Make the article expirable.
8891   (let ((mark (or mark gnus-del-mark)))
8892     (if (= mark gnus-expirable-mark)
8893         (push article gnus-newsgroup-expirable)
8894       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8895     ;; Remove from unread and marked lists.
8896     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8897     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8898     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8899     (push (cons article mark) gnus-newsgroup-reads)
8900     ;; Possibly remove from cache, if that is used.
8901     (when gnus-use-cache
8902       (gnus-cache-enter-remove-article article))
8903     t))
8904
8905 (defun gnus-mark-article-as-unread (article &optional mark)
8906   "Enter ARTICLE in the pertinent lists and remove it from others."
8907   (let ((mark (or mark gnus-ticked-mark)))
8908     (if (<= article 0)
8909         (progn
8910           (gnus-error 1 "Can't mark negative article numbers")
8911           nil)
8912       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8913             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8914             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8915             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8916
8917       ;; Unsuppress duplicates?
8918       (when gnus-suppress-duplicates
8919         (gnus-dup-unsuppress-article article))
8920
8921       (cond ((= mark gnus-ticked-mark)
8922              (push article gnus-newsgroup-marked))
8923             ((= mark gnus-dormant-mark)
8924              (push article gnus-newsgroup-dormant))
8925             (t
8926              (push article gnus-newsgroup-unreads)))
8927       (gnus-pull article gnus-newsgroup-reads)
8928       t)))
8929
8930 (defalias 'gnus-summary-mark-as-unread-forward
8931   'gnus-summary-tick-article-forward)
8932 (make-obsolete 'gnus-summary-mark-as-unread-forward
8933                'gnus-summary-tick-article-forward)
8934 (defun gnus-summary-tick-article-forward (n)
8935   "Tick N articles forwards.
8936 If N is negative, tick backwards instead.
8937 The difference between N and the number of articles ticked is returned."
8938   (interactive "p")
8939   (gnus-summary-mark-forward n gnus-ticked-mark))
8940
8941 (defalias 'gnus-summary-mark-as-unread-backward
8942   'gnus-summary-tick-article-backward)
8943 (make-obsolete 'gnus-summary-mark-as-unread-backward
8944                'gnus-summary-tick-article-backward)
8945 (defun gnus-summary-tick-article-backward (n)
8946   "Tick N articles backwards.
8947 The difference between N and the number of articles ticked is returned."
8948   (interactive "p")
8949   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8950
8951 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8952 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8953 (defun gnus-summary-tick-article (&optional article clear-mark)
8954   "Mark current article as unread.
8955 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8956 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8957   (interactive)
8958   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8959                                        gnus-ticked-mark)))
8960
8961 (defun gnus-summary-mark-as-read-forward (n)
8962   "Mark N articles as read forwards.
8963 If N is negative, mark backwards instead.
8964 The difference between N and the actual number of articles marked is
8965 returned."
8966   (interactive "p")
8967   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8968
8969 (defun gnus-summary-mark-as-read-backward (n)
8970   "Mark the N articles as read backwards.
8971 The difference between N and the actual number of articles marked is
8972 returned."
8973   (interactive "p")
8974   (gnus-summary-mark-forward
8975    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8976
8977 (defun gnus-summary-mark-as-read (&optional article mark)
8978   "Mark current article as read.
8979 ARTICLE specifies the article to be marked as read.
8980 MARK specifies a string to be inserted at the beginning of the line."
8981   (gnus-summary-mark-article article mark))
8982
8983 (defun gnus-summary-clear-mark-forward (n)
8984   "Clear marks from N articles forward.
8985 If N is negative, clear backward instead.
8986 The difference between N and the number of marks cleared is returned."
8987   (interactive "p")
8988   (gnus-summary-mark-forward n gnus-unread-mark))
8989
8990 (defun gnus-summary-clear-mark-backward (n)
8991   "Clear marks from N articles backward.
8992 The difference between N and the number of marks cleared is returned."
8993   (interactive "p")
8994   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8995
8996 (defun gnus-summary-mark-unread-as-read ()
8997   "Intended to be used by `gnus-summary-mark-article-hook'."
8998   (when (memq gnus-current-article gnus-newsgroup-unreads)
8999     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9000
9001 (defun gnus-summary-mark-read-and-unread-as-read ()
9002   "Intended to be used by `gnus-summary-mark-article-hook'."
9003   (let ((mark (gnus-summary-article-mark)))
9004     (when (or (gnus-unread-mark-p mark)
9005               (gnus-read-mark-p mark))
9006       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9007
9008 (defun gnus-summary-mark-unread-as-ticked ()
9009    "Intended to be used by `gnus-summary-mark-article-hook'."
9010   (when (memq gnus-current-article gnus-newsgroup-unreads)
9011     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9012
9013 (defun gnus-summary-mark-region-as-read (point mark all)
9014   "Mark all unread articles between point and mark as read.
9015 If given a prefix, mark all articles between point and mark as read,
9016 even ticked and dormant ones."
9017   (interactive "r\nP")
9018   (save-excursion
9019     (let (article)
9020       (goto-char point)
9021       (beginning-of-line)
9022       (while (and
9023               (< (point) mark)
9024               (progn
9025                 (when (or all
9026                           (memq (setq article (gnus-summary-article-number))
9027                                 gnus-newsgroup-unreads))
9028                   (gnus-summary-mark-article article gnus-del-mark))
9029                 t)
9030               (gnus-summary-find-next))))))
9031
9032 (defun gnus-summary-mark-below (score mark)
9033   "Mark articles with score less than SCORE with MARK."
9034   (interactive "P\ncMark: ")
9035   (setq score (if score
9036                   (prefix-numeric-value score)
9037                 (or gnus-summary-default-score 0)))
9038   (save-excursion
9039     (set-buffer gnus-summary-buffer)
9040     (goto-char (point-min))
9041     (while
9042         (progn
9043           (and (< (gnus-summary-article-score) score)
9044                (gnus-summary-mark-article nil mark))
9045           (gnus-summary-find-next)))))
9046
9047 (defun gnus-summary-kill-below (&optional score)
9048   "Mark articles with score below SCORE as read."
9049   (interactive "P")
9050   (gnus-summary-mark-below score gnus-killed-mark))
9051
9052 (defun gnus-summary-clear-above (&optional score)
9053   "Clear all marks from articles with score above SCORE."
9054   (interactive "P")
9055   (gnus-summary-mark-above score gnus-unread-mark))
9056
9057 (defun gnus-summary-tick-above (&optional score)
9058   "Tick all articles with score above SCORE."
9059   (interactive "P")
9060   (gnus-summary-mark-above score gnus-ticked-mark))
9061
9062 (defun gnus-summary-mark-above (score mark)
9063   "Mark articles with score over SCORE with MARK."
9064   (interactive "P\ncMark: ")
9065   (setq score (if score
9066                   (prefix-numeric-value score)
9067                 (or gnus-summary-default-score 0)))
9068   (save-excursion
9069     (set-buffer gnus-summary-buffer)
9070     (goto-char (point-min))
9071     (while (and (progn
9072                   (when (> (gnus-summary-article-score) score)
9073                     (gnus-summary-mark-article nil mark))
9074                   t)
9075                 (gnus-summary-find-next)))))
9076
9077 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9078 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9079 (defun gnus-summary-limit-include-expunged (&optional no-error)
9080   "Display all the hidden articles that were expunged for low scores."
9081   (interactive)
9082   (let ((buffer-read-only nil))
9083     (let ((scored gnus-newsgroup-scored)
9084           headers h)
9085       (while scored
9086         (unless (gnus-summary-article-header (caar scored))
9087           (and (setq h (gnus-number-to-header (caar scored)))
9088                (< (cdar scored) gnus-summary-expunge-below)
9089                (push h headers)))
9090         (setq scored (cdr scored)))
9091       (if (not headers)
9092           (when (not no-error)
9093             (error "No expunged articles hidden"))
9094         (goto-char (point-min))
9095         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9096         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9097         (mapcar (lambda (x) (push (mail-header-number x) 
9098                                   gnus-newsgroup-limit))
9099                 headers)
9100         (gnus-summary-prepare-unthreaded (nreverse headers))
9101         (goto-char (point-min))
9102         (gnus-summary-position-point)
9103         t))))
9104
9105 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
9106   "Mark all unread articles in this newsgroup as read.
9107 If prefix argument ALL is non-nil, ticked and dormant articles will
9108 also be marked as read.
9109 If QUIETLY is non-nil, no questions will be asked.
9110 If TO-HERE is non-nil, it should be a point in the buffer.  All
9111 articles before this point will be marked as read.
9112 Note that this function will only catch up the unread article
9113 in the current summary buffer limitation.
9114 The number of articles marked as read is returned."
9115   (interactive "P")
9116   (prog1
9117       (save-excursion
9118         (when (or quietly
9119                   (not gnus-interactive-catchup) ;Without confirmation?
9120                   gnus-expert-user
9121                   (gnus-y-or-n-p
9122                    (if all
9123                        "Mark absolutely all articles as read? "
9124                      "Mark all unread articles as read? ")))
9125           (if (and not-mark
9126                    (not gnus-newsgroup-adaptive)
9127                    (not gnus-newsgroup-auto-expire)
9128                    (not gnus-suppress-duplicates)
9129                    (or (not gnus-use-cache)
9130                        (eq gnus-use-cache 'passive)))
9131               (progn
9132                 (when all
9133                   (setq gnus-newsgroup-marked nil
9134                         gnus-newsgroup-dormant nil))
9135                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9136             ;; We actually mark all articles as canceled, which we
9137             ;; have to do when using auto-expiry or adaptive scoring.
9138             (gnus-summary-show-all-threads)
9139             (when (gnus-summary-first-subject (not all) t)
9140               (while (and
9141                       (if to-here (< (point) to-here) t)
9142                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
9143                       (gnus-summary-find-next (not all) nil nil t))))
9144             (gnus-set-mode-line 'summary))
9145           t))
9146     (gnus-summary-position-point)))
9147
9148 (defun gnus-summary-catchup-to-here (&optional all)
9149   "Mark all unticked articles before the current one as read.
9150 If ALL is non-nil, also mark ticked and dormant articles as read."
9151   (interactive "P")
9152   (save-excursion
9153     (gnus-save-hidden-threads
9154       (let ((beg (point)))
9155         ;; We check that there are unread articles.
9156         (when (or all (gnus-summary-find-prev))
9157           (gnus-summary-catchup all t beg)))))
9158   (gnus-summary-position-point))
9159
9160 (defun gnus-summary-catchup-all (&optional quietly)
9161   "Mark all articles in this newsgroup as read."
9162   (interactive "P")
9163   (gnus-summary-catchup t quietly))
9164
9165 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9166   "Mark all unread articles in this group as read, then exit.
9167 If prefix argument ALL is non-nil, all articles are marked as read.
9168 If QUIETLY is non-nil, no questions will be asked."
9169   (interactive "P")
9170   (when (gnus-summary-catchup all quietly nil 'fast)
9171     ;; Select next newsgroup or exit.
9172     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9173              (eq gnus-auto-select-next 'quietly))
9174         (gnus-summary-next-group nil)
9175       (gnus-summary-exit))))
9176
9177 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9178   "Mark all articles in this newsgroup as read, and then exit."
9179   (interactive "P")
9180   (gnus-summary-catchup-and-exit t quietly))
9181
9182 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9183   "Mark all articles in this group as read and select the next group.
9184 If given a prefix, mark all articles, unread as well as ticked, as
9185 read."
9186   (interactive "P")
9187   (save-excursion
9188     (gnus-summary-catchup all))
9189   (gnus-summary-next-group))
9190
9191 ;;;
9192 ;;; with article
9193 ;;;
9194
9195 (defmacro gnus-with-article (article &rest forms)
9196   "Select ARTICLE and perform FORMS in the original article buffer.
9197 Then replace the article with the result."
9198   `(progn
9199      ;; We don't want the article to be marked as read.
9200      (let (gnus-mark-article-hook)
9201        (gnus-summary-select-article t t nil ,article))
9202      (set-buffer gnus-original-article-buffer)
9203      ,@forms
9204      (if (not (gnus-check-backend-function
9205                'request-replace-article (car gnus-article-current)))
9206          (gnus-message 5 "Read-only group; not replacing")
9207        (unless (gnus-request-replace-article
9208                 ,article (car gnus-article-current)
9209                 (current-buffer) t)
9210          (error "Couldn't replace article")))
9211      ;; The cache and backlog have to be flushed somewhat.
9212      (when gnus-keep-backlog
9213        (gnus-backlog-remove-article
9214         (car gnus-article-current) (cdr gnus-article-current)))
9215      (when gnus-use-cache
9216        (gnus-cache-update-article
9217         (car gnus-article-current) (cdr gnus-article-current)))))
9218
9219 (put 'gnus-with-article 'lisp-indent-function 1)
9220 (put 'gnus-with-article 'edebug-form-spec '(form body))
9221
9222 ;; Thread-based commands.
9223
9224 (defun gnus-summary-articles-in-thread (&optional article)
9225   "Return a list of all articles in the current thread.
9226 If ARTICLE is non-nil, return all articles in the thread that starts
9227 with that article."
9228   (let* ((article (or article (gnus-summary-article-number)))
9229          (data (gnus-data-find-list article))
9230          (top-level (gnus-data-level (car data)))
9231          (top-subject
9232           (cond ((null gnus-thread-operation-ignore-subject)
9233                  (gnus-simplify-subject-re
9234                   (mail-header-subject (gnus-data-header (car data)))))
9235                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9236                  (gnus-simplify-subject-fuzzy
9237                   (mail-header-subject (gnus-data-header (car data)))))
9238                 (t nil)))
9239          (end-point (save-excursion
9240                       (if (gnus-summary-go-to-next-thread)
9241                           (point) (point-max))))
9242          articles)
9243     (while (and data
9244                 (< (gnus-data-pos (car data)) end-point))
9245       (when (or (not top-subject)
9246                 (string= top-subject
9247                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9248                              (gnus-simplify-subject-fuzzy
9249                               (mail-header-subject
9250                                (gnus-data-header (car data))))
9251                            (gnus-simplify-subject-re
9252                             (mail-header-subject
9253                              (gnus-data-header (car data)))))))
9254         (push (gnus-data-number (car data)) articles))
9255       (unless (and (setq data (cdr data))
9256                    (> (gnus-data-level (car data)) top-level))
9257         (setq data nil)))
9258     ;; Return the list of articles.
9259     (nreverse articles)))
9260
9261 (defun gnus-summary-rethread-current ()
9262   "Rethread the thread the current article is part of."
9263   (interactive)
9264   (let* ((gnus-show-threads t)
9265          (article (gnus-summary-article-number))
9266          (id (mail-header-id (gnus-summary-article-header)))
9267          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9268     (unless id
9269       (error "No article on the current line"))
9270     (gnus-rebuild-thread id)
9271     (gnus-summary-goto-subject article)))
9272
9273 (defun gnus-summary-reparent-thread ()
9274   "Make the current article child of the marked (or previous) article.
9275
9276 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9277 is non-nil or the Subject: of both articles are the same."
9278   (interactive)
9279   (unless (not (gnus-group-read-only-p))
9280     (error "The current newsgroup does not support article editing"))
9281   (unless (<= (length gnus-newsgroup-processable) 1)
9282     (error "No more than one article may be marked"))
9283   (save-window-excursion
9284     (let ((gnus-article-buffer " *reparent*")
9285           (current-article (gnus-summary-article-number))
9286           ;; First grab the marked article, otherwise one line up.
9287           (parent-article (if (not (null gnus-newsgroup-processable))
9288                               (car gnus-newsgroup-processable)
9289                             (save-excursion
9290                               (if (eq (forward-line -1) 0)
9291                                   (gnus-summary-article-number)
9292                                 (error "Beginning of summary buffer"))))))
9293       (unless (not (eq current-article parent-article))
9294         (error "An article may not be self-referential"))
9295       (let ((message-id (mail-header-id
9296                          (gnus-summary-article-header parent-article))))
9297         (unless (and message-id (not (equal message-id "")))
9298           (error "No message-id in desired parent"))
9299         (gnus-with-article current-article
9300           (save-restriction
9301             (goto-char (point-min))
9302             (message-narrow-to-head)
9303             (if (re-search-forward "^References: " nil t)
9304                 (progn
9305                   (re-search-forward "^[^ \t]" nil t)
9306                   (forward-line -1)
9307                   (end-of-line)
9308                   (insert " " message-id))
9309               (insert "References: " message-id "\n"))))
9310         (set-buffer gnus-summary-buffer)
9311         (gnus-summary-unmark-all-processable)
9312         (gnus-summary-update-article current-article)
9313         (gnus-summary-rethread-current)
9314         (gnus-message 3 "Article %d is now the child of article %d"
9315                       current-article parent-article)))))
9316
9317 (defun gnus-summary-toggle-threads (&optional arg)
9318   "Toggle showing conversation threads.
9319 If ARG is positive number, turn showing conversation threads on."
9320   (interactive "P")
9321   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9322     (setq gnus-show-threads
9323           (if (null arg) (not gnus-show-threads)
9324             (> (prefix-numeric-value arg) 0)))
9325     (gnus-summary-prepare)
9326     (gnus-summary-goto-subject current)
9327     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9328     (gnus-summary-position-point)))
9329
9330 (defun gnus-summary-show-all-threads ()
9331   "Show all threads."
9332   (interactive)
9333   (save-excursion
9334     (let ((buffer-read-only nil))
9335       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9336   (gnus-summary-position-point))
9337
9338 (defun gnus-summary-show-thread ()
9339   "Show thread subtrees.
9340 Returns nil if no thread was there to be shown."
9341   (interactive)
9342   (let ((buffer-read-only nil)
9343         (orig (point))
9344         ;; first goto end then to beg, to have point at beg after let
9345         (end (progn (end-of-line) (point)))
9346         (beg (progn (beginning-of-line) (point))))
9347     (prog1
9348         ;; Any hidden lines here?
9349         (search-forward "\r" end t)
9350       (subst-char-in-region beg end ?\^M ?\n t)
9351       (goto-char orig)
9352       (gnus-summary-position-point))))
9353
9354 (defun gnus-summary-hide-all-threads ()
9355   "Hide all thread subtrees."
9356   (interactive)
9357   (save-excursion
9358     (goto-char (point-min))
9359     (gnus-summary-hide-thread)
9360     (while (zerop (gnus-summary-next-thread 1 t))
9361       (gnus-summary-hide-thread)))
9362   (gnus-summary-position-point))
9363
9364 (defun gnus-summary-hide-thread ()
9365   "Hide thread subtrees.
9366 Returns nil if no threads were there to be hidden."
9367   (interactive)
9368   (let ((buffer-read-only nil)
9369         (start (point))
9370         (article (gnus-summary-article-number)))
9371     (goto-char start)
9372     ;; Go forward until either the buffer ends or the subthread
9373     ;; ends.
9374     (when (and (not (eobp))
9375                (or (zerop (gnus-summary-next-thread 1 t))
9376                    (goto-char (point-max))))
9377       (prog1
9378           (if (and (> (point) start)
9379                    (search-backward "\n" start t))
9380               (progn
9381                 (subst-char-in-region start (point) ?\n ?\^M)
9382                 (gnus-summary-goto-subject article))
9383             (goto-char start)
9384             nil)))))
9385
9386 (defun gnus-summary-go-to-next-thread (&optional previous)
9387   "Go to the same level (or less) next thread.
9388 If PREVIOUS is non-nil, go to previous thread instead.
9389 Return the article number moved to, or nil if moving was impossible."
9390   (let ((level (gnus-summary-thread-level))
9391         (way (if previous -1 1))
9392         (beg (point)))
9393     (forward-line way)
9394     (while (and (not (eobp))
9395                 (< level (gnus-summary-thread-level)))
9396       (forward-line way))
9397     (if (eobp)
9398         (progn
9399           (goto-char beg)
9400           nil)
9401       (setq beg (point))
9402       (prog1
9403           (gnus-summary-article-number)
9404         (goto-char beg)))))
9405
9406 (defun gnus-summary-next-thread (n &optional silent)
9407   "Go to the same level next N'th thread.
9408 If N is negative, search backward instead.
9409 Returns the difference between N and the number of skips actually
9410 done.
9411
9412 If SILENT, don't output messages."
9413   (interactive "p")
9414   (let ((backward (< n 0))
9415         (n (abs n)))
9416     (while (and (> n 0)
9417                 (gnus-summary-go-to-next-thread backward))
9418       (decf n))
9419     (unless silent
9420       (gnus-summary-position-point))
9421     (when (and (not silent) (/= 0 n))
9422       (gnus-message 7 "No more threads"))
9423     n))
9424
9425 (defun gnus-summary-prev-thread (n)
9426   "Go to the same level previous N'th thread.
9427 Returns the difference between N and the number of skips actually
9428 done."
9429   (interactive "p")
9430   (gnus-summary-next-thread (- n)))
9431
9432 (defun gnus-summary-go-down-thread ()
9433   "Go down one level in the current thread."
9434   (let ((children (gnus-summary-article-children)))
9435     (when children
9436       (gnus-summary-goto-subject (car children)))))
9437
9438 (defun gnus-summary-go-up-thread ()
9439   "Go up one level in the current thread."
9440   (let ((parent (gnus-summary-article-parent)))
9441     (when parent
9442       (gnus-summary-goto-subject parent))))
9443
9444 (defun gnus-summary-down-thread (n)
9445   "Go down thread N steps.
9446 If N is negative, go up instead.
9447 Returns the difference between N and how many steps down that were
9448 taken."
9449   (interactive "p")
9450   (let ((up (< n 0))
9451         (n (abs n)))
9452     (while (and (> n 0)
9453                 (if up (gnus-summary-go-up-thread)
9454                   (gnus-summary-go-down-thread)))
9455       (setq n (1- n)))
9456     (gnus-summary-position-point)
9457     (when (/= 0 n)
9458       (gnus-message 7 "Can't go further"))
9459     n))
9460
9461 (defun gnus-summary-up-thread (n)
9462   "Go up thread N steps.
9463 If N is negative, go down instead.
9464 Returns the difference between N and how many steps down that were
9465 taken."
9466   (interactive "p")
9467   (gnus-summary-down-thread (- n)))
9468
9469 (defun gnus-summary-top-thread ()
9470   "Go to the top of the thread."
9471   (interactive)
9472   (while (gnus-summary-go-up-thread))
9473   (gnus-summary-article-number))
9474
9475 (defun gnus-summary-kill-thread (&optional unmark)
9476   "Mark articles under current thread as read.
9477 If the prefix argument is positive, remove any kinds of marks.
9478 If the prefix argument is negative, tick articles instead."
9479   (interactive "P")
9480   (when unmark
9481     (setq unmark (prefix-numeric-value unmark)))
9482   (let ((articles (gnus-summary-articles-in-thread)))
9483     (save-excursion
9484       ;; Expand the thread.
9485       (gnus-summary-show-thread)
9486       ;; Mark all the articles.
9487       (while articles
9488         (gnus-summary-goto-subject (car articles))
9489         (cond ((null unmark)
9490                (gnus-summary-mark-article-as-read gnus-killed-mark))
9491               ((> unmark 0)
9492                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9493               (t
9494                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9495         (setq articles (cdr articles))))
9496     ;; Hide killed subtrees.
9497     (and (null unmark)
9498          gnus-thread-hide-killed
9499          (gnus-summary-hide-thread))
9500     ;; If marked as read, go to next unread subject.
9501     (when (null unmark)
9502       ;; Go to next unread subject.
9503       (gnus-summary-next-subject 1 t)))
9504   (gnus-set-mode-line 'summary))
9505
9506 ;; Summary sorting commands
9507
9508 (defun gnus-summary-sort-by-number (&optional reverse)
9509   "Sort the summary buffer by article number.
9510 Argument REVERSE means reverse order."
9511   (interactive "P")
9512   (gnus-summary-sort 'number reverse))
9513
9514 (defun gnus-summary-sort-by-author (&optional reverse)
9515   "Sort the summary buffer by author name alphabetically.
9516 If `case-fold-search' is non-nil, case of letters is ignored.
9517 Argument REVERSE means reverse order."
9518   (interactive "P")
9519   (gnus-summary-sort 'author reverse))
9520
9521 (defun gnus-summary-sort-by-subject (&optional reverse)
9522   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9523 If `case-fold-search' is non-nil, case of letters is ignored.
9524 Argument REVERSE means reverse order."
9525   (interactive "P")
9526   (gnus-summary-sort 'subject reverse))
9527
9528 (defun gnus-summary-sort-by-date (&optional reverse)
9529   "Sort the summary buffer by date.
9530 Argument REVERSE means reverse order."
9531   (interactive "P")
9532   (gnus-summary-sort 'date reverse))
9533
9534 (defun gnus-summary-sort-by-score (&optional reverse)
9535   "Sort the summary buffer by score.
9536 Argument REVERSE means reverse order."
9537   (interactive "P")
9538   (gnus-summary-sort 'score reverse))
9539
9540 (defun gnus-summary-sort-by-lines (&optional reverse)
9541   "Sort the summary buffer by the number of lines.
9542 Argument REVERSE means reverse order."
9543   (interactive "P")
9544   (gnus-summary-sort 'lines reverse))
9545
9546 (defun gnus-summary-sort-by-chars (&optional reverse)
9547   "Sort the summary buffer by article length.
9548 Argument REVERSE means reverse order."
9549   (interactive "P")
9550   (gnus-summary-sort 'chars reverse))
9551
9552 (defun gnus-summary-sort-by-original (&optional reverse)
9553   "Sort the summary buffer using the default sorting method.
9554 Argument REVERSE means reverse order."
9555   (interactive "P")
9556   (let* ((buffer-read-only)
9557          (gnus-summary-prepare-hook nil))
9558     ;; We do the sorting by regenerating the threads.
9559     (gnus-summary-prepare)
9560     ;; Hide subthreads if needed.
9561     (when (and gnus-show-threads gnus-thread-hide-subtree)
9562       (gnus-summary-hide-all-threads))))
9563
9564 (defun gnus-summary-sort (predicate reverse)
9565   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9566   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9567          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9568          (gnus-thread-sort-functions
9569           (if (not reverse)
9570               thread
9571             `(lambda (t1 t2)
9572                (,thread t2 t1))))
9573          (gnus-sort-gathered-threads-function
9574           gnus-thread-sort-functions)
9575          (gnus-article-sort-functions
9576           (if (not reverse)
9577               article
9578             `(lambda (t1 t2)
9579                (,article t2 t1))))
9580          (buffer-read-only)
9581          (gnus-summary-prepare-hook nil))
9582     ;; We do the sorting by regenerating the threads.
9583     (gnus-summary-prepare)
9584     ;; Hide subthreads if needed.
9585     (when (and gnus-show-threads gnus-thread-hide-subtree)
9586       (gnus-summary-hide-all-threads))))
9587
9588 ;; Summary saving commands.
9589
9590 (defun gnus-summary-save-article (&optional n not-saved)
9591   "Save the current article using the default saver function.
9592 If N is a positive number, save the N next articles.
9593 If N is a negative number, save the N previous articles.
9594 If N is nil and any articles have been marked with the process mark,
9595 save those articles instead.
9596 The variable `gnus-default-article-saver' specifies the saver function."
9597   (interactive "P")
9598   (let* ((articles (gnus-summary-work-articles n))
9599          (save-buffer (save-excursion
9600                         (nnheader-set-temp-buffer " *Gnus Save*")))
9601          (num (length articles))
9602          header file)
9603     (dolist (article articles)
9604       (setq header (gnus-summary-article-header article))
9605       (if (not (vectorp header))
9606           ;; This is a pseudo-article.
9607           (if (assq 'name header)
9608               (gnus-copy-file (cdr (assq 'name header)))
9609             (gnus-message 1 "Article %d is unsaveable" article))
9610         ;; This is a real article.
9611         (save-window-excursion
9612           (gnus-summary-select-article t nil nil article))
9613         (save-excursion
9614           (set-buffer save-buffer)
9615           (erase-buffer)
9616           (insert-buffer-substring gnus-original-article-buffer))
9617         (setq file (gnus-article-save save-buffer file num))
9618         (gnus-summary-remove-process-mark article)
9619         (unless not-saved
9620           (gnus-summary-set-saved-mark article))))
9621     (gnus-kill-buffer save-buffer)
9622     (gnus-summary-position-point)
9623     (gnus-set-mode-line 'summary)
9624     n))
9625
9626 (defun gnus-summary-pipe-output (&optional arg)
9627   "Pipe the current article to a subprocess.
9628 If N is a positive number, pipe the N next articles.
9629 If N is a negative number, pipe the N previous articles.
9630 If N is nil and any articles have been marked with the process mark,
9631 pipe those articles instead."
9632   (interactive "P")
9633   (require 'gnus-art)
9634   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9635     (gnus-summary-save-article arg t))
9636   (gnus-configure-windows 'pipe))
9637
9638 (defun gnus-summary-save-article-mail (&optional arg)
9639   "Append the current article to an mail file.
9640 If N is a positive number, save the N next articles.
9641 If N is a negative number, save the N previous articles.
9642 If N is nil and any articles have been marked with the process mark,
9643 save those articles instead."
9644   (interactive "P")
9645   (require 'gnus-art)
9646   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9647     (gnus-summary-save-article arg)))
9648
9649 (defun gnus-summary-save-article-rmail (&optional arg)
9650   "Append the current article to an rmail file.
9651 If N is a positive number, save the N next articles.
9652 If N is a negative number, save the N previous articles.
9653 If N is nil and any articles have been marked with the process mark,
9654 save those articles instead."
9655   (interactive "P")
9656   (require 'gnus-art)
9657   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9658     (gnus-summary-save-article arg)))
9659
9660 (defun gnus-summary-save-article-file (&optional arg)
9661   "Append the current article to a file.
9662 If N is a positive number, save the N next articles.
9663 If N is a negative number, save the N previous articles.
9664 If N is nil and any articles have been marked with the process mark,
9665 save those articles instead."
9666   (interactive "P")
9667   (require 'gnus-art)
9668   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9669     (gnus-summary-save-article arg)))
9670
9671 (defun gnus-summary-write-article-file (&optional arg)
9672   "Write the current article to a file, deleting the previous file.
9673 If N is a positive number, save the N next articles.
9674 If N is a negative number, save the N previous articles.
9675 If N is nil and any articles have been marked with the process mark,
9676 save those articles instead."
9677   (interactive "P")
9678   (require 'gnus-art)
9679   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9680     (gnus-summary-save-article arg)))
9681
9682 (defun gnus-summary-save-article-body-file (&optional arg)
9683   "Append the current article body to a file.
9684 If N is a positive number, save the N next articles.
9685 If N is a negative number, save the N previous articles.
9686 If N is nil and any articles have been marked with the process mark,
9687 save those articles instead."
9688   (interactive "P")
9689   (require 'gnus-art)
9690   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9691     (gnus-summary-save-article arg)))
9692
9693 (defun gnus-summary-pipe-message (program)
9694   "Pipe the current article through PROGRAM."
9695   (interactive "sProgram: ")
9696   (gnus-summary-select-article)
9697   (let ((mail-header-separator ""))
9698     (gnus-eval-in-buffer-window gnus-article-buffer
9699       (save-restriction
9700         (widen)
9701         (let ((start (window-start))
9702               buffer-read-only)
9703           (message-pipe-buffer-body program)
9704           (set-window-start (get-buffer-window (current-buffer)) start))))))
9705
9706 (defun gnus-get-split-value (methods)
9707   "Return a value based on the split METHODS."
9708   (let (split-name method result match)
9709     (when methods
9710       (save-excursion
9711         (set-buffer gnus-original-article-buffer)
9712         (save-restriction
9713           (nnheader-narrow-to-headers)
9714           (while (and methods (not split-name))
9715             (goto-char (point-min))
9716             (setq method (pop methods))
9717             (setq match (car method))
9718             (when (cond
9719                    ((stringp match)
9720                     ;; Regular expression.
9721                     (ignore-errors
9722                       (re-search-forward match nil t)))
9723                    ((gnus-functionp match)
9724                     ;; Function.
9725                     (save-restriction
9726                       (widen)
9727                       (setq result (funcall match gnus-newsgroup-name))))
9728                    ((consp match)
9729                     ;; Form.
9730                     (save-restriction
9731                       (widen)
9732                       (setq result (eval match)))))
9733               (setq split-name (cdr method))
9734               (cond ((stringp result)
9735                      (push (expand-file-name
9736                             result gnus-article-save-directory)
9737                            split-name))
9738                     ((consp result)
9739                      (setq split-name (append result split-name)))))))))
9740     (nreverse split-name)))
9741
9742 (defun gnus-valid-move-group-p (group)
9743   (and (boundp group)
9744        (symbol-name group)
9745        (symbol-value group)
9746        (gnus-get-function (gnus-find-method-for-group
9747                            (symbol-name group)) 'request-accept-article t)))
9748
9749 (defun gnus-read-move-group-name (prompt default articles prefix)
9750   "Read a group name."
9751   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9752          (minibuffer-confirm-incomplete nil) ; XEmacs
9753          (prom
9754           (format "%s %s to:"
9755                   prompt
9756                   (if (> (length articles) 1)
9757                       (format "these %d articles" (length articles))
9758                     "this article")))
9759          (to-newsgroup
9760           (cond
9761            ((null split-name)
9762             (gnus-completing-read default prom
9763                                   gnus-active-hashtb
9764                                   'gnus-valid-move-group-p
9765                                   nil prefix
9766                                   'gnus-group-history))
9767            ((= 1 (length split-name))
9768             (gnus-completing-read (car split-name) prom
9769                                   gnus-active-hashtb
9770                                   'gnus-valid-move-group-p
9771                                   nil nil
9772                                   'gnus-group-history))
9773            (t
9774             (gnus-completing-read nil prom
9775                                   (mapcar (lambda (el) (list el))
9776                                           (nreverse split-name))
9777                                   nil nil nil
9778                                   'gnus-group-history))))
9779          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9780     (when to-newsgroup
9781       (if (or (string= to-newsgroup "")
9782               (string= to-newsgroup prefix))
9783           (setq to-newsgroup default))
9784       (unless to-newsgroup
9785         (error "No group name entered"))
9786       (or (gnus-active to-newsgroup)
9787           (gnus-activate-group to-newsgroup nil nil to-method)
9788           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9789                                      to-newsgroup))
9790               (or (and (gnus-request-create-group to-newsgroup to-method)
9791                        (gnus-activate-group
9792                         to-newsgroup nil nil to-method)
9793                        (gnus-subscribe-group to-newsgroup))
9794                   (error "Couldn't create group %s" to-newsgroup)))
9795           (error "No such group: %s" to-newsgroup)))
9796     to-newsgroup))
9797
9798 (defun gnus-summary-save-parts (type dir n &optional reverse)
9799   "Save parts matching TYPE to DIR.
9800 If REVERSE, save parts that do not match TYPE."
9801   (interactive
9802    (list (read-string "Save parts of type: "
9803                       (or (car gnus-summary-save-parts-type-history)
9804                           gnus-summary-save-parts-default-mime)
9805                       'gnus-summary-save-parts-type-history)
9806          (setq gnus-summary-save-parts-last-directory
9807                (read-file-name "Save to directory: "
9808                                gnus-summary-save-parts-last-directory
9809                                nil t))
9810          current-prefix-arg))
9811   (gnus-summary-iterate n
9812     (let ((gnus-display-mime-function nil)
9813           (gnus-inhibit-treatment t))
9814       (gnus-summary-select-article))
9815     (save-excursion
9816       (set-buffer gnus-article-buffer)
9817       (let ((handles (or gnus-article-mime-handles
9818                          (mm-dissect-buffer) (mm-uu-dissect))))
9819         (when handles
9820           (gnus-summary-save-parts-1 type dir handles reverse)
9821           (unless gnus-article-mime-handles ;; Don't destroy this case.
9822             (mm-destroy-parts handles)))))))
9823
9824 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9825   (if (stringp (car handle))
9826       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9827               (cdr handle))
9828     (when (if reverse
9829               (not (string-match type (mm-handle-media-type handle)))
9830             (string-match type (mm-handle-media-type handle)))
9831       (let ((file (expand-file-name
9832                    (file-name-nondirectory
9833                     (or
9834                      (mail-content-type-get
9835                       (mm-handle-disposition handle) 'filename)
9836                      (concat gnus-newsgroup-name
9837                              "." (number-to-string
9838                                   (cdr gnus-article-current)))))
9839                    dir)))
9840         (unless (file-exists-p file)
9841           (mm-save-part-to-file handle file))))))
9842
9843 ;; Summary extract commands
9844
9845 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9846   (let ((buffer-read-only nil)
9847         (article (gnus-summary-article-number))
9848         after-article b e)
9849     (unless (gnus-summary-goto-subject article)
9850       (error "No such article: %d" article))
9851     (gnus-summary-position-point)
9852     ;; If all commands are to be bunched up on one line, we collect
9853     ;; them here.
9854     (unless gnus-view-pseudos-separately
9855       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9856             files action)
9857         (while ps
9858           (setq action (cdr (assq 'action (car ps))))
9859           (setq files (list (cdr (assq 'name (car ps)))))
9860           (while (and ps (cdr ps)
9861                       (string= (or action "1")
9862                                (or (cdr (assq 'action (cadr ps))) "2")))
9863             (push (cdr (assq 'name (cadr ps))) files)
9864             (setcdr ps (cddr ps)))
9865           (when files
9866             (when (not (string-match "%s" action))
9867               (push " " files))
9868             (push " " files)
9869             (when (assq 'execute (car ps))
9870               (setcdr (assq 'execute (car ps))
9871                       (funcall (if (string-match "%s" action)
9872                                    'format 'concat)
9873                                action
9874                                (mapconcat
9875                                 (lambda (f)
9876                                   (if (equal f " ")
9877                                       f
9878                                     (gnus-quote-arg-for-sh-or-csh f)))
9879                                 files " ")))))
9880           (setq ps (cdr ps)))))
9881     (if (and gnus-view-pseudos (not not-view))
9882         (while pslist
9883           (when (assq 'execute (car pslist))
9884             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9885                                   (eq gnus-view-pseudos 'not-confirm)))
9886           (setq pslist (cdr pslist)))
9887       (save-excursion
9888         (while pslist
9889           (setq after-article (or (cdr (assq 'article (car pslist)))
9890                                   (gnus-summary-article-number)))
9891           (gnus-summary-goto-subject after-article)
9892           (forward-line 1)
9893           (setq b (point))
9894           (insert "    " (file-name-nondirectory
9895                           (cdr (assq 'name (car pslist))))
9896                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9897           (setq e (point))
9898           (forward-line -1)             ; back to `b'
9899           (gnus-add-text-properties
9900            b (1- e) (list 'gnus-number gnus-reffed-article-number
9901                           gnus-mouse-face-prop gnus-mouse-face))
9902           (gnus-data-enter
9903            after-article gnus-reffed-article-number
9904            gnus-unread-mark b (car pslist) 0 (- e b))
9905           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9906           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9907           (setq pslist (cdr pslist)))))))
9908
9909 (defun gnus-pseudos< (p1 p2)
9910   (let ((c1 (cdr (assq 'action p1)))
9911         (c2 (cdr (assq 'action p2))))
9912     (and c1 c2 (string< c1 c2))))
9913
9914 (defun gnus-request-pseudo-article (props)
9915   (cond ((assq 'execute props)
9916          (gnus-execute-command (cdr (assq 'execute props)))))
9917   (let ((gnus-current-article (gnus-summary-article-number)))
9918     (gnus-run-hooks 'gnus-mark-article-hook)))
9919
9920 (defun gnus-execute-command (command &optional automatic)
9921   (save-excursion
9922     (gnus-article-setup-buffer)
9923     (set-buffer gnus-article-buffer)
9924     (setq buffer-read-only nil)
9925     (let ((command (if automatic command
9926                      (read-string "Command: " (cons command 0)))))
9927       (erase-buffer)
9928       (insert "$ " command "\n\n")
9929       (if gnus-view-pseudo-asynchronously
9930           (start-process "gnus-execute" (current-buffer) shell-file-name
9931                          shell-command-switch command)
9932         (call-process shell-file-name nil t nil
9933                       shell-command-switch command)))))
9934
9935 ;; Summary kill commands.
9936
9937 (defun gnus-summary-edit-global-kill (article)
9938   "Edit the \"global\" kill file."
9939   (interactive (list (gnus-summary-article-number)))
9940   (gnus-group-edit-global-kill article))
9941
9942 (defun gnus-summary-edit-local-kill ()
9943   "Edit a local kill file applied to the current newsgroup."
9944   (interactive)
9945   (setq gnus-current-headers (gnus-summary-article-header))
9946   (gnus-group-edit-local-kill
9947    (gnus-summary-article-number) gnus-newsgroup-name))
9948
9949 ;;; Header reading.
9950
9951 (defun gnus-read-header (id &optional header)
9952   "Read the headers of article ID and enter them into the Gnus system."
9953   (let ((group gnus-newsgroup-name)
9954         (gnus-override-method
9955          (or
9956           gnus-override-method
9957           (and (gnus-news-group-p gnus-newsgroup-name)
9958                (car (gnus-refer-article-methods)))))
9959         where)
9960     ;; First we check to see whether the header in question is already
9961     ;; fetched.
9962     (if (stringp id)
9963         ;; This is a Message-ID.
9964         (setq header (or header (gnus-id-to-header id)))
9965       ;; This is an article number.
9966       (setq header (or header (gnus-summary-article-header id))))
9967     (if (and header
9968              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9969         ;; We have found the header.
9970         header
9971       ;; If this is a sparse article, we have to nix out its
9972       ;; previous entry in the thread hashtb.
9973       (when (and header
9974                  (gnus-summary-article-sparse-p (mail-header-number header)))
9975         (let* ((parent (gnus-parent-id (mail-header-references header)))
9976                (thread (and parent (gnus-id-to-thread parent))))
9977           (when thread
9978             (delq (assq header thread) thread))))
9979       ;; We have to really fetch the header to this article.
9980       (save-excursion
9981         (set-buffer nntp-server-buffer)
9982         (when (setq where (gnus-request-head id group))
9983           (nnheader-fold-continuation-lines)
9984           (goto-char (point-max))
9985           (insert ".\n")
9986           (goto-char (point-min))
9987           (insert "211 ")
9988           (princ (cond
9989                   ((numberp id) id)
9990                   ((cdr where) (cdr where))
9991                   (header (mail-header-number header))
9992                   (t gnus-reffed-article-number))
9993                  (current-buffer))
9994           (insert " Article retrieved.\n"))
9995         (if (or (not where)
9996                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9997             ()                          ; Malformed head.
9998           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9999             (when (and (stringp id)
10000                        (not (string= (gnus-group-real-name group)
10001                                      (car where))))
10002               ;; If we fetched by Message-ID and the article came
10003               ;; from a different group, we fudge some bogus article
10004               ;; numbers for this article.
10005               (mail-header-set-number header gnus-reffed-article-number))
10006             (save-excursion
10007               (set-buffer gnus-summary-buffer)
10008               (decf gnus-reffed-article-number)
10009               (gnus-remove-header (mail-header-number header))
10010               (push header gnus-newsgroup-headers)
10011               (setq gnus-current-headers header)
10012               (push (mail-header-number header) gnus-newsgroup-limit)))
10013           header)))))
10014
10015 (defun gnus-remove-header (number)
10016   "Remove header NUMBER from `gnus-newsgroup-headers'."
10017   (if (and gnus-newsgroup-headers
10018            (= number (mail-header-number (car gnus-newsgroup-headers))))
10019       (pop gnus-newsgroup-headers)
10020     (let ((headers gnus-newsgroup-headers))
10021       (while (and (cdr headers)
10022                   (not (= number (mail-header-number (cadr headers)))))
10023         (pop headers))
10024       (when (cdr headers)
10025         (setcdr headers (cddr headers))))))
10026
10027 ;;;
10028 ;;; summary highlights
10029 ;;;
10030
10031 (defun gnus-highlight-selected-summary ()
10032   "Highlight selected article in summary buffer."
10033   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10034   (when gnus-summary-selected-face
10035     (save-excursion
10036       (let* ((beg (progn (beginning-of-line) (point)))
10037              (end (progn (end-of-line) (point)))
10038              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10039              (from (if (get-text-property beg gnus-mouse-face-prop)
10040                        beg
10041                      (or (next-single-property-change
10042                           beg gnus-mouse-face-prop nil end)
10043                          beg)))
10044              (to
10045               (if (= from end)
10046                   (- from 2)
10047                 (or (next-single-property-change
10048                      from gnus-mouse-face-prop nil end)
10049                     end))))
10050         ;; If no mouse-face prop on line we will have to = from = end,
10051         ;; so we highlight the entire line instead.
10052         (when (= (+ to 2) from)
10053           (setq from beg)
10054           (setq to end))
10055         (if gnus-newsgroup-selected-overlay
10056             ;; Move old overlay.
10057             (gnus-move-overlay
10058              gnus-newsgroup-selected-overlay from to (current-buffer))
10059           ;; Create new overlay.
10060           (gnus-overlay-put
10061            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10062            'face gnus-summary-selected-face))))))
10063
10064 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10065 (defun gnus-summary-highlight-line ()
10066   "Highlight current line according to `gnus-summary-highlight'."
10067   (let* ((list gnus-summary-highlight)
10068          (p (point))
10069          (end (progn (end-of-line) (point)))
10070          ;; now find out where the line starts and leave point there.
10071          (beg (progn (beginning-of-line) (point)))
10072          (article (gnus-summary-article-number))
10073          (score (or (cdr (assq (or article gnus-current-article)
10074                                gnus-newsgroup-scored))
10075                     gnus-summary-default-score 0))
10076          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10077          (inhibit-read-only t))
10078     ;; Eval the cars of the lists until we find a match.
10079     (let ((default gnus-summary-default-score))
10080       (while (and list
10081                   (not (eval (caar list))))
10082         (setq list (cdr list))))
10083     (let ((face (cdar list)))
10084       (unless (eq face (get-text-property beg 'face))
10085         (gnus-put-text-property-excluding-characters-with-faces
10086          beg end 'face
10087          (setq face (if (boundp face) (symbol-value face) face)))
10088         (when gnus-summary-highlight-line-function
10089           (funcall gnus-summary-highlight-line-function article face))))
10090     (goto-char p)))
10091
10092 (defun gnus-update-read-articles (group unread &optional compute)
10093   "Update the list of read articles in GROUP."
10094   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10095          (entry (gnus-gethash group gnus-newsrc-hashtb))
10096          (info (nth 2 entry))
10097          (prev 1)
10098          (unread (sort (copy-sequence unread) '<))
10099          read)
10100     (if (or (not info) (not active))
10101         ;; There is no info on this group if it was, in fact,
10102         ;; killed.  Gnus stores no information on killed groups, so
10103         ;; there's nothing to be done.
10104         ;; One could store the information somewhere temporarily,
10105         ;; perhaps...  Hmmm...
10106         ()
10107       ;; Remove any negative articles numbers.
10108       (while (and unread (< (car unread) 0))
10109         (setq unread (cdr unread)))
10110       ;; Remove any expired article numbers
10111       (while (and unread (< (car unread) (car active)))
10112         (setq unread (cdr unread)))
10113       ;; Compute the ranges of read articles by looking at the list of
10114       ;; unread articles.
10115       (while unread
10116         (when (/= (car unread) prev)
10117           (push (if (= prev (1- (car unread))) prev
10118                   (cons prev (1- (car unread))))
10119                 read))
10120         (setq prev (1+ (car unread)))
10121         (setq unread (cdr unread)))
10122       (when (<= prev (cdr active))
10123         (push (cons prev (cdr active)) read))
10124       (setq read (if (> (length read) 1) (nreverse read) read))
10125       (if compute
10126           read
10127         (save-excursion
10128           (let (setmarkundo)
10129             ;; Propagate the read marks to the backend.
10130             (when (gnus-check-backend-function 'request-set-mark group)
10131               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10132                     (add (gnus-remove-from-range read (gnus-info-read info))))
10133                 (when (or add del)
10134                   (unless (gnus-check-group group)
10135                     (error "Can't open server for %s" group))
10136                   (gnus-request-set-mark
10137                    group (delq nil (list (if add (list add 'add '(read)))
10138                                          (if del (list del 'del '(read))))))
10139                   (setq setmarkundo
10140                         `(gnus-request-set-mark
10141                           ,group
10142                           ',(delq nil (list
10143                                        (if del (list del 'add '(read)))
10144                                        (if add (list add 'del '(read))))))))))
10145             (set-buffer gnus-group-buffer)
10146             (gnus-undo-register
10147               `(progn
10148                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10149                  (gnus-info-set-read ',info ',(gnus-info-read info))
10150                  (gnus-get-unread-articles-in-group ',info
10151                                                     (gnus-active ,group))
10152                  (gnus-group-update-group ,group t)
10153                  ,setmarkundo))))
10154         ;; Enter this list into the group info.
10155         (gnus-info-set-read info read)
10156         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10157         (gnus-get-unread-articles-in-group info (gnus-active group))
10158         t))))
10159
10160 (defun gnus-offer-save-summaries ()
10161   "Offer to save all active summary buffers."
10162   (let (buffers)
10163     ;; Go through all buffers and find all summaries.
10164     (dolist (buffer (buffer-list))
10165       (when (and (setq buffer (buffer-name buffer))
10166                  (string-match "Summary" buffer)
10167                  (save-excursion
10168                    (set-buffer buffer)
10169                    ;; We check that this is, indeed, a summary buffer.
10170                    (and (eq major-mode 'gnus-summary-mode)
10171                         ;; Also make sure this isn't bogus.
10172                         gnus-newsgroup-prepared
10173                         ;; Also make sure that this isn't a
10174                         ;; dead summary buffer.
10175                         (not gnus-dead-summary-mode))))
10176         (push buffer buffers)))
10177     ;; Go through all these summary buffers and offer to save them.
10178     (when buffers
10179       (save-excursion
10180         (map-y-or-n-p
10181          "Update summary buffer %s? "
10182          (lambda (buf)
10183            (switch-to-buffer buf)
10184            (gnus-summary-exit))
10185          buffers)))))
10186
10187
10188 ;;; @ for mime-partial
10189 ;;;
10190
10191 (defun gnus-request-partial-message ()
10192   (save-excursion
10193     (let ((number (gnus-summary-article-number))
10194           (group gnus-newsgroup-name)
10195           (mother gnus-article-buffer))
10196       (set-buffer (get-buffer-create " *Partial Article*"))
10197       (erase-buffer)
10198       (setq mime-preview-buffer mother)
10199       (gnus-request-article-this-buffer number group)
10200       (mime-parse-buffer)
10201       )))
10202
10203 (autoload 'mime-combine-message/partial-pieces-automatically
10204   "mime-partial"
10205   "Internal method to combine message/partial messages automatically.")
10206
10207 (mime-add-condition
10208  'action '((type . message)(subtype . partial)
10209            (major-mode . gnus-original-article-mode)
10210            (method . mime-combine-message/partial-pieces-automatically)
10211            (summary-buffer-exp . gnus-summary-buffer)
10212            (request-partial-message-method . gnus-request-partial-message)
10213            ))
10214
10215
10216 ;;; @ for message/rfc822
10217 ;;;
10218
10219 (defun gnus-mime-extract-message/rfc822 (entity situation)
10220   (let (group article num cwin swin cur)
10221     (with-temp-buffer
10222       (mime-insert-entity-content entity)
10223       (setq group (or (cdr (assq 'group situation))
10224                       (completing-read "Group: "
10225                                        gnus-active-hashtb
10226                                        nil
10227                                        (gnus-read-active-file-p)
10228                                        gnus-newsgroup-name))
10229             article (gnus-request-accept-article group)))
10230     (when (and (consp article)
10231                (numberp (setq article (cdr article))))
10232       (setq num (1+ (or (cdr (assq 'number situation)) 0))
10233             cwin (get-buffer-window (current-buffer) t))
10234       (save-window-excursion
10235         (if (setq swin (get-buffer-window gnus-summary-buffer t))
10236             (select-window swin)
10237           (set-buffer gnus-summary-buffer))
10238         (setq cur gnus-current-article)
10239         (forward-line num)
10240         (let (gnus-show-threads)
10241           (gnus-summary-goto-subject article t))
10242         (gnus-summary-clear-mark-forward 1)
10243         (gnus-summary-goto-subject cur))
10244       (when (and cwin (window-frame cwin))
10245         (select-frame (window-frame cwin)))
10246       (when (boundp 'mime-acting-situation-to-override)
10247         (set-alist 'mime-acting-situation-to-override
10248                    'group
10249                    group)
10250         (set-alist 'mime-acting-situation-to-override
10251                    'after-method
10252                    `(progn
10253                       (save-current-buffer
10254                         (set-buffer gnus-group-buffer)
10255                         (gnus-activate-group ,group))
10256                       (gnus-summary-goto-article ,cur
10257                                                  gnus-show-all-headers)))
10258         (set-alist 'mime-acting-situation-to-override
10259                    'number num)))))
10260
10261 (mime-add-condition
10262  'action '((type . message)(subtype . rfc822)
10263            (major-mode . gnus-original-article-mode)
10264            (method . gnus-mime-extract-message/rfc822)
10265            (mode . "extract")
10266            ))
10267
10268 (mime-add-condition
10269  'action '((type . message)(subtype . news)
10270            (major-mode . gnus-original-article-mode)
10271            (method . gnus-mime-extract-message/rfc822)
10272            (mode . "extract")
10273            ))
10274
10275 (defun gnus-mime-extract-multipart (entity situation)
10276   (let ((children (mime-entity-children entity))
10277         mime-acting-situation-to-override
10278         f)
10279     (while children
10280       (mime-play-entity (car children)
10281                         (cons (assq 'mode situation)
10282                               mime-acting-situation-to-override))
10283       (setq children (cdr children)))
10284     (if (setq f (cdr (assq 'after-method
10285                            mime-acting-situation-to-override)))
10286         (eval f)
10287       )))
10288
10289 (mime-add-condition
10290  'action '((type . multipart)
10291            (method . gnus-mime-extract-multipart)
10292            (mode . "extract")
10293            )
10294  'with-default)
10295
10296
10297 ;;; @ end
10298 ;;;
10299
10300 (defun gnus-summary-setup-default-charset ()
10301   "Setup newsgroup default charset."
10302   (if (equal gnus-newsgroup-name "nndraft:drafts")
10303       (setq gnus-newsgroup-charset nil)
10304     (let* ((ignored-charsets
10305             (or gnus-newsgroup-ephemeral-ignored-charsets
10306                 (append
10307                  (and gnus-newsgroup-name
10308                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10309                  gnus-newsgroup-ignored-charsets))))
10310       (setq gnus-newsgroup-charset
10311             (or gnus-newsgroup-ephemeral-charset
10312                 (and gnus-newsgroup-name
10313                      (gnus-parameter-charset gnus-newsgroup-name))
10314                 gnus-default-charset))
10315       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10316            ignored-charsets))))
10317
10318 ;;;
10319 ;;; Mime Commands
10320 ;;;
10321
10322 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10323   "Display the current article buffer fully MIME-buttonized.
10324 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10325 treated as multipart/mixed."
10326   (interactive "P")
10327   (require 'gnus-art)
10328   (let ((gnus-unbuttonized-mime-types nil)
10329         (gnus-mime-display-multipart-as-mixed show-all-parts))
10330     (gnus-summary-show-article)))
10331
10332 (defun gnus-summary-repair-multipart (article)
10333   "Add a Content-Type header to a multipart article without one."
10334   (interactive (list (gnus-summary-article-number)))
10335   (gnus-with-article article
10336     (message-narrow-to-head)
10337     (message-remove-header "Mime-Version")
10338     (goto-char (point-max))
10339     (insert "Mime-Version: 1.0\n")
10340     (widen)
10341     (when (search-forward "\n--" nil t)
10342       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10343         (message-narrow-to-head)
10344         (message-remove-header "Content-Type")
10345         (goto-char (point-max))
10346         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10347                         separator))
10348         (widen))))
10349   (let (gnus-mark-article-hook)
10350     (gnus-summary-select-article t t nil article)))
10351
10352 (defun gnus-summary-toggle-display-buttonized ()
10353   "Toggle the buttonizing of the article buffer."
10354   (interactive)
10355   (require 'gnus-art)
10356   (if (setq gnus-inhibit-mime-unbuttonizing
10357             (not gnus-inhibit-mime-unbuttonizing))
10358       (let ((gnus-unbuttonized-mime-types nil))
10359         (gnus-summary-show-article))
10360     (gnus-summary-show-article)))
10361
10362 ;;;
10363 ;;; Intelli-mouse commmands
10364 ;;;
10365
10366 (defun gnus-wheel-summary-scroll (event)
10367   (interactive "e")
10368   (let ((amount (if (memq 'shift (event-modifiers event))
10369                     (car gnus-wheel-scroll-amount)
10370                   (cdr gnus-wheel-scroll-amount)))
10371         (direction (- (* (static-if (featurep 'xemacs)
10372                              (event-button event)
10373                            (cond ((eq 'mouse-4 (event-basic-type event))
10374                                   4)
10375                                  ((eq 'mouse-5 (event-basic-type event))
10376                                   5)))
10377                          2) 9))
10378         edge)
10379     (gnus-summary-scroll-up (* amount direction))
10380     (when (gnus-eval-in-buffer-window gnus-article-buffer
10381             (save-restriction
10382               (widen)
10383               (and (if (< 0 direction)
10384                        (gnus-article-next-page 0)
10385                      (gnus-article-prev-page 0)
10386                      (bobp))
10387                    (if (setq edge (get-text-property
10388                                    (point-min) 'gnus-wheel-edge))
10389                        (setq edge (* edge direction))
10390                      (setq edge -1))
10391                    (or (plusp edge)
10392                        (let ((buffer-read-only nil)
10393                              (inhibit-read-only t))
10394                          (put-text-property (point-min) (point-max)
10395                                             'gnus-wheel-edge direction)
10396                          nil))
10397                    (or (> edge gnus-wheel-edge-resistance)
10398                        (let ((buffer-read-only nil)
10399                              (inhibit-read-only t))
10400                          (put-text-property (point-min) (point-max)
10401                                             'gnus-wheel-edge
10402                                             (* (1+ edge) direction))
10403                          nil))
10404                    (eq last-command 'gnus-wheel-summary-scroll))))
10405       (gnus-summary-next-article nil nil (minusp direction)))))
10406
10407 (defun gnus-wheel-install ()
10408   "Enable mouse wheel support on summary window."
10409   (when gnus-use-wheel
10410     (let ((keys
10411            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
10412       (dolist (key keys)
10413         (define-key gnus-summary-mode-map key
10414           'gnus-wheel-summary-scroll)))))
10415
10416 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
10417
10418 ;;;
10419 ;;; Traditional PGP commmands
10420 ;;;
10421
10422 (defun gnus-summary-decrypt-article (&optional force)
10423   "Decrypt the current article in traditional PGP way.
10424 This will have permanent effect only in mail groups.
10425 If FORCE is non-nil, allow editing of articles even in read-only
10426 groups."
10427   (interactive "P")
10428   (gnus-summary-select-article t)
10429   (gnus-eval-in-buffer-window gnus-article-buffer
10430     (save-excursion
10431       (save-restriction
10432         (widen)
10433         (goto-char (point-min))
10434         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10435           (error "Not a traditional PGP message!"))
10436         (let ((armor-start (match-beginning 0)))
10437           (if (and (pgg-decrypt-region armor-start (point-max))
10438                    (or force (not (gnus-group-read-only-p))))
10439               (let ((inhibit-read-only t)
10440                     buffer-read-only)
10441                 (delete-region armor-start
10442                                (progn
10443                                  (re-search-forward "^-+END PGP" nil t)
10444                                  (beginning-of-line 2)
10445                                  (point)))
10446                 (insert-buffer-substring pgg-output-buffer))))))))
10447
10448 (defun gnus-summary-verify-article ()
10449   "Verify the current article in traditional PGP way."
10450   (interactive)
10451   (save-excursion
10452     (set-buffer gnus-original-article-buffer)
10453     (goto-char (point-min))
10454     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10455       (error "Not a traditional PGP message!"))
10456     (re-search-forward "^-+END PGP" nil t)
10457     (beginning-of-line 2)
10458     (call-interactively (function pgg-verify-region))))
10459
10460 ;;;
10461 ;;; Generic summary marking commands
10462 ;;;
10463
10464 (defvar gnus-summary-marking-alist
10465   '((read gnus-del-mark "d")
10466     (unread gnus-unread-mark "u")
10467     (ticked gnus-ticked-mark "!")
10468     (dormant gnus-dormant-mark "?")
10469     (expirable gnus-expirable-mark "e"))
10470   "An alist of names/marks/keystrokes.")
10471
10472 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10473 (defvar gnus-summary-mark-map)
10474
10475 (defun gnus-summary-make-all-marking-commands ()
10476   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10477   (dolist (elem gnus-summary-marking-alist)
10478     (apply 'gnus-summary-make-marking-command elem)))
10479
10480 (defun gnus-summary-make-marking-command (name mark keystroke)
10481   (let ((map (make-sparse-keymap)))
10482     (define-key gnus-summary-generic-mark-map keystroke map)
10483     (dolist (lway `((next "next" next nil "n")
10484                     (next-unread "next unread" next t "N")
10485                     (prev "previous" prev nil "p")
10486                     (prev-unread "previous unread" prev t "P")
10487                     (nomove "" nil nil ,keystroke)))
10488       (let ((func (gnus-summary-make-marking-command-1
10489                    mark (car lway) lway name)))
10490         (setq func (eval func))
10491         (define-key map (nth 4 lway) func)))))
10492
10493 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10494   `(defun ,(intern
10495             (format "gnus-summary-put-mark-as-%s%s"
10496                     name (if (eq way 'nomove)
10497                              ""
10498                            (concat "-" (symbol-name way)))))
10499      (n)
10500      ,(format
10501        "Mark the current article as %s%s.
10502 If N, the prefix, then repeat N times.
10503 If N is negative, move in reverse order.
10504 The difference between N and the actual number of articles marked is
10505 returned."
10506        name (car (cdr lway)))
10507      (interactive "p")
10508      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10509
10510 (defun gnus-summary-generic-mark (n mark move unread)
10511   "Mark N articles with MARK."
10512   (unless (eq major-mode 'gnus-summary-mode)
10513     (error "This command can only be used in the summary buffer"))
10514   (gnus-summary-show-thread)
10515   (let ((nummove
10516          (cond
10517           ((eq move 'next) 1)
10518           ((eq move 'prev) -1)
10519           (t 0))))
10520     (if (zerop nummove)
10521         (setq n 1)
10522       (when (< n 0)
10523         (setq n (abs n)
10524               nummove (* -1 nummove))))
10525     (while (and (> n 0)
10526                 (gnus-summary-mark-article nil mark)
10527                 (zerop (gnus-summary-next-subject nummove unread t)))
10528       (setq n (1- n)))
10529     (when (/= 0 n)
10530       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10531     (gnus-summary-recenter)
10532     (gnus-summary-position-point)
10533     (gnus-set-mode-line 'summary)
10534     n))
10535
10536 (defun gnus-summary-insert-articles (articles)
10537   (when (setq articles
10538               (gnus-set-difference articles
10539                                    (mapcar (lambda (h) (mail-header-number h))
10540                                            gnus-newsgroup-headers)))
10541     (setq gnus-newsgroup-headers 
10542           (merge 'list
10543                  gnus-newsgroup-headers
10544                  (gnus-fetch-headers articles)
10545                  'gnus-article-sort-by-number))
10546     ;; Suppress duplicates?
10547     (when gnus-suppress-duplicates
10548       (gnus-dup-suppress-articles))
10549     
10550     ;; We might want to build some more threads first.
10551     (when (and gnus-fetch-old-headers
10552                (eq gnus-headers-retrieved-by 'nov))
10553       (if (eq gnus-fetch-old-headers 'invisible)
10554         (gnus-build-all-threads)
10555         (gnus-build-old-threads)))
10556     ;; Let the Gnus agent mark articles as read.
10557     (when gnus-agent
10558       (gnus-agent-get-undownloaded-list))
10559     ;; Remove list identifiers from subject
10560     (when gnus-list-identifiers
10561       (gnus-summary-remove-list-identifiers))
10562     ;; First and last article in this newsgroup.
10563     (when gnus-newsgroup-headers
10564       (setq gnus-newsgroup-begin
10565             (mail-header-number (car gnus-newsgroup-headers))
10566             gnus-newsgroup-end
10567             (mail-header-number
10568              (gnus-last-element gnus-newsgroup-headers))))
10569     (when gnus-use-scoring
10570       (gnus-possibly-score-headers))))
10571
10572 (defun gnus-summary-insert-old-articles (&optional all)
10573   "Insert all old articles in this group.
10574 If ALL is non-nil, already read articles become readable.
10575 If ALL is a number, fetch this number of articles."
10576   (interactive "P")
10577   (prog1
10578       (let ((old (mapcar 'car gnus-newsgroup-data))
10579             (i (car gnus-newsgroup-active))
10580             older len)
10581         (while (<= i (cdr gnus-newsgroup-active))
10582           (or (memq i old) (push i older))
10583           (incf i))
10584         (setq len (length older))
10585         (cond 
10586          ((null older) nil)
10587          ((numberp all) 
10588           (if (< all len)
10589               (setq older (subseq older 0 all))))
10590          (all nil)
10591          (t
10592           (if (and (numberp gnus-large-newsgroup)
10593                    (> len gnus-large-newsgroup))
10594               (let ((input
10595                      (read-string
10596                       (format
10597                        "How many articles from %s (default %d): "
10598                        (gnus-limit-string 
10599                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10600                        len))))
10601                 (unless (string-match "^[ \t]*$" input) 
10602                   (setq all (string-to-number input))
10603                   (if (< all len)
10604                       (setq older (subseq older 0 all))))))))
10605         (if (not older)
10606             (message "No old news.")
10607           (gnus-summary-insert-articles older)
10608           (gnus-summary-limit (gnus-union older old))))
10609     (gnus-summary-position-point)))
10610
10611 (defun gnus-summary-insert-new-articles ()
10612   "Insert all new articles in this group."
10613   (interactive)
10614   (prog1
10615       (let ((old (mapcar 'car gnus-newsgroup-data))
10616             (old-active gnus-newsgroup-active)
10617             (nnmail-fetched-sources (list t))
10618             i new)
10619         (setq gnus-newsgroup-active 
10620               (gnus-activate-group gnus-newsgroup-name 'scan))
10621         (setq i (1+ (cdr old-active)))
10622         (while (<= i (cdr gnus-newsgroup-active))
10623           (push i new)
10624           (incf i))
10625         (if (not new)
10626             (message "No gnus is bad news.")
10627           (setq new (nreverse new))
10628           (gnus-summary-insert-articles new)
10629           (setq gnus-newsgroup-unreads
10630                 (append gnus-newsgroup-unreads new))
10631           (gnus-summary-limit (gnus-union old new))))
10632     (gnus-summary-position-point)))
10633
10634 (gnus-summary-make-all-marking-commands)
10635
10636 (gnus-ems-redefine)
10637
10638 (provide 'gnus-sum)
10639
10640 (run-hooks 'gnus-sum-load-hook)
10641
10642 ;;; gnus-sum.el ends here