* gnus-sum.el (gnus-dependencies-add-header): If replaced, don't
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mm-decode)
39 (require 'nnoo)
40
41 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
42 (autoload 'gnus-cache-write-active "gnus-cache")
43 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
44 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
45 (autoload 'mm-uu-dissect "mm-uu")
46
47 (defcustom gnus-kill-summary-on-exit t
48   "*If non-nil, kill the summary buffer when you exit from it.
49 If nil, the summary will become a \"*Dead Summary*\" buffer, and
50 it will be killed sometime later."
51   :group 'gnus-summary-exit
52   :type 'boolean)
53
54 (defcustom gnus-fetch-old-headers nil
55   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
56 If an unread article in the group refers to an older, already read (or
57 just marked as read) article, the old article will not normally be
58 displayed in the Summary buffer.  If this variable is non-nil, Gnus
59 will attempt to grab the headers to the old articles, and thereby
60 build complete threads.  If it has the value `some', only enough
61 headers to connect otherwise loose threads will be displayed.  This
62 variable can also be a number.  In that case, no more than that number
63 of old headers will be fetched.  If it has the value `invisible', all
64 old headers will be fetched, but none will be displayed.
65
66 The server has to support NOV for any of this to work."
67   :group 'gnus-thread
68   :type '(choice (const :tag "off" nil)
69                  (const some)
70                  number
71                  (sexp :menu-tag "other" t)))
72
73 (defcustom gnus-refer-thread-limit 200
74   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
75 If t, fetch all the available old headers."
76   :group 'gnus-thread
77   :type '(choice number
78                  (sexp :menu-tag "other" t)))
79
80 (defcustom gnus-summary-make-false-root 'adopt
81   "*nil means that Gnus won't gather loose threads.
82 If the root of a thread has expired or been read in a previous
83 session, the information necessary to build a complete thread has been
84 lost.  Instead of having many small sub-threads from this original thread
85 scattered all over the summary buffer, Gnus can gather them.
86
87 If non-nil, Gnus will try to gather all loose sub-threads from an
88 original thread into one large thread.
89
90 If this variable is non-nil, it should be one of `none', `adopt',
91 `dummy' or `empty'.
92
93 If this variable is `none', Gnus will not make a false root, but just
94 present the sub-threads after another.
95 If this variable is `dummy', Gnus will create a dummy root that will
96 have all the sub-threads as children.
97 If this variable is `adopt', Gnus will make one of the \"children\"
98 the parent and mark all the step-children as such.
99 If this variable is `empty', the \"children\" are printed with empty
100 subject fields.  (Or rather, they will be printed with a string
101 given by the `gnus-summary-same-subject' variable.)"
102   :group 'gnus-thread
103   :type '(choice (const :tag "off" nil)
104                  (const none)
105                  (const dummy)
106                  (const adopt)
107                  (const empty)))
108
109 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
110   "*A regexp to match subjects to be excluded from loose thread gathering.
111 As loose thread gathering is done on subjects only, that means that
112 there can be many false gatherings performed.  By rooting out certain
113 common subjects, gathering might become saner."
114   :group 'gnus-thread
115   :type 'regexp)
116
117 (defcustom gnus-summary-gather-subject-limit nil
118   "*Maximum length of subject comparisons when gathering loose threads.
119 Use nil to compare full subjects.  Setting this variable to a low
120 number will help gather threads that have been corrupted by
121 newsreaders chopping off subject lines, but it might also mean that
122 unrelated articles that have subject that happen to begin with the
123 same few characters will be incorrectly gathered.
124
125 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
126 comparing subjects."
127   :group 'gnus-thread
128   :type '(choice (const :tag "off" nil)
129                  (const fuzzy)
130                  (sexp :menu-tag "on" t)))
131
132 (defcustom gnus-simplify-subject-functions nil
133   "List of functions taking a string argument that simplify subjects.
134 The functions are applied recursively.
135
136 Useful functions to put in this list include: `gnus-simplify-subject-re',
137 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
138   :group 'gnus-thread
139   :type '(repeat function))
140
141 (defcustom gnus-simplify-ignored-prefixes nil
142   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
143   :group 'gnus-thread
144   :type '(choice (const :tag "off" nil)
145                  regexp))
146
147 (defcustom gnus-build-sparse-threads nil
148   "*If non-nil, fill in the gaps in threads.
149 If `some', only fill in the gaps that are needed to tie loose threads
150 together.  If `more', fill in all leaf nodes that Gnus can find.  If
151 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
152   :group 'gnus-thread
153   :type '(choice (const :tag "off" nil)
154                  (const some)
155                  (const more)
156                  (sexp :menu-tag "all" t)))
157
158 (defcustom gnus-summary-thread-gathering-function
159   'gnus-gather-threads-by-subject
160   "*Function used for gathering loose threads.
161 There are two pre-defined functions: `gnus-gather-threads-by-subject',
162 which only takes Subjects into consideration; and
163 `gnus-gather-threads-by-references', which compared the References
164 headers of the articles to find matches."
165   :group 'gnus-thread
166   :type '(radio (function-item gnus-gather-threads-by-subject)
167                 (function-item gnus-gather-threads-by-references)
168                 (function :tag "other")))
169
170 (defcustom gnus-summary-same-subject ""
171   "*String indicating that the current article has the same subject as the previous.
172 This variable will only be used if the value of
173 `gnus-summary-make-false-root' is `empty'."
174   :group 'gnus-summary-format
175   :type 'string)
176
177 (defcustom gnus-summary-goto-unread t
178   "*If t, many commands will go to the next unread article.
179 This applies to marking commands as well as other commands that
180 \"naturally\" select the next article, like, for instance, `SPC' at
181 the end of an article.
182
183 If nil, the marking commands do NOT go to the next unread article
184 (they go to the next article instead).  If `never', commands that
185 usually go to the next unread article, will go to the next article,
186 whether it is read or not."
187   :group 'gnus-summary-marks
188   :link '(custom-manual "(gnus)Setting Marks")
189   :type '(choice (const :tag "off" nil)
190                  (const never)
191                  (sexp :menu-tag "on" t)))
192
193 (defcustom gnus-summary-default-score 0
194   "*Default article score level.
195 All scores generated by the score files will be added to this score.
196 If this variable is nil, scoring will be disabled."
197   :group 'gnus-score-default
198   :type '(choice (const :tag "disable")
199                  integer))
200
201 (defcustom gnus-summary-default-high-score 0
202   "*Default threshold for a high scored article.
203 An article will be highlighted as high scored if its score is greater
204 than this score."
205   :group 'gnus-score-default
206   :type 'integer)
207
208 (defcustom gnus-summary-default-low-score 0
209   "*Default threshold for a low scored article.
210 An article will be highlighted as low scored if its score is smaller
211 than this score."
212   :group 'gnus-score-default
213   :type 'integer)
214
215 (defcustom gnus-summary-zcore-fuzz 0
216   "*Fuzziness factor for the zcore in the summary buffer.
217 Articles with scores closer than this to `gnus-summary-default-score'
218 will not be marked."
219   :group 'gnus-summary-format
220   :type 'integer)
221
222 (defcustom gnus-simplify-subject-fuzzy-regexp nil
223   "*Strings to be removed when doing fuzzy matches.
224 This can either be a regular expression or list of regular expressions
225 that will be removed from subject strings if fuzzy subject
226 simplification is selected."
227   :group 'gnus-thread
228   :type '(repeat regexp))
229
230 (defcustom gnus-show-threads t
231   "*If non-nil, display threads in summary mode."
232   :group 'gnus-thread
233   :type 'boolean)
234
235 (defcustom gnus-thread-hide-subtree nil
236   "*If non-nil, hide all threads initially.
237 This can be a predicate specifier which says which threads to hide.
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 non-nil, select the article under point.
282 Which article this is is controlled by the `gnus-auto-select-subject'
283 variable.
284
285 If you want to prevent automatic selection of articles in some
286 newsgroups, set the variable to nil in `gnus-select-group-hook'."
287   :group 'gnus-group-select
288   :type '(choice (const :tag "none" nil)
289                  (sexp :menu-tag "first" t)))
290
291 (defcustom gnus-auto-select-subject 'unread
292   "*Says what subject to place under point when entering a group.
293
294 This variable can either be the symbols `first' (place point on the
295 first subject), `unread' (place point on the subject line of the first
296 unread article), `best' (place point on the subject line of the
297 higest-scored article), `unseen' (place point on the subject line of
298 the first unseen article), 'unseen-or-unread' (place point on the subject
299 line of the first unseen article or, if all article have been seen, on the
300 subject line of the first unread article), or a function to be called to
301 place point on some subject line."
302   :group 'gnus-group-select
303   :type '(choice (const best)
304                  (const unread)
305                  (const first)
306                  (const unseen)
307                  (const unseen-or-unread)))
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-move-split-methods nil
376   "*Variable used to suggest where articles are to be moved to.
377 It uses the same syntax as the `gnus-split-methods' variable.
378 However, whereas `gnus-split-methods' specifies file names as targets,
379 this variable specifies group names."
380   :group 'gnus-summary-mail
381   :type '(repeat (choice (list :value (fun) function)
382                          (cons :value ("" "") regexp (repeat string))
383                          (sexp :value nil))))
384
385 (defcustom gnus-unread-mark ?           ;Whitespace
386   "*Mark used for unread articles."
387   :group 'gnus-summary-marks
388   :type 'character)
389
390 (defcustom gnus-ticked-mark ?!
391   "*Mark used for ticked articles."
392   :group 'gnus-summary-marks
393   :type 'character)
394
395 (defcustom gnus-dormant-mark ??
396   "*Mark used for dormant articles."
397   :group 'gnus-summary-marks
398   :type 'character)
399
400 (defcustom gnus-del-mark ?r
401   "*Mark used for del'd articles."
402   :group 'gnus-summary-marks
403   :type 'character)
404
405 (defcustom gnus-read-mark ?R
406   "*Mark used for read articles."
407   :group 'gnus-summary-marks
408   :type 'character)
409
410 (defcustom gnus-expirable-mark ?E
411   "*Mark used for expirable articles."
412   :group 'gnus-summary-marks
413   :type 'character)
414
415 (defcustom gnus-killed-mark ?K
416   "*Mark used for killed articles."
417   :group 'gnus-summary-marks
418   :type 'character)
419
420 (defcustom gnus-souped-mark ?F
421   "*Mark used for souped articles."
422   :group 'gnus-summary-marks
423   :type 'character)
424
425 (defcustom gnus-kill-file-mark ?X
426   "*Mark used for articles killed by kill files."
427   :group 'gnus-summary-marks
428   :type 'character)
429
430 (defcustom gnus-low-score-mark ?Y
431   "*Mark used for articles with a low score."
432   :group 'gnus-summary-marks
433   :type 'character)
434
435 (defcustom gnus-catchup-mark ?C
436   "*Mark used for articles that are caught up."
437   :group 'gnus-summary-marks
438   :type 'character)
439
440 (defcustom gnus-replied-mark ?A
441   "*Mark used for articles that have been replied to."
442   :group 'gnus-summary-marks
443   :type 'character)
444
445 (defcustom gnus-forwarded-mark ?F
446   "*Mark used for articles that have been forwarded."
447   :group 'gnus-summary-marks
448   :type 'character)
449
450 (defcustom gnus-recent-mark ?N
451   "*Mark used for articles that are recent."
452   :group 'gnus-summary-marks
453   :type 'character)
454
455 (defcustom gnus-cached-mark ?*
456   "*Mark used for articles that are in the cache."
457   :group 'gnus-summary-marks
458   :type 'character)
459
460 (defcustom gnus-saved-mark ?S
461   "*Mark used for articles that have been saved."
462   :group 'gnus-summary-marks
463   :type 'character)
464
465 (defcustom gnus-unseen-mark ?.
466   "*Mark used for articles that haven't been seen."
467   :group 'gnus-summary-marks
468   :type 'character)
469
470 (defcustom gnus-no-mark ?               ;Whitespace
471   "*Mark used for articles that have no other secondary mark."
472   :group 'gnus-summary-marks
473   :type 'character)
474
475 (defcustom gnus-ancient-mark ?O
476   "*Mark used for ancient articles."
477   :group 'gnus-summary-marks
478   :type 'character)
479
480 (defcustom gnus-sparse-mark ?Q
481   "*Mark used for sparsely reffed articles."
482   :group 'gnus-summary-marks
483   :type 'character)
484
485 (defcustom gnus-canceled-mark ?G
486   "*Mark used for canceled articles."
487   :group 'gnus-summary-marks
488   :type 'character)
489
490 (defcustom gnus-duplicate-mark ?M
491   "*Mark used for duplicate articles."
492   :group 'gnus-summary-marks
493   :type 'character)
494
495 (defcustom gnus-undownloaded-mark ?@
496   "*Mark used for articles that weren't downloaded."
497   :group 'gnus-summary-marks
498   :type 'character)
499
500 (defcustom gnus-downloadable-mark ?%
501   "*Mark used for articles that are to be downloaded."
502   :group 'gnus-summary-marks
503   :type 'character)
504
505 (defcustom gnus-unsendable-mark ?=
506   "*Mark used for articles that won't be sent."
507   :group 'gnus-summary-marks
508   :type 'character)
509
510 (defcustom gnus-score-over-mark ?+
511   "*Score mark used for articles with high scores."
512   :group 'gnus-summary-marks
513   :type 'character)
514
515 (defcustom gnus-score-below-mark ?-
516   "*Score mark used for articles with low scores."
517   :group 'gnus-summary-marks
518   :type 'character)
519
520 (defcustom gnus-empty-thread-mark ?     ;Whitespace
521   "*There is no thread under the article."
522   :group 'gnus-summary-marks
523   :type 'character)
524
525 (defcustom gnus-not-empty-thread-mark ?=
526   "*There is a thread under the article."
527   :group 'gnus-summary-marks
528   :type 'character)
529
530 (defcustom gnus-view-pseudo-asynchronously nil
531   "*If non-nil, Gnus will view pseudo-articles asynchronously."
532   :group 'gnus-extract-view
533   :type 'boolean)
534
535 (defcustom gnus-auto-expirable-marks
536   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
537         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
538         gnus-souped-mark gnus-duplicate-mark)
539   "*The list of marks converted into expiration if a group is auto-expirable."
540   :version "21.1"
541   :group 'gnus-summary
542   :type '(repeat character))
543
544 (defcustom gnus-inhibit-user-auto-expire t
545   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
546   :version "21.1"
547   :group 'gnus-summary
548   :type 'boolean)
549
550 (defcustom gnus-view-pseudos nil
551   "*If `automatic', pseudo-articles will be viewed automatically.
552 If `not-confirm', pseudos will be viewed automatically, and the user
553 will not be asked to confirm the command."
554   :group 'gnus-extract-view
555   :type '(choice (const :tag "off" nil)
556                  (const automatic)
557                  (const not-confirm)))
558
559 (defcustom gnus-view-pseudos-separately t
560   "*If non-nil, one pseudo-article will be created for each file to be viewed.
561 If nil, all files that use the same viewing command will be given as a
562 list of parameters to that command."
563   :group 'gnus-extract-view
564   :type 'boolean)
565
566 (defcustom gnus-insert-pseudo-articles t
567   "*If non-nil, insert pseudo-articles when decoding articles."
568   :group 'gnus-extract-view
569   :type 'boolean)
570
571 (defcustom gnus-summary-dummy-line-format
572   "  %(:                          :%) %S\n"
573   "*The format specification for the dummy roots in the summary buffer.
574 It works along the same lines as a normal formatting string,
575 with some simple extensions.
576
577 %S  The subject
578
579 General format specifiers can also be used.
580 See (gnus)Formatting Variables."
581   :link '(custom-manual "(gnus)Formatting Variables")
582   :group 'gnus-threading
583   :type 'string)
584
585 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
586   "*The format specification for the summary mode line.
587 It works along the same lines as a normal formatting string,
588 with some simple extensions:
589
590 %G  Group name
591 %p  Unprefixed group name
592 %A  Current article number
593 %z  Current article score
594 %V  Gnus version
595 %U  Number of unread articles in the group
596 %e  Number of unselected articles in the group
597 %Z  A string with unread/unselected article counts
598 %g  Shortish group name
599 %S  Subject of the current article
600 %u  User-defined spec
601 %s  Current score file name
602 %d  Number of dormant articles
603 %r  Number of articles that have been marked as read in this session
604 %E  Number of articles expunged by the score files"
605   :group 'gnus-summary-format
606   :type 'string)
607
608 (defcustom gnus-list-identifiers nil
609   "Regexp that matches list identifiers to be removed from subject.
610 This can also be a list of regexps."
611   :version "21.1"
612   :group 'gnus-summary-format
613   :group 'gnus-article-hiding
614   :type '(choice (const :tag "none" nil)
615                  (regexp :value ".*")
616                  (repeat :value (".*") regexp)))
617
618 (defcustom gnus-summary-mark-below 0
619   "*Mark all articles with a score below this variable as read.
620 This variable is local to each summary buffer and usually set by the
621 score file."
622   :group 'gnus-score-default
623   :type 'integer)
624
625 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
626   "*List of functions used for sorting articles in the summary buffer.
627
628 Each function takes two articles and returns non-nil if the first
629 article should be sorted before the other.  If you use more than one
630 function, the primary sort function should be the last.  You should
631 probably always include `gnus-article-sort-by-number' in the list of
632 sorting functions -- preferably first.  Also note that sorting by date
633 is often much slower than sorting by number, and the sorting order is
634 very similar.  (Sorting by date means sorting by the time the message
635 was sent, sorting by number means sorting by arrival time.)
636
637 Ready-made functions include `gnus-article-sort-by-number',
638 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
639 `gnus-article-sort-by-date' and `gnus-article-sort-by-score'.
640
641 When threading is turned on, the variable `gnus-thread-sort-functions'
642 controls how articles are sorted."
643   :group 'gnus-summary-sort
644   :type '(repeat (choice (function-item gnus-article-sort-by-number)
645                          (function-item gnus-article-sort-by-author)
646                          (function-item gnus-article-sort-by-subject)
647                          (function-item gnus-article-sort-by-date)
648                          (function-item gnus-article-sort-by-score)
649                          (function :tag "other"))))
650
651 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
652   "*List of functions used for sorting threads in the summary buffer.
653 By default, threads are sorted by article number.
654
655 Each function takes two threads and returns non-nil if the first
656 thread should be sorted before the other.  If you use more than one
657 function, the primary sort function should be the last.  You should
658 probably always include `gnus-thread-sort-by-number' in the list of
659 sorting functions -- preferably first.  Also note that sorting by date
660 is often much slower than sorting by number, and the sorting order is
661 very similar.  (Sorting by date means sorting by the time the message
662 was sent, sorting by number means sorting by arrival time.)
663
664 Ready-made functions include `gnus-thread-sort-by-number',
665 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
666 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
667 `gnus-thread-sort-by-most-recent-number',
668 `gnus-thread-sort-by-most-recent-date', and
669 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
670
671 When threading is turned off, the variable
672 `gnus-article-sort-functions' controls how articles are sorted."
673   :group 'gnus-summary-sort
674   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
675                          (function-item gnus-thread-sort-by-author)
676                          (function-item gnus-thread-sort-by-subject)
677                          (function-item gnus-thread-sort-by-date)
678                          (function-item gnus-thread-sort-by-score)
679                          (function-item gnus-thread-sort-by-total-score)
680                          (function :tag "other"))))
681
682 (defcustom gnus-thread-score-function '+
683   "*Function used for calculating the total score of a thread.
684
685 The function is called with the scores of the article and each
686 subthread and should then return the score of the thread.
687
688 Some functions you can use are `+', `max', or `min'."
689   :group 'gnus-summary-sort
690   :type 'function)
691
692 (defcustom gnus-summary-expunge-below nil
693   "All articles that have a score less than this variable will be expunged.
694 This variable is local to the summary buffers."
695   :group 'gnus-score-default
696   :type '(choice (const :tag "off" nil)
697                  integer))
698
699 (defcustom gnus-thread-expunge-below nil
700   "All threads that have a total score less than this variable will be expunged.
701 See `gnus-thread-score-function' for en explanation of what a
702 \"thread score\" is.
703
704 This variable is local to the summary buffers."
705   :group 'gnus-threading
706   :group 'gnus-score-default
707   :type '(choice (const :tag "off" nil)
708                  integer))
709
710 (defcustom gnus-summary-mode-hook nil
711   "*A hook for Gnus summary mode.
712 This hook is run before any variables are set in the summary buffer."
713   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
714   :group 'gnus-summary-various
715   :type 'hook)
716
717 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
718 (when (featurep 'xemacs)
719   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
720   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
721   (add-hook 'gnus-summary-mode-hook
722             'gnus-xmas-switch-horizontal-scrollbar-off))
723
724 (defcustom gnus-summary-menu-hook nil
725   "*Hook run after the creation of the summary mode menu."
726   :group 'gnus-summary-visual
727   :type 'hook)
728
729 (defcustom gnus-summary-exit-hook nil
730   "*A hook called on exit from the summary buffer.
731 It will be called with point in the group buffer."
732   :group 'gnus-summary-exit
733   :type 'hook)
734
735 (defcustom gnus-summary-prepare-hook nil
736   "*A hook called after the summary buffer has been generated.
737 If you want to modify the summary buffer, you can use this hook."
738   :group 'gnus-summary-various
739   :type 'hook)
740
741 (defcustom gnus-summary-prepared-hook nil
742   "*A hook called as the last thing after the summary buffer has been generated."
743   :group 'gnus-summary-various
744   :type 'hook)
745
746 (defcustom gnus-summary-generate-hook nil
747   "*A hook run just before generating the summary buffer.
748 This hook is commonly used to customize threading variables and the
749 like."
750   :group 'gnus-summary-various
751   :type 'hook)
752
753 (defcustom gnus-select-group-hook nil
754   "*A hook called when a newsgroup is selected.
755
756 If you'd like to simplify subjects like the
757 `gnus-summary-next-same-subject' command does, you can use the
758 following hook:
759
760  (add-hook gnus-select-group-hook
761            (lambda ()
762              (mapcar (lambda (header)
763                        (mail-header-set-subject
764                         header
765                         (gnus-simplify-subject
766                          (mail-header-subject header) 're-only)))
767                      gnus-newsgroup-headers)))"
768   :group 'gnus-group-select
769   :type 'hook)
770
771 (defcustom gnus-select-article-hook nil
772   "*A hook called when an article is selected."
773   :group 'gnus-summary-choose
774   :type 'hook)
775
776 (defcustom gnus-visual-mark-article-hook
777   (list 'gnus-highlight-selected-summary)
778   "*Hook run after selecting an article in the summary buffer.
779 It is meant to be used for highlighting the article in some way.  It
780 is not run if `gnus-visual' is nil."
781   :group 'gnus-summary-visual
782   :type 'hook)
783
784 (defcustom gnus-parse-headers-hook nil
785   "*A hook called before parsing the headers."
786   :group 'gnus-various
787   :type 'hook)
788
789 (defcustom gnus-exit-group-hook nil
790   "*A hook called when exiting summary mode.
791 This hook is not called from the non-updating exit commands like `Q'."
792   :group 'gnus-various
793   :type 'hook)
794
795 (defcustom gnus-summary-update-hook
796   (list 'gnus-summary-highlight-line)
797   "*A hook called when a summary line is changed.
798 The hook will not be called if `gnus-visual' is nil.
799
800 The default function `gnus-summary-highlight-line' will
801 highlight the line according to the `gnus-summary-highlight'
802 variable."
803   :group 'gnus-summary-visual
804   :type 'hook)
805
806 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
807   "*A hook called when an article is selected for the first time.
808 The hook is intended to mark an article as read (or unread)
809 automatically when it is selected."
810   :group 'gnus-summary-choose
811   :type 'hook)
812
813 (defcustom gnus-group-no-more-groups-hook nil
814   "*A hook run when returning to group mode having no more (unread) groups."
815   :group 'gnus-group-select
816   :type 'hook)
817
818 (defcustom gnus-ps-print-hook nil
819   "*A hook run before ps-printing something from Gnus."
820   :group 'gnus-summary
821   :type 'hook)
822
823 (defcustom gnus-summary-display-arrow
824   (and (fboundp 'display-graphic-p)
825        (display-graphic-p))
826   "*If non-nil, display an arrow highlighting the current article."
827   :version "21.1"
828   :group 'gnus-summary
829   :type 'boolean)
830
831 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
832   "Face used for highlighting the current article in the summary buffer."
833   :group 'gnus-summary-visual
834   :type 'face)
835
836 (defcustom gnus-summary-highlight
837   '(((= mark gnus-canceled-mark)
838      . gnus-summary-cancelled-face)
839     ((and (> score default-high)
840           (or (= mark gnus-dormant-mark)
841               (= mark gnus-ticked-mark)))
842      . gnus-summary-high-ticked-face)
843     ((and (< score default-low)
844           (or (= mark gnus-dormant-mark)
845               (= mark gnus-ticked-mark)))
846      . gnus-summary-low-ticked-face)
847     ((or (= mark gnus-dormant-mark)
848          (= mark gnus-ticked-mark))
849      . gnus-summary-normal-ticked-face)
850     ((and (> score default-high) (= mark gnus-ancient-mark))
851      . gnus-summary-high-ancient-face)
852     ((and (< score default-low) (= mark gnus-ancient-mark))
853      . gnus-summary-low-ancient-face)
854     ((= mark gnus-ancient-mark)
855      . gnus-summary-normal-ancient-face)
856     ((and (> score default-high) (= mark gnus-unread-mark))
857      . gnus-summary-high-unread-face)
858     ((and (< score default-low) (= mark gnus-unread-mark))
859      . gnus-summary-low-unread-face)
860     ((= mark gnus-unread-mark)
861      . gnus-summary-normal-unread-face)
862     ((and (> score default-high) (memq mark (list gnus-downloadable-mark
863                                                   gnus-undownloaded-mark)))
864      . gnus-summary-high-unread-face)
865     ((and (< score default-low) (memq mark (list gnus-downloadable-mark
866                                                  gnus-undownloaded-mark)))
867      . gnus-summary-low-unread-face)
868     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
869           (memq article gnus-newsgroup-unreads))
870      . gnus-summary-normal-unread-face)
871     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
872      . gnus-summary-normal-read-face)
873     ((> score default-high)
874      . gnus-summary-high-read-face)
875     ((< score default-low)
876      . gnus-summary-low-read-face)
877     (t
878      . gnus-summary-normal-read-face))
879   "*Controls the highlighting of summary buffer lines.
880
881 A list of (FORM . FACE) pairs.  When deciding how a a particular
882 summary line should be displayed, each form is evaluated.  The content
883 of the face field after the first true form is used.  You can change
884 how those summary lines are displayed, by editing the face field.
885
886 You can use the following variables in the FORM field.
887
888 score:        The article's score
889 default:      The default article score.
890 default-high: The default score for high scored articles.
891 default-low:  The default score for low scored articles.
892 below:        The score below which articles are automatically marked as read.
893 mark:         The articles mark."
894   :group 'gnus-summary-visual
895   :type '(repeat (cons (sexp :tag "Form" nil)
896                        face)))
897
898 (defcustom gnus-alter-header-function nil
899   "Function called to allow alteration of article header structures.
900 The function is called with one parameter, the article header vector,
901 which it may alter in any way.")
902
903 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
904   "Variable that says which function should be used to decode a string with encoded words.")
905
906 (defcustom gnus-extra-headers '(To Newsgroups)
907   "*Extra headers to parse."
908   :version "21.1"
909   :group 'gnus-summary
910   :type '(repeat symbol))
911
912 (defcustom gnus-ignored-from-addresses
913   (and user-mail-address (regexp-quote user-mail-address))
914   "*Regexp of From headers that may be suppressed in favor of To headers."
915   :version "21.1"
916   :group 'gnus-summary
917   :type 'regexp)
918
919 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
920   "List of charsets that should be ignored.
921 When these charsets are used in the \"charset\" parameter, the
922 default charset will be used instead."
923   :version "21.1"
924   :type '(repeat symbol)
925   :group 'gnus-charset)
926
927 (gnus-define-group-parameter
928  ignored-charsets
929  :type list
930  :function-document
931  "Return the ignored charsets of GROUP."
932  :variable gnus-group-ignored-charsets-alist
933  :variable-default
934  '(("alt\\.chinese\\.text" iso-8859-1))
935  :variable-document
936  "Alist of regexps (to match group names) and charsets that should be ignored.
937 When these charsets are used in the \"charset\" parameter, the
938 default charset will be used instead."
939  :variable-group gnus-charset
940  :variable-type '(repeat (cons (regexp :tag "Group")
941                                (repeat symbol)))
942  :parameter-type '(choice :tag "Ignored charsets"
943                           :value nil
944                           (repeat (symbol)))
945  :parameter-document       "\
946 List of charsets that should be ignored.
947
948 When these charsets are used in the \"charset\" parameter, the
949 default charset will be used instead.")
950
951 (defcustom gnus-group-highlight-words-alist nil
952   "Alist of group regexps and highlight regexps.
953 This variable uses the same syntax as `gnus-emphasis-alist'."
954   :version "21.1"
955   :type '(repeat (cons (regexp :tag "Group")
956                        (repeat (list (regexp :tag "Highlight regexp")
957                                      (number :tag "Group for entire word" 0)
958                                      (number :tag "Group for displayed part" 0)
959                                      (symbol :tag "Face"
960                                              gnus-emphasis-highlight-words)))))
961   :group 'gnus-summary-visual)
962
963 (defcustom gnus-summary-show-article-charset-alist
964   nil
965   "Alist of number and charset.
966 The article will be shown with the charset corresponding to the
967 numbered argument.
968 For example: ((1 . cn-gb-2312) (2 . big5))."
969   :version "21.1"
970   :type '(repeat (cons (number :tag "Argument" 1)
971                        (symbol :tag "Charset")))
972   :group 'gnus-charset)
973
974 (defcustom gnus-preserve-marks t
975   "Whether marks are preserved when moving, copying and respooling messages."
976   :version "21.1"
977   :type 'boolean
978   :group 'gnus-summary-marks)
979
980 (defcustom gnus-alter-articles-to-read-function nil
981   "Function to be called to alter the list of articles to be selected."
982   :type '(choice (const nil) function)
983   :group 'gnus-summary)
984
985 (defcustom gnus-orphan-score nil
986   "*All orphans get this score added.  Set in the score file."
987   :group 'gnus-score-default
988   :type '(choice (const nil)
989                  integer))
990
991 (defcustom gnus-summary-save-parts-default-mime "image/.*"
992   "*A regexp to match MIME parts when saving multiple parts of a message
993 with gnus-summary-save-parts (X m). This regexp will be used by default
994 when prompting the user for which type of files to save."
995   :group 'gnus-summary
996   :type 'regexp)
997
998
999 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1000   "*A regexp to match MIME parts when saving multiple parts of a message
1001 with gnus-summary-save-parts (X m). This regexp will be used by default
1002 when prompting the user for which type of files to save."
1003   :group 'gnus-summary
1004   :type 'regexp)
1005
1006 (defcustom gnus-read-all-available-headers nil
1007   "Whether Gnus should parse all headers made available to it.
1008 This is mostly relevant for slow backends where the user may
1009 wish to widen the summary buffer to include all headers
1010 that were fetched.  Say, for nnultimate groups."
1011   :group 'gnus-summary
1012   :type '(choice boolean regexp))
1013
1014 (defcustom gnus-summary-muttprint-program "muttprint"
1015   "Command (and optional arguments) used to run Muttprint."
1016   :version "21.3"
1017   :group 'gnus-summary
1018   :type 'string)
1019
1020 (defcustom gnus-article-loose-mime nil
1021   "If non-nil, don't require MIME-Version header.
1022 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1023 supply the MIME-Version header or deliberately strip it From the mail.
1024 Set it to non-nil, Gnus will treat some articles as MIME even if
1025 the MIME-Version header is missed."
1026   :version "21.3"
1027   :type 'boolean
1028   :group 'gnus-article)
1029
1030 ;;; Internal variables
1031
1032 (defvar gnus-summary-display-cache nil)
1033 (defvar gnus-article-mime-handles nil)
1034 (defvar gnus-article-decoded-p nil)
1035 (defvar gnus-article-charset nil)
1036 (defvar gnus-article-ignored-charsets nil)
1037 (defvar gnus-scores-exclude-files nil)
1038 (defvar gnus-page-broken nil)
1039 (defvar gnus-inhibit-mime-unbuttonizing nil)
1040
1041 (defvar gnus-original-article nil)
1042 (defvar gnus-article-internal-prepare-hook nil)
1043 (defvar gnus-newsgroup-process-stack nil)
1044
1045 (defvar gnus-thread-indent-array nil)
1046 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1047 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1048   "Function called to sort the articles within a thread after it has been gathered together.")
1049
1050 (defvar gnus-summary-save-parts-type-history nil)
1051 (defvar gnus-summary-save-parts-last-directory nil)
1052
1053 (defvar gnus-summary-save-parts-type-history nil)
1054 (defvar gnus-summary-save-parts-last-directory nil)
1055
1056 ;; Avoid highlighting in kill files.
1057 (defvar gnus-summary-inhibit-highlight nil)
1058 (defvar gnus-newsgroup-selected-overlay nil)
1059 (defvar gnus-inhibit-limiting nil)
1060 (defvar gnus-newsgroup-adaptive-score-file nil)
1061 (defvar gnus-current-score-file nil)
1062 (defvar gnus-current-move-group nil)
1063 (defvar gnus-current-copy-group nil)
1064 (defvar gnus-current-crosspost-group nil)
1065 (defvar gnus-newsgroup-display nil)
1066
1067 (defvar gnus-newsgroup-dependencies nil)
1068 (defvar gnus-newsgroup-adaptive nil)
1069 (defvar gnus-summary-display-article-function nil)
1070 (defvar gnus-summary-highlight-line-function nil
1071   "Function called after highlighting a summary line.")
1072
1073 (defvar gnus-summary-line-format-alist
1074   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1075     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1076     (?s gnus-tmp-subject-or-nil ?s)
1077     (?n gnus-tmp-name ?s)
1078     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1079         ?s)
1080     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1081             gnus-tmp-from) ?s)
1082     (?F gnus-tmp-from ?s)
1083     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1084     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1085     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1086     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1087     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1088     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1089     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1090     (?L gnus-tmp-lines ?s)
1091     (?I gnus-tmp-indentation ?s)
1092     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1093     (?R gnus-tmp-replied ?c)
1094     (?\[ gnus-tmp-opening-bracket ?c)
1095     (?\] gnus-tmp-closing-bracket ?c)
1096     (?\> (make-string gnus-tmp-level ? ) ?s)
1097     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1098     (?i gnus-tmp-score ?d)
1099     (?z gnus-tmp-score-char ?c)
1100     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1101     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1102     (?U gnus-tmp-unread ?c)
1103     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1104         ?s)
1105     (?t (gnus-summary-number-of-articles-in-thread
1106          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1107         ?d)
1108     (?e (gnus-summary-number-of-articles-in-thread
1109          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1110         ?c)
1111     (?u gnus-tmp-user-defined ?s)
1112     (?P (gnus-pick-line-number) ?d)
1113     (?B gnus-tmp-thread-tree-header-string ?s)
1114     (user-date (gnus-user-date
1115                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1116   "An alist of format specifications that can appear in summary lines.
1117 These are paired with what variables they correspond with, along with
1118 the type of the variable (string, integer, character, etc).")
1119
1120 (defvar gnus-summary-dummy-line-format-alist
1121   `((?S gnus-tmp-subject ?s)
1122     (?N gnus-tmp-number ?d)
1123     (?u gnus-tmp-user-defined ?s)))
1124
1125 (defvar gnus-summary-mode-line-format-alist
1126   `((?G gnus-tmp-group-name ?s)
1127     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1128     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1129     (?A gnus-tmp-article-number ?d)
1130     (?Z gnus-tmp-unread-and-unselected ?s)
1131     (?V gnus-version ?s)
1132     (?U gnus-tmp-unread-and-unticked ?d)
1133     (?S gnus-tmp-subject ?s)
1134     (?e gnus-tmp-unselected ?d)
1135     (?u gnus-tmp-user-defined ?s)
1136     (?d (length gnus-newsgroup-dormant) ?d)
1137     (?t (length gnus-newsgroup-marked) ?d)
1138     (?r (length gnus-newsgroup-reads) ?d)
1139     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1140     (?E gnus-newsgroup-expunged-tally ?d)
1141     (?s (gnus-current-score-file-nondirectory) ?s)))
1142
1143 (defvar gnus-last-search-regexp nil
1144   "Default regexp for article search command.")
1145
1146 (defvar gnus-last-shell-command nil
1147   "Default shell command on article.")
1148
1149 (defvar gnus-newsgroup-begin nil)
1150 (defvar gnus-newsgroup-end nil)
1151 (defvar gnus-newsgroup-last-rmail nil)
1152 (defvar gnus-newsgroup-last-mail nil)
1153 (defvar gnus-newsgroup-last-folder nil)
1154 (defvar gnus-newsgroup-last-file nil)
1155 (defvar gnus-newsgroup-auto-expire nil)
1156 (defvar gnus-newsgroup-active nil)
1157
1158 (defvar gnus-newsgroup-data nil)
1159 (defvar gnus-newsgroup-data-reverse nil)
1160 (defvar gnus-newsgroup-limit nil)
1161 (defvar gnus-newsgroup-limits nil)
1162
1163 (defvar gnus-newsgroup-unreads nil
1164   "Sorted list of unread articles in the current newsgroup.")
1165
1166 (defvar gnus-newsgroup-unselected nil
1167   "Sorted list of unselected unread articles in the current newsgroup.")
1168
1169 (defvar gnus-newsgroup-reads nil
1170   "Alist of read articles and article marks in the current newsgroup.")
1171
1172 (defvar gnus-newsgroup-expunged-tally nil)
1173
1174 (defvar gnus-newsgroup-marked nil
1175   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1176
1177 (defvar gnus-newsgroup-killed nil
1178   "List of ranges of articles that have been through the scoring process.")
1179
1180 (defvar gnus-newsgroup-cached nil
1181   "Sorted list of articles that come from the article cache.")
1182
1183 (defvar gnus-newsgroup-saved nil
1184   "List of articles that have been saved.")
1185
1186 (defvar gnus-newsgroup-kill-headers nil)
1187
1188 (defvar gnus-newsgroup-replied nil
1189   "List of articles that have been replied to in the current newsgroup.")
1190
1191 (defvar gnus-newsgroup-forwarded nil
1192   "List of articles that have been forwarded in the current newsgroup.")
1193
1194 (defvar gnus-newsgroup-recent nil
1195   "List of articles that have are recent in the current newsgroup.")
1196
1197 (defvar gnus-newsgroup-expirable nil
1198   "Sorted list of articles in the current newsgroup that can be expired.")
1199
1200 (defvar gnus-newsgroup-processable nil
1201   "List of articles in the current newsgroup that can be processed.")
1202
1203 (defvar gnus-newsgroup-downloadable nil
1204   "Sorted list of articles in the current newsgroup that can be processed.")
1205
1206 (defvar gnus-newsgroup-undownloaded nil
1207   "List of articles in the current newsgroup that haven't been downloaded..")
1208
1209 (defvar gnus-newsgroup-unsendable nil
1210   "List of articles in the current newsgroup that won't be sent.")
1211
1212 (defvar gnus-newsgroup-bookmarks nil
1213   "List of articles in the current newsgroup that have bookmarks.")
1214
1215 (defvar gnus-newsgroup-dormant nil
1216   "Sorted list of dormant articles in the current newsgroup.")
1217
1218 (defvar gnus-newsgroup-unseen nil
1219   "List of unseen articles in the current newsgroup.")
1220
1221 (defvar gnus-newsgroup-seen nil
1222   "Range of seen articles in the current newsgroup.")
1223
1224 (defvar gnus-newsgroup-articles nil
1225   "List of articles in the current newsgroup.")
1226
1227 (defvar gnus-newsgroup-scored nil
1228   "List of scored articles in the current newsgroup.")
1229
1230 (defvar gnus-newsgroup-headers nil
1231   "List of article headers in the current newsgroup.")
1232
1233 (defvar gnus-newsgroup-threads nil)
1234
1235 (defvar gnus-newsgroup-prepared nil
1236   "Whether the current group has been prepared properly.")
1237
1238 (defvar gnus-newsgroup-ancient nil
1239   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1240
1241 (defvar gnus-newsgroup-sparse nil)
1242
1243 (defvar gnus-current-article nil)
1244 (defvar gnus-article-current nil)
1245 (defvar gnus-current-headers nil)
1246 (defvar gnus-have-all-headers nil)
1247 (defvar gnus-last-article nil)
1248 (defvar gnus-newsgroup-history nil)
1249 (defvar gnus-newsgroup-charset nil)
1250 (defvar gnus-newsgroup-ephemeral-charset nil)
1251 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1252
1253 (defvar gnus-article-before-search nil)
1254
1255 (defconst gnus-summary-local-variables
1256   '(gnus-newsgroup-name
1257     gnus-newsgroup-begin gnus-newsgroup-end
1258     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1259     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1260     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1261     gnus-newsgroup-unselected gnus-newsgroup-marked
1262     gnus-newsgroup-reads gnus-newsgroup-saved
1263     gnus-newsgroup-replied gnus-newsgroup-forwarded
1264     gnus-newsgroup-recent
1265     gnus-newsgroup-expirable
1266     gnus-newsgroup-processable gnus-newsgroup-killed
1267     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1268     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1269     gnus-newsgroup-seen gnus-newsgroup-articles
1270     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1271     gnus-newsgroup-headers gnus-newsgroup-threads
1272     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1273     gnus-current-article gnus-current-headers gnus-have-all-headers
1274     gnus-last-article gnus-article-internal-prepare-hook
1275     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1276     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1277     gnus-thread-expunge-below
1278     gnus-score-alist gnus-current-score-file
1279     (gnus-summary-expunge-below . global)
1280     (gnus-summary-mark-below . global)
1281     (gnus-orphan-score . global)
1282     gnus-newsgroup-active gnus-scores-exclude-files
1283     gnus-newsgroup-history gnus-newsgroup-ancient
1284     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1285     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1286     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1287     (gnus-newsgroup-expunged-tally . 0)
1288     gnus-cache-removable-articles gnus-newsgroup-cached
1289     gnus-newsgroup-data gnus-newsgroup-data-reverse
1290     gnus-newsgroup-limit gnus-newsgroup-limits
1291     gnus-newsgroup-charset gnus-newsgroup-display)
1292   "Variables that are buffer-local to the summary buffers.")
1293
1294 (defvar gnus-newsgroup-variables nil
1295   "A list of variables that have separate values in different newsgroups.
1296 A list of newsgroup (summary buffer) local variables, or cons of
1297 variables and their default values (when the default values are not
1298 nil), that should be made global while the summary buffer is active.
1299 These variables can be used to set variables in the group parameters
1300 while still allowing them to affect operations done in other
1301 buffers. For example:
1302
1303 \(setq gnus-newsgroup-variables
1304      '(message-use-followup-to
1305        (gnus-visible-headers .
1306          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1307 ")
1308
1309 ;; Byte-compiler warning.
1310 (eval-when-compile (defvar gnus-article-mode-map))
1311
1312 ;; MIME stuff.
1313
1314 (defvar gnus-decode-encoded-word-methods
1315   '(mail-decode-encoded-word-string)
1316   "List of methods used to decode encoded words.
1317
1318 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1319 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1320 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1321 whose names match REGEXP.
1322
1323 For example:
1324 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1325  mail-decode-encoded-word-string
1326  (\"chinese\" . rfc1843-decode-string))")
1327
1328 (defvar gnus-decode-encoded-word-methods-cache nil)
1329
1330 (defun gnus-multi-decode-encoded-word-string (string)
1331   "Apply the functions from `gnus-encoded-word-methods' that match."
1332   (unless (and gnus-decode-encoded-word-methods-cache
1333                (eq gnus-newsgroup-name
1334                    (car gnus-decode-encoded-word-methods-cache)))
1335     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1336     (mapcar (lambda (x)
1337               (if (symbolp x)
1338                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1339                 (if (and gnus-newsgroup-name
1340                          (string-match (car x) gnus-newsgroup-name))
1341                     (nconc gnus-decode-encoded-word-methods-cache
1342                            (list (cdr x))))))
1343             gnus-decode-encoded-word-methods))
1344   (let ((xlist gnus-decode-encoded-word-methods-cache))
1345     (pop xlist)
1346     (while xlist
1347       (setq string (funcall (pop xlist) string))))
1348   string)
1349
1350 ;; Subject simplification.
1351
1352 (defun gnus-simplify-whitespace (str)
1353   "Remove excessive whitespace from STR."
1354   (let ((mystr str))
1355     ;; Multiple spaces.
1356     (while (string-match "[ \t][ \t]+" mystr)
1357       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1358                           " "
1359                           (substring mystr (match-end 0)))))
1360     ;; Leading spaces.
1361     (when (string-match "^[ \t]+" mystr)
1362       (setq mystr (substring mystr (match-end 0))))
1363     ;; Trailing spaces.
1364     (when (string-match "[ \t]+$" mystr)
1365       (setq mystr (substring mystr 0 (match-beginning 0))))
1366     mystr))
1367
1368 (defsubst gnus-simplify-subject-re (subject)
1369   "Remove \"Re:\" from subject lines."
1370   (if (string-match message-subject-re-regexp subject)
1371       (substring subject (match-end 0))
1372     subject))
1373
1374 (defun gnus-simplify-subject (subject &optional re-only)
1375   "Remove `Re:' and words in parentheses.
1376 If RE-ONLY is non-nil, strip leading `Re:'s only."
1377   (let ((case-fold-search t))           ;Ignore case.
1378     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1379     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1380       (setq subject (substring subject (match-end 0))))
1381     ;; Remove uninteresting prefixes.
1382     (when (and (not re-only)
1383                gnus-simplify-ignored-prefixes
1384                (string-match gnus-simplify-ignored-prefixes subject))
1385       (setq subject (substring subject (match-end 0))))
1386     ;; Remove words in parentheses from end.
1387     (unless re-only
1388       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1389         (setq subject (substring subject 0 (match-beginning 0)))))
1390     ;; Return subject string.
1391     subject))
1392
1393 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1394 ;; all whitespace.
1395 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1396   (goto-char (point-min))
1397   (while (re-search-forward regexp nil t)
1398     (replace-match (or newtext ""))))
1399
1400 (defun gnus-simplify-buffer-fuzzy ()
1401   "Simplify string in the buffer fuzzily.
1402 The string in the accessible portion of the current buffer is simplified.
1403 It is assumed to be a single-line subject.
1404 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1405 matter is removed.  Additional things can be deleted by setting
1406 `gnus-simplify-subject-fuzzy-regexp'."
1407   (let ((case-fold-search t)
1408         (modified-tick))
1409     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1410
1411     (while (not (eq modified-tick (buffer-modified-tick)))
1412       (setq modified-tick (buffer-modified-tick))
1413       (cond
1414        ((listp gnus-simplify-subject-fuzzy-regexp)
1415         (mapcar 'gnus-simplify-buffer-fuzzy-step
1416                 gnus-simplify-subject-fuzzy-regexp))
1417        (gnus-simplify-subject-fuzzy-regexp
1418         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1419       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1420       (gnus-simplify-buffer-fuzzy-step
1421        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1422       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1423
1424     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1425     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1426     (gnus-simplify-buffer-fuzzy-step " $")
1427     (gnus-simplify-buffer-fuzzy-step "^ +")))
1428
1429 (defun gnus-simplify-subject-fuzzy (subject)
1430   "Simplify a subject string fuzzily.
1431 See `gnus-simplify-buffer-fuzzy' for details."
1432   (save-excursion
1433     (gnus-set-work-buffer)
1434     (let ((case-fold-search t))
1435       ;; Remove uninteresting prefixes.
1436       (when (and gnus-simplify-ignored-prefixes
1437                  (string-match gnus-simplify-ignored-prefixes subject))
1438         (setq subject (substring subject (match-end 0))))
1439       (insert subject)
1440       (inline (gnus-simplify-buffer-fuzzy))
1441       (buffer-string))))
1442
1443 (defsubst gnus-simplify-subject-fully (subject)
1444   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1445   (cond
1446    (gnus-simplify-subject-functions
1447     (gnus-map-function gnus-simplify-subject-functions subject))
1448    ((null gnus-summary-gather-subject-limit)
1449     (gnus-simplify-subject-re subject))
1450    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1451     (gnus-simplify-subject-fuzzy subject))
1452    ((numberp gnus-summary-gather-subject-limit)
1453     (gnus-limit-string (gnus-simplify-subject-re subject)
1454                        gnus-summary-gather-subject-limit))
1455    (t
1456     subject)))
1457
1458 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1459   "Check whether two subjects are equal.
1460 If optional argument simple-first is t, first argument is already
1461 simplified."
1462   (cond
1463    ((null simple-first)
1464     (equal (gnus-simplify-subject-fully s1)
1465            (gnus-simplify-subject-fully s2)))
1466    (t
1467     (equal s1
1468            (gnus-simplify-subject-fully s2)))))
1469
1470 (defun gnus-summary-bubble-group ()
1471   "Increase the score of the current group.
1472 This is a handy function to add to `gnus-summary-exit-hook' to
1473 increase the score of each group you read."
1474   (gnus-group-add-score gnus-newsgroup-name))
1475
1476 \f
1477 ;;;
1478 ;;; Gnus summary mode
1479 ;;;
1480
1481 (put 'gnus-summary-mode 'mode-class 'special)
1482
1483 (defvar gnus-article-commands-menu)
1484
1485 (when t
1486   ;; Non-orthogonal keys
1487
1488   (gnus-define-keys gnus-summary-mode-map
1489     " " gnus-summary-next-page
1490     "\177" gnus-summary-prev-page
1491     [delete] gnus-summary-prev-page
1492     [backspace] gnus-summary-prev-page
1493     "\r" gnus-summary-scroll-up
1494     "\M-\r" gnus-summary-scroll-down
1495     "n" gnus-summary-next-unread-article
1496     "p" gnus-summary-prev-unread-article
1497     "N" gnus-summary-next-article
1498     "P" gnus-summary-prev-article
1499     "\M-\C-n" gnus-summary-next-same-subject
1500     "\M-\C-p" gnus-summary-prev-same-subject
1501     "\M-n" gnus-summary-next-unread-subject
1502     "\M-p" gnus-summary-prev-unread-subject
1503     "." gnus-summary-first-unread-article
1504     "," gnus-summary-best-unread-article
1505     "\M-s" gnus-summary-search-article-forward
1506     "\M-r" gnus-summary-search-article-backward
1507     "<" gnus-summary-beginning-of-article
1508     ">" gnus-summary-end-of-article
1509     "j" gnus-summary-goto-article
1510     "^" gnus-summary-refer-parent-article
1511     "\M-^" gnus-summary-refer-article
1512     "u" gnus-summary-tick-article-forward
1513     "!" gnus-summary-tick-article-forward
1514     "U" gnus-summary-tick-article-backward
1515     "d" gnus-summary-mark-as-read-forward
1516     "D" gnus-summary-mark-as-read-backward
1517     "E" gnus-summary-mark-as-expirable
1518     "\M-u" gnus-summary-clear-mark-forward
1519     "\M-U" gnus-summary-clear-mark-backward
1520     "k" gnus-summary-kill-same-subject-and-select
1521     "\C-k" gnus-summary-kill-same-subject
1522     "\M-\C-k" gnus-summary-kill-thread
1523     "\M-\C-l" gnus-summary-lower-thread
1524     "e" gnus-summary-edit-article
1525     "#" gnus-summary-mark-as-processable
1526     "\M-#" gnus-summary-unmark-as-processable
1527     "\M-\C-t" gnus-summary-toggle-threads
1528     "\M-\C-s" gnus-summary-show-thread
1529     "\M-\C-h" gnus-summary-hide-thread
1530     "\M-\C-f" gnus-summary-next-thread
1531     "\M-\C-b" gnus-summary-prev-thread
1532     [(meta down)] gnus-summary-next-thread
1533     [(meta up)] gnus-summary-prev-thread
1534     "\M-\C-u" gnus-summary-up-thread
1535     "\M-\C-d" gnus-summary-down-thread
1536     "&" gnus-summary-execute-command
1537     "c" gnus-summary-catchup-and-exit
1538     "\C-w" gnus-summary-mark-region-as-read
1539     "\C-t" gnus-summary-toggle-truncation
1540     "?" gnus-summary-mark-as-dormant
1541     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1542     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1543     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1544     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1545     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1546     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1547     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1548     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1549     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1550     "=" gnus-summary-expand-window
1551     "\C-x\C-s" gnus-summary-reselect-current-group
1552     "\M-g" gnus-summary-rescan-group
1553     "w" gnus-summary-stop-page-breaking
1554     "\C-c\C-r" gnus-summary-caesar-message
1555     "f" gnus-summary-followup
1556     "F" gnus-summary-followup-with-original
1557     "C" gnus-summary-cancel-article
1558     "r" gnus-summary-reply
1559     "R" gnus-summary-reply-with-original
1560     "\C-c\C-f" gnus-summary-mail-forward
1561     "o" gnus-summary-save-article
1562     "\C-o" gnus-summary-save-article-mail
1563     "|" gnus-summary-pipe-output
1564     "\M-k" gnus-summary-edit-local-kill
1565     "\M-K" gnus-summary-edit-global-kill
1566     ;; "V" gnus-version
1567     "\C-c\C-d" gnus-summary-describe-group
1568     "q" gnus-summary-exit
1569     "Q" gnus-summary-exit-no-update
1570     "\C-c\C-i" gnus-info-find-node
1571     gnus-mouse-2 gnus-mouse-pick-article
1572     "m" gnus-summary-mail-other-window
1573     "a" gnus-summary-post-news
1574     "i" gnus-summary-news-other-window
1575     "x" gnus-summary-limit-to-unread
1576     "s" gnus-summary-isearch-article
1577     "t" gnus-summary-toggle-header
1578     "g" gnus-summary-show-article
1579     "l" gnus-summary-goto-last-article
1580     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1581     "\C-d" gnus-summary-enter-digest-group
1582     "\M-\C-d" gnus-summary-read-document
1583     "\M-\C-e" gnus-summary-edit-parameters
1584     "\M-\C-a" gnus-summary-customize-parameters
1585     "\C-c\C-b" gnus-bug
1586     "*" gnus-cache-enter-article
1587     "\M-*" gnus-cache-remove-article
1588     "\M-&" gnus-summary-universal-argument
1589     "\C-l" gnus-recenter
1590     "I" gnus-summary-increase-score
1591     "L" gnus-summary-lower-score
1592     "\M-i" gnus-symbolic-argument
1593     "h" gnus-summary-select-article-buffer
1594
1595     "b" gnus-article-view-part
1596     "\M-t" gnus-summary-toggle-display-buttonized
1597
1598     "V" gnus-summary-score-map
1599     "X" gnus-uu-extract-map
1600     "S" gnus-summary-send-map)
1601
1602   ;; Sort of orthogonal keymap
1603   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1604     "t" gnus-summary-tick-article-forward
1605     "!" gnus-summary-tick-article-forward
1606     "d" gnus-summary-mark-as-read-forward
1607     "r" gnus-summary-mark-as-read-forward
1608     "c" gnus-summary-clear-mark-forward
1609     " " gnus-summary-clear-mark-forward
1610     "e" gnus-summary-mark-as-expirable
1611     "x" gnus-summary-mark-as-expirable
1612     "?" gnus-summary-mark-as-dormant
1613     "b" gnus-summary-set-bookmark
1614     "B" gnus-summary-remove-bookmark
1615     "#" gnus-summary-mark-as-processable
1616     "\M-#" gnus-summary-unmark-as-processable
1617     "S" gnus-summary-limit-include-expunged
1618     "C" gnus-summary-catchup
1619     "H" gnus-summary-catchup-to-here
1620     "h" gnus-summary-catchup-from-here
1621     "\C-c" gnus-summary-catchup-all
1622     "k" gnus-summary-kill-same-subject-and-select
1623     "K" gnus-summary-kill-same-subject
1624     "P" gnus-uu-mark-map)
1625
1626   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1627     "c" gnus-summary-clear-above
1628     "u" gnus-summary-tick-above
1629     "m" gnus-summary-mark-above
1630     "k" gnus-summary-kill-below)
1631
1632   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1633     "/" gnus-summary-limit-to-subject
1634     "n" gnus-summary-limit-to-articles
1635     "w" gnus-summary-pop-limit
1636     "s" gnus-summary-limit-to-subject
1637     "a" gnus-summary-limit-to-author
1638     "u" gnus-summary-limit-to-unread
1639     "m" gnus-summary-limit-to-marks
1640     "M" gnus-summary-limit-exclude-marks
1641     "v" gnus-summary-limit-to-score
1642     "*" gnus-summary-limit-include-cached
1643     "D" gnus-summary-limit-include-dormant
1644     "T" gnus-summary-limit-include-thread
1645     "d" gnus-summary-limit-exclude-dormant
1646     "t" gnus-summary-limit-to-age
1647     "x" gnus-summary-limit-to-extra
1648     "p" gnus-summary-limit-to-display-predicate
1649     "E" gnus-summary-limit-include-expunged
1650     "c" gnus-summary-limit-exclude-childless-dormant
1651     "C" gnus-summary-limit-mark-excluded-as-read
1652     "o" gnus-summary-insert-old-articles
1653     "N" gnus-summary-insert-new-articles)
1654
1655   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1656     "n" gnus-summary-next-unread-article
1657     "p" gnus-summary-prev-unread-article
1658     "N" gnus-summary-next-article
1659     "P" gnus-summary-prev-article
1660     "\C-n" gnus-summary-next-same-subject
1661     "\C-p" gnus-summary-prev-same-subject
1662     "\M-n" gnus-summary-next-unread-subject
1663     "\M-p" gnus-summary-prev-unread-subject
1664     "f" gnus-summary-first-unread-article
1665     "b" gnus-summary-best-unread-article
1666     "j" gnus-summary-goto-article
1667     "g" gnus-summary-goto-subject
1668     "l" gnus-summary-goto-last-article
1669     "o" gnus-summary-pop-article)
1670
1671   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1672     "k" gnus-summary-kill-thread
1673     "l" gnus-summary-lower-thread
1674     "i" gnus-summary-raise-thread
1675     "T" gnus-summary-toggle-threads
1676     "t" gnus-summary-rethread-current
1677     "^" gnus-summary-reparent-thread
1678     "s" gnus-summary-show-thread
1679     "S" gnus-summary-show-all-threads
1680     "h" gnus-summary-hide-thread
1681     "H" gnus-summary-hide-all-threads
1682     "n" gnus-summary-next-thread
1683     "p" gnus-summary-prev-thread
1684     "u" gnus-summary-up-thread
1685     "o" gnus-summary-top-thread
1686     "d" gnus-summary-down-thread
1687     "#" gnus-uu-mark-thread
1688     "\M-#" gnus-uu-unmark-thread)
1689
1690   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1691     "g" gnus-summary-prepare
1692     "c" gnus-summary-insert-cached-articles)
1693
1694   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1695     "c" gnus-summary-catchup-and-exit
1696     "C" gnus-summary-catchup-all-and-exit
1697     "E" gnus-summary-exit-no-update
1698     "Q" gnus-summary-exit
1699     "Z" gnus-summary-exit
1700     "n" gnus-summary-catchup-and-goto-next-group
1701     "R" gnus-summary-reselect-current-group
1702     "G" gnus-summary-rescan-group
1703     "N" gnus-summary-next-group
1704     "s" gnus-summary-save-newsrc
1705     "P" gnus-summary-prev-group)
1706
1707   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1708     " " gnus-summary-next-page
1709     "n" gnus-summary-next-page
1710     "\177" gnus-summary-prev-page
1711     [delete] gnus-summary-prev-page
1712     "p" gnus-summary-prev-page
1713     "\r" gnus-summary-scroll-up
1714     "\M-\r" gnus-summary-scroll-down
1715     "<" gnus-summary-beginning-of-article
1716     ">" gnus-summary-end-of-article
1717     "b" gnus-summary-beginning-of-article
1718     "e" gnus-summary-end-of-article
1719     "^" gnus-summary-refer-parent-article
1720     "r" gnus-summary-refer-parent-article
1721     "D" gnus-summary-enter-digest-group
1722     "R" gnus-summary-refer-references
1723     "T" gnus-summary-refer-thread
1724     "g" gnus-summary-show-article
1725     "s" gnus-summary-isearch-article
1726     "P" gnus-summary-print-article
1727     "M" gnus-mailing-list-insinuate
1728     "t" gnus-article-babel)
1729
1730   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1731     "b" gnus-article-add-buttons
1732     "B" gnus-article-add-buttons-to-head
1733     "o" gnus-article-treat-overstrike
1734     "e" gnus-article-emphasize
1735     "w" gnus-article-fill-cited-article
1736     "Q" gnus-article-fill-long-lines
1737     "C" gnus-article-capitalize-sentences
1738     "c" gnus-article-remove-cr
1739     "q" gnus-article-de-quoted-unreadable
1740     "6" gnus-article-de-base64-unreadable
1741     "Z" gnus-article-decode-HZ
1742     "h" gnus-article-wash-html
1743     "u" gnus-article-unsplit-urls
1744     "s" gnus-summary-force-verify-and-decrypt
1745     "f" gnus-article-display-x-face
1746     "l" gnus-summary-stop-page-breaking
1747     "r" gnus-summary-caesar-message
1748     "t" gnus-summary-toggle-header
1749     "g" gnus-treat-smiley
1750     "v" gnus-summary-verbose-headers
1751     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1752     "p" gnus-article-verify-x-pgp-sig
1753     "d" gnus-article-treat-dumbquotes)
1754
1755   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1756     "a" gnus-article-hide
1757     "h" gnus-article-hide-headers
1758     "b" gnus-article-hide-boring-headers
1759     "s" gnus-article-hide-signature
1760     "c" gnus-article-hide-citation
1761     "C" gnus-article-hide-citation-in-followups
1762     "l" gnus-article-hide-list-identifiers
1763     "p" gnus-article-hide-pgp
1764     "B" gnus-article-strip-banner
1765     "P" gnus-article-hide-pem
1766     "\C-c" gnus-article-hide-citation-maybe)
1767
1768   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1769     "a" gnus-article-highlight
1770     "h" gnus-article-highlight-headers
1771     "c" gnus-article-highlight-citation
1772     "s" gnus-article-highlight-signature)
1773
1774   (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1775     "f" gnus-article-treat-fold-headers
1776     "u" gnus-article-treat-unfold-headers
1777     "n" gnus-article-treat-fold-newsgroups)
1778
1779   (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1780     "x" gnus-article-display-x-face
1781     "s" gnus-treat-smiley
1782     "D" gnus-article-remove-images
1783     "f" gnus-treat-from-picon
1784     "m" gnus-treat-mail-picon
1785     "n" gnus-treat-newsgroups-picon)
1786
1787   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1788     "w" gnus-article-decode-mime-words
1789     "c" gnus-article-decode-charset
1790     "v" gnus-mime-view-all-parts
1791     "b" gnus-article-view-part)
1792
1793   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1794     "z" gnus-article-date-ut
1795     "u" gnus-article-date-ut
1796     "l" gnus-article-date-local
1797     "p" gnus-article-date-english
1798     "e" gnus-article-date-lapsed
1799     "o" gnus-article-date-original
1800     "i" gnus-article-date-iso8601
1801     "s" gnus-article-date-user)
1802
1803   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1804     "t" gnus-article-remove-trailing-blank-lines
1805     "l" gnus-article-strip-leading-blank-lines
1806     "m" gnus-article-strip-multiple-blank-lines
1807     "a" gnus-article-strip-blank-lines
1808     "A" gnus-article-strip-all-blank-lines
1809     "s" gnus-article-strip-leading-space
1810     "e" gnus-article-strip-trailing-space
1811     "w" gnus-article-remove-leading-whitespace)
1812
1813   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1814     "v" gnus-version
1815     "f" gnus-summary-fetch-faq
1816     "d" gnus-summary-describe-group
1817     "h" gnus-summary-describe-briefly
1818     "i" gnus-info-find-node)
1819
1820   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1821     "e" gnus-summary-expire-articles
1822     "\M-\C-e" gnus-summary-expire-articles-now
1823     "\177" gnus-summary-delete-article
1824     [delete] gnus-summary-delete-article
1825     [backspace] gnus-summary-delete-article
1826     "m" gnus-summary-move-article
1827     "r" gnus-summary-respool-article
1828     "w" gnus-summary-edit-article
1829     "c" gnus-summary-copy-article
1830     "B" gnus-summary-crosspost-article
1831     "q" gnus-summary-respool-query
1832     "t" gnus-summary-respool-trace
1833     "i" gnus-summary-import-article
1834     "I" gnus-summary-create-article
1835     "p" gnus-summary-article-posted-p)
1836
1837   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1838     "o" gnus-summary-save-article
1839     "m" gnus-summary-save-article-mail
1840     "F" gnus-summary-write-article-file
1841     "r" gnus-summary-save-article-rmail
1842     "f" gnus-summary-save-article-file
1843     "b" gnus-summary-save-article-body-file
1844     "h" gnus-summary-save-article-folder
1845     "v" gnus-summary-save-article-vm
1846     "p" gnus-summary-pipe-output
1847     "P" gnus-summary-muttprint
1848     "s" gnus-soup-add-article)
1849
1850   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1851     "b" gnus-summary-display-buttonized
1852     "m" gnus-summary-repair-multipart
1853     "v" gnus-article-view-part
1854     "o" gnus-article-save-part
1855     "c" gnus-article-copy-part
1856     "C" gnus-article-view-part-as-charset
1857     "e" gnus-article-view-part-externally
1858     "E" gnus-article-encrypt-body
1859     "i" gnus-article-inline-part
1860     "|" gnus-article-pipe-part))
1861
1862 (defvar gnus-article-post-menu nil)
1863
1864 (defun gnus-summary-make-menu-bar ()
1865   (gnus-turn-off-edit-menu 'summary)
1866
1867   (unless (boundp 'gnus-summary-misc-menu)
1868
1869     (easy-menu-define
1870       gnus-summary-kill-menu gnus-summary-mode-map ""
1871       (cons
1872        "Score"
1873        (nconc
1874         (list
1875          ["Customize" gnus-score-customize t])
1876         (gnus-make-score-map 'increase)
1877         (gnus-make-score-map 'lower)
1878         '(("Mark"
1879            ["Kill below" gnus-summary-kill-below t]
1880            ["Mark above" gnus-summary-mark-above t]
1881            ["Tick above" gnus-summary-tick-above t]
1882            ["Clear above" gnus-summary-clear-above t])
1883           ["Current score" gnus-summary-current-score t]
1884           ["Set score" gnus-summary-set-score t]
1885           ["Switch current score file..." gnus-score-change-score-file t]
1886           ["Set mark below..." gnus-score-set-mark-below t]
1887           ["Set expunge below..." gnus-score-set-expunge-below t]
1888           ["Edit current score file" gnus-score-edit-current-scores t]
1889           ["Edit score file" gnus-score-edit-file t]
1890           ["Trace score" gnus-score-find-trace t]
1891           ["Find words" gnus-score-find-favourite-words t]
1892           ["Rescore buffer" gnus-summary-rescore t]
1893           ["Increase score..." gnus-summary-increase-score t]
1894           ["Lower score..." gnus-summary-lower-score t]))))
1895
1896     ;; Define both the Article menu in the summary buffer and the
1897     ;; equivalent Commands menu in the article buffer here for
1898     ;; consistency.
1899     (let ((innards
1900            `(("Hide"
1901               ["All" gnus-article-hide t]
1902               ["Headers" gnus-article-hide-headers t]
1903               ["Signature" gnus-article-hide-signature t]
1904               ["Citation" gnus-article-hide-citation t]
1905               ["List identifiers" gnus-article-hide-list-identifiers t]
1906               ["PGP" gnus-article-hide-pgp t]
1907               ["Banner" gnus-article-strip-banner t]
1908               ["Boring headers" gnus-article-hide-boring-headers t])
1909              ("Highlight"
1910               ["All" gnus-article-highlight t]
1911               ["Headers" gnus-article-highlight-headers t]
1912               ["Signature" gnus-article-highlight-signature t]
1913               ["Citation" gnus-article-highlight-citation t])
1914              ("MIME"
1915               ["Words" gnus-article-decode-mime-words t]
1916               ["Charset" gnus-article-decode-charset t]
1917               ["QP" gnus-article-de-quoted-unreadable t]
1918               ["Base64" gnus-article-de-base64-unreadable t]
1919               ["View all" gnus-mime-view-all-parts t]
1920               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
1921               ["Encrypt body" gnus-article-encrypt-body t])
1922              ("Date"
1923               ["Local" gnus-article-date-local t]
1924               ["ISO8601" gnus-article-date-iso8601 t]
1925               ["UT" gnus-article-date-ut t]
1926               ["Original" gnus-article-date-original t]
1927               ["Lapsed" gnus-article-date-lapsed t]
1928               ["User-defined" gnus-article-date-user t])
1929              ("Display"
1930               ["Remove images" gnus-article-remove-images t]
1931               ["Toggle smiley" gnus-treat-smiley t]
1932               ["Show X-Face" gnus-article-display-x-face t]
1933               ["Show picons in From" gnus-treat-from-picon t]
1934               ["Show picons in mail headers" gnus-treat-mail-picon t]
1935               ["Show picons in news headers" gnus-treat-newsgroups-picon t])
1936              ("Washing"
1937               ("Remove Blanks"
1938                ["Leading" gnus-article-strip-leading-blank-lines t]
1939                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1940                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1941                ["All of the above" gnus-article-strip-blank-lines t]
1942                ["All" gnus-article-strip-all-blank-lines t]
1943                ["Leading space" gnus-article-strip-leading-space t]
1944                ["Trailing space" gnus-article-strip-trailing-space t]
1945                ["Leading space in headers"
1946                 gnus-article-remove-leading-whitespace t])
1947               ["Overstrike" gnus-article-treat-overstrike t]
1948               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1949               ["Emphasis" gnus-article-emphasize t]
1950               ["Word wrap" gnus-article-fill-cited-article t]
1951               ["Fill long lines" gnus-article-fill-long-lines t]
1952               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1953               ["CR" gnus-article-remove-cr t]
1954               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1955               ["Base64" gnus-article-de-base64-unreadable t]
1956               ["Rot 13" gnus-summary-caesar-message
1957                ,@(if (featurep 'xemacs) '(t)
1958                    '(:help "\"Caesar rotate\" article by 13"))]
1959               ["Unix pipe" gnus-summary-pipe-message t]
1960               ["Add buttons" gnus-article-add-buttons t]
1961               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1962               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1963               ["Verbose header" gnus-summary-verbose-headers t]
1964               ["Toggle header" gnus-summary-toggle-header t]
1965               ["Unfold headers" gnus-article-treat-unfold-headers t]
1966               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
1967               ["Html" gnus-article-wash-html t]
1968               ["URLs" gnus-article-unsplit-urls t]
1969               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1970               ["HZ" gnus-article-decode-HZ t])
1971              ("Output"
1972               ["Save in default format" gnus-summary-save-article
1973                ,@(if (featurep 'xemacs) '(t)
1974                    '(:help "Save article using default method"))]
1975               ["Save in file" gnus-summary-save-article-file
1976                ,@(if (featurep 'xemacs) '(t)
1977                    '(:help "Save article in file"))]
1978               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1979               ["Save in MH folder" gnus-summary-save-article-folder t]
1980               ["Save in VM folder" gnus-summary-save-article-vm t]
1981               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1982               ["Save body in file" gnus-summary-save-article-body-file t]
1983               ["Pipe through a filter" gnus-summary-pipe-output t]
1984               ["Add to SOUP packet" gnus-soup-add-article t]
1985               ["Print with Muttprint" gnus-summary-muttprint t]
1986               ["Print" gnus-summary-print-article t])
1987              ("Backend"
1988               ["Respool article..." gnus-summary-respool-article t]
1989               ["Move article..." gnus-summary-move-article
1990                (gnus-check-backend-function
1991                 'request-move-article gnus-newsgroup-name)]
1992               ["Copy article..." gnus-summary-copy-article t]
1993               ["Crosspost article..." gnus-summary-crosspost-article
1994                (gnus-check-backend-function
1995                 'request-replace-article gnus-newsgroup-name)]
1996               ["Import file..." gnus-summary-import-article t]
1997               ["Create article..." gnus-summary-create-article t]
1998               ["Check if posted" gnus-summary-article-posted-p t]
1999               ["Edit article" gnus-summary-edit-article
2000                (not (gnus-group-read-only-p))]
2001               ["Delete article" gnus-summary-delete-article
2002                (gnus-check-backend-function
2003                 'request-expire-articles gnus-newsgroup-name)]
2004               ["Query respool" gnus-summary-respool-query t]
2005               ["Trace respool" gnus-summary-respool-trace t]
2006               ["Delete expirable articles" gnus-summary-expire-articles-now
2007                (gnus-check-backend-function
2008                 'request-expire-articles gnus-newsgroup-name)])
2009              ("Extract"
2010               ["Uudecode" gnus-uu-decode-uu
2011                ,@(if (featurep 'xemacs) '(t)
2012                    '(:help "Decode uuencoded article(s)"))]
2013               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2014               ["Unshar" gnus-uu-decode-unshar t]
2015               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2016               ["Save" gnus-uu-decode-save t]
2017               ["Binhex" gnus-uu-decode-binhex t]
2018               ["Postscript" gnus-uu-decode-postscript t])
2019              ("Cache"
2020               ["Enter article" gnus-cache-enter-article t]
2021               ["Remove article" gnus-cache-remove-article t])
2022              ["Translate" gnus-article-babel t]
2023              ["Select article buffer" gnus-summary-select-article-buffer t]
2024              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2025              ["Isearch article..." gnus-summary-isearch-article t]
2026              ["Beginning of the article" gnus-summary-beginning-of-article t]
2027              ["End of the article" gnus-summary-end-of-article t]
2028              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2029              ["Fetch referenced articles" gnus-summary-refer-references t]
2030              ["Fetch current thread" gnus-summary-refer-thread t]
2031              ["Fetch article with id..." gnus-summary-refer-article t]
2032              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2033              ["Redisplay" gnus-summary-show-article t]
2034              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2035       (easy-menu-define
2036         gnus-summary-article-menu gnus-summary-mode-map ""
2037         (cons "Article" innards))
2038
2039       (if (not (keymapp gnus-summary-article-menu))
2040           (easy-menu-define
2041             gnus-article-commands-menu gnus-article-mode-map ""
2042             (cons "Commands" innards))
2043         ;; in Emacs, don't share menu.
2044         (setq gnus-article-commands-menu
2045               (copy-keymap gnus-summary-article-menu))
2046         (define-key gnus-article-mode-map [menu-bar commands]
2047           (cons "Commands" gnus-article-commands-menu))))
2048
2049     (easy-menu-define
2050       gnus-summary-thread-menu gnus-summary-mode-map ""
2051       '("Threads"
2052         ["Toggle threading" gnus-summary-toggle-threads t]
2053         ["Hide threads" gnus-summary-hide-all-threads t]
2054         ["Show threads" gnus-summary-show-all-threads t]
2055         ["Hide thread" gnus-summary-hide-thread t]
2056         ["Show thread" gnus-summary-show-thread t]
2057         ["Go to next thread" gnus-summary-next-thread t]
2058         ["Go to previous thread" gnus-summary-prev-thread t]
2059         ["Go down thread" gnus-summary-down-thread t]
2060         ["Go up thread" gnus-summary-up-thread t]
2061         ["Top of thread" gnus-summary-top-thread t]
2062         ["Mark thread as read" gnus-summary-kill-thread t]
2063         ["Lower thread score" gnus-summary-lower-thread t]
2064         ["Raise thread score" gnus-summary-raise-thread t]
2065         ["Rethread current" gnus-summary-rethread-current t]))
2066
2067     (easy-menu-define
2068       gnus-summary-post-menu gnus-summary-mode-map ""
2069       `("Post"
2070         ["Send a message (mail or news)" gnus-summary-post-news
2071          ,@(if (featurep 'xemacs) '(t)
2072              '(:help "Post an article"))]
2073         ["Followup" gnus-summary-followup
2074          ,@(if (featurep 'xemacs) '(t)
2075              '(:help "Post followup to this article"))]
2076         ["Followup and yank" gnus-summary-followup-with-original
2077          ,@(if (featurep 'xemacs) '(t)
2078              '(:help "Post followup to this article, quoting its contents"))]
2079         ["Supersede article" gnus-summary-supersede-article t]
2080         ["Cancel article" gnus-summary-cancel-article
2081          ,@(if (featurep 'xemacs) '(t)
2082              '(:help "Cancel an article you posted"))]
2083         ["Reply" gnus-summary-reply t]
2084         ["Reply and yank" gnus-summary-reply-with-original t]
2085         ["Wide reply" gnus-summary-wide-reply t]
2086         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2087          ,@(if (featurep 'xemacs) '(t)
2088              '(:help "Mail a reply, quoting this article"))]
2089         ["Very wide reply" gnus-summary-very-wide-reply t]
2090         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2091          ,@(if (featurep 'xemacs) '(t)
2092              '(:help "Mail a very wide reply, quoting this article"))]
2093         ["Mail forward" gnus-summary-mail-forward t]
2094         ["Post forward" gnus-summary-post-forward t]
2095         ["Digest and mail" gnus-uu-digest-mail-forward t]
2096         ["Digest and post" gnus-uu-digest-post-forward t]
2097         ["Resend message" gnus-summary-resend-message t]
2098         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2099         ["Send a mail" gnus-summary-mail-other-window t]
2100         ["Create a local message" gnus-summary-news-other-window t]
2101         ["Uuencode and post" gnus-uu-post-news
2102          ,@(if (featurep 'xemacs) '(t)
2103              '(:help "Post a uuencoded article"))]
2104         ["Followup via news" gnus-summary-followup-to-mail t]
2105         ["Followup via news and yank"
2106          gnus-summary-followup-to-mail-with-original t]
2107         ;;("Draft"
2108         ;;["Send" gnus-summary-send-draft t]
2109         ;;["Send bounced" gnus-resend-bounced-mail t])
2110         ))
2111
2112     (cond
2113      ((not (keymapp gnus-summary-post-menu))
2114       (setq gnus-article-post-menu gnus-summary-post-menu))
2115      ((not gnus-article-post-menu)
2116       ;; Don't share post menu.
2117       (setq gnus-article-post-menu
2118             (copy-keymap gnus-summary-post-menu))))
2119     (define-key gnus-article-mode-map [menu-bar post]
2120       (cons "Post" gnus-article-post-menu))
2121
2122     (easy-menu-define
2123       gnus-summary-misc-menu gnus-summary-mode-map ""
2124       `("Gnus"
2125         ("Mark Read"
2126          ["Mark as read" gnus-summary-mark-as-read-forward t]
2127          ["Mark same subject and select"
2128           gnus-summary-kill-same-subject-and-select t]
2129          ["Mark same subject" gnus-summary-kill-same-subject t]
2130          ["Catchup" gnus-summary-catchup
2131           ,@(if (featurep 'xemacs) '(t)
2132               '(:help "Mark unread articles in this group as read"))]
2133          ["Catchup all" gnus-summary-catchup-all t]
2134          ["Catchup to here" gnus-summary-catchup-to-here t]
2135          ["Catchup from here" gnus-summary-catchup-from-here t]
2136          ["Catchup region" gnus-summary-mark-region-as-read t]
2137          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2138         ("Mark Various"
2139          ["Tick" gnus-summary-tick-article-forward t]
2140          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2141          ["Remove marks" gnus-summary-clear-mark-forward t]
2142          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2143          ["Set bookmark" gnus-summary-set-bookmark t]
2144          ["Remove bookmark" gnus-summary-remove-bookmark t])
2145         ("Limit to"
2146          ["Marks..." gnus-summary-limit-to-marks t]
2147          ["Subject..." gnus-summary-limit-to-subject t]
2148          ["Author..." gnus-summary-limit-to-author t]
2149          ["Age..." gnus-summary-limit-to-age t]
2150          ["Extra..." gnus-summary-limit-to-extra t]
2151          ["Score" gnus-summary-limit-to-score t]
2152          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2153          ["Unread" gnus-summary-limit-to-unread t]
2154          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2155          ["Articles" gnus-summary-limit-to-articles t]
2156          ["Pop limit" gnus-summary-pop-limit t]
2157          ["Show dormant" gnus-summary-limit-include-dormant t]
2158          ["Hide childless dormant"
2159           gnus-summary-limit-exclude-childless-dormant t]
2160          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2161          ["Hide marked" gnus-summary-limit-exclude-marks t]
2162          ["Show expunged" gnus-summary-limit-include-expunged t])
2163         ("Process Mark"
2164          ["Set mark" gnus-summary-mark-as-processable t]
2165          ["Remove mark" gnus-summary-unmark-as-processable t]
2166          ["Remove all marks" gnus-summary-unmark-all-processable t]
2167          ["Mark above" gnus-uu-mark-over t]
2168          ["Mark series" gnus-uu-mark-series t]
2169          ["Mark region" gnus-uu-mark-region t]
2170          ["Unmark region" gnus-uu-unmark-region t]
2171          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2172          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2173          ["Mark all" gnus-uu-mark-all t]
2174          ["Mark buffer" gnus-uu-mark-buffer t]
2175          ["Mark sparse" gnus-uu-mark-sparse t]
2176          ["Mark thread" gnus-uu-mark-thread t]
2177          ["Unmark thread" gnus-uu-unmark-thread t]
2178          ("Process Mark Sets"
2179           ["Kill" gnus-summary-kill-process-mark t]
2180           ["Yank" gnus-summary-yank-process-mark
2181            gnus-newsgroup-process-stack]
2182           ["Save" gnus-summary-save-process-mark t]))
2183         ("Scroll article"
2184          ["Page forward" gnus-summary-next-page
2185           ,@(if (featurep 'xemacs) '(t)
2186               '(:help "Show next page of article"))]
2187          ["Page backward" gnus-summary-prev-page
2188           ,@(if (featurep 'xemacs) '(t)
2189               '(:help "Show previous page of article"))]
2190          ["Line forward" gnus-summary-scroll-up t])
2191         ("Move"
2192          ["Next unread article" gnus-summary-next-unread-article t]
2193          ["Previous unread article" gnus-summary-prev-unread-article t]
2194          ["Next article" gnus-summary-next-article t]
2195          ["Previous article" gnus-summary-prev-article t]
2196          ["Next unread subject" gnus-summary-next-unread-subject t]
2197          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2198          ["Next article same subject" gnus-summary-next-same-subject t]
2199          ["Previous article same subject" gnus-summary-prev-same-subject t]
2200          ["First unread article" gnus-summary-first-unread-article t]
2201          ["Best unread article" gnus-summary-best-unread-article t]
2202          ["Go to subject number..." gnus-summary-goto-subject t]
2203          ["Go to article number..." gnus-summary-goto-article t]
2204          ["Go to the last article" gnus-summary-goto-last-article t]
2205          ["Pop article off history" gnus-summary-pop-article t])
2206         ("Sort"
2207          ["Sort by number" gnus-summary-sort-by-number t]
2208          ["Sort by author" gnus-summary-sort-by-author t]
2209          ["Sort by subject" gnus-summary-sort-by-subject t]
2210          ["Sort by date" gnus-summary-sort-by-date t]
2211          ["Sort by score" gnus-summary-sort-by-score t]
2212          ["Sort by lines" gnus-summary-sort-by-lines t]
2213          ["Sort by characters" gnus-summary-sort-by-chars t]
2214          ["Original sort" gnus-summary-sort-by-original t])
2215         ("Help"
2216          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2217          ["Describe group" gnus-summary-describe-group t]
2218          ["Read manual" gnus-info-find-node t])
2219         ("Modes"
2220          ["Pick and read" gnus-pick-mode t]
2221          ["Binary" gnus-binary-mode t])
2222         ("Regeneration"
2223          ["Regenerate" gnus-summary-prepare t]
2224          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2225          ["Toggle threading" gnus-summary-toggle-threads t])
2226         ["See old articles" gnus-summary-insert-old-articles t]
2227         ["See new articles" gnus-summary-insert-new-articles t]
2228         ["Filter articles..." gnus-summary-execute-command t]
2229         ["Run command on subjects..." gnus-summary-universal-argument t]
2230         ["Search articles forward..." gnus-summary-search-article-forward t]
2231         ["Search articles backward..." gnus-summary-search-article-backward t]
2232         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2233         ["Expand window" gnus-summary-expand-window t]
2234         ["Expire expirable articles" gnus-summary-expire-articles
2235          (gnus-check-backend-function
2236           'request-expire-articles gnus-newsgroup-name)]
2237         ["Edit local kill file" gnus-summary-edit-local-kill t]
2238         ["Edit main kill file" gnus-summary-edit-global-kill t]
2239         ["Edit group parameters" gnus-summary-edit-parameters t]
2240         ["Customize group parameters" gnus-summary-customize-parameters t]
2241         ["Send a bug report" gnus-bug t]
2242         ("Exit"
2243          ["Catchup and exit" gnus-summary-catchup-and-exit
2244           ,@(if (featurep 'xemacs) '(t)
2245               '(:help "Mark unread articles in this group as read, then exit"))]
2246          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2247          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2248          ["Exit group" gnus-summary-exit
2249           ,@(if (featurep 'xemacs) '(t)
2250               '(:help "Exit current group, return to group selection mode"))]
2251          ["Exit group without updating" gnus-summary-exit-no-update t]
2252          ["Exit and goto next group" gnus-summary-next-group t]
2253          ["Exit and goto prev group" gnus-summary-prev-group t]
2254          ["Reselect group" gnus-summary-reselect-current-group t]
2255          ["Rescan group" gnus-summary-rescan-group t]
2256          ["Update dribble" gnus-summary-save-newsrc t])))
2257
2258     (gnus-run-hooks 'gnus-summary-menu-hook)))
2259
2260 (defvar gnus-summary-tool-bar-map nil)
2261
2262 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2263 (defun gnus-summary-make-tool-bar ()
2264   (if (and (fboundp 'tool-bar-add-item-from-menu)
2265            (default-value 'tool-bar-mode)
2266            (not gnus-summary-tool-bar-map))
2267       (setq gnus-summary-tool-bar-map
2268             (let ((tool-bar-map (make-sparse-keymap))
2269                   (load-path (mm-image-load-path)))
2270               (tool-bar-add-item-from-menu
2271                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2272               (tool-bar-add-item-from-menu
2273                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2274               (tool-bar-add-item-from-menu
2275                'gnus-summary-post-news "post" gnus-summary-mode-map)
2276               (tool-bar-add-item-from-menu
2277                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2278               (tool-bar-add-item-from-menu
2279                'gnus-summary-followup "followup" gnus-summary-mode-map)
2280               (tool-bar-add-item-from-menu
2281                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2282               (tool-bar-add-item-from-menu
2283                'gnus-summary-reply "reply" gnus-summary-mode-map)
2284               (tool-bar-add-item-from-menu
2285                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2286               (tool-bar-add-item-from-menu
2287                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2288               (tool-bar-add-item-from-menu
2289                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2290               (tool-bar-add-item-from-menu
2291                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2292               (tool-bar-add-item-from-menu
2293                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2294               (tool-bar-add-item-from-menu
2295                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2296               (tool-bar-add-item-from-menu
2297                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2298               (tool-bar-add-item-from-menu
2299                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2300               tool-bar-map)))
2301   (if gnus-summary-tool-bar-map
2302       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2303
2304 (defun gnus-score-set-default (var value)
2305   "A version of set that updates the GNU Emacs menu-bar."
2306   (set var value)
2307   ;; It is the message that forces the active status to be updated.
2308   (message ""))
2309
2310 (defun gnus-make-score-map (type)
2311   "Make a summary score map of type TYPE."
2312   (if t
2313       nil
2314     (let ((headers '(("author" "from" string)
2315                      ("subject" "subject" string)
2316                      ("article body" "body" string)
2317                      ("article head" "head" string)
2318                      ("xref" "xref" string)
2319                      ("extra header" "extra" string)
2320                      ("lines" "lines" number)
2321                      ("followups to author" "followup" string)))
2322           (types '((number ("less than" <)
2323                            ("greater than" >)
2324                            ("equal" =))
2325                    (string ("substring" s)
2326                            ("exact string" e)
2327                            ("fuzzy string" f)
2328                            ("regexp" r))))
2329           (perms '(("temporary" (current-time-string))
2330                    ("permanent" nil)
2331                    ("immediate" now)))
2332           header)
2333       (list
2334        (apply
2335         'nconc
2336         (list
2337          (if (eq type 'lower)
2338              "Lower score"
2339            "Increase score"))
2340         (let (outh)
2341           (while headers
2342             (setq header (car headers))
2343             (setq outh
2344                   (cons
2345                    (apply
2346                     'nconc
2347                     (list (car header))
2348                     (let ((ts (cdr (assoc (nth 2 header) types)))
2349                           outt)
2350                       (while ts
2351                         (setq outt
2352                               (cons
2353                                (apply
2354                                 'nconc
2355                                 (list (caar ts))
2356                                 (let ((ps perms)
2357                                       outp)
2358                                   (while ps
2359                                     (setq outp
2360                                           (cons
2361                                            (vector
2362                                             (caar ps)
2363                                             (list
2364                                              'gnus-summary-score-entry
2365                                              (nth 1 header)
2366                                              (if (or (string= (nth 1 header)
2367                                                               "head")
2368                                                      (string= (nth 1 header)
2369                                                               "body"))
2370                                                  ""
2371                                                (list 'gnus-summary-header
2372                                                      (nth 1 header)))
2373                                              (list 'quote (nth 1 (car ts)))
2374                                              (list 'gnus-score-delta-default
2375                                                    nil)
2376                                              (nth 1 (car ps))
2377                                              t)
2378                                             t)
2379                                            outp))
2380                                     (setq ps (cdr ps)))
2381                                   (list (nreverse outp))))
2382                                outt))
2383                         (setq ts (cdr ts)))
2384                       (list (nreverse outt))))
2385                    outh))
2386             (setq headers (cdr headers)))
2387           (list (nreverse outh))))))))
2388
2389 \f
2390
2391 (defun gnus-summary-mode (&optional group)
2392   "Major mode for reading articles.
2393
2394 All normal editing commands are switched off.
2395 \\<gnus-summary-mode-map>
2396 Each line in this buffer represents one article.  To read an
2397 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2398 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2399 respectively.
2400
2401 You can also post articles and send mail from this buffer.  To
2402 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2403 of an article, type `\\[gnus-summary-reply]'.
2404
2405 There are approx. one gazillion commands you can execute in this
2406 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2407
2408 The following commands are available:
2409
2410 \\{gnus-summary-mode-map}"
2411   (interactive)
2412   (kill-all-local-variables)
2413   (when (gnus-visual-p 'summary-menu 'menu)
2414     (gnus-summary-make-menu-bar)
2415     (gnus-summary-make-tool-bar))
2416   (gnus-summary-make-local-variables)
2417   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2418     (gnus-summary-make-local-variables))
2419   (gnus-make-thread-indent-array)
2420   (gnus-simplify-mode-line)
2421   (setq major-mode 'gnus-summary-mode)
2422   (setq mode-name "Summary")
2423   (make-local-variable 'minor-mode-alist)
2424   (use-local-map gnus-summary-mode-map)
2425   (buffer-disable-undo)
2426   (setq buffer-read-only t)             ;Disable modification
2427   (setq truncate-lines t)
2428   (setq selective-display t)
2429   (setq selective-display-ellipses t)   ;Display `...'
2430   (gnus-summary-set-display-table)
2431   (gnus-set-default-directory)
2432   (setq gnus-newsgroup-name group)
2433   (make-local-variable 'gnus-summary-line-format)
2434   (make-local-variable 'gnus-summary-line-format-spec)
2435   (make-local-variable 'gnus-summary-dummy-line-format)
2436   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2437   (make-local-variable 'gnus-summary-mark-positions)
2438   (make-local-hook 'pre-command-hook)
2439   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2440   (gnus-run-hooks 'gnus-summary-mode-hook)
2441   (turn-on-gnus-mailing-list-mode)
2442   (mm-enable-multibyte)
2443   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2444   (gnus-update-summary-mark-positions))
2445
2446 (defun gnus-summary-make-local-variables ()
2447   "Make all the local summary buffer variables."
2448   (let (global)
2449     (dolist (local gnus-summary-local-variables)
2450       (if (consp local)
2451           (progn
2452             (if (eq (cdr local) 'global)
2453                 ;; Copy the global value of the variable.
2454                 (setq global (symbol-value (car local)))
2455               ;; Use the value from the list.
2456               (setq global (eval (cdr local))))
2457             (set (make-local-variable (car local)) global))
2458         ;; Simple nil-valued local variable.
2459         (set (make-local-variable local) nil)))))
2460
2461 (defun gnus-summary-clear-local-variables ()
2462   (let ((locals gnus-summary-local-variables))
2463     (while locals
2464       (if (consp (car locals))
2465           (and (vectorp (caar locals))
2466                (set (caar locals) nil))
2467         (and (vectorp (car locals))
2468              (set (car locals) nil)))
2469       (setq locals (cdr locals)))))
2470
2471 ;; Summary data functions.
2472
2473 (defmacro gnus-data-number (data)
2474   `(car ,data))
2475
2476 (defmacro gnus-data-set-number (data number)
2477   `(setcar ,data ,number))
2478
2479 (defmacro gnus-data-mark (data)
2480   `(nth 1 ,data))
2481
2482 (defmacro gnus-data-set-mark (data mark)
2483   `(setcar (nthcdr 1 ,data) ,mark))
2484
2485 (defmacro gnus-data-pos (data)
2486   `(nth 2 ,data))
2487
2488 (defmacro gnus-data-set-pos (data pos)
2489   `(setcar (nthcdr 2 ,data) ,pos))
2490
2491 (defmacro gnus-data-header (data)
2492   `(nth 3 ,data))
2493
2494 (defmacro gnus-data-set-header (data header)
2495   `(setf (nth 3 ,data) ,header))
2496
2497 (defmacro gnus-data-level (data)
2498   `(nth 4 ,data))
2499
2500 (defmacro gnus-data-unread-p (data)
2501   `(= (nth 1 ,data) gnus-unread-mark))
2502
2503 (defmacro gnus-data-read-p (data)
2504   `(/= (nth 1 ,data) gnus-unread-mark))
2505
2506 (defmacro gnus-data-pseudo-p (data)
2507   `(consp (nth 3 ,data)))
2508
2509 (defmacro gnus-data-find (number)
2510   `(assq ,number gnus-newsgroup-data))
2511
2512 (defmacro gnus-data-find-list (number &optional data)
2513   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2514      (memq (assq ,number bdata)
2515            bdata)))
2516
2517 (defmacro gnus-data-make (number mark pos header level)
2518   `(list ,number ,mark ,pos ,header ,level))
2519
2520 (defun gnus-data-enter (after-article number mark pos header level offset)
2521   (let ((data (gnus-data-find-list after-article)))
2522     (unless data
2523       (error "No such article: %d" after-article))
2524     (setcdr data (cons (gnus-data-make number mark pos header level)
2525                        (cdr data)))
2526     (setq gnus-newsgroup-data-reverse nil)
2527     (gnus-data-update-list (cddr data) offset)))
2528
2529 (defun gnus-data-enter-list (after-article list &optional offset)
2530   (when list
2531     (let ((data (and after-article (gnus-data-find-list after-article)))
2532           (ilist list))
2533       (if (not (or data
2534                    after-article))
2535           (let ((odata gnus-newsgroup-data))
2536             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2537             (when offset
2538               (gnus-data-update-list odata offset)))
2539       ;; Find the last element in the list to be spliced into the main
2540         ;; list.
2541         (while (cdr list)
2542           (setq list (cdr list)))
2543         (if (not data)
2544             (progn
2545               (setcdr list gnus-newsgroup-data)
2546               (setq gnus-newsgroup-data ilist)
2547               (when offset
2548                 (gnus-data-update-list (cdr list) offset)))
2549           (setcdr list (cdr data))
2550           (setcdr data ilist)
2551           (when offset
2552             (gnus-data-update-list (cdr list) offset))))
2553       (setq gnus-newsgroup-data-reverse nil))))
2554
2555 (defun gnus-data-remove (article &optional offset)
2556   (let ((data gnus-newsgroup-data))
2557     (if (= (gnus-data-number (car data)) article)
2558         (progn
2559           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2560                 gnus-newsgroup-data-reverse nil)
2561           (when offset
2562             (gnus-data-update-list gnus-newsgroup-data offset)))
2563       (while (cdr data)
2564         (when (= (gnus-data-number (cadr data)) article)
2565           (setcdr data (cddr data))
2566           (when offset
2567             (gnus-data-update-list (cdr data) offset))
2568           (setq data nil
2569                 gnus-newsgroup-data-reverse nil))
2570         (setq data (cdr data))))))
2571
2572 (defmacro gnus-data-list (backward)
2573   `(if ,backward
2574        (or gnus-newsgroup-data-reverse
2575            (setq gnus-newsgroup-data-reverse
2576                  (reverse gnus-newsgroup-data)))
2577      gnus-newsgroup-data))
2578
2579 (defun gnus-data-update-list (data offset)
2580   "Add OFFSET to the POS of all data entries in DATA."
2581   (setq gnus-newsgroup-data-reverse nil)
2582   (while data
2583     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2584     (setq data (cdr data))))
2585
2586 (defun gnus-summary-article-pseudo-p (article)
2587   "Say whether this article is a pseudo article or not."
2588   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2589
2590 (defmacro gnus-summary-article-sparse-p (article)
2591   "Say whether this article is a sparse article or not."
2592   `(memq ,article gnus-newsgroup-sparse))
2593
2594 (defmacro gnus-summary-article-ancient-p (article)
2595   "Say whether this article is a sparse article or not."
2596   `(memq ,article gnus-newsgroup-ancient))
2597
2598 (defun gnus-article-parent-p (number)
2599   "Say whether this article is a parent or not."
2600   (let ((data (gnus-data-find-list number)))
2601     (and (cdr data)              ; There has to be an article after...
2602          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2603             (gnus-data-level (nth 1 data))))))
2604
2605 (defun gnus-article-children (number)
2606   "Return a list of all children to NUMBER."
2607   (let* ((data (gnus-data-find-list number))
2608          (level (gnus-data-level (car data)))
2609          children)
2610     (setq data (cdr data))
2611     (while (and data
2612                 (= (gnus-data-level (car data)) (1+ level)))
2613       (push (gnus-data-number (car data)) children)
2614       (setq data (cdr data)))
2615     children))
2616
2617 (defmacro gnus-summary-skip-intangible ()
2618   "If the current article is intangible, then jump to a different article."
2619   '(let ((to (get-text-property (point) 'gnus-intangible)))
2620      (and to (gnus-summary-goto-subject to))))
2621
2622 (defmacro gnus-summary-article-intangible-p ()
2623   "Say whether this article is intangible or not."
2624   '(get-text-property (point) 'gnus-intangible))
2625
2626 (defun gnus-article-read-p (article)
2627   "Say whether ARTICLE is read or not."
2628   (not (or (memq article gnus-newsgroup-marked)
2629            (memq article gnus-newsgroup-unreads)
2630            (memq article gnus-newsgroup-unselected)
2631            (memq article gnus-newsgroup-dormant))))
2632
2633 ;; Some summary mode macros.
2634
2635 (defmacro gnus-summary-article-number ()
2636   "The article number of the article on the current line.
2637 If there isn's an article number here, then we return the current
2638 article number."
2639   '(progn
2640      (gnus-summary-skip-intangible)
2641      (or (get-text-property (point) 'gnus-number)
2642          (gnus-summary-last-subject))))
2643
2644 (defmacro gnus-summary-article-header (&optional number)
2645   "Return the header of article NUMBER."
2646   `(gnus-data-header (gnus-data-find
2647                       ,(or number '(gnus-summary-article-number)))))
2648
2649 (defmacro gnus-summary-thread-level (&optional number)
2650   "Return the level of thread that starts with article NUMBER."
2651   `(if (and (eq gnus-summary-make-false-root 'dummy)
2652             (get-text-property (point) 'gnus-intangible))
2653        0
2654      (gnus-data-level (gnus-data-find
2655                        ,(or number '(gnus-summary-article-number))))))
2656
2657 (defmacro gnus-summary-article-mark (&optional number)
2658   "Return the mark of article NUMBER."
2659   `(gnus-data-mark (gnus-data-find
2660                     ,(or number '(gnus-summary-article-number)))))
2661
2662 (defmacro gnus-summary-article-pos (&optional number)
2663   "Return the position of the line of article NUMBER."
2664   `(gnus-data-pos (gnus-data-find
2665                    ,(or number '(gnus-summary-article-number)))))
2666
2667 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2668 (defmacro gnus-summary-article-subject (&optional number)
2669   "Return current subject string or nil if nothing."
2670   `(let ((headers
2671           ,(if number
2672                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2673              '(gnus-data-header (assq (gnus-summary-article-number)
2674                                       gnus-newsgroup-data)))))
2675      (and headers
2676           (vectorp headers)
2677           (mail-header-subject headers))))
2678
2679 (defmacro gnus-summary-article-score (&optional number)
2680   "Return current article score."
2681   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2682                   gnus-newsgroup-scored))
2683        gnus-summary-default-score 0))
2684
2685 (defun gnus-summary-article-children (&optional number)
2686   "Return a list of article numbers that are children of article NUMBER."
2687   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2688          (level (gnus-data-level (car data)))
2689          l children)
2690     (while (and (setq data (cdr data))
2691                 (> (setq l (gnus-data-level (car data))) level))
2692       (and (= (1+ level) l)
2693            (push (gnus-data-number (car data))
2694                  children)))
2695     (nreverse children)))
2696
2697 (defun gnus-summary-article-parent (&optional number)
2698   "Return the article number of the parent of article NUMBER."
2699   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2700                                     (gnus-data-list t)))
2701          (level (gnus-data-level (car data))))
2702     (if (zerop level)
2703         ()                              ; This is a root.
2704       ;; We search until we find an article with a level less than
2705       ;; this one.  That function has to be the parent.
2706       (while (and (setq data (cdr data))
2707                   (not (< (gnus-data-level (car data)) level))))
2708       (and data (gnus-data-number (car data))))))
2709
2710 (defun gnus-unread-mark-p (mark)
2711   "Say whether MARK is the unread mark."
2712   (= mark gnus-unread-mark))
2713
2714 (defun gnus-read-mark-p (mark)
2715   "Say whether MARK is one of the marks that mark as read.
2716 This is all marks except unread, ticked, dormant, and expirable."
2717   (not (or (= mark gnus-unread-mark)
2718            (= mark gnus-ticked-mark)
2719            (= mark gnus-dormant-mark)
2720            (= mark gnus-expirable-mark))))
2721
2722 (defmacro gnus-article-mark (number)
2723   "Return the MARK of article NUMBER.
2724 This macro should only be used when computing the mark the \"first\"
2725 time; i.e., when generating the summary lines.  After that,
2726 `gnus-summary-article-mark' should be used to examine the
2727 marks of articles."
2728   `(cond
2729     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2730     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2731     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2732     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2733     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2734     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2735     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2736     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2737            gnus-ancient-mark))))
2738
2739 ;; Saving hidden threads.
2740
2741 (defmacro gnus-save-hidden-threads (&rest forms)
2742   "Save hidden threads, eval FORMS, and restore the hidden threads."
2743   (let ((config (make-symbol "config")))
2744     `(let ((,config (gnus-hidden-threads-configuration)))
2745        (unwind-protect
2746            (save-excursion
2747              ,@forms)
2748          (gnus-restore-hidden-threads-configuration ,config)))))
2749 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2750 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2751
2752 (defun gnus-data-compute-positions ()
2753   "Compute the positions of all articles."
2754   (setq gnus-newsgroup-data-reverse nil)
2755   (let ((data gnus-newsgroup-data))
2756     (save-excursion
2757       (gnus-save-hidden-threads
2758         (gnus-summary-show-all-threads)
2759         (goto-char (point-min))
2760         (while data
2761           (while (get-text-property (point) 'gnus-intangible)
2762             (forward-line 1))
2763           (gnus-data-set-pos (car data) (+ (point) 3))
2764           (setq data (cdr data))
2765           (forward-line 1))))))
2766
2767 (defun gnus-hidden-threads-configuration ()
2768   "Return the current hidden threads configuration."
2769   (save-excursion
2770     (let (config)
2771       (goto-char (point-min))
2772       (while (search-forward "\r" nil t)
2773         (push (1- (point)) config))
2774       config)))
2775
2776 (defun gnus-restore-hidden-threads-configuration (config)
2777   "Restore hidden threads configuration from CONFIG."
2778   (save-excursion
2779     (let (point buffer-read-only)
2780       (while (setq point (pop config))
2781         (when (and (< point (point-max))
2782                    (goto-char point)
2783                    (eq (char-after) ?\n))
2784           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2785
2786 ;; Various summary mode internalish functions.
2787
2788 (defun gnus-mouse-pick-article (e)
2789   (interactive "e")
2790   (mouse-set-point e)
2791   (gnus-summary-next-page nil t))
2792
2793 (defun gnus-summary-set-display-table ()
2794   "Change the display table.
2795 Odd characters have a tendency to mess
2796 up nicely formatted displays - we make all possible glyphs
2797 display only a single character."
2798
2799   ;; We start from the standard display table, if any.
2800   (let ((table (or (copy-sequence standard-display-table)
2801                    (make-display-table)))
2802         (i 32))
2803     ;; Nix out all the control chars...
2804     (while (>= (setq i (1- i)) 0)
2805       (aset table i [??]))
2806    ;; ... but not newline and cr, of course.  (cr is necessary for the
2807     ;; selective display).
2808     (aset table ?\n nil)
2809     (aset table ?\r nil)
2810     ;; We keep TAB as well.
2811     (aset table ?\t nil)
2812     ;; We nix out any glyphs over 126 that are not set already.
2813     (let ((i 256))
2814       (while (>= (setq i (1- i)) 127)
2815         ;; Only modify if the entry is nil.
2816         (unless (aref table i)
2817           (aset table i [??]))))
2818     (setq buffer-display-table table)))
2819
2820 (defun gnus-summary-set-article-display-arrow (pos)
2821   "Update the overlay arrow to point to line at position POS."
2822   (when (and gnus-summary-display-arrow
2823              (boundp 'overlay-arrow-position)
2824              (boundp 'overlay-arrow-string))
2825     (save-excursion
2826       (goto-char pos)
2827       (beginning-of-line)
2828       (unless overlay-arrow-position
2829         (setq overlay-arrow-position (make-marker)))
2830       (setq overlay-arrow-string "=>"
2831             overlay-arrow-position (set-marker overlay-arrow-position
2832                                                (point)
2833                                                (current-buffer))))))
2834
2835 (defun gnus-summary-buffer-name (group)
2836   "Return the summary buffer name of GROUP."
2837   (concat "*Summary " (gnus-group-decoded-name group) "*"))
2838
2839 (defun gnus-summary-setup-buffer (group)
2840   "Initialize summary buffer."
2841   (let ((buffer (gnus-summary-buffer-name group))
2842         (dead-name (concat "*Dead Summary "
2843                            (gnus-group-decoded-name group) "*")))
2844     ;; If a dead summary buffer exists, we kill it.
2845     (when (gnus-buffer-live-p dead-name)
2846       (gnus-kill-buffer dead-name))
2847     (if (get-buffer buffer)
2848         (progn
2849           (set-buffer buffer)
2850           (setq gnus-summary-buffer (current-buffer))
2851           (not gnus-newsgroup-prepared))
2852       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2853       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2854       (gnus-summary-mode group)
2855       (when gnus-carpal
2856         (gnus-carpal-setup-buffer 'summary))
2857       (unless gnus-single-article-buffer
2858         (make-local-variable 'gnus-article-buffer)
2859         (make-local-variable 'gnus-article-current)
2860         (make-local-variable 'gnus-original-article-buffer))
2861       (setq gnus-newsgroup-name group)
2862       ;; Set any local variables in the group parameters.
2863       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2864       t)))
2865
2866 (defun gnus-set-global-variables ()
2867   "Set the global equivalents of the buffer-local variables.
2868 They are set to the latest values they had.  These reflect the summary
2869 buffer that was in action when the last article was fetched."
2870   (when (eq major-mode 'gnus-summary-mode)
2871     (setq gnus-summary-buffer (current-buffer))
2872     (let ((name gnus-newsgroup-name)
2873           (marked gnus-newsgroup-marked)
2874           (unread gnus-newsgroup-unreads)
2875           (headers gnus-current-headers)
2876           (data gnus-newsgroup-data)
2877           (summary gnus-summary-buffer)
2878           (article-buffer gnus-article-buffer)
2879           (original gnus-original-article-buffer)
2880           (gac gnus-article-current)
2881           (reffed gnus-reffed-article-number)
2882           (score-file gnus-current-score-file)
2883           (default-charset gnus-newsgroup-charset)
2884           vlist)
2885       (let ((locals gnus-newsgroup-variables))
2886         (while locals
2887           (if (consp (car locals))
2888               (push (eval (caar locals)) vlist)
2889             (push (eval (car locals)) vlist))
2890           (setq locals (cdr locals)))
2891         (setq vlist (nreverse vlist)))
2892       (save-excursion
2893         (set-buffer gnus-group-buffer)
2894         (setq gnus-newsgroup-name name
2895               gnus-newsgroup-marked marked
2896               gnus-newsgroup-unreads unread
2897               gnus-current-headers headers
2898               gnus-newsgroup-data data
2899               gnus-article-current gac
2900               gnus-summary-buffer summary
2901               gnus-article-buffer article-buffer
2902               gnus-original-article-buffer original
2903               gnus-reffed-article-number reffed
2904               gnus-current-score-file score-file
2905               gnus-newsgroup-charset default-charset)
2906         (let ((locals gnus-newsgroup-variables))
2907           (while locals
2908             (if (consp (car locals))
2909                 (set (caar locals) (pop vlist))
2910               (set (car locals) (pop vlist)))
2911             (setq locals (cdr locals))))
2912         ;; The article buffer also has local variables.
2913         (when (gnus-buffer-live-p gnus-article-buffer)
2914           (set-buffer gnus-article-buffer)
2915           (setq gnus-summary-buffer summary))))))
2916
2917 (defun gnus-summary-article-unread-p (article)
2918   "Say whether ARTICLE is unread or not."
2919   (memq article gnus-newsgroup-unreads))
2920
2921 (defun gnus-summary-first-article-p (&optional article)
2922   "Return whether ARTICLE is the first article in the buffer."
2923   (if (not (setq article (or article (gnus-summary-article-number))))
2924       nil
2925     (eq article (caar gnus-newsgroup-data))))
2926
2927 (defun gnus-summary-last-article-p (&optional article)
2928   "Return whether ARTICLE is the last article in the buffer."
2929   (if (not (setq article (or article (gnus-summary-article-number))))
2930       ;; All non-existent numbers are the last article.  :-)
2931       t
2932     (not (cdr (gnus-data-find-list article)))))
2933
2934 (defun gnus-make-thread-indent-array ()
2935   (let ((n 200))
2936     (unless (and gnus-thread-indent-array
2937                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2938       (setq gnus-thread-indent-array (make-vector 201 "")
2939             gnus-thread-indent-array-level gnus-thread-indent-level)
2940       (while (>= n 0)
2941         (aset gnus-thread-indent-array n
2942               (make-string (* n gnus-thread-indent-level) ? ))
2943         (setq n (1- n))))))
2944
2945 (defun gnus-update-summary-mark-positions ()
2946   "Compute where the summary marks are to go."
2947   (save-excursion
2948     (when (gnus-buffer-exists-p gnus-summary-buffer)
2949       (set-buffer gnus-summary-buffer))
2950     (let ((gnus-replied-mark 129)
2951           (gnus-score-below-mark 130)
2952           (gnus-score-over-mark 130)
2953           (gnus-download-mark 131)
2954           (spec gnus-summary-line-format-spec)
2955           gnus-visual pos)
2956       (save-excursion
2957         (gnus-set-work-buffer)
2958         (let ((gnus-summary-line-format-spec spec)
2959               (gnus-newsgroup-downloadable '((0 . t))))
2960           (gnus-summary-insert-line
2961            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
2962            0 nil 128 t nil "" nil 1)
2963           (goto-char (point-min))
2964           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2965                                              (- (point) 2)))))
2966           (goto-char (point-min))
2967           (push (cons 'replied (and (search-forward "\201" nil t)
2968                                     (- (point) 2)))
2969                 pos)
2970           (goto-char (point-min))
2971           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2972                 pos)
2973           (goto-char (point-min))
2974           (push (cons 'download
2975                       (and (search-forward "\203" nil t) (- (point) 2)))
2976                 pos)))
2977       (setq gnus-summary-mark-positions pos))))
2978
2979 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2980   "Insert a dummy root in the summary buffer."
2981   (beginning-of-line)
2982   (gnus-add-text-properties
2983    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2984    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2985
2986 (defun gnus-summary-extract-address-component (from)
2987   (or (car (funcall gnus-extract-address-components from))
2988       from))
2989
2990 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
2991   (let ((mail-parse-charset gnus-newsgroup-charset)
2992         ; Is it really necessary to do this next part for each summary line?
2993         ; Luckily, doesn't seem to slow things down much.
2994         (mail-parse-ignored-charsets
2995          (save-excursion (set-buffer gnus-summary-buffer)
2996                          gnus-newsgroup-ignored-charsets)))
2997     (or
2998      (and gnus-ignored-from-addresses
2999           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3000           (let ((extra-headers (mail-header-extra header))
3001                 to
3002                 newsgroups)
3003             (cond
3004              ((setq to (cdr (assq 'To extra-headers)))
3005               (concat "-> "
3006                       (inline
3007                         (gnus-summary-extract-address-component
3008                          (funcall gnus-decode-encoded-word-function to)))))
3009              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3010               (concat "=> " newsgroups)))))
3011      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3012
3013 (defun gnus-summary-insert-line (gnus-tmp-header
3014                                  gnus-tmp-level gnus-tmp-current
3015                                  gnus-tmp-unread gnus-tmp-replied
3016                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3017                                  &optional gnus-tmp-dummy gnus-tmp-score
3018                                  gnus-tmp-process)
3019   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3020          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3021          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3022          (gnus-tmp-score-char
3023           (if (or (null gnus-summary-default-score)
3024                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3025                       gnus-summary-zcore-fuzz))
3026               ?                         ;Whitespace
3027             (if (< gnus-tmp-score gnus-summary-default-score)
3028                 gnus-score-below-mark gnus-score-over-mark)))
3029          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3030          (gnus-tmp-replied
3031           (cond (gnus-tmp-process gnus-process-mark)
3032                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3033                  gnus-cached-mark)
3034                 (gnus-tmp-replied gnus-replied-mark)
3035                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3036                  gnus-forwarded-mark)
3037                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3038                  gnus-saved-mark)
3039                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3040                  gnus-recent-mark)
3041                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3042                  gnus-unseen-mark)
3043                 (t gnus-no-mark)))
3044          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3045          (gnus-tmp-name
3046           (cond
3047            ((string-match "<[^>]+> *$" gnus-tmp-from)
3048             (let ((beg (match-beginning 0)))
3049               (or (and (string-match "^\".+\"" gnus-tmp-from)
3050                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3051                   (substring gnus-tmp-from 0 beg))))
3052            ((string-match "(.+)" gnus-tmp-from)
3053             (substring gnus-tmp-from
3054                        (1+ (match-beginning 0)) (1- (match-end 0))))
3055            (t gnus-tmp-from)))
3056          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3057          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3058          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3059          (buffer-read-only nil))
3060     (when (string= gnus-tmp-name "")
3061       (setq gnus-tmp-name gnus-tmp-from))
3062     (unless (numberp gnus-tmp-lines)
3063       (setq gnus-tmp-lines -1))
3064     (if (= gnus-tmp-lines -1)
3065         (setq gnus-tmp-lines "?")
3066       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3067     (gnus-put-text-property
3068      (point)
3069      (progn (eval gnus-summary-line-format-spec) (point))
3070      'gnus-number gnus-tmp-number)
3071     (when (gnus-visual-p 'summary-highlight 'highlight)
3072       (forward-line -1)
3073       (gnus-run-hooks 'gnus-summary-update-hook)
3074       (forward-line 1))))
3075
3076 (defun gnus-summary-update-line (&optional dont-update)
3077   "Update summary line after change."
3078   (when (and gnus-summary-default-score
3079              (not gnus-summary-inhibit-highlight))
3080     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3081            (article (gnus-summary-article-number))
3082            (score (gnus-summary-article-score article)))
3083       (unless dont-update
3084         (if (and gnus-summary-mark-below
3085                  (< (gnus-summary-article-score)
3086                     gnus-summary-mark-below))
3087             ;; This article has a low score, so we mark it as read.
3088             (when (memq article gnus-newsgroup-unreads)
3089               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3090           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3091             ;; This article was previously marked as read on account
3092             ;; of a low score, but now it has risen, so we mark it as
3093             ;; unread.
3094             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3095         (gnus-summary-update-mark
3096          (if (or (null gnus-summary-default-score)
3097                  (<= (abs (- score gnus-summary-default-score))
3098                      gnus-summary-zcore-fuzz))
3099              ?                          ;Whitespace
3100            (if (< score gnus-summary-default-score)
3101                gnus-score-below-mark gnus-score-over-mark))
3102          'score))
3103       ;; Do visual highlighting.
3104       (when (gnus-visual-p 'summary-highlight 'highlight)
3105         (gnus-run-hooks 'gnus-summary-update-hook)))))
3106
3107 (defvar gnus-tmp-new-adopts nil)
3108
3109 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3110   "Return the number of articles in THREAD.
3111 This may be 0 in some cases -- if none of the articles in
3112 the thread are to be displayed."
3113   (let* ((number
3114          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3115           (cond
3116            ((not (listp thread))
3117             1)
3118            ((and (consp thread) (cdr thread))
3119             (apply
3120              '+ 1 (mapcar
3121                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3122            ((null thread)
3123             1)
3124            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3125             1)
3126            (t 0))))
3127     (when (and level (zerop level) gnus-tmp-new-adopts)
3128       (incf number
3129             (apply '+ (mapcar
3130                        'gnus-summary-number-of-articles-in-thread
3131                        gnus-tmp-new-adopts))))
3132     (if char
3133         (if (> number 1) gnus-not-empty-thread-mark
3134           gnus-empty-thread-mark)
3135       number)))
3136
3137 (defun gnus-summary-set-local-parameters (group)
3138   "Go through the local params of GROUP and set all variable specs in that list."
3139   (let ((params (gnus-group-find-parameter group))
3140         (vars '(quit-config))           ; Ignore quit-config.
3141         elem)
3142     (while params
3143       (setq elem (car params)
3144             params (cdr params))
3145       (and (consp elem)                 ; Has to be a cons.
3146            (consp (cdr elem))           ; The cdr has to be a list.
3147            (symbolp (car elem))         ; Has to be a symbol in there.
3148            (not (memq (car elem) vars))
3149            (ignore-errors               ; So we set it.
3150              (push (car elem) vars)
3151              (make-local-variable (car elem))
3152              (set (car elem) (eval (nth 1 elem))))))))
3153
3154 (defun gnus-summary-read-group (group &optional show-all no-article
3155                                       kill-buffer no-display backward
3156                                       select-articles)
3157   "Start reading news in newsgroup GROUP.
3158 If SHOW-ALL is non-nil, already read articles are also listed.
3159 If NO-ARTICLE is non-nil, no article is selected initially.
3160 If NO-DISPLAY, don't generate a summary buffer."
3161   (let (result)
3162     (while (and group
3163                 (null (setq result
3164                             (let ((gnus-auto-select-next nil))
3165                               (or (gnus-summary-read-group-1
3166                                    group show-all no-article
3167                                    kill-buffer no-display
3168                                    select-articles)
3169                                   (setq show-all nil
3170                                         select-articles nil)))))
3171                 (eq gnus-auto-select-next 'quietly))
3172       (set-buffer gnus-group-buffer)
3173       ;; The entry function called above goes to the next
3174       ;; group automatically, so we go two groups back
3175       ;; if we are searching for the previous group.
3176       (when backward
3177         (gnus-group-prev-unread-group 2))
3178       (if (not (equal group (gnus-group-group-name)))
3179           (setq group (gnus-group-group-name))
3180         (setq group nil)))
3181     result))
3182
3183 (defun gnus-summary-read-group-1 (group show-all no-article
3184                                         kill-buffer no-display
3185                                         &optional select-articles)
3186   ;; Killed foreign groups can't be entered.
3187   ;;  (when (and (not (gnus-group-native-p group))
3188   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3189   ;;    (error "Dead non-native groups can't be entered"))
3190   (gnus-message 5 "Retrieving newsgroup: %s..."
3191                 (gnus-group-decoded-name group))
3192   (let* ((new-group (gnus-summary-setup-buffer group))
3193          (quit-config (gnus-group-quit-config group))
3194          (did-select (and new-group (gnus-select-newsgroup
3195                                      group show-all select-articles))))
3196     (cond
3197      ;; This summary buffer exists already, so we just select it.
3198      ((not new-group)
3199       (gnus-set-global-variables)
3200       (when kill-buffer
3201         (gnus-kill-or-deaden-summary kill-buffer))
3202       (gnus-configure-windows 'summary 'force)
3203       (gnus-set-mode-line 'summary)
3204       (gnus-summary-position-point)
3205       (message "")
3206       t)
3207      ;; We couldn't select this group.
3208      ((null did-select)
3209       (when (and (eq major-mode 'gnus-summary-mode)
3210                  (not (equal (current-buffer) kill-buffer)))
3211         (kill-buffer (current-buffer))
3212         (if (not quit-config)
3213             (progn
3214               ;; Update the info -- marks might need to be removed,
3215               ;; for instance.
3216               (gnus-summary-update-info)
3217               (set-buffer gnus-group-buffer)
3218               (gnus-group-jump-to-group group)
3219               (gnus-group-next-unread-group 1))
3220           (gnus-handle-ephemeral-exit quit-config)))
3221       (let ((grpinfo (gnus-get-info group)))
3222         (if (null (gnus-info-read grpinfo))
3223             (gnus-message 3 "Group %s contains no messages"
3224                           (gnus-group-decoded-name group))
3225           (gnus-message 3 "Can't select group")))
3226       nil)
3227      ;; The user did a `C-g' while prompting for number of articles,
3228      ;; so we exit this group.
3229      ((eq did-select 'quit)
3230       (and (eq major-mode 'gnus-summary-mode)
3231            (not (equal (current-buffer) kill-buffer))
3232            (kill-buffer (current-buffer)))
3233       (when kill-buffer
3234         (gnus-kill-or-deaden-summary kill-buffer))
3235       (if (not quit-config)
3236           (progn
3237             (set-buffer gnus-group-buffer)
3238             (gnus-group-jump-to-group group)
3239             (gnus-group-next-unread-group 1)
3240             (gnus-configure-windows 'group 'force))
3241         (gnus-handle-ephemeral-exit quit-config))
3242       ;; Finally signal the quit.
3243       (signal 'quit nil))
3244      ;; The group was successfully selected.
3245      (t
3246       (gnus-set-global-variables)
3247       ;; Save the active value in effect when the group was entered.
3248       (setq gnus-newsgroup-active
3249             (gnus-copy-sequence
3250              (gnus-active gnus-newsgroup-name)))
3251       ;; You can change the summary buffer in some way with this hook.
3252       (gnus-run-hooks 'gnus-select-group-hook)
3253       (gnus-update-format-specifications
3254        nil 'summary 'summary-mode 'summary-dummy)
3255       (gnus-update-summary-mark-positions)
3256       ;; Do score processing.
3257       (when gnus-use-scoring
3258         (gnus-possibly-score-headers))
3259       ;; Check whether to fill in the gaps in the threads.
3260       (when gnus-build-sparse-threads
3261         (gnus-build-sparse-threads))
3262       ;; Find the initial limit.
3263       (if gnus-show-threads
3264           (if show-all
3265               (let ((gnus-newsgroup-dormant nil))
3266                 (gnus-summary-initial-limit show-all))
3267             (gnus-summary-initial-limit show-all))
3268         ;; When untreaded, all articles are always shown.
3269         (setq gnus-newsgroup-limit
3270               (mapcar
3271                (lambda (header) (mail-header-number header))
3272                gnus-newsgroup-headers)))
3273       ;; Generate the summary buffer.
3274       (unless no-display
3275         (gnus-summary-prepare))
3276       (when gnus-use-trees
3277         (gnus-tree-open group)
3278         (setq gnus-summary-highlight-line-function
3279               'gnus-tree-highlight-article))
3280       ;; If the summary buffer is empty, but there are some low-scored
3281       ;; articles or some excluded dormants, we include these in the
3282       ;; buffer.
3283       (when (and (zerop (buffer-size))
3284                  (not no-display))
3285         (cond (gnus-newsgroup-dormant
3286                (gnus-summary-limit-include-dormant))
3287               ((and gnus-newsgroup-scored show-all)
3288                (gnus-summary-limit-include-expunged t))))
3289       ;; Function `gnus-apply-kill-file' must be called in this hook.
3290       (gnus-run-hooks 'gnus-apply-kill-hook)
3291       (if (and (zerop (buffer-size))
3292                (not no-display))
3293           (progn
3294             ;; This newsgroup is empty.
3295             (gnus-summary-catchup-and-exit nil t)
3296             (gnus-message 6 "No unread news")
3297             (when kill-buffer
3298               (gnus-kill-or-deaden-summary kill-buffer))
3299             ;; Return nil from this function.
3300             nil)
3301         ;; Hide conversation thread subtrees.  We cannot do this in
3302         ;; gnus-summary-prepare-hook since kill processing may not
3303         ;; work with hidden articles.
3304         (gnus-summary-maybe-hide-threads)
3305         (when kill-buffer
3306           (gnus-kill-or-deaden-summary kill-buffer))
3307         (gnus-summary-auto-select-subject)
3308         ;; Show first unread article if requested.
3309         (if (and (not no-article)
3310                  (not no-display)
3311                  gnus-newsgroup-unreads
3312                  gnus-auto-select-first)
3313             (progn
3314               (gnus-configure-windows 'summary)
3315               (let ((art (gnus-summary-article-number)))
3316                 (unless (or (memq art gnus-newsgroup-undownloaded)
3317                             (memq art gnus-newsgroup-downloadable))
3318                   (gnus-summary-goto-article art))))
3319           ;; Don't select any articles.
3320           (gnus-summary-position-point)
3321           (gnus-configure-windows 'summary 'force)
3322           (gnus-set-mode-line 'summary))
3323         (when (get-buffer-window gnus-group-buffer t)
3324           ;; Gotta use windows, because recenter does weird stuff if
3325           ;; the current buffer ain't the displayed window.
3326           (let ((owin (selected-window)))
3327             (select-window (get-buffer-window gnus-group-buffer t))
3328             (when (gnus-group-goto-group group)
3329               (recenter))
3330             (select-window owin)))
3331         ;; Mark this buffer as "prepared".
3332         (setq gnus-newsgroup-prepared t)
3333         (gnus-run-hooks 'gnus-summary-prepared-hook)
3334         t)))))
3335
3336 (defun gnus-summary-auto-select-subject ()
3337   "Select the subject line on initial group entry."
3338   (goto-char (point-min))
3339   (cond
3340    ((eq gnus-auto-select-subject 'best)
3341     (gnus-summary-best-unread-subject))
3342    ((eq gnus-auto-select-subject 'unread)
3343     (gnus-summary-first-unread-subject))
3344    ((eq gnus-auto-select-subject 'unseen)
3345     (gnus-summary-first-unseen-subject))
3346    ((eq gnus-auto-select-subject 'unseen-or-unread)
3347     (gnus-summary-first-unseen-or-unread-subject))
3348    ((eq gnus-auto-select-subject 'first)
3349     ;; Do nothing.
3350     )
3351    ((gnus-functionp gnus-auto-select-subject)
3352     (funcall gnus-auto-select-subject))))
3353
3354 (defun gnus-summary-prepare ()
3355   "Generate the summary buffer."
3356   (interactive)
3357   (let ((buffer-read-only nil))
3358     (erase-buffer)
3359     (setq gnus-newsgroup-data nil
3360           gnus-newsgroup-data-reverse nil)
3361     (gnus-run-hooks 'gnus-summary-generate-hook)
3362     ;; Generate the buffer, either with threads or without.
3363     (when gnus-newsgroup-headers
3364       (gnus-summary-prepare-threads
3365        (if gnus-show-threads
3366            (gnus-sort-gathered-threads
3367             (funcall gnus-summary-thread-gathering-function
3368                      (gnus-sort-threads
3369                       (gnus-cut-threads (gnus-make-threads)))))
3370          ;; Unthreaded display.
3371          (gnus-sort-articles gnus-newsgroup-headers))))
3372     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3373     ;; Call hooks for modifying summary buffer.
3374     (goto-char (point-min))
3375     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3376
3377 (defsubst gnus-general-simplify-subject (subject)
3378   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3379   (setq subject
3380         (cond
3381          ;; Truncate the subject.
3382          (gnus-simplify-subject-functions
3383           (gnus-map-function gnus-simplify-subject-functions subject))
3384          ((numberp gnus-summary-gather-subject-limit)
3385           (setq subject (gnus-simplify-subject-re subject))
3386           (if (> (length subject) gnus-summary-gather-subject-limit)
3387               (substring subject 0 gnus-summary-gather-subject-limit)
3388             subject))
3389          ;; Fuzzily simplify it.
3390          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3391           (gnus-simplify-subject-fuzzy subject))
3392          ;; Just remove the leading "Re:".
3393          (t
3394           (gnus-simplify-subject-re subject))))
3395
3396   (if (and gnus-summary-gather-exclude-subject
3397            (string-match gnus-summary-gather-exclude-subject subject))
3398       nil                         ; This article shouldn't be gathered
3399     subject))
3400
3401 (defun gnus-summary-simplify-subject-query ()
3402   "Query where the respool algorithm would put this article."
3403   (interactive)
3404   (gnus-summary-select-article)
3405   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3406
3407 (defun gnus-gather-threads-by-subject (threads)
3408   "Gather threads by looking at Subject headers."
3409   (if (not gnus-summary-make-false-root)
3410       threads
3411     (let ((hashtb (gnus-make-hashtable 1024))
3412           (prev threads)
3413           (result threads)
3414           subject hthread whole-subject)
3415       (while threads
3416         (setq subject (gnus-general-simplify-subject
3417                        (setq whole-subject (mail-header-subject
3418                                             (caar threads)))))
3419         (when subject
3420           (if (setq hthread (gnus-gethash subject hashtb))
3421               (progn
3422                 ;; We enter a dummy root into the thread, if we
3423                 ;; haven't done that already.
3424                 (unless (stringp (caar hthread))
3425                   (setcar hthread (list whole-subject (car hthread))))
3426                 ;; We add this new gathered thread to this gathered
3427                 ;; thread.
3428                 (setcdr (car hthread)
3429                         (nconc (cdar hthread) (list (car threads))))
3430                 ;; Remove it from the list of threads.
3431                 (setcdr prev (cdr threads))
3432                 (setq threads prev))
3433             ;; Enter this thread into the hash table.
3434             (gnus-sethash subject threads hashtb)))
3435         (setq prev threads)
3436         (setq threads (cdr threads)))
3437       result)))
3438
3439 (defun gnus-gather-threads-by-references (threads)
3440   "Gather threads by looking at References headers."
3441   (let ((idhashtb (gnus-make-hashtable 1024))
3442         (thhashtb (gnus-make-hashtable 1024))
3443         (prev threads)
3444         (result threads)
3445         ids references id gthread gid entered ref)
3446     (while threads
3447       (when (setq references (mail-header-references (caar threads)))
3448         (setq id (mail-header-id (caar threads))
3449               ids (inline (gnus-split-references references))
3450               entered nil)
3451         (while (setq ref (pop ids))
3452           (setq ids (delete ref ids))
3453           (if (not (setq gid (gnus-gethash ref idhashtb)))
3454               (progn
3455                 (gnus-sethash ref id idhashtb)
3456                 (gnus-sethash id threads thhashtb))
3457             (setq gthread (gnus-gethash gid thhashtb))
3458             (unless entered
3459               ;; We enter a dummy root into the thread, if we
3460               ;; haven't done that already.
3461               (unless (stringp (caar gthread))
3462                 (setcar gthread (list (mail-header-subject (caar gthread))
3463                                       (car gthread))))
3464               ;; We add this new gathered thread to this gathered
3465               ;; thread.
3466               (setcdr (car gthread)
3467                       (nconc (cdar gthread) (list (car threads)))))
3468             ;; Add it into the thread hash table.
3469             (gnus-sethash id gthread thhashtb)
3470             (setq entered t)
3471             ;; Remove it from the list of threads.
3472             (setcdr prev (cdr threads))
3473             (setq threads prev))))
3474       (setq prev threads)
3475       (setq threads (cdr threads)))
3476     result))
3477
3478 (defun gnus-sort-gathered-threads (threads)
3479   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3480   (let ((result threads))
3481     (while threads
3482       (when (stringp (caar threads))
3483         (setcdr (car threads)
3484                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3485       (setq threads (cdr threads)))
3486     result))
3487
3488 (defun gnus-thread-loop-p (root thread)
3489   "Say whether ROOT is in THREAD."
3490   (let ((stack (list thread))
3491         (infloop 0)
3492         th)
3493     (while (setq thread (pop stack))
3494       (setq th (cdr thread))
3495       (while (and th
3496                   (not (eq (caar th) root)))
3497         (pop th))
3498       (if th
3499           ;; We have found a loop.
3500           (let (ref-dep)
3501             (setcdr thread (delq (car th) (cdr thread)))
3502             (if (boundp (setq ref-dep (intern "none"
3503                                               gnus-newsgroup-dependencies)))
3504                 (setcdr (symbol-value ref-dep)
3505                         (nconc (cdr (symbol-value ref-dep))
3506                                (list (car th))))
3507               (set ref-dep (list nil (car th))))
3508             (setq infloop 1
3509                   stack nil))
3510         ;; Push all the subthreads onto the stack.
3511         (push (cdr thread) stack)))
3512     infloop))
3513
3514 (defun gnus-make-threads ()
3515   "Go through the dependency hashtb and find the roots.  Return all threads."
3516   (let (threads)
3517     (while (catch 'infloop
3518              (mapatoms
3519               (lambda (refs)
3520                 ;; Deal with self-referencing References loops.
3521                 (when (and (car (symbol-value refs))
3522                            (not (zerop
3523                                  (apply
3524                                   '+
3525                                   (mapcar
3526                                    (lambda (thread)
3527                                      (gnus-thread-loop-p
3528                                       (car (symbol-value refs)) thread))
3529                                    (cdr (symbol-value refs)))))))
3530                   (setq threads nil)
3531                   (throw 'infloop t))
3532                 (unless (car (symbol-value refs))
3533                   ;; These threads do not refer back to any other
3534                   ;; articles, so they're roots.
3535                   (setq threads (append (cdr (symbol-value refs)) threads))))
3536               gnus-newsgroup-dependencies)))
3537     threads))
3538
3539 ;; Build the thread tree.
3540 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3541   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3542
3543 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3544 if it was already present.
3545
3546 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3547 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3548 Message-IDs will be renamed to a unique Message-ID before being
3549 entered.
3550
3551 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3552   (let* ((id (mail-header-id header))
3553          (id-dep (and id (intern id dependencies)))
3554          ref ref-dep ref-header replaced)
3555     ;; Enter this `header' in the `dependencies' table.
3556     (cond
3557      ((not id-dep)
3558       (setq header nil))
3559      ;; The first two cases do the normal part: enter a new `header'
3560      ;; in the `dependencies' table.
3561      ((not (boundp id-dep))
3562       (set id-dep (list header)))
3563      ((null (car (symbol-value id-dep)))
3564       (setcar (symbol-value id-dep) header))
3565
3566      ;; From here the `header' was already present in the
3567      ;; `dependencies' table.
3568      (force-new
3569       ;; Overrides an existing entry;
3570       ;; just set the header part of the entry.
3571       (setcar (symbol-value id-dep) header)
3572       (setq replaced t))
3573
3574      ;; Renames the existing `header' to a unique Message-ID.
3575      ((not gnus-summary-ignore-duplicates)
3576       ;; An article with this Message-ID has already been seen.
3577       ;; We rename the Message-ID.
3578       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3579            (list header))
3580       (mail-header-set-id header id))
3581
3582      ;; The last case ignores an existing entry, except it adds any
3583      ;; additional Xrefs (in case the two articles came from different
3584      ;; servers.
3585      ;; Also sets `header' to `nil' meaning that the `dependencies'
3586      ;; table was *not* modified.
3587      (t
3588       (mail-header-set-xref
3589        (car (symbol-value id-dep))
3590        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3591                    "")
3592                (or (mail-header-xref header) "")))
3593       (setq header nil)))
3594
3595     (when (and header (not replaced))
3596       ;; First check that we are not creating a References loop.
3597       (setq ref (gnus-parent-id (mail-header-references header)))
3598       (while (and ref
3599                   (setq ref-dep (intern-soft ref dependencies))
3600                   (boundp ref-dep)
3601                   (setq ref-header (car (symbol-value ref-dep))))
3602         (if (string= id ref)
3603             ;; Yuk!  This is a reference loop.  Make the article be a
3604             ;; root article.
3605             (progn
3606               (mail-header-set-references (car (symbol-value id-dep)) "none")
3607               (setq ref nil))
3608           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3609       (setq ref (gnus-parent-id (mail-header-references header)))
3610       (setq ref-dep (intern (or ref "none") dependencies))
3611       (if (boundp ref-dep)
3612           (setcdr (symbol-value ref-dep)
3613                   (nconc (cdr (symbol-value ref-dep))
3614                          (list (symbol-value id-dep))))
3615         (set ref-dep (list nil (symbol-value id-dep)))))
3616     header))
3617
3618 (defun gnus-extract-message-id-from-in-reply-to (string)
3619   (if (string-match "<[^>]+>" string)
3620       (substring string (match-beginning 0) (match-end 0))
3621     nil))
3622
3623 (defun gnus-build-sparse-threads ()
3624   (let ((headers gnus-newsgroup-headers)
3625         (mail-parse-charset gnus-newsgroup-charset)
3626         (gnus-summary-ignore-duplicates t)
3627         header references generation relations
3628         subject child end new-child date)
3629     ;; First we create an alist of generations/relations, where
3630     ;; generations is how much we trust the relation, and the relation
3631     ;; is parent/child.
3632     (gnus-message 7 "Making sparse threads...")
3633     (save-excursion
3634       (nnheader-set-temp-buffer " *gnus sparse threads*")
3635       (while (setq header (pop headers))
3636         (when (and (setq references (mail-header-references header))
3637                    (not (string= references "")))
3638           (insert references)
3639           (setq child (mail-header-id header)
3640                 subject (mail-header-subject header)
3641                 date (mail-header-date header)
3642                 generation 0)
3643           (while (search-backward ">" nil t)
3644             (setq end (1+ (point)))
3645             (when (search-backward "<" nil t)
3646               (setq new-child (buffer-substring (point) end))
3647               (push (list (incf generation)
3648                           child (setq child new-child)
3649                           subject date)
3650                     relations)))
3651           (when child
3652             (push (list (1+ generation) child nil subject) relations))
3653           (erase-buffer)))
3654       (kill-buffer (current-buffer)))
3655     ;; Sort over trustworthiness.
3656     (mapcar
3657      (lambda (relation)
3658        (when (gnus-dependencies-add-header
3659               (make-full-mail-header
3660                gnus-reffed-article-number
3661                (nth 3 relation) "" (or (nth 4 relation) "")
3662                (nth 1 relation)
3663                (or (nth 2 relation) "") 0 0 "")
3664               gnus-newsgroup-dependencies nil)
3665          (push gnus-reffed-article-number gnus-newsgroup-limit)
3666          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3667          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3668                gnus-newsgroup-reads)
3669          (decf gnus-reffed-article-number)))
3670      (sort relations 'car-less-than-car))
3671     (gnus-message 7 "Making sparse threads...done")))
3672
3673 (defun gnus-build-old-threads ()
3674   ;; Look at all the articles that refer back to old articles, and
3675   ;; fetch the headers for the articles that aren't there.  This will
3676   ;; build complete threads - if the roots haven't been expired by the
3677   ;; server, that is.
3678   (let ((mail-parse-charset gnus-newsgroup-charset)
3679         id heads)
3680     (mapatoms
3681      (lambda (refs)
3682        (when (not (car (symbol-value refs)))
3683          (setq heads (cdr (symbol-value refs)))
3684          (while heads
3685            (if (memq (mail-header-number (caar heads))
3686                      gnus-newsgroup-dormant)
3687                (setq heads (cdr heads))
3688              (setq id (symbol-name refs))
3689              (while (and (setq id (gnus-build-get-header id))
3690                          (not (car (gnus-id-to-thread id)))))
3691              (setq heads nil)))))
3692      gnus-newsgroup-dependencies)))
3693
3694 ;; This function has to be called with point after the article number
3695 ;; on the beginning of the line.
3696 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3697   (let ((eol (gnus-point-at-eol))
3698         (buffer (current-buffer))
3699         header references in-reply-to)
3700
3701     ;; overview: [num subject from date id refs chars lines misc]
3702     (unwind-protect
3703         (progn
3704           (narrow-to-region (point) eol)
3705           (unless (eobp)
3706             (forward-char))
3707
3708           (setq header
3709                 (make-full-mail-header
3710                  number                 ; number
3711                  (funcall gnus-decode-encoded-word-function
3712                           (nnheader-nov-field)) ; subject
3713                  (funcall gnus-decode-encoded-word-function
3714                           (nnheader-nov-field)) ; from
3715                  (nnheader-nov-field)   ; date
3716                  (nnheader-nov-read-message-id) ; id
3717                  (setq references (nnheader-nov-field)) ; refs
3718                  (nnheader-nov-read-integer) ; chars
3719                  (nnheader-nov-read-integer) ; lines
3720                  (unless (eobp)
3721                    (if (looking-at "Xref: ")
3722                        (goto-char (match-end 0)))
3723                    (nnheader-nov-field)) ; Xref
3724                  (nnheader-nov-parse-extra)))) ; extra
3725
3726       (widen))
3727
3728     (when (and (string= references "")
3729                (setq in-reply-to (mail-header-extra header))
3730                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3731       (mail-header-set-references
3732        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3733
3734     (when gnus-alter-header-function
3735       (funcall gnus-alter-header-function header))
3736     (gnus-dependencies-add-header header dependencies force-new)))
3737
3738 (defun gnus-build-get-header (id)
3739   "Look through the buffer of NOV lines and find the header to ID.
3740 Enter this line into the dependencies hash table, and return
3741 the id of the parent article (if any)."
3742   (let ((deps gnus-newsgroup-dependencies)
3743         found header)
3744     (prog1
3745         (save-excursion
3746           (set-buffer nntp-server-buffer)
3747           (let ((case-fold-search nil))
3748             (goto-char (point-min))
3749             (while (and (not found)
3750                         (search-forward id nil t))
3751               (beginning-of-line)
3752               (setq found (looking-at
3753                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3754                                    (regexp-quote id))))
3755               (or found (beginning-of-line 2)))
3756             (when found
3757               (beginning-of-line)
3758               (and
3759                (setq header (gnus-nov-parse-line
3760                              (read (current-buffer)) deps))
3761                (gnus-parent-id (mail-header-references header))))))
3762       (when header
3763         (let ((number (mail-header-number header)))
3764           (push number gnus-newsgroup-limit)
3765           (push header gnus-newsgroup-headers)
3766           (if (memq number gnus-newsgroup-unselected)
3767               (progn
3768                 (setq gnus-newsgroup-unreads
3769                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
3770                                                number))
3771                 (setq gnus-newsgroup-unselected
3772                       (delq number gnus-newsgroup-unselected)))
3773             (push number gnus-newsgroup-ancient)))))))
3774
3775 (defun gnus-build-all-threads ()
3776   "Read all the headers."
3777   (let ((gnus-summary-ignore-duplicates t)
3778         (mail-parse-charset gnus-newsgroup-charset)
3779         (dependencies gnus-newsgroup-dependencies)
3780         header article)
3781     (save-excursion
3782       (set-buffer nntp-server-buffer)
3783       (let ((case-fold-search nil))
3784         (goto-char (point-min))
3785         (while (not (eobp))
3786           (ignore-errors
3787             (setq article (read (current-buffer))
3788                   header (gnus-nov-parse-line article dependencies)))
3789           (when header
3790             (save-excursion
3791               (set-buffer gnus-summary-buffer)
3792               (push header gnus-newsgroup-headers)
3793               (if (memq (setq article (mail-header-number header))
3794                         gnus-newsgroup-unselected)
3795                   (progn
3796                     (setq gnus-newsgroup-unreads
3797                           (gnus-add-to-sorted-list
3798                            gnus-newsgroup-unreads article))
3799                     (setq gnus-newsgroup-unselected
3800                           (delq article gnus-newsgroup-unselected)))
3801                 (push article gnus-newsgroup-ancient)))
3802             (forward-line 1)))))))
3803
3804 (defun gnus-summary-update-article-line (article header)
3805   "Update the line for ARTICLE using HEADERS."
3806   (let* ((id (mail-header-id header))
3807          (thread (gnus-id-to-thread id)))
3808     (unless thread
3809       (error "Article in no thread"))
3810     ;; Update the thread.
3811     (setcar thread header)
3812     (gnus-summary-goto-subject article)
3813     (let* ((datal (gnus-data-find-list article))
3814            (data (car datal))
3815            (length (when (cdr datal)
3816                      (- (gnus-data-pos data)
3817                         (gnus-data-pos (cadr datal)))))
3818            (buffer-read-only nil)
3819            (level (gnus-summary-thread-level)))
3820       (gnus-delete-line)
3821       (gnus-summary-insert-line
3822        header level nil (gnus-article-mark article)
3823        (memq article gnus-newsgroup-replied)
3824        (memq article gnus-newsgroup-expirable)
3825        ;; Only insert the Subject string when it's different
3826        ;; from the previous Subject string.
3827        (if (and
3828             gnus-show-threads
3829             (gnus-subject-equal
3830              (condition-case ()
3831                  (mail-header-subject
3832                   (gnus-data-header
3833                    (cadr
3834                     (gnus-data-find-list
3835                      article
3836                      (gnus-data-list t)))))
3837                ;; Error on the side of excessive subjects.
3838                (error ""))
3839              (mail-header-subject header)))
3840            ""
3841          (mail-header-subject header))
3842        nil (cdr (assq article gnus-newsgroup-scored))
3843        (memq article gnus-newsgroup-processable))
3844       (when length
3845         (gnus-data-update-list
3846          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3847
3848 (defun gnus-summary-update-article (article &optional iheader)
3849   "Update ARTICLE in the summary buffer."
3850   (set-buffer gnus-summary-buffer)
3851   (let* ((header (gnus-summary-article-header article))
3852          (id (mail-header-id header))
3853          (data (gnus-data-find article))
3854          (thread (gnus-id-to-thread id))
3855          (references (mail-header-references header))
3856          (parent
3857           (gnus-id-to-thread
3858            (or (gnus-parent-id
3859                 (when (and references
3860                            (not (equal "" references)))
3861                   references))
3862                "none")))
3863          (buffer-read-only nil)
3864          (old (car thread)))
3865     (when thread
3866       (unless iheader
3867         (setcar thread nil)
3868         (when parent
3869           (delq thread parent)))
3870       (if (gnus-summary-insert-subject id header)
3871        ;; Set the (possibly) new article number in the data structure.
3872           (gnus-data-set-number data (gnus-id-to-article id))
3873         (setcar thread old)
3874         nil))))
3875
3876 (defun gnus-rebuild-thread (id &optional line)
3877   "Rebuild the thread containing ID.
3878 If LINE, insert the rebuilt thread starting on line LINE."
3879   (let ((buffer-read-only nil)
3880         old-pos current thread data)
3881     (if (not gnus-show-threads)
3882         (setq thread (list (car (gnus-id-to-thread id))))
3883       ;; Get the thread this article is part of.
3884       (setq thread (gnus-remove-thread id)))
3885     (setq old-pos (gnus-point-at-bol))
3886     (setq current (save-excursion
3887                     (and (re-search-backward "[\r\n]" nil t)
3888                          (gnus-summary-article-number))))
3889     ;; If this is a gathered thread, we have to go some re-gathering.
3890     (when (stringp (car thread))
3891       (let ((subject (car thread))
3892             roots thr)
3893         (setq thread (cdr thread))
3894         (while thread
3895           (unless (memq (setq thr (gnus-id-to-thread
3896                                    (gnus-root-id
3897                                     (mail-header-id (caar thread)))))
3898                         roots)
3899             (push thr roots))
3900           (setq thread (cdr thread)))
3901         ;; We now have all (unique) roots.
3902         (if (= (length roots) 1)
3903             ;; All the loose roots are now one solid root.
3904             (setq thread (car roots))
3905           (setq thread (cons subject (gnus-sort-threads roots))))))
3906     (let (threads)
3907       ;; We then insert this thread into the summary buffer.
3908       (when line
3909         (goto-char (point-min))
3910         (forward-line (1- line)))
3911       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3912         (if gnus-show-threads
3913             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3914           (gnus-summary-prepare-unthreaded thread))
3915         (setq data (nreverse gnus-newsgroup-data))
3916         (setq threads gnus-newsgroup-threads))
3917       ;; We splice the new data into the data structure.
3918       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3919       ;;!!! then we want to insert at the beginning of the buffer.
3920       ;;!!! That happens to be true with Gnus now, but that may
3921       ;;!!! change in the future.  Perhaps.
3922       (gnus-data-enter-list
3923        (if line nil current) data (- (point) old-pos))
3924       (setq gnus-newsgroup-threads
3925             (nconc threads gnus-newsgroup-threads))
3926       (gnus-data-compute-positions))))
3927
3928 (defun gnus-number-to-header (number)
3929   "Return the header for article NUMBER."
3930   (let ((headers gnus-newsgroup-headers))
3931     (while (and headers
3932                 (not (= number (mail-header-number (car headers)))))
3933       (pop headers))
3934     (when headers
3935       (car headers))))
3936
3937 (defun gnus-parent-headers (in-headers &optional generation)
3938   "Return the headers of the GENERATIONeth parent of HEADERS."
3939   (unless generation
3940     (setq generation 1))
3941   (let ((parent t)
3942         (headers in-headers)
3943         references)
3944     (while (and parent
3945                 (not (zerop generation))
3946                 (setq references (mail-header-references headers)))
3947       (setq headers (if (and references
3948                              (setq parent (gnus-parent-id references)))
3949                         (car (gnus-id-to-thread parent))
3950                       nil))
3951       (decf generation))
3952     (and (not (eq headers in-headers))
3953          headers)))
3954
3955 (defun gnus-id-to-thread (id)
3956   "Return the (sub-)thread where ID appears."
3957   (gnus-gethash id gnus-newsgroup-dependencies))
3958
3959 (defun gnus-id-to-article (id)
3960   "Return the article number of ID."
3961   (let ((thread (gnus-id-to-thread id)))
3962     (when (and thread
3963                (car thread))
3964       (mail-header-number (car thread)))))
3965
3966 (defun gnus-id-to-header (id)
3967   "Return the article headers of ID."
3968   (car (gnus-id-to-thread id)))
3969
3970 (defun gnus-article-displayed-root-p (article)
3971   "Say whether ARTICLE is a root(ish) article."
3972   (let ((level (gnus-summary-thread-level article))
3973         (refs (mail-header-references  (gnus-summary-article-header article)))
3974         particle)
3975     (cond
3976      ((null level) nil)
3977      ((zerop level) t)
3978      ((null refs) t)
3979      ((null (gnus-parent-id refs)) t)
3980      ((and (= 1 level)
3981            (null (setq particle (gnus-id-to-article
3982                                  (gnus-parent-id refs))))
3983            (null (gnus-summary-thread-level particle)))))))
3984
3985 (defun gnus-root-id (id)
3986   "Return the id of the root of the thread where ID appears."
3987   (let (last-id prev)
3988     (while (and id (setq prev (car (gnus-id-to-thread id))))
3989       (setq last-id id
3990             id (gnus-parent-id (mail-header-references prev))))
3991     last-id))
3992
3993 (defun gnus-articles-in-thread (thread)
3994   "Return the list of articles in THREAD."
3995   (cons (mail-header-number (car thread))
3996         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3997
3998 (defun gnus-remove-thread (id &optional dont-remove)
3999   "Remove the thread that has ID in it."
4000   (let (headers thread last-id)
4001     ;; First go up in this thread until we find the root.
4002     (setq last-id (gnus-root-id id)
4003           headers (message-flatten-list (gnus-id-to-thread last-id)))
4004     ;; We have now found the real root of this thread.  It might have
4005     ;; been gathered into some loose thread, so we have to search
4006     ;; through the threads to find the thread we wanted.
4007     (let ((threads gnus-newsgroup-threads)
4008           sub)
4009       (while threads
4010         (setq sub (car threads))
4011         (if (stringp (car sub))
4012             ;; This is a gathered thread, so we look at the roots
4013             ;; below it to find whether this article is in this
4014             ;; gathered root.
4015             (progn
4016               (setq sub (cdr sub))
4017               (while sub
4018                 (when (member (caar sub) headers)
4019                   (setq thread (car threads)
4020                         threads nil
4021                         sub nil))
4022                 (setq sub (cdr sub))))
4023           ;; It's an ordinary thread, so we check it.
4024           (when (eq (car sub) (car headers))
4025             (setq thread sub
4026                   threads nil)))
4027         (setq threads (cdr threads)))
4028       ;; If this article is in no thread, then it's a root.
4029       (if thread
4030           (unless dont-remove
4031             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4032         (setq thread (gnus-id-to-thread last-id)))
4033       (when thread
4034         (prog1
4035             thread                      ; We return this thread.
4036           (unless dont-remove
4037             (if (stringp (car thread))
4038                 (progn
4039                   ;; If we use dummy roots, then we have to remove the
4040                   ;; dummy root as well.
4041                   (when (eq gnus-summary-make-false-root 'dummy)
4042                     ;; We go to the dummy root by going to
4043                     ;; the first sub-"thread", and then one line up.
4044                     (gnus-summary-goto-article
4045                      (mail-header-number (caadr thread)))
4046                     (forward-line -1)
4047                     (gnus-delete-line)
4048                     (gnus-data-compute-positions))
4049                   (setq thread (cdr thread))
4050                   (while thread
4051                     (gnus-remove-thread-1 (car thread))
4052                     (setq thread (cdr thread))))
4053               (gnus-remove-thread-1 thread))))))))
4054
4055 (defun gnus-remove-thread-1 (thread)
4056   "Remove the thread THREAD recursively."
4057   (let ((number (mail-header-number (pop thread)))
4058         d)
4059     (setq thread (reverse thread))
4060     (while thread
4061       (gnus-remove-thread-1 (pop thread)))
4062     (when (setq d (gnus-data-find number))
4063       (goto-char (gnus-data-pos d))
4064       (gnus-summary-show-thread)
4065       (gnus-data-remove
4066        number
4067        (- (gnus-point-at-bol)
4068           (prog1
4069               (1+ (gnus-point-at-eol))
4070             (gnus-delete-line)))))))
4071
4072 (defun gnus-sort-threads-1 (threads func)
4073   (sort (mapcar (lambda (thread)
4074                   (cons (car thread)
4075                         (and (cdr thread)
4076                              (gnus-sort-threads-1 (cdr thread) func))))
4077                 threads) func))
4078
4079 (defun gnus-sort-threads (threads)
4080   "Sort THREADS."
4081   (if (not gnus-thread-sort-functions)
4082       threads
4083     (gnus-message 8 "Sorting threads...")
4084     (prog1
4085         (gnus-sort-threads-1
4086          threads
4087          (gnus-make-sort-function gnus-thread-sort-functions))
4088       (gnus-message 8 "Sorting threads...done"))))
4089
4090 (defun gnus-sort-articles (articles)
4091   "Sort ARTICLES."
4092   (when gnus-article-sort-functions
4093     (gnus-message 7 "Sorting articles...")
4094     (prog1
4095         (setq gnus-newsgroup-headers
4096               (sort articles (gnus-make-sort-function
4097                               gnus-article-sort-functions)))
4098       (gnus-message 7 "Sorting articles...done"))))
4099
4100 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4101 (defmacro gnus-thread-header (thread)
4102   "Return header of first article in THREAD.
4103 Note that THREAD must never, ever be anything else than a variable -
4104 using some other form will lead to serious barfage."
4105   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4106   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4107   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4108         (vector thread) 2))
4109
4110 (defsubst gnus-article-sort-by-number (h1 h2)
4111   "Sort articles by article number."
4112   (< (mail-header-number h1)
4113      (mail-header-number h2)))
4114
4115 (defun gnus-thread-sort-by-number (h1 h2)
4116   "Sort threads by root article number."
4117   (gnus-article-sort-by-number
4118    (gnus-thread-header h1) (gnus-thread-header h2)))
4119
4120 (defsubst gnus-article-sort-by-lines (h1 h2)
4121   "Sort articles by article Lines header."
4122   (< (mail-header-lines h1)
4123      (mail-header-lines h2)))
4124
4125 (defun gnus-thread-sort-by-lines (h1 h2)
4126   "Sort threads by root article Lines header."
4127   (gnus-article-sort-by-lines
4128    (gnus-thread-header h1) (gnus-thread-header h2)))
4129
4130 (defsubst gnus-article-sort-by-chars (h1 h2)
4131   "Sort articles by octet length."
4132   (< (mail-header-chars h1)
4133      (mail-header-chars h2)))
4134
4135 (defun gnus-thread-sort-by-chars (h1 h2)
4136   "Sort threads by root article octet length."
4137   (gnus-article-sort-by-chars
4138    (gnus-thread-header h1) (gnus-thread-header h2)))
4139
4140 (defsubst gnus-article-sort-by-author (h1 h2)
4141   "Sort articles by root author."
4142   (string-lessp
4143    (let ((extract (funcall
4144                    gnus-extract-address-components
4145                    (mail-header-from h1))))
4146      (or (car extract) (cadr extract) ""))
4147    (let ((extract (funcall
4148                    gnus-extract-address-components
4149                    (mail-header-from h2))))
4150      (or (car extract) (cadr extract) ""))))
4151
4152 (defun gnus-thread-sort-by-author (h1 h2)
4153   "Sort threads by root author."
4154   (gnus-article-sort-by-author
4155    (gnus-thread-header h1)  (gnus-thread-header h2)))
4156
4157 (defsubst gnus-article-sort-by-subject (h1 h2)
4158   "Sort articles by root subject."
4159   (string-lessp
4160    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4161    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4162
4163 (defun gnus-thread-sort-by-subject (h1 h2)
4164   "Sort threads by root subject."
4165   (gnus-article-sort-by-subject
4166    (gnus-thread-header h1) (gnus-thread-header h2)))
4167
4168 (defsubst gnus-article-sort-by-date (h1 h2)
4169   "Sort articles by root article date."
4170   (time-less-p
4171    (gnus-date-get-time (mail-header-date h1))
4172    (gnus-date-get-time (mail-header-date h2))))
4173
4174 (defun gnus-thread-sort-by-date (h1 h2)
4175   "Sort threads by root article date."
4176   (gnus-article-sort-by-date
4177    (gnus-thread-header h1) (gnus-thread-header h2)))
4178
4179 (defsubst gnus-article-sort-by-score (h1 h2)
4180   "Sort articles by root article score.
4181 Unscored articles will be counted as having a score of zero."
4182   (> (or (cdr (assq (mail-header-number h1)
4183                     gnus-newsgroup-scored))
4184          gnus-summary-default-score 0)
4185      (or (cdr (assq (mail-header-number h2)
4186                     gnus-newsgroup-scored))
4187          gnus-summary-default-score 0)))
4188
4189 (defun gnus-thread-sort-by-score (h1 h2)
4190   "Sort threads by root article score."
4191   (gnus-article-sort-by-score
4192    (gnus-thread-header h1) (gnus-thread-header h2)))
4193
4194 (defun gnus-thread-sort-by-total-score (h1 h2)
4195   "Sort threads by the sum of all scores in the thread.
4196 Unscored articles will be counted as having a score of zero."
4197   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4198
4199 (defun gnus-thread-total-score (thread)
4200   ;; This function find the total score of THREAD.
4201   (cond
4202    ((null thread)
4203     0)
4204    ((consp thread)
4205     (if (stringp (car thread))
4206         (apply gnus-thread-score-function 0
4207                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4208       (gnus-thread-total-score-1 thread)))
4209    (t
4210     (gnus-thread-total-score-1 (list thread)))))
4211
4212 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4213   "Sort threads such that the thread with the most recently arrived article comes first."
4214   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4215
4216 (defun gnus-thread-highest-number (thread)
4217   "Return the highest article number in THREAD."
4218   (apply 'max (mapcar (lambda (header)
4219                         (mail-header-number header))
4220                       (message-flatten-list thread))))
4221
4222 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4223   "Sort threads such that the thread with the most recently dated article comes first."
4224   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4225
4226 (defun gnus-thread-latest-date (thread)
4227   "Return the highest article date in THREAD."
4228   (let ((previous-time 0))
4229     (apply 'max (mapcar
4230                  (lambda (header)
4231                    (setq previous-time
4232                          (time-to-seconds
4233                           (mail-header-parse-date
4234                            (condition-case ()
4235                                (mail-header-date header)
4236                              (error previous-time))))))
4237                  (sort
4238                   (message-flatten-list thread)
4239                   (lambda (h1 h2)
4240                     (< (mail-header-number h1)
4241                        (mail-header-number h2))))))))
4242
4243 (defun gnus-thread-total-score-1 (root)
4244   ;; This function find the total score of the thread below ROOT.
4245   (setq root (car root))
4246   (apply gnus-thread-score-function
4247          (or (append
4248               (mapcar 'gnus-thread-total-score
4249                       (cdr (gnus-id-to-thread (mail-header-id root))))
4250               (when (> (mail-header-number root) 0)
4251                 (list (or (cdr (assq (mail-header-number root)
4252                                      gnus-newsgroup-scored))
4253                           gnus-summary-default-score 0))))
4254              (list gnus-summary-default-score)
4255              '(0))))
4256
4257 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4258 (defvar gnus-tmp-prev-subject nil)
4259 (defvar gnus-tmp-false-parent nil)
4260 (defvar gnus-tmp-root-expunged nil)
4261 (defvar gnus-tmp-dummy-line nil)
4262
4263 (eval-when-compile (defvar gnus-tmp-header))
4264 (defun gnus-extra-header (type &optional header)
4265   "Return the extra header of TYPE."
4266   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4267       ""))
4268
4269 (defvar gnus-tmp-thread-tree-header-string "")
4270
4271 (defvar gnus-sum-thread-tree-root "> "
4272   "With %B spec, used for the root of a thread.
4273 If nil, use subject instead.")
4274 (defvar gnus-sum-thread-tree-single-indent ""
4275   "With %B spec, used for a thread with just one message.
4276 If nil, use subject instead.")
4277 (defvar gnus-sum-thread-tree-vertical "| "
4278   "With %B spec, used for drawing a vertical line.")
4279 (defvar gnus-sum-thread-tree-indent "  "
4280   "With %B spec, used for indenting.")
4281 (defvar gnus-sum-thread-tree-leaf-with-other "+-> "
4282   "With %B spec, used for a leaf with brothers.")
4283 (defvar gnus-sum-thread-tree-single-leaf "\\-> "
4284   "With %B spec, used for a leaf without brothers.")
4285
4286 (defun gnus-summary-prepare-threads (threads)
4287   "Prepare summary buffer from THREADS and indentation LEVEL.
4288 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4289 or a straight list of headers."
4290   (gnus-message 7 "Generating summary...")
4291
4292   (setq gnus-newsgroup-threads threads)
4293   (beginning-of-line)
4294
4295   (let ((gnus-tmp-level 0)
4296         (default-score (or gnus-summary-default-score 0))
4297         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4298         thread number subject stack state gnus-tmp-gathered beg-match
4299         new-roots gnus-tmp-new-adopts thread-end
4300         gnus-tmp-header gnus-tmp-unread
4301         gnus-tmp-replied gnus-tmp-subject-or-nil
4302         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4303         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4304         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4305         tree-stack)
4306
4307     (setq gnus-tmp-prev-subject nil)
4308
4309     (if (vectorp (car threads))
4310         ;; If this is a straight (sic) list of headers, then a
4311         ;; threaded summary display isn't required, so we just create
4312         ;; an unthreaded one.
4313         (gnus-summary-prepare-unthreaded threads)
4314
4315       ;; Do the threaded display.
4316
4317       (while (or threads stack gnus-tmp-new-adopts new-roots)
4318
4319         (if (and (= gnus-tmp-level 0)
4320                  (or (not stack)
4321                      (= (caar stack) 0))
4322                  (not gnus-tmp-false-parent)
4323                  (or gnus-tmp-new-adopts new-roots))
4324             (if gnus-tmp-new-adopts
4325                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4326                       thread (list (car gnus-tmp-new-adopts))
4327                       gnus-tmp-header (caar thread)
4328                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4329               (when new-roots
4330                 (setq thread (list (car new-roots))
4331                       gnus-tmp-header (caar thread)
4332                       new-roots (cdr new-roots))))
4333
4334           (if threads
4335               ;; If there are some threads, we do them before the
4336               ;; threads on the stack.
4337               (setq thread threads
4338                     gnus-tmp-header (caar thread))
4339             ;; There were no current threads, so we pop something off
4340             ;; the stack.
4341             (setq state (car stack)
4342                   gnus-tmp-level (car state)
4343                   tree-stack (cadr state)
4344                   thread (caddr state)
4345                   stack (cdr stack)
4346                   gnus-tmp-header (caar thread))))
4347
4348         (setq gnus-tmp-false-parent nil)
4349         (setq gnus-tmp-root-expunged nil)
4350         (setq thread-end nil)
4351
4352         (if (stringp gnus-tmp-header)
4353             ;; The header is a dummy root.
4354             (cond
4355              ((eq gnus-summary-make-false-root 'adopt)
4356               ;; We let the first article adopt the rest.
4357               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4358                                                (cddar thread)))
4359               (setq gnus-tmp-gathered
4360                     (nconc (mapcar
4361                             (lambda (h) (mail-header-number (car h)))
4362                             (cddar thread))
4363                            gnus-tmp-gathered))
4364               (setq thread (cons (list (caar thread)
4365                                        (cadar thread))
4366                                  (cdr thread)))
4367               (setq gnus-tmp-level -1
4368                     gnus-tmp-false-parent t))
4369              ((eq gnus-summary-make-false-root 'empty)
4370               ;; We print adopted articles with empty subject fields.
4371               (setq gnus-tmp-gathered
4372                     (nconc (mapcar
4373                             (lambda (h) (mail-header-number (car h)))
4374                             (cddar thread))
4375                            gnus-tmp-gathered))
4376               (setq gnus-tmp-level -1))
4377              ((eq gnus-summary-make-false-root 'dummy)
4378               ;; We remember that we probably want to output a dummy
4379               ;; root.
4380               (setq gnus-tmp-dummy-line gnus-tmp-header)
4381               (setq gnus-tmp-prev-subject gnus-tmp-header))
4382              (t
4383               ;; We do not make a root for the gathered
4384               ;; sub-threads at all.
4385               (setq gnus-tmp-level -1)))
4386
4387           (setq number (mail-header-number gnus-tmp-header)
4388                 subject (mail-header-subject gnus-tmp-header))
4389
4390           (cond
4391            ;; If the thread has changed subject, we might want to make
4392            ;; this subthread into a root.
4393            ((and (null gnus-thread-ignore-subject)
4394                  (not (zerop gnus-tmp-level))
4395                  gnus-tmp-prev-subject
4396                  (not (inline
4397                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4398             (setq new-roots (nconc new-roots (list (car thread)))
4399                   thread-end t
4400                   gnus-tmp-header nil))
4401            ;; If the article lies outside the current limit,
4402            ;; then we do not display it.
4403            ((not (memq number gnus-newsgroup-limit))
4404             (setq gnus-tmp-gathered
4405                   (nconc (mapcar
4406                           (lambda (h) (mail-header-number (car h)))
4407                           (cdar thread))
4408                          gnus-tmp-gathered))
4409             (setq gnus-tmp-new-adopts (if (cdar thread)
4410                                           (append gnus-tmp-new-adopts
4411                                                   (cdar thread))
4412                                         gnus-tmp-new-adopts)
4413                   thread-end t
4414                   gnus-tmp-header nil)
4415             (when (zerop gnus-tmp-level)
4416               (setq gnus-tmp-root-expunged t)))
4417            ;; Perhaps this article is to be marked as read?
4418            ((and gnus-summary-mark-below
4419                  (< (or (cdr (assq number gnus-newsgroup-scored))
4420                         default-score)
4421                     gnus-summary-mark-below)
4422                  ;; Don't touch sparse articles.
4423                  (not (gnus-summary-article-sparse-p number))
4424                  (not (gnus-summary-article-ancient-p number)))
4425             (setq gnus-newsgroup-unreads
4426                   (delq number gnus-newsgroup-unreads))
4427             (if gnus-newsgroup-auto-expire
4428                 (setq gnus-newsgroup-expirable
4429                       (gnus-add-to-sorted-list
4430                        gnus-newsgroup-expirable number))
4431               (push (cons number gnus-low-score-mark)
4432                     gnus-newsgroup-reads))))
4433
4434           (when gnus-tmp-header
4435             ;; We may have an old dummy line to output before this
4436             ;; article.
4437             (when (and gnus-tmp-dummy-line
4438                        (gnus-subject-equal
4439                         gnus-tmp-dummy-line
4440                         (mail-header-subject gnus-tmp-header)))
4441               (gnus-summary-insert-dummy-line
4442                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4443               (setq gnus-tmp-dummy-line nil))
4444
4445             ;; Compute the mark.
4446             (setq gnus-tmp-unread (gnus-article-mark number))
4447
4448             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4449                                   gnus-tmp-header gnus-tmp-level)
4450                   gnus-newsgroup-data)
4451
4452             ;; Actually insert the line.
4453             (setq
4454              gnus-tmp-subject-or-nil
4455              (cond
4456               ((and gnus-thread-ignore-subject
4457                     gnus-tmp-prev-subject
4458                     (not (inline (gnus-subject-equal
4459                                   gnus-tmp-prev-subject subject))))
4460                subject)
4461               ((zerop gnus-tmp-level)
4462                (if (and (eq gnus-summary-make-false-root 'empty)
4463                         (memq number gnus-tmp-gathered)
4464                         gnus-tmp-prev-subject
4465                         (inline (gnus-subject-equal
4466                                  gnus-tmp-prev-subject subject)))
4467                    gnus-summary-same-subject
4468                  subject))
4469               (t gnus-summary-same-subject)))
4470             (if (and (eq gnus-summary-make-false-root 'adopt)
4471                      (= gnus-tmp-level 1)
4472                      (memq number gnus-tmp-gathered))
4473                 (setq gnus-tmp-opening-bracket ?\<
4474                       gnus-tmp-closing-bracket ?\>)
4475               (setq gnus-tmp-opening-bracket ?\[
4476                     gnus-tmp-closing-bracket ?\]))
4477             (setq
4478              gnus-tmp-indentation
4479              (aref gnus-thread-indent-array gnus-tmp-level)
4480              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4481              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4482                                 gnus-summary-default-score 0)
4483              gnus-tmp-score-char
4484              (if (or (null gnus-summary-default-score)
4485                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4486                          gnus-summary-zcore-fuzz))
4487                  ?                      ;Whitespace
4488                (if (< gnus-tmp-score gnus-summary-default-score)
4489                    gnus-score-below-mark gnus-score-over-mark))
4490              gnus-tmp-replied
4491              (cond ((memq number gnus-newsgroup-processable)
4492                     gnus-process-mark)
4493                    ((memq number gnus-newsgroup-cached)
4494                     gnus-cached-mark)
4495                    ((memq number gnus-newsgroup-replied)
4496                     gnus-replied-mark)
4497                    ((memq number gnus-newsgroup-forwarded)
4498                     gnus-forwarded-mark)
4499                    ((memq number gnus-newsgroup-saved)
4500                     gnus-saved-mark)
4501                    ((memq number gnus-newsgroup-recent)
4502                     gnus-recent-mark)
4503                    ((memq number gnus-newsgroup-unseen)
4504                     gnus-unseen-mark)
4505                    (t gnus-no-mark))
4506              gnus-tmp-from (mail-header-from gnus-tmp-header)
4507              gnus-tmp-name
4508              (cond
4509               ((string-match "<[^>]+> *$" gnus-tmp-from)
4510                (setq beg-match (match-beginning 0))
4511                (or (and (string-match "^\".+\"" gnus-tmp-from)
4512                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4513                    (substring gnus-tmp-from 0 beg-match)))
4514               ((string-match "(.+)" gnus-tmp-from)
4515                (substring gnus-tmp-from
4516                           (1+ (match-beginning 0)) (1- (match-end 0))))
4517               (t gnus-tmp-from))
4518              gnus-tmp-thread-tree-header-string
4519              (cond
4520               ((not gnus-show-threads) "")
4521               ((zerop gnus-tmp-level)
4522                (if (cdar thread)
4523                    (or gnus-sum-thread-tree-root subject)
4524                  (or gnus-sum-thread-tree-single-indent subject)))
4525               (t
4526                (concat (apply 'concat
4527                               (mapcar (lambda (item)
4528                                         (if (= item 1)
4529                                             gnus-sum-thread-tree-vertical
4530                                           gnus-sum-thread-tree-indent))
4531                                       (cdr (reverse tree-stack))))
4532                        (if (nth 1 thread)
4533                            gnus-sum-thread-tree-leaf-with-other
4534                          gnus-sum-thread-tree-single-leaf)))))
4535             (when (string= gnus-tmp-name "")
4536               (setq gnus-tmp-name gnus-tmp-from))
4537             (unless (numberp gnus-tmp-lines)
4538               (setq gnus-tmp-lines -1))
4539             (if (= gnus-tmp-lines -1)
4540                 (setq gnus-tmp-lines "?")
4541               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4542             (gnus-put-text-property
4543              (point)
4544              (progn (eval gnus-summary-line-format-spec) (point))
4545              'gnus-number number)
4546             (when gnus-visual-p
4547               (forward-line -1)
4548               (gnus-run-hooks 'gnus-summary-update-hook)
4549               (forward-line 1))
4550
4551             (setq gnus-tmp-prev-subject subject)))
4552
4553         (when (nth 1 thread)
4554           (push (list (max 0 gnus-tmp-level)
4555                       (copy-list tree-stack)
4556                       (nthcdr 1 thread))
4557                 stack))
4558         (push (if (nth 1 thread) 1 0) tree-stack)
4559         (incf gnus-tmp-level)
4560         (setq threads (if thread-end nil (cdar thread)))
4561         (unless threads
4562           (setq gnus-tmp-level 0)))))
4563   (gnus-message 7 "Generating summary...done"))
4564
4565 (defun gnus-summary-prepare-unthreaded (headers)
4566   "Generate an unthreaded summary buffer based on HEADERS."
4567   (let (header number mark)
4568
4569     (beginning-of-line)
4570
4571     (while headers
4572       ;; We may have to root out some bad articles...
4573       (when (memq (setq number (mail-header-number
4574                                 (setq header (pop headers))))
4575                   gnus-newsgroup-limit)
4576         ;; Mark article as read when it has a low score.
4577         (when (and gnus-summary-mark-below
4578                    (< (or (cdr (assq number gnus-newsgroup-scored))
4579                           gnus-summary-default-score 0)
4580                       gnus-summary-mark-below)
4581                    (not (gnus-summary-article-ancient-p number)))
4582           (setq gnus-newsgroup-unreads
4583                 (delq number gnus-newsgroup-unreads))
4584           (if gnus-newsgroup-auto-expire
4585               (push number gnus-newsgroup-expirable)
4586             (push (cons number gnus-low-score-mark)
4587                   gnus-newsgroup-reads)))
4588
4589         (setq mark (gnus-article-mark number))
4590         (push (gnus-data-make number mark (1+ (point)) header 0)
4591               gnus-newsgroup-data)
4592         (gnus-summary-insert-line
4593          header 0 number
4594          mark (memq number gnus-newsgroup-replied)
4595          (memq number gnus-newsgroup-expirable)
4596          (mail-header-subject header) nil
4597          (cdr (assq number gnus-newsgroup-scored))
4598          (memq number gnus-newsgroup-processable))))))
4599
4600 (defun gnus-summary-remove-list-identifiers ()
4601   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4602   (let ((regexp (if (consp gnus-list-identifiers)
4603                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4604                   gnus-list-identifiers))
4605         changed subject)
4606     (when regexp
4607       (dolist (header gnus-newsgroup-headers)
4608         (setq subject (mail-header-subject header)
4609               changed nil)
4610         (while (string-match
4611                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4612                 subject)
4613           (setq subject
4614                 (concat (substring subject 0 (match-beginning 2))
4615                         (substring subject (match-end 0)))
4616                 changed t))
4617         (when (and changed
4618                    (string-match
4619                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4620           (setq subject
4621                 (concat (substring subject 0 (match-beginning 1))
4622                         (substring subject (match-end 1)))))
4623         (when changed
4624           (mail-header-set-subject header subject))))))
4625
4626 (defun gnus-fetch-headers (articles)
4627   "Fetch headers of ARTICLES."
4628   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4629     (gnus-message 5 "Fetching headers for %s..." name)
4630     (prog1
4631         (if (eq 'nov
4632                 (setq gnus-headers-retrieved-by
4633                       (gnus-retrieve-headers
4634                        articles gnus-newsgroup-name
4635                        ;; We might want to fetch old headers, but
4636                        ;; not if there is only 1 article.
4637                        (and (or (and
4638                                  (not (eq gnus-fetch-old-headers 'some))
4639                                  (not (numberp gnus-fetch-old-headers)))
4640                                 (> (length articles) 1))
4641                             gnus-fetch-old-headers))))
4642             (gnus-get-newsgroup-headers-xover
4643              articles nil nil gnus-newsgroup-name t)
4644           (gnus-get-newsgroup-headers))
4645       (gnus-message 5 "Fetching headers for %s...done" name))))
4646
4647 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4648   "Select newsgroup GROUP.
4649 If READ-ALL is non-nil, all articles in the group are selected.
4650 If SELECT-ARTICLES, only select those articles from GROUP."
4651   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4652          ;;!!! Dirty hack; should be removed.
4653          (gnus-summary-ignore-duplicates
4654           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4655               t
4656             gnus-summary-ignore-duplicates))
4657          (info (nth 2 entry))
4658          articles fetched-articles cached)
4659
4660     (unless (gnus-check-server
4661              (set (make-local-variable 'gnus-current-select-method)
4662                   (gnus-find-method-for-group group)))
4663       (error "Couldn't open server"))
4664
4665     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4666         (gnus-activate-group group)     ; Or we can activate it...
4667         (progn                          ; Or we bug out.
4668           (when (equal major-mode 'gnus-summary-mode)
4669             (kill-buffer (current-buffer)))
4670           (error "Couldn't activate group %s: %s"
4671                  group (gnus-status-message group))))
4672
4673     (unless (gnus-request-group group t)
4674       (when (equal major-mode 'gnus-summary-mode)
4675         (kill-buffer (current-buffer)))
4676       (error "Couldn't request group %s: %s"
4677              group (gnus-status-message group)))
4678
4679     (setq gnus-newsgroup-name group
4680           gnus-newsgroup-unselected nil
4681           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4682
4683     (let ((display (gnus-group-find-parameter group 'display)))
4684       (setq gnus-newsgroup-display
4685             (cond
4686              ((not (zerop (or (car-safe read-all) 0)))
4687               ;; The user entered the group with C-u SPC/RET, let's show
4688               ;; all articles.
4689               'gnus-not-ignore)
4690              ((eq display 'all)
4691               'gnus-not-ignore)
4692              ((arrayp display)
4693               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4694              ((numberp display)
4695               ;; The following is probably the "correct" solution, but
4696               ;; it makes Gnus fetch all headers and then limit the
4697               ;; articles (which is slow), so instead we hack the
4698               ;; select-articles parameter instead. -- Simon Josefsson
4699               ;; <jas@kth.se>
4700               ;;
4701               ;; (gnus-byte-compile
4702               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4703               ;;                         display)))))
4704               (setq select-articles
4705                     (gnus-uncompress-range
4706                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4707                              (if (> tmp 0)
4708                                  tmp
4709                                1))
4710                            (cdr (gnus-active group)))))
4711               nil)
4712              (t
4713               nil))))
4714
4715     (gnus-summary-setup-default-charset)
4716
4717     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4718     (when (gnus-virtual-group-p group)
4719       (setq cached gnus-newsgroup-cached))
4720
4721     (setq gnus-newsgroup-unreads
4722           (gnus-sorted-ndifference
4723            (gnus-sorted-ndifference gnus-newsgroup-unreads
4724                                     gnus-newsgroup-marked)
4725            gnus-newsgroup-dormant))
4726
4727     (setq gnus-newsgroup-processable nil)
4728
4729     (gnus-update-read-articles group gnus-newsgroup-unreads)
4730
4731     ;; Adjust and set lists of article marks.
4732     (when info
4733       (gnus-adjust-marked-articles info))
4734
4735     (if (setq articles select-articles)
4736         (setq gnus-newsgroup-unselected
4737               (gnus-sorted-difference gnus-newsgroup-unreads articles))
4738       (setq articles (gnus-articles-to-read group read-all)))
4739
4740     (cond
4741      ((null articles)
4742       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4743       'quit)
4744      ((eq articles 0) nil)
4745      (t
4746       ;; Init the dependencies hash table.
4747       (setq gnus-newsgroup-dependencies
4748             (gnus-make-hashtable (length articles)))
4749       (gnus-set-global-variables)
4750       ;; Retrieve the headers and read them in.
4751       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4752
4753       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4754       (when cached
4755         (setq gnus-newsgroup-cached cached))
4756
4757       ;; Suppress duplicates?
4758       (when gnus-suppress-duplicates
4759         (gnus-dup-suppress-articles))
4760
4761       ;; Set the initial limit.
4762       (setq gnus-newsgroup-limit (copy-sequence articles))
4763       ;; Remove canceled articles from the list of unread articles.
4764       (setq fetched-articles
4765             (mapcar (lambda (headers) (mail-header-number headers))
4766                     gnus-newsgroup-headers))
4767       (setq gnus-newsgroup-articles fetched-articles)
4768       (setq gnus-newsgroup-unreads
4769             (gnus-sorted-nintersection
4770              gnus-newsgroup-unreads fetched-articles))
4771       (gnus-compute-unseen-list)
4772
4773       ;; Removed marked articles that do not exist.
4774       (gnus-update-missing-marks
4775        (gnus-sorted-difference articles fetched-articles))
4776       ;; We might want to build some more threads first.
4777       (when (and gnus-fetch-old-headers
4778                  (eq gnus-headers-retrieved-by 'nov))
4779         (if (eq gnus-fetch-old-headers 'invisible)
4780             (gnus-build-all-threads)
4781           (gnus-build-old-threads)))
4782       ;; Let the Gnus agent mark articles as read.
4783       (when gnus-agent
4784         (gnus-agent-get-undownloaded-list))
4785       ;; Remove list identifiers from subject
4786       (when gnus-list-identifiers
4787         (gnus-summary-remove-list-identifiers))
4788       ;; Check whether auto-expire is to be done in this group.
4789       (setq gnus-newsgroup-auto-expire
4790             (gnus-group-auto-expirable-p group))
4791       ;; Set up the article buffer now, if necessary.
4792       (unless gnus-single-article-buffer
4793         (gnus-article-setup-buffer))
4794       ;; First and last article in this newsgroup.
4795       (when gnus-newsgroup-headers
4796         (setq gnus-newsgroup-begin
4797               (mail-header-number (car gnus-newsgroup-headers))
4798               gnus-newsgroup-end
4799               (mail-header-number
4800                (gnus-last-element gnus-newsgroup-headers))))
4801       ;; GROUP is successfully selected.
4802       (or gnus-newsgroup-headers t)))))
4803
4804 (defun gnus-compute-unseen-list ()
4805   ;; The `seen' marks are treated specially.
4806   (if (not gnus-newsgroup-seen)
4807       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
4808     (setq gnus-newsgroup-unseen
4809           (gnus-inverse-list-range-intersection
4810            gnus-newsgroup-articles gnus-newsgroup-seen))))
4811
4812 (defun gnus-summary-display-make-predicate (display)
4813   (require 'gnus-agent)
4814   (when (= (length display) 1)
4815     (setq display (car display)))
4816   (unless gnus-summary-display-cache
4817     (dolist (elem (append '((unread . unread)
4818                             (read . read)
4819                             (unseen . unseen))
4820                           gnus-article-mark-lists))
4821       (push (cons (cdr elem)
4822                   (gnus-byte-compile
4823                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
4824             gnus-summary-display-cache)))
4825   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
4826         (gnus-category-predicate-cache gnus-summary-display-cache))
4827     (gnus-get-predicate display)))
4828
4829 ;; Uses the dynamically bound `number' variable.
4830 (defvar number)
4831 (defun gnus-article-marked-p (type &optional article)
4832   (let ((article (or article number)))
4833     (cond
4834      ((eq type 'tick)
4835       (memq article gnus-newsgroup-marked))
4836      ((eq type 'unsend)
4837       (memq article gnus-newsgroup-unsendable))
4838      ((eq type 'undownload)
4839       (memq article gnus-newsgroup-undownloaded))
4840      ((eq type 'download)
4841       (memq article gnus-newsgroup-downloadable))
4842      ((eq type 'unread)
4843       (memq article gnus-newsgroup-unreads))
4844      ((eq type 'read)
4845       (memq article gnus-newsgroup-reads))
4846      ((eq type 'dormant)
4847       (memq article gnus-newsgroup-dormant) )
4848      ((eq type 'expire)
4849       (memq article gnus-newsgroup-expirable))
4850      ((eq type 'reply)
4851       (memq article gnus-newsgroup-replied))
4852      ((eq type 'killed)
4853       (memq article gnus-newsgroup-killed))
4854      ((eq type 'bookmark)
4855       (assq article gnus-newsgroup-bookmarks))
4856      ((eq type 'score)
4857       (assq article gnus-newsgroup-scored))
4858      ((eq type 'save)
4859       (memq article gnus-newsgroup-saved))
4860      ((eq type 'cache)
4861       (memq article gnus-newsgroup-cached))
4862      ((eq type 'forward)
4863       (memq article gnus-newsgroup-forwarded))
4864      ((eq type 'seen)
4865       (not (memq article gnus-newsgroup-unseen)))
4866      ((eq type 'recent)
4867       (memq article gnus-newsgroup-recent))
4868      (t t))))
4869
4870 (defun gnus-articles-to-read (group &optional read-all)
4871   "Find out what articles the user wants to read."
4872   (let* ((articles
4873           ;; Select all articles if `read-all' is non-nil, or if there
4874           ;; are no unread articles.
4875           (if (or read-all
4876                   (and (zerop (length gnus-newsgroup-marked))
4877                        (zerop (length gnus-newsgroup-unreads)))
4878                   ;; Fetch all if the predicate is non-nil.
4879                   gnus-newsgroup-display)
4880               ;; We want to select the headers for all the articles in
4881               ;; the group, so we select either all the active
4882               ;; articles in the group, or (if that's nil), the
4883               ;; articles in the cache.
4884               (or
4885                (gnus-uncompress-range (gnus-active group))
4886                (gnus-cache-articles-in-group group))
4887             ;; Select only the "normal" subset of articles.
4888             (gnus-sorted-nunion
4889              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
4890              gnus-newsgroup-unreads)))
4891          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4892          (scored (length scored-list))
4893          (number (length articles))
4894          (marked (+ (length gnus-newsgroup-marked)
4895                     (length gnus-newsgroup-dormant)))
4896          (select
4897           (cond
4898            ((numberp read-all)
4899             read-all)
4900            ((numberp gnus-newsgroup-display)
4901             gnus-newsgroup-display)
4902            (t
4903             (condition-case ()
4904                 (cond
4905                  ((and (or (<= scored marked) (= scored number))
4906                        (numberp gnus-large-newsgroup)
4907                        (> number gnus-large-newsgroup))
4908                   (let ((input
4909                          (read-string
4910                           (format
4911                            "How many articles from %s (default %d): "
4912                            (gnus-limit-string
4913                             (gnus-group-decoded-name gnus-newsgroup-name)
4914                             35)
4915                            number))))
4916                     (if (string-match "^[ \t]*$" input) number input)))
4917                  ((and (> scored marked) (< scored number)
4918                        (> (- scored number) 20))
4919                   (let ((input
4920                          (read-string
4921                           (format "%s %s (%d scored, %d total): "
4922                                   "How many articles from"
4923                                   (gnus-group-decoded-name group)
4924                                   scored number))))
4925                     (if (string-match "^[ \t]*$" input)
4926                         number input)))
4927                  (t number))
4928               (quit
4929                (message "Quit getting the articles to read")
4930                nil))))))
4931     (setq select (if (stringp select) (string-to-number select) select))
4932     (if (or (null select) (zerop select))
4933         select
4934       (if (and (not (zerop scored)) (<= (abs select) scored))
4935           (progn
4936             (setq articles (sort scored-list '<))
4937             (setq number (length articles)))
4938         (setq articles (copy-sequence articles)))
4939
4940       (when (< (abs select) number)
4941         (if (< select 0)
4942             ;; Select the N oldest articles.
4943             (setcdr (nthcdr (1- (abs select)) articles) nil)
4944           ;; Select the N most recent articles.
4945           (setq articles (nthcdr (- number select) articles))))
4946       (setq gnus-newsgroup-unselected
4947             (gnus-sorted-difference gnus-newsgroup-unreads articles))
4948       (when gnus-alter-articles-to-read-function
4949         (setq gnus-newsgroup-unreads
4950               (sort
4951                (funcall gnus-alter-articles-to-read-function
4952                         gnus-newsgroup-name gnus-newsgroup-unreads)
4953                '<)))
4954       articles)))
4955
4956 (defun gnus-killed-articles (killed articles)
4957   (let (out)
4958     (while articles
4959       (when (inline (gnus-member-of-range (car articles) killed))
4960         (push (car articles) out))
4961       (setq articles (cdr articles)))
4962     out))
4963
4964 (defun gnus-uncompress-marks (marks)
4965   "Uncompress the mark ranges in MARKS."
4966   (let ((uncompressed '(score bookmark))
4967         out)
4968     (while marks
4969       (if (memq (caar marks) uncompressed)
4970           (push (car marks) out)
4971         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4972       (setq marks (cdr marks)))
4973     out))
4974
4975 (defun gnus-article-mark-to-type (mark)
4976   "Return the type of MARK."
4977   (or (cadr (assq mark gnus-article-special-mark-lists))
4978       'list))
4979
4980 (defun gnus-article-unpropagatable-p (mark)
4981   "Return whether MARK should be propagated to backend."
4982   (memq mark gnus-article-unpropagated-mark-lists))
4983
4984 (defun gnus-adjust-marked-articles (info)
4985   "Set all article lists and remove all marks that are no longer valid."
4986   (let* ((marked-lists (gnus-info-marks info))
4987          (active (gnus-active (gnus-info-group info)))
4988          (min (car active))
4989          (max (cdr active))
4990          (types gnus-article-mark-lists)
4991          marks var articles article mark mark-type)
4992
4993     (dolist (marks marked-lists)
4994       (setq mark (car marks)
4995             mark-type (gnus-article-mark-to-type mark)
4996             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
4997
4998       ;; We set the variable according to the type of the marks list,
4999       ;; and then adjust the marks to a subset of the active articles.
5000       (cond
5001        ;; Adjust "simple" lists.
5002        ((eq mark-type 'list)
5003         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5004         (when (memq mark '(tick dormant expire reply save))
5005           (while articles
5006             (when (or (< (setq article (pop articles)) min) (> article max))
5007               (set var (delq article (symbol-value var)))))))
5008        ;; Adjust assocs.
5009        ((eq mark-type 'tuple)
5010         (set var (setq articles (cdr marks)))
5011         (when (not (listp (cdr (symbol-value var))))
5012           (set var (list (symbol-value var))))
5013         (when (not (listp (cdr articles)))
5014           (setq articles (list articles)))
5015         (while articles
5016           (when (or (not (consp (setq article (pop articles))))
5017                     (< (car article) min)
5018                     (> (car article) max))
5019             (set var (delq article (symbol-value var))))))
5020        ;; Adjust ranges (sloppily).
5021        ((eq mark-type 'range)
5022         (cond
5023          ((eq mark 'seen)
5024           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5025           ;; It should be (seen (NUM1 . NUM2)).
5026           (when (numberp (cddr marks))
5027             (setcdr marks (list (cdr marks))))
5028           (setq articles (cdr marks))
5029           (while (and articles
5030                       (or (and (consp (car articles))
5031                                (> min (cdar articles)))
5032                           (and (numberp (car articles))
5033                                (> min (car articles)))))
5034             (pop articles))
5035           (set var articles))))))))
5036
5037 (defun gnus-update-missing-marks (missing)
5038   "Go through the list of MISSING articles and remove them from the mark lists."
5039   (when missing
5040     (let (var m)
5041       ;; Go through all types.
5042       (dolist (elem gnus-article-mark-lists)
5043         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5044           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5045           (when (symbol-value var)
5046             ;; This list has articles.  So we delete all missing
5047             ;; articles from it.
5048             (setq m missing)
5049             (while m
5050               (set var (delq (pop m) (symbol-value var))))))))))
5051
5052 (defun gnus-update-marks ()
5053   "Enter the various lists of marked articles into the newsgroup info list."
5054   (let ((types gnus-article-mark-lists)
5055         (info (gnus-get-info gnus-newsgroup-name))
5056         type list newmarked symbol delta-marks)
5057     (when info
5058       ;; Add all marks lists to the list of marks lists.
5059       (while (setq type (pop types))
5060         (setq list (symbol-value
5061                     (setq symbol
5062                           (intern (format "gnus-newsgroup-%s" (car type))))))
5063
5064         (when list
5065           ;; Get rid of the entries of the articles that have the
5066           ;; default score.
5067           (when (and (eq (cdr type) 'score)
5068                      gnus-save-score
5069                      list)
5070             (let* ((arts list)
5071                    (prev (cons nil list))
5072                    (all prev))
5073               (while arts
5074                 (if (or (not (consp (car arts)))
5075                         (= (cdar arts) gnus-summary-default-score))
5076                     (setcdr prev (cdr arts))
5077                   (setq prev arts))
5078                 (setq arts (cdr arts)))
5079               (setq list (cdr all)))))
5080
5081         (when (eq (cdr type) 'seen)
5082           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5083
5084         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5085           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5086
5087         (when (and (gnus-check-backend-function
5088                     'request-set-mark gnus-newsgroup-name)
5089                    (not (gnus-article-unpropagatable-p (cdr type))))
5090           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5091                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5092                  (add (gnus-remove-from-range
5093                        (gnus-copy-sequence list) old)))
5094             (when add
5095               (push (list add 'add (list (cdr type))) delta-marks))
5096             (when del
5097               (push (list del 'del (list (cdr type))) delta-marks))))
5098
5099         (when list
5100           (push (cons (cdr type) list) newmarked)))
5101
5102       (when delta-marks
5103         (unless (gnus-check-group gnus-newsgroup-name)
5104           (error "Can't open server for %s" gnus-newsgroup-name))
5105         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5106
5107       ;; Enter these new marks into the info of the group.
5108       (if (nthcdr 3 info)
5109           (setcar (nthcdr 3 info) newmarked)
5110         ;; Add the marks lists to the end of the info.
5111         (when newmarked
5112           (setcdr (nthcdr 2 info) (list newmarked))))
5113
5114       ;; Cut off the end of the info if there's nothing else there.
5115       (let ((i 5))
5116         (while (and (> i 2)
5117                     (not (nth i info)))
5118           (when (nthcdr (decf i) info)
5119             (setcdr (nthcdr i info) nil)))))))
5120
5121 (defun gnus-set-mode-line (where)
5122   "Set the mode line of the article or summary buffers.
5123 If WHERE is `summary', the summary mode line format will be used."
5124   ;; Is this mode line one we keep updated?
5125   (when (and (memq where gnus-updated-mode-lines)
5126              (symbol-value
5127               (intern (format "gnus-%s-mode-line-format-spec" where))))
5128     (let (mode-string)
5129       (save-excursion
5130         ;; We evaluate this in the summary buffer since these
5131         ;; variables are buffer-local to that buffer.
5132         (set-buffer gnus-summary-buffer)
5133        ;; We bind all these variables that are used in the `eval' form
5134         ;; below.
5135         (let* ((mformat (symbol-value
5136                          (intern
5137                           (format "gnus-%s-mode-line-format-spec" where))))
5138                (gnus-tmp-group-name (gnus-group-decoded-name
5139                                      gnus-newsgroup-name))
5140                (gnus-tmp-article-number (or gnus-current-article 0))
5141                (gnus-tmp-unread gnus-newsgroup-unreads)
5142                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5143                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5144                (gnus-tmp-unread-and-unselected
5145                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5146                             (zerop gnus-tmp-unselected))
5147                        "")
5148                       ((zerop gnus-tmp-unselected)
5149                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5150                       (t (format "{%d(+%d) more}"
5151                                  gnus-tmp-unread-and-unticked
5152                                  gnus-tmp-unselected))))
5153                (gnus-tmp-subject
5154                 (if (and gnus-current-headers
5155                          (vectorp gnus-current-headers))
5156                     (gnus-mode-string-quote
5157                      (mail-header-subject gnus-current-headers))
5158                   ""))
5159                bufname-length max-len
5160                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5161           (setq mode-string (eval mformat))
5162           (setq bufname-length (if (string-match "%b" mode-string)
5163                                    (- (length
5164                                        (buffer-name
5165                                         (if (eq where 'summary)
5166                                             nil
5167                                           (get-buffer gnus-article-buffer))))
5168                                       2)
5169                                  0))
5170           (setq max-len (max 4 (if gnus-mode-non-string-length
5171                                    (- (window-width)
5172                                       gnus-mode-non-string-length
5173                                       bufname-length)
5174                                  (length mode-string))))
5175           ;; We might have to chop a bit of the string off...
5176           (when (> (length mode-string) max-len)
5177             (setq mode-string
5178                   (concat (truncate-string-to-width mode-string (- max-len 3))
5179                           "...")))
5180           ;; Pad the mode string a bit.
5181           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5182       ;; Update the mode line.
5183       (setq mode-line-buffer-identification
5184             (gnus-mode-line-buffer-identification (list mode-string)))
5185       (set-buffer-modified-p t))))
5186
5187 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5188   "Go through the HEADERS list and add all Xrefs to a hash table.
5189 The resulting hash table is returned, or nil if no Xrefs were found."
5190   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5191          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5192          (xref-hashtb (gnus-make-hashtable))
5193          start group entry number xrefs header)
5194     (while headers
5195       (setq header (pop headers))
5196       (when (and (setq xrefs (mail-header-xref header))
5197                  (not (memq (setq number (mail-header-number header))
5198                             unreads)))
5199         (setq start 0)
5200         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5201           (setq start (match-end 0))
5202           (setq group (if prefix
5203                           (concat prefix (substring xrefs (match-beginning 1)
5204                                                     (match-end 1)))
5205                         (substring xrefs (match-beginning 1) (match-end 1))))
5206           (setq number
5207                 (string-to-int (substring xrefs (match-beginning 2)
5208                                           (match-end 2))))
5209           (if (setq entry (gnus-gethash group xref-hashtb))
5210               (setcdr entry (cons number (cdr entry)))
5211             (gnus-sethash group (cons number nil) xref-hashtb)))))
5212     (and start xref-hashtb)))
5213
5214 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5215   "Look through all the headers and mark the Xrefs as read."
5216   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5217         name entry info xref-hashtb idlist method nth4)
5218     (save-excursion
5219       (set-buffer gnus-group-buffer)
5220       (when (setq xref-hashtb
5221                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5222         (mapatoms
5223          (lambda (group)
5224            (unless (string= from-newsgroup (setq name (symbol-name group)))
5225              (setq idlist (symbol-value group))
5226              ;; Dead groups are not updated.
5227              (and (prog1
5228                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5229                             info (nth 2 entry))
5230                     (when (stringp (setq nth4 (gnus-info-method info)))
5231                       (setq nth4 (gnus-server-to-method nth4))))
5232                   ;; Only do the xrefs if the group has the same
5233                   ;; select method as the group we have just read.
5234                   (or (gnus-methods-equal-p
5235                        nth4 (gnus-find-method-for-group from-newsgroup))
5236                       virtual
5237                       (equal nth4 (setq method (gnus-find-method-for-group
5238                                                 from-newsgroup)))
5239                       (and (equal (car nth4) (car method))
5240                            (equal (nth 1 nth4) (nth 1 method))))
5241                   gnus-use-cross-reference
5242                   (or (not (eq gnus-use-cross-reference t))
5243                       virtual
5244                       ;; Only do cross-references on subscribed
5245                       ;; groups, if that is what is wanted.
5246                       (<= (gnus-info-level info) gnus-level-subscribed))
5247                   (gnus-group-make-articles-read name idlist))))
5248          xref-hashtb)))))
5249
5250 (defun gnus-compute-read-articles (group articles)
5251   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5252          (info (nth 2 entry))
5253          (active (gnus-active group))
5254          ninfo)
5255     (when entry
5256       ;; First peel off all invalid article numbers.
5257       (when active
5258         (let ((ids articles)
5259               id first)
5260           (while (setq id (pop ids))
5261             (when (and first (> id (cdr active)))
5262               ;; We'll end up in this situation in one particular
5263               ;; obscure situation.  If you re-scan a group and get
5264               ;; a new article that is cross-posted to a different
5265               ;; group that has not been re-scanned, you might get
5266               ;; crossposted article that has a higher number than
5267               ;; Gnus believes possible.  So we re-activate this
5268               ;; group as well.  This might mean doing the
5269               ;; crossposting thingy will *increase* the number
5270               ;; of articles in some groups.  Tsk, tsk.
5271               (setq active (or (gnus-activate-group group) active)))
5272             (when (or (> id (cdr active))
5273                       (< id (car active)))
5274               (setq articles (delq id articles))))))
5275       ;; If the read list is nil, we init it.
5276       (if (and active
5277                (null (gnus-info-read info))
5278                (> (car active) 1))
5279           (setq ninfo (cons 1 (1- (car active))))
5280         (setq ninfo (gnus-info-read info)))
5281       ;; Then we add the read articles to the range.
5282       (gnus-add-to-range
5283        ninfo (setq articles (sort articles '<))))))
5284
5285 (defun gnus-group-make-articles-read (group articles)
5286   "Update the info of GROUP to say that ARTICLES are read."
5287   (let* ((num 0)
5288          (entry (gnus-gethash group gnus-newsrc-hashtb))
5289          (info (nth 2 entry))
5290          (active (gnus-active group))
5291          range)
5292     (when entry
5293       (setq range (gnus-compute-read-articles group articles))
5294       (save-excursion
5295         (set-buffer gnus-group-buffer)
5296         (gnus-undo-register
5297           `(progn
5298              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5299              (gnus-info-set-read ',info ',(gnus-info-read info))
5300              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5301              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5302              (gnus-group-update-group ,group t))))
5303       ;; Add the read articles to the range.
5304       (gnus-info-set-read info range)
5305       (gnus-request-set-mark group (list (list range 'add '(read))))
5306       ;; Then we have to re-compute how many unread
5307       ;; articles there are in this group.
5308       (when active
5309         (cond
5310          ((not range)
5311           (setq num (- (1+ (cdr active)) (car active))))
5312          ((not (listp (cdr range)))
5313           (setq num (- (cdr active) (- (1+ (cdr range))
5314                                        (car range)))))
5315          (t
5316           (while range
5317             (if (numberp (car range))
5318                 (setq num (1+ num))
5319               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5320             (setq range (cdr range)))
5321           (setq num (- (cdr active) num))))
5322         ;; Update the number of unread articles.
5323         (setcar entry num)
5324         ;; Update the group buffer.
5325         (gnus-group-update-group group t)))))
5326
5327 (defvar gnus-newsgroup-none-id 0)
5328
5329 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5330   (let ((cur nntp-server-buffer)
5331         (dependencies
5332          (or dependencies
5333              (save-excursion (set-buffer gnus-summary-buffer)
5334                              gnus-newsgroup-dependencies)))
5335         headers id end ref
5336         (mail-parse-charset gnus-newsgroup-charset)
5337         (mail-parse-ignored-charsets
5338          (save-excursion (condition-case nil
5339                              (set-buffer gnus-summary-buffer)
5340                            (error))
5341                          gnus-newsgroup-ignored-charsets)))
5342     (save-excursion
5343       (set-buffer nntp-server-buffer)
5344       ;; Translate all TAB characters into SPACE characters.
5345       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5346       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5347       (gnus-run-hooks 'gnus-parse-headers-hook)
5348       (let ((case-fold-search t)
5349             in-reply-to header p lines chars)
5350         (goto-char (point-min))
5351         ;; Search to the beginning of the next header.  Error messages
5352         ;; do not begin with 2 or 3.
5353         (while (re-search-forward "^[23][0-9]+ " nil t)
5354           (setq id nil
5355                 ref nil)
5356           ;; This implementation of this function, with nine
5357           ;; search-forwards instead of the one re-search-forward and
5358           ;; a case (which basically was the old function) is actually
5359           ;; about twice as fast, even though it looks messier.  You
5360           ;; can't have everything, I guess.  Speed and elegance
5361           ;; doesn't always go hand in hand.
5362           (setq
5363            header
5364            (vector
5365             ;; Number.
5366             (prog1
5367                 (read cur)
5368               (end-of-line)
5369               (setq p (point))
5370               (narrow-to-region (point)
5371                                 (or (and (search-forward "\n.\n" nil t)
5372                                          (- (point) 2))
5373                                     (point))))
5374             ;; Subject.
5375             (progn
5376               (goto-char p)
5377               (if (search-forward "\nsubject:" nil t)
5378                   (funcall gnus-decode-encoded-word-function
5379                            (nnheader-header-value))
5380                 "(none)"))
5381             ;; From.
5382             (progn
5383               (goto-char p)
5384               (if (search-forward "\nfrom:" nil t)
5385                   (funcall gnus-decode-encoded-word-function
5386                            (nnheader-header-value))
5387                 "(nobody)"))
5388             ;; Date.
5389             (progn
5390               (goto-char p)
5391               (if (search-forward "\ndate:" nil t)
5392                   (nnheader-header-value) ""))
5393             ;; Message-ID.
5394             (progn
5395               (goto-char p)
5396               (setq id (if (re-search-forward
5397                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5398                            ;; We do it this way to make sure the Message-ID
5399                            ;; is (somewhat) syntactically valid.
5400                            (buffer-substring (match-beginning 1)
5401                                              (match-end 1))
5402                          ;; If there was no message-id, we just fake one
5403                          ;; to make subsequent routines simpler.
5404                          (nnheader-generate-fake-message-id))))
5405             ;; References.
5406             (progn
5407               (goto-char p)
5408               (if (search-forward "\nreferences:" nil t)
5409                   (progn
5410                     (setq end (point))
5411                     (prog1
5412                         (nnheader-header-value)
5413                       (setq ref
5414                             (buffer-substring
5415                              (progn
5416                                (end-of-line)
5417                                (search-backward ">" end t)
5418                                (1+ (point)))
5419                              (progn
5420                                (search-backward "<" end t)
5421                                (point))))))
5422                 ;; Get the references from the in-reply-to header if there
5423                 ;; were no references and the in-reply-to header looks
5424                 ;; promising.
5425                 (if (and (search-forward "\nin-reply-to:" nil t)
5426                          (setq in-reply-to (nnheader-header-value))
5427                          (string-match "<[^>]+>" in-reply-to))
5428                     (let (ref2)
5429                       (setq ref (substring in-reply-to (match-beginning 0)
5430                                            (match-end 0)))
5431                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5432                         (setq ref2 (substring in-reply-to (match-beginning 0)
5433                                               (match-end 0)))
5434                         (when (> (length ref2) (length ref))
5435                           (setq ref ref2)))
5436                       ref)
5437                   (setq ref nil))))
5438             ;; Chars.
5439             (progn
5440               (goto-char p)
5441               (if (search-forward "\nchars: " nil t)
5442                   (if (numberp (setq chars (ignore-errors (read cur))))
5443                       chars -1)
5444                 -1))
5445             ;; Lines.
5446             (progn
5447               (goto-char p)
5448               (if (search-forward "\nlines: " nil t)
5449                   (if (numberp (setq lines (ignore-errors (read cur))))
5450                       lines -1)
5451                 -1))
5452             ;; Xref.
5453             (progn
5454               (goto-char p)
5455               (and (search-forward "\nxref:" nil t)
5456                    (nnheader-header-value)))
5457             ;; Extra.
5458             (when gnus-extra-headers
5459               (let ((extra gnus-extra-headers)
5460                     out)
5461                 (while extra
5462                   (goto-char p)
5463                   (when (search-forward
5464                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5465                     (push (cons (car extra) (nnheader-header-value))
5466                           out))
5467                   (pop extra))
5468                 out))))
5469           (when (equal id ref)
5470             (setq ref nil))
5471
5472           (when gnus-alter-header-function
5473             (funcall gnus-alter-header-function header)
5474             (setq id (mail-header-id header)
5475                   ref (gnus-parent-id (mail-header-references header))))
5476
5477           (when (setq header
5478                       (gnus-dependencies-add-header
5479                        header dependencies force-new))
5480             (push header headers))
5481           (goto-char (point-max))
5482           (widen))
5483         (nreverse headers)))))
5484
5485 ;; Goes through the xover lines and returns a list of vectors
5486 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5487                                                   force-new dependencies
5488                                                   group also-fetch-heads)
5489   "Parse the news overview data in the server buffer.
5490 Return a list of headers that match SEQUENCE (see
5491 `nntp-retrieve-headers')."
5492   ;; Get the Xref when the users reads the articles since most/some
5493   ;; NNTP servers do not include Xrefs when using XOVER.
5494   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5495   (let ((mail-parse-charset gnus-newsgroup-charset)
5496         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5497         (cur nntp-server-buffer)
5498         (dependencies (or dependencies gnus-newsgroup-dependencies))
5499         (allp (cond
5500                ((eq gnus-read-all-available-headers t)
5501                 t)
5502                ((stringp gnus-read-all-available-headers)
5503                 (string-match gnus-read-all-available-headers group))
5504                (t
5505                 nil)))
5506         number headers header)
5507     (save-excursion
5508       (set-buffer nntp-server-buffer)
5509       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5510       ;; Allow the user to mangle the headers before parsing them.
5511       (gnus-run-hooks 'gnus-parse-headers-hook)
5512       (goto-char (point-min))
5513       (gnus-parse-without-error
5514         (while (and (or sequence allp)
5515                     (not (eobp)))
5516           (setq number (read cur))
5517           (when (not allp)
5518             (while (and sequence
5519                         (< (car sequence) number))
5520               (setq sequence (cdr sequence))))
5521           (when (and (or allp
5522                          (and sequence
5523                               (eq number (car sequence))))
5524                      (progn
5525                        (setq sequence (cdr sequence))
5526                        (setq header (inline
5527                                       (gnus-nov-parse-line
5528                                        number dependencies force-new)))))
5529             (push header headers))
5530           (forward-line 1)))
5531       ;; A common bug in inn is that if you have posted an article and
5532       ;; then retrieves the active file, it will answer correctly --
5533       ;; the new article is included.  However, a NOV entry for the
5534       ;; article may not have been generated yet, so this may fail.
5535       ;; We work around this problem by retrieving the last few
5536       ;; headers using HEAD.
5537       (if (or (not also-fetch-heads)
5538               (not sequence))
5539           ;; We (probably) got all the headers.
5540           (nreverse headers)
5541         (let ((gnus-nov-is-evil t))
5542           (nconc
5543            (nreverse headers)
5544            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5545              (gnus-get-newsgroup-headers))))))))
5546
5547 (defun gnus-article-get-xrefs ()
5548   "Fill in the Xref value in `gnus-current-headers', if necessary.
5549 This is meant to be called in `gnus-article-internal-prepare-hook'."
5550   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5551                                  gnus-current-headers)))
5552     (or (not gnus-use-cross-reference)
5553         (not headers)
5554         (and (mail-header-xref headers)
5555              (not (string= (mail-header-xref headers) "")))
5556         (let ((case-fold-search t)
5557               xref)
5558           (save-restriction
5559             (nnheader-narrow-to-headers)
5560             (goto-char (point-min))
5561             (when (or (and (not (eobp))
5562                            (eq (downcase (char-after)) ?x)
5563                            (looking-at "Xref:"))
5564                       (search-forward "\nXref:" nil t))
5565               (goto-char (1+ (match-end 0)))
5566               (setq xref (buffer-substring (point)
5567                                            (progn (end-of-line) (point))))
5568               (mail-header-set-xref headers xref)))))))
5569
5570 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5571   "Find article ID and insert the summary line for that article.
5572 OLD-HEADER can either be a header or a line number to insert
5573 the subject line on."
5574   (let* ((line (and (numberp old-header) old-header))
5575          (old-header (and (vectorp old-header) old-header))
5576          (header (cond ((and old-header use-old-header)
5577                         old-header)
5578                        ((and (numberp id)
5579                              (gnus-number-to-header id))
5580                         (gnus-number-to-header id))
5581                        (t
5582                         (gnus-read-header id))))
5583          (number (and (numberp id) id))
5584          d)
5585     (when header
5586       ;; Rebuild the thread that this article is part of and go to the
5587       ;; article we have fetched.
5588       (when (and (not gnus-show-threads)
5589                  old-header)
5590         (when (and number
5591                    (setq d (gnus-data-find (mail-header-number old-header))))
5592           (goto-char (gnus-data-pos d))
5593           (gnus-data-remove
5594            number
5595            (- (gnus-point-at-bol)
5596               (prog1
5597                   (1+ (gnus-point-at-eol))
5598                 (gnus-delete-line))))))
5599       (when old-header
5600         (mail-header-set-number header (mail-header-number old-header)))
5601       (setq gnus-newsgroup-sparse
5602             (delq (setq number (mail-header-number header))
5603                   gnus-newsgroup-sparse))
5604       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5605       (push number gnus-newsgroup-limit)
5606       (gnus-rebuild-thread (mail-header-id header) line)
5607       (gnus-summary-goto-subject number nil t))
5608     (when (and (numberp number)
5609                (> number 0))
5610       ;; We have to update the boundaries even if we can't fetch the
5611       ;; article if ID is a number -- so that the next `P' or `N'
5612       ;; command will fetch the previous (or next) article even
5613       ;; if the one we tried to fetch this time has been canceled.
5614       (when (> number gnus-newsgroup-end)
5615         (setq gnus-newsgroup-end number))
5616       (when (< number gnus-newsgroup-begin)
5617         (setq gnus-newsgroup-begin number))
5618       (setq gnus-newsgroup-unselected
5619             (delq number gnus-newsgroup-unselected)))
5620     ;; Report back a success?
5621     (and header (mail-header-number header))))
5622
5623 ;;; Process/prefix in the summary buffer
5624
5625 (defun gnus-summary-work-articles (n)
5626   "Return a list of articles to be worked upon.
5627 The prefix argument, the list of process marked articles, and the
5628 current article will be taken into consideration."
5629   (save-excursion
5630     (set-buffer gnus-summary-buffer)
5631     (cond
5632      (n
5633       ;; A numerical prefix has been given.
5634       (setq n (prefix-numeric-value n))
5635       (let ((backward (< n 0))
5636             (n (abs (prefix-numeric-value n)))
5637             articles article)
5638         (save-excursion
5639           (while
5640               (and (> n 0)
5641                    (push (setq article (gnus-summary-article-number))
5642                          articles)
5643                    (if backward
5644                        (gnus-summary-find-prev nil article)
5645                      (gnus-summary-find-next nil article)))
5646             (decf n)))
5647         (nreverse articles)))
5648      ((and (gnus-region-active-p) (mark))
5649       (message "region active")
5650       ;; Work on the region between point and mark.
5651       (let ((max (max (point) (mark)))
5652             articles article)
5653         (save-excursion
5654           (goto-char (min (min (point) (mark))))
5655           (while
5656               (and
5657                (push (setq article (gnus-summary-article-number)) articles)
5658                (gnus-summary-find-next nil article)
5659                (< (point) max)))
5660           (nreverse articles))))
5661      (gnus-newsgroup-processable
5662       ;; There are process-marked articles present.
5663       ;; Save current state.
5664       (gnus-summary-save-process-mark)
5665       ;; Return the list.
5666       (reverse gnus-newsgroup-processable))
5667      (t
5668       ;; Just return the current article.
5669       (list (gnus-summary-article-number))))))
5670
5671 (defmacro gnus-summary-iterate (arg &rest forms)
5672   "Iterate over the process/prefixed articles and do FORMS.
5673 ARG is the interactive prefix given to the command.  FORMS will be
5674 executed with point over the summary line of the articles."
5675   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5676     `(let ((,articles (gnus-summary-work-articles ,arg)))
5677        (while ,articles
5678          (gnus-summary-goto-subject (car ,articles))
5679          ,@forms
5680          (pop ,articles)))))
5681
5682 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5683 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5684
5685 (defun gnus-summary-save-process-mark ()
5686   "Push the current set of process marked articles on the stack."
5687   (interactive)
5688   (push (copy-sequence gnus-newsgroup-processable)
5689         gnus-newsgroup-process-stack))
5690
5691 (defun gnus-summary-kill-process-mark ()
5692   "Push the current set of process marked articles on the stack and unmark."
5693   (interactive)
5694   (gnus-summary-save-process-mark)
5695   (gnus-summary-unmark-all-processable))
5696
5697 (defun gnus-summary-yank-process-mark ()
5698   "Pop the last process mark state off the stack and restore it."
5699   (interactive)
5700   (unless gnus-newsgroup-process-stack
5701     (error "Empty mark stack"))
5702   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5703
5704 (defun gnus-summary-process-mark-set (set)
5705   "Make SET into the current process marked articles."
5706   (gnus-summary-unmark-all-processable)
5707   (while set
5708     (gnus-summary-set-process-mark (pop set))))
5709
5710 ;;; Searching and stuff
5711
5712 (defun gnus-summary-search-group (&optional backward use-level)
5713   "Search for next unread newsgroup.
5714 If optional argument BACKWARD is non-nil, search backward instead."
5715   (save-excursion
5716     (set-buffer gnus-group-buffer)
5717     (when (gnus-group-search-forward
5718            backward nil (if use-level (gnus-group-group-level) nil))
5719       (gnus-group-group-name))))
5720
5721 (defun gnus-summary-best-group (&optional exclude-group)
5722   "Find the name of the best unread group.
5723 If EXCLUDE-GROUP, do not go to this group."
5724   (save-excursion
5725     (set-buffer gnus-group-buffer)
5726     (save-excursion
5727       (gnus-group-best-unread-group exclude-group))))
5728
5729 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5730   (if backward (gnus-summary-find-prev)
5731     (let* ((dummy (gnus-summary-article-intangible-p))
5732            (article (or article (gnus-summary-article-number)))
5733            (arts (gnus-data-find-list article))
5734            result)
5735       (when (and (not dummy)
5736                  (or (not gnus-summary-check-current)
5737                      (not unread)
5738                      (not (gnus-data-unread-p (car arts)))))
5739         (setq arts (cdr arts)))
5740       (when (setq result
5741                   (if unread
5742                       (progn
5743                         (while arts
5744                           (when (or (and undownloaded
5745                                          (eq gnus-undownloaded-mark
5746                                              (gnus-data-mark (car arts))))
5747                                     (gnus-data-unread-p (car arts)))
5748                             (setq result (car arts)
5749                                   arts nil))
5750                           (setq arts (cdr arts)))
5751                         result)
5752                     (car arts)))
5753         (goto-char (gnus-data-pos result))
5754         (gnus-data-number result)))))
5755
5756 (defun gnus-summary-find-prev (&optional unread article)
5757   (let* ((eobp (eobp))
5758          (article (or article (gnus-summary-article-number)))
5759          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5760          result)
5761     (when (and (not eobp)
5762                (or (not gnus-summary-check-current)
5763                    (not unread)
5764                    (not (gnus-data-unread-p (car arts)))))
5765       (setq arts (cdr arts)))
5766     (when (setq result
5767                 (if unread
5768                     (progn
5769                       (while arts
5770                         (when (gnus-data-unread-p (car arts))
5771                           (setq result (car arts)
5772                                 arts nil))
5773                         (setq arts (cdr arts)))
5774                       result)
5775                   (car arts)))
5776       (goto-char (gnus-data-pos result))
5777       (gnus-data-number result))))
5778
5779 (defun gnus-summary-find-subject (subject &optional unread backward article)
5780   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5781          (article (or article (gnus-summary-article-number)))
5782          (articles (gnus-data-list backward))
5783          (arts (gnus-data-find-list article articles))
5784          result)
5785     (when (or (not gnus-summary-check-current)
5786               (not unread)
5787               (not (gnus-data-unread-p (car arts))))
5788       (setq arts (cdr arts)))
5789     (while arts
5790       (and (or (not unread)
5791                (gnus-data-unread-p (car arts)))
5792            (vectorp (gnus-data-header (car arts)))
5793            (gnus-subject-equal
5794             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5795            (setq result (car arts)
5796                  arts nil))
5797       (setq arts (cdr arts)))
5798     (and result
5799          (goto-char (gnus-data-pos result))
5800          (gnus-data-number result))))
5801
5802 (defun gnus-summary-search-forward (&optional unread subject backward)
5803   "Search forward for an article.
5804 If UNREAD, look for unread articles.  If SUBJECT, look for
5805 articles with that subject.  If BACKWARD, search backward instead."
5806   (cond (subject (gnus-summary-find-subject subject unread backward))
5807         (backward (gnus-summary-find-prev unread))
5808         (t (gnus-summary-find-next unread))))
5809
5810 (defun gnus-recenter (&optional n)
5811   "Center point in window and redisplay frame.
5812 Also do horizontal recentering."
5813   (interactive "P")
5814   (when (and gnus-auto-center-summary
5815              (not (eq gnus-auto-center-summary 'vertical)))
5816     (gnus-horizontal-recenter))
5817   (recenter n))
5818
5819 (defun gnus-summary-recenter ()
5820   "Center point in the summary window.
5821 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5822 displayed, no centering will be performed."
5823   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5824 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5825   (interactive)
5826   (let* ((top (cond ((< (window-height) 4) 0)
5827                     ((< (window-height) 7) 1)
5828                     (t (if (numberp gnus-auto-center-summary)
5829                            gnus-auto-center-summary
5830                          2))))
5831          (height (1- (window-height)))
5832          (bottom (save-excursion (goto-char (point-max))
5833                                  (forward-line (- height))
5834                                  (point)))
5835          (window (get-buffer-window (current-buffer))))
5836     ;; The user has to want it.
5837     (when gnus-auto-center-summary
5838       (when (get-buffer-window gnus-article-buffer)
5839         ;; Only do recentering when the article buffer is displayed,
5840       ;; Set the window start to either `bottom', which is the biggest
5841         ;; possible valid number, or the second line from the top,
5842         ;; whichever is the least.
5843         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5844           (if (> bottom top-pos)
5845               ;; Keep the second line from the top visible
5846               (set-window-start window top-pos t)
5847             ;; Try to keep the bottom line visible; if it's partially
5848             ;; obscured, either scroll one more line to make it fully
5849             ;; visible, or revert to using TOP-POS.
5850             (save-excursion
5851               (goto-char (point-max))
5852               (forward-line -1)
5853               (let ((last-line-start (point)))
5854                 (goto-char bottom)
5855                 (set-window-start window (point) t)
5856                 (when (not (pos-visible-in-window-p last-line-start window))
5857                   (forward-line 1)
5858                   (set-window-start window (min (point) top-pos) t)))))))
5859       ;; Do horizontal recentering while we're at it.
5860       (when (and (get-buffer-window (current-buffer) t)
5861                  (not (eq gnus-auto-center-summary 'vertical)))
5862         (let ((selected (selected-window)))
5863           (select-window (get-buffer-window (current-buffer) t))
5864           (gnus-summary-position-point)
5865           (gnus-horizontal-recenter)
5866           (select-window selected))))))
5867
5868 (defun gnus-summary-jump-to-group (newsgroup)
5869   "Move point to NEWSGROUP in group mode buffer."
5870   ;; Keep update point of group mode buffer if visible.
5871   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5872       (save-window-excursion
5873         ;; Take care of tree window mode.
5874         (when (get-buffer-window gnus-group-buffer)
5875           (pop-to-buffer gnus-group-buffer))
5876         (gnus-group-jump-to-group newsgroup))
5877     (save-excursion
5878       ;; Take care of tree window mode.
5879       (if (get-buffer-window gnus-group-buffer)
5880           (pop-to-buffer gnus-group-buffer)
5881         (set-buffer gnus-group-buffer))
5882       (gnus-group-jump-to-group newsgroup))))
5883
5884 ;; This function returns a list of article numbers based on the
5885 ;; difference between the ranges of read articles in this group and
5886 ;; the range of active articles.
5887 (defun gnus-list-of-unread-articles (group)
5888   (let* ((read (gnus-info-read (gnus-get-info group)))
5889          (active (or (gnus-active group) (gnus-activate-group group)))
5890          (last (cdr active))
5891          first nlast unread)
5892     ;; If none are read, then all are unread.
5893     (if (not read)
5894         (setq first (car active))
5895       ;; If the range of read articles is a single range, then the
5896       ;; first unread article is the article after the last read
5897       ;; article.  Sounds logical, doesn't it?
5898       (if (and (not (listp (cdr read)))
5899                (or (< (car read) (car active))
5900                    (progn (setq read (list read))
5901                           nil)))
5902           (setq first (max (car active) (1+ (cdr read))))
5903         ;; `read' is a list of ranges.
5904         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5905                                   (caar read)))
5906                   1)
5907           (setq first (car active)))
5908         (while read
5909           (when first
5910             (while (< first nlast)
5911               (push first unread)
5912               (setq first (1+ first))))
5913           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5914           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5915           (setq read (cdr read)))))
5916     ;; And add the last unread articles.
5917     (while (<= first last)
5918       (push first unread)
5919       (setq first (1+ first)))
5920     ;; Return the list of unread articles.
5921     (delq 0 (nreverse unread))))
5922
5923 (defun gnus-list-of-read-articles (group)
5924   "Return a list of unread, unticked and non-dormant articles."
5925   (let* ((info (gnus-get-info group))
5926          (marked (gnus-info-marks info))
5927          (active (gnus-active group)))
5928     (and info active
5929          (gnus-list-range-difference
5930           (gnus-list-range-difference
5931            (gnus-sorted-complement
5932             (gnus-uncompress-range active)
5933             (gnus-list-of-unread-articles group))
5934            (cdr (assq 'dormant marked)))
5935           (cdr (assq 'tick marked))))))
5936
5937 ;; Various summary commands
5938
5939 (defun gnus-summary-select-article-buffer ()
5940   "Reconfigure windows to show article buffer."
5941   (interactive)
5942   (if (not (gnus-buffer-live-p gnus-article-buffer))
5943       (error "There is no article buffer for this summary buffer")
5944     (gnus-configure-windows 'article)
5945     (select-window (get-buffer-window gnus-article-buffer))))
5946
5947 (defun gnus-summary-universal-argument (arg)
5948   "Perform any operation on all articles that are process/prefixed."
5949   (interactive "P")
5950   (let ((articles (gnus-summary-work-articles arg))
5951         func article)
5952     (if (eq
5953          (setq
5954           func
5955           (key-binding
5956            (read-key-sequence
5957             (substitute-command-keys
5958              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5959          'undefined)
5960         (gnus-error 1 "Undefined key")
5961       (save-excursion
5962         (while articles
5963           (gnus-summary-goto-subject (setq article (pop articles)))
5964           (let (gnus-newsgroup-processable)
5965             (command-execute func))
5966           (gnus-summary-remove-process-mark article)))))
5967   (gnus-summary-position-point))
5968
5969 (defun gnus-summary-toggle-truncation (&optional arg)
5970   "Toggle truncation of summary lines.
5971 With arg, turn line truncation on iff arg is positive."
5972   (interactive "P")
5973   (setq truncate-lines
5974         (if (null arg) (not truncate-lines)
5975           (> (prefix-numeric-value arg) 0)))
5976   (redraw-display))
5977
5978 (defun gnus-summary-reselect-current-group (&optional all rescan)
5979   "Exit and then reselect the current newsgroup.
5980 The prefix argument ALL means to select all articles."
5981   (interactive "P")
5982   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5983     (error "Ephemeral groups can't be reselected"))
5984   (let ((current-subject (gnus-summary-article-number))
5985         (group gnus-newsgroup-name))
5986     (setq gnus-newsgroup-begin nil)
5987     (gnus-summary-exit)
5988     ;; We have to adjust the point of group mode buffer because
5989     ;; point was moved to the next unread newsgroup by exiting.
5990     (gnus-summary-jump-to-group group)
5991     (when rescan
5992       (save-excursion
5993         (gnus-group-get-new-news-this-group 1)))
5994     (gnus-group-read-group all t)
5995     (gnus-summary-goto-subject current-subject nil t)))
5996
5997 (defun gnus-summary-rescan-group (&optional all)
5998   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5999   (interactive "P")
6000   (gnus-summary-reselect-current-group all t))
6001
6002 (defun gnus-summary-update-info (&optional non-destructive)
6003   (save-excursion
6004     (let ((group gnus-newsgroup-name))
6005       (when group
6006         (when gnus-newsgroup-kill-headers
6007           (setq gnus-newsgroup-killed
6008                 (gnus-compress-sequence
6009                  (gnus-sorted-union
6010                   (gnus-list-range-intersection
6011                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6012                   gnus-newsgroup-unreads)
6013                  t)))
6014         (unless (listp (cdr gnus-newsgroup-killed))
6015           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6016         (let ((headers gnus-newsgroup-headers))
6017           ;; Set the new ranges of read articles.
6018           (save-excursion
6019             (set-buffer gnus-group-buffer)
6020             (gnus-undo-force-boundary))
6021           (gnus-update-read-articles
6022            group (gnus-sorted-union
6023                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6024           ;; Set the current article marks.
6025           (let ((gnus-newsgroup-scored
6026                  (if (and (not gnus-save-score)
6027                           (not non-destructive))
6028                      nil
6029                    gnus-newsgroup-scored)))
6030             (save-excursion
6031               (gnus-update-marks)))
6032           ;; Do the cross-ref thing.
6033           (when gnus-use-cross-reference
6034             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6035           ;; Do not switch windows but change the buffer to work.
6036           (set-buffer gnus-group-buffer)
6037           (unless (gnus-ephemeral-group-p group)
6038             (gnus-group-update-group group)))))))
6039
6040 (defun gnus-summary-save-newsrc (&optional force)
6041   "Save the current number of read/marked articles in the dribble buffer.
6042 The dribble buffer will then be saved.
6043 If FORCE (the prefix), also save the .newsrc file(s)."
6044   (interactive "P")
6045   (gnus-summary-update-info t)
6046   (if force
6047       (gnus-save-newsrc-file)
6048     (gnus-dribble-save)))
6049
6050 (defun gnus-summary-exit (&optional temporary)
6051   "Exit reading current newsgroup, and then return to group selection mode.
6052 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6053   (interactive)
6054   (gnus-set-global-variables)
6055   (when (gnus-buffer-live-p gnus-article-buffer)
6056     (save-excursion
6057       (set-buffer gnus-article-buffer)
6058       (mm-destroy-parts gnus-article-mime-handles)
6059       ;; Set it to nil for safety reason.
6060       (setq gnus-article-mime-handle-alist nil)
6061       (setq gnus-article-mime-handles nil)))
6062   (gnus-kill-save-kill-buffer)
6063   (gnus-async-halt-prefetch)
6064   (let* ((group gnus-newsgroup-name)
6065          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6066          (mode major-mode)
6067          (group-point nil)
6068          (buf (current-buffer)))
6069     (unless quit-config
6070       ;; Do adaptive scoring, and possibly save score files.
6071       (when gnus-newsgroup-adaptive
6072         (gnus-score-adaptive))
6073       (when gnus-use-scoring
6074         (gnus-score-save)))
6075     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6076     ;; If we have several article buffers, we kill them at exit.
6077     (unless gnus-single-article-buffer
6078       (gnus-kill-buffer gnus-original-article-buffer)
6079       (setq gnus-article-current nil))
6080     (when gnus-use-cache
6081       (gnus-cache-possibly-remove-articles)
6082       (gnus-cache-save-buffers))
6083     (gnus-async-prefetch-remove-group group)
6084     (when gnus-suppress-duplicates
6085       (gnus-dup-enter-articles))
6086     (when gnus-use-trees
6087       (gnus-tree-close group))
6088     (when gnus-use-cache
6089       (gnus-cache-write-active))
6090     ;; Remove entries for this group.
6091     (nnmail-purge-split-history (gnus-group-real-name group))
6092     ;; Make all changes in this group permanent.
6093     (unless quit-config
6094       (gnus-run-hooks 'gnus-exit-group-hook)
6095       (gnus-summary-update-info))
6096     (gnus-close-group group)
6097     ;; Make sure where we were, and go to next newsgroup.
6098     (set-buffer gnus-group-buffer)
6099     (unless quit-config
6100       (gnus-group-jump-to-group group))
6101     (gnus-run-hooks 'gnus-summary-exit-hook)
6102     (unless (or quit-config
6103                 ;; If this group has disappeared from the summary
6104                 ;; buffer, don't skip forwards.
6105                 (not (string= group (gnus-group-group-name))))
6106       (gnus-group-next-unread-group 1))
6107     (setq group-point (point))
6108     (if temporary
6109         nil                             ;Nothing to do.
6110       ;; If we have several article buffers, we kill them at exit.
6111       (unless gnus-single-article-buffer
6112         (gnus-kill-buffer gnus-article-buffer)
6113         (gnus-kill-buffer gnus-original-article-buffer)
6114         (setq gnus-article-current nil))
6115       (set-buffer buf)
6116       (if (not gnus-kill-summary-on-exit)
6117           (progn
6118             (gnus-deaden-summary)
6119             (setq mode nil))
6120        ;; We set all buffer-local variables to nil.  It is unclear why
6121         ;; this is needed, but if we don't, buffer-local variables are
6122         ;; not garbage-collected, it seems.  This would the lead to en
6123         ;; ever-growing Emacs.
6124         (gnus-summary-clear-local-variables)
6125         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6126           (gnus-summary-clear-local-variables))
6127         (when (get-buffer gnus-article-buffer)
6128           (bury-buffer gnus-article-buffer))
6129         ;; We clear the global counterparts of the buffer-local
6130         ;; variables as well, just to be on the safe side.
6131         (set-buffer gnus-group-buffer)
6132         (gnus-summary-clear-local-variables)
6133         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6134           (gnus-summary-clear-local-variables)))
6135       (setq gnus-current-select-method gnus-select-method)
6136       (pop-to-buffer gnus-group-buffer)
6137       (if (not quit-config)
6138           (progn
6139             (goto-char group-point)
6140             (gnus-configure-windows 'group 'force))
6141         (gnus-handle-ephemeral-exit quit-config))
6142       ;; Return to group mode buffer.
6143       (when (eq mode 'gnus-summary-mode)
6144         (gnus-kill-buffer buf))
6145       ;; Clear the current group name.
6146       (unless quit-config
6147         (setq gnus-newsgroup-name nil)))))
6148
6149 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6150 (defun gnus-summary-exit-no-update (&optional no-questions)
6151   "Quit reading current newsgroup without updating read article info."
6152   (interactive)
6153   (let* ((group gnus-newsgroup-name)
6154          (quit-config (gnus-group-quit-config group)))
6155     (when (or no-questions
6156               gnus-expert-user
6157               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6158       (gnus-async-halt-prefetch)
6159       (mapcar 'funcall
6160               (delq 'gnus-summary-expire-articles
6161                     (copy-sequence gnus-summary-prepare-exit-hook)))
6162       (when (gnus-buffer-live-p gnus-article-buffer)
6163         (save-excursion
6164           (set-buffer gnus-article-buffer)
6165           (mm-destroy-parts gnus-article-mime-handles)
6166           ;; Set it to nil for safety reason.
6167           (setq gnus-article-mime-handle-alist nil)
6168           (setq gnus-article-mime-handles nil)))
6169       ;; If we have several article buffers, we kill them at exit.
6170       (unless gnus-single-article-buffer
6171         (gnus-kill-buffer gnus-article-buffer)
6172         (gnus-kill-buffer gnus-original-article-buffer)
6173         (setq gnus-article-current nil))
6174       (if (not gnus-kill-summary-on-exit)
6175           (gnus-deaden-summary)
6176         (gnus-close-group group)
6177         (gnus-summary-clear-local-variables)
6178         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6179           (gnus-summary-clear-local-variables))
6180         (set-buffer gnus-group-buffer)
6181         (gnus-summary-clear-local-variables)
6182         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6183           (gnus-summary-clear-local-variables))
6184         (when (get-buffer gnus-summary-buffer)
6185           (kill-buffer gnus-summary-buffer)))
6186       (unless gnus-single-article-buffer
6187         (setq gnus-article-current nil))
6188       (when gnus-use-trees
6189         (gnus-tree-close group))
6190       (gnus-async-prefetch-remove-group group)
6191       (when (get-buffer gnus-article-buffer)
6192         (bury-buffer gnus-article-buffer))
6193       ;; Return to the group buffer.
6194       (gnus-configure-windows 'group 'force)
6195       ;; Clear the current group name.
6196       (setq gnus-newsgroup-name nil)
6197       (when (equal (gnus-group-group-name) group)
6198         (gnus-group-next-unread-group 1))
6199       (when quit-config
6200         (gnus-handle-ephemeral-exit quit-config)))))
6201
6202 (defun gnus-handle-ephemeral-exit (quit-config)
6203   "Handle movement when leaving an ephemeral group.
6204 The state which existed when entering the ephemeral is reset."
6205   (if (not (buffer-name (car quit-config)))
6206       (gnus-configure-windows 'group 'force)
6207     (set-buffer (car quit-config))
6208     (cond ((eq major-mode 'gnus-summary-mode)
6209            (gnus-set-global-variables))
6210           ((eq major-mode 'gnus-article-mode)
6211            (save-excursion
6212              ;; The `gnus-summary-buffer' variable may point
6213              ;; to the old summary buffer when using a single
6214              ;; article buffer.
6215              (unless (gnus-buffer-live-p gnus-summary-buffer)
6216                (set-buffer gnus-group-buffer))
6217              (set-buffer gnus-summary-buffer)
6218              (gnus-set-global-variables))))
6219     (if (or (eq (cdr quit-config) 'article)
6220             (eq (cdr quit-config) 'pick))
6221         (progn
6222           ;; The current article may be from the ephemeral group
6223           ;; thus it is best that we reload this article
6224           (gnus-summary-show-article)
6225           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6226               (gnus-configure-windows 'pick 'force)
6227             (gnus-configure-windows (cdr quit-config) 'force)))
6228       (gnus-configure-windows (cdr quit-config) 'force))
6229     (when (eq major-mode 'gnus-summary-mode)
6230       (gnus-summary-next-subject 1 nil t)
6231       (gnus-summary-recenter)
6232       (gnus-summary-position-point))))
6233
6234 ;;; Dead summaries.
6235
6236 (defvar gnus-dead-summary-mode-map nil)
6237
6238 (unless gnus-dead-summary-mode-map
6239   (setq gnus-dead-summary-mode-map (make-keymap))
6240   (suppress-keymap gnus-dead-summary-mode-map)
6241   (substitute-key-definition
6242    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6243   (dolist (key '("\C-d" "\r" "\177" [delete]))
6244     (define-key gnus-dead-summary-mode-map
6245       key 'gnus-summary-wake-up-the-dead))
6246   (dolist (key '("q" "Q"))
6247     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6248
6249 (defvar gnus-dead-summary-mode nil
6250   "Minor mode for Gnus summary buffers.")
6251
6252 (defun gnus-dead-summary-mode (&optional arg)
6253   "Minor mode for Gnus summary buffers."
6254   (interactive "P")
6255   (when (eq major-mode 'gnus-summary-mode)
6256     (make-local-variable 'gnus-dead-summary-mode)
6257     (setq gnus-dead-summary-mode
6258           (if (null arg) (not gnus-dead-summary-mode)
6259             (> (prefix-numeric-value arg) 0)))
6260     (when gnus-dead-summary-mode
6261       (gnus-add-minor-mode
6262        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6263
6264 (defun gnus-deaden-summary ()
6265   "Make the current summary buffer into a dead summary buffer."
6266   ;; Kill any previous dead summary buffer.
6267   (when (and gnus-dead-summary
6268              (buffer-name gnus-dead-summary))
6269     (save-excursion
6270       (set-buffer gnus-dead-summary)
6271       (when gnus-dead-summary-mode
6272         (kill-buffer (current-buffer)))))
6273   ;; Make this the current dead summary.
6274   (setq gnus-dead-summary (current-buffer))
6275   (gnus-dead-summary-mode 1)
6276   (let ((name (buffer-name)))
6277     (when (string-match "Summary" name)
6278       (rename-buffer
6279        (concat (substring name 0 (match-beginning 0)) "Dead "
6280                (substring name (match-beginning 0)))
6281        t)
6282       (bury-buffer))))
6283
6284 (defun gnus-kill-or-deaden-summary (buffer)
6285   "Kill or deaden the summary BUFFER."
6286   (save-excursion
6287     (when (and (buffer-name buffer)
6288                (not gnus-single-article-buffer))
6289       (save-excursion
6290         (set-buffer buffer)
6291         (gnus-kill-buffer gnus-article-buffer)
6292         (gnus-kill-buffer gnus-original-article-buffer)))
6293     (cond
6294      ;; Kill the buffer.
6295      (gnus-kill-summary-on-exit
6296       (when (and gnus-use-trees
6297                  (gnus-buffer-exists-p buffer))
6298         (save-excursion
6299           (set-buffer buffer)
6300           (gnus-tree-close gnus-newsgroup-name)))
6301       (gnus-kill-buffer buffer))
6302      ;; Deaden the buffer.
6303      ((gnus-buffer-exists-p buffer)
6304       (save-excursion
6305         (set-buffer buffer)
6306         (gnus-deaden-summary))))))
6307
6308 (defun gnus-summary-wake-up-the-dead (&rest args)
6309   "Wake up the dead summary buffer."
6310   (interactive)
6311   (gnus-dead-summary-mode -1)
6312   (let ((name (buffer-name)))
6313     (when (string-match "Dead " name)
6314       (rename-buffer
6315        (concat (substring name 0 (match-beginning 0))
6316                (substring name (match-end 0)))
6317        t)))
6318   (gnus-message 3 "This dead summary is now alive again"))
6319
6320 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6321 (defun gnus-summary-fetch-faq (&optional faq-dir)
6322   "Fetch the FAQ for the current group.
6323 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6324 in."
6325   (interactive
6326    (list
6327     (when current-prefix-arg
6328       (completing-read
6329        "Faq dir: " (and (listp gnus-group-faq-directory)
6330                         (mapcar (lambda (file) (list file))
6331                                 gnus-group-faq-directory))))))
6332   (let (gnus-faq-buffer)
6333     (when (setq gnus-faq-buffer
6334                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6335       (gnus-configure-windows 'summary-faq))))
6336
6337 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6338 (defun gnus-summary-describe-group (&optional force)
6339   "Describe the current newsgroup."
6340   (interactive "P")
6341   (gnus-group-describe-group force gnus-newsgroup-name))
6342
6343 (defun gnus-summary-describe-briefly ()
6344   "Describe summary mode commands briefly."
6345   (interactive)
6346   (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")))
6347
6348 ;; Walking around group mode buffer from summary mode.
6349
6350 (defun gnus-summary-next-group (&optional no-article target-group backward)
6351   "Exit current newsgroup and then select next unread newsgroup.
6352 If prefix argument NO-ARTICLE is non-nil, no article is selected
6353 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6354 previous group instead."
6355   (interactive "P")
6356   ;; Stop pre-fetching.
6357   (gnus-async-halt-prefetch)
6358   (let ((current-group gnus-newsgroup-name)
6359         (current-buffer (current-buffer))
6360         entered)
6361    ;; First we semi-exit this group to update Xrefs and all variables.
6362     ;; We can't do a real exit, because the window conf must remain
6363     ;; the same in case the user is prompted for info, and we don't
6364     ;; want the window conf to change before that...
6365     (gnus-summary-exit t)
6366     (while (not entered)
6367       ;; Then we find what group we are supposed to enter.
6368       (set-buffer gnus-group-buffer)
6369       (gnus-group-jump-to-group current-group)
6370       (setq target-group
6371             (or target-group
6372                 (if (eq gnus-keep-same-level 'best)
6373                     (gnus-summary-best-group gnus-newsgroup-name)
6374                   (gnus-summary-search-group backward gnus-keep-same-level))))
6375       (if (not target-group)
6376           ;; There are no further groups, so we return to the group
6377           ;; buffer.
6378           (progn
6379             (gnus-message 5 "Returning to the group buffer")
6380             (setq entered t)
6381             (when (gnus-buffer-live-p current-buffer)
6382               (set-buffer current-buffer)
6383               (gnus-summary-exit))
6384             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6385         ;; We try to enter the target group.
6386         (gnus-group-jump-to-group target-group)
6387         (let ((unreads (gnus-group-group-unread)))
6388           (if (and (or (eq t unreads)
6389                        (and unreads (not (zerop unreads))))
6390                    (gnus-summary-read-group
6391                     target-group nil no-article
6392                     (and (buffer-name current-buffer) current-buffer)
6393                     nil backward))
6394               (setq entered t)
6395             (setq current-group target-group
6396                   target-group nil)))))))
6397
6398 (defun gnus-summary-prev-group (&optional no-article)
6399   "Exit current newsgroup and then select previous unread newsgroup.
6400 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6401   (interactive "P")
6402   (gnus-summary-next-group no-article nil t))
6403
6404 ;; Walking around summary lines.
6405
6406 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6407   "Go to the first unread subject.
6408 If UNREAD is non-nil, go to the first unread article.
6409 Returns the article selected or nil if there are no unread articles."
6410   (interactive "P")
6411   (prog1
6412       (cond
6413        ;; Empty summary.
6414        ((null gnus-newsgroup-data)
6415         (gnus-message 3 "No articles in the group")
6416         nil)
6417        ;; Pick the first article.
6418        ((not unread)
6419         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6420         (gnus-data-number (car gnus-newsgroup-data)))
6421        ;; No unread articles.
6422        ((null gnus-newsgroup-unreads)
6423         (gnus-message 3 "No more unread articles")
6424         nil)
6425        ;; Find the first unread article.
6426        (t
6427         (let ((data gnus-newsgroup-data))
6428           (while (and data
6429                       (and (not (and undownloaded
6430                                      (eq gnus-undownloaded-mark
6431                                          (gnus-data-mark (car data)))))
6432                            (if unseen
6433                                (or (not (memq
6434                                          (gnus-data-number (car data))
6435                                          gnus-newsgroup-unseen))
6436                                    (not (gnus-data-unread-p (car data))))
6437                              (not (gnus-data-unread-p (car data))))))
6438             (setq data (cdr data)))
6439           (when data
6440             (goto-char (gnus-data-pos (car data)))
6441             (gnus-data-number (car data))))))
6442     (gnus-summary-position-point)))
6443
6444 (defun gnus-summary-next-subject (n &optional unread dont-display)
6445   "Go to next N'th summary line.
6446 If N is negative, go to the previous N'th subject line.
6447 If UNREAD is non-nil, only unread articles are selected.
6448 The difference between N and the actual number of steps taken is
6449 returned."
6450   (interactive "p")
6451   (let ((backward (< n 0))
6452         (n (abs n)))
6453     (while (and (> n 0)
6454                 (if backward
6455                     (gnus-summary-find-prev unread)
6456                   (gnus-summary-find-next unread)))
6457       (unless (zerop (setq n (1- n)))
6458         (gnus-summary-show-thread)))
6459     (when (/= 0 n)
6460       (gnus-message 7 "No more%s articles"
6461                     (if unread " unread" "")))
6462     (unless dont-display
6463       (gnus-summary-recenter)
6464       (gnus-summary-position-point))
6465     n))
6466
6467 (defun gnus-summary-next-unread-subject (n)
6468   "Go to next N'th unread summary line."
6469   (interactive "p")
6470   (gnus-summary-next-subject n t))
6471
6472 (defun gnus-summary-prev-subject (n &optional unread)
6473   "Go to previous N'th summary line.
6474 If optional argument UNREAD is non-nil, only unread article is selected."
6475   (interactive "p")
6476   (gnus-summary-next-subject (- n) unread))
6477
6478 (defun gnus-summary-prev-unread-subject (n)
6479   "Go to previous N'th unread summary line."
6480   (interactive "p")
6481   (gnus-summary-next-subject (- n) t))
6482
6483 (defun gnus-summary-goto-subject (article &optional force silent)
6484   "Go the subject line of ARTICLE.
6485 If FORCE, also allow jumping to articles not currently shown."
6486   (interactive "nArticle number: ")
6487   (unless (numberp article)
6488     (error "Article %s is not a number" article))
6489   (let ((b (point))
6490         (data (gnus-data-find article)))
6491     ;; We read in the article if we have to.
6492     (and (not data)
6493          force
6494          (gnus-summary-insert-subject
6495           article
6496           (if (or (numberp force) (vectorp force)) force)
6497           t)
6498          (setq data (gnus-data-find article)))
6499     (goto-char b)
6500     (if (not data)
6501         (progn
6502           (unless silent
6503             (gnus-message 3 "Can't find article %d" article))
6504           nil)
6505       (let ((pt (gnus-data-pos data)))
6506         (goto-char pt)
6507         (gnus-summary-set-article-display-arrow pt))
6508       (gnus-summary-position-point)
6509       article)))
6510
6511 ;; Walking around summary lines with displaying articles.
6512
6513 (defun gnus-summary-expand-window (&optional arg)
6514   "Make the summary buffer take up the entire Emacs frame.
6515 Given a prefix, will force an `article' buffer configuration."
6516   (interactive "P")
6517   (if arg
6518       (gnus-configure-windows 'article 'force)
6519     (gnus-configure-windows 'summary 'force)))
6520
6521 (defun gnus-summary-display-article (article &optional all-header)
6522   "Display ARTICLE in article buffer."
6523   (when (gnus-buffer-live-p gnus-article-buffer)
6524     (with-current-buffer gnus-article-buffer
6525       (mm-enable-multibyte)))
6526   (gnus-set-global-variables)
6527   (when (gnus-buffer-live-p gnus-article-buffer)
6528     (with-current-buffer gnus-article-buffer
6529       (setq gnus-article-charset gnus-newsgroup-charset)
6530       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6531       (mm-enable-multibyte)))
6532   (if (null article)
6533       nil
6534     (prog1
6535         (if gnus-summary-display-article-function
6536             (funcall gnus-summary-display-article-function article all-header)
6537           (gnus-article-prepare article all-header))
6538       (gnus-run-hooks 'gnus-select-article-hook)
6539       (when (and gnus-current-article
6540                  (not (zerop gnus-current-article)))
6541         (gnus-summary-goto-subject gnus-current-article))
6542       (gnus-summary-recenter)
6543       (when (and gnus-use-trees gnus-show-threads)
6544         (gnus-possibly-generate-tree article)
6545         (gnus-highlight-selected-tree article))
6546       ;; Successfully display article.
6547       (gnus-article-set-window-start
6548        (cdr (assq article gnus-newsgroup-bookmarks))))))
6549
6550 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6551   "Select the current article.
6552 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6553 non-nil, the article will be re-fetched even if it already present in
6554 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6555 be displayed."
6556   ;; Make sure we are in the summary buffer to work around bbdb bug.
6557   (unless (eq major-mode 'gnus-summary-mode)
6558     (set-buffer gnus-summary-buffer))
6559   (let ((article (or article (gnus-summary-article-number)))
6560         (all-headers (not (not all-headers))) ;Must be t or nil.
6561         gnus-summary-display-article-function)
6562     (and (not pseudo)
6563          (gnus-summary-article-pseudo-p article)
6564          (error "This is a pseudo-article"))
6565     (save-excursion
6566       (set-buffer gnus-summary-buffer)
6567       (if (or (and gnus-single-article-buffer
6568                    (or (null gnus-current-article)
6569                        (null gnus-article-current)
6570                        (null (get-buffer gnus-article-buffer))
6571                        (not (eq article (cdr gnus-article-current)))
6572                        (not (equal (car gnus-article-current)
6573                                    gnus-newsgroup-name))))
6574               (and (not gnus-single-article-buffer)
6575                    (or (null gnus-current-article)
6576                        (not (eq gnus-current-article article))))
6577               force)
6578           ;; The requested article is different from the current article.
6579           (progn
6580             (gnus-summary-display-article article all-headers)
6581             (when (gnus-buffer-live-p gnus-article-buffer)
6582               (with-current-buffer gnus-article-buffer
6583                 (if (not gnus-article-decoded-p) ;; a local variable
6584                     (mm-disable-multibyte))))
6585             (gnus-article-set-window-start
6586              (cdr (assq article gnus-newsgroup-bookmarks)))
6587             article)
6588         'old))))
6589
6590 (defun gnus-summary-force-verify-and-decrypt ()
6591   (interactive)
6592   (let ((mm-verify-option 'known)
6593         (mm-decrypt-option 'known))
6594     (gnus-summary-select-article nil 'force)))
6595
6596 (defun gnus-summary-set-current-mark (&optional current-mark)
6597   "Obsolete function."
6598   nil)
6599
6600 (defun gnus-summary-next-article (&optional unread subject backward push)
6601   "Select the next article.
6602 If UNREAD, only unread articles are selected.
6603 If SUBJECT, only articles with SUBJECT are selected.
6604 If BACKWARD, the previous article is selected instead of the next."
6605   (interactive "P")
6606   (cond
6607    ;; Is there such an article?
6608    ((and (gnus-summary-search-forward unread subject backward)
6609          (or (gnus-summary-display-article (gnus-summary-article-number))
6610              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6611     (gnus-summary-position-point))
6612    ;; If not, we try the first unread, if that is wanted.
6613    ((and subject
6614          gnus-auto-select-same
6615          (gnus-summary-first-unread-article))
6616     (gnus-summary-position-point)
6617     (gnus-message 6 "Wrapped"))
6618    ;; Try to get next/previous article not displayed in this group.
6619    ((and gnus-auto-extend-newsgroup
6620          (not unread) (not subject))
6621     (gnus-summary-goto-article
6622      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6623      nil (count-lines (point-min) (point))))
6624    ;; Go to next/previous group.
6625    (t
6626     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6627       (gnus-summary-jump-to-group gnus-newsgroup-name))
6628     (let ((cmd last-command-char)
6629           (point
6630            (save-excursion
6631              (set-buffer gnus-group-buffer)
6632              (point)))
6633           (group
6634            (if (eq gnus-keep-same-level 'best)
6635                (gnus-summary-best-group gnus-newsgroup-name)
6636              (gnus-summary-search-group backward gnus-keep-same-level))))
6637       ;; For some reason, the group window gets selected.  We change
6638       ;; it back.
6639       (select-window (get-buffer-window (current-buffer)))
6640       ;; Select next unread newsgroup automagically.
6641       (cond
6642        ((or (not gnus-auto-select-next)
6643             (not cmd))
6644         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6645        ((or (eq gnus-auto-select-next 'quietly)
6646             (and (eq gnus-auto-select-next 'slightly-quietly)
6647                  push)
6648             (and (eq gnus-auto-select-next 'almost-quietly)
6649                  (gnus-summary-last-article-p)))
6650         ;; Select quietly.
6651         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6652             (gnus-summary-exit)
6653           (gnus-message 7 "No more%s articles (%s)..."
6654                         (if unread " unread" "")
6655                         (if group (concat "selecting " group)
6656                           "exiting"))
6657           (gnus-summary-next-group nil group backward)))
6658        (t
6659         (when (gnus-key-press-event-p last-input-event)
6660           (gnus-summary-walk-group-buffer
6661            gnus-newsgroup-name cmd unread backward point))))))))
6662
6663 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6664   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6665                       (?\C-p (gnus-group-prev-unread-group 1))))
6666         (cursor-in-echo-area t)
6667         keve key group ended)
6668     (save-excursion
6669       (set-buffer gnus-group-buffer)
6670       (goto-char start)
6671       (setq group
6672             (if (eq gnus-keep-same-level 'best)
6673                 (gnus-summary-best-group gnus-newsgroup-name)
6674               (gnus-summary-search-group backward gnus-keep-same-level))))
6675     (while (not ended)
6676       (gnus-message
6677        5 "No more%s articles%s" (if unread " unread" "")
6678        (if (and group
6679                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6680            (format " (Type %s for %s [%s])"
6681                    (single-key-description cmd) group
6682                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6683          (format " (Type %s to exit %s)"
6684                  (single-key-description cmd)
6685                  gnus-newsgroup-name)))
6686       ;; Confirm auto selection.
6687       (setq key (car (setq keve (gnus-read-event-char))))
6688       (setq ended t)
6689       (cond
6690        ((assq key keystrokes)
6691         (let ((obuf (current-buffer)))
6692           (switch-to-buffer gnus-group-buffer)
6693           (when group
6694             (gnus-group-jump-to-group group))
6695           (eval (cadr (assq key keystrokes)))
6696           (setq group (gnus-group-group-name))
6697           (switch-to-buffer obuf))
6698         (setq ended nil))
6699        ((equal key cmd)
6700         (if (or (not group)
6701                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6702             (gnus-summary-exit)
6703           (gnus-summary-next-group nil group backward)))
6704        (t
6705         (push (cdr keve) unread-command-events))))))
6706
6707 (defun gnus-summary-next-unread-article ()
6708   "Select unread article after current one."
6709   (interactive)
6710   (gnus-summary-next-article
6711    (or (not (eq gnus-summary-goto-unread 'never))
6712        (gnus-summary-last-article-p (gnus-summary-article-number)))
6713    (and gnus-auto-select-same
6714         (gnus-summary-article-subject))))
6715
6716 (defun gnus-summary-prev-article (&optional unread subject)
6717   "Select the article after the current one.
6718 If UNREAD is non-nil, only unread articles are selected."
6719   (interactive "P")
6720   (gnus-summary-next-article unread subject t))
6721
6722 (defun gnus-summary-prev-unread-article ()
6723   "Select unread article before current one."
6724   (interactive)
6725   (gnus-summary-prev-article
6726    (or (not (eq gnus-summary-goto-unread 'never))
6727        (gnus-summary-first-article-p (gnus-summary-article-number)))
6728    (and gnus-auto-select-same
6729         (gnus-summary-article-subject))))
6730
6731 (defun gnus-summary-next-page (&optional lines circular)
6732   "Show next page of the selected article.
6733 If at the end of the current article, select the next article.
6734 LINES says how many lines should be scrolled up.
6735
6736 If CIRCULAR is non-nil, go to the start of the article instead of
6737 selecting the next article when reaching the end of the current
6738 article."
6739   (interactive "P")
6740   (setq gnus-summary-buffer (current-buffer))
6741   (gnus-set-global-variables)
6742   (let ((article (gnus-summary-article-number))
6743         (article-window (get-buffer-window gnus-article-buffer t))
6744         endp)
6745     ;; If the buffer is empty, we have no article.
6746     (unless article
6747       (error "No article to select"))
6748     (gnus-configure-windows 'article)
6749     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6750         (if (and (eq gnus-summary-goto-unread 'never)
6751                  (not (gnus-summary-last-article-p article)))
6752             (gnus-summary-next-article)
6753           (gnus-summary-next-unread-article))
6754       (if (or (null gnus-current-article)
6755               (null gnus-article-current)
6756               (/= article (cdr gnus-article-current))
6757               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6758           ;; Selected subject is different from current article's.
6759           (gnus-summary-display-article article)
6760         (when article-window
6761           (gnus-eval-in-buffer-window gnus-article-buffer
6762             (setq endp (gnus-article-next-page lines)))
6763           (when endp
6764             (cond (circular
6765                    (gnus-summary-beginning-of-article))
6766                   (lines
6767                    (gnus-message 3 "End of message"))
6768                   ((null lines)
6769                    (if (and (eq gnus-summary-goto-unread 'never)
6770                             (not (gnus-summary-last-article-p article)))
6771                        (gnus-summary-next-article)
6772                      (gnus-summary-next-unread-article))))))))
6773     (gnus-summary-recenter)
6774     (gnus-summary-position-point)))
6775
6776 (defun gnus-summary-prev-page (&optional lines move)
6777   "Show previous page of selected article.
6778 Argument LINES specifies lines to be scrolled down.
6779 If MOVE, move to the previous unread article if point is at
6780 the beginning of the buffer."
6781   (interactive "P")
6782   (let ((article (gnus-summary-article-number))
6783         (article-window (get-buffer-window gnus-article-buffer t))
6784         endp)
6785     (gnus-configure-windows 'article)
6786     (if (or (null gnus-current-article)
6787             (null gnus-article-current)
6788             (/= article (cdr gnus-article-current))
6789             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6790         ;; Selected subject is different from current article's.
6791         (gnus-summary-display-article article)
6792       (gnus-summary-recenter)
6793       (when article-window
6794         (gnus-eval-in-buffer-window gnus-article-buffer
6795           (setq endp (gnus-article-prev-page lines)))
6796         (when (and move endp)
6797           (cond (lines
6798                  (gnus-message 3 "Beginning of message"))
6799                 ((null lines)
6800                  (if (and (eq gnus-summary-goto-unread 'never)
6801                           (not (gnus-summary-first-article-p article)))
6802                      (gnus-summary-prev-article)
6803                    (gnus-summary-prev-unread-article))))))))
6804   (gnus-summary-position-point))
6805
6806 (defun gnus-summary-prev-page-or-article (&optional lines)
6807   "Show previous page of selected article.
6808 Argument LINES specifies lines to be scrolled down.
6809 If at the beginning of the article, go to the next article."
6810   (interactive "P")
6811   (gnus-summary-prev-page lines t))
6812
6813 (defun gnus-summary-scroll-up (lines)
6814   "Scroll up (or down) one line current article.
6815 Argument LINES specifies lines to be scrolled up (or down if negative)."
6816   (interactive "p")
6817   (gnus-configure-windows 'article)
6818   (gnus-summary-show-thread)
6819   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6820     (gnus-eval-in-buffer-window gnus-article-buffer
6821       (cond ((> lines 0)
6822              (when (gnus-article-next-page lines)
6823                (gnus-message 3 "End of message")))
6824             ((< lines 0)
6825              (gnus-article-prev-page (- lines))))))
6826   (gnus-summary-recenter)
6827   (gnus-summary-position-point))
6828
6829 (defun gnus-summary-scroll-down (lines)
6830   "Scroll down (or up) one line current article.
6831 Argument LINES specifies lines to be scrolled down (or up if negative)."
6832   (interactive "p")
6833   (gnus-summary-scroll-up (- lines)))
6834
6835 (defun gnus-summary-next-same-subject ()
6836   "Select next article which has the same subject as current one."
6837   (interactive)
6838   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6839
6840 (defun gnus-summary-prev-same-subject ()
6841   "Select previous article which has the same subject as current one."
6842   (interactive)
6843   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6844
6845 (defun gnus-summary-next-unread-same-subject ()
6846   "Select next unread article which has the same subject as current one."
6847   (interactive)
6848   (gnus-summary-next-article t (gnus-summary-article-subject)))
6849
6850 (defun gnus-summary-prev-unread-same-subject ()
6851   "Select previous unread article which has the same subject as current one."
6852   (interactive)
6853   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6854
6855 (defun gnus-summary-first-unread-article ()
6856   "Select the first unread article.
6857 Return nil if there are no unread articles."
6858   (interactive)
6859   (prog1
6860       (when (gnus-summary-first-subject t)
6861         (gnus-summary-show-thread)
6862         (gnus-summary-first-subject t)
6863         (gnus-summary-display-article (gnus-summary-article-number)))
6864     (gnus-summary-position-point)))
6865
6866 (defun gnus-summary-first-unread-subject ()
6867   "Place the point on the subject line of the first unread article.
6868 Return nil if there are no unread articles."
6869   (interactive)
6870   (prog1
6871       (when (gnus-summary-first-subject t)
6872         (gnus-summary-show-thread)
6873         (gnus-summary-first-subject t))
6874     (gnus-summary-position-point)))
6875
6876 (defun gnus-summary-first-unseen-subject ()
6877   "Place the point on the subject line of the first unseen article.
6878 Return nil if there are no unseen articles."
6879   (interactive)
6880   (prog1
6881       (when (gnus-summary-first-subject t t t)
6882         (gnus-summary-show-thread)
6883         (gnus-summary-first-subject t t t))
6884     (gnus-summary-position-point)))
6885
6886 (defun gnus-summary-first-unseen-or-unread-subject ()
6887   "Place the point on the subject line of the first unseen article.
6888 Return nil if there are no unseen articles."
6889   (interactive)
6890   (prog1
6891       (unless (when (gnus-summary-first-subject t t t)
6892                 (gnus-summary-show-thread)
6893                 (gnus-summary-first-subject t t t))
6894         (when (gnus-summary-first-subject t)
6895           (gnus-summary-show-thread)
6896           (gnus-summary-first-subject t)))
6897     (gnus-summary-position-point)))
6898
6899 (defun gnus-summary-first-article ()
6900   "Select the first article.
6901 Return nil if there are no articles."
6902   (interactive)
6903   (prog1
6904       (when (gnus-summary-first-subject)
6905         (gnus-summary-show-thread)
6906         (gnus-summary-first-subject)
6907         (gnus-summary-display-article (gnus-summary-article-number)))
6908     (gnus-summary-position-point)))
6909
6910 (defun gnus-summary-best-unread-article (&optional arg)
6911   "Select the unread article with the highest score.
6912 If given a prefix argument, select the next unread article that has a
6913 score higher than the default score."
6914   (interactive "P")
6915   (let ((article (if arg
6916                      (gnus-summary-better-unread-subject)
6917                    (gnus-summary-best-unread-subject))))
6918     (if article
6919         (gnus-summary-goto-article article)
6920       (error "No unread articles"))))
6921
6922 (defun gnus-summary-best-unread-subject ()
6923   "Select the unread subject with the highest score."
6924   (interactive)
6925   (let ((best -1000000)
6926         (data gnus-newsgroup-data)
6927         article score)
6928     (while data
6929       (and (gnus-data-unread-p (car data))
6930            (> (setq score
6931                     (gnus-summary-article-score (gnus-data-number (car data))))
6932               best)
6933            (setq best score
6934                  article (gnus-data-number (car data))))
6935       (setq data (cdr data)))
6936     (when article
6937       (gnus-summary-goto-subject article))
6938     (gnus-summary-position-point)
6939     article))
6940
6941 (defun gnus-summary-better-unread-subject ()
6942   "Select the first unread subject that has a score over the default score."
6943   (interactive)
6944   (let ((data gnus-newsgroup-data)
6945         article score)
6946     (while (and (setq article (gnus-data-number (car data)))
6947                 (or (gnus-data-read-p (car data))
6948                     (not (> (gnus-summary-article-score article)
6949                             gnus-summary-default-score))))
6950       (setq data (cdr data)))
6951     (when article
6952       (gnus-summary-goto-subject article))
6953     (gnus-summary-position-point)
6954     article))
6955
6956 (defun gnus-summary-last-subject ()
6957   "Go to the last displayed subject line in the group."
6958   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6959     (when article
6960       (gnus-summary-goto-subject article))))
6961
6962 (defun gnus-summary-goto-article (article &optional all-headers force)
6963   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6964 If ALL-HEADERS is non-nil, no header lines are hidden.
6965 If FORCE, go to the article even if it isn't displayed.  If FORCE
6966 is a number, it is the line the article is to be displayed on."
6967   (interactive
6968    (list
6969     (completing-read
6970      "Article number or Message-ID: "
6971      (mapcar (lambda (number) (list (int-to-string number)))
6972              gnus-newsgroup-limit))
6973     current-prefix-arg
6974     t))
6975   (prog1
6976       (if (and (stringp article)
6977                (string-match "@" article))
6978           (gnus-summary-refer-article article)
6979         (when (stringp article)
6980           (setq article (string-to-number article)))
6981         (if (gnus-summary-goto-subject article force)
6982             (gnus-summary-display-article article all-headers)
6983           (gnus-message 4 "Couldn't go to article %s" article) nil))
6984     (gnus-summary-position-point)))
6985
6986 (defun gnus-summary-goto-last-article ()
6987   "Go to the previously read article."
6988   (interactive)
6989   (prog1
6990       (when gnus-last-article
6991         (gnus-summary-goto-article gnus-last-article nil t))
6992     (gnus-summary-position-point)))
6993
6994 (defun gnus-summary-pop-article (number)
6995   "Pop one article off the history and go to the previous.
6996 NUMBER articles will be popped off."
6997   (interactive "p")
6998   (let (to)
6999     (setq gnus-newsgroup-history
7000           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7001     (if to
7002         (gnus-summary-goto-article (car to) nil t)
7003       (error "Article history empty")))
7004   (gnus-summary-position-point))
7005
7006 ;; Summary commands and functions for limiting the summary buffer.
7007
7008 (defun gnus-summary-limit-to-articles (n)
7009   "Limit the summary buffer to the next N articles.
7010 If not given a prefix, use the process marked articles instead."
7011   (interactive "P")
7012   (prog1
7013       (let ((articles (gnus-summary-work-articles n)))
7014         (setq gnus-newsgroup-processable nil)
7015         (gnus-summary-limit articles))
7016     (gnus-summary-position-point)))
7017
7018 (defun gnus-summary-pop-limit (&optional total)
7019   "Restore the previous limit.
7020 If given a prefix, remove all limits."
7021   (interactive "P")
7022   (when total
7023     (setq gnus-newsgroup-limits
7024           (list (mapcar (lambda (h) (mail-header-number h))
7025                         gnus-newsgroup-headers))))
7026   (unless gnus-newsgroup-limits
7027     (error "No limit to pop"))
7028   (prog1
7029       (gnus-summary-limit nil 'pop)
7030     (gnus-summary-position-point)))
7031
7032 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7033   "Limit the summary buffer to articles that have subjects that match a regexp.
7034 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7035   (interactive
7036    (list (read-string (if current-prefix-arg
7037                           "Exclude subject (regexp): "
7038                         "Limit to subject (regexp): "))
7039          nil current-prefix-arg))
7040   (unless header
7041     (setq header "subject"))
7042   (when (not (equal "" subject))
7043     (prog1
7044         (let ((articles (gnus-summary-find-matching
7045                          (or header "subject") subject 'all nil nil
7046                          not-matching)))
7047           (unless articles
7048             (error "Found no matches for \"%s\"" subject))
7049           (gnus-summary-limit articles))
7050       (gnus-summary-position-point))))
7051
7052 (defun gnus-summary-limit-to-author (from &optional not-matching)
7053   "Limit the summary buffer to articles that have authors that match a regexp.
7054 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7055   (interactive
7056    (list (read-string (if current-prefix-arg
7057                           "Exclude author (regexp): "
7058                         "Limit to author (regexp): "))
7059          current-prefix-arg))
7060   (gnus-summary-limit-to-subject from "from" not-matching))
7061
7062 (defun gnus-summary-limit-to-age (age &optional younger-p)
7063   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7064 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7065 articles that are younger than AGE days."
7066   (interactive
7067    (let ((younger current-prefix-arg)
7068          (days-got nil)
7069          days)
7070      (while (not days-got)
7071        (setq days (if younger
7072                       (read-string "Limit to articles within (in days): ")
7073                     (read-string "Limit to articles older than (in days): ")))
7074        (when (> (length days) 0)
7075          (setq days (read days)))
7076        (if (numberp days)
7077            (progn
7078              (setq days-got t)
7079              (if (< days 0)
7080                  (progn
7081                    (setq younger (not younger))
7082                    (setq days (* days -1)))))
7083          (message "Please enter a number.")
7084          (sleep-for 1)))
7085      (list days younger)))
7086   (prog1
7087       (let ((data gnus-newsgroup-data)
7088             (cutoff (days-to-time age))
7089             articles d date is-younger)
7090         (while (setq d (pop data))
7091           (when (and (vectorp (gnus-data-header d))
7092                      (setq date (mail-header-date (gnus-data-header d))))
7093             (setq is-younger (time-less-p
7094                               (time-since (condition-case ()
7095                                               (date-to-time date)
7096                                             (error '(0 0))))
7097                               cutoff))
7098             (when (if younger-p
7099                       is-younger
7100                     (not is-younger))
7101               (push (gnus-data-number d) articles))))
7102         (gnus-summary-limit (nreverse articles)))
7103     (gnus-summary-position-point)))
7104
7105 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7106   "Limit the summary buffer to articles that match an 'extra' header."
7107   (interactive
7108    (let ((header
7109           (intern
7110            (gnus-completing-read-with-default
7111             (symbol-name (car gnus-extra-headers))
7112             (if current-prefix-arg
7113                 "Exclude extra header:"
7114               "Limit extra header:")
7115             (mapcar (lambda (x)
7116                       (cons (symbol-name x) x))
7117                     gnus-extra-headers)
7118             nil
7119             t))))
7120      (list header
7121            (read-string (format "%s header %s (regexp): "
7122                                 (if current-prefix-arg "Exclude" "Limit to")
7123                                 header))
7124            current-prefix-arg)))
7125   (when (not (equal "" regexp))
7126     (prog1
7127         (let ((articles (gnus-summary-find-matching
7128                          (cons 'extra header) regexp 'all nil nil
7129                          not-matching)))
7130           (unless articles
7131             (error "Found no matches for \"%s\"" regexp))
7132           (gnus-summary-limit articles))
7133       (gnus-summary-position-point))))
7134
7135 (defun gnus-summary-limit-to-display-predicate ()
7136   "Limit the summary buffer to the predicated in the `display' group parameter."
7137   (interactive)
7138   (unless gnus-newsgroup-display
7139     (error "There is no `display' group parameter"))
7140   (let (articles)
7141     (dolist (number gnus-newsgroup-articles)
7142       (when (funcall gnus-newsgroup-display)
7143         (push number articles)))
7144     (gnus-summary-limit articles))
7145   (gnus-summary-position-point))
7146
7147 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7148 (make-obsolete
7149  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7150
7151 (defun gnus-summary-limit-to-unread (&optional all)
7152   "Limit the summary buffer to articles that are not marked as read.
7153 If ALL is non-nil, limit strictly to unread articles."
7154   (interactive "P")
7155   (if all
7156       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7157     (gnus-summary-limit-to-marks
7158      ;; Concat all the marks that say that an article is read and have
7159      ;; those removed.
7160      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7161            gnus-killed-mark gnus-kill-file-mark
7162            gnus-low-score-mark gnus-expirable-mark
7163            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7164            gnus-duplicate-mark gnus-souped-mark)
7165      'reverse)))
7166
7167 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7168 (make-obsolete 'gnus-summary-delete-marked-with
7169                'gnus-summary-limit-exlude-marks)
7170
7171 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7172   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7173 If REVERSE, limit the summary buffer to articles that are marked
7174 with MARKS.  MARKS can either be a string of marks or a list of marks.
7175 Returns how many articles were removed."
7176   (interactive "sMarks: ")
7177   (gnus-summary-limit-to-marks marks t))
7178
7179 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7180   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7181 If REVERSE (the prefix), limit the summary buffer to articles that are
7182 not marked with MARKS.  MARKS can either be a string of marks or a
7183 list of marks.
7184 Returns how many articles were removed."
7185   (interactive "sMarks: \nP")
7186   (prog1
7187       (let ((data gnus-newsgroup-data)
7188             (marks (if (listp marks) marks
7189                      (append marks nil))) ; Transform to list.
7190             articles)
7191         (while data
7192           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7193                   (memq (gnus-data-mark (car data)) marks))
7194             (push (gnus-data-number (car data)) articles))
7195           (setq data (cdr data)))
7196         (gnus-summary-limit articles))
7197     (gnus-summary-position-point)))
7198
7199 (defun gnus-summary-limit-to-score (score)
7200   "Limit to articles with score at or above SCORE."
7201   (interactive "NLimit to articles with score of at least: ")
7202   (let ((data gnus-newsgroup-data)
7203         articles)
7204     (while data
7205       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7206                 score)
7207         (push (gnus-data-number (car data)) articles))
7208       (setq data (cdr data)))
7209     (prog1
7210         (gnus-summary-limit articles)
7211       (gnus-summary-position-point))))
7212
7213 (defun gnus-summary-limit-include-thread (id)
7214   "Display all the hidden articles that is in the thread with ID in it.
7215 When called interactively, ID is the Message-ID of the current
7216 article."
7217   (interactive (list (mail-header-id (gnus-summary-article-header))))
7218   (let ((articles (gnus-articles-in-thread
7219                    (gnus-id-to-thread (gnus-root-id id)))))
7220     (prog1
7221         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7222       (gnus-summary-limit-include-matching-articles
7223        "subject"
7224        (regexp-quote (gnus-simplify-subject-re
7225                       (mail-header-subject (gnus-id-to-header id)))))
7226       (gnus-summary-position-point))))
7227
7228 (defun gnus-summary-limit-include-matching-articles (header regexp)
7229   "Display all the hidden articles that have HEADERs that match REGEXP."
7230   (interactive (list (read-string "Match on header: ")
7231                      (read-string "Regexp: ")))
7232   (let ((articles (gnus-find-matching-articles header regexp)))
7233     (prog1
7234         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7235       (gnus-summary-position-point))))
7236
7237 (defun gnus-summary-limit-include-dormant ()
7238   "Display all the hidden articles that are marked as dormant.
7239 Note that this command only works on a subset of the articles currently
7240 fetched for this group."
7241   (interactive)
7242   (unless gnus-newsgroup-dormant
7243     (error "There are no dormant articles in this group"))
7244   (prog1
7245       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7246     (gnus-summary-position-point)))
7247
7248 (defun gnus-summary-limit-exclude-dormant ()
7249   "Hide all dormant articles."
7250   (interactive)
7251   (prog1
7252       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7253     (gnus-summary-position-point)))
7254
7255 (defun gnus-summary-limit-exclude-childless-dormant ()
7256   "Hide all dormant articles that have no children."
7257   (interactive)
7258   (let ((data (gnus-data-list t))
7259         articles d children)
7260     ;; Find all articles that are either not dormant or have
7261     ;; children.
7262     (while (setq d (pop data))
7263       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7264                 (and (setq children
7265                            (gnus-article-children (gnus-data-number d)))
7266                      (let (found)
7267                        (while children
7268                          (when (memq (car children) articles)
7269                            (setq children nil
7270                                  found t))
7271                          (pop children))
7272                        found)))
7273         (push (gnus-data-number d) articles)))
7274     ;; Do the limiting.
7275     (prog1
7276         (gnus-summary-limit articles)
7277       (gnus-summary-position-point))))
7278
7279 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7280   "Mark all unread excluded articles as read.
7281 If ALL, mark even excluded ticked and dormants as read."
7282   (interactive "P")
7283   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7284   (let ((articles (gnus-sorted-ndifference
7285                    (sort
7286                     (mapcar (lambda (h) (mail-header-number h))
7287                             gnus-newsgroup-headers)
7288                     '<)
7289                    gnus-newsgroup-limit))
7290         article)
7291     (setq gnus-newsgroup-unreads
7292           (gnus-sorted-intersection gnus-newsgroup-unreads
7293                                     gnus-newsgroup-limit))
7294     (if all
7295         (setq gnus-newsgroup-dormant nil
7296               gnus-newsgroup-marked nil
7297               gnus-newsgroup-reads
7298               (nconc
7299                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7300                gnus-newsgroup-reads))
7301       (while (setq article (pop articles))
7302         (unless (or (memq article gnus-newsgroup-dormant)
7303                     (memq article gnus-newsgroup-marked))
7304           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7305
7306 (defun gnus-summary-limit (articles &optional pop)
7307   (if pop
7308       ;; We pop the previous limit off the stack and use that.
7309       (setq articles (car gnus-newsgroup-limits)
7310             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7311     ;; We use the new limit, so we push the old limit on the stack.
7312     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7313   ;; Set the limit.
7314   (setq gnus-newsgroup-limit articles)
7315   (let ((total (length gnus-newsgroup-data))
7316         (data (gnus-data-find-list (gnus-summary-article-number)))
7317         (gnus-summary-mark-below nil)   ; Inhibit this.
7318         found)
7319     ;; This will do all the work of generating the new summary buffer
7320     ;; according to the new limit.
7321     (gnus-summary-prepare)
7322     ;; Hide any threads, possibly.
7323     (gnus-summary-maybe-hide-threads)
7324     ;; Try to return to the article you were at, or one in the
7325     ;; neighborhood.
7326     (when data
7327       ;; We try to find some article after the current one.
7328       (while data
7329         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7330           (setq data nil
7331                 found t))
7332         (setq data (cdr data))))
7333     (unless found
7334       ;; If there is no data, that means that we were after the last
7335       ;; article.  The same goes when we can't find any articles
7336       ;; after the current one.
7337       (goto-char (point-max))
7338       (gnus-summary-find-prev))
7339     (gnus-set-mode-line 'summary)
7340     ;; We return how many articles were removed from the summary
7341     ;; buffer as a result of the new limit.
7342     (- total (length gnus-newsgroup-data))))
7343
7344 (defsubst gnus-invisible-cut-children (threads)
7345   (let ((num 0))
7346     (while threads
7347       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7348         (incf num))
7349       (pop threads))
7350     (< num 2)))
7351
7352 (defsubst gnus-cut-thread (thread)
7353   "Go forwards in the thread until we find an article that we want to display."
7354   (when (or (eq gnus-fetch-old-headers 'some)
7355             (eq gnus-fetch-old-headers 'invisible)
7356             (numberp gnus-fetch-old-headers)
7357             (eq gnus-build-sparse-threads 'some)
7358             (eq gnus-build-sparse-threads 'more))
7359     ;; Deal with old-fetched headers and sparse threads.
7360     (while (and
7361             thread
7362             (or
7363              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7364              (gnus-summary-article-ancient-p
7365               (mail-header-number (car thread))))
7366             (if (or (<= (length (cdr thread)) 1)
7367                     (eq gnus-fetch-old-headers 'invisible))
7368                 (setq gnus-newsgroup-limit
7369                       (delq (mail-header-number (car thread))
7370                             gnus-newsgroup-limit)
7371                       thread (cadr thread))
7372               (when (gnus-invisible-cut-children (cdr thread))
7373                 (let ((th (cdr thread)))
7374                   (while th
7375                     (if (memq (mail-header-number (caar th))
7376                               gnus-newsgroup-limit)
7377                         (setq thread (car th)
7378                               th nil)
7379                       (setq th (cdr th))))))))))
7380   thread)
7381
7382 (defun gnus-cut-threads (threads)
7383   "Cut off all uninteresting articles from the beginning of threads."
7384   (when (or (eq gnus-fetch-old-headers 'some)
7385             (eq gnus-fetch-old-headers 'invisible)
7386             (numberp gnus-fetch-old-headers)
7387             (eq gnus-build-sparse-threads 'some)
7388             (eq gnus-build-sparse-threads 'more))
7389     (let ((th threads))
7390       (while th
7391         (setcar th (gnus-cut-thread (car th)))
7392         (setq th (cdr th)))))
7393   ;; Remove nixed out threads.
7394   (delq nil threads))
7395
7396 (defun gnus-summary-initial-limit (&optional show-if-empty)
7397   "Figure out what the initial limit is supposed to be on group entry.
7398 This entails weeding out unwanted dormants, low-scored articles,
7399 fetch-old-headers verbiage, and so on."
7400   ;; Most groups have nothing to remove.
7401   (if (or gnus-inhibit-limiting
7402           (and (null gnus-newsgroup-dormant)
7403                (eq gnus-newsgroup-display 'gnus-not-ignore)
7404                (not (eq gnus-fetch-old-headers 'some))
7405                (not (numberp gnus-fetch-old-headers))
7406                (not (eq gnus-fetch-old-headers 'invisible))
7407                (null gnus-summary-expunge-below)
7408                (not (eq gnus-build-sparse-threads 'some))
7409                (not (eq gnus-build-sparse-threads 'more))
7410                (null gnus-thread-expunge-below)
7411                (not gnus-use-nocem)))
7412       ()                                ; Do nothing.
7413     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7414     (setq gnus-newsgroup-limit nil)
7415     (mapatoms
7416      (lambda (node)
7417        (unless (car (symbol-value node))
7418          ;; These threads have no parents -- they are roots.
7419          (let ((nodes (cdr (symbol-value node)))
7420                thread)
7421            (while nodes
7422              (if (and gnus-thread-expunge-below
7423                       (< (gnus-thread-total-score (car nodes))
7424                          gnus-thread-expunge-below))
7425                  (gnus-expunge-thread (pop nodes))
7426                (setq thread (pop nodes))
7427                (gnus-summary-limit-children thread))))))
7428      gnus-newsgroup-dependencies)
7429     ;; If this limitation resulted in an empty group, we might
7430     ;; pop the previous limit and use it instead.
7431     (when (and (not gnus-newsgroup-limit)
7432                show-if-empty)
7433       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7434     gnus-newsgroup-limit))
7435
7436 (defun gnus-summary-limit-children (thread)
7437   "Return 1 if this subthread is visible and 0 if it is not."
7438   ;; First we get the number of visible children to this thread.  This
7439   ;; is done by recursing down the thread using this function, so this
7440   ;; will really go down to a leaf article first, before slowly
7441   ;; working its way up towards the root.
7442   (when thread
7443     (let ((children
7444            (if (cdr thread)
7445                (apply '+ (mapcar 'gnus-summary-limit-children
7446                                  (cdr thread)))
7447              0))
7448           (number (mail-header-number (car thread)))
7449           score)
7450       (if (and
7451            (not (memq number gnus-newsgroup-marked))
7452            (or
7453             ;; If this article is dormant and has absolutely no visible
7454             ;; children, then this article isn't visible.
7455             (and (memq number gnus-newsgroup-dormant)
7456                  (zerop children))
7457             ;; If this is "fetch-old-headered" and there is no
7458             ;; visible children, then we don't want this article.
7459             (and (or (eq gnus-fetch-old-headers 'some)
7460                      (numberp gnus-fetch-old-headers))
7461                  (gnus-summary-article-ancient-p number)
7462                  (zerop children))
7463             ;; If this is "fetch-old-headered" and `invisible', then
7464             ;; we don't want this article.
7465             (and (eq gnus-fetch-old-headers 'invisible)
7466                  (gnus-summary-article-ancient-p number))
7467             ;; If this is a sparsely inserted article with no children,
7468             ;; we don't want it.
7469             (and (eq gnus-build-sparse-threads 'some)
7470                  (gnus-summary-article-sparse-p number)
7471                  (zerop children))
7472             ;; If we use expunging, and this article is really
7473             ;; low-scored, then we don't want this article.
7474             (when (and gnus-summary-expunge-below
7475                        (< (setq score
7476                                 (or (cdr (assq number gnus-newsgroup-scored))
7477                                     gnus-summary-default-score))
7478                           gnus-summary-expunge-below))
7479               ;; We increase the expunge-tally here, but that has
7480               ;; nothing to do with the limits, really.
7481               (incf gnus-newsgroup-expunged-tally)
7482               ;; We also mark as read here, if that's wanted.
7483               (when (and gnus-summary-mark-below
7484                          (< score gnus-summary-mark-below))
7485                 (setq gnus-newsgroup-unreads
7486                       (delq number gnus-newsgroup-unreads))
7487                 (if gnus-newsgroup-auto-expire
7488                     (push number gnus-newsgroup-expirable)
7489                   (push (cons number gnus-low-score-mark)
7490                         gnus-newsgroup-reads)))
7491               t)
7492             ;; Do the `display' group parameter.
7493             (and gnus-newsgroup-display
7494                  (not (funcall gnus-newsgroup-display)))
7495             ;; Check NoCeM things.
7496             (if (and gnus-use-nocem
7497                      (gnus-nocem-unwanted-article-p
7498                       (mail-header-id (car thread))))
7499                 (progn
7500                   (setq gnus-newsgroup-unreads
7501                         (delq number gnus-newsgroup-unreads))
7502                   t))))
7503           ;; Nope, invisible article.
7504           0
7505         ;; Ok, this article is to be visible, so we add it to the limit
7506         ;; and return 1.
7507         (push number gnus-newsgroup-limit)
7508         1))))
7509
7510 (defun gnus-expunge-thread (thread)
7511   "Mark all articles in THREAD as read."
7512   (let* ((number (mail-header-number (car thread))))
7513     (incf gnus-newsgroup-expunged-tally)
7514     ;; We also mark as read here, if that's wanted.
7515     (setq gnus-newsgroup-unreads
7516           (delq number gnus-newsgroup-unreads))
7517     (if gnus-newsgroup-auto-expire
7518         (push number gnus-newsgroup-expirable)
7519       (push (cons number gnus-low-score-mark)
7520             gnus-newsgroup-reads)))
7521   ;; Go recursively through all subthreads.
7522   (mapcar 'gnus-expunge-thread (cdr thread)))
7523
7524 ;; Summary article oriented commands
7525
7526 (defun gnus-summary-refer-parent-article (n)
7527   "Refer parent article N times.
7528 If N is negative, go to ancestor -N instead.
7529 The difference between N and the number of articles fetched is returned."
7530   (interactive "p")
7531   (let ((skip 1)
7532         error header ref)
7533     (when (not (natnump n))
7534       (setq skip (abs n)
7535             n 1))
7536     (while (and (> n 0)
7537                 (not error))
7538       (setq header (gnus-summary-article-header))
7539       (if (and (eq (mail-header-number header)
7540                    (cdr gnus-article-current))
7541                (equal gnus-newsgroup-name
7542                       (car gnus-article-current)))
7543           ;; If we try to find the parent of the currently
7544           ;; displayed article, then we take a look at the actual
7545           ;; References header, since this is slightly more
7546           ;; reliable than the References field we got from the
7547           ;; server.
7548           (save-excursion
7549             (set-buffer gnus-original-article-buffer)
7550             (nnheader-narrow-to-headers)
7551             (unless (setq ref (message-fetch-field "references"))
7552               (setq ref (message-fetch-field "in-reply-to")))
7553             (widen))
7554         (setq ref
7555               ;; It's not the current article, so we take a bet on
7556               ;; the value we got from the server.
7557               (mail-header-references header)))
7558       (if (and ref
7559                (not (equal ref "")))
7560           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7561             (gnus-message 1 "Couldn't find parent"))
7562         (gnus-message 1 "No references in article %d"
7563                       (gnus-summary-article-number))
7564         (setq error t))
7565       (decf n))
7566     (gnus-summary-position-point)
7567     n))
7568
7569 (defun gnus-summary-refer-references ()
7570   "Fetch all articles mentioned in the References header.
7571 Return the number of articles fetched."
7572   (interactive)
7573   (let ((ref (mail-header-references (gnus-summary-article-header)))
7574         (current (gnus-summary-article-number))
7575         (n 0))
7576     (if (or (not ref)
7577             (equal ref ""))
7578         (error "No References in the current article")
7579       ;; For each Message-ID in the References header...
7580       (while (string-match "<[^>]*>" ref)
7581         (incf n)
7582         ;; ... fetch that article.
7583         (gnus-summary-refer-article
7584          (prog1 (match-string 0 ref)
7585            (setq ref (substring ref (match-end 0))))))
7586       (gnus-summary-goto-subject current)
7587       (gnus-summary-position-point)
7588       n)))
7589
7590 (defun gnus-summary-refer-thread (&optional limit)
7591   "Fetch all articles in the current thread.
7592 If LIMIT (the numerical prefix), fetch that many old headers instead
7593 of what's specified by the `gnus-refer-thread-limit' variable."
7594   (interactive "P")
7595   (let ((id (mail-header-id (gnus-summary-article-header)))
7596         (limit (if limit (prefix-numeric-value limit)
7597                  gnus-refer-thread-limit)))
7598     ;; We want to fetch LIMIT *old* headers, but we also have to
7599     ;; re-fetch all the headers in the current buffer, because many of
7600     ;; them may be undisplayed.  So we adjust LIMIT.
7601     (when (numberp limit)
7602       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7603     (unless (eq gnus-fetch-old-headers 'invisible)
7604       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7605       ;; Retrieve the headers and read them in.
7606       (if (eq (gnus-retrieve-headers
7607                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7608               'nov)
7609           (gnus-build-all-threads)
7610         (error "Can't fetch thread from backends that don't support NOV"))
7611       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7612     (gnus-summary-limit-include-thread id)))
7613
7614 (defun gnus-summary-refer-article (message-id)
7615   "Fetch an article specified by MESSAGE-ID."
7616   (interactive "sMessage-ID: ")
7617   (when (and (stringp message-id)
7618              (not (zerop (length message-id))))
7619     ;; Construct the correct Message-ID if necessary.
7620     ;; Suggested by tale@pawl.rpi.edu.
7621     (unless (string-match "^<" message-id)
7622       (setq message-id (concat "<" message-id)))
7623     (unless (string-match ">$" message-id)
7624       (setq message-id (concat message-id ">")))
7625     (let* ((header (gnus-id-to-header message-id))
7626            (sparse (and header
7627                         (gnus-summary-article-sparse-p
7628                          (mail-header-number header))
7629                         (memq (mail-header-number header)
7630                               gnus-newsgroup-limit)))
7631            number)
7632       (cond
7633        ;; If the article is present in the buffer we just go to it.
7634        ((and header
7635              (or (not (gnus-summary-article-sparse-p
7636                        (mail-header-number header)))
7637                  sparse))
7638         (prog1
7639             (gnus-summary-goto-article
7640              (mail-header-number header) nil t)
7641           (when sparse
7642             (gnus-summary-update-article (mail-header-number header)))))
7643        (t
7644         ;; We fetch the article.
7645         (catch 'found
7646           (dolist (gnus-override-method (gnus-refer-article-methods))
7647             (gnus-check-server gnus-override-method)
7648             ;; Fetch the header, and display the article.
7649             (when (setq number (gnus-summary-insert-subject message-id))
7650               (gnus-summary-select-article nil nil nil number)
7651               (throw 'found t)))
7652           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7653
7654 (defun gnus-refer-article-methods ()
7655   "Return a list of referrable methods."
7656   (cond
7657    ;; No method, so we default to current and native.
7658    ((null gnus-refer-article-method)
7659     (list gnus-current-select-method gnus-select-method))
7660    ;; Current.
7661    ((eq 'current gnus-refer-article-method)
7662     (list gnus-current-select-method))
7663    ;; List of select methods.
7664    ((not (and (symbolp (car gnus-refer-article-method))
7665               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7666     (let (out)
7667       (dolist (method gnus-refer-article-method)
7668         (push (if (eq 'current method)
7669                   gnus-current-select-method
7670                 method)
7671               out))
7672       (nreverse out)))
7673    ;; One single select method.
7674    (t
7675     (list gnus-refer-article-method))))
7676
7677 (defun gnus-summary-edit-parameters ()
7678   "Edit the group parameters of the current group."
7679   (interactive)
7680   (gnus-group-edit-group gnus-newsgroup-name 'params))
7681
7682 (defun gnus-summary-customize-parameters ()
7683   "Customize the group parameters of the current group."
7684   (interactive)
7685   (gnus-group-customize gnus-newsgroup-name))
7686
7687 (defun gnus-summary-enter-digest-group (&optional force)
7688   "Enter an nndoc group based on the current article.
7689 If FORCE, force a digest interpretation.  If not, try
7690 to guess what the document format is."
7691   (interactive "P")
7692   (let ((conf gnus-current-window-configuration))
7693     (save-excursion
7694       (gnus-summary-select-article))
7695     (setq gnus-current-window-configuration conf)
7696     (let* ((name (format "%s-%d"
7697                          (gnus-group-prefixed-name
7698                           gnus-newsgroup-name (list 'nndoc ""))
7699                          (save-excursion
7700                            (set-buffer gnus-summary-buffer)
7701                            gnus-current-article)))
7702            (ogroup gnus-newsgroup-name)
7703            (params (append (gnus-info-params (gnus-get-info ogroup))
7704                            (list (cons 'to-group ogroup))
7705                            (list (cons 'save-article-group ogroup))))
7706            (case-fold-search t)
7707            (buf (current-buffer))
7708            dig to-address)
7709       (save-excursion
7710         (set-buffer gnus-original-article-buffer)
7711         ;; Have the digest group inherit the main mail address of
7712         ;; the parent article.
7713         (when (setq to-address (or (message-fetch-field "reply-to")
7714                                    (message-fetch-field "from")))
7715           (setq params (append
7716                         (list (cons 'to-address
7717                                     (funcall gnus-decode-encoded-word-function
7718                                              to-address))))))
7719         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7720         (insert-buffer-substring gnus-original-article-buffer)
7721         ;; Remove lines that may lead nndoc to misinterpret the
7722         ;; document type.
7723         (narrow-to-region
7724          (goto-char (point-min))
7725          (or (search-forward "\n\n" nil t) (point)))
7726         (goto-char (point-min))
7727         (delete-matching-lines "^Path:\\|^From ")
7728         (widen))
7729       (unwind-protect
7730           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7731                     (gnus-newsgroup-ephemeral-ignored-charsets
7732                      gnus-newsgroup-ignored-charsets))
7733                 (gnus-group-read-ephemeral-group
7734                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7735                               (nndoc-article-type
7736                                ,(if force 'mbox 'guess)))
7737                  t nil nil nil
7738                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
7739                                                         "ADAPT")))))
7740               ;; Make all postings to this group go to the parent group.
7741               (nconc (gnus-info-params (gnus-get-info name))
7742                      params)
7743             ;; Couldn't select this doc group.
7744             (switch-to-buffer buf)
7745             (gnus-set-global-variables)
7746             (gnus-configure-windows 'summary)
7747             (gnus-message 3 "Article couldn't be entered?"))
7748         (kill-buffer dig)))))
7749
7750 (defun gnus-summary-read-document (n)
7751   "Open a new group based on the current article(s).
7752 This will allow you to read digests and other similar
7753 documents as newsgroups.
7754 Obeys the standard process/prefix convention."
7755   (interactive "P")
7756   (let* ((articles (gnus-summary-work-articles n))
7757          (ogroup gnus-newsgroup-name)
7758          (params (append (gnus-info-params (gnus-get-info ogroup))
7759                          (list (cons 'to-group ogroup))))
7760          article group egroup groups vgroup)
7761     (while (setq article (pop articles))
7762       (setq group (format "%s-%d" gnus-newsgroup-name article))
7763       (gnus-summary-remove-process-mark article)
7764       (when (gnus-summary-display-article article)
7765         (save-excursion
7766           (with-temp-buffer
7767             (insert-buffer-substring gnus-original-article-buffer)
7768             ;; Remove some headers that may lead nndoc to make
7769             ;; the wrong guess.
7770             (message-narrow-to-head)
7771             (goto-char (point-min))
7772             (delete-matching-lines "^\\(Path\\):\\|^From ")
7773             (widen)
7774             (if (setq egroup
7775                       (gnus-group-read-ephemeral-group
7776                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7777                                      (nndoc-article-type guess))
7778                        t nil t))
7779                 (progn
7780             ;; Make all postings to this group go to the parent group.
7781                   (nconc (gnus-info-params (gnus-get-info egroup))
7782                          params)
7783                   (push egroup groups))
7784               ;; Couldn't select this doc group.
7785               (gnus-error 3 "Article couldn't be entered"))))))
7786     ;; Now we have selected all the documents.
7787     (cond
7788      ((not groups)
7789       (error "None of the articles could be interpreted as documents"))
7790      ((gnus-group-read-ephemeral-group
7791        (setq vgroup (format
7792                      "nnvirtual:%s-%s" gnus-newsgroup-name
7793                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7794        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7795        t
7796        (cons (current-buffer) 'summary)))
7797      (t
7798       (error "Couldn't select virtual nndoc group")))))
7799
7800 (defun gnus-summary-isearch-article (&optional regexp-p)
7801   "Do incremental search forward on the current article.
7802 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7803   (interactive "P")
7804   (gnus-summary-select-article)
7805   (gnus-configure-windows 'article)
7806   (gnus-eval-in-buffer-window gnus-article-buffer
7807     (save-restriction
7808       (widen)
7809       (isearch-forward regexp-p))))
7810
7811 (defun gnus-summary-search-article-forward (regexp &optional backward)
7812   "Search for an article containing REGEXP forward.
7813 If BACKWARD, search backward instead."
7814   (interactive
7815    (list (read-string
7816           (format "Search article %s (regexp%s): "
7817                   (if current-prefix-arg "backward" "forward")
7818                   (if gnus-last-search-regexp
7819                       (concat ", default " gnus-last-search-regexp)
7820                     "")))
7821          current-prefix-arg))
7822   (if (string-equal regexp "")
7823       (setq regexp (or gnus-last-search-regexp ""))
7824     (setq gnus-last-search-regexp regexp)
7825     (setq gnus-article-before-search gnus-current-article))
7826   ;; Intentionally set gnus-last-article.
7827   (setq gnus-last-article gnus-article-before-search)
7828   (let ((gnus-last-article gnus-last-article))
7829     (if (gnus-summary-search-article regexp backward)
7830         (gnus-summary-show-thread)
7831       (error "Search failed: \"%s\"" regexp))))
7832
7833 (defun gnus-summary-search-article-backward (regexp)
7834   "Search for an article containing REGEXP backward."
7835   (interactive
7836    (list (read-string
7837           (format "Search article backward (regexp%s): "
7838                   (if gnus-last-search-regexp
7839                       (concat ", default " gnus-last-search-regexp)
7840                     "")))))
7841   (gnus-summary-search-article-forward regexp 'backward))
7842
7843 (defun gnus-summary-search-article (regexp &optional backward)
7844   "Search for an article containing REGEXP.
7845 Optional argument BACKWARD means do search for backward.
7846 `gnus-select-article-hook' is not called during the search."
7847   ;; We have to require this here to make sure that the following
7848   ;; dynamic binding isn't shadowed by autoloading.
7849   (require 'gnus-async)
7850   (require 'gnus-art)
7851   (let ((gnus-select-article-hook nil)  ;Disable hook.
7852         (gnus-article-prepare-hook nil)
7853         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7854         (gnus-use-article-prefetch nil)
7855         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7856         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7857         (sum (current-buffer))
7858         (gnus-display-mime-function nil)
7859         (found nil)
7860         point)
7861     (gnus-save-hidden-threads
7862       (gnus-summary-select-article)
7863       (set-buffer gnus-article-buffer)
7864       (goto-char (window-point (get-buffer-window (current-buffer))))
7865       (when backward
7866         (forward-line -1))
7867       (while (not found)
7868         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7869         (if (if backward
7870                 (re-search-backward regexp nil t)
7871               (re-search-forward regexp nil t))
7872             ;; We found the regexp.
7873             (progn
7874               (setq found 'found)
7875               (beginning-of-line)
7876               (set-window-start
7877                (get-buffer-window (current-buffer))
7878                (point))
7879               (forward-line 1)
7880               (set-window-point
7881                (get-buffer-window (current-buffer))
7882                (point))
7883               (set-buffer sum)
7884               (setq point (point)))
7885           ;; We didn't find it, so we go to the next article.
7886           (set-buffer sum)
7887           (setq found 'not)
7888           (while (eq found 'not)
7889             (if (not (if backward (gnus-summary-find-prev)
7890                        (gnus-summary-find-next)))
7891                 ;; No more articles.
7892                 (setq found t)
7893               ;; Select the next article and adjust point.
7894               (unless (gnus-summary-article-sparse-p
7895                        (gnus-summary-article-number))
7896                 (setq found nil)
7897                 (gnus-summary-select-article)
7898                 (set-buffer gnus-article-buffer)
7899                 (widen)
7900                 (goto-char (if backward (point-max) (point-min))))))))
7901       (gnus-message 7 ""))
7902     ;; Return whether we found the regexp.
7903     (when (eq found 'found)
7904       (goto-char point)
7905       (gnus-summary-show-thread)
7906       (gnus-summary-goto-subject gnus-current-article)
7907       (gnus-summary-position-point)
7908       t)))
7909
7910 (defun gnus-find-matching-articles (header regexp)
7911   "Return a list of all articles that match REGEXP on HEADER.
7912 This search includes all articles in the current group that Gnus has
7913 fetched headers for, whether they are displayed or not."
7914   (let ((articles nil)
7915         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7916         (case-fold-search t))
7917     (dolist (header gnus-newsgroup-headers)
7918       (when (string-match regexp (funcall func header))
7919         (push (mail-header-number header) articles)))
7920     (nreverse articles)))
7921
7922 (defun gnus-summary-find-matching (header regexp &optional backward unread
7923                                           not-case-fold not-matching)
7924   "Return a list of all articles that match REGEXP on HEADER.
7925 The search stars on the current article and goes forwards unless
7926 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7927 If UNREAD is non-nil, only unread articles will
7928 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7929 in the comparisons. If NOT-MATCHING, return a list of all articles that
7930 not match REGEXP on HEADER."
7931   (let ((case-fold-search (not not-case-fold))
7932         articles d func)
7933     (if (consp header)
7934         (if (eq (car header) 'extra)
7935             (setq func
7936                   `(lambda (h)
7937                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7938                          "")))
7939           (error "%s is an invalid header" header))
7940       (unless (fboundp (intern (concat "mail-header-" header)))
7941         (error "%s is not a valid header" header))
7942       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7943     (dolist (d (if (eq backward 'all)
7944                    gnus-newsgroup-data
7945                  (gnus-data-find-list
7946                   (gnus-summary-article-number)
7947                   (gnus-data-list backward))))
7948       (when (and (or (not unread)       ; We want all articles...
7949                      (gnus-data-unread-p d)) ; Or just unreads.
7950                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7951                  (if not-matching
7952                      (not (string-match
7953                            regexp
7954                            (funcall func (gnus-data-header d))))
7955                    (string-match regexp
7956                                  (funcall func (gnus-data-header d)))))
7957         (push (gnus-data-number d) articles))) ; Success!
7958     (nreverse articles)))
7959
7960 (defun gnus-summary-execute-command (header regexp command &optional backward)
7961   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7962 If HEADER is an empty string (or nil), the match is done on the entire
7963 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7964   (interactive
7965    (list (let ((completion-ignore-case t))
7966            (completing-read
7967             "Header name: "
7968             (mapcar (lambda (header) (list (format "%s" header)))
7969                     (append
7970                      '("Number" "Subject" "From" "Lines" "Date"
7971                        "Message-ID" "Xref" "References" "Body")
7972                      gnus-extra-headers))
7973             nil 'require-match))
7974          (read-string "Regexp: ")
7975          (read-key-sequence "Command: ")
7976          current-prefix-arg))
7977   (when (equal header "Body")
7978     (setq header ""))
7979   ;; Hidden thread subtrees must be searched as well.
7980   (gnus-summary-show-all-threads)
7981   ;; We don't want to change current point nor window configuration.
7982   (save-excursion
7983     (save-window-excursion
7984       (gnus-message 6 "Executing %s..." (key-description command))
7985 ;; We'd like to execute COMMAND interactively so as to give arguments.
7986       (gnus-execute header regexp
7987                     `(call-interactively ',(key-binding command))
7988                     backward)
7989       (gnus-message 6 "Executing %s...done" (key-description command)))))
7990
7991 (defun gnus-summary-beginning-of-article ()
7992   "Scroll the article back to the beginning."
7993   (interactive)
7994   (gnus-summary-select-article)
7995   (gnus-configure-windows 'article)
7996   (gnus-eval-in-buffer-window gnus-article-buffer
7997     (widen)
7998     (goto-char (point-min))
7999     (when gnus-page-broken
8000       (gnus-narrow-to-page))))
8001
8002 (defun gnus-summary-end-of-article ()
8003   "Scroll to the end of the article."
8004   (interactive)
8005   (gnus-summary-select-article)
8006   (gnus-configure-windows 'article)
8007   (gnus-eval-in-buffer-window gnus-article-buffer
8008     (widen)
8009     (goto-char (point-max))
8010     (recenter -3)
8011     (when gnus-page-broken
8012       (gnus-narrow-to-page))))
8013
8014 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8015   "Truncate to LEN and quote all \"(\"'s in STRING."
8016   (gnus-replace-in-string (if (and len (> (length string) len))
8017                               (substring string 0 len)
8018                             string)
8019                           "[()]" "\\\\\\&"))
8020
8021 (defun gnus-summary-print-article (&optional filename n)
8022   "Generate and print a PostScript image of the N next (mail) articles.
8023
8024 If N is negative, print the N previous articles.  If N is nil and articles
8025 have been marked with the process mark, print these instead.
8026
8027 If the optional first argument FILENAME is nil, send the image to the
8028 printer.  If FILENAME is a string, save the PostScript image in a file with
8029 that name.  If FILENAME is a number, prompt the user for the name of the file
8030 to save in."
8031   (interactive (list (ps-print-preprint current-prefix-arg)))
8032   (dolist (article (gnus-summary-work-articles n))
8033     (gnus-summary-select-article nil nil 'pseudo article)
8034     (gnus-eval-in-buffer-window gnus-article-buffer
8035       (gnus-print-buffer))
8036     (gnus-summary-remove-process-mark article))
8037   (ps-despool filename))
8038
8039 (defun gnus-print-buffer ()
8040   (let ((buffer (generate-new-buffer " *print*")))
8041     (unwind-protect
8042         (progn
8043           (copy-to-buffer buffer (point-min) (point-max))
8044           (set-buffer buffer)
8045           (gnus-article-delete-invisible-text)
8046           (when (gnus-visual-p 'article-highlight 'highlight)
8047             ;; Copy-to-buffer doesn't copy overlay.  So redo
8048             ;; highlight.
8049             (let ((gnus-article-buffer buffer))
8050               (gnus-article-highlight-citation t)
8051               (gnus-article-highlight-signature)))
8052           (let ((ps-left-header
8053                  (list
8054                   (concat "("
8055                           (gnus-summary-print-truncate-and-quote
8056                            (mail-header-subject gnus-current-headers)
8057                            66) ")")
8058                   (concat "("
8059                           (gnus-summary-print-truncate-and-quote
8060                            (mail-header-from gnus-current-headers)
8061                            45) ")")))
8062                 (ps-right-header
8063                  (list
8064                   "/pagenumberstring load"
8065                   (concat "("
8066                           (mail-header-date gnus-current-headers) ")"))))
8067             (gnus-run-hooks 'gnus-ps-print-hook)
8068             (save-excursion
8069               (if window-system
8070                   (ps-spool-buffer-with-faces)
8071                 (ps-spool-buffer)))))
8072       (kill-buffer buffer))))
8073
8074 (defun gnus-summary-show-article (&optional arg)
8075   "Force redisplaying of the current article.
8076 If ARG (the prefix) is a number, show the article with the charset
8077 defined in `gnus-summary-show-article-charset-alist', or the charset
8078 input.
8079 If ARG (the prefix) is non-nil and not a number, show the raw article
8080 without any article massaging functions being run.  Normally, the key strokes
8081 are `C-u g'."
8082   (interactive "P")
8083   (cond
8084    ((numberp arg)
8085     (gnus-summary-show-article t)
8086     (let ((gnus-newsgroup-charset
8087            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8088                (mm-read-coding-system
8089                 "View as charset: " ;; actually it is coding system.
8090                 (save-excursion
8091                   (set-buffer gnus-article-buffer)
8092                   (mm-detect-coding-region (point) (point-max))))))
8093           (gnus-newsgroup-ignored-charsets 'gnus-all))
8094       (gnus-summary-select-article nil 'force)
8095       (let ((deps gnus-newsgroup-dependencies)
8096             head header lines)
8097         (save-excursion
8098           (set-buffer gnus-original-article-buffer)
8099           (save-restriction
8100             (message-narrow-to-head)
8101             (setq head (buffer-string))
8102             (goto-char (point-min))
8103             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8104               (goto-char (point-max))
8105               (widen)
8106               (setq lines (1- (count-lines (point) (point-max))))))
8107           (with-temp-buffer
8108             (insert (format "211 %d Article retrieved.\n"
8109                             (cdr gnus-article-current)))
8110             (insert head)
8111             (if lines (insert (format "Lines: %d\n" lines)))
8112             (insert ".\n")
8113             (let ((nntp-server-buffer (current-buffer)))
8114               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8115         (gnus-data-set-header
8116          (gnus-data-find (cdr gnus-article-current))
8117          header)
8118         (gnus-summary-update-article-line
8119          (cdr gnus-article-current) header)
8120         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8121           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8122    ((not arg)
8123     ;; Select the article the normal way.
8124     (gnus-summary-select-article nil 'force))
8125    (t
8126     ;; We have to require this here to make sure that the following
8127     ;; dynamic binding isn't shadowed by autoloading.
8128     (require 'gnus-async)
8129     (require 'gnus-art)
8130     ;; Bind the article treatment functions to nil.
8131     (let ((gnus-have-all-headers t)
8132           gnus-article-prepare-hook
8133           gnus-article-decode-hook
8134           gnus-display-mime-function
8135           gnus-break-pages)
8136       ;; Destroy any MIME parts.
8137       (when (gnus-buffer-live-p gnus-article-buffer)
8138         (save-excursion
8139           (set-buffer gnus-article-buffer)
8140           (mm-destroy-parts gnus-article-mime-handles)
8141           ;; Set it to nil for safety reason.
8142           (setq gnus-article-mime-handle-alist nil)
8143           (setq gnus-article-mime-handles nil)))
8144       (gnus-summary-select-article nil 'force))))
8145   (gnus-summary-goto-subject gnus-current-article)
8146   (gnus-summary-position-point))
8147
8148 (defun gnus-summary-show-raw-article ()
8149   "Show the raw article without any article massaging functions being run."
8150   (interactive)
8151   (gnus-summary-show-article t))
8152
8153 (defun gnus-summary-verbose-headers (&optional arg)
8154   "Toggle permanent full header display.
8155 If ARG is a positive number, turn header display on.
8156 If ARG is a negative number, turn header display off."
8157   (interactive "P")
8158   (setq gnus-show-all-headers
8159         (cond ((or (not (numberp arg))
8160                    (zerop arg))
8161                (not gnus-show-all-headers))
8162               ((natnump arg)
8163                t)))
8164   (gnus-summary-show-article))
8165
8166 (defun gnus-summary-toggle-header (&optional arg)
8167   "Show the headers if they are hidden, or hide them if they are shown.
8168 If ARG is a positive number, show the entire header.
8169 If ARG is a negative number, hide the unwanted header lines."
8170   (interactive "P")
8171   (save-excursion
8172     (set-buffer gnus-article-buffer)
8173     (save-restriction
8174       (let* ((buffer-read-only nil)
8175              (inhibit-point-motion-hooks t)
8176              hidden e)
8177         (save-restriction
8178           (article-narrow-to-head)
8179           (setq e (point-max)
8180                 hidden (if (numberp arg)
8181                            (>= arg 0)
8182                          (gnus-article-hidden-text-p 'headers))))
8183         (delete-region (point-min) e)
8184         (goto-char (point-min))
8185         (save-excursion
8186           (set-buffer gnus-original-article-buffer)
8187           (goto-char (point-min))
8188           (setq e (search-forward "\n\n" nil t)
8189                 e (if e (1- e) (point-max))))
8190         (insert-buffer-substring gnus-original-article-buffer 1 e)
8191         (save-restriction
8192           (narrow-to-region (point-min) (point))
8193           (article-decode-encoded-words)
8194           (if  hidden
8195               (let ((gnus-treat-hide-headers nil)
8196                     (gnus-treat-hide-boring-headers nil))
8197                 (gnus-delete-wash-type 'headers)
8198                 (gnus-treat-article 'head))
8199             (gnus-treat-article 'head)))
8200         (gnus-set-mode-line 'article)))))
8201
8202 (defun gnus-summary-show-all-headers ()
8203   "Make all header lines visible."
8204   (interactive)
8205   (gnus-summary-toggle-header 1))
8206
8207 (defun gnus-summary-caesar-message (&optional arg)
8208   "Caesar rotate the current article by 13.
8209 The numerical prefix specifies how many places to rotate each letter
8210 forward."
8211   (interactive "P")
8212   (gnus-summary-select-article)
8213   (let ((mail-header-separator ""))
8214     (gnus-eval-in-buffer-window gnus-article-buffer
8215       (save-restriction
8216         (widen)
8217         (let ((start (window-start))
8218               buffer-read-only)
8219           (message-caesar-buffer-body arg)
8220           (set-window-start (get-buffer-window (current-buffer)) start))))))
8221
8222 (defun gnus-summary-stop-page-breaking ()
8223   "Stop page breaking in the current article."
8224   (interactive)
8225   (gnus-summary-select-article)
8226   (gnus-eval-in-buffer-window gnus-article-buffer
8227     (widen)
8228     (when (gnus-visual-p 'page-marker)
8229       (let ((buffer-read-only nil))
8230         (gnus-remove-text-with-property 'gnus-prev)
8231         (gnus-remove-text-with-property 'gnus-next))
8232       (setq gnus-page-broken nil))))
8233
8234 (defun gnus-summary-move-article (&optional n to-newsgroup
8235                                             select-method action)
8236   "Move the current article to a different newsgroup.
8237 If N is a positive number, move the N next articles.
8238 If N is a negative number, move the N previous articles.
8239 If N is nil and any articles have been marked with the process mark,
8240 move those articles instead.
8241 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8242 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8243 re-spool using this method.
8244
8245 For this function to work, both the current newsgroup and the
8246 newsgroup that you want to move to have to support the `request-move'
8247 and `request-accept' functions.
8248
8249 ACTION can be either `move' (the default), `crosspost' or `copy'."
8250   (interactive "P")
8251   (unless action
8252     (setq action 'move))
8253   ;; Check whether the source group supports the required functions.
8254   (cond ((and (eq action 'move)
8255               (not (gnus-check-backend-function
8256                     'request-move-article gnus-newsgroup-name)))
8257          (error "The current group does not support article moving"))
8258         ((and (eq action 'crosspost)
8259               (not (gnus-check-backend-function
8260                     'request-replace-article gnus-newsgroup-name)))
8261          (error "The current group does not support article editing")))
8262   (let ((articles (gnus-summary-work-articles n))
8263         (prefix (if (gnus-check-backend-function
8264                      'request-move-article gnus-newsgroup-name)
8265                     (gnus-group-real-prefix gnus-newsgroup-name)
8266                   ""))
8267         (names '((move "Move" "Moving")
8268                  (copy "Copy" "Copying")
8269                  (crosspost "Crosspost" "Crossposting")))
8270         (copy-buf (save-excursion
8271                     (nnheader-set-temp-buffer " *copy article*")))
8272         art-group to-method new-xref article to-groups)
8273     (unless (assq action names)
8274       (error "Unknown action %s" action))
8275     ;; Read the newsgroup name.
8276     (when (and (not to-newsgroup)
8277                (not select-method))
8278       (if (and gnus-move-split-methods
8279                (not
8280                 (and (memq gnus-current-article articles)
8281                      (gnus-buffer-live-p gnus-original-article-buffer))))
8282           ;; When `gnus-move-split-methods' is non-nil, we have to
8283           ;; select an article to give `gnus-read-move-group-name' an
8284           ;; opportunity to suggest an appropriate default.  However,
8285           ;; we needn't render or mark the article.
8286           (let ((gnus-display-mime-function nil)
8287                 (gnus-article-prepare-hook nil)
8288                 (gnus-mark-article-hook nil))
8289             (gnus-summary-select-article nil nil nil (car articles))))
8290       (setq to-newsgroup
8291             (gnus-read-move-group-name
8292              (cadr (assq action names))
8293              (symbol-value (intern (format "gnus-current-%s-group" action)))
8294              articles prefix))
8295       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8296     (setq to-method (or select-method
8297                         (gnus-server-to-method
8298                          (gnus-group-method to-newsgroup))))
8299     ;; Check the method we are to move this article to...
8300     (unless (gnus-check-backend-function
8301              'request-accept-article (car to-method))
8302       (error "%s does not support article copying" (car to-method)))
8303     (unless (gnus-check-server to-method)
8304       (error "Can't open server %s" (car to-method)))
8305     (gnus-message 6 "%s to %s: %s..."
8306                   (caddr (assq action names))
8307                   (or (car select-method) to-newsgroup) articles)
8308     (while articles
8309       (setq article (pop articles))
8310       (setq
8311        art-group
8312        (cond
8313         ;; Move the article.
8314         ((eq action 'move)
8315          ;; Remove this article from future suppression.
8316          (gnus-dup-unsuppress-article article)
8317          (gnus-request-move-article
8318           article                       ; Article to move
8319           gnus-newsgroup-name           ; From newsgroup
8320           (nth 1 (gnus-find-method-for-group
8321                   gnus-newsgroup-name)) ; Server
8322           (list 'gnus-request-accept-article
8323                 to-newsgroup (list 'quote select-method)
8324                 (not articles) t)       ; Accept form
8325           (not articles)))              ; Only save nov last time
8326         ;; Copy the article.
8327         ((eq action 'copy)
8328          (save-excursion
8329            (set-buffer copy-buf)
8330            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8331              (gnus-request-accept-article
8332               to-newsgroup select-method (not articles) t))))
8333         ;; Crosspost the article.
8334         ((eq action 'crosspost)
8335          (let ((xref (message-tokenize-header
8336                       (mail-header-xref (gnus-summary-article-header article))
8337                       " ")))
8338            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8339                                   ":" (number-to-string article)))
8340            (unless xref
8341              (setq xref (list (system-name))))
8342            (setq new-xref
8343                  (concat
8344                   (mapconcat 'identity
8345                              (delete "Xref:" (delete new-xref xref))
8346                              " ")
8347                   " " new-xref))
8348            (save-excursion
8349              (set-buffer copy-buf)
8350              ;; First put the article in the destination group.
8351              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8352              (when (consp (setq art-group
8353                                 (gnus-request-accept-article
8354                                  to-newsgroup select-method (not articles))))
8355                (setq new-xref (concat new-xref " " (car art-group)
8356                                       ":"
8357                                       (number-to-string (cdr art-group))))
8358                ;; Now we have the new Xrefs header, so we insert
8359                ;; it and replace the new article.
8360                (nnheader-replace-header "Xref" new-xref)
8361                (gnus-request-replace-article
8362                 (cdr art-group) to-newsgroup (current-buffer))
8363                art-group))))))
8364       (cond
8365        ((not art-group)
8366         (gnus-message 1 "Couldn't %s article %s: %s"
8367                       (cadr (assq action names)) article
8368                       (nnheader-get-report (car to-method))))
8369        ((eq art-group 'junk)
8370         (when (eq action 'move)
8371           (gnus-summary-mark-article article gnus-canceled-mark)
8372           (gnus-message 4 "Deleted article %s" article)))
8373        (t
8374         (let* ((pto-group (gnus-group-prefixed-name
8375                            (car art-group) to-method))
8376                (entry
8377                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8378                (info (nth 2 entry))
8379                (to-group (gnus-info-group info))
8380                to-marks)
8381           ;; Update the group that has been moved to.
8382           (when (and info
8383                      (memq action '(move copy)))
8384             (unless (member to-group to-groups)
8385               (push to-group to-groups))
8386
8387             (unless (memq article gnus-newsgroup-unreads)
8388               (push 'read to-marks)
8389               (gnus-info-set-read
8390                info (gnus-add-to-range (gnus-info-read info)
8391                                        (list (cdr art-group)))))
8392
8393             ;; See whether the article is to be put in the cache.
8394             (let ((marks gnus-article-mark-lists)
8395                   (to-article (cdr art-group)))
8396
8397               ;; Enter the article into the cache in the new group,
8398               ;; if that is required.
8399               (when gnus-use-cache
8400                 (gnus-cache-possibly-enter-article
8401                  to-group to-article
8402                  (memq article gnus-newsgroup-marked)
8403                  (memq article gnus-newsgroup-dormant)
8404                  (memq article gnus-newsgroup-unreads)))
8405
8406               (when gnus-preserve-marks
8407                 ;; Copy any marks over to the new group.
8408                 (when (and (equal to-group gnus-newsgroup-name)
8409                            (not (memq article gnus-newsgroup-unreads)))
8410                   ;; Mark this article as read in this group.
8411                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8412                   (setcdr (gnus-active to-group) to-article)
8413                   (setcdr gnus-newsgroup-active to-article))
8414
8415                 (while marks
8416                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8417                     (when (memq article (symbol-value
8418                                          (intern (format "gnus-newsgroup-%s"
8419                                                          (caar marks)))))
8420                       (push (cdar marks) to-marks)
8421                       ;; If the other group is the same as this group,
8422                       ;; then we have to add the mark to the list.
8423                       (when (equal to-group gnus-newsgroup-name)
8424                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8425                              (cons to-article
8426                                    (symbol-value
8427                                     (intern (format "gnus-newsgroup-%s"
8428                                                     (caar marks)))))))
8429                       ;; Copy the marks to other group.
8430                       (gnus-add-marked-articles
8431                        to-group (cdar marks) (list to-article) info)))
8432                   (setq marks (cdr marks)))
8433
8434                 (gnus-request-set-mark to-group (list (list (list to-article)
8435                                                             'add
8436                                                             to-marks))))
8437
8438               (gnus-dribble-enter
8439                (concat "(gnus-group-set-info '"
8440                        (gnus-prin1-to-string (gnus-get-info to-group))
8441                        ")"))))
8442
8443           ;; Update the Xref header in this article to point to
8444           ;; the new crossposted article we have just created.
8445           (when (eq action 'crosspost)
8446             (save-excursion
8447               (set-buffer copy-buf)
8448               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8449               (nnheader-replace-header "Xref" new-xref)
8450               (gnus-request-replace-article
8451                article gnus-newsgroup-name (current-buffer)))))
8452
8453         ;;;!!!Why is this necessary?
8454         (set-buffer gnus-summary-buffer)
8455
8456         (gnus-summary-goto-subject article)
8457         (when (eq action 'move)
8458           (gnus-summary-mark-article article gnus-canceled-mark))))
8459       (gnus-summary-remove-process-mark article))
8460     ;; Re-activate all groups that have been moved to.
8461     (save-excursion
8462       (set-buffer gnus-group-buffer)
8463       (let ((gnus-group-marked to-groups))
8464         (gnus-group-get-new-news-this-group nil t)))
8465
8466     (gnus-kill-buffer copy-buf)
8467     (gnus-summary-position-point)
8468     (gnus-set-mode-line 'summary)))
8469
8470 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8471   "Move the current article to a different newsgroup.
8472 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8473 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8474 re-spool using this method."
8475   (interactive "P")
8476   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8477
8478 (defun gnus-summary-crosspost-article (&optional n)
8479   "Crosspost the current article to some other group."
8480   (interactive "P")
8481   (gnus-summary-move-article n nil nil 'crosspost))
8482
8483 (defcustom gnus-summary-respool-default-method nil
8484   "Default method type for respooling an article.
8485 If nil, use to the current newsgroup method."
8486   :type 'symbol
8487   :group 'gnus-summary-mail)
8488
8489 (defun gnus-summary-respool-article (&optional n method)
8490   "Respool the current article.
8491 The article will be squeezed through the mail spooling process again,
8492 which means that it will be put in some mail newsgroup or other
8493 depending on `nnmail-split-methods'.
8494 If N is a positive number, respool the N next articles.
8495 If N is a negative number, respool the N previous articles.
8496 If N is nil and any articles have been marked with the process mark,
8497 respool those articles instead.
8498
8499 Respooling can be done both from mail groups and \"real\" newsgroups.
8500 In the former case, the articles in question will be moved from the
8501 current group into whatever groups they are destined to.  In the
8502 latter case, they will be copied into the relevant groups."
8503   (interactive
8504    (list current-prefix-arg
8505          (let* ((methods (gnus-methods-using 'respool))
8506                 (methname
8507                  (symbol-name (or gnus-summary-respool-default-method
8508                                   (car (gnus-find-method-for-group
8509                                         gnus-newsgroup-name)))))
8510                 (method
8511                  (gnus-completing-read-with-default
8512                   methname "What backend do you want to use when respooling?"
8513                   methods nil t nil 'gnus-mail-method-history))
8514                 ms)
8515            (cond
8516             ((zerop (length (setq ms (gnus-servers-using-backend
8517                                       (intern method)))))
8518              (list (intern method) ""))
8519             ((= 1 (length ms))
8520              (car ms))
8521             (t
8522              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8523                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8524                            ms-alist))))))))
8525   (unless method
8526     (error "No method given for respooling"))
8527   (if (assoc (symbol-name
8528               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8529              (gnus-methods-using 'respool))
8530       (gnus-summary-move-article n nil method)
8531     (gnus-summary-copy-article n nil method)))
8532
8533 (defun gnus-summary-import-article (file &optional edit)
8534   "Import an arbitrary file into a mail newsgroup."
8535   (interactive "fImport file: \nP")
8536   (let ((group gnus-newsgroup-name)
8537         (now (current-time))
8538         atts lines group-art)
8539     (unless (gnus-check-backend-function 'request-accept-article group)
8540       (error "%s does not support article importing" group))
8541     (or (file-readable-p file)
8542         (not (file-regular-p file))
8543         (error "Can't read %s" file))
8544     (save-excursion
8545       (set-buffer (gnus-get-buffer-create " *import file*"))
8546       (erase-buffer)
8547       (nnheader-insert-file-contents file)
8548       (goto-char (point-min))
8549       (if (nnheader-article-p)
8550           (save-restriction
8551             (goto-char (point-min))
8552             (search-forward "\n\n" nil t)
8553             (narrow-to-region (point-min) (1- (point)))
8554             (goto-char (point-min))
8555             (unless (re-search-forward "^date:" nil t)
8556               (goto-char (point-max))
8557               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8558        ;; This doesn't look like an article, so we fudge some headers.
8559         (setq atts (file-attributes file)
8560               lines (count-lines (point-min) (point-max)))
8561         (insert "From: " (read-string "From: ") "\n"
8562                 "Subject: " (read-string "Subject: ") "\n"
8563                 "Date: " (message-make-date (nth 5 atts)) "\n"
8564                 "Message-ID: " (message-make-message-id) "\n"
8565                 "Lines: " (int-to-string lines) "\n"
8566                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8567       (setq group-art (gnus-request-accept-article group nil t))
8568       (kill-buffer (current-buffer)))
8569     (setq gnus-newsgroup-active (gnus-activate-group group))
8570     (forward-line 1)
8571     (gnus-summary-goto-article (cdr group-art) nil t)
8572     (when edit
8573       (gnus-summary-edit-article))))
8574
8575 (defun gnus-summary-create-article ()
8576   "Create an article in a mail newsgroup."
8577   (interactive)
8578   (let ((group gnus-newsgroup-name)
8579         (now (current-time))
8580         group-art)
8581     (unless (gnus-check-backend-function 'request-accept-article group)
8582       (error "%s does not support article importing" group))
8583     (save-excursion
8584       (set-buffer (gnus-get-buffer-create " *import file*"))
8585       (erase-buffer)
8586       (goto-char (point-min))
8587       ;; This doesn't look like an article, so we fudge some headers.
8588       (insert "From: " (read-string "From: ") "\n"
8589               "Subject: " (read-string "Subject: ") "\n"
8590               "Date: " (message-make-date now) "\n"
8591               "Message-ID: " (message-make-message-id) "\n")
8592       (setq group-art (gnus-request-accept-article group nil t))
8593       (kill-buffer (current-buffer)))
8594     (setq gnus-newsgroup-active (gnus-activate-group group))
8595     (forward-line 1)
8596     (gnus-summary-goto-article (cdr group-art) nil t)
8597     (gnus-summary-edit-article)))
8598
8599 (defun gnus-summary-article-posted-p ()
8600   "Say whether the current (mail) article is available from news as well.
8601 This will be the case if the article has both been mailed and posted."
8602   (interactive)
8603   (let ((id (mail-header-references (gnus-summary-article-header)))
8604         (gnus-override-method (car (gnus-refer-article-methods))))
8605     (if (gnus-request-head id "")
8606         (gnus-message 2 "The current message was found on %s"
8607                       gnus-override-method)
8608       (gnus-message 2 "The current message couldn't be found on %s"
8609                     gnus-override-method)
8610       nil)))
8611
8612 (defun gnus-summary-expire-articles (&optional now)
8613   "Expire all articles that are marked as expirable in the current group."
8614   (interactive)
8615   (when (gnus-check-backend-function
8616          'request-expire-articles gnus-newsgroup-name)
8617     ;; This backend supports expiry.
8618     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8619            (expirable (if total
8620                           (progn
8621                             ;; We need to update the info for
8622                             ;; this group for `gnus-list-of-read-articles'
8623                             ;; to give us the right answer.
8624                             (gnus-run-hooks 'gnus-exit-group-hook)
8625                             (gnus-summary-update-info)
8626                             (gnus-list-of-read-articles gnus-newsgroup-name))
8627                         (setq gnus-newsgroup-expirable
8628                               (sort gnus-newsgroup-expirable '<))))
8629            (expiry-wait (if now 'immediate
8630                           (gnus-group-find-parameter
8631                            gnus-newsgroup-name 'expiry-wait)))
8632            (nnmail-expiry-target
8633             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8634                 nnmail-expiry-target))
8635            es)
8636       (when expirable
8637         ;; There are expirable articles in this group, so we run them
8638         ;; through the expiry process.
8639         (gnus-message 6 "Expiring articles...")
8640         (unless (gnus-check-group gnus-newsgroup-name)
8641           (error "Can't open server for %s" gnus-newsgroup-name))
8642         ;; The list of articles that weren't expired is returned.
8643         (save-excursion
8644           (if expiry-wait
8645               (let ((nnmail-expiry-wait-function nil)
8646                     (nnmail-expiry-wait expiry-wait))
8647                 (setq es (gnus-request-expire-articles
8648                           expirable gnus-newsgroup-name)))
8649             (setq es (gnus-request-expire-articles
8650                       expirable gnus-newsgroup-name)))
8651           (unless total
8652             (setq gnus-newsgroup-expirable es))
8653           ;; We go through the old list of expirable, and mark all
8654           ;; really expired articles as nonexistent.
8655           (unless (eq es expirable) ;If nothing was expired, we don't mark.
8656             (let ((gnus-use-cache nil))
8657               (dolist (article expirable)
8658                 (when (and (not (memq article es))
8659                            (gnus-data-find article))
8660                   (gnus-summary-mark-article article gnus-canceled-mark))))))
8661         (gnus-message 6 "Expiring articles...done")))))
8662
8663 (defun gnus-summary-expire-articles-now ()
8664   "Expunge all expirable articles in the current group.
8665 This means that *all* articles that are marked as expirable will be
8666 deleted forever, right now."
8667   (interactive)
8668   (or gnus-expert-user
8669       (gnus-yes-or-no-p
8670        "Are you really, really, really sure you want to delete all these messages? ")
8671       (error "Phew!"))
8672   (gnus-summary-expire-articles t))
8673
8674 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8675 (defun gnus-summary-delete-article (&optional n)
8676   "Delete the N next (mail) articles.
8677 This command actually deletes articles.  This is not a marking
8678 command.  The article will disappear forever from your life, never to
8679 return.
8680 If N is negative, delete backwards.
8681 If N is nil and articles have been marked with the process mark,
8682 delete these instead."
8683   (interactive "P")
8684   (unless (gnus-check-backend-function 'request-expire-articles
8685                                        gnus-newsgroup-name)
8686     (error "The current newsgroup does not support article deletion"))
8687   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8688     (error "Couldn't open server"))
8689   ;; Compute the list of articles to delete.
8690   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8691         not-deleted)
8692     (if (and gnus-novice-user
8693              (not (gnus-yes-or-no-p
8694                    (format "Do you really want to delete %s forever? "
8695                            (if (> (length articles) 1)
8696                                (format "these %s articles" (length articles))
8697                              "this article")))))
8698         ()
8699       ;; Delete the articles.
8700       (setq not-deleted (gnus-request-expire-articles
8701                          articles gnus-newsgroup-name 'force))
8702       (while articles
8703         (gnus-summary-remove-process-mark (car articles))
8704         ;; The backend might not have been able to delete the article
8705         ;; after all.
8706         (unless (memq (car articles) not-deleted)
8707           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8708         (setq articles (cdr articles)))
8709       (when not-deleted
8710         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8711     (gnus-summary-position-point)
8712     (gnus-set-mode-line 'summary)
8713     not-deleted))
8714
8715 (defun gnus-summary-edit-article (&optional arg)
8716   "Edit the current article.
8717 This will have permanent effect only in mail groups.
8718 If ARG is nil, edit the decoded articles.
8719 If ARG is 1, edit the raw articles.
8720 If ARG is 2, edit the raw articles even in read-only groups.
8721 If ARG is 3, edit the articles with the current handles.
8722 Otherwise, allow editing of articles even in read-only
8723 groups."
8724   (interactive "P")
8725   (let (force raw current-handles)
8726     (cond
8727      ((null arg))
8728      ((eq arg 1)
8729       (setq raw t))
8730      ((eq arg 2)
8731       (setq raw t
8732             force t))
8733      ((eq arg 3)
8734       (setq current-handles
8735             (and (gnus-buffer-live-p gnus-article-buffer)
8736                  (with-current-buffer gnus-article-buffer
8737                    (prog1
8738                        gnus-article-mime-handles
8739                      (setq gnus-article-mime-handles nil))))))
8740      (t
8741       (setq force t)))
8742     (when (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
8743       (error "Can't edit the raw article in group nndraft:drafts"))
8744     (save-excursion
8745       (set-buffer gnus-summary-buffer)
8746       (let ((mail-parse-charset gnus-newsgroup-charset)
8747             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8748         (gnus-set-global-variables)
8749         (when (and (not force)
8750                    (gnus-group-read-only-p))
8751           (error "The current newsgroup does not support article editing"))
8752         (gnus-summary-show-article t)
8753         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
8754           (with-current-buffer gnus-article-buffer
8755             (mm-enable-multibyte)))
8756         (if (equal gnus-newsgroup-name "nndraft:drafts")
8757             (setq raw t))
8758         (gnus-article-edit-article
8759          (if raw 'ignore
8760            `(lambda ()
8761               (let ((mbl mml-buffer-list))
8762                 (setq mml-buffer-list nil)
8763                 (mime-to-mml ,'current-handles)
8764                 (let ((mbl1 mml-buffer-list))
8765                   (setq mml-buffer-list mbl)
8766                   (set (make-local-variable 'mml-buffer-list) mbl1))
8767                 (make-local-hook 'kill-buffer-hook)
8768                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
8769          `(lambda (no-highlight)
8770             (let ((mail-parse-charset ',gnus-newsgroup-charset)
8771                   (message-options message-options)
8772                   (message-options-set-recipient)
8773                   (mail-parse-ignored-charsets
8774                    ',gnus-newsgroup-ignored-charsets))
8775               ,(if (not raw) '(progn
8776                                 (mml-to-mime)
8777                                 (mml-destroy-buffers)
8778                                 (remove-hook 'kill-buffer-hook
8779                                              'mml-destroy-buffers t)
8780                                 (kill-local-variable 'mml-buffer-list)))
8781               (gnus-summary-edit-article-done
8782                ,(or (mail-header-references gnus-current-headers) "")
8783                ,(gnus-group-read-only-p)
8784                ,gnus-summary-buffer no-highlight))))))))
8785
8786 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8787
8788 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8789                                                  no-highlight)
8790   "Make edits to the current article permanent."
8791   (interactive)
8792   (save-excursion
8793    ;; The buffer restriction contains the entire article if it exists.
8794     (when (article-goto-body)
8795       (let ((lines (count-lines (point) (point-max)))
8796             (length (- (point-max) (point)))
8797             (case-fold-search t)
8798             (body (copy-marker (point))))
8799         (goto-char (point-min))
8800         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8801           (delete-region (match-beginning 1) (match-end 1))
8802           (insert (number-to-string length)))
8803         (goto-char (point-min))
8804         (when (re-search-forward
8805                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8806           (delete-region (match-beginning 1) (match-end 1))
8807           (insert (number-to-string length)))
8808         (goto-char (point-min))
8809         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8810           (delete-region (match-beginning 1) (match-end 1))
8811           (insert (number-to-string lines))))))
8812   ;; Replace the article.
8813   (let ((buf (current-buffer)))
8814     (with-temp-buffer
8815       (insert-buffer-substring buf)
8816
8817       (if (and (not read-only)
8818                (not (gnus-request-replace-article
8819                      (cdr gnus-article-current) (car gnus-article-current)
8820                      (current-buffer) t)))
8821           (error "Couldn't replace article")
8822         ;; Update the summary buffer.
8823         (if (and references
8824                  (equal (message-tokenize-header references " ")
8825                         (message-tokenize-header
8826                          (or (message-fetch-field "references") "") " ")))
8827             ;; We only have to update this line.
8828             (save-excursion
8829               (save-restriction
8830                 (message-narrow-to-head)
8831                 (let ((head (buffer-string))
8832                       header)
8833                   (with-temp-buffer
8834                     (insert (format "211 %d Article retrieved.\n"
8835                                     (cdr gnus-article-current)))
8836                     (insert head)
8837                     (insert ".\n")
8838                     (let ((nntp-server-buffer (current-buffer)))
8839                       (setq header (car (gnus-get-newsgroup-headers
8840                                          nil t))))
8841                     (save-excursion
8842                       (set-buffer gnus-summary-buffer)
8843                       (gnus-data-set-header
8844                        (gnus-data-find (cdr gnus-article-current))
8845                        header)
8846                       (gnus-summary-update-article-line
8847                        (cdr gnus-article-current) header)
8848                       (if (gnus-summary-goto-subject
8849                            (cdr gnus-article-current) nil t)
8850                           (gnus-summary-update-secondary-mark
8851                            (cdr gnus-article-current))))))))
8852           ;; Update threads.
8853           (set-buffer (or buffer gnus-summary-buffer))
8854           (gnus-summary-update-article (cdr gnus-article-current))
8855           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8856               (gnus-summary-update-secondary-mark
8857                (cdr gnus-article-current))))
8858         ;; Prettify the article buffer again.
8859         (unless no-highlight
8860           (save-excursion
8861             (set-buffer gnus-article-buffer)
8862             ;;;!!! Fix this -- article should be rehighlighted.
8863             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8864             (set-buffer gnus-original-article-buffer)
8865             (gnus-request-article
8866              (cdr gnus-article-current)
8867              (car gnus-article-current) (current-buffer))))
8868         ;; Prettify the summary buffer line.
8869         (when (gnus-visual-p 'summary-highlight 'highlight)
8870           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8871
8872 (defun gnus-summary-edit-wash (key)
8873   "Perform editing command KEY in the article buffer."
8874   (interactive
8875    (list
8876     (progn
8877       (message "%s" (concat (this-command-keys) "- "))
8878       (read-char))))
8879   (message "")
8880   (gnus-summary-edit-article)
8881   (execute-kbd-macro (concat (this-command-keys) key))
8882   (gnus-article-edit-done))
8883
8884 ;;; Respooling
8885
8886 (defun gnus-summary-respool-query (&optional silent trace)
8887   "Query where the respool algorithm would put this article."
8888   (interactive)
8889   (let (gnus-mark-article-hook)
8890     (gnus-summary-select-article)
8891     (save-excursion
8892       (set-buffer gnus-original-article-buffer)
8893       (save-restriction
8894         (message-narrow-to-head)
8895         (let ((groups (nnmail-article-group 'identity trace)))
8896           (unless silent
8897             (if groups
8898                 (message "This message would go to %s"
8899                          (mapconcat 'car groups ", "))
8900               (message "This message would go to no groups"))
8901             groups))))))
8902
8903 (defun gnus-summary-respool-trace ()
8904   "Trace where the respool algorithm would put this article.
8905 Display a buffer showing all fancy splitting patterns which matched."
8906   (interactive)
8907   (gnus-summary-respool-query nil t))
8908
8909 ;; Summary marking commands.
8910
8911 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8912   "Mark articles which has the same subject as read, and then select the next.
8913 If UNMARK is positive, remove any kind of mark.
8914 If UNMARK is negative, tick articles."
8915   (interactive "P")
8916   (when unmark
8917     (setq unmark (prefix-numeric-value unmark)))
8918   (let ((count
8919          (gnus-summary-mark-same-subject
8920           (gnus-summary-article-subject) unmark)))
8921     ;; Select next unread article.  If auto-select-same mode, should
8922     ;; select the first unread article.
8923     (gnus-summary-next-article t (and gnus-auto-select-same
8924                                       (gnus-summary-article-subject)))
8925     (gnus-message 7 "%d article%s marked as %s"
8926                   count (if (= count 1) " is" "s are")
8927                   (if unmark "unread" "read"))))
8928
8929 (defun gnus-summary-kill-same-subject (&optional unmark)
8930   "Mark articles which has the same subject as read.
8931 If UNMARK is positive, remove any kind of mark.
8932 If UNMARK is negative, tick articles."
8933   (interactive "P")
8934   (when unmark
8935     (setq unmark (prefix-numeric-value unmark)))
8936   (let ((count
8937          (gnus-summary-mark-same-subject
8938           (gnus-summary-article-subject) unmark)))
8939     ;; If marked as read, go to next unread subject.
8940     (when (null unmark)
8941       ;; Go to next unread subject.
8942       (gnus-summary-next-subject 1 t))
8943     (gnus-message 7 "%d articles are marked as %s"
8944                   count (if unmark "unread" "read"))))
8945
8946 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8947   "Mark articles with same SUBJECT as read, and return marked number.
8948 If optional argument UNMARK is positive, remove any kinds of marks.
8949 If optional argument UNMARK is negative, mark articles as unread instead."
8950   (let ((count 1))
8951     (save-excursion
8952       (cond
8953        ((null unmark)                   ; Mark as read.
8954         (while (and
8955                 (progn
8956                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8957                   (gnus-summary-show-thread) t)
8958                 (gnus-summary-find-subject subject))
8959           (setq count (1+ count))))
8960        ((> unmark 0)                    ; Tick.
8961         (while (and
8962                 (progn
8963                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8964                   (gnus-summary-show-thread) t)
8965                 (gnus-summary-find-subject subject))
8966           (setq count (1+ count))))
8967        (t                               ; Mark as unread.
8968         (while (and
8969                 (progn
8970                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8971                   (gnus-summary-show-thread) t)
8972                 (gnus-summary-find-subject subject))
8973           (setq count (1+ count)))))
8974       (gnus-set-mode-line 'summary)
8975       ;; Return the number of marked articles.
8976       count)))
8977
8978 (defun gnus-summary-mark-as-processable (n &optional unmark)
8979   "Set the process mark on the next N articles.
8980 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8981 the process mark instead.  The difference between N and the actual
8982 number of articles marked is returned."
8983   (interactive "P")
8984   (if (and (null n) (gnus-region-active-p))
8985       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8986     (setq n (prefix-numeric-value n))
8987     (let ((backward (< n 0))
8988           (n (abs n)))
8989       (while (and
8990               (> n 0)
8991               (if unmark
8992                   (gnus-summary-remove-process-mark
8993                    (gnus-summary-article-number))
8994                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8995               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8996         (setq n (1- n)))
8997       (when (/= 0 n)
8998         (gnus-message 7 "No more articles"))
8999       (gnus-summary-recenter)
9000       (gnus-summary-position-point)
9001       n)))
9002
9003 (defun gnus-summary-unmark-as-processable (n)
9004   "Remove the process mark from the next N articles.
9005 If N is negative, unmark backward instead.  The difference between N and
9006 the actual number of articles unmarked is returned."
9007   (interactive "P")
9008   (gnus-summary-mark-as-processable n t))
9009
9010 (defun gnus-summary-unmark-all-processable ()
9011   "Remove the process mark from all articles."
9012   (interactive)
9013   (save-excursion
9014     (while gnus-newsgroup-processable
9015       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9016   (gnus-summary-position-point))
9017
9018 (defun gnus-summary-add-mark (article type)
9019   "Mark ARTICLE with a mark of TYPE."
9020   (let ((vtype (car (assq type gnus-article-mark-lists)))
9021         var)
9022     (if (not vtype)
9023         (error "No such mark type: %s" type)
9024       (setq var (intern (format "gnus-newsgroup-%s" type)))
9025       (set var (cons article (symbol-value var)))
9026       (if (memq type '(processable cached replied forwarded recent saved))
9027           (gnus-summary-update-secondary-mark article)
9028         ;;; !!! This is bogus.  We should find out what primary
9029         ;;; !!! mark we want to set.
9030         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9031
9032 (defun gnus-summary-mark-as-expirable (n)
9033   "Mark N articles forward as expirable.
9034 If N is negative, mark backward instead.  The difference between N and
9035 the actual number of articles marked is returned."
9036   (interactive "p")
9037   (gnus-summary-mark-forward n gnus-expirable-mark))
9038
9039 (defun gnus-summary-mark-article-as-replied (article)
9040   "Mark ARTICLE as replied to and update the summary line.
9041 ARTICLE can also be a list of articles."
9042   (interactive (list (gnus-summary-article-number)))
9043   (let ((articles (if (listp article) article (list article))))
9044     (dolist (article articles)
9045       (push article gnus-newsgroup-replied)
9046       (let ((buffer-read-only nil))
9047         (when (gnus-summary-goto-subject article nil t)
9048           (gnus-summary-update-secondary-mark article))))))
9049
9050 (defun gnus-summary-mark-article-as-forwarded (article)
9051   "Mark ARTICLE as forwarded and update the summary line.
9052 ARTICLE can also be a list of articles."
9053   (let ((articles (if (listp article) article (list article))))
9054     (dolist (article articles)
9055       (push article gnus-newsgroup-forwarded)
9056       (let ((buffer-read-only nil))
9057         (when (gnus-summary-goto-subject article nil t)
9058           (gnus-summary-update-secondary-mark article))))))
9059
9060 (defun gnus-summary-set-bookmark (article)
9061   "Set a bookmark in current article."
9062   (interactive (list (gnus-summary-article-number)))
9063   (when (or (not (get-buffer gnus-article-buffer))
9064             (not gnus-current-article)
9065             (not gnus-article-current)
9066             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9067     (error "No current article selected"))
9068   ;; Remove old bookmark, if one exists.
9069   (let ((old (assq article gnus-newsgroup-bookmarks)))
9070     (when old
9071       (setq gnus-newsgroup-bookmarks
9072             (delq old gnus-newsgroup-bookmarks))))
9073   ;; Set the new bookmark, which is on the form
9074   ;; (article-number . line-number-in-body).
9075   (push
9076    (cons article
9077          (save-excursion
9078            (set-buffer gnus-article-buffer)
9079            (count-lines
9080             (min (point)
9081                  (save-excursion
9082                    (goto-char (point-min))
9083                    (search-forward "\n\n" nil t)
9084                    (point)))
9085             (point))))
9086    gnus-newsgroup-bookmarks)
9087   (gnus-message 6 "A bookmark has been added to the current article."))
9088
9089 (defun gnus-summary-remove-bookmark (article)
9090   "Remove the bookmark from the current article."
9091   (interactive (list (gnus-summary-article-number)))
9092   ;; Remove old bookmark, if one exists.
9093   (let ((old (assq article gnus-newsgroup-bookmarks)))
9094     (if old
9095         (progn
9096           (setq gnus-newsgroup-bookmarks
9097                 (delq old gnus-newsgroup-bookmarks))
9098           (gnus-message 6 "Removed bookmark."))
9099       (gnus-message 6 "No bookmark in current article."))))
9100
9101 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9102 (defun gnus-summary-mark-as-dormant (n)
9103   "Mark N articles forward as dormant.
9104 If N is negative, mark backward instead.  The difference between N and
9105 the actual number of articles marked is returned."
9106   (interactive "p")
9107   (gnus-summary-mark-forward n gnus-dormant-mark))
9108
9109 (defun gnus-summary-set-process-mark (article)
9110   "Set the process mark on ARTICLE and update the summary line."
9111   (setq gnus-newsgroup-processable
9112         (cons article
9113               (delq article gnus-newsgroup-processable)))
9114   (when (gnus-summary-goto-subject article)
9115     (gnus-summary-show-thread)
9116     (gnus-summary-goto-subject article)
9117     (gnus-summary-update-secondary-mark article)))
9118
9119 (defun gnus-summary-remove-process-mark (article)
9120   "Remove the process mark from ARTICLE and update the summary line."
9121   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9122   (when (gnus-summary-goto-subject article)
9123     (gnus-summary-show-thread)
9124     (gnus-summary-goto-subject article)
9125     (gnus-summary-update-secondary-mark article)))
9126
9127 (defun gnus-summary-set-saved-mark (article)
9128   "Set the process mark on ARTICLE and update the summary line."
9129   (push article gnus-newsgroup-saved)
9130   (when (gnus-summary-goto-subject article)
9131     (gnus-summary-update-secondary-mark article)))
9132
9133 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9134   "Mark N articles as read forwards.
9135 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9136 The difference between N and the actual number of articles marked is
9137 returned.
9138 Iff NO-EXPIRE, auto-expiry will be inhibited."
9139   (interactive "p")
9140   (gnus-summary-show-thread)
9141   (let ((backward (< n 0))
9142         (gnus-summary-goto-unread
9143          (and gnus-summary-goto-unread
9144               (not (eq gnus-summary-goto-unread 'never))
9145               (not (memq mark (list gnus-unread-mark
9146                                     gnus-ticked-mark gnus-dormant-mark)))))
9147         (n (abs n))
9148         (mark (or mark gnus-del-mark)))
9149     (while (and (> n 0)
9150                 (gnus-summary-mark-article nil mark no-expire)
9151                 (zerop (gnus-summary-next-subject
9152                         (if backward -1 1)
9153                         (and gnus-summary-goto-unread
9154                              (not (eq gnus-summary-goto-unread 'never)))
9155                         t)))
9156       (setq n (1- n)))
9157     (when (/= 0 n)
9158       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9159     (gnus-summary-recenter)
9160     (gnus-summary-position-point)
9161     (gnus-set-mode-line 'summary)
9162     n))
9163
9164 (defun gnus-summary-mark-article-as-read (mark)
9165   "Mark the current article quickly as read with MARK."
9166   (let ((article (gnus-summary-article-number)))
9167     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9168     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9169     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9170     (push (cons article mark) gnus-newsgroup-reads)
9171     ;; Possibly remove from cache, if that is used.
9172     (when gnus-use-cache
9173       (gnus-cache-enter-remove-article article))
9174     ;; Allow the backend to change the mark.
9175     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9176     ;; Check for auto-expiry.
9177     (when (and gnus-newsgroup-auto-expire
9178                (memq mark gnus-auto-expirable-marks))
9179       (setq mark gnus-expirable-mark)
9180       ;; Let the backend know about the mark change.
9181       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9182       (push article gnus-newsgroup-expirable))
9183     ;; Set the mark in the buffer.
9184     (gnus-summary-update-mark mark 'unread)
9185     t))
9186
9187 (defun gnus-summary-mark-article-as-unread (mark)
9188   "Mark the current article quickly as unread with MARK."
9189   (let* ((article (gnus-summary-article-number))
9190          (old-mark (gnus-summary-article-mark article)))
9191     ;; Allow the backend to change the mark.
9192     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9193     (if (eq mark old-mark)
9194         t
9195       (if (<= article 0)
9196           (progn
9197             (gnus-error 1 "Can't mark negative article numbers")
9198             nil)
9199         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9200         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9201         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9202         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9203         (cond ((= mark gnus-ticked-mark)
9204                (setq gnus-newsgroup-marked
9205                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9206                                               article)))
9207               ((= mark gnus-dormant-mark)
9208                (setq gnus-newsgroup-dormant
9209                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9210                                               article)))
9211               (t
9212                (setq gnus-newsgroup-unreads
9213                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9214                                               article))))
9215         (gnus-pull article gnus-newsgroup-reads)
9216
9217         ;; See whether the article is to be put in the cache.
9218         (and gnus-use-cache
9219              (vectorp (gnus-summary-article-header article))
9220              (save-excursion
9221                (gnus-cache-possibly-enter-article
9222                 gnus-newsgroup-name article
9223                 (= mark gnus-ticked-mark)
9224                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9225
9226         ;; Fix the mark.
9227         (gnus-summary-update-mark mark 'unread)
9228         t))))
9229
9230 (defun gnus-summary-mark-article (&optional article mark no-expire)
9231   "Mark ARTICLE with MARK.  MARK can be any character.
9232 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9233 `??' (dormant) and `?E' (expirable).
9234 If MARK is nil, then the default character `?r' is used.
9235 If ARTICLE is nil, then the article on the current line will be
9236 marked.
9237 Iff NO-EXPIRE, auto-expiry will be inhibited."
9238   ;; The mark might be a string.
9239   (when (stringp mark)
9240     (setq mark (aref mark 0)))
9241   ;; If no mark is given, then we check auto-expiring.
9242   (when (null mark)
9243     (setq mark gnus-del-mark))
9244   (when (and (not no-expire)
9245              gnus-newsgroup-auto-expire
9246              (memq mark gnus-auto-expirable-marks))
9247     (setq mark gnus-expirable-mark))
9248   (let ((article (or article (gnus-summary-article-number)))
9249         (old-mark (gnus-summary-article-mark article)))
9250     ;; Allow the backend to change the mark.
9251     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9252     (if (eq mark old-mark)
9253         t
9254       (unless article
9255         (error "No article on current line"))
9256       (if (not (if (or (= mark gnus-unread-mark)
9257                        (= mark gnus-ticked-mark)
9258                        (= mark gnus-dormant-mark))
9259                    (gnus-mark-article-as-unread article mark)
9260                  (gnus-mark-article-as-read article mark)))
9261           t
9262         ;; See whether the article is to be put in the cache.
9263         (and gnus-use-cache
9264              (not (= mark gnus-canceled-mark))
9265              (vectorp (gnus-summary-article-header article))
9266              (save-excursion
9267                (gnus-cache-possibly-enter-article
9268                 gnus-newsgroup-name article
9269                 (= mark gnus-ticked-mark)
9270                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9271
9272         (when (gnus-summary-goto-subject article nil t)
9273           (let ((buffer-read-only nil))
9274             (gnus-summary-show-thread)
9275             ;; Fix the mark.
9276             (gnus-summary-update-mark mark 'unread)
9277             t))))))
9278
9279 (defun gnus-summary-update-secondary-mark (article)
9280   "Update the secondary (read, process, cache) mark."
9281   (gnus-summary-update-mark
9282    (cond ((memq article gnus-newsgroup-processable)
9283           gnus-process-mark)
9284          ((memq article gnus-newsgroup-cached)
9285           gnus-cached-mark)
9286          ((memq article gnus-newsgroup-replied)
9287           gnus-replied-mark)
9288          ((memq article gnus-newsgroup-forwarded)
9289           gnus-forwarded-mark)
9290          ((memq article gnus-newsgroup-saved)
9291           gnus-saved-mark)
9292          ((memq article gnus-newsgroup-recent)
9293           gnus-recent-mark)
9294          ((memq article gnus-newsgroup-unseen)
9295           gnus-unseen-mark)
9296          (t gnus-no-mark))
9297    'replied)
9298   (when (gnus-visual-p 'summary-highlight 'highlight)
9299     (gnus-run-hooks 'gnus-summary-update-hook))
9300   t)
9301
9302 (defun gnus-summary-update-mark (mark type)
9303   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9304         (buffer-read-only nil))
9305     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9306     (when forward
9307       (when (looking-at "\r")
9308         (incf forward))
9309       (when (<= (+ forward (point)) (point-max))
9310         ;; Go to the right position on the line.
9311         (goto-char (+ forward (point)))
9312         ;; Replace the old mark with the new mark.
9313         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9314         ;; Optionally update the marks by some user rule.
9315         (when (eq type 'unread)
9316           (gnus-data-set-mark
9317            (gnus-data-find (gnus-summary-article-number)) mark)
9318           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9319
9320 (defun gnus-mark-article-as-read (article &optional mark)
9321   "Enter ARTICLE in the pertinent lists and remove it from others."
9322   ;; Make the article expirable.
9323   (let ((mark (or mark gnus-del-mark)))
9324     (setq gnus-newsgroup-expirable
9325           (if (= mark gnus-expirable-mark)
9326               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9327             (delq article gnus-newsgroup-expirable)))
9328     ;; Remove from unread and marked lists.
9329     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9330     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9331     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9332     (push (cons article mark) gnus-newsgroup-reads)
9333     ;; Possibly remove from cache, if that is used.
9334     (when gnus-use-cache
9335       (gnus-cache-enter-remove-article article))
9336     t))
9337
9338 (defun gnus-mark-article-as-unread (article &optional mark)
9339   "Enter ARTICLE in the pertinent lists and remove it from others."
9340   (let ((mark (or mark gnus-ticked-mark)))
9341     (if (<= article 0)
9342         (progn
9343           (gnus-error 1 "Can't mark negative article numbers")
9344           nil)
9345       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9346             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9347             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9348             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9349
9350       ;; Unsuppress duplicates?
9351       (when gnus-suppress-duplicates
9352         (gnus-dup-unsuppress-article article))
9353
9354       (cond ((= mark gnus-ticked-mark)
9355              (setq gnus-newsgroup-marked
9356                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9357             ((= mark gnus-dormant-mark)
9358              (setq gnus-newsgroup-dormant
9359                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9360             (t
9361              (setq gnus-newsgroup-unreads
9362                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9363       (gnus-pull article gnus-newsgroup-reads)
9364       t)))
9365
9366 (defalias 'gnus-summary-mark-as-unread-forward
9367   'gnus-summary-tick-article-forward)
9368 (make-obsolete 'gnus-summary-mark-as-unread-forward
9369                'gnus-summary-tick-article-forward)
9370 (defun gnus-summary-tick-article-forward (n)
9371   "Tick N articles forwards.
9372 If N is negative, tick backwards instead.
9373 The difference between N and the number of articles ticked is returned."
9374   (interactive "p")
9375   (gnus-summary-mark-forward n gnus-ticked-mark))
9376
9377 (defalias 'gnus-summary-mark-as-unread-backward
9378   'gnus-summary-tick-article-backward)
9379 (make-obsolete 'gnus-summary-mark-as-unread-backward
9380                'gnus-summary-tick-article-backward)
9381 (defun gnus-summary-tick-article-backward (n)
9382   "Tick N articles backwards.
9383 The difference between N and the number of articles ticked is returned."
9384   (interactive "p")
9385   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9386
9387 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9388 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9389 (defun gnus-summary-tick-article (&optional article clear-mark)
9390   "Mark current article as unread.
9391 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9392 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9393   (interactive)
9394   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9395                                        gnus-ticked-mark)))
9396
9397 (defun gnus-summary-mark-as-read-forward (n)
9398   "Mark N articles as read forwards.
9399 If N is negative, mark backwards instead.
9400 The difference between N and the actual number of articles marked is
9401 returned."
9402   (interactive "p")
9403   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9404
9405 (defun gnus-summary-mark-as-read-backward (n)
9406   "Mark the N articles as read backwards.
9407 The difference between N and the actual number of articles marked is
9408 returned."
9409   (interactive "p")
9410   (gnus-summary-mark-forward
9411    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9412
9413 (defun gnus-summary-mark-as-read (&optional article mark)
9414   "Mark current article as read.
9415 ARTICLE specifies the article to be marked as read.
9416 MARK specifies a string to be inserted at the beginning of the line."
9417   (gnus-summary-mark-article article mark))
9418
9419 (defun gnus-summary-clear-mark-forward (n)
9420   "Clear marks from N articles forward.
9421 If N is negative, clear backward instead.
9422 The difference between N and the number of marks cleared is returned."
9423   (interactive "p")
9424   (gnus-summary-mark-forward n gnus-unread-mark))
9425
9426 (defun gnus-summary-clear-mark-backward (n)
9427   "Clear marks from N articles backward.
9428 The difference between N and the number of marks cleared is returned."
9429   (interactive "p")
9430   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9431
9432 (defun gnus-summary-mark-unread-as-read ()
9433   "Intended to be used by `gnus-summary-mark-article-hook'."
9434   (when (memq gnus-current-article gnus-newsgroup-unreads)
9435     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9436
9437 (defun gnus-summary-mark-read-and-unread-as-read ()
9438   "Intended to be used by `gnus-summary-mark-article-hook'."
9439   (let ((mark (gnus-summary-article-mark)))
9440     (when (or (gnus-unread-mark-p mark)
9441               (gnus-read-mark-p mark))
9442       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9443
9444 (defun gnus-summary-mark-unread-as-ticked ()
9445   "Intended to be used by `gnus-summary-mark-article-hook'."
9446   (when (memq gnus-current-article gnus-newsgroup-unreads)
9447     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9448
9449 (defun gnus-summary-mark-region-as-read (point mark all)
9450   "Mark all unread articles between point and mark as read.
9451 If given a prefix, mark all articles between point and mark as read,
9452 even ticked and dormant ones."
9453   (interactive "r\nP")
9454   (save-excursion
9455     (let (article)
9456       (goto-char point)
9457       (beginning-of-line)
9458       (while (and
9459               (< (point) mark)
9460               (progn
9461                 (when (or all
9462                           (memq (setq article (gnus-summary-article-number))
9463                                 gnus-newsgroup-unreads))
9464                   (gnus-summary-mark-article article gnus-del-mark))
9465                 t)
9466               (gnus-summary-find-next))))))
9467
9468 (defun gnus-summary-mark-below (score mark)
9469   "Mark articles with score less than SCORE with MARK."
9470   (interactive "P\ncMark: ")
9471   (setq score (if score
9472                   (prefix-numeric-value score)
9473                 (or gnus-summary-default-score 0)))
9474   (save-excursion
9475     (set-buffer gnus-summary-buffer)
9476     (goto-char (point-min))
9477     (while
9478         (progn
9479           (and (< (gnus-summary-article-score) score)
9480                (gnus-summary-mark-article nil mark))
9481           (gnus-summary-find-next)))))
9482
9483 (defun gnus-summary-kill-below (&optional score)
9484   "Mark articles with score below SCORE as read."
9485   (interactive "P")
9486   (gnus-summary-mark-below score gnus-killed-mark))
9487
9488 (defun gnus-summary-clear-above (&optional score)
9489   "Clear all marks from articles with score above SCORE."
9490   (interactive "P")
9491   (gnus-summary-mark-above score gnus-unread-mark))
9492
9493 (defun gnus-summary-tick-above (&optional score)
9494   "Tick all articles with score above SCORE."
9495   (interactive "P")
9496   (gnus-summary-mark-above score gnus-ticked-mark))
9497
9498 (defun gnus-summary-mark-above (score mark)
9499   "Mark articles with score over SCORE with MARK."
9500   (interactive "P\ncMark: ")
9501   (setq score (if score
9502                   (prefix-numeric-value score)
9503                 (or gnus-summary-default-score 0)))
9504   (save-excursion
9505     (set-buffer gnus-summary-buffer)
9506     (goto-char (point-min))
9507     (while (and (progn
9508                   (when (> (gnus-summary-article-score) score)
9509                     (gnus-summary-mark-article nil mark))
9510                   t)
9511                 (gnus-summary-find-next)))))
9512
9513 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9514 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9515 (defun gnus-summary-limit-include-expunged (&optional no-error)
9516   "Display all the hidden articles that were expunged for low scores."
9517   (interactive)
9518   (let ((buffer-read-only nil))
9519     (let ((scored gnus-newsgroup-scored)
9520           headers h)
9521       (while scored
9522         (unless (gnus-summary-article-header (caar scored))
9523           (and (setq h (gnus-number-to-header (caar scored)))
9524                (< (cdar scored) gnus-summary-expunge-below)
9525                (push h headers)))
9526         (setq scored (cdr scored)))
9527       (if (not headers)
9528           (when (not no-error)
9529             (error "No expunged articles hidden"))
9530         (goto-char (point-min))
9531         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9532         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9533         (mapcar (lambda (x) (push (mail-header-number x)
9534                                   gnus-newsgroup-limit))
9535                 headers)
9536         (gnus-summary-prepare-unthreaded (nreverse headers))
9537         (goto-char (point-min))
9538         (gnus-summary-position-point)
9539         t))))
9540
9541 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9542   "Mark all unread articles in this newsgroup as read.
9543 If prefix argument ALL is non-nil, ticked and dormant articles will
9544 also be marked as read.
9545 If QUIETLY is non-nil, no questions will be asked.
9546 If TO-HERE is non-nil, it should be a point in the buffer.  All
9547 articles before (after, if REVERSE is set) this point will be marked as read.
9548 Note that this function will only catch up the unread article
9549 in the current summary buffer limitation.
9550 The number of articles marked as read is returned."
9551   (interactive "P")
9552   (prog1
9553       (save-excursion
9554         (when (or quietly
9555                   (not gnus-interactive-catchup) ;Without confirmation?
9556                   gnus-expert-user
9557                   (gnus-y-or-n-p
9558                    (if all
9559                        "Mark absolutely all articles as read? "
9560                      "Mark all unread articles as read? ")))
9561           (if (and not-mark
9562                    (not gnus-newsgroup-adaptive)
9563                    (not gnus-newsgroup-auto-expire)
9564                    (not gnus-suppress-duplicates)
9565                    (or (not gnus-use-cache)
9566                        (eq gnus-use-cache 'passive)))
9567               (progn
9568                 (when all
9569                   (setq gnus-newsgroup-marked nil
9570                         gnus-newsgroup-dormant nil))
9571                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9572             ;; We actually mark all articles as canceled, which we
9573             ;; have to do when using auto-expiry or adaptive scoring.
9574             (gnus-summary-show-all-threads)
9575             (if (and to-here reverse)
9576                 (progn
9577                   (goto-char to-here)
9578                   (while (and
9579                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9580                           (gnus-summary-find-next (not all) nil nil t))))
9581               (when (gnus-summary-first-subject (not all) t)
9582                 (while (and
9583                         (if to-here (< (point) to-here) t)
9584                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9585                         (gnus-summary-find-next (not all) nil nil t)))))
9586             (gnus-set-mode-line 'summary))
9587           t))
9588     (gnus-summary-position-point)))
9589
9590 (defun gnus-summary-catchup-to-here (&optional all)
9591   "Mark all unticked articles before the current one as read.
9592 If ALL is non-nil, also mark ticked and dormant articles as read."
9593   (interactive "P")
9594   (save-excursion
9595     (gnus-save-hidden-threads
9596       (let ((beg (point)))
9597         ;; We check that there are unread articles.
9598         (when (or all (gnus-summary-find-prev))
9599           (gnus-summary-catchup all t beg)))))
9600   (gnus-summary-position-point))
9601
9602 (defun gnus-summary-catchup-from-here (&optional all)
9603   "Mark all unticked articles after the current one as read.
9604 If ALL is non-nil, also mark ticked and dormant articles as read."
9605   (interactive "P")
9606   (save-excursion
9607     (gnus-save-hidden-threads
9608       (let ((beg (point)))
9609         ;; We check that there are unread articles.
9610         (when (or all (gnus-summary-find-next))
9611           (gnus-summary-catchup all t beg nil t)))))
9612
9613   (gnus-summary-position-point))
9614 (defun gnus-summary-catchup-all (&optional quietly)
9615   "Mark all articles in this newsgroup as read."
9616   (interactive "P")
9617   (gnus-summary-catchup t quietly))
9618
9619 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9620   "Mark all unread articles in this group as read, then exit.
9621 If prefix argument ALL is non-nil, all articles are marked as read.
9622 If QUIETLY is non-nil, no questions will be asked."
9623   (interactive "P")
9624   (when (gnus-summary-catchup all quietly nil 'fast)
9625     ;; Select next newsgroup or exit.
9626     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9627              (eq gnus-auto-select-next 'quietly))
9628         (gnus-summary-next-group nil)
9629       (gnus-summary-exit))))
9630
9631 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9632   "Mark all articles in this newsgroup as read, and then exit."
9633   (interactive "P")
9634   (gnus-summary-catchup-and-exit t quietly))
9635
9636 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9637   "Mark all articles in this group as read and select the next group.
9638 If given a prefix, mark all articles, unread as well as ticked, as
9639 read."
9640   (interactive "P")
9641   (save-excursion
9642     (gnus-summary-catchup all))
9643   (gnus-summary-next-group))
9644
9645 ;;;
9646 ;;; with article
9647 ;;;
9648
9649 (defmacro gnus-with-article (article &rest forms)
9650   "Select ARTICLE and perform FORMS in the original article buffer.
9651 Then replace the article with the result."
9652   `(progn
9653      ;; We don't want the article to be marked as read.
9654      (let (gnus-mark-article-hook)
9655        (gnus-summary-select-article t t nil ,article))
9656      (set-buffer gnus-original-article-buffer)
9657      ,@forms
9658      (if (not (gnus-check-backend-function
9659                'request-replace-article (car gnus-article-current)))
9660          (gnus-message 5 "Read-only group; not replacing")
9661        (unless (gnus-request-replace-article
9662                 ,article (car gnus-article-current)
9663                 (current-buffer) t)
9664          (error "Couldn't replace article")))
9665      ;; The cache and backlog have to be flushed somewhat.
9666      (when gnus-keep-backlog
9667        (gnus-backlog-remove-article
9668         (car gnus-article-current) (cdr gnus-article-current)))
9669      (when gnus-use-cache
9670        (gnus-cache-update-article
9671         (car gnus-article-current) (cdr gnus-article-current)))))
9672
9673 (put 'gnus-with-article 'lisp-indent-function 1)
9674 (put 'gnus-with-article 'edebug-form-spec '(form body))
9675
9676 ;; Thread-based commands.
9677
9678 (defun gnus-summary-articles-in-thread (&optional article)
9679   "Return a list of all articles in the current thread.
9680 If ARTICLE is non-nil, return all articles in the thread that starts
9681 with that article."
9682   (let* ((article (or article (gnus-summary-article-number)))
9683          (data (gnus-data-find-list article))
9684          (top-level (gnus-data-level (car data)))
9685          (top-subject
9686           (cond ((null gnus-thread-operation-ignore-subject)
9687                  (gnus-simplify-subject-re
9688                   (mail-header-subject (gnus-data-header (car data)))))
9689                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9690                  (gnus-simplify-subject-fuzzy
9691                   (mail-header-subject (gnus-data-header (car data)))))
9692                 (t nil)))
9693          (end-point (save-excursion
9694                       (if (gnus-summary-go-to-next-thread)
9695                           (point) (point-max))))
9696          articles)
9697     (while (and data
9698                 (< (gnus-data-pos (car data)) end-point))
9699       (when (or (not top-subject)
9700                 (string= top-subject
9701                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9702                              (gnus-simplify-subject-fuzzy
9703                               (mail-header-subject
9704                                (gnus-data-header (car data))))
9705                            (gnus-simplify-subject-re
9706                             (mail-header-subject
9707                              (gnus-data-header (car data)))))))
9708         (push (gnus-data-number (car data)) articles))
9709       (unless (and (setq data (cdr data))
9710                    (> (gnus-data-level (car data)) top-level))
9711         (setq data nil)))
9712     ;; Return the list of articles.
9713     (nreverse articles)))
9714
9715 (defun gnus-summary-rethread-current ()
9716   "Rethread the thread the current article is part of."
9717   (interactive)
9718   (let* ((gnus-show-threads t)
9719          (article (gnus-summary-article-number))
9720          (id (mail-header-id (gnus-summary-article-header)))
9721          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9722     (unless id
9723       (error "No article on the current line"))
9724     (gnus-rebuild-thread id)
9725     (gnus-summary-goto-subject article)))
9726
9727 (defun gnus-summary-reparent-thread ()
9728   "Make the current article child of the marked (or previous) article.
9729
9730 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9731 is non-nil or the Subject: of both articles are the same."
9732   (interactive)
9733   (unless (not (gnus-group-read-only-p))
9734     (error "The current newsgroup does not support article editing"))
9735   (unless (<= (length gnus-newsgroup-processable) 1)
9736     (error "No more than one article may be marked"))
9737   (save-window-excursion
9738     (let ((gnus-article-buffer " *reparent*")
9739           (current-article (gnus-summary-article-number))
9740           ;; First grab the marked article, otherwise one line up.
9741           (parent-article (if (not (null gnus-newsgroup-processable))
9742                               (car gnus-newsgroup-processable)
9743                             (save-excursion
9744                               (if (eq (forward-line -1) 0)
9745                                   (gnus-summary-article-number)
9746                                 (error "Beginning of summary buffer"))))))
9747       (unless (not (eq current-article parent-article))
9748         (error "An article may not be self-referential"))
9749       (let ((message-id (mail-header-id
9750                          (gnus-summary-article-header parent-article))))
9751         (unless (and message-id (not (equal message-id "")))
9752           (error "No message-id in desired parent"))
9753         (gnus-with-article current-article
9754           (save-restriction
9755             (goto-char (point-min))
9756             (message-narrow-to-head)
9757             (if (re-search-forward "^References: " nil t)
9758                 (progn
9759                   (re-search-forward "^[^ \t]" nil t)
9760                   (forward-line -1)
9761                   (end-of-line)
9762                   (insert " " message-id))
9763               (insert "References: " message-id "\n"))))
9764         (set-buffer gnus-summary-buffer)
9765         (gnus-summary-unmark-all-processable)
9766         (gnus-summary-update-article current-article)
9767         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9768             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
9769         (gnus-summary-rethread-current)
9770         (gnus-message 3 "Article %d is now the child of article %d"
9771                       current-article parent-article)))))
9772
9773 (defun gnus-summary-toggle-threads (&optional arg)
9774   "Toggle showing conversation threads.
9775 If ARG is positive number, turn showing conversation threads on."
9776   (interactive "P")
9777   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9778     (setq gnus-show-threads
9779           (if (null arg) (not gnus-show-threads)
9780             (> (prefix-numeric-value arg) 0)))
9781     (gnus-summary-prepare)
9782     (gnus-summary-goto-subject current)
9783     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9784     (gnus-summary-position-point)))
9785
9786 (defun gnus-summary-show-all-threads ()
9787   "Show all threads."
9788   (interactive)
9789   (save-excursion
9790     (let ((buffer-read-only nil))
9791       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9792   (gnus-summary-position-point))
9793
9794 (defun gnus-summary-show-thread ()
9795   "Show thread subtrees.
9796 Returns nil if no thread was there to be shown."
9797   (interactive)
9798   (let ((buffer-read-only nil)
9799         (orig (point))
9800         ;; first goto end then to beg, to have point at beg after let
9801         (end (progn (end-of-line) (point)))
9802         (beg (progn (beginning-of-line) (point))))
9803     (prog1
9804         ;; Any hidden lines here?
9805         (search-forward "\r" end t)
9806       (subst-char-in-region beg end ?\^M ?\n t)
9807       (goto-char orig)
9808       (gnus-summary-position-point))))
9809
9810 (defun gnus-summary-maybe-hide-threads ()
9811   "If requested, hide the threads that should be hidden."
9812   (when (and gnus-show-threads
9813              gnus-thread-hide-subtree)
9814     (gnus-summary-hide-all-threads
9815      (if (or (consp gnus-thread-hide-subtree)
9816              (gnus-functionp gnus-thread-hide-subtree))
9817          (gnus-make-predicate gnus-thread-hide-subtree)
9818        nil))))
9819
9820 ;;; Hiding predicates.
9821
9822 (defun gnus-article-unread-p (header)
9823   (memq (mail-header-number header) gnus-newsgroup-unreads))
9824
9825 (defun gnus-article-unseen-p (header)
9826   (memq (mail-header-number header) gnus-newsgroup-unseen))
9827
9828 (defun gnus-map-articles (predicate articles)
9829   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
9830   (apply 'gnus-or (mapcar predicate
9831                           (mapcar 'gnus-summary-article-header articles))))
9832
9833 (defun gnus-summary-hide-all-threads (&optional predicate)
9834   "Hide all thread subtrees.
9835 If PREDICATE is supplied, threads that satisfy this predicate
9836 will not be hidden."
9837   (interactive)
9838   (save-excursion
9839     (goto-char (point-min))
9840     (let ((end nil))
9841       (while (not end)
9842         (when (or (not predicate)
9843                   (gnus-map-articles
9844                    predicate (gnus-summary-article-children)))
9845             (gnus-summary-hide-thread))
9846         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
9847   (gnus-summary-position-point))
9848
9849 (defun gnus-summary-hide-thread ()
9850   "Hide thread subtrees.
9851 If PREDICATE is supplied, threads that satisfy this predicate
9852 will not be hidden.
9853 Returns nil if no threads were there to be hidden."
9854   (interactive)
9855   (let ((buffer-read-only nil)
9856         (start (point))
9857         (article (gnus-summary-article-number)))
9858     (goto-char start)
9859     ;; Go forward until either the buffer ends or the subthread
9860     ;; ends.
9861     (when (and (not (eobp))
9862                (or (zerop (gnus-summary-next-thread 1 t))
9863                    (goto-char (point-max))))
9864       (prog1
9865           (if (and (> (point) start)
9866                    (search-backward "\n" start t))
9867               (progn
9868                 (subst-char-in-region start (point) ?\n ?\^M)
9869                 (gnus-summary-goto-subject article))
9870             (goto-char start)
9871             nil)))))
9872
9873 (defun gnus-summary-go-to-next-thread (&optional previous)
9874   "Go to the same level (or less) next thread.
9875 If PREVIOUS is non-nil, go to previous thread instead.
9876 Return the article number moved to, or nil if moving was impossible."
9877   (let ((level (gnus-summary-thread-level))
9878         (way (if previous -1 1))
9879         (beg (point)))
9880     (forward-line way)
9881     (while (and (not (eobp))
9882                 (< level (gnus-summary-thread-level)))
9883       (forward-line way))
9884     (if (eobp)
9885         (progn
9886           (goto-char beg)
9887           nil)
9888       (setq beg (point))
9889       (prog1
9890           (gnus-summary-article-number)
9891         (goto-char beg)))))
9892
9893 (defun gnus-summary-next-thread (n &optional silent)
9894   "Go to the same level next N'th thread.
9895 If N is negative, search backward instead.
9896 Returns the difference between N and the number of skips actually
9897 done.
9898
9899 If SILENT, don't output messages."
9900   (interactive "p")
9901   (let ((backward (< n 0))
9902         (n (abs n)))
9903     (while (and (> n 0)
9904                 (gnus-summary-go-to-next-thread backward))
9905       (decf n))
9906     (unless silent
9907       (gnus-summary-position-point))
9908     (when (and (not silent) (/= 0 n))
9909       (gnus-message 7 "No more threads"))
9910     n))
9911
9912 (defun gnus-summary-prev-thread (n)
9913   "Go to the same level previous N'th thread.
9914 Returns the difference between N and the number of skips actually
9915 done."
9916   (interactive "p")
9917   (gnus-summary-next-thread (- n)))
9918
9919 (defun gnus-summary-go-down-thread ()
9920   "Go down one level in the current thread."
9921   (let ((children (gnus-summary-article-children)))
9922     (when children
9923       (gnus-summary-goto-subject (car children)))))
9924
9925 (defun gnus-summary-go-up-thread ()
9926   "Go up one level in the current thread."
9927   (let ((parent (gnus-summary-article-parent)))
9928     (when parent
9929       (gnus-summary-goto-subject parent))))
9930
9931 (defun gnus-summary-down-thread (n)
9932   "Go down thread N steps.
9933 If N is negative, go up instead.
9934 Returns the difference between N and how many steps down that were
9935 taken."
9936   (interactive "p")
9937   (let ((up (< n 0))
9938         (n (abs n)))
9939     (while (and (> n 0)
9940                 (if up (gnus-summary-go-up-thread)
9941                   (gnus-summary-go-down-thread)))
9942       (setq n (1- n)))
9943     (gnus-summary-position-point)
9944     (when (/= 0 n)
9945       (gnus-message 7 "Can't go further"))
9946     n))
9947
9948 (defun gnus-summary-up-thread (n)
9949   "Go up thread N steps.
9950 If N is negative, go down instead.
9951 Returns the difference between N and how many steps down that were
9952 taken."
9953   (interactive "p")
9954   (gnus-summary-down-thread (- n)))
9955
9956 (defun gnus-summary-top-thread ()
9957   "Go to the top of the thread."
9958   (interactive)
9959   (while (gnus-summary-go-up-thread))
9960   (gnus-summary-article-number))
9961
9962 (defun gnus-summary-kill-thread (&optional unmark)
9963   "Mark articles under current thread as read.
9964 If the prefix argument is positive, remove any kinds of marks.
9965 If the prefix argument is negative, tick articles instead."
9966   (interactive "P")
9967   (when unmark
9968     (setq unmark (prefix-numeric-value unmark)))
9969   (let ((articles (gnus-summary-articles-in-thread)))
9970     (save-excursion
9971       ;; Expand the thread.
9972       (gnus-summary-show-thread)
9973       ;; Mark all the articles.
9974       (while articles
9975         (gnus-summary-goto-subject (car articles))
9976         (cond ((null unmark)
9977                (gnus-summary-mark-article-as-read gnus-killed-mark))
9978               ((> unmark 0)
9979                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9980               (t
9981                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9982         (setq articles (cdr articles))))
9983     ;; Hide killed subtrees.
9984     (and (null unmark)
9985          gnus-thread-hide-killed
9986          (gnus-summary-hide-thread))
9987     ;; If marked as read, go to next unread subject.
9988     (when (null unmark)
9989       ;; Go to next unread subject.
9990       (gnus-summary-next-subject 1 t)))
9991   (gnus-set-mode-line 'summary))
9992
9993 ;; Summary sorting commands
9994
9995 (defun gnus-summary-sort-by-number (&optional reverse)
9996   "Sort the summary buffer by article number.
9997 Argument REVERSE means reverse order."
9998   (interactive "P")
9999   (gnus-summary-sort 'number reverse))
10000
10001 (defun gnus-summary-sort-by-author (&optional reverse)
10002   "Sort the summary buffer by author name alphabetically.
10003 If `case-fold-search' is non-nil, case of letters is ignored.
10004 Argument REVERSE means reverse order."
10005   (interactive "P")
10006   (gnus-summary-sort 'author reverse))
10007
10008 (defun gnus-summary-sort-by-subject (&optional reverse)
10009   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10010 If `case-fold-search' is non-nil, case of letters is ignored.
10011 Argument REVERSE means reverse order."
10012   (interactive "P")
10013   (gnus-summary-sort 'subject reverse))
10014
10015 (defun gnus-summary-sort-by-date (&optional reverse)
10016   "Sort the summary buffer by date.
10017 Argument REVERSE means reverse order."
10018   (interactive "P")
10019   (gnus-summary-sort 'date reverse))
10020
10021 (defun gnus-summary-sort-by-score (&optional reverse)
10022   "Sort the summary buffer by score.
10023 Argument REVERSE means reverse order."
10024   (interactive "P")
10025   (gnus-summary-sort 'score reverse))
10026
10027 (defun gnus-summary-sort-by-lines (&optional reverse)
10028   "Sort the summary buffer by the number of lines.
10029 Argument REVERSE means reverse order."
10030   (interactive "P")
10031   (gnus-summary-sort 'lines reverse))
10032
10033 (defun gnus-summary-sort-by-chars (&optional reverse)
10034   "Sort the summary buffer by article length.
10035 Argument REVERSE means reverse order."
10036   (interactive "P")
10037   (gnus-summary-sort 'chars reverse))
10038
10039 (defun gnus-summary-sort-by-original (&optional reverse)
10040   "Sort the summary buffer using the default sorting method.
10041 Argument REVERSE means reverse order."
10042   (interactive "P")
10043   (let* ((buffer-read-only)
10044          (gnus-summary-prepare-hook nil))
10045     ;; We do the sorting by regenerating the threads.
10046     (gnus-summary-prepare)
10047     ;; Hide subthreads if needed.
10048     (gnus-summary-maybe-hide-threads)))
10049
10050 (defun gnus-summary-sort (predicate reverse)
10051   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10052   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10053          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10054          (gnus-thread-sort-functions
10055           (if (not reverse)
10056               thread
10057             `(lambda (t1 t2)
10058                (,thread t2 t1))))
10059          (gnus-sort-gathered-threads-function
10060           gnus-thread-sort-functions)
10061          (gnus-article-sort-functions
10062           (if (not reverse)
10063               article
10064             `(lambda (t1 t2)
10065                (,article t2 t1))))
10066          (buffer-read-only)
10067          (gnus-summary-prepare-hook nil))
10068     ;; We do the sorting by regenerating the threads.
10069     (gnus-summary-prepare)
10070     ;; Hide subthreads if needed.
10071     (gnus-summary-maybe-hide-threads)))
10072
10073 ;; Summary saving commands.
10074
10075 (defun gnus-summary-save-article (&optional n not-saved)
10076   "Save the current article using the default saver function.
10077 If N is a positive number, save the N next articles.
10078 If N is a negative number, save the N previous articles.
10079 If N is nil and any articles have been marked with the process mark,
10080 save those articles instead.
10081 The variable `gnus-default-article-saver' specifies the saver function."
10082   (interactive "P")
10083   (let* ((articles (gnus-summary-work-articles n))
10084          (save-buffer (save-excursion
10085                         (nnheader-set-temp-buffer " *Gnus Save*")))
10086          (num (length articles))
10087          header file)
10088     (dolist (article articles)
10089       (setq header (gnus-summary-article-header article))
10090       (if (not (vectorp header))
10091           ;; This is a pseudo-article.
10092           (if (assq 'name header)
10093               (gnus-copy-file (cdr (assq 'name header)))
10094             (gnus-message 1 "Article %d is unsaveable" article))
10095         ;; This is a real article.
10096         (save-window-excursion
10097           (let ((gnus-display-mime-function nil)
10098                 (gnus-article-prepare-hook nil))
10099             (gnus-summary-select-article t nil nil article)))
10100         (save-excursion
10101           (set-buffer save-buffer)
10102           (erase-buffer)
10103           (insert-buffer-substring gnus-original-article-buffer))
10104         (setq file (gnus-article-save save-buffer file num))
10105         (gnus-summary-remove-process-mark article)
10106         (unless not-saved
10107           (gnus-summary-set-saved-mark article))))
10108     (gnus-kill-buffer save-buffer)
10109     (gnus-summary-position-point)
10110     (gnus-set-mode-line 'summary)
10111     n))
10112
10113 (defun gnus-summary-pipe-output (&optional arg)
10114   "Pipe the current article to a subprocess.
10115 If N is a positive number, pipe the N next articles.
10116 If N is a negative number, pipe the N previous articles.
10117 If N is nil and any articles have been marked with the process mark,
10118 pipe those articles instead."
10119   (interactive "P")
10120   (require 'gnus-art)
10121   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
10122     (gnus-summary-save-article arg t))
10123   (let ((buffer (get-buffer "*Shell Command Output*")))
10124     (if (and buffer
10125              (with-current-buffer buffer (> (point-max) (point-min))))
10126         (gnus-configure-windows 'pipe))))
10127
10128 (defun gnus-summary-save-article-mail (&optional arg)
10129   "Append the current article to an mail file.
10130 If N is a positive number, save the N next articles.
10131 If N is a negative number, save the N previous articles.
10132 If N is nil and any articles have been marked with the process mark,
10133 save those articles instead."
10134   (interactive "P")
10135   (require 'gnus-art)
10136   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10137     (gnus-summary-save-article arg)))
10138
10139 (defun gnus-summary-save-article-rmail (&optional arg)
10140   "Append the current article to an rmail file.
10141 If N is a positive number, save the N next articles.
10142 If N is a negative number, save the N previous articles.
10143 If N is nil and any articles have been marked with the process mark,
10144 save those articles instead."
10145   (interactive "P")
10146   (require 'gnus-art)
10147   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10148     (gnus-summary-save-article arg)))
10149
10150 (defun gnus-summary-save-article-file (&optional arg)
10151   "Append the current article to a file.
10152 If N is a positive number, save the N next articles.
10153 If N is a negative number, save the N previous articles.
10154 If N is nil and any articles have been marked with the process mark,
10155 save those articles instead."
10156   (interactive "P")
10157   (require 'gnus-art)
10158   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10159     (gnus-summary-save-article arg)))
10160
10161 (defun gnus-summary-write-article-file (&optional arg)
10162   "Write the current article to a file, deleting the previous file.
10163 If N is a positive number, save the N next articles.
10164 If N is a negative number, save the N previous articles.
10165 If N is nil and any articles have been marked with the process mark,
10166 save those articles instead."
10167   (interactive "P")
10168   (require 'gnus-art)
10169   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10170     (gnus-summary-save-article arg)))
10171
10172 (defun gnus-summary-save-article-body-file (&optional arg)
10173   "Append the current article body to a file.
10174 If N is a positive number, save the N next articles.
10175 If N is a negative number, save the N previous articles.
10176 If N is nil and any articles have been marked with the process mark,
10177 save those articles instead."
10178   (interactive "P")
10179   (require 'gnus-art)
10180   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10181     (gnus-summary-save-article arg)))
10182
10183 (defun gnus-summary-muttprint (&optional arg)
10184   "Print the current article using Muttprint.
10185 If N is a positive number, save the N next articles.
10186 If N is a negative number, save the N previous articles.
10187 If N is nil and any articles have been marked with the process mark,
10188 save those articles instead."
10189   (interactive "P")
10190   (require 'gnus-art)
10191   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10192     (gnus-summary-save-article arg t)))
10193
10194 (defun gnus-summary-pipe-message (program)
10195   "Pipe the current article through PROGRAM."
10196   (interactive "sProgram: ")
10197   (gnus-summary-select-article)
10198   (let ((mail-header-separator ""))
10199     (gnus-eval-in-buffer-window gnus-article-buffer
10200       (save-restriction
10201         (widen)
10202         (let ((start (window-start))
10203               buffer-read-only)
10204           (message-pipe-buffer-body program)
10205           (set-window-start (get-buffer-window (current-buffer)) start))))))
10206
10207 (defun gnus-get-split-value (methods)
10208   "Return a value based on the split METHODS."
10209   (let (split-name method result match)
10210     (when methods
10211       (save-excursion
10212         (set-buffer gnus-original-article-buffer)
10213         (save-restriction
10214           (nnheader-narrow-to-headers)
10215           (while (and methods (not split-name))
10216             (goto-char (point-min))
10217             (setq method (pop methods))
10218             (setq match (car method))
10219             (when (cond
10220                    ((stringp match)
10221                     ;; Regular expression.
10222                     (ignore-errors
10223                       (re-search-forward match nil t)))
10224                    ((gnus-functionp match)
10225                     ;; Function.
10226                     (save-restriction
10227                       (widen)
10228                       (setq result (funcall match gnus-newsgroup-name))))
10229                    ((consp match)
10230                     ;; Form.
10231                     (save-restriction
10232                       (widen)
10233                       (setq result (eval match)))))
10234               (setq split-name (cdr method))
10235               (cond ((stringp result)
10236                      (push (expand-file-name
10237                             result gnus-article-save-directory)
10238                            split-name))
10239                     ((consp result)
10240                      (setq split-name (append result split-name)))))))))
10241     (nreverse split-name)))
10242
10243 (defun gnus-valid-move-group-p (group)
10244   (and (boundp group)
10245        (symbol-name group)
10246        (symbol-value group)
10247        (gnus-get-function (gnus-find-method-for-group
10248                            (symbol-name group)) 'request-accept-article t)))
10249
10250 (defun gnus-read-move-group-name (prompt default articles prefix)
10251   "Read a group name."
10252   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10253          (minibuffer-confirm-incomplete nil) ; XEmacs
10254          (prom
10255           (format "%s %s to:"
10256                   prompt
10257                   (if (> (length articles) 1)
10258                       (format "these %d articles" (length articles))
10259                     "this article")))
10260          (to-newsgroup
10261           (cond
10262            ((null split-name)
10263             (gnus-completing-read-with-default
10264              default prom
10265              gnus-active-hashtb
10266              'gnus-valid-move-group-p
10267              nil prefix
10268              'gnus-group-history))
10269            ((= 1 (length split-name))
10270             (gnus-completing-read-with-default
10271              (car split-name) prom
10272              gnus-active-hashtb
10273              'gnus-valid-move-group-p
10274              nil nil
10275              'gnus-group-history))
10276            (t
10277             (gnus-completing-read-with-default
10278              nil prom
10279              (mapcar (lambda (el) (list el))
10280                      (nreverse split-name))
10281              nil nil nil
10282              'gnus-group-history))))
10283          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10284     (when to-newsgroup
10285       (if (or (string= to-newsgroup "")
10286               (string= to-newsgroup prefix))
10287           (setq to-newsgroup default))
10288       (unless to-newsgroup
10289         (error "No group name entered"))
10290       (or (gnus-active to-newsgroup)
10291           (gnus-activate-group to-newsgroup nil nil to-method)
10292           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10293                                      to-newsgroup))
10294               (or (and (gnus-request-create-group to-newsgroup to-method)
10295                        (gnus-activate-group
10296                         to-newsgroup nil nil to-method)
10297                        (gnus-subscribe-group to-newsgroup))
10298                   (error "Couldn't create group %s" to-newsgroup)))
10299           (error "No such group: %s" to-newsgroup)))
10300     to-newsgroup))
10301
10302 (defun gnus-summary-save-parts (type dir n &optional reverse)
10303   "Save parts matching TYPE to DIR.
10304 If REVERSE, save parts that do not match TYPE."
10305   (interactive
10306    (list (read-string "Save parts of type: "
10307                       (or (car gnus-summary-save-parts-type-history)
10308                           gnus-summary-save-parts-default-mime)
10309                       'gnus-summary-save-parts-type-history)
10310          (setq gnus-summary-save-parts-last-directory
10311                (read-file-name "Save to directory: "
10312                                gnus-summary-save-parts-last-directory
10313                                nil t))
10314          current-prefix-arg))
10315   (gnus-summary-iterate n
10316     (let ((gnus-display-mime-function nil)
10317           (gnus-inhibit-treatment t))
10318       (gnus-summary-select-article))
10319     (save-excursion
10320       (set-buffer gnus-article-buffer)
10321       (let ((handles (or gnus-article-mime-handles
10322                          (mm-dissect-buffer nil gnus-article-loose-mime)
10323                          (mm-uu-dissect))))
10324         (when handles
10325           (gnus-summary-save-parts-1 type dir handles reverse)
10326           (unless gnus-article-mime-handles ;; Don't destroy this case.
10327             (mm-destroy-parts handles)))))))
10328
10329 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10330   (if (stringp (car handle))
10331       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10332               (cdr handle))
10333     (when (if reverse
10334               (not (string-match type (mm-handle-media-type handle)))
10335             (string-match type (mm-handle-media-type handle)))
10336       (let ((file (expand-file-name
10337                    (file-name-nondirectory
10338                     (or
10339                      (mail-content-type-get
10340                       (mm-handle-disposition handle) 'filename)
10341                      (concat gnus-newsgroup-name
10342                              "." (number-to-string
10343                                   (cdr gnus-article-current)))))
10344                    dir)))
10345         (unless (file-exists-p file)
10346           (mm-save-part-to-file handle file))))))
10347
10348 ;; Summary extract commands
10349
10350 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10351   (let ((buffer-read-only nil)
10352         (article (gnus-summary-article-number))
10353         after-article b e)
10354     (unless (gnus-summary-goto-subject article)
10355       (error "No such article: %d" article))
10356     (gnus-summary-position-point)
10357     ;; If all commands are to be bunched up on one line, we collect
10358     ;; them here.
10359     (unless gnus-view-pseudos-separately
10360       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10361             files action)
10362         (while ps
10363           (setq action (cdr (assq 'action (car ps))))
10364           (setq files (list (cdr (assq 'name (car ps)))))
10365           (while (and ps (cdr ps)
10366                       (string= (or action "1")
10367                                (or (cdr (assq 'action (cadr ps))) "2")))
10368             (push (cdr (assq 'name (cadr ps))) files)
10369             (setcdr ps (cddr ps)))
10370           (when files
10371             (when (not (string-match "%s" action))
10372               (push " " files))
10373             (push " " files)
10374             (when (assq 'execute (car ps))
10375               (setcdr (assq 'execute (car ps))
10376                       (funcall (if (string-match "%s" action)
10377                                    'format 'concat)
10378                                action
10379                                (mapconcat
10380                                 (lambda (f)
10381                                   (if (equal f " ")
10382                                       f
10383                                     (mm-quote-arg f)))
10384                                 files " ")))))
10385           (setq ps (cdr ps)))))
10386     (if (and gnus-view-pseudos (not not-view))
10387         (while pslist
10388           (when (assq 'execute (car pslist))
10389             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10390                                   (eq gnus-view-pseudos 'not-confirm)))
10391           (setq pslist (cdr pslist)))
10392       (save-excursion
10393         (while pslist
10394           (setq after-article (or (cdr (assq 'article (car pslist)))
10395                                   (gnus-summary-article-number)))
10396           (gnus-summary-goto-subject after-article)
10397           (forward-line 1)
10398           (setq b (point))
10399           (insert "    " (file-name-nondirectory
10400                           (cdr (assq 'name (car pslist))))
10401                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10402           (setq e (point))
10403           (forward-line -1)             ; back to `b'
10404           (gnus-add-text-properties
10405            b (1- e) (list 'gnus-number gnus-reffed-article-number
10406                           gnus-mouse-face-prop gnus-mouse-face))
10407           (gnus-data-enter
10408            after-article gnus-reffed-article-number
10409            gnus-unread-mark b (car pslist) 0 (- e b))
10410           (setq gnus-newsgroup-unreads
10411                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10412                                          gnus-reffed-article-number))
10413           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10414           (setq pslist (cdr pslist)))))))
10415
10416 (defun gnus-pseudos< (p1 p2)
10417   (let ((c1 (cdr (assq 'action p1)))
10418         (c2 (cdr (assq 'action p2))))
10419     (and c1 c2 (string< c1 c2))))
10420
10421 (defun gnus-request-pseudo-article (props)
10422   (cond ((assq 'execute props)
10423          (gnus-execute-command (cdr (assq 'execute props)))))
10424   (let ((gnus-current-article (gnus-summary-article-number)))
10425     (gnus-run-hooks 'gnus-mark-article-hook)))
10426
10427 (defun gnus-execute-command (command &optional automatic)
10428   (save-excursion
10429     (gnus-article-setup-buffer)
10430     (set-buffer gnus-article-buffer)
10431     (setq buffer-read-only nil)
10432     (let ((command (if automatic command
10433                      (read-string "Command: " (cons command 0)))))
10434       (erase-buffer)
10435       (insert "$ " command "\n\n")
10436       (if gnus-view-pseudo-asynchronously
10437           (start-process "gnus-execute" (current-buffer) shell-file-name
10438                          shell-command-switch command)
10439         (call-process shell-file-name nil t nil
10440                       shell-command-switch command)))))
10441
10442 ;; Summary kill commands.
10443
10444 (defun gnus-summary-edit-global-kill (article)
10445   "Edit the \"global\" kill file."
10446   (interactive (list (gnus-summary-article-number)))
10447   (gnus-group-edit-global-kill article))
10448
10449 (defun gnus-summary-edit-local-kill ()
10450   "Edit a local kill file applied to the current newsgroup."
10451   (interactive)
10452   (setq gnus-current-headers (gnus-summary-article-header))
10453   (gnus-group-edit-local-kill
10454    (gnus-summary-article-number) gnus-newsgroup-name))
10455
10456 ;;; Header reading.
10457
10458 (defun gnus-read-header (id &optional header)
10459   "Read the headers of article ID and enter them into the Gnus system."
10460   (let ((group gnus-newsgroup-name)
10461         (gnus-override-method
10462          (or
10463           gnus-override-method
10464           (and (gnus-news-group-p gnus-newsgroup-name)
10465                (car (gnus-refer-article-methods)))))
10466         where)
10467     ;; First we check to see whether the header in question is already
10468     ;; fetched.
10469     (if (stringp id)
10470         ;; This is a Message-ID.
10471         (setq header (or header (gnus-id-to-header id)))
10472       ;; This is an article number.
10473       (setq header (or header (gnus-summary-article-header id))))
10474     (if (and header
10475              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10476         ;; We have found the header.
10477         header
10478       ;; If this is a sparse article, we have to nix out its
10479       ;; previous entry in the thread hashtb.
10480       (when (and header
10481                  (gnus-summary-article-sparse-p (mail-header-number header)))
10482         (let* ((parent (gnus-parent-id (mail-header-references header)))
10483                (thread (and parent (gnus-id-to-thread parent))))
10484           (when thread
10485             (delq (assq header thread) thread))))
10486       ;; We have to really fetch the header to this article.
10487       (save-excursion
10488         (set-buffer nntp-server-buffer)
10489         (when (setq where (gnus-request-head id group))
10490           (nnheader-fold-continuation-lines)
10491           (goto-char (point-max))
10492           (insert ".\n")
10493           (goto-char (point-min))
10494           (insert "211 ")
10495           (princ (cond
10496                   ((numberp id) id)
10497                   ((cdr where) (cdr where))
10498                   (header (mail-header-number header))
10499                   (t gnus-reffed-article-number))
10500                  (current-buffer))
10501           (insert " Article retrieved.\n"))
10502         (if (or (not where)
10503                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10504             ()                          ; Malformed head.
10505           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10506             (when (and (stringp id)
10507                        (not (string= (gnus-group-real-name group)
10508                                      (car where))))
10509               ;; If we fetched by Message-ID and the article came
10510               ;; from a different group, we fudge some bogus article
10511               ;; numbers for this article.
10512               (mail-header-set-number header gnus-reffed-article-number))
10513             (save-excursion
10514               (set-buffer gnus-summary-buffer)
10515               (decf gnus-reffed-article-number)
10516               (gnus-remove-header (mail-header-number header))
10517               (push header gnus-newsgroup-headers)
10518               (setq gnus-current-headers header)
10519               (push (mail-header-number header) gnus-newsgroup-limit)))
10520           header)))))
10521
10522 (defun gnus-remove-header (number)
10523   "Remove header NUMBER from `gnus-newsgroup-headers'."
10524   (if (and gnus-newsgroup-headers
10525            (= number (mail-header-number (car gnus-newsgroup-headers))))
10526       (pop gnus-newsgroup-headers)
10527     (let ((headers gnus-newsgroup-headers))
10528       (while (and (cdr headers)
10529                   (not (= number (mail-header-number (cadr headers)))))
10530         (pop headers))
10531       (when (cdr headers)
10532         (setcdr headers (cddr headers))))))
10533
10534 ;;;
10535 ;;; summary highlights
10536 ;;;
10537
10538 (defun gnus-highlight-selected-summary ()
10539   "Highlight selected article in summary buffer."
10540   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10541   (when gnus-summary-selected-face
10542     (save-excursion
10543       (let* ((beg (progn (beginning-of-line) (point)))
10544              (end (progn (end-of-line) (point)))
10545              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10546              (from (if (get-text-property beg gnus-mouse-face-prop)
10547                        beg
10548                      (or (next-single-property-change
10549                           beg gnus-mouse-face-prop nil end)
10550                          beg)))
10551              (to
10552               (if (= from end)
10553                   (- from 2)
10554                 (or (next-single-property-change
10555                      from gnus-mouse-face-prop nil end)
10556                     end))))
10557         ;; If no mouse-face prop on line we will have to = from = end,
10558         ;; so we highlight the entire line instead.
10559         (when (= (+ to 2) from)
10560           (setq from beg)
10561           (setq to end))
10562         (if gnus-newsgroup-selected-overlay
10563             ;; Move old overlay.
10564             (gnus-move-overlay
10565              gnus-newsgroup-selected-overlay from to (current-buffer))
10566           ;; Create new overlay.
10567           (gnus-overlay-put
10568            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10569            'face gnus-summary-selected-face))))))
10570
10571 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10572 (defun gnus-summary-highlight-line ()
10573   "Highlight current line according to `gnus-summary-highlight'."
10574   (let* ((list gnus-summary-highlight)
10575          (p (point))
10576          (end (progn (end-of-line) (point)))
10577          ;; now find out where the line starts and leave point there.
10578          (beg (progn (beginning-of-line) (point)))
10579          (article (gnus-summary-article-number))
10580          (score (or (cdr (assq (or article gnus-current-article)
10581                                gnus-newsgroup-scored))
10582                     gnus-summary-default-score 0))
10583          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10584          (inhibit-read-only t))
10585     ;; Eval the cars of the lists until we find a match.
10586     (let ((default gnus-summary-default-score)
10587           (default-high gnus-summary-default-high-score)
10588           (default-low gnus-summary-default-low-score))
10589       (while (and list
10590                   (not (eval (caar list))))
10591         (setq list (cdr list))))
10592     (let ((face (cdar list)))
10593       (unless (eq face (get-text-property beg 'face))
10594         (gnus-put-text-property-excluding-characters-with-faces
10595          beg end 'face
10596          (setq face (if (boundp face) (symbol-value face) face)))
10597         (when gnus-summary-highlight-line-function
10598           (funcall gnus-summary-highlight-line-function article face))))
10599     (goto-char p)))
10600
10601 (defun gnus-update-read-articles (group unread &optional compute)
10602   "Update the list of read articles in GROUP.
10603 UNREAD is a sorted list."
10604   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10605          (entry (gnus-gethash group gnus-newsrc-hashtb))
10606          (info (nth 2 entry))
10607          (prev 1)
10608          read)
10609     (if (or (not info) (not active))
10610         ;; There is no info on this group if it was, in fact,
10611         ;; killed.  Gnus stores no information on killed groups, so
10612         ;; there's nothing to be done.
10613         ;; One could store the information somewhere temporarily,
10614         ;; perhaps...  Hmmm...
10615         ()
10616       ;; Remove any negative articles numbers.
10617       (while (and unread (< (car unread) 0))
10618         (setq unread (cdr unread)))
10619       ;; Remove any expired article numbers
10620       (while (and unread (< (car unread) (car active)))
10621         (setq unread (cdr unread)))
10622       ;; Compute the ranges of read articles by looking at the list of
10623       ;; unread articles.
10624       (while unread
10625         (when (/= (car unread) prev)
10626           (push (if (= prev (1- (car unread))) prev
10627                   (cons prev (1- (car unread))))
10628                 read))
10629         (setq prev (1+ (car unread)))
10630         (setq unread (cdr unread)))
10631       (when (<= prev (cdr active))
10632         (push (cons prev (cdr active)) read))
10633       (setq read (if (> (length read) 1) (nreverse read) read))
10634       (if compute
10635           read
10636         (save-excursion
10637           (let (setmarkundo)
10638             ;; Propagate the read marks to the backend.
10639             (when (gnus-check-backend-function 'request-set-mark group)
10640               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10641                     (add (gnus-remove-from-range read (gnus-info-read info))))
10642                 (when (or add del)
10643                   (unless (gnus-check-group group)
10644                     (error "Can't open server for %s" group))
10645                   (gnus-request-set-mark
10646                    group (delq nil (list (if add (list add 'add '(read)))
10647                                          (if del (list del 'del '(read))))))
10648                   (setq setmarkundo
10649                         `(gnus-request-set-mark
10650                           ,group
10651                           ',(delq nil (list
10652                                        (if del (list del 'add '(read)))
10653                                        (if add (list add 'del '(read))))))))))
10654             (set-buffer gnus-group-buffer)
10655             (gnus-undo-register
10656               `(progn
10657                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10658                  (gnus-info-set-read ',info ',(gnus-info-read info))
10659                  (gnus-get-unread-articles-in-group ',info
10660                                                     (gnus-active ,group))
10661                  (gnus-group-update-group ,group t)
10662                  ,setmarkundo))))
10663         ;; Enter this list into the group info.
10664         (gnus-info-set-read info read)
10665         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10666         (gnus-get-unread-articles-in-group info (gnus-active group))
10667         t))))
10668
10669 (defun gnus-offer-save-summaries ()
10670   "Offer to save all active summary buffers."
10671   (let (buffers)
10672     ;; Go through all buffers and find all summaries.
10673     (dolist (buffer (buffer-list))
10674       (when (and (setq buffer (buffer-name buffer))
10675                  (string-match "Summary" buffer)
10676                  (save-excursion
10677                    (set-buffer buffer)
10678                    ;; We check that this is, indeed, a summary buffer.
10679                    (and (eq major-mode 'gnus-summary-mode)
10680                         ;; Also make sure this isn't bogus.
10681                         gnus-newsgroup-prepared
10682                         ;; Also make sure that this isn't a
10683                         ;; dead summary buffer.
10684                         (not gnus-dead-summary-mode))))
10685         (push buffer buffers)))
10686     ;; Go through all these summary buffers and offer to save them.
10687     (when buffers
10688       (save-excursion
10689         (map-y-or-n-p
10690          "Update summary buffer %s? "
10691          (lambda (buf)
10692            (switch-to-buffer buf)
10693            (gnus-summary-exit))
10694          buffers)))))
10695
10696 (defun gnus-summary-setup-default-charset ()
10697   "Setup newsgroup default charset."
10698   (if (equal gnus-newsgroup-name "nndraft:drafts")
10699       (setq gnus-newsgroup-charset nil)
10700     (let* ((ignored-charsets
10701             (or gnus-newsgroup-ephemeral-ignored-charsets
10702                 (append
10703                  (and gnus-newsgroup-name
10704                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10705                  gnus-newsgroup-ignored-charsets))))
10706       (setq gnus-newsgroup-charset
10707             (or gnus-newsgroup-ephemeral-charset
10708                 (and gnus-newsgroup-name
10709                      (gnus-parameter-charset gnus-newsgroup-name))
10710                 gnus-default-charset))
10711       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10712            ignored-charsets))))
10713
10714 ;;;
10715 ;;; Mime Commands
10716 ;;;
10717
10718 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10719   "Display the current article buffer fully MIME-buttonized.
10720 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10721 treated as multipart/mixed."
10722   (interactive "P")
10723   (require 'gnus-art)
10724   (let ((gnus-unbuttonized-mime-types nil)
10725         (gnus-mime-display-multipart-as-mixed show-all-parts))
10726     (gnus-summary-show-article)))
10727
10728 (defun gnus-summary-repair-multipart (article)
10729   "Add a Content-Type header to a multipart article without one."
10730   (interactive (list (gnus-summary-article-number)))
10731   (gnus-with-article article
10732     (message-narrow-to-head)
10733     (message-remove-header "Mime-Version")
10734     (goto-char (point-max))
10735     (insert "Mime-Version: 1.0\n")
10736     (widen)
10737     (when (search-forward "\n--" nil t)
10738       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10739         (message-narrow-to-head)
10740         (message-remove-header "Content-Type")
10741         (goto-char (point-max))
10742         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10743                         separator))
10744         (widen))))
10745   (let (gnus-mark-article-hook)
10746     (gnus-summary-select-article t t nil article)))
10747
10748 (defun gnus-summary-toggle-display-buttonized ()
10749   "Toggle the buttonizing of the article buffer."
10750   (interactive)
10751   (require 'gnus-art)
10752   (if (setq gnus-inhibit-mime-unbuttonizing
10753             (not gnus-inhibit-mime-unbuttonizing))
10754       (let ((gnus-unbuttonized-mime-types nil))
10755         (gnus-summary-show-article))
10756     (gnus-summary-show-article)))
10757
10758 ;;;
10759 ;;; Generic summary marking commands
10760 ;;;
10761
10762 (defvar gnus-summary-marking-alist
10763   '((read gnus-del-mark "d")
10764     (unread gnus-unread-mark "u")
10765     (ticked gnus-ticked-mark "!")
10766     (dormant gnus-dormant-mark "?")
10767     (expirable gnus-expirable-mark "e"))
10768   "An alist of names/marks/keystrokes.")
10769
10770 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10771 (defvar gnus-summary-mark-map)
10772
10773 (defun gnus-summary-make-all-marking-commands ()
10774   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10775   (dolist (elem gnus-summary-marking-alist)
10776     (apply 'gnus-summary-make-marking-command elem)))
10777
10778 (defun gnus-summary-make-marking-command (name mark keystroke)
10779   (let ((map (make-sparse-keymap)))
10780     (define-key gnus-summary-generic-mark-map keystroke map)
10781     (dolist (lway `((next "next" next nil "n")
10782                     (next-unread "next unread" next t "N")
10783                     (prev "previous" prev nil "p")
10784                     (prev-unread "previous unread" prev t "P")
10785                     (nomove "" nil nil ,keystroke)))
10786       (let ((func (gnus-summary-make-marking-command-1
10787                    mark (car lway) lway name)))
10788         (setq func (eval func))
10789         (define-key map (nth 4 lway) func)))))
10790
10791 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10792   `(defun ,(intern
10793             (format "gnus-summary-put-mark-as-%s%s"
10794                     name (if (eq way 'nomove)
10795                              ""
10796                            (concat "-" (symbol-name way)))))
10797      (n)
10798      ,(format
10799        "Mark the current article as %s%s.
10800 If N, the prefix, then repeat N times.
10801 If N is negative, move in reverse order.
10802 The difference between N and the actual number of articles marked is
10803 returned."
10804        name (cadr lway))
10805      (interactive "p")
10806      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10807
10808 (defun gnus-summary-generic-mark (n mark move unread)
10809   "Mark N articles with MARK."
10810   (unless (eq major-mode 'gnus-summary-mode)
10811     (error "This command can only be used in the summary buffer"))
10812   (gnus-summary-show-thread)
10813   (let ((nummove
10814          (cond
10815           ((eq move 'next) 1)
10816           ((eq move 'prev) -1)
10817           (t 0))))
10818     (if (zerop nummove)
10819         (setq n 1)
10820       (when (< n 0)
10821         (setq n (abs n)
10822               nummove (* -1 nummove))))
10823     (while (and (> n 0)
10824                 (gnus-summary-mark-article nil mark)
10825                 (zerop (gnus-summary-next-subject nummove unread t)))
10826       (setq n (1- n)))
10827     (when (/= 0 n)
10828       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10829     (gnus-summary-recenter)
10830     (gnus-summary-position-point)
10831     (gnus-set-mode-line 'summary)
10832     n))
10833
10834 (defun gnus-summary-insert-articles (articles)
10835   (when (setq articles
10836               (gnus-sorted-difference articles
10837                                       (mapcar (lambda (h)
10838                                                 (mail-header-number h))
10839                                               gnus-newsgroup-headers)))
10840     (setq gnus-newsgroup-headers
10841           (merge 'list
10842                  gnus-newsgroup-headers
10843                  (gnus-fetch-headers articles)
10844                  'gnus-article-sort-by-number))
10845     ;; Suppress duplicates?
10846     (when gnus-suppress-duplicates
10847       (gnus-dup-suppress-articles))
10848
10849     ;; We might want to build some more threads first.
10850     (when (and gnus-fetch-old-headers
10851                (eq gnus-headers-retrieved-by 'nov))
10852       (if (eq gnus-fetch-old-headers 'invisible)
10853           (gnus-build-all-threads)
10854         (gnus-build-old-threads)))
10855     ;; Let the Gnus agent mark articles as read.
10856     (when gnus-agent
10857       (gnus-agent-get-undownloaded-list))
10858     ;; Remove list identifiers from subject
10859     (when gnus-list-identifiers
10860       (gnus-summary-remove-list-identifiers))
10861     ;; First and last article in this newsgroup.
10862     (when gnus-newsgroup-headers
10863       (setq gnus-newsgroup-begin
10864             (mail-header-number (car gnus-newsgroup-headers))
10865             gnus-newsgroup-end
10866             (mail-header-number
10867              (gnus-last-element gnus-newsgroup-headers))))
10868     (when gnus-use-scoring
10869       (gnus-possibly-score-headers))))
10870
10871 (defun gnus-summary-insert-old-articles (&optional all)
10872   "Insert all old articles in this group.
10873 If ALL is non-nil, already read articles become readable.
10874 If ALL is a number, fetch this number of articles."
10875   (interactive "P")
10876   (prog1
10877       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
10878             older len)
10879         (setq older
10880               (gnus-sorted-difference
10881                (gnus-uncompress-range (list gnus-newsgroup-active))
10882                old))
10883         (setq len (length older))
10884         (cond
10885          ((null older) nil)
10886          ((numberp all)
10887           (if (< all len)
10888               (setq older (last older all))))
10889          (all nil)
10890          (t
10891           (if (and (numberp gnus-large-newsgroup)
10892                    (> len gnus-large-newsgroup))
10893               (let ((input
10894                      (read-string
10895                       (format
10896                        "How many articles from %s (default %d): "
10897                        (gnus-limit-string
10898                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10899                        len))))
10900                 (unless (string-match "^[ \t]*$" input)
10901                   (setq all (string-to-number input))
10902                   (if (< all len)
10903                       (setq older (last older all))))))))
10904         (if (not older)
10905             (message "No old news.")
10906           (gnus-summary-insert-articles older)
10907           (gnus-summary-limit (gnus-sorted-nunion old older))))
10908     (gnus-summary-position-point)))
10909
10910 (defun gnus-summary-insert-new-articles ()
10911   "Insert all new articles in this group."
10912   (interactive)
10913   (prog1
10914       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
10915             (old-active gnus-newsgroup-active)
10916             (nnmail-fetched-sources (list t))
10917             i new)
10918         (setq gnus-newsgroup-active
10919               (gnus-activate-group gnus-newsgroup-name 'scan))
10920         (setq i (1+ (cdr old-active)))
10921         (while (<= i (cdr gnus-newsgroup-active))
10922           (push i new)
10923           (incf i))
10924         (if (not new)
10925             (message "No gnus is bad news.")
10926           (setq new (nreverse new))
10927           (gnus-summary-insert-articles new)
10928           (setq gnus-newsgroup-unreads
10929                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
10930           (gnus-summary-limit (gnus-sorted-nunion old new))))
10931     (gnus-summary-position-point)))
10932
10933 (gnus-summary-make-all-marking-commands)
10934
10935 (gnus-ems-redefine)
10936
10937 (provide 'gnus-sum)
10938
10939 (run-hooks 'gnus-sum-load-hook)
10940
10941 ;;; gnus-sum.el ends here