* gnus-sum.el (gnus-compute-unseen-list): Use new optimized
[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   :group 'gnus-summary
1017   :type 'string)
1018
1019 ;;; Internal variables
1020
1021 (defvar gnus-summary-display-cache nil)
1022 (defvar gnus-article-mime-handles nil)
1023 (defvar gnus-article-decoded-p nil)
1024 (defvar gnus-article-charset nil)
1025 (defvar gnus-article-ignored-charsets nil)
1026 (defvar gnus-scores-exclude-files nil)
1027 (defvar gnus-page-broken nil)
1028 (defvar gnus-inhibit-mime-unbuttonizing nil)
1029
1030 (defvar gnus-original-article nil)
1031 (defvar gnus-article-internal-prepare-hook nil)
1032 (defvar gnus-newsgroup-process-stack nil)
1033
1034 (defvar gnus-thread-indent-array nil)
1035 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1036 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1037   "Function called to sort the articles within a thread after it has been gathered together.")
1038
1039 (defvar gnus-summary-save-parts-type-history nil)
1040 (defvar gnus-summary-save-parts-last-directory nil)
1041
1042 (defvar gnus-summary-save-parts-type-history nil)
1043 (defvar gnus-summary-save-parts-last-directory nil)
1044
1045 ;; Avoid highlighting in kill files.
1046 (defvar gnus-summary-inhibit-highlight nil)
1047 (defvar gnus-newsgroup-selected-overlay nil)
1048 (defvar gnus-inhibit-limiting nil)
1049 (defvar gnus-newsgroup-adaptive-score-file nil)
1050 (defvar gnus-current-score-file nil)
1051 (defvar gnus-current-move-group nil)
1052 (defvar gnus-current-copy-group nil)
1053 (defvar gnus-current-crosspost-group nil)
1054 (defvar gnus-newsgroup-display nil)
1055
1056 (defvar gnus-newsgroup-dependencies nil)
1057 (defvar gnus-newsgroup-adaptive nil)
1058 (defvar gnus-summary-display-article-function nil)
1059 (defvar gnus-summary-highlight-line-function nil
1060   "Function called after highlighting a summary line.")
1061
1062 (defvar gnus-summary-line-format-alist
1063   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1064     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1065     (?s gnus-tmp-subject-or-nil ?s)
1066     (?n gnus-tmp-name ?s)
1067     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1068         ?s)
1069     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1070             gnus-tmp-from) ?s)
1071     (?F gnus-tmp-from ?s)
1072     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1073     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1074     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1075     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1076     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1077     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1078     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1079     (?L gnus-tmp-lines ?s)
1080     (?I gnus-tmp-indentation ?s)
1081     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1082     (?R gnus-tmp-replied ?c)
1083     (?\[ gnus-tmp-opening-bracket ?c)
1084     (?\] gnus-tmp-closing-bracket ?c)
1085     (?\> (make-string gnus-tmp-level ? ) ?s)
1086     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1087     (?i gnus-tmp-score ?d)
1088     (?z gnus-tmp-score-char ?c)
1089     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1090     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1091     (?U gnus-tmp-unread ?c)
1092     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1093         ?s)
1094     (?t (gnus-summary-number-of-articles-in-thread
1095          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1096         ?d)
1097     (?e (gnus-summary-number-of-articles-in-thread
1098          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1099         ?c)
1100     (?u gnus-tmp-user-defined ?s)
1101     (?P (gnus-pick-line-number) ?d)
1102     (?B gnus-tmp-thread-tree-header-string ?s)
1103     (user-date (gnus-user-date
1104                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1105   "An alist of format specifications that can appear in summary lines.
1106 These are paired with what variables they correspond with, along with
1107 the type of the variable (string, integer, character, etc).")
1108
1109 (defvar gnus-summary-dummy-line-format-alist
1110   `((?S gnus-tmp-subject ?s)
1111     (?N gnus-tmp-number ?d)
1112     (?u gnus-tmp-user-defined ?s)))
1113
1114 (defvar gnus-summary-mode-line-format-alist
1115   `((?G gnus-tmp-group-name ?s)
1116     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1117     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1118     (?A gnus-tmp-article-number ?d)
1119     (?Z gnus-tmp-unread-and-unselected ?s)
1120     (?V gnus-version ?s)
1121     (?U gnus-tmp-unread-and-unticked ?d)
1122     (?S gnus-tmp-subject ?s)
1123     (?e gnus-tmp-unselected ?d)
1124     (?u gnus-tmp-user-defined ?s)
1125     (?d (length gnus-newsgroup-dormant) ?d)
1126     (?t (length gnus-newsgroup-marked) ?d)
1127     (?r (length gnus-newsgroup-reads) ?d)
1128     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1129     (?E gnus-newsgroup-expunged-tally ?d)
1130     (?s (gnus-current-score-file-nondirectory) ?s)))
1131
1132 (defvar gnus-last-search-regexp nil
1133   "Default regexp for article search command.")
1134
1135 (defvar gnus-last-shell-command nil
1136   "Default shell command on article.")
1137
1138 (defvar gnus-newsgroup-begin nil)
1139 (defvar gnus-newsgroup-end nil)
1140 (defvar gnus-newsgroup-last-rmail nil)
1141 (defvar gnus-newsgroup-last-mail nil)
1142 (defvar gnus-newsgroup-last-folder nil)
1143 (defvar gnus-newsgroup-last-file nil)
1144 (defvar gnus-newsgroup-auto-expire nil)
1145 (defvar gnus-newsgroup-active nil)
1146
1147 (defvar gnus-newsgroup-data nil)
1148 (defvar gnus-newsgroup-data-reverse nil)
1149 (defvar gnus-newsgroup-limit nil)
1150 (defvar gnus-newsgroup-limits nil)
1151
1152 (defvar gnus-newsgroup-unreads nil
1153   "List of unread articles in the current newsgroup.")
1154
1155 (defvar gnus-newsgroup-unselected nil
1156   "List of unselected unread articles in the current newsgroup.")
1157
1158 (defvar gnus-newsgroup-reads nil
1159   "Alist of read articles and article marks in the current newsgroup.")
1160
1161 (defvar gnus-newsgroup-expunged-tally nil)
1162
1163 (defvar gnus-newsgroup-marked nil
1164   "List of ticked articles in the current newsgroup (a subset of unread art).")
1165
1166 (defvar gnus-newsgroup-killed nil
1167   "List of ranges of articles that have been through the scoring process.")
1168
1169 (defvar gnus-newsgroup-cached nil
1170   "List of articles that come from the article cache.")
1171
1172 (defvar gnus-newsgroup-saved nil
1173   "List of articles that have been saved.")
1174
1175 (defvar gnus-newsgroup-kill-headers nil)
1176
1177 (defvar gnus-newsgroup-replied nil
1178   "List of articles that have been replied to in the current newsgroup.")
1179
1180 (defvar gnus-newsgroup-forwarded nil
1181   "List of articles that have been forwarded in the current newsgroup.")
1182
1183 (defvar gnus-newsgroup-recent nil
1184   "List of articles that have are recent in the current newsgroup.")
1185
1186 (defvar gnus-newsgroup-expirable nil
1187   "List of articles in the current newsgroup that can be expired.")
1188
1189 (defvar gnus-newsgroup-processable nil
1190   "List of articles in the current newsgroup that can be processed.")
1191
1192 (defvar gnus-newsgroup-downloadable nil
1193   "List of articles in the current newsgroup that can be processed.")
1194
1195 (defvar gnus-newsgroup-undownloaded nil
1196   "List of articles in the current newsgroup that haven't been downloaded..")
1197
1198 (defvar gnus-newsgroup-unsendable nil
1199   "List of articles in the current newsgroup that won't be sent.")
1200
1201 (defvar gnus-newsgroup-bookmarks nil
1202   "List of articles in the current newsgroup that have bookmarks.")
1203
1204 (defvar gnus-newsgroup-dormant nil
1205   "List of dormant articles in the current newsgroup.")
1206
1207 (defvar gnus-newsgroup-unseen nil
1208   "List of unseen articles in the current newsgroup.")
1209
1210 (defvar gnus-newsgroup-seen nil
1211   "Range of seen articles in the current newsgroup.")
1212
1213 (defvar gnus-newsgroup-articles nil
1214   "List of articles in the current newsgroup.")
1215
1216 (defvar gnus-newsgroup-scored nil
1217   "List of scored articles in the current newsgroup.")
1218
1219 (defvar gnus-newsgroup-headers nil
1220   "List of article headers in the current newsgroup.")
1221
1222 (defvar gnus-newsgroup-threads nil)
1223
1224 (defvar gnus-newsgroup-prepared nil
1225   "Whether the current group has been prepared properly.")
1226
1227 (defvar gnus-newsgroup-ancient nil
1228   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1229
1230 (defvar gnus-newsgroup-sparse nil)
1231
1232 (defvar gnus-current-article nil)
1233 (defvar gnus-article-current nil)
1234 (defvar gnus-current-headers nil)
1235 (defvar gnus-have-all-headers nil)
1236 (defvar gnus-last-article nil)
1237 (defvar gnus-newsgroup-history nil)
1238 (defvar gnus-newsgroup-charset nil)
1239 (defvar gnus-newsgroup-ephemeral-charset nil)
1240 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1241
1242 (defvar gnus-article-before-search nil)
1243
1244 (defconst gnus-summary-local-variables
1245   '(gnus-newsgroup-name
1246     gnus-newsgroup-begin gnus-newsgroup-end
1247     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1248     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1249     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1250     gnus-newsgroup-unselected gnus-newsgroup-marked
1251     gnus-newsgroup-reads gnus-newsgroup-saved
1252     gnus-newsgroup-replied gnus-newsgroup-forwarded
1253     gnus-newsgroup-recent
1254     gnus-newsgroup-expirable
1255     gnus-newsgroup-processable gnus-newsgroup-killed
1256     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1257     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1258     gnus-newsgroup-seen gnus-newsgroup-articles
1259     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1260     gnus-newsgroup-headers gnus-newsgroup-threads
1261     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1262     gnus-current-article gnus-current-headers gnus-have-all-headers
1263     gnus-last-article gnus-article-internal-prepare-hook
1264     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1265     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1266     gnus-thread-expunge-below
1267     gnus-score-alist gnus-current-score-file
1268     (gnus-summary-expunge-below . global)
1269     (gnus-summary-mark-below . global)
1270     (gnus-orphan-score . global)
1271     gnus-newsgroup-active gnus-scores-exclude-files
1272     gnus-newsgroup-history gnus-newsgroup-ancient
1273     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1274     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1275     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1276     (gnus-newsgroup-expunged-tally . 0)
1277     gnus-cache-removable-articles gnus-newsgroup-cached
1278     gnus-newsgroup-data gnus-newsgroup-data-reverse
1279     gnus-newsgroup-limit gnus-newsgroup-limits
1280     gnus-newsgroup-charset gnus-newsgroup-display)
1281   "Variables that are buffer-local to the summary buffers.")
1282
1283 (defvar gnus-newsgroup-variables nil
1284   "A list of variables that have separate values in different newsgroups.
1285 A list of newsgroup (summary buffer) local variables, or cons of
1286 variables and their default values (when the default values are not
1287 nil), that should be made global while the summary buffer is active.
1288 These variables can be used to set variables in the group parameters
1289 while still allowing them to affect operations done in other
1290 buffers. For example:
1291
1292 \(setq gnus-newsgroup-variables
1293      '(message-use-followup-to
1294        (gnus-visible-headers .
1295          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1296 ")
1297
1298 ;; Byte-compiler warning.
1299 (eval-when-compile (defvar gnus-article-mode-map))
1300
1301 ;; MIME stuff.
1302
1303 (defvar gnus-decode-encoded-word-methods
1304   '(mail-decode-encoded-word-string)
1305   "List of methods used to decode encoded words.
1306
1307 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1308 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1309 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1310 whose names match REGEXP.
1311
1312 For example:
1313 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1314  mail-decode-encoded-word-string
1315  (\"chinese\" . rfc1843-decode-string))")
1316
1317 (defvar gnus-decode-encoded-word-methods-cache nil)
1318
1319 (defun gnus-multi-decode-encoded-word-string (string)
1320   "Apply the functions from `gnus-encoded-word-methods' that match."
1321   (unless (and gnus-decode-encoded-word-methods-cache
1322                (eq gnus-newsgroup-name
1323                    (car gnus-decode-encoded-word-methods-cache)))
1324     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1325     (mapcar (lambda (x)
1326               (if (symbolp x)
1327                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1328                 (if (and gnus-newsgroup-name
1329                          (string-match (car x) gnus-newsgroup-name))
1330                     (nconc gnus-decode-encoded-word-methods-cache
1331                            (list (cdr x))))))
1332             gnus-decode-encoded-word-methods))
1333   (let ((xlist gnus-decode-encoded-word-methods-cache))
1334     (pop xlist)
1335     (while xlist
1336       (setq string (funcall (pop xlist) string))))
1337   string)
1338
1339 ;; Subject simplification.
1340
1341 (defun gnus-simplify-whitespace (str)
1342   "Remove excessive whitespace from STR."
1343   (let ((mystr str))
1344     ;; Multiple spaces.
1345     (while (string-match "[ \t][ \t]+" mystr)
1346       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1347                           " "
1348                           (substring mystr (match-end 0)))))
1349     ;; Leading spaces.
1350     (when (string-match "^[ \t]+" mystr)
1351       (setq mystr (substring mystr (match-end 0))))
1352     ;; Trailing spaces.
1353     (when (string-match "[ \t]+$" mystr)
1354       (setq mystr (substring mystr 0 (match-beginning 0))))
1355     mystr))
1356
1357 (defsubst gnus-simplify-subject-re (subject)
1358   "Remove \"Re:\" from subject lines."
1359   (if (string-match message-subject-re-regexp subject)
1360       (substring subject (match-end 0))
1361     subject))
1362
1363 (defun gnus-simplify-subject (subject &optional re-only)
1364   "Remove `Re:' and words in parentheses.
1365 If RE-ONLY is non-nil, strip leading `Re:'s only."
1366   (let ((case-fold-search t))           ;Ignore case.
1367     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1368     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1369       (setq subject (substring subject (match-end 0))))
1370     ;; Remove uninteresting prefixes.
1371     (when (and (not re-only)
1372                gnus-simplify-ignored-prefixes
1373                (string-match gnus-simplify-ignored-prefixes subject))
1374       (setq subject (substring subject (match-end 0))))
1375     ;; Remove words in parentheses from end.
1376     (unless re-only
1377       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1378         (setq subject (substring subject 0 (match-beginning 0)))))
1379     ;; Return subject string.
1380     subject))
1381
1382 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1383 ;; all whitespace.
1384 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1385   (goto-char (point-min))
1386   (while (re-search-forward regexp nil t)
1387     (replace-match (or newtext ""))))
1388
1389 (defun gnus-simplify-buffer-fuzzy ()
1390   "Simplify string in the buffer fuzzily.
1391 The string in the accessible portion of the current buffer is simplified.
1392 It is assumed to be a single-line subject.
1393 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1394 matter is removed.  Additional things can be deleted by setting
1395 `gnus-simplify-subject-fuzzy-regexp'."
1396   (let ((case-fold-search t)
1397         (modified-tick))
1398     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1399
1400     (while (not (eq modified-tick (buffer-modified-tick)))
1401       (setq modified-tick (buffer-modified-tick))
1402       (cond
1403        ((listp gnus-simplify-subject-fuzzy-regexp)
1404         (mapcar 'gnus-simplify-buffer-fuzzy-step
1405                 gnus-simplify-subject-fuzzy-regexp))
1406        (gnus-simplify-subject-fuzzy-regexp
1407         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1408       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1409       (gnus-simplify-buffer-fuzzy-step
1410        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1411       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1412
1413     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1414     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1415     (gnus-simplify-buffer-fuzzy-step " $")
1416     (gnus-simplify-buffer-fuzzy-step "^ +")))
1417
1418 (defun gnus-simplify-subject-fuzzy (subject)
1419   "Simplify a subject string fuzzily.
1420 See `gnus-simplify-buffer-fuzzy' for details."
1421   (save-excursion
1422     (gnus-set-work-buffer)
1423     (let ((case-fold-search t))
1424       ;; Remove uninteresting prefixes.
1425       (when (and gnus-simplify-ignored-prefixes
1426                  (string-match gnus-simplify-ignored-prefixes subject))
1427         (setq subject (substring subject (match-end 0))))
1428       (insert subject)
1429       (inline (gnus-simplify-buffer-fuzzy))
1430       (buffer-string))))
1431
1432 (defsubst gnus-simplify-subject-fully (subject)
1433   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1434   (cond
1435    (gnus-simplify-subject-functions
1436     (gnus-map-function gnus-simplify-subject-functions subject))
1437    ((null gnus-summary-gather-subject-limit)
1438     (gnus-simplify-subject-re subject))
1439    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1440     (gnus-simplify-subject-fuzzy subject))
1441    ((numberp gnus-summary-gather-subject-limit)
1442     (gnus-limit-string (gnus-simplify-subject-re subject)
1443                        gnus-summary-gather-subject-limit))
1444    (t
1445     subject)))
1446
1447 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1448   "Check whether two subjects are equal.
1449 If optional argument simple-first is t, first argument is already
1450 simplified."
1451   (cond
1452    ((null simple-first)
1453     (equal (gnus-simplify-subject-fully s1)
1454            (gnus-simplify-subject-fully s2)))
1455    (t
1456     (equal s1
1457            (gnus-simplify-subject-fully s2)))))
1458
1459 (defun gnus-summary-bubble-group ()
1460   "Increase the score of the current group.
1461 This is a handy function to add to `gnus-summary-exit-hook' to
1462 increase the score of each group you read."
1463   (gnus-group-add-score gnus-newsgroup-name))
1464
1465 \f
1466 ;;;
1467 ;;; Gnus summary mode
1468 ;;;
1469
1470 (put 'gnus-summary-mode 'mode-class 'special)
1471
1472 (defvar gnus-article-commands-menu)
1473
1474 (when t
1475   ;; Non-orthogonal keys
1476
1477   (gnus-define-keys gnus-summary-mode-map
1478     " " gnus-summary-next-page
1479     "\177" gnus-summary-prev-page
1480     [delete] gnus-summary-prev-page
1481     [backspace] gnus-summary-prev-page
1482     "\r" gnus-summary-scroll-up
1483     "\M-\r" gnus-summary-scroll-down
1484     "n" gnus-summary-next-unread-article
1485     "p" gnus-summary-prev-unread-article
1486     "N" gnus-summary-next-article
1487     "P" gnus-summary-prev-article
1488     "\M-\C-n" gnus-summary-next-same-subject
1489     "\M-\C-p" gnus-summary-prev-same-subject
1490     "\M-n" gnus-summary-next-unread-subject
1491     "\M-p" gnus-summary-prev-unread-subject
1492     "." gnus-summary-first-unread-article
1493     "," gnus-summary-best-unread-article
1494     "\M-s" gnus-summary-search-article-forward
1495     "\M-r" gnus-summary-search-article-backward
1496     "<" gnus-summary-beginning-of-article
1497     ">" gnus-summary-end-of-article
1498     "j" gnus-summary-goto-article
1499     "^" gnus-summary-refer-parent-article
1500     "\M-^" gnus-summary-refer-article
1501     "u" gnus-summary-tick-article-forward
1502     "!" gnus-summary-tick-article-forward
1503     "U" gnus-summary-tick-article-backward
1504     "d" gnus-summary-mark-as-read-forward
1505     "D" gnus-summary-mark-as-read-backward
1506     "E" gnus-summary-mark-as-expirable
1507     "\M-u" gnus-summary-clear-mark-forward
1508     "\M-U" gnus-summary-clear-mark-backward
1509     "k" gnus-summary-kill-same-subject-and-select
1510     "\C-k" gnus-summary-kill-same-subject
1511     "\M-\C-k" gnus-summary-kill-thread
1512     "\M-\C-l" gnus-summary-lower-thread
1513     "e" gnus-summary-edit-article
1514     "#" gnus-summary-mark-as-processable
1515     "\M-#" gnus-summary-unmark-as-processable
1516     "\M-\C-t" gnus-summary-toggle-threads
1517     "\M-\C-s" gnus-summary-show-thread
1518     "\M-\C-h" gnus-summary-hide-thread
1519     "\M-\C-f" gnus-summary-next-thread
1520     "\M-\C-b" gnus-summary-prev-thread
1521     [(meta down)] gnus-summary-next-thread
1522     [(meta up)] gnus-summary-prev-thread
1523     "\M-\C-u" gnus-summary-up-thread
1524     "\M-\C-d" gnus-summary-down-thread
1525     "&" gnus-summary-execute-command
1526     "c" gnus-summary-catchup-and-exit
1527     "\C-w" gnus-summary-mark-region-as-read
1528     "\C-t" gnus-summary-toggle-truncation
1529     "?" gnus-summary-mark-as-dormant
1530     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1531     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1532     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1533     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1534     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1535     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1536     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1537     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1538     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1539     "=" gnus-summary-expand-window
1540     "\C-x\C-s" gnus-summary-reselect-current-group
1541     "\M-g" gnus-summary-rescan-group
1542     "w" gnus-summary-stop-page-breaking
1543     "\C-c\C-r" gnus-summary-caesar-message
1544     "f" gnus-summary-followup
1545     "F" gnus-summary-followup-with-original
1546     "C" gnus-summary-cancel-article
1547     "r" gnus-summary-reply
1548     "R" gnus-summary-reply-with-original
1549     "\C-c\C-f" gnus-summary-mail-forward
1550     "o" gnus-summary-save-article
1551     "\C-o" gnus-summary-save-article-mail
1552     "|" gnus-summary-pipe-output
1553     "\M-k" gnus-summary-edit-local-kill
1554     "\M-K" gnus-summary-edit-global-kill
1555     ;; "V" gnus-version
1556     "\C-c\C-d" gnus-summary-describe-group
1557     "q" gnus-summary-exit
1558     "Q" gnus-summary-exit-no-update
1559     "\C-c\C-i" gnus-info-find-node
1560     gnus-mouse-2 gnus-mouse-pick-article
1561     "m" gnus-summary-mail-other-window
1562     "a" gnus-summary-post-news
1563     "i" gnus-summary-news-other-window
1564     "x" gnus-summary-limit-to-unread
1565     "s" gnus-summary-isearch-article
1566     "t" gnus-summary-toggle-header
1567     "g" gnus-summary-show-article
1568     "l" gnus-summary-goto-last-article
1569     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1570     "\C-d" gnus-summary-enter-digest-group
1571     "\M-\C-d" gnus-summary-read-document
1572     "\M-\C-e" gnus-summary-edit-parameters
1573     "\M-\C-a" gnus-summary-customize-parameters
1574     "\C-c\C-b" gnus-bug
1575     "*" gnus-cache-enter-article
1576     "\M-*" gnus-cache-remove-article
1577     "\M-&" gnus-summary-universal-argument
1578     "\C-l" gnus-recenter
1579     "I" gnus-summary-increase-score
1580     "L" gnus-summary-lower-score
1581     "\M-i" gnus-symbolic-argument
1582     "h" gnus-summary-select-article-buffer
1583
1584     "b" gnus-article-view-part
1585     "\M-t" gnus-summary-toggle-display-buttonized
1586
1587     "V" gnus-summary-score-map
1588     "X" gnus-uu-extract-map
1589     "S" gnus-summary-send-map)
1590
1591   ;; Sort of orthogonal keymap
1592   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1593     "t" gnus-summary-tick-article-forward
1594     "!" gnus-summary-tick-article-forward
1595     "d" gnus-summary-mark-as-read-forward
1596     "r" gnus-summary-mark-as-read-forward
1597     "c" gnus-summary-clear-mark-forward
1598     " " gnus-summary-clear-mark-forward
1599     "e" gnus-summary-mark-as-expirable
1600     "x" gnus-summary-mark-as-expirable
1601     "?" gnus-summary-mark-as-dormant
1602     "b" gnus-summary-set-bookmark
1603     "B" gnus-summary-remove-bookmark
1604     "#" gnus-summary-mark-as-processable
1605     "\M-#" gnus-summary-unmark-as-processable
1606     "S" gnus-summary-limit-include-expunged
1607     "C" gnus-summary-catchup
1608     "H" gnus-summary-catchup-to-here
1609     "h" gnus-summary-catchup-from-here
1610     "\C-c" gnus-summary-catchup-all
1611     "k" gnus-summary-kill-same-subject-and-select
1612     "K" gnus-summary-kill-same-subject
1613     "P" gnus-uu-mark-map)
1614
1615   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1616     "c" gnus-summary-clear-above
1617     "u" gnus-summary-tick-above
1618     "m" gnus-summary-mark-above
1619     "k" gnus-summary-kill-below)
1620
1621   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1622     "/" gnus-summary-limit-to-subject
1623     "n" gnus-summary-limit-to-articles
1624     "w" gnus-summary-pop-limit
1625     "s" gnus-summary-limit-to-subject
1626     "a" gnus-summary-limit-to-author
1627     "u" gnus-summary-limit-to-unread
1628     "m" gnus-summary-limit-to-marks
1629     "M" gnus-summary-limit-exclude-marks
1630     "v" gnus-summary-limit-to-score
1631     "*" gnus-summary-limit-include-cached
1632     "D" gnus-summary-limit-include-dormant
1633     "T" gnus-summary-limit-include-thread
1634     "d" gnus-summary-limit-exclude-dormant
1635     "t" gnus-summary-limit-to-age
1636     "x" gnus-summary-limit-to-extra
1637     "p" gnus-summary-limit-to-display-predicate
1638     "E" gnus-summary-limit-include-expunged
1639     "c" gnus-summary-limit-exclude-childless-dormant
1640     "C" gnus-summary-limit-mark-excluded-as-read
1641     "o" gnus-summary-insert-old-articles
1642     "N" gnus-summary-insert-new-articles)
1643
1644   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1645     "n" gnus-summary-next-unread-article
1646     "p" gnus-summary-prev-unread-article
1647     "N" gnus-summary-next-article
1648     "P" gnus-summary-prev-article
1649     "\C-n" gnus-summary-next-same-subject
1650     "\C-p" gnus-summary-prev-same-subject
1651     "\M-n" gnus-summary-next-unread-subject
1652     "\M-p" gnus-summary-prev-unread-subject
1653     "f" gnus-summary-first-unread-article
1654     "b" gnus-summary-best-unread-article
1655     "j" gnus-summary-goto-article
1656     "g" gnus-summary-goto-subject
1657     "l" gnus-summary-goto-last-article
1658     "o" gnus-summary-pop-article)
1659
1660   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1661     "k" gnus-summary-kill-thread
1662     "l" gnus-summary-lower-thread
1663     "i" gnus-summary-raise-thread
1664     "T" gnus-summary-toggle-threads
1665     "t" gnus-summary-rethread-current
1666     "^" gnus-summary-reparent-thread
1667     "s" gnus-summary-show-thread
1668     "S" gnus-summary-show-all-threads
1669     "h" gnus-summary-hide-thread
1670     "H" gnus-summary-hide-all-threads
1671     "n" gnus-summary-next-thread
1672     "p" gnus-summary-prev-thread
1673     "u" gnus-summary-up-thread
1674     "o" gnus-summary-top-thread
1675     "d" gnus-summary-down-thread
1676     "#" gnus-uu-mark-thread
1677     "\M-#" gnus-uu-unmark-thread)
1678
1679   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1680     "g" gnus-summary-prepare
1681     "c" gnus-summary-insert-cached-articles)
1682
1683   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1684     "c" gnus-summary-catchup-and-exit
1685     "C" gnus-summary-catchup-all-and-exit
1686     "E" gnus-summary-exit-no-update
1687     "Q" gnus-summary-exit
1688     "Z" gnus-summary-exit
1689     "n" gnus-summary-catchup-and-goto-next-group
1690     "R" gnus-summary-reselect-current-group
1691     "G" gnus-summary-rescan-group
1692     "N" gnus-summary-next-group
1693     "s" gnus-summary-save-newsrc
1694     "P" gnus-summary-prev-group)
1695
1696   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1697     " " gnus-summary-next-page
1698     "n" gnus-summary-next-page
1699     "\177" gnus-summary-prev-page
1700     [delete] gnus-summary-prev-page
1701     "p" gnus-summary-prev-page
1702     "\r" gnus-summary-scroll-up
1703     "\M-\r" gnus-summary-scroll-down
1704     "<" gnus-summary-beginning-of-article
1705     ">" gnus-summary-end-of-article
1706     "b" gnus-summary-beginning-of-article
1707     "e" gnus-summary-end-of-article
1708     "^" gnus-summary-refer-parent-article
1709     "r" gnus-summary-refer-parent-article
1710     "D" gnus-summary-enter-digest-group
1711     "R" gnus-summary-refer-references
1712     "T" gnus-summary-refer-thread
1713     "g" gnus-summary-show-article
1714     "s" gnus-summary-isearch-article
1715     "P" gnus-summary-print-article
1716     "M" gnus-mailing-list-insinuate
1717     "t" gnus-article-babel)
1718
1719   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1720     "b" gnus-article-add-buttons
1721     "B" gnus-article-add-buttons-to-head
1722     "o" gnus-article-treat-overstrike
1723     "e" gnus-article-emphasize
1724     "w" gnus-article-fill-cited-article
1725     "Q" gnus-article-fill-long-lines
1726     "C" gnus-article-capitalize-sentences
1727     "c" gnus-article-remove-cr
1728     "q" gnus-article-de-quoted-unreadable
1729     "6" gnus-article-de-base64-unreadable
1730     "Z" gnus-article-decode-HZ
1731     "h" gnus-article-wash-html
1732     "s" gnus-summary-force-verify-and-decrypt
1733     "f" gnus-article-display-x-face
1734     "l" gnus-summary-stop-page-breaking
1735     "r" gnus-summary-caesar-message
1736     "t" gnus-summary-toggle-header
1737     "g" gnus-treat-smiley
1738     "v" gnus-summary-verbose-headers
1739     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1740     "p" gnus-article-verify-x-pgp-sig
1741     "d" gnus-article-treat-dumbquotes)
1742
1743   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1744     "a" gnus-article-hide
1745     "h" gnus-article-hide-headers
1746     "b" gnus-article-hide-boring-headers
1747     "s" gnus-article-hide-signature
1748     "c" gnus-article-hide-citation
1749     "C" gnus-article-hide-citation-in-followups
1750     "l" gnus-article-hide-list-identifiers
1751     "p" gnus-article-hide-pgp
1752     "B" gnus-article-strip-banner
1753     "P" gnus-article-hide-pem
1754     "\C-c" gnus-article-hide-citation-maybe)
1755
1756   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1757     "a" gnus-article-highlight
1758     "h" gnus-article-highlight-headers
1759     "c" gnus-article-highlight-citation
1760     "s" gnus-article-highlight-signature)
1761
1762   (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1763     "f" gnus-article-treat-fold-headers
1764     "u" gnus-article-treat-unfold-headers
1765     "n" gnus-article-treat-fold-newsgroups)
1766
1767   (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1768     "x" gnus-article-display-x-face
1769     "s" gnus-treat-smiley
1770     "D" gnus-article-remove-images
1771     "f" gnus-treat-from-picon
1772     "m" gnus-treat-mail-picon
1773     "n" gnus-treat-newsgroups-picon)
1774
1775   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1776     "w" gnus-article-decode-mime-words
1777     "c" gnus-article-decode-charset
1778     "v" gnus-mime-view-all-parts
1779     "b" gnus-article-view-part)
1780
1781   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1782     "z" gnus-article-date-ut
1783     "u" gnus-article-date-ut
1784     "l" gnus-article-date-local
1785     "p" gnus-article-date-english
1786     "e" gnus-article-date-lapsed
1787     "o" gnus-article-date-original
1788     "i" gnus-article-date-iso8601
1789     "s" gnus-article-date-user)
1790
1791   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1792     "t" gnus-article-remove-trailing-blank-lines
1793     "l" gnus-article-strip-leading-blank-lines
1794     "m" gnus-article-strip-multiple-blank-lines
1795     "a" gnus-article-strip-blank-lines
1796     "A" gnus-article-strip-all-blank-lines
1797     "s" gnus-article-strip-leading-space
1798     "e" gnus-article-strip-trailing-space
1799     "w" gnus-article-remove-leading-whitespace)
1800
1801   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1802     "v" gnus-version
1803     "f" gnus-summary-fetch-faq
1804     "d" gnus-summary-describe-group
1805     "h" gnus-summary-describe-briefly
1806     "i" gnus-info-find-node)
1807
1808   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1809     "e" gnus-summary-expire-articles
1810     "\M-\C-e" gnus-summary-expire-articles-now
1811     "\177" gnus-summary-delete-article
1812     [delete] gnus-summary-delete-article
1813     [backspace] gnus-summary-delete-article
1814     "m" gnus-summary-move-article
1815     "r" gnus-summary-respool-article
1816     "w" gnus-summary-edit-article
1817     "c" gnus-summary-copy-article
1818     "B" gnus-summary-crosspost-article
1819     "q" gnus-summary-respool-query
1820     "t" gnus-summary-respool-trace
1821     "i" gnus-summary-import-article
1822     "I" gnus-summary-create-article
1823     "p" gnus-summary-article-posted-p)
1824
1825   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1826     "o" gnus-summary-save-article
1827     "m" gnus-summary-save-article-mail
1828     "F" gnus-summary-write-article-file
1829     "r" gnus-summary-save-article-rmail
1830     "f" gnus-summary-save-article-file
1831     "b" gnus-summary-save-article-body-file
1832     "h" gnus-summary-save-article-folder
1833     "v" gnus-summary-save-article-vm
1834     "p" gnus-summary-pipe-output
1835     "P" gnus-summary-muttprint
1836     "s" gnus-soup-add-article)
1837
1838   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1839     "b" gnus-summary-display-buttonized
1840     "m" gnus-summary-repair-multipart
1841     "v" gnus-article-view-part
1842     "o" gnus-article-save-part
1843     "c" gnus-article-copy-part
1844     "C" gnus-article-view-part-as-charset
1845     "e" gnus-article-externalize-part
1846     "E" gnus-article-encrypt-body
1847     "i" gnus-article-inline-part
1848     "|" gnus-article-pipe-part))
1849
1850 (defvar gnus-article-post-menu nil)
1851
1852 (defun gnus-summary-make-menu-bar ()
1853   (gnus-turn-off-edit-menu 'summary)
1854
1855   (unless (boundp 'gnus-summary-misc-menu)
1856
1857     (easy-menu-define
1858       gnus-summary-kill-menu gnus-summary-mode-map ""
1859       (cons
1860        "Score"
1861        (nconc
1862         (list
1863          ["Customize" gnus-score-customize t])
1864         (gnus-make-score-map 'increase)
1865         (gnus-make-score-map 'lower)
1866         '(("Mark"
1867            ["Kill below" gnus-summary-kill-below t]
1868            ["Mark above" gnus-summary-mark-above t]
1869            ["Tick above" gnus-summary-tick-above t]
1870            ["Clear above" gnus-summary-clear-above t])
1871           ["Current score" gnus-summary-current-score t]
1872           ["Set score" gnus-summary-set-score t]
1873           ["Switch current score file..." gnus-score-change-score-file t]
1874           ["Set mark below..." gnus-score-set-mark-below t]
1875           ["Set expunge below..." gnus-score-set-expunge-below t]
1876           ["Edit current score file" gnus-score-edit-current-scores t]
1877           ["Edit score file" gnus-score-edit-file t]
1878           ["Trace score" gnus-score-find-trace t]
1879           ["Find words" gnus-score-find-favourite-words t]
1880           ["Rescore buffer" gnus-summary-rescore t]
1881           ["Increase score..." gnus-summary-increase-score t]
1882           ["Lower score..." gnus-summary-lower-score t]))))
1883
1884     ;; Define both the Article menu in the summary buffer and the
1885     ;; equivalent Commands menu in the article buffer here for
1886     ;; consistency.
1887     (let ((innards
1888            `(("Hide"
1889               ["All" gnus-article-hide t]
1890               ["Headers" gnus-article-hide-headers t]
1891               ["Signature" gnus-article-hide-signature t]
1892               ["Citation" gnus-article-hide-citation t]
1893               ["List identifiers" gnus-article-hide-list-identifiers t]
1894               ["PGP" gnus-article-hide-pgp t]
1895               ["Banner" gnus-article-strip-banner t]
1896               ["Boring headers" gnus-article-hide-boring-headers t])
1897              ("Highlight"
1898               ["All" gnus-article-highlight t]
1899               ["Headers" gnus-article-highlight-headers t]
1900               ["Signature" gnus-article-highlight-signature t]
1901               ["Citation" gnus-article-highlight-citation t])
1902              ("MIME"
1903               ["Words" gnus-article-decode-mime-words t]
1904               ["Charset" gnus-article-decode-charset t]
1905               ["QP" gnus-article-de-quoted-unreadable t]
1906               ["Base64" gnus-article-de-base64-unreadable t]
1907               ["View all" gnus-mime-view-all-parts t]
1908               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
1909               ["Encrypt body" gnus-article-encrypt-body t])
1910              ("Date"
1911               ["Local" gnus-article-date-local t]
1912               ["ISO8601" gnus-article-date-iso8601 t]
1913               ["UT" gnus-article-date-ut t]
1914               ["Original" gnus-article-date-original t]
1915               ["Lapsed" gnus-article-date-lapsed t]
1916               ["User-defined" gnus-article-date-user t])
1917              ("Display"
1918               ["Remove images" gnus-article-remove-images t]
1919               ["Toggle smiley" gnus-treat-smiley t]
1920               ["Show X-Face" gnus-article-display-x-face t]
1921               ["Show picons in From" gnus-treat-from-picon t]
1922               ["Show picons in mail headers" gnus-treat-mail-picon t]
1923               ["Show picons in news headers" gnus-treat-newsgroups-picon t])
1924              ("Washing"
1925               ("Remove Blanks"
1926                ["Leading" gnus-article-strip-leading-blank-lines t]
1927                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1928                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1929                ["All of the above" gnus-article-strip-blank-lines t]
1930                ["All" gnus-article-strip-all-blank-lines t]
1931                ["Leading space" gnus-article-strip-leading-space t]
1932                ["Trailing space" gnus-article-strip-trailing-space t]
1933                ["Leading space in headers"
1934                 gnus-article-remove-leading-whitespace t])
1935               ["Overstrike" gnus-article-treat-overstrike t]
1936               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1937               ["Emphasis" gnus-article-emphasize t]
1938               ["Word wrap" gnus-article-fill-cited-article t]
1939               ["Fill long lines" gnus-article-fill-long-lines t]
1940               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1941               ["CR" gnus-article-remove-cr t]
1942               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1943               ["Base64" gnus-article-de-base64-unreadable t]
1944               ["Rot 13" gnus-summary-caesar-message
1945                ,@(if (featurep 'xemacs) '(t)
1946                    '(:help "\"Caesar rotate\" article by 13"))]
1947               ["Unix pipe" gnus-summary-pipe-message t]
1948               ["Add buttons" gnus-article-add-buttons t]
1949               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1950               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1951               ["Verbose header" gnus-summary-verbose-headers t]
1952               ["Toggle header" gnus-summary-toggle-header t]
1953               ["Unfold headers" gnus-article-treat-unfold-headers t]
1954               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
1955               ["Html" gnus-article-wash-html t]
1956               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1957               ["HZ" gnus-article-decode-HZ t])
1958              ("Output"
1959               ["Save in default format" gnus-summary-save-article
1960                ,@(if (featurep 'xemacs) '(t)
1961                    '(:help "Save article using default method"))]
1962               ["Save in file" gnus-summary-save-article-file
1963                ,@(if (featurep 'xemacs) '(t)
1964                    '(:help "Save article in file"))]
1965               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1966               ["Save in MH folder" gnus-summary-save-article-folder t]
1967               ["Save in VM folder" gnus-summary-save-article-vm t]
1968               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1969               ["Save body in file" gnus-summary-save-article-body-file t]
1970               ["Pipe through a filter" gnus-summary-pipe-output t]
1971               ["Add to SOUP packet" gnus-soup-add-article t]
1972               ["Print with Muttprint" gnus-summary-muttprint t]
1973               ["Print" gnus-summary-print-article t])
1974              ("Backend"
1975               ["Respool article..." gnus-summary-respool-article t]
1976               ["Move article..." gnus-summary-move-article
1977                (gnus-check-backend-function
1978                 'request-move-article gnus-newsgroup-name)]
1979               ["Copy article..." gnus-summary-copy-article t]
1980               ["Crosspost article..." gnus-summary-crosspost-article
1981                (gnus-check-backend-function
1982                 'request-replace-article gnus-newsgroup-name)]
1983               ["Import file..." gnus-summary-import-article t]
1984               ["Create article..." gnus-summary-create-article t]
1985               ["Check if posted" gnus-summary-article-posted-p t]
1986               ["Edit article" gnus-summary-edit-article
1987                (not (gnus-group-read-only-p))]
1988               ["Delete article" gnus-summary-delete-article
1989                (gnus-check-backend-function
1990                 'request-expire-articles gnus-newsgroup-name)]
1991               ["Query respool" gnus-summary-respool-query t]
1992               ["Trace respool" gnus-summary-respool-trace t]
1993               ["Delete expirable articles" gnus-summary-expire-articles-now
1994                (gnus-check-backend-function
1995                 'request-expire-articles gnus-newsgroup-name)])
1996              ("Extract"
1997               ["Uudecode" gnus-uu-decode-uu
1998                ,@(if (featurep 'xemacs) '(t)
1999                    '(:help "Decode uuencoded article(s)"))]
2000               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2001               ["Unshar" gnus-uu-decode-unshar t]
2002               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2003               ["Save" gnus-uu-decode-save t]
2004               ["Binhex" gnus-uu-decode-binhex t]
2005               ["Postscript" gnus-uu-decode-postscript t])
2006              ("Cache"
2007               ["Enter article" gnus-cache-enter-article t]
2008               ["Remove article" gnus-cache-remove-article t])
2009              ["Translate" gnus-article-babel t]
2010              ["Select article buffer" gnus-summary-select-article-buffer t]
2011              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2012              ["Isearch article..." gnus-summary-isearch-article t]
2013              ["Beginning of the article" gnus-summary-beginning-of-article t]
2014              ["End of the article" gnus-summary-end-of-article t]
2015              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2016              ["Fetch referenced articles" gnus-summary-refer-references t]
2017              ["Fetch current thread" gnus-summary-refer-thread t]
2018              ["Fetch article with id..." gnus-summary-refer-article t]
2019              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2020              ["Redisplay" gnus-summary-show-article t]
2021              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2022       (easy-menu-define
2023         gnus-summary-article-menu gnus-summary-mode-map ""
2024         (cons "Article" innards))
2025
2026       (if (not (keymapp gnus-summary-article-menu))
2027           (easy-menu-define
2028             gnus-article-commands-menu gnus-article-mode-map ""
2029             (cons "Commands" innards))
2030         ;; in Emacs, don't share menu.
2031         (setq gnus-article-commands-menu
2032               (copy-keymap gnus-summary-article-menu))
2033         (define-key gnus-article-mode-map [menu-bar commands]
2034           (cons "Commands" gnus-article-commands-menu))))
2035
2036     (easy-menu-define
2037       gnus-summary-thread-menu gnus-summary-mode-map ""
2038       '("Threads"
2039         ["Toggle threading" gnus-summary-toggle-threads t]
2040         ["Hide threads" gnus-summary-hide-all-threads t]
2041         ["Show threads" gnus-summary-show-all-threads t]
2042         ["Hide thread" gnus-summary-hide-thread t]
2043         ["Show thread" gnus-summary-show-thread t]
2044         ["Go to next thread" gnus-summary-next-thread t]
2045         ["Go to previous thread" gnus-summary-prev-thread t]
2046         ["Go down thread" gnus-summary-down-thread t]
2047         ["Go up thread" gnus-summary-up-thread t]
2048         ["Top of thread" gnus-summary-top-thread t]
2049         ["Mark thread as read" gnus-summary-kill-thread t]
2050         ["Lower thread score" gnus-summary-lower-thread t]
2051         ["Raise thread score" gnus-summary-raise-thread t]
2052         ["Rethread current" gnus-summary-rethread-current t]))
2053
2054     (easy-menu-define
2055       gnus-summary-post-menu gnus-summary-mode-map ""
2056       `("Post"
2057         ["Send a message (mail or news)" gnus-summary-post-news
2058          ,@(if (featurep 'xemacs) '(t)
2059              '(:help "Post an article"))]
2060         ["Followup" gnus-summary-followup
2061          ,@(if (featurep 'xemacs) '(t)
2062              '(:help "Post followup to this article"))]
2063         ["Followup and yank" gnus-summary-followup-with-original
2064          ,@(if (featurep 'xemacs) '(t)
2065              '(:help "Post followup to this article, quoting its contents"))]
2066         ["Supersede article" gnus-summary-supersede-article t]
2067         ["Cancel article" gnus-summary-cancel-article
2068          ,@(if (featurep 'xemacs) '(t)
2069              '(:help "Cancel an article you posted"))]
2070         ["Reply" gnus-summary-reply t]
2071         ["Reply and yank" gnus-summary-reply-with-original t]
2072         ["Wide reply" gnus-summary-wide-reply t]
2073         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2074          ,@(if (featurep 'xemacs) '(t)
2075              '(:help "Mail a reply, quoting this article"))]
2076         ["Very wide reply" gnus-summary-very-wide-reply t]
2077         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2078         ,@(if (featurep 'xemacs) '(t)
2079             '(:help "Mail a very wide reply, quoting this article"))]
2080         ["Mail forward" gnus-summary-mail-forward t]
2081         ["Post forward" gnus-summary-post-forward t]
2082         ["Digest and mail" gnus-uu-digest-mail-forward t]
2083         ["Digest and post" gnus-uu-digest-post-forward t]
2084         ["Resend message" gnus-summary-resend-message t]
2085         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2086         ["Send a mail" gnus-summary-mail-other-window t]
2087         ["Create a local message" gnus-summary-news-other-window t]
2088         ["Uuencode and post" gnus-uu-post-news
2089          ,@(if (featurep 'xemacs) '(t)
2090              '(:help "Post a uuencoded article"))]
2091         ["Followup via news" gnus-summary-followup-to-mail t]
2092         ["Followup via news and yank"
2093          gnus-summary-followup-to-mail-with-original t]
2094         ;;("Draft"
2095         ;;["Send" gnus-summary-send-draft t]
2096         ;;["Send bounced" gnus-resend-bounced-mail t])
2097         ))
2098
2099     (cond
2100      ((not (keymapp gnus-summary-post-menu))
2101       (setq gnus-article-post-menu gnus-summary-post-menu))
2102      ((not gnus-article-post-menu)
2103       ;; Don't share post menu.
2104       (setq gnus-article-post-menu
2105             (copy-keymap gnus-summary-post-menu))))
2106     (define-key gnus-article-mode-map [menu-bar post]
2107       (cons "Post" gnus-article-post-menu))
2108
2109     (easy-menu-define
2110       gnus-summary-misc-menu gnus-summary-mode-map ""
2111       `("Gnus"
2112         ("Mark Read"
2113          ["Mark as read" gnus-summary-mark-as-read-forward t]
2114          ["Mark same subject and select"
2115           gnus-summary-kill-same-subject-and-select t]
2116          ["Mark same subject" gnus-summary-kill-same-subject t]
2117          ["Catchup" gnus-summary-catchup
2118           ,@(if (featurep 'xemacs) '(t)
2119               '(:help "Mark unread articles in this group as read"))]
2120          ["Catchup all" gnus-summary-catchup-all t]
2121          ["Catchup to here" gnus-summary-catchup-to-here t]
2122          ["Catchup from here" gnus-summary-catchup-from-here t]
2123          ["Catchup region" gnus-summary-mark-region-as-read t]
2124          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2125         ("Mark Various"
2126          ["Tick" gnus-summary-tick-article-forward t]
2127          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2128          ["Remove marks" gnus-summary-clear-mark-forward t]
2129          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2130          ["Set bookmark" gnus-summary-set-bookmark t]
2131          ["Remove bookmark" gnus-summary-remove-bookmark t])
2132         ("Limit to"
2133          ["Marks..." gnus-summary-limit-to-marks t]
2134          ["Subject..." gnus-summary-limit-to-subject t]
2135          ["Author..." gnus-summary-limit-to-author t]
2136          ["Age..." gnus-summary-limit-to-age t]
2137          ["Extra..." gnus-summary-limit-to-extra t]
2138          ["Score" gnus-summary-limit-to-score t]
2139          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2140          ["Unread" gnus-summary-limit-to-unread t]
2141          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2142          ["Articles" gnus-summary-limit-to-articles t]
2143          ["Pop limit" gnus-summary-pop-limit t]
2144          ["Show dormant" gnus-summary-limit-include-dormant t]
2145          ["Hide childless dormant"
2146           gnus-summary-limit-exclude-childless-dormant t]
2147          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2148          ["Hide marked" gnus-summary-limit-exclude-marks t]
2149          ["Show expunged" gnus-summary-limit-include-expunged t])
2150         ("Process Mark"
2151          ["Set mark" gnus-summary-mark-as-processable t]
2152          ["Remove mark" gnus-summary-unmark-as-processable t]
2153          ["Remove all marks" gnus-summary-unmark-all-processable t]
2154          ["Mark above" gnus-uu-mark-over t]
2155          ["Mark series" gnus-uu-mark-series t]
2156          ["Mark region" gnus-uu-mark-region t]
2157          ["Unmark region" gnus-uu-unmark-region t]
2158          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2159          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2160          ["Mark all" gnus-uu-mark-all t]
2161          ["Mark buffer" gnus-uu-mark-buffer t]
2162          ["Mark sparse" gnus-uu-mark-sparse t]
2163          ["Mark thread" gnus-uu-mark-thread t]
2164          ["Unmark thread" gnus-uu-unmark-thread t]
2165          ("Process Mark Sets"
2166           ["Kill" gnus-summary-kill-process-mark t]
2167           ["Yank" gnus-summary-yank-process-mark
2168            gnus-newsgroup-process-stack]
2169           ["Save" gnus-summary-save-process-mark t]))
2170         ("Scroll article"
2171          ["Page forward" gnus-summary-next-page
2172           ,@(if (featurep 'xemacs) '(t)
2173               '(:help "Show next page of article"))]
2174          ["Page backward" gnus-summary-prev-page
2175           ,@(if (featurep 'xemacs) '(t)
2176               '(:help "Show previous page of article"))]
2177          ["Line forward" gnus-summary-scroll-up t])
2178         ("Move"
2179          ["Next unread article" gnus-summary-next-unread-article t]
2180          ["Previous unread article" gnus-summary-prev-unread-article t]
2181          ["Next article" gnus-summary-next-article t]
2182          ["Previous article" gnus-summary-prev-article t]
2183          ["Next unread subject" gnus-summary-next-unread-subject t]
2184          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2185          ["Next article same subject" gnus-summary-next-same-subject t]
2186          ["Previous article same subject" gnus-summary-prev-same-subject t]
2187          ["First unread article" gnus-summary-first-unread-article t]
2188          ["Best unread article" gnus-summary-best-unread-article t]
2189          ["Go to subject number..." gnus-summary-goto-subject t]
2190          ["Go to article number..." gnus-summary-goto-article t]
2191          ["Go to the last article" gnus-summary-goto-last-article t]
2192          ["Pop article off history" gnus-summary-pop-article t])
2193         ("Sort"
2194          ["Sort by number" gnus-summary-sort-by-number t]
2195          ["Sort by author" gnus-summary-sort-by-author t]
2196          ["Sort by subject" gnus-summary-sort-by-subject t]
2197          ["Sort by date" gnus-summary-sort-by-date t]
2198          ["Sort by score" gnus-summary-sort-by-score t]
2199          ["Sort by lines" gnus-summary-sort-by-lines t]
2200          ["Sort by characters" gnus-summary-sort-by-chars t]
2201          ["Original sort" gnus-summary-sort-by-original t])
2202         ("Help"
2203          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2204          ["Describe group" gnus-summary-describe-group t]
2205          ["Read manual" gnus-info-find-node t])
2206         ("Modes"
2207          ["Pick and read" gnus-pick-mode t]
2208          ["Binary" gnus-binary-mode t])
2209         ("Regeneration"
2210          ["Regenerate" gnus-summary-prepare t]
2211          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2212          ["Toggle threading" gnus-summary-toggle-threads t])
2213         ["See old articles" gnus-summary-insert-old-articles t]
2214         ["See new articles" gnus-summary-insert-new-articles t]
2215         ["Filter articles..." gnus-summary-execute-command t]
2216         ["Run command on subjects..." gnus-summary-universal-argument t]
2217         ["Search articles forward..." gnus-summary-search-article-forward t]
2218         ["Search articles backward..." gnus-summary-search-article-backward t]
2219         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2220         ["Expand window" gnus-summary-expand-window t]
2221         ["Expire expirable articles" gnus-summary-expire-articles
2222          (gnus-check-backend-function
2223           'request-expire-articles gnus-newsgroup-name)]
2224         ["Edit local kill file" gnus-summary-edit-local-kill t]
2225         ["Edit main kill file" gnus-summary-edit-global-kill t]
2226         ["Edit group parameters" gnus-summary-edit-parameters t]
2227         ["Customize group parameters" gnus-summary-customize-parameters t]
2228         ["Send a bug report" gnus-bug t]
2229         ("Exit"
2230          ["Catchup and exit" gnus-summary-catchup-and-exit
2231           ,@(if (featurep 'xemacs) '(t)
2232               '(:help "Mark unread articles in this group as read, then exit"))]
2233          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2234          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2235          ["Exit group" gnus-summary-exit
2236           ,@(if (featurep 'xemacs) '(t)
2237               '(:help "Exit current group, return to group selection mode"))]
2238          ["Exit group without updating" gnus-summary-exit-no-update t]
2239          ["Exit and goto next group" gnus-summary-next-group t]
2240          ["Exit and goto prev group" gnus-summary-prev-group t]
2241          ["Reselect group" gnus-summary-reselect-current-group t]
2242          ["Rescan group" gnus-summary-rescan-group t]
2243          ["Update dribble" gnus-summary-save-newsrc t])))
2244
2245     (gnus-run-hooks 'gnus-summary-menu-hook)))
2246
2247 (defvar gnus-summary-tool-bar-map nil)
2248
2249 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2250 (defun gnus-summary-make-tool-bar ()
2251   (if (and (fboundp 'tool-bar-add-item-from-menu)
2252            (default-value 'tool-bar-mode)
2253            (not gnus-summary-tool-bar-map))
2254       (setq gnus-summary-tool-bar-map
2255             (let ((tool-bar-map (make-sparse-keymap))
2256                   (load-path (mm-image-load-path)))
2257               (tool-bar-add-item-from-menu
2258                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2259               (tool-bar-add-item-from-menu
2260                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2261               (tool-bar-add-item-from-menu
2262                'gnus-summary-post-news "post" gnus-summary-mode-map)
2263               (tool-bar-add-item-from-menu
2264                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2265               (tool-bar-add-item-from-menu
2266                'gnus-summary-followup "followup" gnus-summary-mode-map)
2267               (tool-bar-add-item-from-menu
2268                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2269               (tool-bar-add-item-from-menu
2270                'gnus-summary-reply "reply" gnus-summary-mode-map)
2271               (tool-bar-add-item-from-menu
2272                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2273               (tool-bar-add-item-from-menu
2274                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2275               (tool-bar-add-item-from-menu
2276                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2277               (tool-bar-add-item-from-menu
2278                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2279               (tool-bar-add-item-from-menu
2280                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2281               (tool-bar-add-item-from-menu
2282                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2283               (tool-bar-add-item-from-menu
2284                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2285               (tool-bar-add-item-from-menu
2286                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2287               tool-bar-map)))
2288   (if gnus-summary-tool-bar-map
2289       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2290
2291 (defun gnus-score-set-default (var value)
2292   "A version of set that updates the GNU Emacs menu-bar."
2293   (set var value)
2294   ;; It is the message that forces the active status to be updated.
2295   (message ""))
2296
2297 (defun gnus-make-score-map (type)
2298   "Make a summary score map of type TYPE."
2299   (if t
2300       nil
2301     (let ((headers '(("author" "from" string)
2302                      ("subject" "subject" string)
2303                      ("article body" "body" string)
2304                      ("article head" "head" string)
2305                      ("xref" "xref" string)
2306                      ("extra header" "extra" string)
2307                      ("lines" "lines" number)
2308                      ("followups to author" "followup" string)))
2309           (types '((number ("less than" <)
2310                            ("greater than" >)
2311                            ("equal" =))
2312                    (string ("substring" s)
2313                            ("exact string" e)
2314                            ("fuzzy string" f)
2315                            ("regexp" r))))
2316           (perms '(("temporary" (current-time-string))
2317                    ("permanent" nil)
2318                    ("immediate" now)))
2319           header)
2320       (list
2321        (apply
2322         'nconc
2323         (list
2324          (if (eq type 'lower)
2325              "Lower score"
2326            "Increase score"))
2327         (let (outh)
2328           (while headers
2329             (setq header (car headers))
2330             (setq outh
2331                   (cons
2332                    (apply
2333                     'nconc
2334                     (list (car header))
2335                     (let ((ts (cdr (assoc (nth 2 header) types)))
2336                           outt)
2337                       (while ts
2338                         (setq outt
2339                               (cons
2340                                (apply
2341                                 'nconc
2342                                 (list (caar ts))
2343                                 (let ((ps perms)
2344                                       outp)
2345                                   (while ps
2346                                     (setq outp
2347                                           (cons
2348                                            (vector
2349                                             (caar ps)
2350                                             (list
2351                                              'gnus-summary-score-entry
2352                                              (nth 1 header)
2353                                              (if (or (string= (nth 1 header)
2354                                                               "head")
2355                                                      (string= (nth 1 header)
2356                                                               "body"))
2357                                                  ""
2358                                                (list 'gnus-summary-header
2359                                                      (nth 1 header)))
2360                                              (list 'quote (nth 1 (car ts)))
2361                                              (list 'gnus-score-delta-default
2362                                                    nil)
2363                                              (nth 1 (car ps))
2364                                              t)
2365                                             t)
2366                                            outp))
2367                                     (setq ps (cdr ps)))
2368                                   (list (nreverse outp))))
2369                                outt))
2370                         (setq ts (cdr ts)))
2371                       (list (nreverse outt))))
2372                    outh))
2373             (setq headers (cdr headers)))
2374           (list (nreverse outh))))))))
2375
2376 \f
2377
2378 (defun gnus-summary-mode (&optional group)
2379   "Major mode for reading articles.
2380
2381 All normal editing commands are switched off.
2382 \\<gnus-summary-mode-map>
2383 Each line in this buffer represents one article.  To read an
2384 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2385 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2386 respectively.
2387
2388 You can also post articles and send mail from this buffer.  To
2389 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2390 of an article, type `\\[gnus-summary-reply]'.
2391
2392 There are approx. one gazillion commands you can execute in this
2393 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2394
2395 The following commands are available:
2396
2397 \\{gnus-summary-mode-map}"
2398   (interactive)
2399   (kill-all-local-variables)
2400   (when (gnus-visual-p 'summary-menu 'menu)
2401     (gnus-summary-make-menu-bar)
2402     (gnus-summary-make-tool-bar))
2403   (gnus-summary-make-local-variables)
2404   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2405     (gnus-summary-make-local-variables))
2406   (gnus-make-thread-indent-array)
2407   (gnus-simplify-mode-line)
2408   (setq major-mode 'gnus-summary-mode)
2409   (setq mode-name "Summary")
2410   (make-local-variable 'minor-mode-alist)
2411   (use-local-map gnus-summary-mode-map)
2412   (buffer-disable-undo)
2413   (setq buffer-read-only t)             ;Disable modification
2414   (setq truncate-lines t)
2415   (setq selective-display t)
2416   (setq selective-display-ellipses t)   ;Display `...'
2417   (gnus-summary-set-display-table)
2418   (gnus-set-default-directory)
2419   (setq gnus-newsgroup-name group)
2420   (make-local-variable 'gnus-summary-line-format)
2421   (make-local-variable 'gnus-summary-line-format-spec)
2422   (make-local-variable 'gnus-summary-dummy-line-format)
2423   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2424   (make-local-variable 'gnus-summary-mark-positions)
2425   (make-local-hook 'pre-command-hook)
2426   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2427   (gnus-run-hooks 'gnus-summary-mode-hook)
2428   (turn-on-gnus-mailing-list-mode)
2429   (mm-enable-multibyte)
2430   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2431   (gnus-update-summary-mark-positions))
2432
2433 (defun gnus-summary-make-local-variables ()
2434   "Make all the local summary buffer variables."
2435   (let (global)
2436     (dolist (local gnus-summary-local-variables)
2437       (if (consp local)
2438           (progn
2439             (if (eq (cdr local) 'global)
2440                 ;; Copy the global value of the variable.
2441                 (setq global (symbol-value (car local)))
2442               ;; Use the value from the list.
2443               (setq global (eval (cdr local))))
2444             (set (make-local-variable (car local)) global))
2445         ;; Simple nil-valued local variable.
2446         (set (make-local-variable local) nil)))))
2447
2448 (defun gnus-summary-clear-local-variables ()
2449   (let ((locals gnus-summary-local-variables))
2450     (while locals
2451       (if (consp (car locals))
2452           (and (vectorp (caar locals))
2453                (set (caar locals) nil))
2454         (and (vectorp (car locals))
2455              (set (car locals) nil)))
2456       (setq locals (cdr locals)))))
2457
2458 ;; Summary data functions.
2459
2460 (defmacro gnus-data-number (data)
2461   `(car ,data))
2462
2463 (defmacro gnus-data-set-number (data number)
2464   `(setcar ,data ,number))
2465
2466 (defmacro gnus-data-mark (data)
2467   `(nth 1 ,data))
2468
2469 (defmacro gnus-data-set-mark (data mark)
2470   `(setcar (nthcdr 1 ,data) ,mark))
2471
2472 (defmacro gnus-data-pos (data)
2473   `(nth 2 ,data))
2474
2475 (defmacro gnus-data-set-pos (data pos)
2476   `(setcar (nthcdr 2 ,data) ,pos))
2477
2478 (defmacro gnus-data-header (data)
2479   `(nth 3 ,data))
2480
2481 (defmacro gnus-data-set-header (data header)
2482   `(setf (nth 3 ,data) ,header))
2483
2484 (defmacro gnus-data-level (data)
2485   `(nth 4 ,data))
2486
2487 (defmacro gnus-data-unread-p (data)
2488   `(= (nth 1 ,data) gnus-unread-mark))
2489
2490 (defmacro gnus-data-read-p (data)
2491   `(/= (nth 1 ,data) gnus-unread-mark))
2492
2493 (defmacro gnus-data-pseudo-p (data)
2494   `(consp (nth 3 ,data)))
2495
2496 (defmacro gnus-data-find (number)
2497   `(assq ,number gnus-newsgroup-data))
2498
2499 (defmacro gnus-data-find-list (number &optional data)
2500   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2501      (memq (assq ,number bdata)
2502            bdata)))
2503
2504 (defmacro gnus-data-make (number mark pos header level)
2505   `(list ,number ,mark ,pos ,header ,level))
2506
2507 (defun gnus-data-enter (after-article number mark pos header level offset)
2508   (let ((data (gnus-data-find-list after-article)))
2509     (unless data
2510       (error "No such article: %d" after-article))
2511     (setcdr data (cons (gnus-data-make number mark pos header level)
2512                        (cdr data)))
2513     (setq gnus-newsgroup-data-reverse nil)
2514     (gnus-data-update-list (cddr data) offset)))
2515
2516 (defun gnus-data-enter-list (after-article list &optional offset)
2517   (when list
2518     (let ((data (and after-article (gnus-data-find-list after-article)))
2519           (ilist list))
2520       (if (not (or data
2521                    after-article))
2522           (let ((odata gnus-newsgroup-data))
2523             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2524             (when offset
2525               (gnus-data-update-list odata offset)))
2526       ;; Find the last element in the list to be spliced into the main
2527         ;; list.
2528         (while (cdr list)
2529           (setq list (cdr list)))
2530         (if (not data)
2531             (progn
2532               (setcdr list gnus-newsgroup-data)
2533               (setq gnus-newsgroup-data ilist)
2534               (when offset
2535                 (gnus-data-update-list (cdr list) offset)))
2536           (setcdr list (cdr data))
2537           (setcdr data ilist)
2538           (when offset
2539             (gnus-data-update-list (cdr list) offset))))
2540       (setq gnus-newsgroup-data-reverse nil))))
2541
2542 (defun gnus-data-remove (article &optional offset)
2543   (let ((data gnus-newsgroup-data))
2544     (if (= (gnus-data-number (car data)) article)
2545         (progn
2546           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2547                 gnus-newsgroup-data-reverse nil)
2548           (when offset
2549             (gnus-data-update-list gnus-newsgroup-data offset)))
2550       (while (cdr data)
2551         (when (= (gnus-data-number (cadr data)) article)
2552           (setcdr data (cddr data))
2553           (when offset
2554             (gnus-data-update-list (cdr data) offset))
2555           (setq data nil
2556                 gnus-newsgroup-data-reverse nil))
2557         (setq data (cdr data))))))
2558
2559 (defmacro gnus-data-list (backward)
2560   `(if ,backward
2561        (or gnus-newsgroup-data-reverse
2562            (setq gnus-newsgroup-data-reverse
2563                  (reverse gnus-newsgroup-data)))
2564      gnus-newsgroup-data))
2565
2566 (defun gnus-data-update-list (data offset)
2567   "Add OFFSET to the POS of all data entries in DATA."
2568   (setq gnus-newsgroup-data-reverse nil)
2569   (while data
2570     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2571     (setq data (cdr data))))
2572
2573 (defun gnus-summary-article-pseudo-p (article)
2574   "Say whether this article is a pseudo article or not."
2575   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2576
2577 (defmacro gnus-summary-article-sparse-p (article)
2578   "Say whether this article is a sparse article or not."
2579   `(memq ,article gnus-newsgroup-sparse))
2580
2581 (defmacro gnus-summary-article-ancient-p (article)
2582   "Say whether this article is a sparse article or not."
2583   `(memq ,article gnus-newsgroup-ancient))
2584
2585 (defun gnus-article-parent-p (number)
2586   "Say whether this article is a parent or not."
2587   (let ((data (gnus-data-find-list number)))
2588     (and (cdr data)              ; There has to be an article after...
2589          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2590             (gnus-data-level (nth 1 data))))))
2591
2592 (defun gnus-article-children (number)
2593   "Return a list of all children to NUMBER."
2594   (let* ((data (gnus-data-find-list number))
2595          (level (gnus-data-level (car data)))
2596          children)
2597     (setq data (cdr data))
2598     (while (and data
2599                 (= (gnus-data-level (car data)) (1+ level)))
2600       (push (gnus-data-number (car data)) children)
2601       (setq data (cdr data)))
2602     children))
2603
2604 (defmacro gnus-summary-skip-intangible ()
2605   "If the current article is intangible, then jump to a different article."
2606   '(let ((to (get-text-property (point) 'gnus-intangible)))
2607      (and to (gnus-summary-goto-subject to))))
2608
2609 (defmacro gnus-summary-article-intangible-p ()
2610   "Say whether this article is intangible or not."
2611   '(get-text-property (point) 'gnus-intangible))
2612
2613 (defun gnus-article-read-p (article)
2614   "Say whether ARTICLE is read or not."
2615   (not (or (memq article gnus-newsgroup-marked)
2616            (memq article gnus-newsgroup-unreads)
2617            (memq article gnus-newsgroup-unselected)
2618            (memq article gnus-newsgroup-dormant))))
2619
2620 ;; Some summary mode macros.
2621
2622 (defmacro gnus-summary-article-number ()
2623   "The article number of the article on the current line.
2624 If there isn's an article number here, then we return the current
2625 article number."
2626   '(progn
2627      (gnus-summary-skip-intangible)
2628      (or (get-text-property (point) 'gnus-number)
2629          (gnus-summary-last-subject))))
2630
2631 (defmacro gnus-summary-article-header (&optional number)
2632   "Return the header of article NUMBER."
2633   `(gnus-data-header (gnus-data-find
2634                       ,(or number '(gnus-summary-article-number)))))
2635
2636 (defmacro gnus-summary-thread-level (&optional number)
2637   "Return the level of thread that starts with article NUMBER."
2638   `(if (and (eq gnus-summary-make-false-root 'dummy)
2639             (get-text-property (point) 'gnus-intangible))
2640        0
2641      (gnus-data-level (gnus-data-find
2642                        ,(or number '(gnus-summary-article-number))))))
2643
2644 (defmacro gnus-summary-article-mark (&optional number)
2645   "Return the mark of article NUMBER."
2646   `(gnus-data-mark (gnus-data-find
2647                     ,(or number '(gnus-summary-article-number)))))
2648
2649 (defmacro gnus-summary-article-pos (&optional number)
2650   "Return the position of the line of article NUMBER."
2651   `(gnus-data-pos (gnus-data-find
2652                    ,(or number '(gnus-summary-article-number)))))
2653
2654 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2655 (defmacro gnus-summary-article-subject (&optional number)
2656   "Return current subject string or nil if nothing."
2657   `(let ((headers
2658           ,(if number
2659                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2660              '(gnus-data-header (assq (gnus-summary-article-number)
2661                                       gnus-newsgroup-data)))))
2662      (and headers
2663           (vectorp headers)
2664           (mail-header-subject headers))))
2665
2666 (defmacro gnus-summary-article-score (&optional number)
2667   "Return current article score."
2668   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2669                   gnus-newsgroup-scored))
2670        gnus-summary-default-score 0))
2671
2672 (defun gnus-summary-article-children (&optional number)
2673   "Return a list of article numbers that are children of article NUMBER."
2674   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2675          (level (gnus-data-level (car data)))
2676          l children)
2677     (while (and (setq data (cdr data))
2678                 (> (setq l (gnus-data-level (car data))) level))
2679       (and (= (1+ level) l)
2680            (push (gnus-data-number (car data))
2681                  children)))
2682     (nreverse children)))
2683
2684 (defun gnus-summary-article-parent (&optional number)
2685   "Return the article number of the parent of article NUMBER."
2686   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2687                                     (gnus-data-list t)))
2688          (level (gnus-data-level (car data))))
2689     (if (zerop level)
2690         ()                              ; This is a root.
2691       ;; We search until we find an article with a level less than
2692       ;; this one.  That function has to be the parent.
2693       (while (and (setq data (cdr data))
2694                   (not (< (gnus-data-level (car data)) level))))
2695       (and data (gnus-data-number (car data))))))
2696
2697 (defun gnus-unread-mark-p (mark)
2698   "Say whether MARK is the unread mark."
2699   (= mark gnus-unread-mark))
2700
2701 (defun gnus-read-mark-p (mark)
2702   "Say whether MARK is one of the marks that mark as read.
2703 This is all marks except unread, ticked, dormant, and expirable."
2704   (not (or (= mark gnus-unread-mark)
2705            (= mark gnus-ticked-mark)
2706            (= mark gnus-dormant-mark)
2707            (= mark gnus-expirable-mark))))
2708
2709 (defmacro gnus-article-mark (number)
2710   "Return the MARK of article NUMBER.
2711 This macro should only be used when computing the mark the \"first\"
2712 time; i.e., when generating the summary lines.  After that,
2713 `gnus-summary-article-mark' should be used to examine the
2714 marks of articles."
2715   `(cond
2716     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2717     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2718     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2719     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2720     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2721     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2722     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2723     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2724            gnus-ancient-mark))))
2725
2726 ;; Saving hidden threads.
2727
2728 (defmacro gnus-save-hidden-threads (&rest forms)
2729   "Save hidden threads, eval FORMS, and restore the hidden threads."
2730   (let ((config (make-symbol "config")))
2731     `(let ((,config (gnus-hidden-threads-configuration)))
2732        (unwind-protect
2733            (save-excursion
2734              ,@forms)
2735          (gnus-restore-hidden-threads-configuration ,config)))))
2736 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2737 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2738
2739 (defun gnus-data-compute-positions ()
2740   "Compute the positions of all articles."
2741   (setq gnus-newsgroup-data-reverse nil)
2742   (let ((data gnus-newsgroup-data))
2743     (save-excursion
2744       (gnus-save-hidden-threads
2745         (gnus-summary-show-all-threads)
2746         (goto-char (point-min))
2747         (while data
2748           (while (get-text-property (point) 'gnus-intangible)
2749             (forward-line 1))
2750           (gnus-data-set-pos (car data) (+ (point) 3))
2751           (setq data (cdr data))
2752           (forward-line 1))))))
2753
2754 (defun gnus-hidden-threads-configuration ()
2755   "Return the current hidden threads configuration."
2756   (save-excursion
2757     (let (config)
2758       (goto-char (point-min))
2759       (while (search-forward "\r" nil t)
2760         (push (1- (point)) config))
2761       config)))
2762
2763 (defun gnus-restore-hidden-threads-configuration (config)
2764   "Restore hidden threads configuration from CONFIG."
2765   (save-excursion
2766     (let (point buffer-read-only)
2767       (while (setq point (pop config))
2768         (when (and (< point (point-max))
2769                    (goto-char point)
2770                    (eq (char-after) ?\n))
2771           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2772
2773 ;; Various summary mode internalish functions.
2774
2775 (defun gnus-mouse-pick-article (e)
2776   (interactive "e")
2777   (mouse-set-point e)
2778   (gnus-summary-next-page nil t))
2779
2780 (defun gnus-summary-set-display-table ()
2781   "Change the display table.
2782 Odd characters have a tendency to mess
2783 up nicely formatted displays - we make all possible glyphs
2784 display only a single character."
2785
2786   ;; We start from the standard display table, if any.
2787   (let ((table (or (copy-sequence standard-display-table)
2788                    (make-display-table)))
2789         (i 32))
2790     ;; Nix out all the control chars...
2791     (while (>= (setq i (1- i)) 0)
2792       (aset table i [??]))
2793    ;; ... but not newline and cr, of course.  (cr is necessary for the
2794     ;; selective display).
2795     (aset table ?\n nil)
2796     (aset table ?\r nil)
2797     ;; We keep TAB as well.
2798     (aset table ?\t nil)
2799     ;; We nix out any glyphs over 126 that are not set already.
2800     (let ((i 256))
2801       (while (>= (setq i (1- i)) 127)
2802         ;; Only modify if the entry is nil.
2803         (unless (aref table i)
2804           (aset table i [??]))))
2805     (setq buffer-display-table table)))
2806
2807 (defun gnus-summary-set-article-display-arrow (pos)
2808   "Update the overlay arrow to point to line at position POS."
2809   (when (and gnus-summary-display-arrow
2810              (boundp 'overlay-arrow-position)
2811              (boundp 'overlay-arrow-string))
2812     (save-excursion
2813       (goto-char pos)
2814       (beginning-of-line)
2815       (unless overlay-arrow-position
2816         (setq overlay-arrow-position (make-marker)))
2817       (setq overlay-arrow-string "=>"
2818             overlay-arrow-position (set-marker overlay-arrow-position
2819                                                (point)
2820                                                (current-buffer))))))
2821
2822 (defun gnus-summary-buffer-name (group)
2823   "Return the summary buffer name of GROUP."
2824   (concat "*Summary " (gnus-group-decoded-name group) "*"))
2825
2826 (defun gnus-summary-setup-buffer (group)
2827   "Initialize summary buffer."
2828   (let ((buffer (gnus-summary-buffer-name group))
2829         (dead-name (concat "*Dead Summary "
2830                            (gnus-group-decoded-name group) "*")))
2831     ;; If a dead summary buffer exists, we kill it.
2832     (when (gnus-buffer-live-p dead-name)
2833       (gnus-kill-buffer dead-name))
2834     (if (get-buffer buffer)
2835         (progn
2836           (set-buffer buffer)
2837           (setq gnus-summary-buffer (current-buffer))
2838           (not gnus-newsgroup-prepared))
2839       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2840       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2841       (gnus-summary-mode group)
2842       (when gnus-carpal
2843         (gnus-carpal-setup-buffer 'summary))
2844       (unless gnus-single-article-buffer
2845         (make-local-variable 'gnus-article-buffer)
2846         (make-local-variable 'gnus-article-current)
2847         (make-local-variable 'gnus-original-article-buffer))
2848       (setq gnus-newsgroup-name group)
2849       ;; Set any local variables in the group parameters.
2850       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2851       t)))
2852
2853 (defun gnus-set-global-variables ()
2854   "Set the global equivalents of the buffer-local variables.
2855 They are set to the latest values they had.  These reflect the summary
2856 buffer that was in action when the last article was fetched."
2857   (when (eq major-mode 'gnus-summary-mode)
2858     (setq gnus-summary-buffer (current-buffer))
2859     (let ((name gnus-newsgroup-name)
2860           (marked gnus-newsgroup-marked)
2861           (unread gnus-newsgroup-unreads)
2862           (headers gnus-current-headers)
2863           (data gnus-newsgroup-data)
2864           (summary gnus-summary-buffer)
2865           (article-buffer gnus-article-buffer)
2866           (original gnus-original-article-buffer)
2867           (gac gnus-article-current)
2868           (reffed gnus-reffed-article-number)
2869           (score-file gnus-current-score-file)
2870           (default-charset gnus-newsgroup-charset)
2871           vlist)
2872       (let ((locals gnus-newsgroup-variables))
2873         (while locals
2874           (if (consp (car locals))
2875               (push (eval (caar locals)) vlist)
2876             (push (eval (car locals)) vlist))
2877           (setq locals (cdr locals)))
2878         (setq vlist (nreverse vlist)))
2879       (save-excursion
2880         (set-buffer gnus-group-buffer)
2881         (setq gnus-newsgroup-name name
2882               gnus-newsgroup-marked marked
2883               gnus-newsgroup-unreads unread
2884               gnus-current-headers headers
2885               gnus-newsgroup-data data
2886               gnus-article-current gac
2887               gnus-summary-buffer summary
2888               gnus-article-buffer article-buffer
2889               gnus-original-article-buffer original
2890               gnus-reffed-article-number reffed
2891               gnus-current-score-file score-file
2892               gnus-newsgroup-charset default-charset)
2893         (let ((locals gnus-newsgroup-variables))
2894           (while locals
2895             (if (consp (car locals))
2896                 (set (caar locals) (pop vlist))
2897               (set (car locals) (pop vlist)))
2898             (setq locals (cdr locals))))
2899         ;; The article buffer also has local variables.
2900         (when (gnus-buffer-live-p gnus-article-buffer)
2901           (set-buffer gnus-article-buffer)
2902           (setq gnus-summary-buffer summary))))))
2903
2904 (defun gnus-summary-article-unread-p (article)
2905   "Say whether ARTICLE is unread or not."
2906   (memq article gnus-newsgroup-unreads))
2907
2908 (defun gnus-summary-first-article-p (&optional article)
2909   "Return whether ARTICLE is the first article in the buffer."
2910   (if (not (setq article (or article (gnus-summary-article-number))))
2911       nil
2912     (eq article (caar gnus-newsgroup-data))))
2913
2914 (defun gnus-summary-last-article-p (&optional article)
2915   "Return whether ARTICLE is the last article in the buffer."
2916   (if (not (setq article (or article (gnus-summary-article-number))))
2917       ;; All non-existent numbers are the last article.  :-)
2918       t
2919     (not (cdr (gnus-data-find-list article)))))
2920
2921 (defun gnus-make-thread-indent-array ()
2922   (let ((n 200))
2923     (unless (and gnus-thread-indent-array
2924                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2925       (setq gnus-thread-indent-array (make-vector 201 "")
2926             gnus-thread-indent-array-level gnus-thread-indent-level)
2927       (while (>= n 0)
2928         (aset gnus-thread-indent-array n
2929               (make-string (* n gnus-thread-indent-level) ? ))
2930         (setq n (1- n))))))
2931
2932 (defun gnus-update-summary-mark-positions ()
2933   "Compute where the summary marks are to go."
2934   (save-excursion
2935     (when (gnus-buffer-exists-p gnus-summary-buffer)
2936       (set-buffer gnus-summary-buffer))
2937     (let ((gnus-replied-mark 129)
2938           (gnus-score-below-mark 130)
2939           (gnus-score-over-mark 130)
2940           (gnus-download-mark 131)
2941           (spec gnus-summary-line-format-spec)
2942           gnus-visual pos)
2943       (save-excursion
2944         (gnus-set-work-buffer)
2945         (let ((gnus-summary-line-format-spec spec)
2946               (gnus-newsgroup-downloadable '((0 . t))))
2947           (gnus-summary-insert-line
2948            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
2949            0 nil 128 t nil "" nil 1)
2950           (goto-char (point-min))
2951           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2952                                              (- (point) 2)))))
2953           (goto-char (point-min))
2954           (push (cons 'replied (and (search-forward "\201" nil t)
2955                                     (- (point) 2)))
2956                 pos)
2957           (goto-char (point-min))
2958           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2959                 pos)
2960           (goto-char (point-min))
2961           (push (cons 'download
2962                       (and (search-forward "\203" nil t) (- (point) 2)))
2963                 pos)))
2964       (setq gnus-summary-mark-positions pos))))
2965
2966 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2967   "Insert a dummy root in the summary buffer."
2968   (beginning-of-line)
2969   (gnus-add-text-properties
2970    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2971    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2972
2973 (defun gnus-summary-extract-address-component (from)
2974   (or (car (funcall gnus-extract-address-components from))
2975       from))
2976
2977 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
2978   (let ((mail-parse-charset gnus-newsgroup-charset)
2979         ; Is it really necessary to do this next part for each summary line?
2980         ; Luckily, doesn't seem to slow things down much.
2981         (mail-parse-ignored-charsets
2982          (save-excursion (set-buffer gnus-summary-buffer)
2983                          gnus-newsgroup-ignored-charsets)))
2984     (or
2985      (and gnus-ignored-from-addresses
2986           (string-match gnus-ignored-from-addresses gnus-tmp-from)
2987           (let ((extra-headers (mail-header-extra header))
2988                 to
2989                 newsgroups)
2990             (cond
2991              ((setq to (cdr (assq 'To extra-headers)))
2992               (concat "-> "
2993                       (inline
2994                         (gnus-summary-extract-address-component
2995                          (funcall gnus-decode-encoded-word-function to)))))
2996              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
2997               (concat "=> " newsgroups)))))
2998      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
2999
3000 (defun gnus-summary-insert-line (gnus-tmp-header
3001                                  gnus-tmp-level gnus-tmp-current
3002                                  gnus-tmp-unread gnus-tmp-replied
3003                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3004                                  &optional gnus-tmp-dummy gnus-tmp-score
3005                                  gnus-tmp-process)
3006   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3007          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3008          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3009          (gnus-tmp-score-char
3010           (if (or (null gnus-summary-default-score)
3011                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3012                       gnus-summary-zcore-fuzz))
3013               ?                         ;Whitespace
3014             (if (< gnus-tmp-score gnus-summary-default-score)
3015                 gnus-score-below-mark gnus-score-over-mark)))
3016          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3017          (gnus-tmp-replied
3018           (cond (gnus-tmp-process gnus-process-mark)
3019                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3020                  gnus-cached-mark)
3021                 (gnus-tmp-replied gnus-replied-mark)
3022                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3023                  gnus-forwarded-mark)
3024                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3025                  gnus-saved-mark)
3026                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3027                  gnus-recent-mark)
3028                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3029                  gnus-unseen-mark)
3030                 (t gnus-no-mark)))
3031          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3032          (gnus-tmp-name
3033           (cond
3034            ((string-match "<[^>]+> *$" gnus-tmp-from)
3035             (let ((beg (match-beginning 0)))
3036               (or (and (string-match "^\".+\"" gnus-tmp-from)
3037                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3038                   (substring gnus-tmp-from 0 beg))))
3039            ((string-match "(.+)" gnus-tmp-from)
3040             (substring gnus-tmp-from
3041                        (1+ (match-beginning 0)) (1- (match-end 0))))
3042            (t gnus-tmp-from)))
3043          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3044          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3045          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3046          (buffer-read-only nil))
3047     (when (string= gnus-tmp-name "")
3048       (setq gnus-tmp-name gnus-tmp-from))
3049     (unless (numberp gnus-tmp-lines)
3050       (setq gnus-tmp-lines -1))
3051     (if (= gnus-tmp-lines -1)
3052         (setq gnus-tmp-lines "?")
3053       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3054     (gnus-put-text-property
3055      (point)
3056      (progn (eval gnus-summary-line-format-spec) (point))
3057      'gnus-number gnus-tmp-number)
3058     (when (gnus-visual-p 'summary-highlight 'highlight)
3059       (forward-line -1)
3060       (gnus-run-hooks 'gnus-summary-update-hook)
3061       (forward-line 1))))
3062
3063 (defun gnus-summary-update-line (&optional dont-update)
3064   "Update summary line after change."
3065   (when (and gnus-summary-default-score
3066              (not gnus-summary-inhibit-highlight))
3067     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3068            (article (gnus-summary-article-number))
3069            (score (gnus-summary-article-score article)))
3070       (unless dont-update
3071         (if (and gnus-summary-mark-below
3072                  (< (gnus-summary-article-score)
3073                     gnus-summary-mark-below))
3074             ;; This article has a low score, so we mark it as read.
3075             (when (memq article gnus-newsgroup-unreads)
3076               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3077           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3078             ;; This article was previously marked as read on account
3079             ;; of a low score, but now it has risen, so we mark it as
3080             ;; unread.
3081             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3082         (gnus-summary-update-mark
3083          (if (or (null gnus-summary-default-score)
3084                  (<= (abs (- score gnus-summary-default-score))
3085                      gnus-summary-zcore-fuzz))
3086              ?                          ;Whitespace
3087            (if (< score gnus-summary-default-score)
3088                gnus-score-below-mark gnus-score-over-mark))
3089          'score))
3090       ;; Do visual highlighting.
3091       (when (gnus-visual-p 'summary-highlight 'highlight)
3092         (gnus-run-hooks 'gnus-summary-update-hook)))))
3093
3094 (defvar gnus-tmp-new-adopts nil)
3095
3096 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3097   "Return the number of articles in THREAD.
3098 This may be 0 in some cases -- if none of the articles in
3099 the thread are to be displayed."
3100   (let* ((number
3101          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3102           (cond
3103            ((not (listp thread))
3104             1)
3105            ((and (consp thread) (cdr thread))
3106             (apply
3107              '+ 1 (mapcar
3108                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3109            ((null thread)
3110             1)
3111            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3112             1)
3113            (t 0))))
3114     (when (and level (zerop level) gnus-tmp-new-adopts)
3115       (incf number
3116             (apply '+ (mapcar
3117                        'gnus-summary-number-of-articles-in-thread
3118                        gnus-tmp-new-adopts))))
3119     (if char
3120         (if (> number 1) gnus-not-empty-thread-mark
3121           gnus-empty-thread-mark)
3122       number)))
3123
3124 (defun gnus-summary-set-local-parameters (group)
3125   "Go through the local params of GROUP and set all variable specs in that list."
3126   (let ((params (gnus-group-find-parameter group))
3127         (vars '(quit-config))           ; Ignore quit-config.
3128         elem)
3129     (while params
3130       (setq elem (car params)
3131             params (cdr params))
3132       (and (consp elem)                 ; Has to be a cons.
3133            (consp (cdr elem))           ; The cdr has to be a list.
3134            (symbolp (car elem))         ; Has to be a symbol in there.
3135            (not (memq (car elem) vars))
3136            (ignore-errors               ; So we set it.
3137              (push (car elem) vars)
3138              (make-local-variable (car elem))
3139              (set (car elem) (eval (nth 1 elem))))))))
3140
3141 (defun gnus-summary-read-group (group &optional show-all no-article
3142                                       kill-buffer no-display backward
3143                                       select-articles)
3144   "Start reading news in newsgroup GROUP.
3145 If SHOW-ALL is non-nil, already read articles are also listed.
3146 If NO-ARTICLE is non-nil, no article is selected initially.
3147 If NO-DISPLAY, don't generate a summary buffer."
3148   (let (result)
3149     (while (and group
3150                 (null (setq result
3151                             (let ((gnus-auto-select-next nil))
3152                               (or (gnus-summary-read-group-1
3153                                    group show-all no-article
3154                                    kill-buffer no-display
3155                                    select-articles)
3156                                   (setq show-all nil
3157                                         select-articles nil)))))
3158                 (eq gnus-auto-select-next 'quietly))
3159       (set-buffer gnus-group-buffer)
3160       ;; The entry function called above goes to the next
3161       ;; group automatically, so we go two groups back
3162       ;; if we are searching for the previous group.
3163       (when backward
3164         (gnus-group-prev-unread-group 2))
3165       (if (not (equal group (gnus-group-group-name)))
3166           (setq group (gnus-group-group-name))
3167         (setq group nil)))
3168     result))
3169
3170 (defun gnus-summary-read-group-1 (group show-all no-article
3171                                         kill-buffer no-display
3172                                         &optional select-articles)
3173   ;; Killed foreign groups can't be entered.
3174   ;;  (when (and (not (gnus-group-native-p group))
3175   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3176   ;;    (error "Dead non-native groups can't be entered"))
3177   (gnus-message 5 "Retrieving newsgroup: %s..."
3178                 (gnus-group-decoded-name group))
3179   (let* ((new-group (gnus-summary-setup-buffer group))
3180          (quit-config (gnus-group-quit-config group))
3181          (did-select (and new-group (gnus-select-newsgroup
3182                                      group show-all select-articles))))
3183     (cond
3184      ;; This summary buffer exists already, so we just select it.
3185      ((not new-group)
3186       (gnus-set-global-variables)
3187       (when kill-buffer
3188         (gnus-kill-or-deaden-summary kill-buffer))
3189       (gnus-configure-windows 'summary 'force)
3190       (gnus-set-mode-line 'summary)
3191       (gnus-summary-position-point)
3192       (message "")
3193       t)
3194      ;; We couldn't select this group.
3195      ((null did-select)
3196       (when (and (eq major-mode 'gnus-summary-mode)
3197                  (not (equal (current-buffer) kill-buffer)))
3198         (kill-buffer (current-buffer))
3199         (if (not quit-config)
3200             (progn
3201               ;; Update the info -- marks might need to be removed,
3202               ;; for instance.
3203               (gnus-summary-update-info)
3204               (set-buffer gnus-group-buffer)
3205               (gnus-group-jump-to-group group)
3206               (gnus-group-next-unread-group 1))
3207           (gnus-handle-ephemeral-exit quit-config)))
3208       (let ((grpinfo (gnus-get-info group)))
3209         (if (null (gnus-info-read grpinfo))
3210             (gnus-message 3 "Group %s contains no messages"
3211                           (gnus-group-decoded-name group))
3212           (gnus-message 3 "Can't select group")))
3213       nil)
3214      ;; The user did a `C-g' while prompting for number of articles,
3215      ;; so we exit this group.
3216      ((eq did-select 'quit)
3217       (and (eq major-mode 'gnus-summary-mode)
3218            (not (equal (current-buffer) kill-buffer))
3219            (kill-buffer (current-buffer)))
3220       (when kill-buffer
3221         (gnus-kill-or-deaden-summary kill-buffer))
3222       (if (not quit-config)
3223           (progn
3224             (set-buffer gnus-group-buffer)
3225             (gnus-group-jump-to-group group)
3226             (gnus-group-next-unread-group 1)
3227             (gnus-configure-windows 'group 'force))
3228         (gnus-handle-ephemeral-exit quit-config))
3229       ;; Finally signal the quit.
3230       (signal 'quit nil))
3231      ;; The group was successfully selected.
3232      (t
3233       (gnus-set-global-variables)
3234       ;; Save the active value in effect when the group was entered.
3235       (setq gnus-newsgroup-active
3236             (gnus-copy-sequence
3237              (gnus-active gnus-newsgroup-name)))
3238       ;; You can change the summary buffer in some way with this hook.
3239       (gnus-run-hooks 'gnus-select-group-hook)
3240       (gnus-update-format-specifications
3241        nil 'summary 'summary-mode 'summary-dummy)
3242       (gnus-update-summary-mark-positions)
3243       ;; Do score processing.
3244       (when gnus-use-scoring
3245         (gnus-possibly-score-headers))
3246       ;; Check whether to fill in the gaps in the threads.
3247       (when gnus-build-sparse-threads
3248         (gnus-build-sparse-threads))
3249       ;; Find the initial limit.
3250       (if gnus-show-threads
3251           (if show-all
3252               (let ((gnus-newsgroup-dormant nil))
3253                 (gnus-summary-initial-limit show-all))
3254             (gnus-summary-initial-limit show-all))
3255         ;; When untreaded, all articles are always shown.
3256         (setq gnus-newsgroup-limit
3257               (mapcar
3258                (lambda (header) (mail-header-number header))
3259                gnus-newsgroup-headers)))
3260       ;; Generate the summary buffer.
3261       (unless no-display
3262         (gnus-summary-prepare))
3263       (when gnus-use-trees
3264         (gnus-tree-open group)
3265         (setq gnus-summary-highlight-line-function
3266               'gnus-tree-highlight-article))
3267       ;; If the summary buffer is empty, but there are some low-scored
3268       ;; articles or some excluded dormants, we include these in the
3269       ;; buffer.
3270       (when (and (zerop (buffer-size))
3271                  (not no-display))
3272         (cond (gnus-newsgroup-dormant
3273                (gnus-summary-limit-include-dormant))
3274               ((and gnus-newsgroup-scored show-all)
3275                (gnus-summary-limit-include-expunged t))))
3276       ;; Function `gnus-apply-kill-file' must be called in this hook.
3277       (gnus-run-hooks 'gnus-apply-kill-hook)
3278       (if (and (zerop (buffer-size))
3279                (not no-display))
3280           (progn
3281             ;; This newsgroup is empty.
3282             (gnus-summary-catchup-and-exit nil t)
3283             (gnus-message 6 "No unread news")
3284             (when kill-buffer
3285               (gnus-kill-or-deaden-summary kill-buffer))
3286             ;; Return nil from this function.
3287             nil)
3288         ;; Hide conversation thread subtrees.  We cannot do this in
3289         ;; gnus-summary-prepare-hook since kill processing may not
3290         ;; work with hidden articles.
3291         (gnus-summary-maybe-hide-threads)
3292         (when kill-buffer
3293           (gnus-kill-or-deaden-summary kill-buffer))
3294         (gnus-summary-auto-select-subject)
3295         ;; Show first unread article if requested.
3296         (if (and (not no-article)
3297                  (not no-display)
3298                  gnus-newsgroup-unreads
3299                  gnus-auto-select-first)
3300             (progn
3301               (gnus-configure-windows 'summary)
3302               (let ((art (gnus-summary-article-number)))
3303                 (unless (or (memq art gnus-newsgroup-undownloaded)
3304                             (memq art gnus-newsgroup-downloadable))
3305                   (gnus-summary-goto-article art))))
3306           ;; Don't select any articles.
3307           (gnus-summary-position-point)
3308           (gnus-configure-windows 'summary 'force)
3309           (gnus-set-mode-line 'summary))
3310         (when (get-buffer-window gnus-group-buffer t)
3311           ;; Gotta use windows, because recenter does weird stuff if
3312           ;; the current buffer ain't the displayed window.
3313           (let ((owin (selected-window)))
3314             (select-window (get-buffer-window gnus-group-buffer t))
3315             (when (gnus-group-goto-group group)
3316               (recenter))
3317             (select-window owin)))
3318         ;; Mark this buffer as "prepared".
3319         (setq gnus-newsgroup-prepared t)
3320         (gnus-run-hooks 'gnus-summary-prepared-hook)
3321         t)))))
3322
3323 (defun gnus-summary-auto-select-subject ()
3324   "Select the subject line on initial group entry."
3325   (goto-char (point-min))
3326   (cond
3327    ((eq gnus-auto-select-subject 'best)
3328     (gnus-summary-best-unread-subject))
3329    ((eq gnus-auto-select-subject 'unread)
3330     (gnus-summary-first-unread-subject))
3331    ((eq gnus-auto-select-subject 'unseen)
3332     (gnus-summary-first-unseen-subject))
3333    ((eq gnus-auto-select-subject 'unseen-or-unread)
3334     (gnus-summary-first-unseen-or-unread-subject))
3335    ((eq gnus-auto-select-subject 'first)
3336     ;; Do nothing.
3337     )
3338    ((gnus-functionp gnus-auto-select-subject)
3339     (funcall gnus-auto-select-subject))))
3340
3341 (defun gnus-summary-prepare ()
3342   "Generate the summary buffer."
3343   (interactive)
3344   (let ((buffer-read-only nil))
3345     (erase-buffer)
3346     (setq gnus-newsgroup-data nil
3347           gnus-newsgroup-data-reverse nil)
3348     (gnus-run-hooks 'gnus-summary-generate-hook)
3349     ;; Generate the buffer, either with threads or without.
3350     (when gnus-newsgroup-headers
3351       (gnus-summary-prepare-threads
3352        (if gnus-show-threads
3353            (gnus-sort-gathered-threads
3354             (funcall gnus-summary-thread-gathering-function
3355                      (gnus-sort-threads
3356                       (gnus-cut-threads (gnus-make-threads)))))
3357          ;; Unthreaded display.
3358          (gnus-sort-articles gnus-newsgroup-headers))))
3359     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3360     ;; Call hooks for modifying summary buffer.
3361     (goto-char (point-min))
3362     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3363
3364 (defsubst gnus-general-simplify-subject (subject)
3365   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3366   (setq subject
3367         (cond
3368          ;; Truncate the subject.
3369          (gnus-simplify-subject-functions
3370           (gnus-map-function gnus-simplify-subject-functions subject))
3371          ((numberp gnus-summary-gather-subject-limit)
3372           (setq subject (gnus-simplify-subject-re subject))
3373           (if (> (length subject) gnus-summary-gather-subject-limit)
3374               (substring subject 0 gnus-summary-gather-subject-limit)
3375             subject))
3376          ;; Fuzzily simplify it.
3377          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3378           (gnus-simplify-subject-fuzzy subject))
3379          ;; Just remove the leading "Re:".
3380          (t
3381           (gnus-simplify-subject-re subject))))
3382
3383   (if (and gnus-summary-gather-exclude-subject
3384            (string-match gnus-summary-gather-exclude-subject subject))
3385       nil                         ; This article shouldn't be gathered
3386     subject))
3387
3388 (defun gnus-summary-simplify-subject-query ()
3389   "Query where the respool algorithm would put this article."
3390   (interactive)
3391   (gnus-summary-select-article)
3392   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3393
3394 (defun gnus-gather-threads-by-subject (threads)
3395   "Gather threads by looking at Subject headers."
3396   (if (not gnus-summary-make-false-root)
3397       threads
3398     (let ((hashtb (gnus-make-hashtable 1024))
3399           (prev threads)
3400           (result threads)
3401           subject hthread whole-subject)
3402       (while threads
3403         (setq subject (gnus-general-simplify-subject
3404                        (setq whole-subject (mail-header-subject
3405                                             (caar threads)))))
3406         (when subject
3407           (if (setq hthread (gnus-gethash subject hashtb))
3408               (progn
3409                 ;; We enter a dummy root into the thread, if we
3410                 ;; haven't done that already.
3411                 (unless (stringp (caar hthread))
3412                   (setcar hthread (list whole-subject (car hthread))))
3413                 ;; We add this new gathered thread to this gathered
3414                 ;; thread.
3415                 (setcdr (car hthread)
3416                         (nconc (cdar hthread) (list (car threads))))
3417                 ;; Remove it from the list of threads.
3418                 (setcdr prev (cdr threads))
3419                 (setq threads prev))
3420             ;; Enter this thread into the hash table.
3421             (gnus-sethash subject threads hashtb)))
3422         (setq prev threads)
3423         (setq threads (cdr threads)))
3424       result)))
3425
3426 (defun gnus-gather-threads-by-references (threads)
3427   "Gather threads by looking at References headers."
3428   (let ((idhashtb (gnus-make-hashtable 1024))
3429         (thhashtb (gnus-make-hashtable 1024))
3430         (prev threads)
3431         (result threads)
3432         ids references id gthread gid entered ref)
3433     (while threads
3434       (when (setq references (mail-header-references (caar threads)))
3435         (setq id (mail-header-id (caar threads))
3436               ids (inline (gnus-split-references references))
3437               entered nil)
3438         (while (setq ref (pop ids))
3439           (setq ids (delete ref ids))
3440           (if (not (setq gid (gnus-gethash ref idhashtb)))
3441               (progn
3442                 (gnus-sethash ref id idhashtb)
3443                 (gnus-sethash id threads thhashtb))
3444             (setq gthread (gnus-gethash gid thhashtb))
3445             (unless entered
3446               ;; We enter a dummy root into the thread, if we
3447               ;; haven't done that already.
3448               (unless (stringp (caar gthread))
3449                 (setcar gthread (list (mail-header-subject (caar gthread))
3450                                       (car gthread))))
3451               ;; We add this new gathered thread to this gathered
3452               ;; thread.
3453               (setcdr (car gthread)
3454                       (nconc (cdar gthread) (list (car threads)))))
3455             ;; Add it into the thread hash table.
3456             (gnus-sethash id gthread thhashtb)
3457             (setq entered t)
3458             ;; Remove it from the list of threads.
3459             (setcdr prev (cdr threads))
3460             (setq threads prev))))
3461       (setq prev threads)
3462       (setq threads (cdr threads)))
3463     result))
3464
3465 (defun gnus-sort-gathered-threads (threads)
3466   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3467   (let ((result threads))
3468     (while threads
3469       (when (stringp (caar threads))
3470         (setcdr (car threads)
3471                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3472       (setq threads (cdr threads)))
3473     result))
3474
3475 (defun gnus-thread-loop-p (root thread)
3476   "Say whether ROOT is in THREAD."
3477   (let ((stack (list thread))
3478         (infloop 0)
3479         th)
3480     (while (setq thread (pop stack))
3481       (setq th (cdr thread))
3482       (while (and th
3483                   (not (eq (caar th) root)))
3484         (pop th))
3485       (if th
3486           ;; We have found a loop.
3487           (let (ref-dep)
3488             (setcdr thread (delq (car th) (cdr thread)))
3489             (if (boundp (setq ref-dep (intern "none"
3490                                               gnus-newsgroup-dependencies)))
3491                 (setcdr (symbol-value ref-dep)
3492                         (nconc (cdr (symbol-value ref-dep))
3493                                (list (car th))))
3494               (set ref-dep (list nil (car th))))
3495             (setq infloop 1
3496                   stack nil))
3497         ;; Push all the subthreads onto the stack.
3498         (push (cdr thread) stack)))
3499     infloop))
3500
3501 (defun gnus-make-threads ()
3502   "Go through the dependency hashtb and find the roots.  Return all threads."
3503   (let (threads)
3504     (while (catch 'infloop
3505              (mapatoms
3506               (lambda (refs)
3507                 ;; Deal with self-referencing References loops.
3508                 (when (and (car (symbol-value refs))
3509                            (not (zerop
3510                                  (apply
3511                                   '+
3512                                   (mapcar
3513                                    (lambda (thread)
3514                                      (gnus-thread-loop-p
3515                                       (car (symbol-value refs)) thread))
3516                                    (cdr (symbol-value refs)))))))
3517                   (setq threads nil)
3518                   (throw 'infloop t))
3519                 (unless (car (symbol-value refs))
3520              ;; These threads do not refer back to any other articles,
3521                   ;; so they're roots.
3522                   (setq threads (append (cdr (symbol-value refs)) threads))))
3523               gnus-newsgroup-dependencies)))
3524     threads))
3525
3526 ;; Build the thread tree.
3527 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3528   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3529
3530 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3531 if it was already present.
3532
3533 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3534 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3535 Message-IDs will be renamed be renamed to a unique Message-ID before
3536 being entered.
3537
3538 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3539   (let* ((id (mail-header-id header))
3540          (id-dep (and id (intern id dependencies)))
3541          ref ref-dep ref-header)
3542     ;; Enter this `header' in the `dependencies' table.
3543     (cond
3544      ((not id-dep)
3545       (setq header nil))
3546      ;; The first two cases do the normal part: enter a new `header'
3547      ;; in the `dependencies' table.
3548      ((not (boundp id-dep))
3549       (set id-dep (list header)))
3550      ((null (car (symbol-value id-dep)))
3551       (setcar (symbol-value id-dep) header))
3552
3553      ;; From here the `header' was already present in the
3554      ;; `dependencies' table.
3555      (force-new
3556       ;; Overrides an existing entry;
3557       ;; just set the header part of the entry.
3558       (setcar (symbol-value id-dep) header))
3559
3560      ;; Renames the existing `header' to a unique Message-ID.
3561      ((not gnus-summary-ignore-duplicates)
3562       ;; An article with this Message-ID has already been seen.
3563       ;; We rename the Message-ID.
3564       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3565            (list header))
3566       (mail-header-set-id header id))
3567
3568      ;; The last case ignores an existing entry, except it adds any
3569      ;; additional Xrefs (in case the two articles came from different
3570      ;; servers.
3571      ;; Also sets `header' to `nil' meaning that the `dependencies'
3572      ;; table was *not* modified.
3573      (t
3574       (mail-header-set-xref
3575        (car (symbol-value id-dep))
3576        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3577                    "")
3578                (or (mail-header-xref header) "")))
3579       (setq header nil)))
3580
3581     (when header
3582       ;; First check if that we are not creating a References loop.
3583       (setq ref (gnus-parent-id (mail-header-references header)))
3584       (while (and ref
3585                   (setq ref-dep (intern-soft ref dependencies))
3586                   (boundp ref-dep)
3587                   (setq ref-header (car (symbol-value ref-dep))))
3588         (if (string= id ref)
3589             ;; Yuk!  This is a reference loop.  Make the article be a
3590             ;; root article.
3591             (progn
3592               (mail-header-set-references (car (symbol-value id-dep)) "none")
3593               (setq ref nil))
3594           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3595       (setq ref (gnus-parent-id (mail-header-references header)))
3596       (setq ref-dep (intern (or ref "none") dependencies))
3597       (if (boundp ref-dep)
3598           (setcdr (symbol-value ref-dep)
3599                   (nconc (cdr (symbol-value ref-dep))
3600                          (list (symbol-value id-dep))))
3601         (set ref-dep (list nil (symbol-value id-dep)))))
3602     header))
3603
3604 (defun gnus-build-sparse-threads ()
3605   (let ((headers gnus-newsgroup-headers)
3606         (mail-parse-charset gnus-newsgroup-charset)
3607         (gnus-summary-ignore-duplicates t)
3608         header references generation relations
3609         subject child end new-child date)
3610     ;; First we create an alist of generations/relations, where
3611     ;; generations is how much we trust the relation, and the relation
3612     ;; is parent/child.
3613     (gnus-message 7 "Making sparse threads...")
3614     (save-excursion
3615       (nnheader-set-temp-buffer " *gnus sparse threads*")
3616       (while (setq header (pop headers))
3617         (when (and (setq references (mail-header-references header))
3618                    (not (string= references "")))
3619           (insert references)
3620           (setq child (mail-header-id header)
3621                 subject (mail-header-subject header)
3622                 date (mail-header-date header)
3623                 generation 0)
3624           (while (search-backward ">" nil t)
3625             (setq end (1+ (point)))
3626             (when (search-backward "<" nil t)
3627               (setq new-child (buffer-substring (point) end))
3628               (push (list (incf generation)
3629                           child (setq child new-child)
3630                           subject date)
3631                     relations)))
3632           (when child
3633             (push (list (1+ generation) child nil subject) relations))
3634           (erase-buffer)))
3635       (kill-buffer (current-buffer)))
3636     ;; Sort over trustworthiness.
3637     (mapcar
3638      (lambda (relation)
3639        (when (gnus-dependencies-add-header
3640               (make-full-mail-header
3641                gnus-reffed-article-number
3642                (nth 3 relation) "" (or (nth 4 relation) "")
3643                (nth 1 relation)
3644                (or (nth 2 relation) "") 0 0 "")
3645               gnus-newsgroup-dependencies nil)
3646          (push gnus-reffed-article-number gnus-newsgroup-limit)
3647          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3648          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3649                gnus-newsgroup-reads)
3650          (decf gnus-reffed-article-number)))
3651      (sort relations 'car-less-than-car))
3652     (gnus-message 7 "Making sparse threads...done")))
3653
3654 (defun gnus-build-old-threads ()
3655   ;; Look at all the articles that refer back to old articles, and
3656   ;; fetch the headers for the articles that aren't there.  This will
3657   ;; build complete threads - if the roots haven't been expired by the
3658   ;; server, that is.
3659   (let ((mail-parse-charset gnus-newsgroup-charset)
3660         id heads)
3661     (mapatoms
3662      (lambda (refs)
3663        (when (not (car (symbol-value refs)))
3664          (setq heads (cdr (symbol-value refs)))
3665          (while heads
3666            (if (memq (mail-header-number (caar heads))
3667                      gnus-newsgroup-dormant)
3668                (setq heads (cdr heads))
3669              (setq id (symbol-name refs))
3670              (while (and (setq id (gnus-build-get-header id))
3671                          (not (car (gnus-id-to-thread id)))))
3672              (setq heads nil)))))
3673      gnus-newsgroup-dependencies)))
3674
3675 ;; This function has to be called with point after the article number
3676 ;; on the beginning of the line.
3677 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3678   (let ((eol (gnus-point-at-eol))
3679         (buffer (current-buffer))
3680         header)
3681
3682     ;; overview: [num subject from date id refs chars lines misc]
3683     (unwind-protect
3684         (progn
3685           (narrow-to-region (point) eol)
3686           (unless (eobp)
3687             (forward-char))
3688
3689           (setq header
3690                 (make-full-mail-header
3691                  number                 ; number
3692                  (funcall gnus-decode-encoded-word-function
3693                           (nnheader-nov-field)) ; subject
3694                  (funcall gnus-decode-encoded-word-function
3695                           (nnheader-nov-field)) ; from
3696                  (nnheader-nov-field)   ; date
3697                  (nnheader-nov-read-message-id) ; id
3698                  (nnheader-nov-field)   ; refs
3699                  (nnheader-nov-read-integer) ; chars
3700                  (nnheader-nov-read-integer) ; lines
3701                  (unless (eobp)
3702                    (if (looking-at "Xref: ")
3703                        (goto-char (match-end 0)))
3704                    (nnheader-nov-field)) ; Xref
3705                  (nnheader-nov-parse-extra)))) ; extra
3706
3707       (widen))
3708
3709     (when gnus-alter-header-function
3710       (funcall gnus-alter-header-function header))
3711     (gnus-dependencies-add-header header dependencies force-new)))
3712
3713 (defsubst gnus-nov-parse-line-1 (number dependencies &optional force-new)
3714   (let ((eol (gnus-point-at-eol))
3715         (buffer (current-buffer))
3716         header)
3717
3718     ;; overview: [num subject from date id refs chars lines misc]
3719     (unwind-protect
3720         (progn
3721           (narrow-to-region (point) eol)
3722           (unless (eobp)
3723             (forward-char))
3724
3725           (setq header
3726                 (make-full-mail-header
3727                  number                 ; number
3728                  (nnheader-nov-field)   ; subject
3729                  (nnheader-nov-field)   ; from
3730                  (nnheader-nov-field)   ; date
3731                  (nnheader-nov-read-message-id) ; id
3732                  (nnheader-nov-field)   ; refs
3733                  (nnheader-nov-read-integer) ; chars
3734                  (nnheader-nov-read-integer) ; lines
3735                  (unless (eobp)
3736                    (nnheader-nov-field)) ; Xref
3737                  (nnheader-nov-parse-extra)))) ; extra
3738
3739       (widen))
3740     (gnus-dependencies-add-header header dependencies force-new)))
3741
3742 (defun gnus-build-get-header (id)
3743   "Look through the buffer of NOV lines and find the header to ID.
3744 Enter this line into the dependencies hash table, and return
3745 the id of the parent article (if any)."
3746   (let ((deps gnus-newsgroup-dependencies)
3747         found header)
3748     (prog1
3749         (save-excursion
3750           (set-buffer nntp-server-buffer)
3751           (let ((case-fold-search nil))
3752             (goto-char (point-min))
3753             (while (and (not found)
3754                         (search-forward id nil t))
3755               (beginning-of-line)
3756               (setq found (looking-at
3757                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3758                                    (regexp-quote id))))
3759               (or found (beginning-of-line 2)))
3760             (when found
3761               (beginning-of-line)
3762               (and
3763                (setq header (gnus-nov-parse-line
3764                              (read (current-buffer)) deps))
3765                (gnus-parent-id (mail-header-references header))))))
3766       (when header
3767         (let ((number (mail-header-number header)))
3768           (push number gnus-newsgroup-limit)
3769           (push header gnus-newsgroup-headers)
3770           (if (memq number gnus-newsgroup-unselected)
3771               (progn
3772                 (push number gnus-newsgroup-unreads)
3773                 (setq gnus-newsgroup-unselected
3774                       (delq number gnus-newsgroup-unselected)))
3775             (push number gnus-newsgroup-ancient)))))))
3776
3777 (defun gnus-build-all-threads ()
3778   "Read all the headers."
3779   (let ((gnus-summary-ignore-duplicates t)
3780         (mail-parse-charset gnus-newsgroup-charset)
3781         (dependencies gnus-newsgroup-dependencies)
3782         header article)
3783     (save-excursion
3784       (set-buffer nntp-server-buffer)
3785       (let ((case-fold-search nil))
3786         (goto-char (point-min))
3787         (while (not (eobp))
3788           (ignore-errors
3789             (setq article (read (current-buffer))
3790                   header (gnus-nov-parse-line article dependencies)))
3791           (when header
3792             (save-excursion
3793               (set-buffer gnus-summary-buffer)
3794               (push header gnus-newsgroup-headers)
3795               (if (memq (setq article (mail-header-number header))
3796                         gnus-newsgroup-unselected)
3797                   (progn
3798                     (push article gnus-newsgroup-unreads)
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                 (push number gnus-newsgroup-expirable)
4429               (push (cons number gnus-low-score-mark)
4430                     gnus-newsgroup-reads))))
4431
4432           (when gnus-tmp-header
4433             ;; We may have an old dummy line to output before this
4434             ;; article.
4435             (when (and gnus-tmp-dummy-line
4436                        (gnus-subject-equal
4437                         gnus-tmp-dummy-line
4438                         (mail-header-subject gnus-tmp-header)))
4439               (gnus-summary-insert-dummy-line
4440                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4441               (setq gnus-tmp-dummy-line nil))
4442
4443             ;; Compute the mark.
4444             (setq gnus-tmp-unread (gnus-article-mark number))
4445
4446             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4447                                   gnus-tmp-header gnus-tmp-level)
4448                   gnus-newsgroup-data)
4449
4450             ;; Actually insert the line.
4451             (setq
4452              gnus-tmp-subject-or-nil
4453              (cond
4454               ((and gnus-thread-ignore-subject
4455                     gnus-tmp-prev-subject
4456                     (not (inline (gnus-subject-equal
4457                                   gnus-tmp-prev-subject subject))))
4458                subject)
4459               ((zerop gnus-tmp-level)
4460                (if (and (eq gnus-summary-make-false-root 'empty)
4461                         (memq number gnus-tmp-gathered)
4462                         gnus-tmp-prev-subject
4463                         (inline (gnus-subject-equal
4464                                  gnus-tmp-prev-subject subject)))
4465                    gnus-summary-same-subject
4466                  subject))
4467               (t gnus-summary-same-subject)))
4468             (if (and (eq gnus-summary-make-false-root 'adopt)
4469                      (= gnus-tmp-level 1)
4470                      (memq number gnus-tmp-gathered))
4471                 (setq gnus-tmp-opening-bracket ?\<
4472                       gnus-tmp-closing-bracket ?\>)
4473               (setq gnus-tmp-opening-bracket ?\[
4474                     gnus-tmp-closing-bracket ?\]))
4475             (setq
4476              gnus-tmp-indentation
4477              (aref gnus-thread-indent-array gnus-tmp-level)
4478              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4479              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4480                                 gnus-summary-default-score 0)
4481              gnus-tmp-score-char
4482              (if (or (null gnus-summary-default-score)
4483                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4484                          gnus-summary-zcore-fuzz))
4485                  ?                      ;Whitespace
4486                (if (< gnus-tmp-score gnus-summary-default-score)
4487                    gnus-score-below-mark gnus-score-over-mark))
4488              gnus-tmp-replied
4489              (cond ((memq number gnus-newsgroup-processable)
4490                     gnus-process-mark)
4491                    ((memq number gnus-newsgroup-cached)
4492                     gnus-cached-mark)
4493                    ((memq number gnus-newsgroup-replied)
4494                     gnus-replied-mark)
4495                    ((memq number gnus-newsgroup-forwarded)
4496                     gnus-forwarded-mark)
4497                    ((memq number gnus-newsgroup-saved)
4498                     gnus-saved-mark)
4499                    ((memq number gnus-newsgroup-recent)
4500                     gnus-recent-mark)
4501                    ((memq number gnus-newsgroup-unseen)
4502                     gnus-unseen-mark)
4503                    (t gnus-no-mark))
4504              gnus-tmp-from (mail-header-from gnus-tmp-header)
4505              gnus-tmp-name
4506              (cond
4507               ((string-match "<[^>]+> *$" gnus-tmp-from)
4508                (setq beg-match (match-beginning 0))
4509                (or (and (string-match "^\".+\"" gnus-tmp-from)
4510                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4511                    (substring gnus-tmp-from 0 beg-match)))
4512               ((string-match "(.+)" gnus-tmp-from)
4513                (substring gnus-tmp-from
4514                           (1+ (match-beginning 0)) (1- (match-end 0))))
4515               (t gnus-tmp-from))
4516              gnus-tmp-thread-tree-header-string
4517              (cond
4518               ((not gnus-show-threads) "")
4519               ((zerop gnus-tmp-level)
4520                (if (cdar thread)
4521                    (or gnus-sum-thread-tree-root subject)
4522                  (or gnus-sum-thread-tree-single-indent subject)))
4523               (t
4524                (concat (apply 'concat
4525                               (mapcar (lambda (item)
4526                                         (if (= item 1)
4527                                             gnus-sum-thread-tree-vertical
4528                                           gnus-sum-thread-tree-indent))
4529                                       (cdr (reverse tree-stack))))
4530                        (if (nth 1 thread)
4531                            gnus-sum-thread-tree-leaf-with-other
4532                          gnus-sum-thread-tree-single-leaf)))))
4533             (when (string= gnus-tmp-name "")
4534               (setq gnus-tmp-name gnus-tmp-from))
4535             (unless (numberp gnus-tmp-lines)
4536               (setq gnus-tmp-lines -1))
4537             (if (= gnus-tmp-lines -1)
4538                 (setq gnus-tmp-lines "?")
4539               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4540             (gnus-put-text-property
4541              (point)
4542              (progn (eval gnus-summary-line-format-spec) (point))
4543              'gnus-number number)
4544             (when gnus-visual-p
4545               (forward-line -1)
4546               (gnus-run-hooks 'gnus-summary-update-hook)
4547               (forward-line 1))
4548
4549             (setq gnus-tmp-prev-subject subject)))
4550
4551         (when (nth 1 thread)
4552           (push (list (max 0 gnus-tmp-level)
4553                       (copy-list tree-stack)
4554                       (nthcdr 1 thread))
4555                 stack))
4556         (push (if (nth 1 thread) 1 0) tree-stack)
4557         (incf gnus-tmp-level)
4558         (setq threads (if thread-end nil (cdar thread)))
4559         (unless threads
4560           (setq gnus-tmp-level 0)))))
4561   (gnus-message 7 "Generating summary...done"))
4562
4563 (defun gnus-summary-prepare-unthreaded (headers)
4564   "Generate an unthreaded summary buffer based on HEADERS."
4565   (let (header number mark)
4566
4567     (beginning-of-line)
4568
4569     (while headers
4570       ;; We may have to root out some bad articles...
4571       (when (memq (setq number (mail-header-number
4572                                 (setq header (pop headers))))
4573                   gnus-newsgroup-limit)
4574         ;; Mark article as read when it has a low score.
4575         (when (and gnus-summary-mark-below
4576                    (< (or (cdr (assq number gnus-newsgroup-scored))
4577                           gnus-summary-default-score 0)
4578                       gnus-summary-mark-below)
4579                    (not (gnus-summary-article-ancient-p number)))
4580           (setq gnus-newsgroup-unreads
4581                 (delq number gnus-newsgroup-unreads))
4582           (if gnus-newsgroup-auto-expire
4583               (push number gnus-newsgroup-expirable)
4584             (push (cons number gnus-low-score-mark)
4585                   gnus-newsgroup-reads)))
4586
4587         (setq mark (gnus-article-mark number))
4588         (push (gnus-data-make number mark (1+ (point)) header 0)
4589               gnus-newsgroup-data)
4590         (gnus-summary-insert-line
4591          header 0 number
4592          mark (memq number gnus-newsgroup-replied)
4593          (memq number gnus-newsgroup-expirable)
4594          (mail-header-subject header) nil
4595          (cdr (assq number gnus-newsgroup-scored))
4596          (memq number gnus-newsgroup-processable))))))
4597
4598 (defun gnus-summary-remove-list-identifiers ()
4599   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4600   (let ((regexp (if (consp gnus-list-identifiers)
4601                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4602                   gnus-list-identifiers))
4603         changed subject)
4604     (when regexp
4605       (dolist (header gnus-newsgroup-headers)
4606         (setq subject (mail-header-subject header)
4607               changed nil)
4608         (while (string-match
4609                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4610                 subject)
4611           (setq subject
4612                 (concat (substring subject 0 (match-beginning 2))
4613                         (substring subject (match-end 0)))
4614                 changed t))
4615         (when (and changed
4616                    (string-match
4617                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4618           (setq subject
4619                 (concat (substring subject 0 (match-beginning 1))
4620                         (substring subject (match-end 1)))))
4621         (when changed
4622           (mail-header-set-subject header subject))))))
4623
4624 (defun gnus-fetch-headers (articles)
4625   "Fetch headers of ARTICLES."
4626   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4627     (gnus-message 5 "Fetching headers for %s..." name)
4628     (prog1
4629         (if (eq 'nov
4630                 (setq gnus-headers-retrieved-by
4631                       (gnus-retrieve-headers
4632                        articles gnus-newsgroup-name
4633                        ;; We might want to fetch old headers, but
4634                        ;; not if there is only 1 article.
4635                        (and (or (and
4636                                  (not (eq gnus-fetch-old-headers 'some))
4637                                  (not (numberp gnus-fetch-old-headers)))
4638                                 (> (length articles) 1))
4639                             gnus-fetch-old-headers))))
4640             (gnus-get-newsgroup-headers-xover
4641              articles nil nil gnus-newsgroup-name t)
4642           (gnus-get-newsgroup-headers))
4643       (gnus-message 5 "Fetching headers for %s...done" name))))
4644
4645 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4646   "Select newsgroup GROUP.
4647 If READ-ALL is non-nil, all articles in the group are selected.
4648 If SELECT-ARTICLES, only select those articles from GROUP."
4649   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4650          ;;!!! Dirty hack; should be removed.
4651          (gnus-summary-ignore-duplicates
4652           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4653               t
4654             gnus-summary-ignore-duplicates))
4655          (info (nth 2 entry))
4656          articles fetched-articles cached)
4657
4658     (unless (gnus-check-server
4659              (set (make-local-variable 'gnus-current-select-method)
4660                   (gnus-find-method-for-group group)))
4661       (error "Couldn't open server"))
4662
4663     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4664         (gnus-activate-group group)     ; Or we can activate it...
4665         (progn                          ; Or we bug out.
4666           (when (equal major-mode 'gnus-summary-mode)
4667             (kill-buffer (current-buffer)))
4668           (error "Couldn't activate group %s: %s"
4669                  group (gnus-status-message group))))
4670
4671     (unless (gnus-request-group group t)
4672       (when (equal major-mode 'gnus-summary-mode)
4673         (kill-buffer (current-buffer)))
4674       (error "Couldn't request group %s: %s"
4675              group (gnus-status-message group)))
4676
4677     (setq gnus-newsgroup-name group
4678           gnus-newsgroup-unselected nil
4679           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4680
4681     (let ((display (gnus-group-find-parameter group 'display)))
4682       (setq gnus-newsgroup-display
4683             (cond
4684              ((not (zerop (or (car-safe read-all) 0)))
4685               ;; The user entered the group with C-u SPC/RET, let's show
4686               ;; all articles.
4687               'gnus-not-ignore)
4688              ((eq display 'all)
4689               'gnus-not-ignore)
4690              ((arrayp display)
4691               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4692              ((numberp display)
4693               ;; The following is probably the "correct" solution, but
4694               ;; it makes Gnus fetch all headers and then limit the
4695               ;; articles (which is slow), so instead we hack the
4696               ;; select-articles parameter instead. -- Simon Josefsson
4697               ;; <jas@kth.se>
4698               ;;
4699               ;; (gnus-byte-compile
4700               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4701               ;;                         display)))))
4702               (setq select-articles
4703                     (gnus-uncompress-range
4704                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4705                              (if (> tmp 0)
4706                                  tmp
4707                                1))
4708                            (cdr (gnus-active group)))))
4709               nil)
4710              (t
4711               nil))))
4712
4713     (gnus-summary-setup-default-charset)
4714
4715     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4716     (when (gnus-virtual-group-p group)
4717       (setq cached gnus-newsgroup-cached))
4718
4719     (setq gnus-newsgroup-unreads
4720           (gnus-set-difference
4721            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4722            gnus-newsgroup-dormant))
4723
4724     (setq gnus-newsgroup-processable nil)
4725
4726     (gnus-update-read-articles group gnus-newsgroup-unreads)
4727
4728     ;; Adjust and set lists of article marks.
4729     (when info
4730       (gnus-adjust-marked-articles info))
4731
4732     (if (setq articles select-articles)
4733         (setq gnus-newsgroup-unselected
4734               (gnus-sorted-intersection
4735                gnus-newsgroup-unreads
4736                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4737       (setq articles (gnus-articles-to-read group read-all)))
4738
4739     (cond
4740      ((null articles)
4741       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4742       'quit)
4743      ((eq articles 0) nil)
4744      (t
4745       ;; Init the dependencies hash table.
4746       (setq gnus-newsgroup-dependencies
4747             (gnus-make-hashtable (length articles)))
4748       (gnus-set-global-variables)
4749       ;; Retrieve the headers and read them in.
4750       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4751
4752       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4753       (when cached
4754         (setq gnus-newsgroup-cached cached))
4755
4756       ;; Suppress duplicates?
4757       (when gnus-suppress-duplicates
4758         (gnus-dup-suppress-articles))
4759
4760       ;; Set the initial limit.
4761       (setq gnus-newsgroup-limit (copy-sequence articles))
4762       ;; Remove canceled articles from the list of unread articles.
4763       (setq fetched-articles
4764             (mapcar (lambda (headers) (mail-header-number headers))
4765                     gnus-newsgroup-headers))
4766       (setq gnus-newsgroup-articles fetched-articles)
4767       (setq gnus-newsgroup-unreads
4768             (gnus-set-sorted-intersection
4769              gnus-newsgroup-unreads fetched-articles))
4770       (gnus-compute-unseen-list)
4771
4772       ;; Removed marked articles that do not exist.
4773       (gnus-update-missing-marks
4774        (gnus-sorted-complement fetched-articles articles))
4775       ;; We might want to build some more threads first.
4776       (when (and gnus-fetch-old-headers
4777                  (eq gnus-headers-retrieved-by 'nov))
4778         (if (eq gnus-fetch-old-headers 'invisible)
4779             (gnus-build-all-threads)
4780           (gnus-build-old-threads)))
4781       ;; Let the Gnus agent mark articles as read.
4782       (when gnus-agent
4783         (gnus-agent-get-undownloaded-list))
4784       ;; Remove list identifiers from subject
4785       (when gnus-list-identifiers
4786         (gnus-summary-remove-list-identifiers))
4787       ;; Check whether auto-expire is to be done in this group.
4788       (setq gnus-newsgroup-auto-expire
4789             (gnus-group-auto-expirable-p group))
4790       ;; Set up the article buffer now, if necessary.
4791       (unless gnus-single-article-buffer
4792         (gnus-article-setup-buffer))
4793       ;; First and last article in this newsgroup.
4794       (when gnus-newsgroup-headers
4795         (setq gnus-newsgroup-begin
4796               (mail-header-number (car gnus-newsgroup-headers))
4797               gnus-newsgroup-end
4798               (mail-header-number
4799                (gnus-last-element gnus-newsgroup-headers))))
4800       ;; GROUP is successfully selected.
4801       (or gnus-newsgroup-headers t)))))
4802
4803 (defun gnus-compute-unseen-list ()
4804   ;; The `seen' marks are treated specially.
4805   (if (not gnus-newsgroup-seen)
4806       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
4807     (setq gnus-newsgroup-unseen
4808           (gnus-inverse-list-range-intersection
4809            gnus-newsgroup-articles gnus-newsgroup-seen))))
4810
4811 (defun gnus-summary-display-make-predicate (display)
4812   (require 'gnus-agent)
4813   (when (= (length display) 1)
4814     (setq display (car display)))
4815   (unless gnus-summary-display-cache
4816     (dolist (elem (append (list (cons 'read 'read)
4817                                 (cons 'unseen 'unseen))
4818                           gnus-article-mark-lists))
4819       (push (cons (cdr elem)
4820                   (gnus-byte-compile
4821                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
4822             gnus-summary-display-cache)))
4823   (let ((gnus-category-predicate-alist gnus-summary-display-cache))
4824     (gnus-get-predicate display)))
4825
4826 ;; Uses the dynamically bound `number' variable.
4827 (defvar number)
4828 (defun gnus-article-marked-p (type &optional article)
4829   (let ((article (or article number)))
4830     (cond
4831      ((eq type 'tick)
4832       (memq article gnus-newsgroup-marked))
4833      ((eq type 'unsend)
4834       (memq article gnus-newsgroup-unsendable))
4835      ((eq type 'undownload)
4836       (memq article gnus-newsgroup-undownloaded))
4837      ((eq type 'download)
4838       (memq article gnus-newsgroup-downloadable))
4839      ((eq type 'unread)
4840       (memq article gnus-newsgroup-unreads))
4841      ((eq type 'read)
4842       (memq article gnus-newsgroup-reads))
4843      ((eq type 'dormant)
4844       (memq article gnus-newsgroup-dormant) )
4845      ((eq type 'expire)
4846       (memq article gnus-newsgroup-expirable))
4847      ((eq type 'reply)
4848       (memq article gnus-newsgroup-replied))
4849      ((eq type 'killed)
4850       (memq article gnus-newsgroup-killed))
4851      ((eq type 'bookmark)
4852       (assq article gnus-newsgroup-bookmarks))
4853      ((eq type 'score)
4854       (assq article gnus-newsgroup-scored))
4855      ((eq type 'save)
4856       (memq article gnus-newsgroup-saved))
4857      ((eq type 'cache)
4858       (memq article gnus-newsgroup-cached))
4859      ((eq type 'forward)
4860       (memq article gnus-newsgroup-forwarded))
4861      ((eq type 'seen)
4862       (not (memq article gnus-newsgroup-unseen)))
4863      ((eq type 'recent)
4864       (memq article gnus-newsgroup-recent))
4865      (t t))))
4866
4867 (defun gnus-articles-to-read (group &optional read-all)
4868   "Find out what articles the user wants to read."
4869   (let* ((articles
4870           ;; Select all articles if `read-all' is non-nil, or if there
4871           ;; are no unread articles.
4872           (if (or read-all
4873                   (and (zerop (length gnus-newsgroup-marked))
4874                        (zerop (length gnus-newsgroup-unreads)))
4875                   (eq gnus-newsgroup-display 'gnus-not-ignore))
4876               ;; We want to select the headers for all the articles in
4877               ;; the group, so we select either all the active
4878               ;; articles in the group, or (if that's nil), the
4879               ;; articles in the cache.
4880               (or
4881                (gnus-uncompress-range (gnus-active group))
4882                (gnus-cache-articles-in-group group))
4883             ;; Select only the "normal" subset of articles.
4884             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4885                           (copy-sequence gnus-newsgroup-unreads))
4886                   '<)))
4887          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4888          (scored (length scored-list))
4889          (number (length articles))
4890          (marked (+ (length gnus-newsgroup-marked)
4891                     (length gnus-newsgroup-dormant)))
4892          (select
4893           (cond
4894            ((numberp read-all)
4895             read-all)
4896            (t
4897             (condition-case ()
4898                 (cond
4899                  ((and (or (<= scored marked) (= scored number))
4900                        (numberp gnus-large-newsgroup)
4901                        (> number gnus-large-newsgroup))
4902                   (let ((input
4903                          (read-string
4904                           (format
4905                            "How many articles from %s (default %d): "
4906                            (gnus-limit-string
4907                             (gnus-group-decoded-name gnus-newsgroup-name)
4908                             35)
4909                            number))))
4910                     (if (string-match "^[ \t]*$" input) number input)))
4911                  ((and (> scored marked) (< scored number)
4912                        (> (- scored number) 20))
4913                   (let ((input
4914                          (read-string
4915                           (format "%s %s (%d scored, %d total): "
4916                                   "How many articles from"
4917                                   (gnus-group-decoded-name group)
4918                                   scored number))))
4919                     (if (string-match "^[ \t]*$" input)
4920                         number input)))
4921                  (t number))
4922               (quit
4923                (message "Quit getting the articles to read")
4924                nil))))))
4925     (setq select (if (stringp select) (string-to-number select) select))
4926     (if (or (null select) (zerop select))
4927         select
4928       (if (and (not (zerop scored)) (<= (abs select) scored))
4929           (progn
4930             (setq articles (sort scored-list '<))
4931             (setq number (length articles)))
4932         (setq articles (copy-sequence articles)))
4933
4934       (when (< (abs select) number)
4935         (if (< select 0)
4936             ;; Select the N oldest articles.
4937             (setcdr (nthcdr (1- (abs select)) articles) nil)
4938           ;; Select the N most recent articles.
4939           (setq articles (nthcdr (- number select) articles))))
4940       (setq gnus-newsgroup-unselected
4941             (gnus-sorted-intersection
4942              gnus-newsgroup-unreads
4943              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4944       (when gnus-alter-articles-to-read-function
4945         (setq gnus-newsgroup-unreads
4946               (sort
4947                (funcall gnus-alter-articles-to-read-function
4948                         gnus-newsgroup-name gnus-newsgroup-unreads)
4949                '<)))
4950       articles)))
4951
4952 (defun gnus-killed-articles (killed articles)
4953   (let (out)
4954     (while articles
4955       (when (inline (gnus-member-of-range (car articles) killed))
4956         (push (car articles) out))
4957       (setq articles (cdr articles)))
4958     out))
4959
4960 (defun gnus-uncompress-marks (marks)
4961   "Uncompress the mark ranges in MARKS."
4962   (let ((uncompressed '(score bookmark))
4963         out)
4964     (while marks
4965       (if (memq (caar marks) uncompressed)
4966           (push (car marks) out)
4967         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4968       (setq marks (cdr marks)))
4969     out))
4970
4971 (defun gnus-article-mark-to-type (mark)
4972   "Return the type of MARK."
4973   (or (cadr (assq mark gnus-article-special-mark-lists))
4974       'list))
4975
4976 (defun gnus-article-unpropagatable-p (mark)
4977   "Return whether MARK should be propagated to backend."
4978   (memq mark gnus-article-unpropagated-mark-lists))
4979
4980 (defun gnus-adjust-marked-articles (info)
4981   "Set all article lists and remove all marks that are no longer valid."
4982   (let* ((marked-lists (gnus-info-marks info))
4983          (active (gnus-active (gnus-info-group info)))
4984          (min (car active))
4985          (max (cdr active))
4986          (types gnus-article-mark-lists)
4987          marks var articles article mark mark-type)
4988
4989     (dolist (marks marked-lists)
4990       (setq mark (car marks)
4991             mark-type (gnus-article-mark-to-type mark)
4992             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
4993
4994       ;; We set the variable according to the type of the marks list,
4995       ;; and then adjust the marks to a subset of the active articles.
4996       (cond
4997        ;; Adjust "simple" lists.
4998        ((eq mark-type 'list)
4999         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5000         (when (memq mark '(tick dormant expire reply save))
5001           (while articles
5002             (when (or (< (setq article (pop articles)) min) (> article max))
5003               (set var (delq article (symbol-value var)))))))
5004        ;; Adjust assocs.
5005        ((eq mark-type 'tuple)
5006         (set var (setq articles (cdr marks)))
5007         (when (not (listp (cdr (symbol-value var))))
5008           (set var (list (symbol-value var))))
5009         (when (not (listp (cdr articles)))
5010           (setq articles (list articles)))
5011         (while articles
5012           (when (or (not (consp (setq article (pop articles))))
5013                     (< (car article) min)
5014                     (> (car article) max))
5015             (set var (delq article (symbol-value var))))))
5016        ;; Adjust ranges (sloppily).
5017        ((eq mark-type 'range)
5018         (cond
5019          ((eq mark 'seen)
5020           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5021           ;; It should be (seen (NUM1 . NUM2)).
5022           (when (numberp (cddr marks))
5023             (setcdr marks (list (cdr marks))))
5024           (setq articles (cdr marks))
5025           (while (and articles
5026                       (or (and (consp (car articles))
5027                                (> min (cdar articles)))
5028                           (and (numberp (car articles))
5029                                (> min (car articles)))))
5030             (pop articles))
5031           (set var articles))))))))
5032
5033 (defun gnus-update-missing-marks (missing)
5034   "Go through the list of MISSING articles and remove them from the mark lists."
5035   (when missing
5036     (let (var m)
5037       ;; Go through all types.
5038       (dolist (elem gnus-article-mark-lists)
5039         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5040           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5041           (when (symbol-value var)
5042             ;; This list has articles.  So we delete all missing
5043             ;; articles from it.
5044             (setq m missing)
5045             (while m
5046               (set var (delq (pop m) (symbol-value var))))))))))
5047
5048 (defun gnus-update-marks ()
5049   "Enter the various lists of marked articles into the newsgroup info list."
5050   (let ((types gnus-article-mark-lists)
5051         (info (gnus-get-info gnus-newsgroup-name))
5052         type list newmarked symbol delta-marks)
5053     (when info
5054       ;; Add all marks lists to the list of marks lists.
5055       (while (setq type (pop types))
5056         (setq list (symbol-value
5057                     (setq symbol
5058                           (intern (format "gnus-newsgroup-%s" (car type))))))
5059
5060         (when list
5061           ;; Get rid of the entries of the articles that have the
5062           ;; default score.
5063           (when (and (eq (cdr type) 'score)
5064                      gnus-save-score
5065                      list)
5066             (let* ((arts list)
5067                    (prev (cons nil list))
5068                    (all prev))
5069               (while arts
5070                 (if (or (not (consp (car arts)))
5071                         (= (cdar arts) gnus-summary-default-score))
5072                     (setcdr prev (cdr arts))
5073                   (setq prev arts))
5074                 (setq arts (cdr arts)))
5075               (setq list (cdr all)))))
5076
5077         (when (eq (cdr type) 'seen)
5078           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5079
5080         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5081           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5082
5083         (when (and (gnus-check-backend-function
5084                     'request-set-mark gnus-newsgroup-name)
5085                    (not (gnus-article-unpropagatable-p (cdr type))))
5086           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5087                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5088                  (add (gnus-remove-from-range
5089                        (gnus-copy-sequence list) old)))
5090             (when add
5091               (push (list add 'add (list (cdr type))) delta-marks))
5092             (when del
5093               (push (list del 'del (list (cdr type))) delta-marks))))
5094
5095         (when list
5096           (push (cons (cdr type) list) newmarked)))
5097
5098       (when delta-marks
5099         (unless (gnus-check-group gnus-newsgroup-name)
5100           (error "Can't open server for %s" gnus-newsgroup-name))
5101         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5102
5103       ;; Enter these new marks into the info of the group.
5104       (if (nthcdr 3 info)
5105           (setcar (nthcdr 3 info) newmarked)
5106         ;; Add the marks lists to the end of the info.
5107         (when newmarked
5108           (setcdr (nthcdr 2 info) (list newmarked))))
5109
5110       ;; Cut off the end of the info if there's nothing else there.
5111       (let ((i 5))
5112         (while (and (> i 2)
5113                     (not (nth i info)))
5114           (when (nthcdr (decf i) info)
5115             (setcdr (nthcdr i info) nil)))))))
5116
5117 (defun gnus-set-mode-line (where)
5118   "Set the mode line of the article or summary buffers.
5119 If WHERE is `summary', the summary mode line format will be used."
5120   ;; Is this mode line one we keep updated?
5121   (when (and (memq where gnus-updated-mode-lines)
5122              (symbol-value
5123               (intern (format "gnus-%s-mode-line-format-spec" where))))
5124     (let (mode-string)
5125       (save-excursion
5126         ;; We evaluate this in the summary buffer since these
5127         ;; variables are buffer-local to that buffer.
5128         (set-buffer gnus-summary-buffer)
5129        ;; We bind all these variables that are used in the `eval' form
5130         ;; below.
5131         (let* ((mformat (symbol-value
5132                          (intern
5133                           (format "gnus-%s-mode-line-format-spec" where))))
5134                (gnus-tmp-group-name (gnus-group-decoded-name
5135                                      gnus-newsgroup-name))
5136                (gnus-tmp-article-number (or gnus-current-article 0))
5137                (gnus-tmp-unread gnus-newsgroup-unreads)
5138                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5139                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5140                (gnus-tmp-unread-and-unselected
5141                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5142                             (zerop gnus-tmp-unselected))
5143                        "")
5144                       ((zerop gnus-tmp-unselected)
5145                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5146                       (t (format "{%d(+%d) more}"
5147                                  gnus-tmp-unread-and-unticked
5148                                  gnus-tmp-unselected))))
5149                (gnus-tmp-subject
5150                 (if (and gnus-current-headers
5151                          (vectorp gnus-current-headers))
5152                     (gnus-mode-string-quote
5153                      (mail-header-subject gnus-current-headers))
5154                   ""))
5155                bufname-length max-len
5156                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5157           (setq mode-string (eval mformat))
5158           (setq bufname-length (if (string-match "%b" mode-string)
5159                                    (- (length
5160                                        (buffer-name
5161                                         (if (eq where 'summary)
5162                                             nil
5163                                           (get-buffer gnus-article-buffer))))
5164                                       2)
5165                                  0))
5166           (setq max-len (max 4 (if gnus-mode-non-string-length
5167                                    (- (window-width)
5168                                       gnus-mode-non-string-length
5169                                       bufname-length)
5170                                  (length mode-string))))
5171           ;; We might have to chop a bit of the string off...
5172           (when (> (length mode-string) max-len)
5173             (setq mode-string
5174                   (concat (truncate-string-to-width mode-string (- max-len 3))
5175                           "...")))
5176           ;; Pad the mode string a bit.
5177           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5178       ;; Update the mode line.
5179       (setq mode-line-buffer-identification
5180             (gnus-mode-line-buffer-identification (list mode-string)))
5181       (set-buffer-modified-p t))))
5182
5183 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5184   "Go through the HEADERS list and add all Xrefs to a hash table.
5185 The resulting hash table is returned, or nil if no Xrefs were found."
5186   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5187          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5188          (xref-hashtb (gnus-make-hashtable))
5189          start group entry number xrefs header)
5190     (while headers
5191       (setq header (pop headers))
5192       (when (and (setq xrefs (mail-header-xref header))
5193                  (not (memq (setq number (mail-header-number header))
5194                             unreads)))
5195         (setq start 0)
5196         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5197           (setq start (match-end 0))
5198           (setq group (if prefix
5199                           (concat prefix (substring xrefs (match-beginning 1)
5200                                                     (match-end 1)))
5201                         (substring xrefs (match-beginning 1) (match-end 1))))
5202           (setq number
5203                 (string-to-int (substring xrefs (match-beginning 2)
5204                                           (match-end 2))))
5205           (if (setq entry (gnus-gethash group xref-hashtb))
5206               (setcdr entry (cons number (cdr entry)))
5207             (gnus-sethash group (cons number nil) xref-hashtb)))))
5208     (and start xref-hashtb)))
5209
5210 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5211   "Look through all the headers and mark the Xrefs as read."
5212   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5213         name entry info xref-hashtb idlist method nth4)
5214     (save-excursion
5215       (set-buffer gnus-group-buffer)
5216       (when (setq xref-hashtb
5217                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5218         (mapatoms
5219          (lambda (group)
5220            (unless (string= from-newsgroup (setq name (symbol-name group)))
5221              (setq idlist (symbol-value group))
5222              ;; Dead groups are not updated.
5223              (and (prog1
5224                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5225                             info (nth 2 entry))
5226                     (when (stringp (setq nth4 (gnus-info-method info)))
5227                       (setq nth4 (gnus-server-to-method nth4))))
5228                   ;; Only do the xrefs if the group has the same
5229                   ;; select method as the group we have just read.
5230                   (or (gnus-methods-equal-p
5231                        nth4 (gnus-find-method-for-group from-newsgroup))
5232                       virtual
5233                       (equal nth4 (setq method (gnus-find-method-for-group
5234                                                 from-newsgroup)))
5235                       (and (equal (car nth4) (car method))
5236                            (equal (nth 1 nth4) (nth 1 method))))
5237                   gnus-use-cross-reference
5238                   (or (not (eq gnus-use-cross-reference t))
5239                       virtual
5240                       ;; Only do cross-references on subscribed
5241                       ;; groups, if that is what is wanted.
5242                       (<= (gnus-info-level info) gnus-level-subscribed))
5243                   (gnus-group-make-articles-read name idlist))))
5244          xref-hashtb)))))
5245
5246 (defun gnus-compute-read-articles (group articles)
5247   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5248          (info (nth 2 entry))
5249          (active (gnus-active group))
5250          ninfo)
5251     (when entry
5252       ;; First peel off all invalid article numbers.
5253       (when active
5254         (let ((ids articles)
5255               id first)
5256           (while (setq id (pop ids))
5257             (when (and first (> id (cdr active)))
5258               ;; We'll end up in this situation in one particular
5259               ;; obscure situation.  If you re-scan a group and get
5260               ;; a new article that is cross-posted to a different
5261               ;; group that has not been re-scanned, you might get
5262               ;; crossposted article that has a higher number than
5263               ;; Gnus believes possible.  So we re-activate this
5264               ;; group as well.  This might mean doing the
5265               ;; crossposting thingy will *increase* the number
5266               ;; of articles in some groups.  Tsk, tsk.
5267               (setq active (or (gnus-activate-group group) active)))
5268             (when (or (> id (cdr active))
5269                       (< id (car active)))
5270               (setq articles (delq id articles))))))
5271       ;; If the read list is nil, we init it.
5272       (if (and active
5273                (null (gnus-info-read info))
5274                (> (car active) 1))
5275           (setq ninfo (cons 1 (1- (car active))))
5276         (setq ninfo (gnus-info-read info)))
5277       ;; Then we add the read articles to the range.
5278       (gnus-add-to-range
5279        ninfo (setq articles (sort articles '<))))))
5280
5281 (defun gnus-group-make-articles-read (group articles)
5282   "Update the info of GROUP to say that ARTICLES are read."
5283   (let* ((num 0)
5284          (entry (gnus-gethash group gnus-newsrc-hashtb))
5285          (info (nth 2 entry))
5286          (active (gnus-active group))
5287          range)
5288     (when entry
5289       (setq range (gnus-compute-read-articles group articles))
5290       (save-excursion
5291         (set-buffer gnus-group-buffer)
5292         (gnus-undo-register
5293           `(progn
5294              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5295              (gnus-info-set-read ',info ',(gnus-info-read info))
5296              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5297              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5298              (gnus-group-update-group ,group t))))
5299       ;; Add the read articles to the range.
5300       (gnus-info-set-read info range)
5301       (gnus-request-set-mark group (list (list range 'add '(read))))
5302       ;; Then we have to re-compute how many unread
5303       ;; articles there are in this group.
5304       (when active
5305         (cond
5306          ((not range)
5307           (setq num (- (1+ (cdr active)) (car active))))
5308          ((not (listp (cdr range)))
5309           (setq num (- (cdr active) (- (1+ (cdr range))
5310                                        (car range)))))
5311          (t
5312           (while range
5313             (if (numberp (car range))
5314                 (setq num (1+ num))
5315               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5316             (setq range (cdr range)))
5317           (setq num (- (cdr active) num))))
5318         ;; Update the number of unread articles.
5319         (setcar entry num)
5320         ;; Update the group buffer.
5321         (gnus-group-update-group group t)))))
5322
5323 (defvar gnus-newsgroup-none-id 0)
5324
5325 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5326   (let ((cur nntp-server-buffer)
5327         (dependencies
5328          (or dependencies
5329              (save-excursion (set-buffer gnus-summary-buffer)
5330                              gnus-newsgroup-dependencies)))
5331         headers id end ref
5332         (mail-parse-charset gnus-newsgroup-charset)
5333         (mail-parse-ignored-charsets
5334          (save-excursion (condition-case nil
5335                              (set-buffer gnus-summary-buffer)
5336                            (error))
5337                          gnus-newsgroup-ignored-charsets)))
5338     (save-excursion
5339       (set-buffer nntp-server-buffer)
5340       ;; Translate all TAB characters into SPACE characters.
5341       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5342       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5343       (gnus-run-hooks 'gnus-parse-headers-hook)
5344       (let ((case-fold-search t)
5345             in-reply-to header p lines chars)
5346         (goto-char (point-min))
5347         ;; Search to the beginning of the next header.  Error messages
5348         ;; do not begin with 2 or 3.
5349         (while (re-search-forward "^[23][0-9]+ " nil t)
5350           (setq id nil
5351                 ref nil)
5352           ;; This implementation of this function, with nine
5353           ;; search-forwards instead of the one re-search-forward and
5354           ;; a case (which basically was the old function) is actually
5355           ;; about twice as fast, even though it looks messier.  You
5356           ;; can't have everything, I guess.  Speed and elegance
5357           ;; doesn't always go hand in hand.
5358           (setq
5359            header
5360            (vector
5361             ;; Number.
5362             (prog1
5363                 (read cur)
5364               (end-of-line)
5365               (setq p (point))
5366               (narrow-to-region (point)
5367                                 (or (and (search-forward "\n.\n" nil t)
5368                                          (- (point) 2))
5369                                     (point))))
5370             ;; Subject.
5371             (progn
5372               (goto-char p)
5373               (if (search-forward "\nsubject:" nil t)
5374                   (funcall gnus-decode-encoded-word-function
5375                            (nnheader-header-value))
5376                 "(none)"))
5377             ;; From.
5378             (progn
5379               (goto-char p)
5380               (if (search-forward "\nfrom:" nil t)
5381                   (funcall gnus-decode-encoded-word-function
5382                            (nnheader-header-value))
5383                 "(nobody)"))
5384             ;; Date.
5385             (progn
5386               (goto-char p)
5387               (if (search-forward "\ndate:" nil t)
5388                   (nnheader-header-value) ""))
5389             ;; Message-ID.
5390             (progn
5391               (goto-char p)
5392               (setq id (if (re-search-forward
5393                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5394                            ;; We do it this way to make sure the Message-ID
5395                            ;; is (somewhat) syntactically valid.
5396                            (buffer-substring (match-beginning 1)
5397                                              (match-end 1))
5398                          ;; If there was no message-id, we just fake one
5399                          ;; to make subsequent routines simpler.
5400                          (nnheader-generate-fake-message-id))))
5401             ;; References.
5402             (progn
5403               (goto-char p)
5404               (if (search-forward "\nreferences:" nil t)
5405                   (progn
5406                     (setq end (point))
5407                     (prog1
5408                         (nnheader-header-value)
5409                       (setq ref
5410                             (buffer-substring
5411                              (progn
5412                                (end-of-line)
5413                                (search-backward ">" end t)
5414                                (1+ (point)))
5415                              (progn
5416                                (search-backward "<" end t)
5417                                (point))))))
5418                 ;; Get the references from the in-reply-to header if there
5419                 ;; were no references and the in-reply-to header looks
5420                 ;; promising.
5421                 (if (and (search-forward "\nin-reply-to:" nil t)
5422                          (setq in-reply-to (nnheader-header-value))
5423                          (string-match "<[^>]+>" in-reply-to))
5424                     (let (ref2)
5425                       (setq ref (substring in-reply-to (match-beginning 0)
5426                                            (match-end 0)))
5427                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5428                         (setq ref2 (substring in-reply-to (match-beginning 0)
5429                                               (match-end 0)))
5430                         (when (> (length ref2) (length ref))
5431                           (setq ref ref2)))
5432                       ref)
5433                   (setq ref nil))))
5434             ;; Chars.
5435             (progn
5436               (goto-char p)
5437               (if (search-forward "\nchars: " nil t)
5438                   (if (numberp (setq chars (ignore-errors (read cur))))
5439                       chars -1)
5440                 -1))
5441             ;; Lines.
5442             (progn
5443               (goto-char p)
5444               (if (search-forward "\nlines: " nil t)
5445                   (if (numberp (setq lines (ignore-errors (read cur))))
5446                       lines -1)
5447                 -1))
5448             ;; Xref.
5449             (progn
5450               (goto-char p)
5451               (and (search-forward "\nxref:" nil t)
5452                    (nnheader-header-value)))
5453             ;; Extra.
5454             (when gnus-extra-headers
5455               (let ((extra gnus-extra-headers)
5456                     out)
5457                 (while extra
5458                   (goto-char p)
5459                   (when (search-forward
5460                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5461                     (push (cons (car extra) (nnheader-header-value))
5462                           out))
5463                   (pop extra))
5464                 out))))
5465           (when (equal id ref)
5466             (setq ref nil))
5467
5468           (when gnus-alter-header-function
5469             (funcall gnus-alter-header-function header)
5470             (setq id (mail-header-id header)
5471                   ref (gnus-parent-id (mail-header-references header))))
5472
5473           (when (setq header
5474                       (gnus-dependencies-add-header
5475                        header dependencies force-new))
5476             (push header headers))
5477           (goto-char (point-max))
5478           (widen))
5479         (nreverse headers)))))
5480
5481 ;; Goes through the xover lines and returns a list of vectors
5482 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5483                                                   force-new dependencies
5484                                                   group also-fetch-heads)
5485   "Parse the news overview data in the server buffer.
5486 Return a list of headers that match SEQUENCE (see
5487 `nntp-retrieve-headers')."
5488   ;; Get the Xref when the users reads the articles since most/some
5489   ;; NNTP servers do not include Xrefs when using XOVER.
5490   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5491   (let ((mail-parse-charset gnus-newsgroup-charset)
5492         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5493         (cur nntp-server-buffer)
5494         (dependencies (or dependencies gnus-newsgroup-dependencies))
5495         (allp (cond
5496                ((eq gnus-read-all-available-headers t)
5497                 t)
5498                ((stringp gnus-read-all-available-headers)
5499                 (string-match gnus-read-all-available-headers group))
5500                (t
5501                 nil)))
5502         number headers header)
5503     (save-excursion
5504       (set-buffer nntp-server-buffer)
5505       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5506       ;; Allow the user to mangle the headers before parsing them.
5507       (gnus-run-hooks 'gnus-parse-headers-hook)
5508       (goto-char (point-min))
5509       (while (not (eobp))
5510         (condition-case ()
5511             (while (and (or sequence allp)
5512                         (not (eobp)))
5513               (setq number (read cur))
5514               (when (not allp)
5515                 (while (and sequence
5516                             (< (car sequence) number))
5517                   (setq sequence (cdr sequence))))
5518               (when (and (or allp
5519                              (and sequence
5520                                   (eq number (car sequence))))
5521                          (progn
5522                            (setq sequence (cdr sequence))
5523                            (setq header (inline
5524                                           (gnus-nov-parse-line
5525                                            number dependencies force-new)))))
5526                 (push header headers))
5527               (forward-line 1))
5528           (error
5529            (gnus-error 4 "Strange nov line (%d)"
5530                        (count-lines (point-min) (point)))))
5531         (forward-line 1))
5532       ;; A common bug in inn is that if you have posted an article and
5533       ;; then retrieves the active file, it will answer correctly --
5534       ;; the new article is included.  However, a NOV entry for the
5535       ;; article may not have been generated yet, so this may fail.
5536       ;; We work around this problem by retrieving the last few
5537       ;; headers using HEAD.
5538       (if (or (not also-fetch-heads)
5539               (not sequence))
5540           ;; We (probably) got all the headers.
5541           (nreverse headers)
5542         (let ((gnus-nov-is-evil t))
5543           (nconc
5544            (nreverse headers)
5545            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5546              (gnus-get-newsgroup-headers))))))))
5547
5548 (defun gnus-article-get-xrefs ()
5549   "Fill in the Xref value in `gnus-current-headers', if necessary.
5550 This is meant to be called in `gnus-article-internal-prepare-hook'."
5551   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5552                                  gnus-current-headers)))
5553     (or (not gnus-use-cross-reference)
5554         (not headers)
5555         (and (mail-header-xref headers)
5556              (not (string= (mail-header-xref headers) "")))
5557         (let ((case-fold-search t)
5558               xref)
5559           (save-restriction
5560             (nnheader-narrow-to-headers)
5561             (goto-char (point-min))
5562             (when (or (and (not (eobp))
5563                            (eq (downcase (char-after)) ?x)
5564                            (looking-at "Xref:"))
5565                       (search-forward "\nXref:" nil t))
5566               (goto-char (1+ (match-end 0)))
5567               (setq xref (buffer-substring (point)
5568                                            (progn (end-of-line) (point))))
5569               (mail-header-set-xref headers xref)))))))
5570
5571 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5572   "Find article ID and insert the summary line for that article.
5573 OLD-HEADER can either be a header or a line number to insert
5574 the subject line on."
5575   (let* ((line (and (numberp old-header) old-header))
5576          (old-header (and (vectorp old-header) old-header))
5577          (header (cond ((and old-header use-old-header)
5578                         old-header)
5579                        ((and (numberp id)
5580                              (gnus-number-to-header id))
5581                         (gnus-number-to-header id))
5582                        (t
5583                         (gnus-read-header id))))
5584          (number (and (numberp id) id))
5585          d)
5586     (when header
5587       ;; Rebuild the thread that this article is part of and go to the
5588       ;; article we have fetched.
5589       (when (and (not gnus-show-threads)
5590                  old-header)
5591         (when (and number
5592                    (setq d (gnus-data-find (mail-header-number old-header))))
5593           (goto-char (gnus-data-pos d))
5594           (gnus-data-remove
5595            number
5596            (- (gnus-point-at-bol)
5597               (prog1
5598                   (1+ (gnus-point-at-eol))
5599                 (gnus-delete-line))))))
5600       (when old-header
5601         (mail-header-set-number header (mail-header-number old-header)))
5602       (setq gnus-newsgroup-sparse
5603             (delq (setq number (mail-header-number header))
5604                   gnus-newsgroup-sparse))
5605       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5606       (push number gnus-newsgroup-limit)
5607       (gnus-rebuild-thread (mail-header-id header) line)
5608       (gnus-summary-goto-subject number nil t))
5609     (when (and (numberp number)
5610                (> number 0))
5611       ;; We have to update the boundaries even if we can't fetch the
5612       ;; article if ID is a number -- so that the next `P' or `N'
5613       ;; command will fetch the previous (or next) article even
5614       ;; if the one we tried to fetch this time has been canceled.
5615       (when (> number gnus-newsgroup-end)
5616         (setq gnus-newsgroup-end number))
5617       (when (< number gnus-newsgroup-begin)
5618         (setq gnus-newsgroup-begin number))
5619       (setq gnus-newsgroup-unselected
5620             (delq number gnus-newsgroup-unselected)))
5621     ;; Report back a success?
5622     (and header (mail-header-number header))))
5623
5624 ;;; Process/prefix in the summary buffer
5625
5626 (defun gnus-summary-work-articles (n)
5627   "Return a list of articles to be worked upon.
5628 The prefix argument, the list of process marked articles, and the
5629 current article will be taken into consideration."
5630   (save-excursion
5631     (set-buffer gnus-summary-buffer)
5632     (cond
5633      (n
5634       ;; A numerical prefix has been given.
5635       (setq n (prefix-numeric-value n))
5636       (let ((backward (< n 0))
5637             (n (abs (prefix-numeric-value n)))
5638             articles article)
5639         (save-excursion
5640           (while
5641               (and (> n 0)
5642                    (push (setq article (gnus-summary-article-number))
5643                          articles)
5644                    (if backward
5645                        (gnus-summary-find-prev nil article)
5646                      (gnus-summary-find-next nil article)))
5647             (decf n)))
5648         (nreverse articles)))
5649      ((and (gnus-region-active-p) (mark))
5650       (message "region active")
5651       ;; Work on the region between point and mark.
5652       (let ((max (max (point) (mark)))
5653             articles article)
5654         (save-excursion
5655           (goto-char (min (min (point) (mark))))
5656           (while
5657               (and
5658                (push (setq article (gnus-summary-article-number)) articles)
5659                (gnus-summary-find-next nil article)
5660                (< (point) max)))
5661           (nreverse articles))))
5662      (gnus-newsgroup-processable
5663       ;; There are process-marked articles present.
5664       ;; Save current state.
5665       (gnus-summary-save-process-mark)
5666       ;; Return the list.
5667       (reverse gnus-newsgroup-processable))
5668      (t
5669       ;; Just return the current article.
5670       (list (gnus-summary-article-number))))))
5671
5672 (defmacro gnus-summary-iterate (arg &rest forms)
5673   "Iterate over the process/prefixed articles and do FORMS.
5674 ARG is the interactive prefix given to the command.  FORMS will be
5675 executed with point over the summary line of the articles."
5676   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5677     `(let ((,articles (gnus-summary-work-articles ,arg)))
5678        (while ,articles
5679          (gnus-summary-goto-subject (car ,articles))
5680          ,@forms
5681          (pop ,articles)))))
5682
5683 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5684 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5685
5686 (defun gnus-summary-save-process-mark ()
5687   "Push the current set of process marked articles on the stack."
5688   (interactive)
5689   (push (copy-sequence gnus-newsgroup-processable)
5690         gnus-newsgroup-process-stack))
5691
5692 (defun gnus-summary-kill-process-mark ()
5693   "Push the current set of process marked articles on the stack and unmark."
5694   (interactive)
5695   (gnus-summary-save-process-mark)
5696   (gnus-summary-unmark-all-processable))
5697
5698 (defun gnus-summary-yank-process-mark ()
5699   "Pop the last process mark state off the stack and restore it."
5700   (interactive)
5701   (unless gnus-newsgroup-process-stack
5702     (error "Empty mark stack"))
5703   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5704
5705 (defun gnus-summary-process-mark-set (set)
5706   "Make SET into the current process marked articles."
5707   (gnus-summary-unmark-all-processable)
5708   (while set
5709     (gnus-summary-set-process-mark (pop set))))
5710
5711 ;;; Searching and stuff
5712
5713 (defun gnus-summary-search-group (&optional backward use-level)
5714   "Search for next unread newsgroup.
5715 If optional argument BACKWARD is non-nil, search backward instead."
5716   (save-excursion
5717     (set-buffer gnus-group-buffer)
5718     (when (gnus-group-search-forward
5719            backward nil (if use-level (gnus-group-group-level) nil))
5720       (gnus-group-group-name))))
5721
5722 (defun gnus-summary-best-group (&optional exclude-group)
5723   "Find the name of the best unread group.
5724 If EXCLUDE-GROUP, do not go to this group."
5725   (save-excursion
5726     (set-buffer gnus-group-buffer)
5727     (save-excursion
5728       (gnus-group-best-unread-group exclude-group))))
5729
5730 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5731   (if backward (gnus-summary-find-prev)
5732     (let* ((dummy (gnus-summary-article-intangible-p))
5733            (article (or article (gnus-summary-article-number)))
5734            (arts (gnus-data-find-list article))
5735            result)
5736       (when (and (not dummy)
5737                  (or (not gnus-summary-check-current)
5738                      (not unread)
5739                      (not (gnus-data-unread-p (car arts)))))
5740         (setq arts (cdr arts)))
5741       (when (setq result
5742                   (if unread
5743                       (progn
5744                         (while arts
5745                           (when (or (and undownloaded
5746                                          (eq gnus-undownloaded-mark
5747                                              (gnus-data-mark (car arts))))
5748                                     (gnus-data-unread-p (car arts)))
5749                             (setq result (car arts)
5750                                   arts nil))
5751                           (setq arts (cdr arts)))
5752                         result)
5753                     (car arts)))
5754         (goto-char (gnus-data-pos result))
5755         (gnus-data-number result)))))
5756
5757 (defun gnus-summary-find-prev (&optional unread article)
5758   (let* ((eobp (eobp))
5759          (article (or article (gnus-summary-article-number)))
5760          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5761          result)
5762     (when (and (not eobp)
5763                (or (not gnus-summary-check-current)
5764                    (not unread)
5765                    (not (gnus-data-unread-p (car arts)))))
5766       (setq arts (cdr arts)))
5767     (when (setq result
5768                 (if unread
5769                     (progn
5770                       (while arts
5771                         (when (gnus-data-unread-p (car arts))
5772                           (setq result (car arts)
5773                                 arts nil))
5774                         (setq arts (cdr arts)))
5775                       result)
5776                   (car arts)))
5777       (goto-char (gnus-data-pos result))
5778       (gnus-data-number result))))
5779
5780 (defun gnus-summary-find-subject (subject &optional unread backward article)
5781   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5782          (article (or article (gnus-summary-article-number)))
5783          (articles (gnus-data-list backward))
5784          (arts (gnus-data-find-list article articles))
5785          result)
5786     (when (or (not gnus-summary-check-current)
5787               (not unread)
5788               (not (gnus-data-unread-p (car arts))))
5789       (setq arts (cdr arts)))
5790     (while arts
5791       (and (or (not unread)
5792                (gnus-data-unread-p (car arts)))
5793            (vectorp (gnus-data-header (car arts)))
5794            (gnus-subject-equal
5795             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5796            (setq result (car arts)
5797                  arts nil))
5798       (setq arts (cdr arts)))
5799     (and result
5800          (goto-char (gnus-data-pos result))
5801          (gnus-data-number result))))
5802
5803 (defun gnus-summary-search-forward (&optional unread subject backward)
5804   "Search forward for an article.
5805 If UNREAD, look for unread articles.  If SUBJECT, look for
5806 articles with that subject.  If BACKWARD, search backward instead."
5807   (cond (subject (gnus-summary-find-subject subject unread backward))
5808         (backward (gnus-summary-find-prev unread))
5809         (t (gnus-summary-find-next unread))))
5810
5811 (defun gnus-recenter (&optional n)
5812   "Center point in window and redisplay frame.
5813 Also do horizontal recentering."
5814   (interactive "P")
5815   (when (and gnus-auto-center-summary
5816              (not (eq gnus-auto-center-summary 'vertical)))
5817     (gnus-horizontal-recenter))
5818   (recenter n))
5819
5820 (defun gnus-summary-recenter ()
5821   "Center point in the summary window.
5822 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5823 displayed, no centering will be performed."
5824   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5825 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5826   (interactive)
5827   (let* ((top (cond ((< (window-height) 4) 0)
5828                     ((< (window-height) 7) 1)
5829                     (t (if (numberp gnus-auto-center-summary)
5830                            gnus-auto-center-summary
5831                          2))))
5832          (height (1- (window-height)))
5833          (bottom (save-excursion (goto-char (point-max))
5834                                  (forward-line (- height))
5835                                  (point)))
5836          (window (get-buffer-window (current-buffer))))
5837     ;; The user has to want it.
5838     (when gnus-auto-center-summary
5839       (when (get-buffer-window gnus-article-buffer)
5840         ;; Only do recentering when the article buffer is displayed,
5841       ;; Set the window start to either `bottom', which is the biggest
5842         ;; possible valid number, or the second line from the top,
5843         ;; whichever is the least.
5844         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5845           (if (> bottom top-pos)
5846               ;; Keep the second line from the top visible
5847               (set-window-start window top-pos t)
5848             ;; Try to keep the bottom line visible; if it's partially
5849             ;; obscured, either scroll one more line to make it fully
5850             ;; visible, or revert to using TOP-POS.
5851             (save-excursion
5852               (goto-char (point-max))
5853               (forward-line -1)
5854               (let ((last-line-start (point)))
5855                 (goto-char bottom)
5856                 (set-window-start window (point) t)
5857                 (when (not (pos-visible-in-window-p last-line-start window))
5858                   (forward-line 1)
5859                   (set-window-start window (min (point) top-pos) t)))))))
5860       ;; Do horizontal recentering while we're at it.
5861       (when (and (get-buffer-window (current-buffer) t)
5862                  (not (eq gnus-auto-center-summary 'vertical)))
5863         (let ((selected (selected-window)))
5864           (select-window (get-buffer-window (current-buffer) t))
5865           (gnus-summary-position-point)
5866           (gnus-horizontal-recenter)
5867           (select-window selected))))))
5868
5869 (defun gnus-summary-jump-to-group (newsgroup)
5870   "Move point to NEWSGROUP in group mode buffer."
5871   ;; Keep update point of group mode buffer if visible.
5872   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5873       (save-window-excursion
5874         ;; Take care of tree window mode.
5875         (when (get-buffer-window gnus-group-buffer)
5876           (pop-to-buffer gnus-group-buffer))
5877         (gnus-group-jump-to-group newsgroup))
5878     (save-excursion
5879       ;; Take care of tree window mode.
5880       (if (get-buffer-window gnus-group-buffer)
5881           (pop-to-buffer gnus-group-buffer)
5882         (set-buffer gnus-group-buffer))
5883       (gnus-group-jump-to-group newsgroup))))
5884
5885 ;; This function returns a list of article numbers based on the
5886 ;; difference between the ranges of read articles in this group and
5887 ;; the range of active articles.
5888 (defun gnus-list-of-unread-articles (group)
5889   (let* ((read (gnus-info-read (gnus-get-info group)))
5890          (active (or (gnus-active group) (gnus-activate-group group)))
5891          (last (cdr active))
5892          first nlast unread)
5893     ;; If none are read, then all are unread.
5894     (if (not read)
5895         (setq first (car active))
5896       ;; If the range of read articles is a single range, then the
5897       ;; first unread article is the article after the last read
5898       ;; article.  Sounds logical, doesn't it?
5899       (if (and (not (listp (cdr read)))
5900                (or (< (car read) (car active))
5901                    (progn (setq read (list read))
5902                           nil)))
5903           (setq first (max (car active) (1+ (cdr read))))
5904         ;; `read' is a list of ranges.
5905         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5906                                   (caar read)))
5907                   1)
5908           (setq first (car active)))
5909         (while read
5910           (when first
5911             (while (< first nlast)
5912               (push first unread)
5913               (setq first (1+ first))))
5914           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5915           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5916           (setq read (cdr read)))))
5917     ;; And add the last unread articles.
5918     (while (<= first last)
5919       (push first unread)
5920       (setq first (1+ first)))
5921     ;; Return the list of unread articles.
5922     (delq 0 (nreverse unread))))
5923
5924 (defun gnus-list-of-read-articles (group)
5925   "Return a list of unread, unticked and non-dormant articles."
5926   (let* ((info (gnus-get-info group))
5927          (marked (gnus-info-marks info))
5928          (active (gnus-active group)))
5929     (and info active
5930          (gnus-set-difference
5931           (gnus-sorted-complement
5932            (gnus-uncompress-range active)
5933            (gnus-list-of-unread-articles group))
5934           (append
5935            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5936            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5937
5938 ;; Various summary commands
5939
5940 (defun gnus-summary-select-article-buffer ()
5941   "Reconfigure windows to show article buffer."
5942   (interactive)
5943   (if (not (gnus-buffer-live-p gnus-article-buffer))
5944       (error "There is no article buffer for this summary buffer")
5945     (gnus-configure-windows 'article)
5946     (select-window (get-buffer-window gnus-article-buffer))))
5947
5948 (defun gnus-summary-universal-argument (arg)
5949   "Perform any operation on all articles that are process/prefixed."
5950   (interactive "P")
5951   (let ((articles (gnus-summary-work-articles arg))
5952         func article)
5953     (if (eq
5954          (setq
5955           func
5956           (key-binding
5957            (read-key-sequence
5958             (substitute-command-keys
5959              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5960          'undefined)
5961         (gnus-error 1 "Undefined key")
5962       (save-excursion
5963         (while articles
5964           (gnus-summary-goto-subject (setq article (pop articles)))
5965           (let (gnus-newsgroup-processable)
5966             (command-execute func))
5967           (gnus-summary-remove-process-mark article)))))
5968   (gnus-summary-position-point))
5969
5970 (defun gnus-summary-toggle-truncation (&optional arg)
5971   "Toggle truncation of summary lines.
5972 With arg, turn line truncation on iff arg is positive."
5973   (interactive "P")
5974   (setq truncate-lines
5975         (if (null arg) (not truncate-lines)
5976           (> (prefix-numeric-value arg) 0)))
5977   (redraw-display))
5978
5979 (defun gnus-summary-reselect-current-group (&optional all rescan)
5980   "Exit and then reselect the current newsgroup.
5981 The prefix argument ALL means to select all articles."
5982   (interactive "P")
5983   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5984     (error "Ephemeral groups can't be reselected"))
5985   (let ((current-subject (gnus-summary-article-number))
5986         (group gnus-newsgroup-name))
5987     (setq gnus-newsgroup-begin nil)
5988     (gnus-summary-exit)
5989     ;; We have to adjust the point of group mode buffer because
5990     ;; point was moved to the next unread newsgroup by exiting.
5991     (gnus-summary-jump-to-group group)
5992     (when rescan
5993       (save-excursion
5994         (gnus-group-get-new-news-this-group 1)))
5995     (gnus-group-read-group all t)
5996     (gnus-summary-goto-subject current-subject nil t)))
5997
5998 (defun gnus-summary-rescan-group (&optional all)
5999   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6000   (interactive "P")
6001   (gnus-summary-reselect-current-group all t))
6002
6003 (defun gnus-summary-update-info (&optional non-destructive)
6004   (save-excursion
6005     (let ((group gnus-newsgroup-name))
6006       (when group
6007         (when gnus-newsgroup-kill-headers
6008           (setq gnus-newsgroup-killed
6009                 (gnus-compress-sequence
6010                  (nconc
6011                   (gnus-set-sorted-intersection
6012                    (gnus-uncompress-range gnus-newsgroup-killed)
6013                    (setq gnus-newsgroup-unselected
6014                          (sort gnus-newsgroup-unselected '<)))
6015                   (setq gnus-newsgroup-unreads
6016                         (sort gnus-newsgroup-unreads '<)))
6017                  t)))
6018         (unless (listp (cdr gnus-newsgroup-killed))
6019           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6020         (let ((headers gnus-newsgroup-headers))
6021           ;; Set the new ranges of read articles.
6022           (save-excursion
6023             (set-buffer gnus-group-buffer)
6024             (gnus-undo-force-boundary))
6025           (gnus-update-read-articles
6026            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
6027           ;; Set the current article marks.
6028           (let ((gnus-newsgroup-scored
6029                  (if (and (not gnus-save-score)
6030                           (not non-destructive))
6031                      nil
6032                    gnus-newsgroup-scored)))
6033             (save-excursion
6034               (gnus-update-marks)))
6035           ;; Do the cross-ref thing.
6036           (when gnus-use-cross-reference
6037             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6038           ;; Do not switch windows but change the buffer to work.
6039           (set-buffer gnus-group-buffer)
6040           (unless (gnus-ephemeral-group-p group)
6041             (gnus-group-update-group group)))))))
6042
6043 (defun gnus-summary-save-newsrc (&optional force)
6044   "Save the current number of read/marked articles in the dribble buffer.
6045 The dribble buffer will then be saved.
6046 If FORCE (the prefix), also save the .newsrc file(s)."
6047   (interactive "P")
6048   (gnus-summary-update-info t)
6049   (if force
6050       (gnus-save-newsrc-file)
6051     (gnus-dribble-save)))
6052
6053 (defun gnus-summary-exit (&optional temporary)
6054   "Exit reading current newsgroup, and then return to group selection mode.
6055 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6056   (interactive)
6057   (gnus-set-global-variables)
6058   (when (gnus-buffer-live-p gnus-article-buffer)
6059     (save-excursion
6060       (set-buffer gnus-article-buffer)
6061       (mm-destroy-parts gnus-article-mime-handles)
6062       ;; Set it to nil for safety reason.
6063       (setq gnus-article-mime-handle-alist nil)
6064       (setq gnus-article-mime-handles nil)))
6065   (gnus-kill-save-kill-buffer)
6066   (gnus-async-halt-prefetch)
6067   (let* ((group gnus-newsgroup-name)
6068          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6069          (mode major-mode)
6070          (group-point nil)
6071          (buf (current-buffer)))
6072     (unless quit-config
6073       ;; Do adaptive scoring, and possibly save score files.
6074       (when gnus-newsgroup-adaptive
6075         (gnus-score-adaptive))
6076       (when gnus-use-scoring
6077         (gnus-score-save)))
6078     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6079     ;; If we have several article buffers, we kill them at exit.
6080     (unless gnus-single-article-buffer
6081       (gnus-kill-buffer gnus-original-article-buffer)
6082       (setq gnus-article-current nil))
6083     (when gnus-use-cache
6084       (gnus-cache-possibly-remove-articles)
6085       (gnus-cache-save-buffers))
6086     (gnus-async-prefetch-remove-group group)
6087     (when gnus-suppress-duplicates
6088       (gnus-dup-enter-articles))
6089     (when gnus-use-trees
6090       (gnus-tree-close group))
6091     (when gnus-use-cache
6092       (gnus-cache-write-active))
6093     ;; Remove entries for this group.
6094     (nnmail-purge-split-history (gnus-group-real-name group))
6095     ;; Make all changes in this group permanent.
6096     (unless quit-config
6097       (gnus-run-hooks 'gnus-exit-group-hook)
6098       (gnus-summary-update-info))
6099     (gnus-close-group group)
6100     ;; Make sure where we were, and go to next newsgroup.
6101     (set-buffer gnus-group-buffer)
6102     (unless quit-config
6103       (gnus-group-jump-to-group group))
6104     (gnus-run-hooks 'gnus-summary-exit-hook)
6105     (unless (or quit-config
6106                 ;; If this group has disappeared from the summary
6107                 ;; buffer, don't skip forwards.
6108                 (not (string= group (gnus-group-group-name))))
6109       (gnus-group-next-unread-group 1))
6110     (setq group-point (point))
6111     (if temporary
6112         nil                             ;Nothing to do.
6113       ;; If we have several article buffers, we kill them at exit.
6114       (unless gnus-single-article-buffer
6115         (gnus-kill-buffer gnus-article-buffer)
6116         (gnus-kill-buffer gnus-original-article-buffer)
6117         (setq gnus-article-current nil))
6118       (set-buffer buf)
6119       (if (not gnus-kill-summary-on-exit)
6120           (progn
6121             (gnus-deaden-summary)
6122             (setq mode nil))
6123        ;; We set all buffer-local variables to nil.  It is unclear why
6124         ;; this is needed, but if we don't, buffer-local variables are
6125         ;; not garbage-collected, it seems.  This would the lead to en
6126         ;; ever-growing Emacs.
6127         (gnus-summary-clear-local-variables)
6128         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6129           (gnus-summary-clear-local-variables))
6130         (when (get-buffer gnus-article-buffer)
6131           (bury-buffer gnus-article-buffer))
6132         ;; We clear the global counterparts of the buffer-local
6133         ;; variables as well, just to be on the safe side.
6134         (set-buffer gnus-group-buffer)
6135         (gnus-summary-clear-local-variables)
6136         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6137           (gnus-summary-clear-local-variables)))
6138       (setq gnus-current-select-method gnus-select-method)
6139       (pop-to-buffer gnus-group-buffer)
6140       (if (not quit-config)
6141           (progn
6142             (goto-char group-point)
6143             (gnus-configure-windows 'group 'force))
6144         (gnus-handle-ephemeral-exit quit-config))
6145       ;; Return to group mode buffer.
6146       (when (eq mode 'gnus-summary-mode)
6147         (gnus-kill-buffer buf))
6148       ;; Clear the current group name.
6149       (unless quit-config
6150         (setq gnus-newsgroup-name nil)))))
6151
6152 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6153 (defun gnus-summary-exit-no-update (&optional no-questions)
6154   "Quit reading current newsgroup without updating read article info."
6155   (interactive)
6156   (let* ((group gnus-newsgroup-name)
6157          (quit-config (gnus-group-quit-config group)))
6158     (when (or no-questions
6159               gnus-expert-user
6160               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6161       (gnus-async-halt-prefetch)
6162       (mapcar 'funcall
6163               (delq 'gnus-summary-expire-articles
6164                     (copy-sequence gnus-summary-prepare-exit-hook)))
6165       (when (gnus-buffer-live-p gnus-article-buffer)
6166         (save-excursion
6167           (set-buffer gnus-article-buffer)
6168           (mm-destroy-parts gnus-article-mime-handles)
6169           ;; Set it to nil for safety reason.
6170           (setq gnus-article-mime-handle-alist nil)
6171           (setq gnus-article-mime-handles nil)))
6172       ;; If we have several article buffers, we kill them at exit.
6173       (unless gnus-single-article-buffer
6174         (gnus-kill-buffer gnus-article-buffer)
6175         (gnus-kill-buffer gnus-original-article-buffer)
6176         (setq gnus-article-current nil))
6177       (if (not gnus-kill-summary-on-exit)
6178           (gnus-deaden-summary)
6179         (gnus-close-group group)
6180         (gnus-summary-clear-local-variables)
6181         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6182           (gnus-summary-clear-local-variables))
6183         (set-buffer gnus-group-buffer)
6184         (gnus-summary-clear-local-variables)
6185         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6186           (gnus-summary-clear-local-variables))
6187         (when (get-buffer gnus-summary-buffer)
6188           (kill-buffer gnus-summary-buffer)))
6189       (unless gnus-single-article-buffer
6190         (setq gnus-article-current nil))
6191       (when gnus-use-trees
6192         (gnus-tree-close group))
6193       (gnus-async-prefetch-remove-group group)
6194       (when (get-buffer gnus-article-buffer)
6195         (bury-buffer gnus-article-buffer))
6196       ;; Return to the group buffer.
6197       (gnus-configure-windows 'group 'force)
6198       ;; Clear the current group name.
6199       (setq gnus-newsgroup-name nil)
6200       (when (equal (gnus-group-group-name) group)
6201         (gnus-group-next-unread-group 1))
6202       (when quit-config
6203         (gnus-handle-ephemeral-exit quit-config)))))
6204
6205 (defun gnus-handle-ephemeral-exit (quit-config)
6206   "Handle movement when leaving an ephemeral group.
6207 The state which existed when entering the ephemeral is reset."
6208   (if (not (buffer-name (car quit-config)))
6209       (gnus-configure-windows 'group 'force)
6210     (set-buffer (car quit-config))
6211     (cond ((eq major-mode 'gnus-summary-mode)
6212            (gnus-set-global-variables))
6213           ((eq major-mode 'gnus-article-mode)
6214            (save-excursion
6215              ;; The `gnus-summary-buffer' variable may point
6216              ;; to the old summary buffer when using a single
6217              ;; article buffer.
6218              (unless (gnus-buffer-live-p gnus-summary-buffer)
6219                (set-buffer gnus-group-buffer))
6220              (set-buffer gnus-summary-buffer)
6221              (gnus-set-global-variables))))
6222     (if (or (eq (cdr quit-config) 'article)
6223             (eq (cdr quit-config) 'pick))
6224         (progn
6225           ;; The current article may be from the ephemeral group
6226           ;; thus it is best that we reload this article
6227           (gnus-summary-show-article)
6228           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6229               (gnus-configure-windows 'pick 'force)
6230             (gnus-configure-windows (cdr quit-config) 'force)))
6231       (gnus-configure-windows (cdr quit-config) 'force))
6232     (when (eq major-mode 'gnus-summary-mode)
6233       (gnus-summary-next-subject 1 nil t)
6234       (gnus-summary-recenter)
6235       (gnus-summary-position-point))))
6236
6237 ;;; Dead summaries.
6238
6239 (defvar gnus-dead-summary-mode-map nil)
6240
6241 (unless gnus-dead-summary-mode-map
6242   (setq gnus-dead-summary-mode-map (make-keymap))
6243   (suppress-keymap gnus-dead-summary-mode-map)
6244   (substitute-key-definition
6245    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6246   (dolist (key '("\C-d" "\r" "\177" [delete]))
6247     (define-key gnus-dead-summary-mode-map
6248       key 'gnus-summary-wake-up-the-dead))
6249   (dolist (key '("q" "Q"))
6250     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6251
6252 (defvar gnus-dead-summary-mode nil
6253   "Minor mode for Gnus summary buffers.")
6254
6255 (defun gnus-dead-summary-mode (&optional arg)
6256   "Minor mode for Gnus summary buffers."
6257   (interactive "P")
6258   (when (eq major-mode 'gnus-summary-mode)
6259     (make-local-variable 'gnus-dead-summary-mode)
6260     (setq gnus-dead-summary-mode
6261           (if (null arg) (not gnus-dead-summary-mode)
6262             (> (prefix-numeric-value arg) 0)))
6263     (when gnus-dead-summary-mode
6264       (gnus-add-minor-mode
6265        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6266
6267 (defun gnus-deaden-summary ()
6268   "Make the current summary buffer into a dead summary buffer."
6269   ;; Kill any previous dead summary buffer.
6270   (when (and gnus-dead-summary
6271              (buffer-name gnus-dead-summary))
6272     (save-excursion
6273       (set-buffer gnus-dead-summary)
6274       (when gnus-dead-summary-mode
6275         (kill-buffer (current-buffer)))))
6276   ;; Make this the current dead summary.
6277   (setq gnus-dead-summary (current-buffer))
6278   (gnus-dead-summary-mode 1)
6279   (let ((name (buffer-name)))
6280     (when (string-match "Summary" name)
6281       (rename-buffer
6282        (concat (substring name 0 (match-beginning 0)) "Dead "
6283                (substring name (match-beginning 0)))
6284        t)
6285       (bury-buffer))))
6286
6287 (defun gnus-kill-or-deaden-summary (buffer)
6288   "Kill or deaden the summary BUFFER."
6289   (save-excursion
6290     (when (and (buffer-name buffer)
6291                (not gnus-single-article-buffer))
6292       (save-excursion
6293         (set-buffer buffer)
6294         (gnus-kill-buffer gnus-article-buffer)
6295         (gnus-kill-buffer gnus-original-article-buffer)))
6296     (cond
6297      ;; Kill the buffer.
6298      (gnus-kill-summary-on-exit
6299       (when (and gnus-use-trees
6300                  (gnus-buffer-exists-p buffer))
6301         (save-excursion
6302           (set-buffer buffer)
6303           (gnus-tree-close gnus-newsgroup-name)))
6304       (gnus-kill-buffer buffer))
6305      ;; Deaden the buffer.
6306      ((gnus-buffer-exists-p buffer)
6307       (save-excursion
6308         (set-buffer buffer)
6309         (gnus-deaden-summary))))))
6310
6311 (defun gnus-summary-wake-up-the-dead (&rest args)
6312   "Wake up the dead summary buffer."
6313   (interactive)
6314   (gnus-dead-summary-mode -1)
6315   (let ((name (buffer-name)))
6316     (when (string-match "Dead " name)
6317       (rename-buffer
6318        (concat (substring name 0 (match-beginning 0))
6319                (substring name (match-end 0)))
6320        t)))
6321   (gnus-message 3 "This dead summary is now alive again"))
6322
6323 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6324 (defun gnus-summary-fetch-faq (&optional faq-dir)
6325   "Fetch the FAQ for the current group.
6326 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6327 in."
6328   (interactive
6329    (list
6330     (when current-prefix-arg
6331       (completing-read
6332        "Faq dir: " (and (listp gnus-group-faq-directory)
6333                         (mapcar (lambda (file) (list file))
6334                                 gnus-group-faq-directory))))))
6335   (let (gnus-faq-buffer)
6336     (when (setq gnus-faq-buffer
6337                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6338       (gnus-configure-windows 'summary-faq))))
6339
6340 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6341 (defun gnus-summary-describe-group (&optional force)
6342   "Describe the current newsgroup."
6343   (interactive "P")
6344   (gnus-group-describe-group force gnus-newsgroup-name))
6345
6346 (defun gnus-summary-describe-briefly ()
6347   "Describe summary mode commands briefly."
6348   (interactive)
6349   (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")))
6350
6351 ;; Walking around group mode buffer from summary mode.
6352
6353 (defun gnus-summary-next-group (&optional no-article target-group backward)
6354   "Exit current newsgroup and then select next unread newsgroup.
6355 If prefix argument NO-ARTICLE is non-nil, no article is selected
6356 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6357 previous group instead."
6358   (interactive "P")
6359   ;; Stop pre-fetching.
6360   (gnus-async-halt-prefetch)
6361   (let ((current-group gnus-newsgroup-name)
6362         (current-buffer (current-buffer))
6363         entered)
6364    ;; First we semi-exit this group to update Xrefs and all variables.
6365     ;; We can't do a real exit, because the window conf must remain
6366     ;; the same in case the user is prompted for info, and we don't
6367     ;; want the window conf to change before that...
6368     (gnus-summary-exit t)
6369     (while (not entered)
6370       ;; Then we find what group we are supposed to enter.
6371       (set-buffer gnus-group-buffer)
6372       (gnus-group-jump-to-group current-group)
6373       (setq target-group
6374             (or target-group
6375                 (if (eq gnus-keep-same-level 'best)
6376                     (gnus-summary-best-group gnus-newsgroup-name)
6377                   (gnus-summary-search-group backward gnus-keep-same-level))))
6378       (if (not target-group)
6379           ;; There are no further groups, so we return to the group
6380           ;; buffer.
6381           (progn
6382             (gnus-message 5 "Returning to the group buffer")
6383             (setq entered t)
6384             (when (gnus-buffer-live-p current-buffer)
6385               (set-buffer current-buffer)
6386               (gnus-summary-exit))
6387             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6388         ;; We try to enter the target group.
6389         (gnus-group-jump-to-group target-group)
6390         (let ((unreads (gnus-group-group-unread)))
6391           (if (and (or (eq t unreads)
6392                        (and unreads (not (zerop unreads))))
6393                    (gnus-summary-read-group
6394                     target-group nil no-article
6395                     (and (buffer-name current-buffer) current-buffer)
6396                     nil backward))
6397               (setq entered t)
6398             (setq current-group target-group
6399                   target-group nil)))))))
6400
6401 (defun gnus-summary-prev-group (&optional no-article)
6402   "Exit current newsgroup and then select previous unread newsgroup.
6403 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6404   (interactive "P")
6405   (gnus-summary-next-group no-article nil t))
6406
6407 ;; Walking around summary lines.
6408
6409 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6410   "Go to the first unread subject.
6411 If UNREAD is non-nil, go to the first unread article.
6412 Returns the article selected or nil if there are no unread articles."
6413   (interactive "P")
6414   (prog1
6415       (cond
6416        ;; Empty summary.
6417        ((null gnus-newsgroup-data)
6418         (gnus-message 3 "No articles in the group")
6419         nil)
6420        ;; Pick the first article.
6421        ((not unread)
6422         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6423         (gnus-data-number (car gnus-newsgroup-data)))
6424        ;; No unread articles.
6425        ((null gnus-newsgroup-unreads)
6426         (gnus-message 3 "No more unread articles")
6427         nil)
6428        ;; Find the first unread article.
6429        (t
6430         (let ((data gnus-newsgroup-data))
6431           (while (and data
6432                       (and (not (and undownloaded
6433                                      (eq gnus-undownloaded-mark
6434                                          (gnus-data-mark (car data)))))
6435                            (if unseen
6436                                (or (not (memq
6437                                          (gnus-data-number (car data))
6438                                          gnus-newsgroup-unseen))
6439                                    (not (gnus-data-unread-p (car data))))
6440                              (not (gnus-data-unread-p (car data))))))
6441             (setq data (cdr data)))
6442           (when data
6443             (goto-char (gnus-data-pos (car data)))
6444             (gnus-data-number (car data))))))
6445     (gnus-summary-position-point)))
6446
6447 (defun gnus-summary-next-subject (n &optional unread dont-display)
6448   "Go to next N'th summary line.
6449 If N is negative, go to the previous N'th subject line.
6450 If UNREAD is non-nil, only unread articles are selected.
6451 The difference between N and the actual number of steps taken is
6452 returned."
6453   (interactive "p")
6454   (let ((backward (< n 0))
6455         (n (abs n)))
6456     (while (and (> n 0)
6457                 (if backward
6458                     (gnus-summary-find-prev unread)
6459                   (gnus-summary-find-next unread)))
6460       (unless (zerop (setq n (1- n)))
6461         (gnus-summary-show-thread)))
6462     (when (/= 0 n)
6463       (gnus-message 7 "No more%s articles"
6464                     (if unread " unread" "")))
6465     (unless dont-display
6466       (gnus-summary-recenter)
6467       (gnus-summary-position-point))
6468     n))
6469
6470 (defun gnus-summary-next-unread-subject (n)
6471   "Go to next N'th unread summary line."
6472   (interactive "p")
6473   (gnus-summary-next-subject n t))
6474
6475 (defun gnus-summary-prev-subject (n &optional unread)
6476   "Go to previous N'th summary line.
6477 If optional argument UNREAD is non-nil, only unread article is selected."
6478   (interactive "p")
6479   (gnus-summary-next-subject (- n) unread))
6480
6481 (defun gnus-summary-prev-unread-subject (n)
6482   "Go to previous N'th unread summary line."
6483   (interactive "p")
6484   (gnus-summary-next-subject (- n) t))
6485
6486 (defun gnus-summary-goto-subject (article &optional force silent)
6487   "Go the subject line of ARTICLE.
6488 If FORCE, also allow jumping to articles not currently shown."
6489   (interactive "nArticle number: ")
6490   (unless (numberp article)
6491     (error "Article %s is not a number" article))
6492   (let ((b (point))
6493         (data (gnus-data-find article)))
6494     ;; We read in the article if we have to.
6495     (and (not data)
6496          force
6497          (gnus-summary-insert-subject
6498           article
6499           (if (or (numberp force) (vectorp force)) force)
6500           t)
6501          (setq data (gnus-data-find article)))
6502     (goto-char b)
6503     (if (not data)
6504         (progn
6505           (unless silent
6506             (gnus-message 3 "Can't find article %d" article))
6507           nil)
6508       (let ((pt (gnus-data-pos data)))
6509         (goto-char pt)
6510         (gnus-summary-set-article-display-arrow pt))
6511       (gnus-summary-position-point)
6512       article)))
6513
6514 ;; Walking around summary lines with displaying articles.
6515
6516 (defun gnus-summary-expand-window (&optional arg)
6517   "Make the summary buffer take up the entire Emacs frame.
6518 Given a prefix, will force an `article' buffer configuration."
6519   (interactive "P")
6520   (if arg
6521       (gnus-configure-windows 'article 'force)
6522     (gnus-configure-windows 'summary 'force)))
6523
6524 (defun gnus-summary-display-article (article &optional all-header)
6525   "Display ARTICLE in article buffer."
6526   (when (gnus-buffer-live-p gnus-article-buffer)
6527     (with-current-buffer gnus-article-buffer
6528       (mm-enable-multibyte)))
6529   (gnus-set-global-variables)
6530   (when (gnus-buffer-live-p gnus-article-buffer)
6531     (with-current-buffer gnus-article-buffer
6532       (setq gnus-article-charset gnus-newsgroup-charset)
6533       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6534       (mm-enable-multibyte)))
6535   (if (null article)
6536       nil
6537     (prog1
6538         (if gnus-summary-display-article-function
6539             (funcall gnus-summary-display-article-function article all-header)
6540           (gnus-article-prepare article all-header))
6541       (gnus-run-hooks 'gnus-select-article-hook)
6542       (when (and gnus-current-article
6543                  (not (zerop gnus-current-article)))
6544         (gnus-summary-goto-subject gnus-current-article))
6545       (gnus-summary-recenter)
6546       (when (and gnus-use-trees gnus-show-threads)
6547         (gnus-possibly-generate-tree article)
6548         (gnus-highlight-selected-tree article))
6549       ;; Successfully display article.
6550       (gnus-article-set-window-start
6551        (cdr (assq article gnus-newsgroup-bookmarks))))))
6552
6553 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6554   "Select the current article.
6555 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6556 non-nil, the article will be re-fetched even if it already present in
6557 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6558 be displayed."
6559   ;; Make sure we are in the summary buffer to work around bbdb bug.
6560   (unless (eq major-mode 'gnus-summary-mode)
6561     (set-buffer gnus-summary-buffer))
6562   (let ((article (or article (gnus-summary-article-number)))
6563         (all-headers (not (not all-headers))) ;Must be t or nil.
6564         gnus-summary-display-article-function)
6565     (and (not pseudo)
6566          (gnus-summary-article-pseudo-p article)
6567          (error "This is a pseudo-article"))
6568     (save-excursion
6569       (set-buffer gnus-summary-buffer)
6570       (if (or (and gnus-single-article-buffer
6571                    (or (null gnus-current-article)
6572                        (null gnus-article-current)
6573                        (null (get-buffer gnus-article-buffer))
6574                        (not (eq article (cdr gnus-article-current)))
6575                        (not (equal (car gnus-article-current)
6576                                    gnus-newsgroup-name))))
6577               (and (not gnus-single-article-buffer)
6578                    (or (null gnus-current-article)
6579                        (not (eq gnus-current-article article))))
6580               force)
6581           ;; The requested article is different from the current article.
6582           (progn
6583             (gnus-summary-display-article article all-headers)
6584             (when (gnus-buffer-live-p gnus-article-buffer)
6585               (with-current-buffer gnus-article-buffer
6586                 (if (not gnus-article-decoded-p) ;; a local variable
6587                     (mm-disable-multibyte))))
6588 ;;; Hidden headers are not hidden text any more.
6589 ;;          (when (or all-headers gnus-show-all-headers)
6590 ;;            (gnus-article-show-all-headers))
6591             (gnus-article-set-window-start
6592              (cdr (assq article gnus-newsgroup-bookmarks)))
6593             article)
6594 ;;      (when (or all-headers gnus-show-all-headers)
6595 ;;        (gnus-article-show-all-headers))
6596         'old))))
6597
6598 (defun gnus-summary-force-verify-and-decrypt ()
6599   (interactive)
6600   (let ((mm-verify-option 'known)
6601         (mm-decrypt-option 'known))
6602     (gnus-summary-select-article nil 'force)))
6603
6604 (defun gnus-summary-set-current-mark (&optional current-mark)
6605   "Obsolete function."
6606   nil)
6607
6608 (defun gnus-summary-next-article (&optional unread subject backward push)
6609   "Select the next article.
6610 If UNREAD, only unread articles are selected.
6611 If SUBJECT, only articles with SUBJECT are selected.
6612 If BACKWARD, the previous article is selected instead of the next."
6613   (interactive "P")
6614   (cond
6615    ;; Is there such an article?
6616    ((and (gnus-summary-search-forward unread subject backward)
6617          (or (gnus-summary-display-article (gnus-summary-article-number))
6618              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6619     (gnus-summary-position-point))
6620    ;; If not, we try the first unread, if that is wanted.
6621    ((and subject
6622          gnus-auto-select-same
6623          (gnus-summary-first-unread-article))
6624     (gnus-summary-position-point)
6625     (gnus-message 6 "Wrapped"))
6626    ;; Try to get next/previous article not displayed in this group.
6627    ((and gnus-auto-extend-newsgroup
6628          (not unread) (not subject))
6629     (gnus-summary-goto-article
6630      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6631      nil (count-lines (point-min) (point))))
6632    ;; Go to next/previous group.
6633    (t
6634     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6635       (gnus-summary-jump-to-group gnus-newsgroup-name))
6636     (let ((cmd last-command-char)
6637           (point
6638            (save-excursion
6639              (set-buffer gnus-group-buffer)
6640              (point)))
6641           (group
6642            (if (eq gnus-keep-same-level 'best)
6643                (gnus-summary-best-group gnus-newsgroup-name)
6644              (gnus-summary-search-group backward gnus-keep-same-level))))
6645       ;; For some reason, the group window gets selected.  We change
6646       ;; it back.
6647       (select-window (get-buffer-window (current-buffer)))
6648       ;; Select next unread newsgroup automagically.
6649       (cond
6650        ((or (not gnus-auto-select-next)
6651             (not cmd))
6652         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6653        ((or (eq gnus-auto-select-next 'quietly)
6654             (and (eq gnus-auto-select-next 'slightly-quietly)
6655                  push)
6656             (and (eq gnus-auto-select-next 'almost-quietly)
6657                  (gnus-summary-last-article-p)))
6658         ;; Select quietly.
6659         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6660             (gnus-summary-exit)
6661           (gnus-message 7 "No more%s articles (%s)..."
6662                         (if unread " unread" "")
6663                         (if group (concat "selecting " group)
6664                           "exiting"))
6665           (gnus-summary-next-group nil group backward)))
6666        (t
6667         (when (gnus-key-press-event-p last-input-event)
6668           (gnus-summary-walk-group-buffer
6669            gnus-newsgroup-name cmd unread backward point))))))))
6670
6671 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6672   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6673                       (?\C-p (gnus-group-prev-unread-group 1))))
6674         (cursor-in-echo-area t)
6675         keve key group ended)
6676     (save-excursion
6677       (set-buffer gnus-group-buffer)
6678       (goto-char start)
6679       (setq group
6680             (if (eq gnus-keep-same-level 'best)
6681                 (gnus-summary-best-group gnus-newsgroup-name)
6682               (gnus-summary-search-group backward gnus-keep-same-level))))
6683     (while (not ended)
6684       (gnus-message
6685        5 "No more%s articles%s" (if unread " unread" "")
6686        (if (and group
6687                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6688            (format " (Type %s for %s [%s])"
6689                    (single-key-description cmd) group
6690                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6691          (format " (Type %s to exit %s)"
6692                  (single-key-description cmd)
6693                  gnus-newsgroup-name)))
6694       ;; Confirm auto selection.
6695       (setq key (car (setq keve (gnus-read-event-char))))
6696       (setq ended t)
6697       (cond
6698        ((assq key keystrokes)
6699         (let ((obuf (current-buffer)))
6700           (switch-to-buffer gnus-group-buffer)
6701           (when group
6702             (gnus-group-jump-to-group group))
6703           (eval (cadr (assq key keystrokes)))
6704           (setq group (gnus-group-group-name))
6705           (switch-to-buffer obuf))
6706         (setq ended nil))
6707        ((equal key cmd)
6708         (if (or (not group)
6709                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6710             (gnus-summary-exit)
6711           (gnus-summary-next-group nil group backward)))
6712        (t
6713         (push (cdr keve) unread-command-events))))))
6714
6715 (defun gnus-summary-next-unread-article ()
6716   "Select unread article after current one."
6717   (interactive)
6718   (gnus-summary-next-article
6719    (or (not (eq gnus-summary-goto-unread 'never))
6720        (gnus-summary-last-article-p (gnus-summary-article-number)))
6721    (and gnus-auto-select-same
6722         (gnus-summary-article-subject))))
6723
6724 (defun gnus-summary-prev-article (&optional unread subject)
6725   "Select the article after the current one.
6726 If UNREAD is non-nil, only unread articles are selected."
6727   (interactive "P")
6728   (gnus-summary-next-article unread subject t))
6729
6730 (defun gnus-summary-prev-unread-article ()
6731   "Select unread article before current one."
6732   (interactive)
6733   (gnus-summary-prev-article
6734    (or (not (eq gnus-summary-goto-unread 'never))
6735        (gnus-summary-first-article-p (gnus-summary-article-number)))
6736    (and gnus-auto-select-same
6737         (gnus-summary-article-subject))))
6738
6739 (defun gnus-summary-next-page (&optional lines circular)
6740   "Show next page of the selected article.
6741 If at the end of the current article, select the next article.
6742 LINES says how many lines should be scrolled up.
6743
6744 If CIRCULAR is non-nil, go to the start of the article instead of
6745 selecting the next article when reaching the end of the current
6746 article."
6747   (interactive "P")
6748   (setq gnus-summary-buffer (current-buffer))
6749   (gnus-set-global-variables)
6750   (let ((article (gnus-summary-article-number))
6751         (article-window (get-buffer-window gnus-article-buffer t))
6752         endp)
6753     ;; If the buffer is empty, we have no article.
6754     (unless article
6755       (error "No article to select"))
6756     (gnus-configure-windows 'article)
6757     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6758         (if (and (eq gnus-summary-goto-unread 'never)
6759                  (not (gnus-summary-last-article-p article)))
6760             (gnus-summary-next-article)
6761           (gnus-summary-next-unread-article))
6762       (if (or (null gnus-current-article)
6763               (null gnus-article-current)
6764               (/= article (cdr gnus-article-current))
6765               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6766           ;; Selected subject is different from current article's.
6767           (gnus-summary-display-article article)
6768         (when article-window
6769           (gnus-eval-in-buffer-window gnus-article-buffer
6770             (setq endp (gnus-article-next-page lines)))
6771           (when endp
6772             (cond (circular
6773                    (gnus-summary-beginning-of-article))
6774                   (lines
6775                    (gnus-message 3 "End of message"))
6776                   ((null lines)
6777                    (if (and (eq gnus-summary-goto-unread 'never)
6778                             (not (gnus-summary-last-article-p article)))
6779                        (gnus-summary-next-article)
6780                      (gnus-summary-next-unread-article))))))))
6781     (gnus-summary-recenter)
6782     (gnus-summary-position-point)))
6783
6784 (defun gnus-summary-prev-page (&optional lines move)
6785   "Show previous page of selected article.
6786 Argument LINES specifies lines to be scrolled down.
6787 If MOVE, move to the previous unread article if point is at
6788 the beginning of the buffer."
6789   (interactive "P")
6790   (let ((article (gnus-summary-article-number))
6791         (article-window (get-buffer-window gnus-article-buffer t))
6792         endp)
6793     (gnus-configure-windows 'article)
6794     (if (or (null gnus-current-article)
6795             (null gnus-article-current)
6796             (/= article (cdr gnus-article-current))
6797             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6798         ;; Selected subject is different from current article's.
6799         (gnus-summary-display-article article)
6800       (gnus-summary-recenter)
6801       (when article-window
6802         (gnus-eval-in-buffer-window gnus-article-buffer
6803           (setq endp (gnus-article-prev-page lines)))
6804         (when (and move endp)
6805           (cond (lines
6806                  (gnus-message 3 "Beginning of message"))
6807                 ((null lines)
6808                  (if (and (eq gnus-summary-goto-unread 'never)
6809                           (not (gnus-summary-first-article-p article)))
6810                      (gnus-summary-prev-article)
6811                    (gnus-summary-prev-unread-article))))))))
6812   (gnus-summary-position-point))
6813
6814 (defun gnus-summary-prev-page-or-article (&optional lines)
6815   "Show previous page of selected article.
6816 Argument LINES specifies lines to be scrolled down.
6817 If at the beginning of the article, go to the next article."
6818   (interactive "P")
6819   (gnus-summary-prev-page lines t))
6820
6821 (defun gnus-summary-scroll-up (lines)
6822   "Scroll up (or down) one line current article.
6823 Argument LINES specifies lines to be scrolled up (or down if negative)."
6824   (interactive "p")
6825   (gnus-configure-windows 'article)
6826   (gnus-summary-show-thread)
6827   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6828     (gnus-eval-in-buffer-window gnus-article-buffer
6829       (cond ((> lines 0)
6830              (when (gnus-article-next-page lines)
6831                (gnus-message 3 "End of message")))
6832             ((< lines 0)
6833              (gnus-article-prev-page (- lines))))))
6834   (gnus-summary-recenter)
6835   (gnus-summary-position-point))
6836
6837 (defun gnus-summary-scroll-down (lines)
6838   "Scroll down (or up) one line current article.
6839 Argument LINES specifies lines to be scrolled down (or up if negative)."
6840   (interactive "p")
6841   (gnus-summary-scroll-up (- lines)))
6842
6843 (defun gnus-summary-next-same-subject ()
6844   "Select next article which has the same subject as current one."
6845   (interactive)
6846   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6847
6848 (defun gnus-summary-prev-same-subject ()
6849   "Select previous article which has the same subject as current one."
6850   (interactive)
6851   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6852
6853 (defun gnus-summary-next-unread-same-subject ()
6854   "Select next unread article which has the same subject as current one."
6855   (interactive)
6856   (gnus-summary-next-article t (gnus-summary-article-subject)))
6857
6858 (defun gnus-summary-prev-unread-same-subject ()
6859   "Select previous unread article which has the same subject as current one."
6860   (interactive)
6861   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6862
6863 (defun gnus-summary-first-unread-article ()
6864   "Select the first unread article.
6865 Return nil if there are no unread articles."
6866   (interactive)
6867   (prog1
6868       (when (gnus-summary-first-subject t)
6869         (gnus-summary-show-thread)
6870         (gnus-summary-first-subject t)
6871         (gnus-summary-display-article (gnus-summary-article-number)))
6872     (gnus-summary-position-point)))
6873
6874 (defun gnus-summary-first-unread-subject ()
6875   "Place the point on the subject line of the first unread article.
6876 Return nil if there are no unread articles."
6877   (interactive)
6878   (prog1
6879       (when (gnus-summary-first-subject t)
6880         (gnus-summary-show-thread)
6881         (gnus-summary-first-subject t))
6882     (gnus-summary-position-point)))
6883
6884 (defun gnus-summary-first-unseen-subject ()
6885   "Place the point on the subject line of the first unseen article.
6886 Return nil if there are no unseen articles."
6887   (interactive)
6888   (prog1
6889       (when (gnus-summary-first-subject t t t)
6890         (gnus-summary-show-thread)
6891         (gnus-summary-first-subject t t t))
6892     (gnus-summary-position-point)))
6893
6894 (defun gnus-summary-first-unseen-or-unread-subject ()
6895   "Place the point on the subject line of the first unseen article.
6896 Return nil if there are no unseen articles."
6897   (interactive)
6898   (prog1
6899       (unless (when (gnus-summary-first-subject t t t)
6900                 (gnus-summary-show-thread)
6901                 (gnus-summary-first-subject t t t))
6902         (when (gnus-summary-first-subject t)
6903           (gnus-summary-show-thread)
6904           (gnus-summary-first-subject t)))
6905     (gnus-summary-position-point)))
6906
6907 (defun gnus-summary-first-article ()
6908   "Select the first article.
6909 Return nil if there are no articles."
6910   (interactive)
6911   (prog1
6912       (when (gnus-summary-first-subject)
6913         (gnus-summary-show-thread)
6914         (gnus-summary-first-subject)
6915         (gnus-summary-display-article (gnus-summary-article-number)))
6916     (gnus-summary-position-point)))
6917
6918 (defun gnus-summary-best-unread-article (&optional arg)
6919   "Select the unread article with the highest score.
6920 If given a prefix argument, select the next unread article that has a
6921 score higher than the default score."
6922   (interactive "P")
6923   (let ((article (if arg
6924                      (gnus-summary-better-unread-subject)
6925                    (gnus-summary-best-unread-subject))))
6926     (if article
6927         (gnus-summary-goto-article article)
6928       (error "No unread articles"))))
6929
6930 (defun gnus-summary-best-unread-subject ()
6931   "Select the unread subject with the highest score."
6932   (interactive)
6933   (let ((best -1000000)
6934         (data gnus-newsgroup-data)
6935         article score)
6936     (while data
6937       (and (gnus-data-unread-p (car data))
6938            (> (setq score
6939                     (gnus-summary-article-score (gnus-data-number (car data))))
6940               best)
6941            (setq best score
6942                  article (gnus-data-number (car data))))
6943       (setq data (cdr data)))
6944     (when article
6945       (gnus-summary-goto-subject article))
6946     (gnus-summary-position-point)
6947     article))
6948
6949 (defun gnus-summary-better-unread-subject ()
6950   "Select the first unread subject that has a score over the default score."
6951   (interactive)
6952   (let ((data gnus-newsgroup-data)
6953         article score)
6954     (while (and (setq article (gnus-data-number (car data)))
6955                 (or (gnus-data-read-p (car data))
6956                     (not (> (gnus-summary-article-score article)
6957                             gnus-summary-default-score))))
6958       (setq data (cdr data)))
6959     (when article
6960       (gnus-summary-goto-subject article))
6961     (gnus-summary-position-point)
6962     article))
6963
6964 (defun gnus-summary-last-subject ()
6965   "Go to the last displayed subject line in the group."
6966   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6967     (when article
6968       (gnus-summary-goto-subject article))))
6969
6970 (defun gnus-summary-goto-article (article &optional all-headers force)
6971   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6972 If ALL-HEADERS is non-nil, no header lines are hidden.
6973 If FORCE, go to the article even if it isn't displayed.  If FORCE
6974 is a number, it is the line the article is to be displayed on."
6975   (interactive
6976    (list
6977     (completing-read
6978      "Article number or Message-ID: "
6979      (mapcar (lambda (number) (list (int-to-string number)))
6980              gnus-newsgroup-limit))
6981     current-prefix-arg
6982     t))
6983   (prog1
6984       (if (and (stringp article)
6985                (string-match "@" article))
6986           (gnus-summary-refer-article article)
6987         (when (stringp article)
6988           (setq article (string-to-number article)))
6989         (if (gnus-summary-goto-subject article force)
6990             (gnus-summary-display-article article all-headers)
6991           (gnus-message 4 "Couldn't go to article %s" article) nil))
6992     (gnus-summary-position-point)))
6993
6994 (defun gnus-summary-goto-last-article ()
6995   "Go to the previously read article."
6996   (interactive)
6997   (prog1
6998       (when gnus-last-article
6999         (gnus-summary-goto-article gnus-last-article nil t))
7000     (gnus-summary-position-point)))
7001
7002 (defun gnus-summary-pop-article (number)
7003   "Pop one article off the history and go to the previous.
7004 NUMBER articles will be popped off."
7005   (interactive "p")
7006   (let (to)
7007     (setq gnus-newsgroup-history
7008           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7009     (if to
7010         (gnus-summary-goto-article (car to) nil t)
7011       (error "Article history empty")))
7012   (gnus-summary-position-point))
7013
7014 ;; Summary commands and functions for limiting the summary buffer.
7015
7016 (defun gnus-summary-limit-to-articles (n)
7017   "Limit the summary buffer to the next N articles.
7018 If not given a prefix, use the process marked articles instead."
7019   (interactive "P")
7020   (prog1
7021       (let ((articles (gnus-summary-work-articles n)))
7022         (setq gnus-newsgroup-processable nil)
7023         (gnus-summary-limit articles))
7024     (gnus-summary-position-point)))
7025
7026 (defun gnus-summary-pop-limit (&optional total)
7027   "Restore the previous limit.
7028 If given a prefix, remove all limits."
7029   (interactive "P")
7030   (when total
7031     (setq gnus-newsgroup-limits
7032           (list (mapcar (lambda (h) (mail-header-number h))
7033                         gnus-newsgroup-headers))))
7034   (unless gnus-newsgroup-limits
7035     (error "No limit to pop"))
7036   (prog1
7037       (gnus-summary-limit nil 'pop)
7038     (gnus-summary-position-point)))
7039
7040 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7041   "Limit the summary buffer to articles that have subjects that match a regexp.
7042 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7043   (interactive
7044    (list (read-string (if current-prefix-arg
7045                           "Exclude subject (regexp): "
7046                         "Limit to subject (regexp): "))
7047          nil current-prefix-arg))
7048   (unless header
7049     (setq header "subject"))
7050   (when (not (equal "" subject))
7051     (prog1
7052         (let ((articles (gnus-summary-find-matching
7053                          (or header "subject") subject 'all nil nil
7054                          not-matching)))
7055           (unless articles
7056             (error "Found no matches for \"%s\"" subject))
7057           (gnus-summary-limit articles))
7058       (gnus-summary-position-point))))
7059
7060 (defun gnus-summary-limit-to-author (from &optional not-matching)
7061   "Limit the summary buffer to articles that have authors that match a regexp.
7062 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7063   (interactive
7064    (list (read-string (if current-prefix-arg
7065                           "Exclude author (regexp): "
7066                         "Limit to author (regexp): "))
7067          current-prefix-arg))
7068   (gnus-summary-limit-to-subject from "from" not-matching))
7069
7070 (defun gnus-summary-limit-to-age (age &optional younger-p)
7071   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7072 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7073 articles that are younger than AGE days."
7074   (interactive
7075    (let ((younger current-prefix-arg)
7076          (days-got nil)
7077          days)
7078      (while (not days-got)
7079        (setq days (if younger
7080                       (read-string "Limit to articles within (in days): ")
7081                     (read-string "Limit to articles older than (in days): ")))
7082        (when (> (length days) 0)
7083          (setq days (read days)))
7084        (if (numberp days)
7085            (progn 
7086              (setq days-got t)
7087              (if (< days 0)
7088                  (progn 
7089                    (setq younger (not younger))
7090                    (setq days (* days -1)))))
7091          (message "Please enter a number.")
7092          (sleep-for 1)))
7093      (list days younger)))
7094   (prog1
7095       (let ((data gnus-newsgroup-data)
7096             (cutoff (days-to-time age))
7097             articles d date is-younger)
7098         (while (setq d (pop data))
7099           (when (and (vectorp (gnus-data-header d))
7100                      (setq date (mail-header-date (gnus-data-header d))))
7101             (setq is-younger (time-less-p
7102                               (time-since (condition-case ()
7103                                               (date-to-time date)
7104                                             (error '(0 0))))
7105                               cutoff))
7106             (when (if younger-p
7107                       is-younger
7108                     (not is-younger))
7109               (push (gnus-data-number d) articles))))
7110         (gnus-summary-limit (nreverse articles)))
7111     (gnus-summary-position-point)))
7112
7113 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7114   "Limit the summary buffer to articles that match an 'extra' header."
7115   (interactive
7116    (let ((header
7117           (intern
7118            (gnus-completing-read-with-default
7119             (symbol-name (car gnus-extra-headers))
7120             (if current-prefix-arg
7121                 "Exclude extra header:"
7122               "Limit extra header:")
7123             (mapcar (lambda (x)
7124                       (cons (symbol-name x) x))
7125                     gnus-extra-headers)
7126             nil
7127             t))))
7128      (list header
7129            (read-string (format "%s header %s (regexp): "
7130                                 (if current-prefix-arg "Exclude" "Limit to")
7131                                 header))
7132            current-prefix-arg)))
7133   (when (not (equal "" regexp))
7134     (prog1
7135         (let ((articles (gnus-summary-find-matching
7136                          (cons 'extra header) regexp 'all nil nil
7137                          not-matching)))
7138           (unless articles
7139             (error "Found no matches for \"%s\"" regexp))
7140           (gnus-summary-limit articles))
7141       (gnus-summary-position-point))))
7142
7143 (defun gnus-summary-limit-to-display-predicate ()
7144   "Limit the summary buffer to the predicated in the `display' group parameter."
7145   (interactive)
7146   (unless gnus-newsgroup-display
7147     (error "There is no `display' group parameter"))
7148   (let (articles)
7149     (dolist (number gnus-newsgroup-articles)
7150       (when (funcall gnus-newsgroup-display)
7151         (push number articles)))
7152     (gnus-summary-limit articles))
7153   (gnus-summary-position-point))
7154
7155 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7156 (make-obsolete
7157  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7158
7159 (defun gnus-summary-limit-to-unread (&optional all)
7160   "Limit the summary buffer to articles that are not marked as read.
7161 If ALL is non-nil, limit strictly to unread articles."
7162   (interactive "P")
7163   (if all
7164       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7165     (gnus-summary-limit-to-marks
7166      ;; Concat all the marks that say that an article is read and have
7167      ;; those removed.
7168      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7169            gnus-killed-mark gnus-kill-file-mark
7170            gnus-low-score-mark gnus-expirable-mark
7171            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7172            gnus-duplicate-mark gnus-souped-mark)
7173      'reverse)))
7174
7175 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7176 (make-obsolete 'gnus-summary-delete-marked-with
7177                'gnus-summary-limit-exlude-marks)
7178
7179 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7180   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7181 If REVERSE, limit the summary buffer to articles that are marked
7182 with MARKS.  MARKS can either be a string of marks or a list of marks.
7183 Returns how many articles were removed."
7184   (interactive "sMarks: ")
7185   (gnus-summary-limit-to-marks marks t))
7186
7187 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7188   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7189 If REVERSE (the prefix), limit the summary buffer to articles that are
7190 not marked with MARKS.  MARKS can either be a string of marks or a
7191 list of marks.
7192 Returns how many articles were removed."
7193   (interactive "sMarks: \nP")
7194   (prog1
7195       (let ((data gnus-newsgroup-data)
7196             (marks (if (listp marks) marks
7197                      (append marks nil))) ; Transform to list.
7198             articles)
7199         (while data
7200           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7201                   (memq (gnus-data-mark (car data)) marks))
7202             (push (gnus-data-number (car data)) articles))
7203           (setq data (cdr data)))
7204         (gnus-summary-limit articles))
7205     (gnus-summary-position-point)))
7206
7207 (defun gnus-summary-limit-to-score (score)
7208   "Limit to articles with score at or above SCORE."
7209   (interactive "NLimit to articles with score of at least: ")
7210   (let ((data gnus-newsgroup-data)
7211         articles)
7212     (while data
7213       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7214                 score)
7215         (push (gnus-data-number (car data)) articles))
7216       (setq data (cdr data)))
7217     (prog1
7218         (gnus-summary-limit articles)
7219       (gnus-summary-position-point))))
7220
7221 (defun gnus-summary-limit-include-thread (id)
7222   "Display all the hidden articles that is in the thread with ID in it.
7223 When called interactively, ID is the Message-ID of the current
7224 article."
7225   (interactive (list (mail-header-id (gnus-summary-article-header))))
7226   (let ((articles (gnus-articles-in-thread
7227                    (gnus-id-to-thread (gnus-root-id id)))))
7228     (prog1
7229         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7230       (gnus-summary-limit-include-matching-articles
7231        "subject"
7232        (regexp-quote (gnus-simplify-subject-re
7233                       (mail-header-subject (gnus-id-to-header id)))))
7234       (gnus-summary-position-point))))
7235
7236 (defun gnus-summary-limit-include-matching-articles (header regexp)
7237   "Display all the hidden articles that have HEADERs that match REGEXP."
7238   (interactive (list (read-string "Match on header: ")
7239                      (read-string "Regexp: ")))
7240   (let ((articles (gnus-find-matching-articles header regexp)))
7241     (prog1
7242         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7243       (gnus-summary-position-point))))
7244
7245 (defun gnus-summary-limit-include-dormant ()
7246   "Display all the hidden articles that are marked as dormant.
7247 Note that this command only works on a subset of the articles currently
7248 fetched for this group."
7249   (interactive)
7250   (unless gnus-newsgroup-dormant
7251     (error "There are no dormant articles in this group"))
7252   (prog1
7253       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7254     (gnus-summary-position-point)))
7255
7256 (defun gnus-summary-limit-exclude-dormant ()
7257   "Hide all dormant articles."
7258   (interactive)
7259   (prog1
7260       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7261     (gnus-summary-position-point)))
7262
7263 (defun gnus-summary-limit-exclude-childless-dormant ()
7264   "Hide all dormant articles that have no children."
7265   (interactive)
7266   (let ((data (gnus-data-list t))
7267         articles d children)
7268     ;; Find all articles that are either not dormant or have
7269     ;; children.
7270     (while (setq d (pop data))
7271       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7272                 (and (setq children
7273                            (gnus-article-children (gnus-data-number d)))
7274                      (let (found)
7275                        (while children
7276                          (when (memq (car children) articles)
7277                            (setq children nil
7278                                  found t))
7279                          (pop children))
7280                        found)))
7281         (push (gnus-data-number d) articles)))
7282     ;; Do the limiting.
7283     (prog1
7284         (gnus-summary-limit articles)
7285       (gnus-summary-position-point))))
7286
7287 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7288   "Mark all unread excluded articles as read.
7289 If ALL, mark even excluded ticked and dormants as read."
7290   (interactive "P")
7291   (let ((articles (gnus-sorted-complement
7292                    (sort
7293                     (mapcar (lambda (h) (mail-header-number h))
7294                             gnus-newsgroup-headers)
7295                     '<)
7296                    (sort gnus-newsgroup-limit '<)))
7297         article)
7298     (setq gnus-newsgroup-unreads
7299           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
7300     (if all
7301         (setq gnus-newsgroup-dormant nil
7302               gnus-newsgroup-marked nil
7303               gnus-newsgroup-reads
7304               (nconc
7305                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7306                gnus-newsgroup-reads))
7307       (while (setq article (pop articles))
7308         (unless (or (memq article gnus-newsgroup-dormant)
7309                     (memq article gnus-newsgroup-marked))
7310           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7311
7312 (defun gnus-summary-limit (articles &optional pop)
7313   (if pop
7314       ;; We pop the previous limit off the stack and use that.
7315       (setq articles (car gnus-newsgroup-limits)
7316             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7317     ;; We use the new limit, so we push the old limit on the stack.
7318     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7319   ;; Set the limit.
7320   (setq gnus-newsgroup-limit articles)
7321   (let ((total (length gnus-newsgroup-data))
7322         (data (gnus-data-find-list (gnus-summary-article-number)))
7323         (gnus-summary-mark-below nil)   ; Inhibit this.
7324         found)
7325     ;; This will do all the work of generating the new summary buffer
7326     ;; according to the new limit.
7327     (gnus-summary-prepare)
7328     ;; Hide any threads, possibly.
7329     (gnus-summary-maybe-hide-threads)
7330     ;; Try to return to the article you were at, or one in the
7331     ;; neighborhood.
7332     (when data
7333       ;; We try to find some article after the current one.
7334       (while data
7335         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7336           (setq data nil
7337                 found t))
7338         (setq data (cdr data))))
7339     (unless found
7340       ;; If there is no data, that means that we were after the last
7341       ;; article.  The same goes when we can't find any articles
7342       ;; after the current one.
7343       (goto-char (point-max))
7344       (gnus-summary-find-prev))
7345     (gnus-set-mode-line 'summary)
7346     ;; We return how many articles were removed from the summary
7347     ;; buffer as a result of the new limit.
7348     (- total (length gnus-newsgroup-data))))
7349
7350 (defsubst gnus-invisible-cut-children (threads)
7351   (let ((num 0))
7352     (while threads
7353       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7354         (incf num))
7355       (pop threads))
7356     (< num 2)))
7357
7358 (defsubst gnus-cut-thread (thread)
7359   "Go forwards in the thread until we find an article that we want to display."
7360   (when (or (eq gnus-fetch-old-headers 'some)
7361             (eq gnus-fetch-old-headers 'invisible)
7362             (numberp gnus-fetch-old-headers)
7363             (eq gnus-build-sparse-threads 'some)
7364             (eq gnus-build-sparse-threads 'more))
7365     ;; Deal with old-fetched headers and sparse threads.
7366     (while (and
7367             thread
7368             (or
7369              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7370              (gnus-summary-article-ancient-p
7371               (mail-header-number (car thread))))
7372             (if (or (<= (length (cdr thread)) 1)
7373                     (eq gnus-fetch-old-headers 'invisible))
7374                 (setq gnus-newsgroup-limit
7375                       (delq (mail-header-number (car thread))
7376                             gnus-newsgroup-limit)
7377                       thread (cadr thread))
7378               (when (gnus-invisible-cut-children (cdr thread))
7379                 (let ((th (cdr thread)))
7380                   (while th
7381                     (if (memq (mail-header-number (caar th))
7382                               gnus-newsgroup-limit)
7383                         (setq thread (car th)
7384                               th nil)
7385                       (setq th (cdr th))))))))))
7386   thread)
7387
7388 (defun gnus-cut-threads (threads)
7389   "Cut off all uninteresting articles from the beginning of threads."
7390   (when (or (eq gnus-fetch-old-headers 'some)
7391             (eq gnus-fetch-old-headers 'invisible)
7392             (numberp gnus-fetch-old-headers)
7393             (eq gnus-build-sparse-threads 'some)
7394             (eq gnus-build-sparse-threads 'more))
7395     (let ((th threads))
7396       (while th
7397         (setcar th (gnus-cut-thread (car th)))
7398         (setq th (cdr th)))))
7399   ;; Remove nixed out threads.
7400   (delq nil threads))
7401
7402 (defun gnus-summary-initial-limit (&optional show-if-empty)
7403   "Figure out what the initial limit is supposed to be on group entry.
7404 This entails weeding out unwanted dormants, low-scored articles,
7405 fetch-old-headers verbiage, and so on."
7406   ;; Most groups have nothing to remove.
7407   (if (or gnus-inhibit-limiting
7408           (and (null gnus-newsgroup-dormant)
7409                (eq gnus-newsgroup-display 'gnus-not-ignore)
7410                (not (eq gnus-fetch-old-headers 'some))
7411                (not (numberp gnus-fetch-old-headers))
7412                (not (eq gnus-fetch-old-headers 'invisible))
7413                (null gnus-summary-expunge-below)
7414                (not (eq gnus-build-sparse-threads 'some))
7415                (not (eq gnus-build-sparse-threads 'more))
7416                (null gnus-thread-expunge-below)
7417                (not gnus-use-nocem)))
7418       ()                                ; Do nothing.
7419     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7420     (setq gnus-newsgroup-limit nil)
7421     (mapatoms
7422      (lambda (node)
7423        (unless (car (symbol-value node))
7424          ;; These threads have no parents -- they are roots.
7425          (let ((nodes (cdr (symbol-value node)))
7426                thread)
7427            (while nodes
7428              (if (and gnus-thread-expunge-below
7429                       (< (gnus-thread-total-score (car nodes))
7430                          gnus-thread-expunge-below))
7431                  (gnus-expunge-thread (pop nodes))
7432                (setq thread (pop nodes))
7433                (gnus-summary-limit-children thread))))))
7434      gnus-newsgroup-dependencies)
7435     ;; If this limitation resulted in an empty group, we might
7436     ;; pop the previous limit and use it instead.
7437     (when (and (not gnus-newsgroup-limit)
7438                show-if-empty)
7439       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7440     gnus-newsgroup-limit))
7441
7442 (defun gnus-summary-limit-children (thread)
7443   "Return 1 if this subthread is visible and 0 if it is not."
7444   ;; First we get the number of visible children to this thread.  This
7445   ;; is done by recursing down the thread using this function, so this
7446   ;; will really go down to a leaf article first, before slowly
7447   ;; working its way up towards the root.
7448   (when thread
7449     (let ((children
7450            (if (cdr thread)
7451                (apply '+ (mapcar 'gnus-summary-limit-children
7452                                  (cdr thread)))
7453              0))
7454           (number (mail-header-number (car thread)))
7455           score)
7456       (if (and
7457            (not (memq number gnus-newsgroup-marked))
7458            (or
7459             ;; If this article is dormant and has absolutely no visible
7460             ;; children, then this article isn't visible.
7461             (and (memq number gnus-newsgroup-dormant)
7462                  (zerop children))
7463             ;; If this is "fetch-old-headered" and there is no
7464             ;; visible children, then we don't want this article.
7465             (and (or (eq gnus-fetch-old-headers 'some)
7466                      (numberp gnus-fetch-old-headers))
7467                  (gnus-summary-article-ancient-p number)
7468                  (zerop children))
7469             ;; If this is "fetch-old-headered" and `invisible', then
7470             ;; we don't want this article.
7471             (and (eq gnus-fetch-old-headers 'invisible)
7472                  (gnus-summary-article-ancient-p number))
7473             ;; If this is a sparsely inserted article with no children,
7474             ;; we don't want it.
7475             (and (eq gnus-build-sparse-threads 'some)
7476                  (gnus-summary-article-sparse-p number)
7477                  (zerop children))
7478             ;; If we use expunging, and this article is really
7479             ;; low-scored, then we don't want this article.
7480             (when (and gnus-summary-expunge-below
7481                        (< (setq score
7482                                 (or (cdr (assq number gnus-newsgroup-scored))
7483                                     gnus-summary-default-score))
7484                           gnus-summary-expunge-below))
7485               ;; We increase the expunge-tally here, but that has
7486               ;; nothing to do with the limits, really.
7487               (incf gnus-newsgroup-expunged-tally)
7488               ;; We also mark as read here, if that's wanted.
7489               (when (and gnus-summary-mark-below
7490                          (< score gnus-summary-mark-below))
7491                 (setq gnus-newsgroup-unreads
7492                       (delq number gnus-newsgroup-unreads))
7493                 (if gnus-newsgroup-auto-expire
7494                     (push number gnus-newsgroup-expirable)
7495                   (push (cons number gnus-low-score-mark)
7496                         gnus-newsgroup-reads)))
7497               t)
7498             ;; Do the `display' group parameter.
7499             (and gnus-newsgroup-display
7500                  (not (funcall gnus-newsgroup-display)))
7501             ;; Check NoCeM things.
7502             (if (and gnus-use-nocem
7503                      (gnus-nocem-unwanted-article-p
7504                       (mail-header-id (car thread))))
7505                 (progn
7506                   (setq gnus-newsgroup-unreads
7507                         (delq number gnus-newsgroup-unreads))
7508                   t))))
7509           ;; Nope, invisible article.
7510           0
7511         ;; Ok, this article is to be visible, so we add it to the limit
7512         ;; and return 1.
7513         (push number gnus-newsgroup-limit)
7514         1))))
7515
7516 (defun gnus-expunge-thread (thread)
7517   "Mark all articles in THREAD as read."
7518   (let* ((number (mail-header-number (car thread))))
7519     (incf gnus-newsgroup-expunged-tally)
7520     ;; We also mark as read here, if that's wanted.
7521     (setq gnus-newsgroup-unreads
7522           (delq number gnus-newsgroup-unreads))
7523     (if gnus-newsgroup-auto-expire
7524         (push number gnus-newsgroup-expirable)
7525       (push (cons number gnus-low-score-mark)
7526             gnus-newsgroup-reads)))
7527   ;; Go recursively through all subthreads.
7528   (mapcar 'gnus-expunge-thread (cdr thread)))
7529
7530 ;; Summary article oriented commands
7531
7532 (defun gnus-summary-refer-parent-article (n)
7533   "Refer parent article N times.
7534 If N is negative, go to ancestor -N instead.
7535 The difference between N and the number of articles fetched is returned."
7536   (interactive "p")
7537   (let ((skip 1)
7538         error header ref)
7539     (when (not (natnump n))
7540       (setq skip (abs n)
7541             n 1))
7542     (while (and (> n 0)
7543                 (not error))
7544       (setq header (gnus-summary-article-header))
7545       (if (and (eq (mail-header-number header)
7546                    (cdr gnus-article-current))
7547                (equal gnus-newsgroup-name
7548                       (car gnus-article-current)))
7549           ;; If we try to find the parent of the currently
7550           ;; displayed article, then we take a look at the actual
7551           ;; References header, since this is slightly more
7552           ;; reliable than the References field we got from the
7553           ;; server.
7554           (save-excursion
7555             (set-buffer gnus-original-article-buffer)
7556             (nnheader-narrow-to-headers)
7557             (unless (setq ref (message-fetch-field "references"))
7558               (setq ref (message-fetch-field "in-reply-to")))
7559             (widen))
7560         (setq ref
7561               ;; It's not the current article, so we take a bet on
7562               ;; the value we got from the server.
7563               (mail-header-references header)))
7564       (if (and ref
7565                (not (equal ref "")))
7566           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7567             (gnus-message 1 "Couldn't find parent"))
7568         (gnus-message 1 "No references in article %d"
7569                       (gnus-summary-article-number))
7570         (setq error t))
7571       (decf n))
7572     (gnus-summary-position-point)
7573     n))
7574
7575 (defun gnus-summary-refer-references ()
7576   "Fetch all articles mentioned in the References header.
7577 Return the number of articles fetched."
7578   (interactive)
7579   (let ((ref (mail-header-references (gnus-summary-article-header)))
7580         (current (gnus-summary-article-number))
7581         (n 0))
7582     (if (or (not ref)
7583             (equal ref ""))
7584         (error "No References in the current article")
7585       ;; For each Message-ID in the References header...
7586       (while (string-match "<[^>]*>" ref)
7587         (incf n)
7588         ;; ... fetch that article.
7589         (gnus-summary-refer-article
7590          (prog1 (match-string 0 ref)
7591            (setq ref (substring ref (match-end 0))))))
7592       (gnus-summary-goto-subject current)
7593       (gnus-summary-position-point)
7594       n)))
7595
7596 (defun gnus-summary-refer-thread (&optional limit)
7597   "Fetch all articles in the current thread.
7598 If LIMIT (the numerical prefix), fetch that many old headers instead
7599 of what's specified by the `gnus-refer-thread-limit' variable."
7600   (interactive "P")
7601   (let ((id (mail-header-id (gnus-summary-article-header)))
7602         (limit (if limit (prefix-numeric-value limit)
7603                  gnus-refer-thread-limit)))
7604     ;; We want to fetch LIMIT *old* headers, but we also have to
7605     ;; re-fetch all the headers in the current buffer, because many of
7606     ;; them may be undisplayed.  So we adjust LIMIT.
7607     (when (numberp limit)
7608       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7609     (unless (eq gnus-fetch-old-headers 'invisible)
7610       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7611       ;; Retrieve the headers and read them in.
7612       (if (eq (gnus-retrieve-headers
7613                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7614               'nov)
7615           (gnus-build-all-threads)
7616         (error "Can't fetch thread from backends that don't support NOV"))
7617       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7618     (gnus-summary-limit-include-thread id)))
7619
7620 (defun gnus-summary-refer-article (message-id)
7621   "Fetch an article specified by MESSAGE-ID."
7622   (interactive "sMessage-ID: ")
7623   (when (and (stringp message-id)
7624              (not (zerop (length message-id))))
7625     ;; Construct the correct Message-ID if necessary.
7626     ;; Suggested by tale@pawl.rpi.edu.
7627     (unless (string-match "^<" message-id)
7628       (setq message-id (concat "<" message-id)))
7629     (unless (string-match ">$" message-id)
7630       (setq message-id (concat message-id ">")))
7631     (let* ((header (gnus-id-to-header message-id))
7632            (sparse (and header
7633                         (gnus-summary-article-sparse-p
7634                          (mail-header-number header))
7635                         (memq (mail-header-number header)
7636                               gnus-newsgroup-limit)))
7637            number)
7638       (cond
7639        ;; If the article is present in the buffer we just go to it.
7640        ((and header
7641              (or (not (gnus-summary-article-sparse-p
7642                        (mail-header-number header)))
7643                  sparse))
7644         (prog1
7645             (gnus-summary-goto-article
7646              (mail-header-number header) nil t)
7647           (when sparse
7648             (gnus-summary-update-article (mail-header-number header)))))
7649        (t
7650         ;; We fetch the article.
7651         (catch 'found
7652           (dolist (gnus-override-method (gnus-refer-article-methods))
7653             (gnus-check-server gnus-override-method)
7654             ;; Fetch the header, and display the article.
7655             (when (setq number (gnus-summary-insert-subject message-id))
7656               (gnus-summary-select-article nil nil nil number)
7657               (throw 'found t)))
7658           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7659
7660 (defun gnus-refer-article-methods ()
7661   "Return a list of referrable methods."
7662   (cond
7663    ;; No method, so we default to current and native.
7664    ((null gnus-refer-article-method)
7665     (list gnus-current-select-method gnus-select-method))
7666    ;; Current.
7667    ((eq 'current gnus-refer-article-method)
7668     (list gnus-current-select-method))
7669    ;; List of select methods.
7670    ((not (and (symbolp (car gnus-refer-article-method))
7671               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7672     (let (out)
7673       (dolist (method gnus-refer-article-method)
7674         (push (if (eq 'current method)
7675                   gnus-current-select-method
7676                 method)
7677               out))
7678       (nreverse out)))
7679    ;; One single select method.
7680    (t
7681     (list gnus-refer-article-method))))
7682
7683 (defun gnus-summary-edit-parameters ()
7684   "Edit the group parameters of the current group."
7685   (interactive)
7686   (gnus-group-edit-group gnus-newsgroup-name 'params))
7687
7688 (defun gnus-summary-customize-parameters ()
7689   "Customize the group parameters of the current group."
7690   (interactive)
7691   (gnus-group-customize gnus-newsgroup-name))
7692
7693 (defun gnus-summary-enter-digest-group (&optional force)
7694   "Enter an nndoc group based on the current article.
7695 If FORCE, force a digest interpretation.  If not, try
7696 to guess what the document format is."
7697   (interactive "P")
7698   (let ((conf gnus-current-window-configuration))
7699     (save-excursion
7700       (gnus-summary-select-article))
7701     (setq gnus-current-window-configuration conf)
7702     (let* ((name (format "%s-%d"
7703                          (gnus-group-prefixed-name
7704                           gnus-newsgroup-name (list 'nndoc ""))
7705                          (save-excursion
7706                            (set-buffer gnus-summary-buffer)
7707                            gnus-current-article)))
7708            (ogroup gnus-newsgroup-name)
7709            (params (append (gnus-info-params (gnus-get-info ogroup))
7710                            (list (cons 'to-group ogroup))
7711                            (list (cons 'save-article-group ogroup))))
7712            (case-fold-search t)
7713            (buf (current-buffer))
7714            dig to-address)
7715       (save-excursion
7716         (set-buffer gnus-original-article-buffer)
7717         ;; Have the digest group inherit the main mail address of
7718         ;; the parent article.
7719         (when (setq to-address (or (message-fetch-field "reply-to")
7720                                    (message-fetch-field "from")))
7721           (setq params (append
7722                         (list (cons 'to-address
7723                                     (funcall gnus-decode-encoded-word-function
7724                                              to-address))))))
7725         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7726         (insert-buffer-substring gnus-original-article-buffer)
7727         ;; Remove lines that may lead nndoc to misinterpret the
7728         ;; document type.
7729         (narrow-to-region
7730          (goto-char (point-min))
7731          (or (search-forward "\n\n" nil t) (point)))
7732         (goto-char (point-min))
7733         (delete-matching-lines "^Path:\\|^From ")
7734         (widen))
7735       (unwind-protect
7736           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7737                     (gnus-newsgroup-ephemeral-ignored-charsets
7738                      gnus-newsgroup-ignored-charsets))
7739                 (gnus-group-read-ephemeral-group
7740                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7741                               (nndoc-article-type
7742                                ,(if force 'mbox 'guess)))
7743                  t nil nil nil
7744                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
7745                                                         "ADAPT")))))
7746               ;; Make all postings to this group go to the parent group.
7747               (nconc (gnus-info-params (gnus-get-info name))
7748                      params)
7749             ;; Couldn't select this doc group.
7750             (switch-to-buffer buf)
7751             (gnus-set-global-variables)
7752             (gnus-configure-windows 'summary)
7753             (gnus-message 3 "Article couldn't be entered?"))
7754         (kill-buffer dig)))))
7755
7756 (defun gnus-summary-read-document (n)
7757   "Open a new group based on the current article(s).
7758 This will allow you to read digests and other similar
7759 documents as newsgroups.
7760 Obeys the standard process/prefix convention."
7761   (interactive "P")
7762   (let* ((articles (gnus-summary-work-articles n))
7763          (ogroup gnus-newsgroup-name)
7764          (params (append (gnus-info-params (gnus-get-info ogroup))
7765                          (list (cons 'to-group ogroup))))
7766          article group egroup groups vgroup)
7767     (while (setq article (pop articles))
7768       (setq group (format "%s-%d" gnus-newsgroup-name article))
7769       (gnus-summary-remove-process-mark article)
7770       (when (gnus-summary-display-article article)
7771         (save-excursion
7772           (with-temp-buffer
7773             (insert-buffer-substring gnus-original-article-buffer)
7774             ;; Remove some headers that may lead nndoc to make
7775             ;; the wrong guess.
7776             (message-narrow-to-head)
7777             (goto-char (point-min))
7778             (delete-matching-lines "^\\(Path\\):\\|^From ")
7779             (widen)
7780             (if (setq egroup
7781                       (gnus-group-read-ephemeral-group
7782                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7783                                      (nndoc-article-type guess))
7784                        t nil t))
7785                 (progn
7786             ;; Make all postings to this group go to the parent group.
7787                   (nconc (gnus-info-params (gnus-get-info egroup))
7788                          params)
7789                   (push egroup groups))
7790               ;; Couldn't select this doc group.
7791               (gnus-error 3 "Article couldn't be entered"))))))
7792     ;; Now we have selected all the documents.
7793     (cond
7794      ((not groups)
7795       (error "None of the articles could be interpreted as documents"))
7796      ((gnus-group-read-ephemeral-group
7797        (setq vgroup (format
7798                      "nnvirtual:%s-%s" gnus-newsgroup-name
7799                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7800        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7801        t
7802        (cons (current-buffer) 'summary)))
7803      (t
7804       (error "Couldn't select virtual nndoc group")))))
7805
7806 (defun gnus-summary-isearch-article (&optional regexp-p)
7807   "Do incremental search forward on the current article.
7808 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7809   (interactive "P")
7810   (gnus-summary-select-article)
7811   (gnus-configure-windows 'article)
7812   (gnus-eval-in-buffer-window gnus-article-buffer
7813     (save-restriction
7814       (widen)
7815       (isearch-forward regexp-p))))
7816
7817 (defun gnus-summary-search-article-forward (regexp &optional backward)
7818   "Search for an article containing REGEXP forward.
7819 If BACKWARD, search backward instead."
7820   (interactive
7821    (list (read-string
7822           (format "Search article %s (regexp%s): "
7823                   (if current-prefix-arg "backward" "forward")
7824                   (if gnus-last-search-regexp
7825                       (concat ", default " gnus-last-search-regexp)
7826                     "")))
7827          current-prefix-arg))
7828   (if (string-equal regexp "")
7829       (setq regexp (or gnus-last-search-regexp ""))
7830     (setq gnus-last-search-regexp regexp)
7831     (setq gnus-article-before-search gnus-current-article))
7832   ;; Intentionally set gnus-last-article.
7833   (setq gnus-last-article gnus-article-before-search)
7834   (let ((gnus-last-article gnus-last-article))
7835     (if (gnus-summary-search-article regexp backward)
7836         (gnus-summary-show-thread)
7837       (error "Search failed: \"%s\"" regexp))))
7838
7839 (defun gnus-summary-search-article-backward (regexp)
7840   "Search for an article containing REGEXP backward."
7841   (interactive
7842    (list (read-string
7843           (format "Search article backward (regexp%s): "
7844                   (if gnus-last-search-regexp
7845                       (concat ", default " gnus-last-search-regexp)
7846                     "")))))
7847   (gnus-summary-search-article-forward regexp 'backward))
7848
7849 (defun gnus-summary-search-article (regexp &optional backward)
7850   "Search for an article containing REGEXP.
7851 Optional argument BACKWARD means do search for backward.
7852 `gnus-select-article-hook' is not called during the search."
7853   ;; We have to require this here to make sure that the following
7854   ;; dynamic binding isn't shadowed by autoloading.
7855   (require 'gnus-async)
7856   (require 'gnus-art)
7857   (let ((gnus-select-article-hook nil)  ;Disable hook.
7858         (gnus-article-prepare-hook nil)
7859         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7860         (gnus-use-article-prefetch nil)
7861         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7862         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7863         (sum (current-buffer))
7864         (gnus-display-mime-function nil)
7865         (found nil)
7866         point)
7867     (gnus-save-hidden-threads
7868       (gnus-summary-select-article)
7869       (set-buffer gnus-article-buffer)
7870       (goto-char (window-point (get-buffer-window (current-buffer))))
7871       (when backward
7872         (forward-line -1))
7873       (while (not found)
7874         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7875         (if (if backward
7876                 (re-search-backward regexp nil t)
7877               (re-search-forward regexp nil t))
7878             ;; We found the regexp.
7879             (progn
7880               (setq found 'found)
7881               (beginning-of-line)
7882               (set-window-start
7883                (get-buffer-window (current-buffer))
7884                (point))
7885               (forward-line 1)
7886               (set-window-point
7887                (get-buffer-window (current-buffer))
7888                (point))
7889               (set-buffer sum)
7890               (setq point (point)))
7891           ;; We didn't find it, so we go to the next article.
7892           (set-buffer sum)
7893           (setq found 'not)
7894           (while (eq found 'not)
7895             (if (not (if backward (gnus-summary-find-prev)
7896                        (gnus-summary-find-next)))
7897                 ;; No more articles.
7898                 (setq found t)
7899               ;; Select the next article and adjust point.
7900               (unless (gnus-summary-article-sparse-p
7901                        (gnus-summary-article-number))
7902                 (setq found nil)
7903                 (gnus-summary-select-article)
7904                 (set-buffer gnus-article-buffer)
7905                 (widen)
7906                 (goto-char (if backward (point-max) (point-min))))))))
7907       (gnus-message 7 ""))
7908     ;; Return whether we found the regexp.
7909     (when (eq found 'found)
7910       (goto-char point)
7911       (gnus-summary-show-thread)
7912       (gnus-summary-goto-subject gnus-current-article)
7913       (gnus-summary-position-point)
7914       t)))
7915
7916 (defun gnus-find-matching-articles (header regexp)
7917   "Return a list of all articles that match REGEXP on HEADER.
7918 This search includes all articles in the current group that Gnus has
7919 fetched headers for, whether they are displayed or not."
7920   (let ((articles nil)
7921         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7922         (case-fold-search t))
7923     (dolist (header gnus-newsgroup-headers)
7924       (when (string-match regexp (funcall func header))
7925         (push (mail-header-number header) articles)))
7926     (nreverse articles)))
7927
7928 (defun gnus-summary-find-matching (header regexp &optional backward unread
7929                                           not-case-fold not-matching)
7930   "Return a list of all articles that match REGEXP on HEADER.
7931 The search stars on the current article and goes forwards unless
7932 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7933 If UNREAD is non-nil, only unread articles will
7934 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7935 in the comparisons. If NOT-MATCHING, return a list of all articles that
7936 not match REGEXP on HEADER."
7937   (let ((case-fold-search (not not-case-fold))
7938         articles d func)
7939     (if (consp header)
7940         (if (eq (car header) 'extra)
7941             (setq func
7942                   `(lambda (h)
7943                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7944                          "")))
7945           (error "%s is an invalid header" header))
7946       (unless (fboundp (intern (concat "mail-header-" header)))
7947         (error "%s is not a valid header" header))
7948       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7949     (dolist (d (if (eq backward 'all)
7950                    gnus-newsgroup-data
7951                  (gnus-data-find-list
7952                   (gnus-summary-article-number)
7953                   (gnus-data-list backward))))
7954       (when (and (or (not unread)       ; We want all articles...
7955                      (gnus-data-unread-p d)) ; Or just unreads.
7956                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7957                  (if not-matching
7958                      (not (string-match
7959                            regexp
7960                            (funcall func (gnus-data-header d))))
7961                    (string-match regexp
7962                                  (funcall func (gnus-data-header d)))))
7963         (push (gnus-data-number d) articles))) ; Success!
7964     (nreverse articles)))
7965
7966 (defun gnus-summary-execute-command (header regexp command &optional backward)
7967   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7968 If HEADER is an empty string (or nil), the match is done on the entire
7969 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7970   (interactive
7971    (list (let ((completion-ignore-case t))
7972            (completing-read
7973             "Header name: "
7974             (mapcar (lambda (header) (list (format "%s" header)))
7975                     (append
7976                      '("Number" "Subject" "From" "Lines" "Date"
7977                        "Message-ID" "Xref" "References" "Body")
7978                      gnus-extra-headers))
7979             nil 'require-match))
7980          (read-string "Regexp: ")
7981          (read-key-sequence "Command: ")
7982          current-prefix-arg))
7983   (when (equal header "Body")
7984     (setq header ""))
7985   ;; Hidden thread subtrees must be searched as well.
7986   (gnus-summary-show-all-threads)
7987   ;; We don't want to change current point nor window configuration.
7988   (save-excursion
7989     (save-window-excursion
7990       (gnus-message 6 "Executing %s..." (key-description command))
7991 ;; We'd like to execute COMMAND interactively so as to give arguments.
7992       (gnus-execute header regexp
7993                     `(call-interactively ',(key-binding command))
7994                     backward)
7995       (gnus-message 6 "Executing %s...done" (key-description command)))))
7996
7997 (defun gnus-summary-beginning-of-article ()
7998   "Scroll the article back to the beginning."
7999   (interactive)
8000   (gnus-summary-select-article)
8001   (gnus-configure-windows 'article)
8002   (gnus-eval-in-buffer-window gnus-article-buffer
8003     (widen)
8004     (goto-char (point-min))
8005     (when gnus-page-broken
8006       (gnus-narrow-to-page))))
8007
8008 (defun gnus-summary-end-of-article ()
8009   "Scroll to the end of the article."
8010   (interactive)
8011   (gnus-summary-select-article)
8012   (gnus-configure-windows 'article)
8013   (gnus-eval-in-buffer-window gnus-article-buffer
8014     (widen)
8015     (goto-char (point-max))
8016     (recenter -3)
8017     (when gnus-page-broken
8018       (gnus-narrow-to-page))))
8019
8020 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8021   "Truncate to LEN and quote all \"(\"'s in STRING."
8022   (gnus-replace-in-string (if (and len (> (length string) len))
8023                               (substring string 0 len)
8024                             string)
8025                           "[()]" "\\\\\\&"))
8026
8027 (defun gnus-summary-print-article (&optional filename n)
8028   "Generate and print a PostScript image of the N next (mail) articles.
8029
8030 If N is negative, print the N previous articles.  If N is nil and articles
8031 have been marked with the process mark, print these instead.
8032
8033 If the optional first argument FILENAME is nil, send the image to the
8034 printer.  If FILENAME is a string, save the PostScript image in a file with
8035 that name.  If FILENAME is a number, prompt the user for the name of the file
8036 to save in."
8037   (interactive (list (ps-print-preprint current-prefix-arg)))
8038   (dolist (article (gnus-summary-work-articles n))
8039     (gnus-summary-select-article nil nil 'pseudo article)
8040     (gnus-eval-in-buffer-window gnus-article-buffer
8041       (gnus-print-buffer))
8042     (gnus-summary-remove-process-mark article))
8043   (ps-despool filename))
8044
8045 (defun gnus-print-buffer ()
8046   (let ((buffer (generate-new-buffer " *print*")))
8047     (unwind-protect
8048         (progn
8049           (copy-to-buffer buffer (point-min) (point-max))
8050           (set-buffer buffer)
8051           (gnus-article-delete-invisible-text)
8052           (when (gnus-visual-p 'article-highlight 'highlight)
8053             ;; Copy-to-buffer doesn't copy overlay.  So redo
8054             ;; highlight.
8055             (let ((gnus-article-buffer buffer))
8056               (gnus-article-highlight-citation t)
8057               (gnus-article-highlight-signature)))
8058           (let ((ps-left-header
8059                  (list
8060                   (concat "("
8061                           (gnus-summary-print-truncate-and-quote
8062                            (mail-header-subject gnus-current-headers)
8063                            66) ")")
8064                   (concat "("
8065                           (gnus-summary-print-truncate-and-quote
8066                            (mail-header-from gnus-current-headers)
8067                            45) ")")))
8068                 (ps-right-header
8069                  (list
8070                   "/pagenumberstring load"
8071                   (concat "("
8072                           (mail-header-date gnus-current-headers) ")"))))
8073             (gnus-run-hooks 'gnus-ps-print-hook)
8074             (save-excursion
8075               (if window-system
8076                   (ps-spool-buffer-with-faces)
8077                 (ps-spool-buffer)))))
8078       (kill-buffer buffer))))
8079
8080 (defun gnus-summary-show-article (&optional arg)
8081   "Force redisplaying of the current article.
8082 If ARG (the prefix) is a number, show the article with the charset
8083 defined in `gnus-summary-show-article-charset-alist', or the charset
8084 input.
8085 If ARG (the prefix) is non-nil and not a number, show the raw article
8086 without any article massaging functions being run.  Normally, the key strokes 
8087 are `C-u g'."
8088   (interactive "P")
8089   (cond
8090    ((numberp arg)
8091     (gnus-summary-show-article t)
8092     (let ((gnus-newsgroup-charset
8093            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8094                (mm-read-coding-system
8095                 "View as charset: " ;; actually it is coding system.
8096                 (save-excursion
8097                   (set-buffer gnus-article-buffer)
8098                   (mm-detect-coding-region (point) (point-max))))))
8099           (gnus-newsgroup-ignored-charsets 'gnus-all))
8100       (gnus-summary-select-article nil 'force)
8101       (let ((deps gnus-newsgroup-dependencies)
8102             head header lines)
8103         (save-excursion
8104           (set-buffer gnus-original-article-buffer)
8105           (save-restriction
8106             (message-narrow-to-head)
8107             (setq head (buffer-string))
8108             (goto-char (point-min))
8109             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8110               (goto-char (point-max))
8111               (widen)
8112               (setq lines (1- (count-lines (point) (point-max))))))
8113           (with-temp-buffer
8114             (insert (format "211 %d Article retrieved.\n"
8115                             (cdr gnus-article-current)))
8116             (insert head)
8117             (if lines (insert (format "Lines: %d\n" lines)))
8118             (insert ".\n")
8119             (let ((nntp-server-buffer (current-buffer)))
8120               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8121         (gnus-data-set-header
8122          (gnus-data-find (cdr gnus-article-current))
8123          header)
8124         (gnus-summary-update-article-line
8125          (cdr gnus-article-current) header)
8126         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8127           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8128    ((not arg)
8129     ;; Select the article the normal way.
8130     (gnus-summary-select-article nil 'force))
8131    (t
8132     ;; We have to require this here to make sure that the following
8133     ;; dynamic binding isn't shadowed by autoloading.
8134     (require 'gnus-async)
8135     (require 'gnus-art)
8136     ;; Bind the article treatment functions to nil.
8137     (let ((gnus-have-all-headers t)
8138           gnus-article-prepare-hook
8139           gnus-article-decode-hook
8140           gnus-display-mime-function
8141           gnus-break-pages)
8142       ;; Destroy any MIME parts.
8143       (when (gnus-buffer-live-p gnus-article-buffer)
8144         (save-excursion
8145           (set-buffer gnus-article-buffer)
8146           (mm-destroy-parts gnus-article-mime-handles)
8147           ;; Set it to nil for safety reason.
8148           (setq gnus-article-mime-handle-alist nil)
8149           (setq gnus-article-mime-handles nil)))
8150       (gnus-summary-select-article nil 'force))))
8151   (gnus-summary-goto-subject gnus-current-article)
8152   (gnus-summary-position-point))
8153
8154 (defun gnus-summary-show-raw-article ()
8155   "Show the raw article without any article massaging functions being run."
8156   (interactive)
8157   (gnus-summary-show-article t))
8158
8159 (defun gnus-summary-verbose-headers (&optional arg)
8160   "Toggle permanent full header display.
8161 If ARG is a positive number, turn header display on.
8162 If ARG is a negative number, turn header display off."
8163   (interactive "P")
8164   (setq gnus-show-all-headers
8165         (cond ((or (not (numberp arg))
8166                    (zerop arg))
8167                (not gnus-show-all-headers))
8168               ((natnump arg)
8169                t)))
8170   (gnus-summary-show-article))
8171
8172 (defun gnus-summary-toggle-header (&optional arg)
8173   "Show the headers if they are hidden, or hide them if they are shown.
8174 If ARG is a positive number, show the entire header.
8175 If ARG is a negative number, hide the unwanted header lines."
8176   (interactive "P")
8177   (save-excursion
8178     (set-buffer gnus-article-buffer)
8179     (save-restriction
8180       (let* ((buffer-read-only nil)
8181              (inhibit-point-motion-hooks t)
8182              hidden e)
8183         (setq hidden
8184               (if (numberp arg)
8185                   (>= arg 0)
8186                 (save-restriction
8187                   (article-narrow-to-head)
8188                   (gnus-article-hidden-text-p 'headers))))
8189         (goto-char (point-min))
8190         (when (search-forward "\n\n" nil t)
8191           (delete-region (point-min) (1- (point))))
8192         (goto-char (point-min))
8193         (save-excursion
8194           (set-buffer gnus-original-article-buffer)
8195           (goto-char (point-min))
8196           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
8197         (insert-buffer-substring gnus-original-article-buffer 1 e)
8198         (save-restriction
8199           (narrow-to-region (point-min) (point))
8200           (article-decode-encoded-words)
8201           (if  hidden
8202               (let ((gnus-treat-hide-headers nil)
8203                     (gnus-treat-hide-boring-headers nil))
8204                 (gnus-delete-wash-type 'headers)
8205                 (gnus-treat-article 'head))
8206             (gnus-treat-article 'head)))
8207         (gnus-set-mode-line 'article)))))
8208
8209 (defun gnus-summary-show-all-headers ()
8210   "Make all header lines visible."
8211   (interactive)
8212   (gnus-summary-toggle-header 1))
8213
8214 (defun gnus-summary-caesar-message (&optional arg)
8215   "Caesar rotate the current article by 13.
8216 The numerical prefix specifies how many places to rotate each letter
8217 forward."
8218   (interactive "P")
8219   (gnus-summary-select-article)
8220   (let ((mail-header-separator ""))
8221     (gnus-eval-in-buffer-window gnus-article-buffer
8222       (save-restriction
8223         (widen)
8224         (let ((start (window-start))
8225               buffer-read-only)
8226           (message-caesar-buffer-body arg)
8227           (set-window-start (get-buffer-window (current-buffer)) start))))))
8228
8229 (defun gnus-summary-stop-page-breaking ()
8230   "Stop page breaking in the current article."
8231   (interactive)
8232   (gnus-summary-select-article)
8233   (gnus-eval-in-buffer-window gnus-article-buffer
8234     (widen)
8235     (when (gnus-visual-p 'page-marker)
8236       (let ((buffer-read-only nil))
8237         (gnus-remove-text-with-property 'gnus-prev)
8238         (gnus-remove-text-with-property 'gnus-next))
8239       (setq gnus-page-broken nil))))
8240
8241 (defun gnus-summary-move-article (&optional n to-newsgroup
8242                                             select-method action)
8243   "Move the current article to a different newsgroup.
8244 If N is a positive number, move the N next articles.
8245 If N is a negative number, move the N previous articles.
8246 If N is nil and any articles have been marked with the process mark,
8247 move those articles instead.
8248 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8249 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8250 re-spool using this method.
8251
8252 For this function to work, both the current newsgroup and the
8253 newsgroup that you want to move to have to support the `request-move'
8254 and `request-accept' functions.
8255
8256 ACTION can be either `move' (the default), `crosspost' or `copy'."
8257   (interactive "P")
8258   (unless action
8259     (setq action 'move))
8260   ;; Check whether the source group supports the required functions.
8261   (cond ((and (eq action 'move)
8262               (not (gnus-check-backend-function
8263                     'request-move-article gnus-newsgroup-name)))
8264          (error "The current group does not support article moving"))
8265         ((and (eq action 'crosspost)
8266               (not (gnus-check-backend-function
8267                     'request-replace-article gnus-newsgroup-name)))
8268          (error "The current group does not support article editing")))
8269   (let ((articles (gnus-summary-work-articles n))
8270         (prefix (if (gnus-check-backend-function
8271                      'request-move-article gnus-newsgroup-name)
8272                     (gnus-group-real-prefix gnus-newsgroup-name)
8273                   ""))
8274         (names '((move "Move" "Moving")
8275                  (copy "Copy" "Copying")
8276                  (crosspost "Crosspost" "Crossposting")))
8277         (copy-buf (save-excursion
8278                     (nnheader-set-temp-buffer " *copy article*")))
8279         art-group to-method new-xref article to-groups)
8280     (unless (assq action names)
8281       (error "Unknown action %s" action))
8282     ;; We have to select an article to give
8283     ;; `gnus-read-move-group-name' an opportunity to suggest an
8284     ;; appropriate default.
8285     (unless (gnus-buffer-live-p gnus-original-article-buffer)
8286       (let ((gnus-display-mime-function nil)
8287             (gnus-article-prepare-hook nil))
8288         (gnus-summary-select-article nil nil nil (car articles))))
8289     ;; Read the newsgroup name.
8290     (when (and (not to-newsgroup)
8291                (not select-method))
8292       (setq to-newsgroup
8293             (gnus-read-move-group-name
8294              (cadr (assq action names))
8295              (symbol-value (intern (format "gnus-current-%s-group" action)))
8296              articles prefix))
8297       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8298     (setq to-method (or select-method
8299                         (gnus-server-to-method
8300                          (gnus-group-method to-newsgroup))))
8301     ;; Check the method we are to move this article to...
8302     (unless (gnus-check-backend-function
8303              'request-accept-article (car to-method))
8304       (error "%s does not support article copying" (car to-method)))
8305     (unless (gnus-check-server to-method)
8306       (error "Can't open server %s" (car to-method)))
8307     (gnus-message 6 "%s to %s: %s..."
8308                   (caddr (assq action names))
8309                   (or (car select-method) to-newsgroup) articles)
8310     (while articles
8311       (setq article (pop articles))
8312       (setq
8313        art-group
8314        (cond
8315         ;; Move the article.
8316         ((eq action 'move)
8317          ;; Remove this article from future suppression.
8318          (gnus-dup-unsuppress-article article)
8319          (gnus-request-move-article
8320           article                       ; Article to move
8321           gnus-newsgroup-name           ; From newsgroup
8322           (nth 1 (gnus-find-method-for-group
8323                   gnus-newsgroup-name)) ; Server
8324           (list 'gnus-request-accept-article
8325                 to-newsgroup (list 'quote select-method)
8326                 (not articles) t)       ; Accept form
8327           (not articles)))              ; Only save nov last time
8328         ;; Copy the article.
8329         ((eq action 'copy)
8330          (save-excursion
8331            (set-buffer copy-buf)
8332            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8333              (gnus-request-accept-article
8334               to-newsgroup select-method (not articles) t))))
8335         ;; Crosspost the article.
8336         ((eq action 'crosspost)
8337          (let ((xref (message-tokenize-header
8338                       (mail-header-xref (gnus-summary-article-header article))
8339                       " ")))
8340            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8341                                   ":" (number-to-string article)))
8342            (unless xref
8343              (setq xref (list (system-name))))
8344            (setq new-xref
8345                  (concat
8346                   (mapconcat 'identity
8347                              (delete "Xref:" (delete new-xref xref))
8348                              " ")
8349                   " " new-xref))
8350            (save-excursion
8351              (set-buffer copy-buf)
8352              ;; First put the article in the destination group.
8353              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8354              (when (consp (setq art-group
8355                                 (gnus-request-accept-article
8356                                  to-newsgroup select-method (not articles))))
8357                (setq new-xref (concat new-xref " " (car art-group)
8358                                       ":"
8359                                       (number-to-string (cdr art-group))))
8360                ;; Now we have the new Xrefs header, so we insert
8361                ;; it and replace the new article.
8362                (nnheader-replace-header "Xref" new-xref)
8363                (gnus-request-replace-article
8364                 (cdr art-group) to-newsgroup (current-buffer))
8365                art-group))))))
8366       (cond
8367        ((not art-group)
8368         (gnus-message 1 "Couldn't %s article %s: %s"
8369                       (cadr (assq action names)) article
8370                       (nnheader-get-report (car to-method))))
8371        ((eq art-group 'junk)
8372         (when (eq action 'move)
8373           (gnus-summary-mark-article article gnus-canceled-mark)
8374           (gnus-message 4 "Deleted article %s" article)))
8375        (t
8376         (let* ((pto-group (gnus-group-prefixed-name
8377                            (car art-group) to-method))
8378                (entry
8379                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8380                (info (nth 2 entry))
8381                (to-group (gnus-info-group info))
8382                to-marks)
8383           ;; Update the group that has been moved to.
8384           (when (and info
8385                      (memq action '(move copy)))
8386             (unless (member to-group to-groups)
8387               (push to-group to-groups))
8388
8389             (unless (memq article gnus-newsgroup-unreads)
8390               (push 'read to-marks)
8391               (gnus-info-set-read
8392                info (gnus-add-to-range (gnus-info-read info)
8393                                        (list (cdr art-group)))))
8394
8395             ;; See whether the article is to be put in the cache.
8396             (let ((marks gnus-article-mark-lists)
8397                   (to-article (cdr art-group)))
8398
8399               ;; Enter the article into the cache in the new group,
8400               ;; if that is required.
8401               (when gnus-use-cache
8402                 (gnus-cache-possibly-enter-article
8403                  to-group to-article
8404                  (memq article gnus-newsgroup-marked)
8405                  (memq article gnus-newsgroup-dormant)
8406                  (memq article gnus-newsgroup-unreads)))
8407
8408               (when gnus-preserve-marks
8409                 ;; Copy any marks over to the new group.
8410                 (when (and (equal to-group gnus-newsgroup-name)
8411                            (not (memq article gnus-newsgroup-unreads)))
8412                   ;; Mark this article as read in this group.
8413                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8414                   (setcdr (gnus-active to-group) to-article)
8415                   (setcdr gnus-newsgroup-active to-article))
8416
8417                 (while marks
8418                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8419                     (when (memq article (symbol-value
8420                                          (intern (format "gnus-newsgroup-%s"
8421                                                          (caar marks)))))
8422                       (push (cdar marks) to-marks)
8423                       ;; If the other group is the same as this group,
8424                       ;; then we have to add the mark to the list.
8425                       (when (equal to-group gnus-newsgroup-name)
8426                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8427                              (cons to-article
8428                                    (symbol-value
8429                                     (intern (format "gnus-newsgroup-%s"
8430                                                     (caar marks)))))))
8431                       ;; Copy the marks to other group.
8432                       (gnus-add-marked-articles
8433                        to-group (cdar marks) (list to-article) info)))
8434                   (setq marks (cdr marks)))
8435
8436                 (gnus-request-set-mark to-group (list (list (list to-article)
8437                                                             'add
8438                                                             to-marks))))
8439
8440               (gnus-dribble-enter
8441                (concat "(gnus-group-set-info '"
8442                        (gnus-prin1-to-string (gnus-get-info to-group))
8443                        ")"))))
8444
8445           ;; Update the Xref header in this article to point to
8446           ;; the new crossposted article we have just created.
8447           (when (eq action 'crosspost)
8448             (save-excursion
8449               (set-buffer copy-buf)
8450               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8451               (nnheader-replace-header "Xref" new-xref)
8452               (gnus-request-replace-article
8453                article gnus-newsgroup-name (current-buffer)))))
8454
8455         ;;;!!!Why is this necessary?
8456         (set-buffer gnus-summary-buffer)
8457
8458         (gnus-summary-goto-subject article)
8459         (when (eq action 'move)
8460           (gnus-summary-mark-article article gnus-canceled-mark))))
8461       (gnus-summary-remove-process-mark article))
8462     ;; Re-activate all groups that have been moved to.
8463     (save-excursion
8464       (set-buffer gnus-group-buffer)
8465       (let ((gnus-group-marked to-groups))
8466         (gnus-group-get-new-news-this-group nil t)))
8467
8468     (gnus-kill-buffer copy-buf)
8469     (gnus-summary-position-point)
8470     (gnus-set-mode-line 'summary)))
8471
8472 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8473   "Move the current article to a different newsgroup.
8474 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8475 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8476 re-spool using this method."
8477   (interactive "P")
8478   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8479
8480 (defun gnus-summary-crosspost-article (&optional n)
8481   "Crosspost the current article to some other group."
8482   (interactive "P")
8483   (gnus-summary-move-article n nil nil 'crosspost))
8484
8485 (defcustom gnus-summary-respool-default-method nil
8486   "Default method type for respooling an article.
8487 If nil, use to the current newsgroup method."
8488   :type 'symbol
8489   :group 'gnus-summary-mail)
8490
8491 (defun gnus-summary-respool-article (&optional n method)
8492   "Respool the current article.
8493 The article will be squeezed through the mail spooling process again,
8494 which means that it will be put in some mail newsgroup or other
8495 depending on `nnmail-split-methods'.
8496 If N is a positive number, respool the N next articles.
8497 If N is a negative number, respool the N previous articles.
8498 If N is nil and any articles have been marked with the process mark,
8499 respool those articles instead.
8500
8501 Respooling can be done both from mail groups and \"real\" newsgroups.
8502 In the former case, the articles in question will be moved from the
8503 current group into whatever groups they are destined to.  In the
8504 latter case, they will be copied into the relevant groups."
8505   (interactive
8506    (list current-prefix-arg
8507          (let* ((methods (gnus-methods-using 'respool))
8508                 (methname
8509                  (symbol-name (or gnus-summary-respool-default-method
8510                                   (car (gnus-find-method-for-group
8511                                         gnus-newsgroup-name)))))
8512                 (method
8513                  (gnus-completing-read-with-default
8514                   methname "What backend do you want to use when respooling?"
8515                   methods nil t nil 'gnus-mail-method-history))
8516                 ms)
8517            (cond
8518             ((zerop (length (setq ms (gnus-servers-using-backend
8519                                       (intern method)))))
8520              (list (intern method) ""))
8521             ((= 1 (length ms))
8522              (car ms))
8523             (t
8524              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8525                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8526                            ms-alist))))))))
8527   (unless method
8528     (error "No method given for respooling"))
8529   (if (assoc (symbol-name
8530               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8531              (gnus-methods-using 'respool))
8532       (gnus-summary-move-article n nil method)
8533     (gnus-summary-copy-article n nil method)))
8534
8535 (defun gnus-summary-import-article (file &optional edit)
8536   "Import an arbitrary file into a mail newsgroup."
8537   (interactive "fImport file: \nP")
8538   (let ((group gnus-newsgroup-name)
8539         (now (current-time))
8540         atts lines group-art)
8541     (unless (gnus-check-backend-function 'request-accept-article group)
8542       (error "%s does not support article importing" group))
8543     (or (file-readable-p file)
8544         (not (file-regular-p file))
8545         (error "Can't read %s" file))
8546     (save-excursion
8547       (set-buffer (gnus-get-buffer-create " *import file*"))
8548       (erase-buffer)
8549       (nnheader-insert-file-contents file)
8550       (goto-char (point-min))
8551       (if (nnheader-article-p)
8552           (save-restriction
8553             (goto-char (point-min))
8554             (search-forward "\n\n" nil t)
8555             (narrow-to-region (point-min) (1- (point)))
8556             (goto-char (point-min))
8557             (unless (re-search-forward "^date:" nil t)
8558               (goto-char (point-max))
8559               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8560        ;; This doesn't look like an article, so we fudge some headers.
8561         (setq atts (file-attributes file)
8562               lines (count-lines (point-min) (point-max)))
8563         (insert "From: " (read-string "From: ") "\n"
8564                 "Subject: " (read-string "Subject: ") "\n"
8565                 "Date: " (message-make-date (nth 5 atts)) "\n"
8566                 "Message-ID: " (message-make-message-id) "\n"
8567                 "Lines: " (int-to-string lines) "\n"
8568                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8569       (setq group-art (gnus-request-accept-article group nil t))
8570       (kill-buffer (current-buffer)))
8571     (setq gnus-newsgroup-active (gnus-activate-group group))
8572     (forward-line 1)
8573     (gnus-summary-goto-article (cdr group-art) nil t)
8574     (when edit
8575       (gnus-summary-edit-article))))
8576
8577 (defun gnus-summary-create-article ()
8578   "Create an article in a mail newsgroup."
8579   (interactive)
8580   (let ((group gnus-newsgroup-name)
8581         (now (current-time))
8582         group-art)
8583     (unless (gnus-check-backend-function 'request-accept-article group)
8584       (error "%s does not support article importing" group))
8585     (save-excursion
8586       (set-buffer (gnus-get-buffer-create " *import file*"))
8587       (erase-buffer)
8588       (goto-char (point-min))
8589       ;; This doesn't look like an article, so we fudge some headers.
8590       (insert "From: " (read-string "From: ") "\n"
8591               "Subject: " (read-string "Subject: ") "\n"
8592               "Date: " (message-make-date now) "\n"
8593               "Message-ID: " (message-make-message-id) "\n")
8594       (setq group-art (gnus-request-accept-article group nil t))
8595       (kill-buffer (current-buffer)))
8596     (setq gnus-newsgroup-active (gnus-activate-group group))
8597     (forward-line 1)
8598     (gnus-summary-goto-article (cdr group-art) nil t)
8599     (gnus-summary-edit-article)))
8600
8601 (defun gnus-summary-article-posted-p ()
8602   "Say whether the current (mail) article is available from news as well.
8603 This will be the case if the article has both been mailed and posted."
8604   (interactive)
8605   (let ((id (mail-header-references (gnus-summary-article-header)))
8606         (gnus-override-method (car (gnus-refer-article-methods))))
8607     (if (gnus-request-head id "")
8608         (gnus-message 2 "The current message was found on %s"
8609                       gnus-override-method)
8610       (gnus-message 2 "The current message couldn't be found on %s"
8611                     gnus-override-method)
8612       nil)))
8613
8614 (defun gnus-summary-expire-articles (&optional now)
8615   "Expire all articles that are marked as expirable in the current group."
8616   (interactive)
8617   (when (gnus-check-backend-function
8618          'request-expire-articles gnus-newsgroup-name)
8619     ;; This backend supports expiry.
8620     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8621            (expirable (if total
8622                           (progn
8623                             ;; We need to update the info for
8624                             ;; this group for `gnus-list-of-read-articles'
8625                             ;; to give us the right answer.
8626                             (gnus-run-hooks 'gnus-exit-group-hook)
8627                             (gnus-summary-update-info)
8628                             (gnus-list-of-read-articles gnus-newsgroup-name))
8629                         (setq gnus-newsgroup-expirable
8630                               (sort gnus-newsgroup-expirable '<))))
8631            (expiry-wait (if now 'immediate
8632                           (gnus-group-find-parameter
8633                            gnus-newsgroup-name 'expiry-wait)))
8634            (nnmail-expiry-target
8635             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8636                 nnmail-expiry-target))
8637            es)
8638       (when expirable
8639         ;; There are expirable articles in this group, so we run them
8640         ;; through the expiry process.
8641         (gnus-message 6 "Expiring articles...")
8642         (unless (gnus-check-group gnus-newsgroup-name)
8643           (error "Can't open server for %s" gnus-newsgroup-name))
8644         ;; The list of articles that weren't expired is returned.
8645         (save-excursion
8646           (if expiry-wait
8647               (let ((nnmail-expiry-wait-function nil)
8648                     (nnmail-expiry-wait expiry-wait))
8649                 (setq es (gnus-request-expire-articles
8650                           expirable gnus-newsgroup-name)))
8651             (setq es (gnus-request-expire-articles
8652                       expirable gnus-newsgroup-name)))
8653           (unless total
8654             (setq gnus-newsgroup-expirable es))
8655           ;; We go through the old list of expirable, and mark all
8656           ;; really expired articles as nonexistent.
8657           (unless (eq es expirable) ;If nothing was expired, we don't mark.
8658             (let ((gnus-use-cache nil))
8659               (while expirable
8660                 (unless (memq (car expirable) es)
8661                   (when (gnus-data-find (car expirable))
8662                     (gnus-summary-mark-article
8663                      (car expirable) gnus-canceled-mark)))
8664                 (setq expirable (cdr expirable))))))
8665         (gnus-message 6 "Expiring articles...done")))))
8666
8667 (defun gnus-summary-expire-articles-now ()
8668   "Expunge all expirable articles in the current group.
8669 This means that *all* articles that are marked as expirable will be
8670 deleted forever, right now."
8671   (interactive)
8672   (or gnus-expert-user
8673       (gnus-yes-or-no-p
8674        "Are you really, really, really sure you want to delete all these messages? ")
8675       (error "Phew!"))
8676   (gnus-summary-expire-articles t))
8677
8678 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8679 (defun gnus-summary-delete-article (&optional n)
8680   "Delete the N next (mail) articles.
8681 This command actually deletes articles.  This is not a marking
8682 command.  The article will disappear forever from your life, never to
8683 return.
8684 If N is negative, delete backwards.
8685 If N is nil and articles have been marked with the process mark,
8686 delete these instead."
8687   (interactive "P")
8688   (unless (gnus-check-backend-function 'request-expire-articles
8689                                        gnus-newsgroup-name)
8690     (error "The current newsgroup does not support article deletion"))
8691   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8692     (error "Couldn't open server"))
8693   ;; Compute the list of articles to delete.
8694   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8695         not-deleted)
8696     (if (and gnus-novice-user
8697              (not (gnus-yes-or-no-p
8698                    (format "Do you really want to delete %s forever? "
8699                            (if (> (length articles) 1)
8700                                (format "these %s articles" (length articles))
8701                              "this article")))))
8702         ()
8703       ;; Delete the articles.
8704       (setq not-deleted (gnus-request-expire-articles
8705                          articles gnus-newsgroup-name 'force))
8706       (while articles
8707         (gnus-summary-remove-process-mark (car articles))
8708         ;; The backend might not have been able to delete the article
8709         ;; after all.
8710         (unless (memq (car articles) not-deleted)
8711           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8712         (setq articles (cdr articles)))
8713       (when not-deleted
8714         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8715     (gnus-summary-position-point)
8716     (gnus-set-mode-line 'summary)
8717     not-deleted))
8718
8719 (defun gnus-summary-edit-article (&optional arg)
8720   "Edit the current article.
8721 This will have permanent effect only in mail groups.
8722 If ARG is nil, edit the decoded articles.
8723 If ARG is 1, edit the raw articles.
8724 If ARG is 2, edit the raw articles even in read-only groups.
8725 If ARG is 3, edit the articles with the current handles.
8726 Otherwise, allow editing of articles even in read-only
8727 groups."
8728   (interactive "P")
8729   (let (force raw current-handles)
8730     (cond
8731      ((null arg))
8732      ((eq arg 1)
8733       (setq raw t))
8734      ((eq arg 2)
8735       (setq raw t
8736             force t))
8737      ((eq arg 3)
8738       (setq current-handles
8739             (and (gnus-buffer-live-p gnus-article-buffer)
8740                  (with-current-buffer gnus-article-buffer
8741                    (prog1
8742                        gnus-article-mime-handles
8743                      (setq gnus-article-mime-handles nil))))))
8744      (t
8745       (setq force t)))
8746     (when (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
8747       (error "Can't edit the raw article in group nndraft:drafts"))
8748     (save-excursion
8749       (set-buffer gnus-summary-buffer)
8750       (let ((mail-parse-charset gnus-newsgroup-charset)
8751             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8752         (gnus-set-global-variables)
8753         (when (and (not force)
8754                    (gnus-group-read-only-p))
8755           (error "The current newsgroup does not support article editing"))
8756         (gnus-summary-show-article t)
8757         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
8758           (with-current-buffer gnus-article-buffer
8759             (mm-enable-multibyte)))
8760         (if (equal gnus-newsgroup-name "nndraft:drafts")
8761             (setq raw t))
8762         (gnus-article-edit-article
8763          (if raw 'ignore
8764            `(lambda ()
8765               (let ((mbl mml-buffer-list))
8766                 (setq mml-buffer-list nil)
8767                 (mime-to-mml ,'current-handles)
8768                 (let ((mbl1 mml-buffer-list))
8769                   (setq mml-buffer-list mbl)
8770                   (set (make-local-variable 'mml-buffer-list) mbl1))
8771                 (make-local-hook 'kill-buffer-hook)
8772                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
8773          `(lambda (no-highlight)
8774             (let ((mail-parse-charset ',gnus-newsgroup-charset)
8775                   (message-options message-options)
8776                   (message-options-set-recipient)
8777                   (mail-parse-ignored-charsets
8778                    ',gnus-newsgroup-ignored-charsets))
8779               ,(if (not raw) '(progn
8780                                 (mml-to-mime)
8781                                 (mml-destroy-buffers)
8782                                 (remove-hook 'kill-buffer-hook
8783                                              'mml-destroy-buffers t)
8784                                 (kill-local-variable 'mml-buffer-list)))
8785               (gnus-summary-edit-article-done
8786                ,(or (mail-header-references gnus-current-headers) "")
8787                ,(gnus-group-read-only-p)
8788                ,gnus-summary-buffer no-highlight))))))))
8789
8790 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8791
8792 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8793                                                  no-highlight)
8794   "Make edits to the current article permanent."
8795   (interactive)
8796   (save-excursion
8797    ;; The buffer restriction contains the entire article if it exists.
8798     (when (article-goto-body)
8799       (let ((lines (count-lines (point) (point-max)))
8800             (length (- (point-max) (point)))
8801             (case-fold-search t)
8802             (body (copy-marker (point))))
8803         (goto-char (point-min))
8804         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8805           (delete-region (match-beginning 1) (match-end 1))
8806           (insert (number-to-string length)))
8807         (goto-char (point-min))
8808         (when (re-search-forward
8809                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8810           (delete-region (match-beginning 1) (match-end 1))
8811           (insert (number-to-string length)))
8812         (goto-char (point-min))
8813         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8814           (delete-region (match-beginning 1) (match-end 1))
8815           (insert (number-to-string lines))))))
8816   ;; Replace the article.
8817   (let ((buf (current-buffer)))
8818     (with-temp-buffer
8819       (insert-buffer-substring buf)
8820
8821       (if (and (not read-only)
8822                (not (gnus-request-replace-article
8823                      (cdr gnus-article-current) (car gnus-article-current)
8824                      (current-buffer) t)))
8825           (error "Couldn't replace article")
8826         ;; Update the summary buffer.
8827         (if (and references
8828                  (equal (message-tokenize-header references " ")
8829                         (message-tokenize-header
8830                          (or (message-fetch-field "references") "") " ")))
8831             ;; We only have to update this line.
8832             (save-excursion
8833               (save-restriction
8834                 (message-narrow-to-head)
8835                 (let ((head (buffer-string))
8836                       header)
8837                   (with-temp-buffer
8838                     (insert (format "211 %d Article retrieved.\n"
8839                                     (cdr gnus-article-current)))
8840                     (insert head)
8841                     (insert ".\n")
8842                     (let ((nntp-server-buffer (current-buffer)))
8843                       (setq header (car (gnus-get-newsgroup-headers
8844                                          (save-excursion
8845                                            (set-buffer gnus-summary-buffer)
8846                                            gnus-newsgroup-dependencies)
8847                                          t))))
8848                     (save-excursion
8849                       (set-buffer gnus-summary-buffer)
8850                       (gnus-data-set-header
8851                        (gnus-data-find (cdr gnus-article-current))
8852                        header)
8853                       (gnus-summary-update-article-line
8854                        (cdr gnus-article-current) header)
8855                       (if (gnus-summary-goto-subject
8856                            (cdr gnus-article-current) nil t)
8857                           (gnus-summary-update-secondary-mark
8858                            (cdr gnus-article-current))))))))
8859           ;; Update threads.
8860           (set-buffer (or buffer gnus-summary-buffer))
8861           (gnus-summary-update-article (cdr gnus-article-current))
8862           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8863               (gnus-summary-update-secondary-mark
8864                (cdr gnus-article-current))))
8865         ;; Prettify the article buffer again.
8866         (unless no-highlight
8867           (save-excursion
8868             (set-buffer gnus-article-buffer)
8869             ;;;!!! Fix this -- article should be rehighlighted.
8870             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8871             (set-buffer gnus-original-article-buffer)
8872             (gnus-request-article
8873              (cdr gnus-article-current)
8874              (car gnus-article-current) (current-buffer))))
8875         ;; Prettify the summary buffer line.
8876         (when (gnus-visual-p 'summary-highlight 'highlight)
8877           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8878
8879 (defun gnus-summary-edit-wash (key)
8880   "Perform editing command KEY in the article buffer."
8881   (interactive
8882    (list
8883     (progn
8884       (message "%s" (concat (this-command-keys) "- "))
8885       (read-char))))
8886   (message "")
8887   (gnus-summary-edit-article)
8888   (execute-kbd-macro (concat (this-command-keys) key))
8889   (gnus-article-edit-done))
8890
8891 ;;; Respooling
8892
8893 (defun gnus-summary-respool-query (&optional silent trace)
8894   "Query where the respool algorithm would put this article."
8895   (interactive)
8896   (let (gnus-mark-article-hook)
8897     (gnus-summary-select-article)
8898     (save-excursion
8899       (set-buffer gnus-original-article-buffer)
8900       (save-restriction
8901         (message-narrow-to-head)
8902         (let ((groups (nnmail-article-group 'identity trace)))
8903           (unless silent
8904             (if groups
8905                 (message "This message would go to %s"
8906                          (mapconcat 'car groups ", "))
8907               (message "This message would go to no groups"))
8908             groups))))))
8909
8910 (defun gnus-summary-respool-trace ()
8911   "Trace where the respool algorithm would put this article.
8912 Display a buffer showing all fancy splitting patterns which matched."
8913   (interactive)
8914   (gnus-summary-respool-query nil t))
8915
8916 ;; Summary marking commands.
8917
8918 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8919   "Mark articles which has the same subject as read, and then select the next.
8920 If UNMARK is positive, remove any kind of mark.
8921 If UNMARK is negative, tick articles."
8922   (interactive "P")
8923   (when unmark
8924     (setq unmark (prefix-numeric-value unmark)))
8925   (let ((count
8926          (gnus-summary-mark-same-subject
8927           (gnus-summary-article-subject) unmark)))
8928     ;; Select next unread article.  If auto-select-same mode, should
8929     ;; select the first unread article.
8930     (gnus-summary-next-article t (and gnus-auto-select-same
8931                                       (gnus-summary-article-subject)))
8932     (gnus-message 7 "%d article%s marked as %s"
8933                   count (if (= count 1) " is" "s are")
8934                   (if unmark "unread" "read"))))
8935
8936 (defun gnus-summary-kill-same-subject (&optional unmark)
8937   "Mark articles which has the same subject as read.
8938 If UNMARK is positive, remove any kind of mark.
8939 If UNMARK is negative, tick articles."
8940   (interactive "P")
8941   (when unmark
8942     (setq unmark (prefix-numeric-value unmark)))
8943   (let ((count
8944          (gnus-summary-mark-same-subject
8945           (gnus-summary-article-subject) unmark)))
8946     ;; If marked as read, go to next unread subject.
8947     (when (null unmark)
8948       ;; Go to next unread subject.
8949       (gnus-summary-next-subject 1 t))
8950     (gnus-message 7 "%d articles are marked as %s"
8951                   count (if unmark "unread" "read"))))
8952
8953 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8954   "Mark articles with same SUBJECT as read, and return marked number.
8955 If optional argument UNMARK is positive, remove any kinds of marks.
8956 If optional argument UNMARK is negative, mark articles as unread instead."
8957   (let ((count 1))
8958     (save-excursion
8959       (cond
8960        ((null unmark)                   ; Mark as read.
8961         (while (and
8962                 (progn
8963                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8964                   (gnus-summary-show-thread) t)
8965                 (gnus-summary-find-subject subject))
8966           (setq count (1+ count))))
8967        ((> unmark 0)                    ; Tick.
8968         (while (and
8969                 (progn
8970                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8971                   (gnus-summary-show-thread) t)
8972                 (gnus-summary-find-subject subject))
8973           (setq count (1+ count))))
8974        (t                               ; Mark as unread.
8975         (while (and
8976                 (progn
8977                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8978                   (gnus-summary-show-thread) t)
8979                 (gnus-summary-find-subject subject))
8980           (setq count (1+ count)))))
8981       (gnus-set-mode-line 'summary)
8982       ;; Return the number of marked articles.
8983       count)))
8984
8985 (defun gnus-summary-mark-as-processable (n &optional unmark)
8986   "Set the process mark on the next N articles.
8987 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8988 the process mark instead.  The difference between N and the actual
8989 number of articles marked is returned."
8990   (interactive "P")
8991   (if (and (null n) (gnus-region-active-p))
8992       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8993     (setq n (prefix-numeric-value n))
8994     (let ((backward (< n 0))
8995           (n (abs n)))
8996       (while (and
8997               (> n 0)
8998               (if unmark
8999                   (gnus-summary-remove-process-mark
9000                    (gnus-summary-article-number))
9001                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9002               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9003         (setq n (1- n)))
9004       (when (/= 0 n)
9005         (gnus-message 7 "No more articles"))
9006       (gnus-summary-recenter)
9007       (gnus-summary-position-point)
9008       n)))
9009
9010 (defun gnus-summary-unmark-as-processable (n)
9011   "Remove the process mark from the next N articles.
9012 If N is negative, unmark backward instead.  The difference between N and
9013 the actual number of articles unmarked is returned."
9014   (interactive "P")
9015   (gnus-summary-mark-as-processable n t))
9016
9017 (defun gnus-summary-unmark-all-processable ()
9018   "Remove the process mark from all articles."
9019   (interactive)
9020   (save-excursion
9021     (while gnus-newsgroup-processable
9022       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9023   (gnus-summary-position-point))
9024
9025 (defun gnus-summary-add-mark (article type)
9026   "Mark ARTICLE with a mark of TYPE."
9027   (let ((vtype (car (assq type gnus-article-mark-lists)))
9028         var)
9029     (if (not vtype)
9030         (error "No such mark type: %s" type)
9031       (setq var (intern (format "gnus-newsgroup-%s" type)))
9032       (set var (cons article (symbol-value var)))
9033       (if (memq type '(processable cached replied forwarded recent saved))
9034           (gnus-summary-update-secondary-mark article)
9035         ;;; !!! This is bogus.  We should find out what primary
9036         ;;; !!! mark we want to set.
9037         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9038
9039 (defun gnus-summary-mark-as-expirable (n)
9040   "Mark N articles forward as expirable.
9041 If N is negative, mark backward instead.  The difference between N and
9042 the actual number of articles marked is returned."
9043   (interactive "p")
9044   (gnus-summary-mark-forward n gnus-expirable-mark))
9045
9046 (defun gnus-summary-mark-article-as-replied (article)
9047   "Mark ARTICLE as replied to and update the summary line.
9048 ARTICLE can also be a list of articles."
9049   (interactive (list (gnus-summary-article-number)))
9050   (let ((articles (if (listp article) article (list article))))
9051     (dolist (article articles)
9052       (push article gnus-newsgroup-replied)
9053       (let ((buffer-read-only nil))
9054         (when (gnus-summary-goto-subject article nil t)
9055           (gnus-summary-update-secondary-mark article))))))
9056
9057 (defun gnus-summary-mark-article-as-forwarded (article)
9058   "Mark ARTICLE as forwarded and update the summary line.
9059 ARTICLE can also be a list of articles."
9060   (let ((articles (if (listp article) article (list article))))
9061     (dolist (article articles)
9062       (push article gnus-newsgroup-forwarded)
9063       (let ((buffer-read-only nil))
9064         (when (gnus-summary-goto-subject article nil t)
9065           (gnus-summary-update-secondary-mark article))))))
9066
9067 (defun gnus-summary-set-bookmark (article)
9068   "Set a bookmark in current article."
9069   (interactive (list (gnus-summary-article-number)))
9070   (when (or (not (get-buffer gnus-article-buffer))
9071             (not gnus-current-article)
9072             (not gnus-article-current)
9073             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9074     (error "No current article selected"))
9075   ;; Remove old bookmark, if one exists.
9076   (let ((old (assq article gnus-newsgroup-bookmarks)))
9077     (when old
9078       (setq gnus-newsgroup-bookmarks
9079             (delq old gnus-newsgroup-bookmarks))))
9080   ;; Set the new bookmark, which is on the form
9081   ;; (article-number . line-number-in-body).
9082   (push
9083    (cons article
9084          (save-excursion
9085            (set-buffer gnus-article-buffer)
9086            (count-lines
9087             (min (point)
9088                  (save-excursion
9089                    (goto-char (point-min))
9090                    (search-forward "\n\n" nil t)
9091                    (point)))
9092             (point))))
9093    gnus-newsgroup-bookmarks)
9094   (gnus-message 6 "A bookmark has been added to the current article."))
9095
9096 (defun gnus-summary-remove-bookmark (article)
9097   "Remove the bookmark from the current article."
9098   (interactive (list (gnus-summary-article-number)))
9099   ;; Remove old bookmark, if one exists.
9100   (let ((old (assq article gnus-newsgroup-bookmarks)))
9101     (if old
9102         (progn
9103           (setq gnus-newsgroup-bookmarks
9104                 (delq old gnus-newsgroup-bookmarks))
9105           (gnus-message 6 "Removed bookmark."))
9106       (gnus-message 6 "No bookmark in current article."))))
9107
9108 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9109 (defun gnus-summary-mark-as-dormant (n)
9110   "Mark N articles forward as dormant.
9111 If N is negative, mark backward instead.  The difference between N and
9112 the actual number of articles marked is returned."
9113   (interactive "p")
9114   (gnus-summary-mark-forward n gnus-dormant-mark))
9115
9116 (defun gnus-summary-set-process-mark (article)
9117   "Set the process mark on ARTICLE and update the summary line."
9118   (setq gnus-newsgroup-processable
9119         (cons article
9120               (delq article gnus-newsgroup-processable)))
9121   (when (gnus-summary-goto-subject article)
9122     (gnus-summary-show-thread)
9123     (gnus-summary-goto-subject article)
9124     (gnus-summary-update-secondary-mark article)))
9125
9126 (defun gnus-summary-remove-process-mark (article)
9127   "Remove the process mark from ARTICLE and update the summary line."
9128   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9129   (when (gnus-summary-goto-subject article)
9130     (gnus-summary-show-thread)
9131     (gnus-summary-goto-subject article)
9132     (gnus-summary-update-secondary-mark article)))
9133
9134 (defun gnus-summary-set-saved-mark (article)
9135   "Set the process mark on ARTICLE and update the summary line."
9136   (push article gnus-newsgroup-saved)
9137   (when (gnus-summary-goto-subject article)
9138     (gnus-summary-update-secondary-mark article)))
9139
9140 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9141   "Mark N articles as read forwards.
9142 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9143 The difference between N and the actual number of articles marked is
9144 returned.
9145 Iff NO-EXPIRE, auto-expiry will be inhibited."
9146   (interactive "p")
9147   (gnus-summary-show-thread)
9148   (let ((backward (< n 0))
9149         (gnus-summary-goto-unread
9150          (and gnus-summary-goto-unread
9151               (not (eq gnus-summary-goto-unread 'never))
9152               (not (memq mark (list gnus-unread-mark
9153                                     gnus-ticked-mark gnus-dormant-mark)))))
9154         (n (abs n))
9155         (mark (or mark gnus-del-mark)))
9156     (while (and (> n 0)
9157                 (gnus-summary-mark-article nil mark no-expire)
9158                 (zerop (gnus-summary-next-subject
9159                         (if backward -1 1)
9160                         (and gnus-summary-goto-unread
9161                              (not (eq gnus-summary-goto-unread 'never)))
9162                         t)))
9163       (setq n (1- n)))
9164     (when (/= 0 n)
9165       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9166     (gnus-summary-recenter)
9167     (gnus-summary-position-point)
9168     (gnus-set-mode-line 'summary)
9169     n))
9170
9171 (defun gnus-summary-mark-article-as-read (mark)
9172   "Mark the current article quickly as read with MARK."
9173   (let ((article (gnus-summary-article-number)))
9174     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9175     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9176     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9177     (push (cons article mark) gnus-newsgroup-reads)
9178     ;; Possibly remove from cache, if that is used.
9179     (when gnus-use-cache
9180       (gnus-cache-enter-remove-article article))
9181     ;; Allow the backend to change the mark.
9182     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9183     ;; Check for auto-expiry.
9184     (when (and gnus-newsgroup-auto-expire
9185                (memq mark gnus-auto-expirable-marks))
9186       (setq mark gnus-expirable-mark)
9187       ;; Let the backend know about the mark change.
9188       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9189       (push article gnus-newsgroup-expirable))
9190     ;; Set the mark in the buffer.
9191     (gnus-summary-update-mark mark 'unread)
9192     t))
9193
9194 (defun gnus-summary-mark-article-as-unread (mark)
9195   "Mark the current article quickly as unread with MARK."
9196   (let* ((article (gnus-summary-article-number))
9197          (old-mark (gnus-summary-article-mark article)))
9198     ;; Allow the backend to change the mark.
9199     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9200     (if (eq mark old-mark)
9201         t
9202       (if (<= article 0)
9203           (progn
9204             (gnus-error 1 "Can't mark negative article numbers")
9205             nil)
9206         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9207         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9208         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9209         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9210         (cond ((= mark gnus-ticked-mark)
9211                (push article gnus-newsgroup-marked))
9212               ((= mark gnus-dormant-mark)
9213                (push article gnus-newsgroup-dormant))
9214               (t
9215                (push article gnus-newsgroup-unreads)))
9216         (gnus-pull article gnus-newsgroup-reads)
9217
9218         ;; See whether the article is to be put in the cache.
9219         (and gnus-use-cache
9220              (vectorp (gnus-summary-article-header article))
9221              (save-excursion
9222                (gnus-cache-possibly-enter-article
9223                 gnus-newsgroup-name article
9224                 (= mark gnus-ticked-mark)
9225                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9226
9227         ;; Fix the mark.
9228         (gnus-summary-update-mark mark 'unread)
9229         t))))
9230
9231 (defun gnus-summary-mark-article (&optional article mark no-expire)
9232   "Mark ARTICLE with MARK.  MARK can be any character.
9233 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9234 `??' (dormant) and `?E' (expirable).
9235 If MARK is nil, then the default character `?r' is used.
9236 If ARTICLE is nil, then the article on the current line will be
9237 marked.
9238 Iff NO-EXPIRE, auto-expiry will be inhibited."
9239   ;; The mark might be a string.
9240   (when (stringp mark)
9241     (setq mark (aref mark 0)))
9242   ;; If no mark is given, then we check auto-expiring.
9243   (when (null mark)
9244     (setq mark gnus-del-mark))
9245   (when (and (not no-expire)
9246              gnus-newsgroup-auto-expire
9247              (memq mark gnus-auto-expirable-marks))
9248     (setq mark gnus-expirable-mark))
9249   (let ((article (or article (gnus-summary-article-number)))
9250         (old-mark (gnus-summary-article-mark article)))
9251     ;; Allow the backend to change the mark.
9252     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9253     (if (eq mark old-mark)
9254         t
9255       (unless article
9256         (error "No article on current line"))
9257       (if (not (if (or (= mark gnus-unread-mark)
9258                        (= mark gnus-ticked-mark)
9259                        (= mark gnus-dormant-mark))
9260                    (gnus-mark-article-as-unread article mark)
9261                  (gnus-mark-article-as-read article mark)))
9262           t
9263         ;; See whether the article is to be put in the cache.
9264         (and gnus-use-cache
9265              (not (= mark gnus-canceled-mark))
9266              (vectorp (gnus-summary-article-header article))
9267              (save-excursion
9268                (gnus-cache-possibly-enter-article
9269                 gnus-newsgroup-name article
9270                 (= mark gnus-ticked-mark)
9271                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9272
9273         (when (gnus-summary-goto-subject article nil t)
9274           (let ((buffer-read-only nil))
9275             (gnus-summary-show-thread)
9276             ;; Fix the mark.
9277             (gnus-summary-update-mark mark 'unread)
9278             t))))))
9279
9280 (defun gnus-summary-update-secondary-mark (article)
9281   "Update the secondary (read, process, cache) mark."
9282   (gnus-summary-update-mark
9283    (cond ((memq article gnus-newsgroup-processable)
9284           gnus-process-mark)
9285          ((memq article gnus-newsgroup-cached)
9286           gnus-cached-mark)
9287          ((memq article gnus-newsgroup-replied)
9288           gnus-replied-mark)
9289          ((memq article gnus-newsgroup-forwarded)
9290           gnus-forwarded-mark)
9291          ((memq article gnus-newsgroup-saved)
9292           gnus-saved-mark)
9293          ((memq article gnus-newsgroup-recent)
9294           gnus-recent-mark)
9295          ((memq article gnus-newsgroup-unseen)
9296           gnus-unseen-mark)
9297          (t gnus-no-mark))
9298    'replied)
9299   (when (gnus-visual-p 'summary-highlight 'highlight)
9300     (gnus-run-hooks 'gnus-summary-update-hook))
9301   t)
9302
9303 (defun gnus-summary-update-mark (mark type)
9304   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9305         (buffer-read-only nil))
9306     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9307     (when forward
9308       (when (looking-at "\r")
9309         (incf forward))
9310       (when (<= (+ forward (point)) (point-max))
9311         ;; Go to the right position on the line.
9312         (goto-char (+ forward (point)))
9313         ;; Replace the old mark with the new mark.
9314         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9315         ;; Optionally update the marks by some user rule.
9316         (when (eq type 'unread)
9317           (gnus-data-set-mark
9318            (gnus-data-find (gnus-summary-article-number)) mark)
9319           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9320
9321 (defun gnus-mark-article-as-read (article &optional mark)
9322   "Enter ARTICLE in the pertinent lists and remove it from others."
9323   ;; Make the article expirable.
9324   (let ((mark (or mark gnus-del-mark)))
9325     (if (= mark gnus-expirable-mark)
9326         (push article gnus-newsgroup-expirable)
9327       (setq gnus-newsgroup-expirable (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              (push article gnus-newsgroup-marked))
9356             ((= mark gnus-dormant-mark)
9357              (push article gnus-newsgroup-dormant))
9358             (t
9359              (push article gnus-newsgroup-unreads)))
9360       (gnus-pull article gnus-newsgroup-reads)
9361       t)))
9362
9363 (defalias 'gnus-summary-mark-as-unread-forward
9364   'gnus-summary-tick-article-forward)
9365 (make-obsolete 'gnus-summary-mark-as-unread-forward
9366                'gnus-summary-tick-article-forward)
9367 (defun gnus-summary-tick-article-forward (n)
9368   "Tick N articles forwards.
9369 If N is negative, tick backwards instead.
9370 The difference between N and the number of articles ticked is returned."
9371   (interactive "p")
9372   (gnus-summary-mark-forward n gnus-ticked-mark))
9373
9374 (defalias 'gnus-summary-mark-as-unread-backward
9375   'gnus-summary-tick-article-backward)
9376 (make-obsolete 'gnus-summary-mark-as-unread-backward
9377                'gnus-summary-tick-article-backward)
9378 (defun gnus-summary-tick-article-backward (n)
9379   "Tick N articles backwards.
9380 The difference between N and the number of articles ticked is returned."
9381   (interactive "p")
9382   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9383
9384 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9385 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9386 (defun gnus-summary-tick-article (&optional article clear-mark)
9387   "Mark current article as unread.
9388 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9389 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9390   (interactive)
9391   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9392                                        gnus-ticked-mark)))
9393
9394 (defun gnus-summary-mark-as-read-forward (n)
9395   "Mark N articles as read forwards.
9396 If N is negative, mark backwards instead.
9397 The difference between N and the actual number of articles marked is
9398 returned."
9399   (interactive "p")
9400   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9401
9402 (defun gnus-summary-mark-as-read-backward (n)
9403   "Mark the N articles as read backwards.
9404 The difference between N and the actual number of articles marked is
9405 returned."
9406   (interactive "p")
9407   (gnus-summary-mark-forward
9408    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9409
9410 (defun gnus-summary-mark-as-read (&optional article mark)
9411   "Mark current article as read.
9412 ARTICLE specifies the article to be marked as read.
9413 MARK specifies a string to be inserted at the beginning of the line."
9414   (gnus-summary-mark-article article mark))
9415
9416 (defun gnus-summary-clear-mark-forward (n)
9417   "Clear marks from N articles forward.
9418 If N is negative, clear backward instead.
9419 The difference between N and the number of marks cleared is returned."
9420   (interactive "p")
9421   (gnus-summary-mark-forward n gnus-unread-mark))
9422
9423 (defun gnus-summary-clear-mark-backward (n)
9424   "Clear marks from N articles backward.
9425 The difference between N and the number of marks cleared is returned."
9426   (interactive "p")
9427   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9428
9429 (defun gnus-summary-mark-unread-as-read ()
9430   "Intended to be used by `gnus-summary-mark-article-hook'."
9431   (when (memq gnus-current-article gnus-newsgroup-unreads)
9432     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9433
9434 (defun gnus-summary-mark-read-and-unread-as-read ()
9435   "Intended to be used by `gnus-summary-mark-article-hook'."
9436   (let ((mark (gnus-summary-article-mark)))
9437     (when (or (gnus-unread-mark-p mark)
9438               (gnus-read-mark-p mark))
9439       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9440
9441 (defun gnus-summary-mark-unread-as-ticked ()
9442   "Intended to be used by `gnus-summary-mark-article-hook'."
9443   (when (memq gnus-current-article gnus-newsgroup-unreads)
9444     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9445
9446 (defun gnus-summary-mark-region-as-read (point mark all)
9447   "Mark all unread articles between point and mark as read.
9448 If given a prefix, mark all articles between point and mark as read,
9449 even ticked and dormant ones."
9450   (interactive "r\nP")
9451   (save-excursion
9452     (let (article)
9453       (goto-char point)
9454       (beginning-of-line)
9455       (while (and
9456               (< (point) mark)
9457               (progn
9458                 (when (or all
9459                           (memq (setq article (gnus-summary-article-number))
9460                                 gnus-newsgroup-unreads))
9461                   (gnus-summary-mark-article article gnus-del-mark))
9462                 t)
9463               (gnus-summary-find-next))))))
9464
9465 (defun gnus-summary-mark-below (score mark)
9466   "Mark articles with score less than SCORE with MARK."
9467   (interactive "P\ncMark: ")
9468   (setq score (if score
9469                   (prefix-numeric-value score)
9470                 (or gnus-summary-default-score 0)))
9471   (save-excursion
9472     (set-buffer gnus-summary-buffer)
9473     (goto-char (point-min))
9474     (while
9475         (progn
9476           (and (< (gnus-summary-article-score) score)
9477                (gnus-summary-mark-article nil mark))
9478           (gnus-summary-find-next)))))
9479
9480 (defun gnus-summary-kill-below (&optional score)
9481   "Mark articles with score below SCORE as read."
9482   (interactive "P")
9483   (gnus-summary-mark-below score gnus-killed-mark))
9484
9485 (defun gnus-summary-clear-above (&optional score)
9486   "Clear all marks from articles with score above SCORE."
9487   (interactive "P")
9488   (gnus-summary-mark-above score gnus-unread-mark))
9489
9490 (defun gnus-summary-tick-above (&optional score)
9491   "Tick all articles with score above SCORE."
9492   (interactive "P")
9493   (gnus-summary-mark-above score gnus-ticked-mark))
9494
9495 (defun gnus-summary-mark-above (score mark)
9496   "Mark articles with score over SCORE with MARK."
9497   (interactive "P\ncMark: ")
9498   (setq score (if score
9499                   (prefix-numeric-value score)
9500                 (or gnus-summary-default-score 0)))
9501   (save-excursion
9502     (set-buffer gnus-summary-buffer)
9503     (goto-char (point-min))
9504     (while (and (progn
9505                   (when (> (gnus-summary-article-score) score)
9506                     (gnus-summary-mark-article nil mark))
9507                   t)
9508                 (gnus-summary-find-next)))))
9509
9510 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9511 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9512 (defun gnus-summary-limit-include-expunged (&optional no-error)
9513   "Display all the hidden articles that were expunged for low scores."
9514   (interactive)
9515   (let ((buffer-read-only nil))
9516     (let ((scored gnus-newsgroup-scored)
9517           headers h)
9518       (while scored
9519         (unless (gnus-summary-article-header (caar scored))
9520           (and (setq h (gnus-number-to-header (caar scored)))
9521                (< (cdar scored) gnus-summary-expunge-below)
9522                (push h headers)))
9523         (setq scored (cdr scored)))
9524       (if (not headers)
9525           (when (not no-error)
9526             (error "No expunged articles hidden"))
9527         (goto-char (point-min))
9528         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9529         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9530         (mapcar (lambda (x) (push (mail-header-number x)
9531                                   gnus-newsgroup-limit))
9532                 headers)
9533         (gnus-summary-prepare-unthreaded (nreverse headers))
9534         (goto-char (point-min))
9535         (gnus-summary-position-point)
9536         t))))
9537
9538 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9539   "Mark all unread articles in this newsgroup as read.
9540 If prefix argument ALL is non-nil, ticked and dormant articles will
9541 also be marked as read.
9542 If QUIETLY is non-nil, no questions will be asked.
9543 If TO-HERE is non-nil, it should be a point in the buffer.  All
9544 articles before (after, if REVERSE is set) this point will be marked as read.
9545 Note that this function will only catch up the unread article
9546 in the current summary buffer limitation.
9547 The number of articles marked as read is returned."
9548   (interactive "P")
9549   (prog1
9550       (save-excursion
9551         (when (or quietly
9552                   (not gnus-interactive-catchup) ;Without confirmation?
9553                   gnus-expert-user
9554                   (gnus-y-or-n-p
9555                    (if all
9556                        "Mark absolutely all articles as read? "
9557                      "Mark all unread articles as read? ")))
9558           (if (and not-mark
9559                    (not gnus-newsgroup-adaptive)
9560                    (not gnus-newsgroup-auto-expire)
9561                    (not gnus-suppress-duplicates)
9562                    (or (not gnus-use-cache)
9563                        (eq gnus-use-cache 'passive)))
9564               (progn
9565                 (when all
9566                   (setq gnus-newsgroup-marked nil
9567                         gnus-newsgroup-dormant nil))
9568                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9569             ;; We actually mark all articles as canceled, which we
9570             ;; have to do when using auto-expiry or adaptive scoring.
9571             (gnus-summary-show-all-threads)
9572             (if (and to-here reverse)
9573                 (progn
9574                   (goto-char to-here)
9575                   (while (and
9576                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9577                           (gnus-summary-find-next (not all) nil nil t))))
9578               (when (gnus-summary-first-subject (not all) t)
9579                 (while (and
9580                         (if to-here (< (point) to-here) t)
9581                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9582                         (gnus-summary-find-next (not all) nil nil t)))))
9583             (gnus-set-mode-line 'summary))
9584           t))
9585     (gnus-summary-position-point)))
9586
9587 (defun gnus-summary-catchup-to-here (&optional all)
9588   "Mark all unticked articles before the current one as read.
9589 If ALL is non-nil, also mark ticked and dormant articles as read."
9590   (interactive "P")
9591   (save-excursion
9592     (gnus-save-hidden-threads
9593       (let ((beg (point)))
9594         ;; We check that there are unread articles.
9595         (when (or all (gnus-summary-find-prev))
9596           (gnus-summary-catchup all t beg)))))
9597   (gnus-summary-position-point))
9598
9599 (defun gnus-summary-catchup-from-here (&optional all)
9600   "Mark all unticked articles after the current one as read.
9601 If ALL is non-nil, also mark ticked and dormant articles as read."
9602   (interactive "P")
9603   (save-excursion
9604     (gnus-save-hidden-threads
9605       (let ((beg (point)))
9606         ;; We check that there are unread articles.
9607         (when (or all (gnus-summary-find-next))
9608           (gnus-summary-catchup all t beg nil t)))))
9609
9610   (gnus-summary-position-point))
9611 (defun gnus-summary-catchup-all (&optional quietly)
9612   "Mark all articles in this newsgroup as read."
9613   (interactive "P")
9614   (gnus-summary-catchup t quietly))
9615
9616 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9617   "Mark all unread articles in this group as read, then exit.
9618 If prefix argument ALL is non-nil, all articles are marked as read.
9619 If QUIETLY is non-nil, no questions will be asked."
9620   (interactive "P")
9621   (when (gnus-summary-catchup all quietly nil 'fast)
9622     ;; Select next newsgroup or exit.
9623     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9624              (eq gnus-auto-select-next 'quietly))
9625         (gnus-summary-next-group nil)
9626       (gnus-summary-exit))))
9627
9628 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9629   "Mark all articles in this newsgroup as read, and then exit."
9630   (interactive "P")
9631   (gnus-summary-catchup-and-exit t quietly))
9632
9633 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9634   "Mark all articles in this group as read and select the next group.
9635 If given a prefix, mark all articles, unread as well as ticked, as
9636 read."
9637   (interactive "P")
9638   (save-excursion
9639     (gnus-summary-catchup all))
9640   (gnus-summary-next-group))
9641
9642 ;;;
9643 ;;; with article
9644 ;;;
9645
9646 (defmacro gnus-with-article (article &rest forms)
9647   "Select ARTICLE and perform FORMS in the original article buffer.
9648 Then replace the article with the result."
9649   `(progn
9650      ;; We don't want the article to be marked as read.
9651      (let (gnus-mark-article-hook)
9652        (gnus-summary-select-article t t nil ,article))
9653      (set-buffer gnus-original-article-buffer)
9654      ,@forms
9655      (if (not (gnus-check-backend-function
9656                'request-replace-article (car gnus-article-current)))
9657          (gnus-message 5 "Read-only group; not replacing")
9658        (unless (gnus-request-replace-article
9659                 ,article (car gnus-article-current)
9660                 (current-buffer) t)
9661          (error "Couldn't replace article")))
9662      ;; The cache and backlog have to be flushed somewhat.
9663      (when gnus-keep-backlog
9664        (gnus-backlog-remove-article
9665         (car gnus-article-current) (cdr gnus-article-current)))
9666      (when gnus-use-cache
9667        (gnus-cache-update-article
9668         (car gnus-article-current) (cdr gnus-article-current)))))
9669
9670 (put 'gnus-with-article 'lisp-indent-function 1)
9671 (put 'gnus-with-article 'edebug-form-spec '(form body))
9672
9673 ;; Thread-based commands.
9674
9675 (defun gnus-summary-articles-in-thread (&optional article)
9676   "Return a list of all articles in the current thread.
9677 If ARTICLE is non-nil, return all articles in the thread that starts
9678 with that article."
9679   (let* ((article (or article (gnus-summary-article-number)))
9680          (data (gnus-data-find-list article))
9681          (top-level (gnus-data-level (car data)))
9682          (top-subject
9683           (cond ((null gnus-thread-operation-ignore-subject)
9684                  (gnus-simplify-subject-re
9685                   (mail-header-subject (gnus-data-header (car data)))))
9686                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9687                  (gnus-simplify-subject-fuzzy
9688                   (mail-header-subject (gnus-data-header (car data)))))
9689                 (t nil)))
9690          (end-point (save-excursion
9691                       (if (gnus-summary-go-to-next-thread)
9692                           (point) (point-max))))
9693          articles)
9694     (while (and data
9695                 (< (gnus-data-pos (car data)) end-point))
9696       (when (or (not top-subject)
9697                 (string= top-subject
9698                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9699                              (gnus-simplify-subject-fuzzy
9700                               (mail-header-subject
9701                                (gnus-data-header (car data))))
9702                            (gnus-simplify-subject-re
9703                             (mail-header-subject
9704                              (gnus-data-header (car data)))))))
9705         (push (gnus-data-number (car data)) articles))
9706       (unless (and (setq data (cdr data))
9707                    (> (gnus-data-level (car data)) top-level))
9708         (setq data nil)))
9709     ;; Return the list of articles.
9710     (nreverse articles)))
9711
9712 (defun gnus-summary-rethread-current ()
9713   "Rethread the thread the current article is part of."
9714   (interactive)
9715   (let* ((gnus-show-threads t)
9716          (article (gnus-summary-article-number))
9717          (id (mail-header-id (gnus-summary-article-header)))
9718          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9719     (unless id
9720       (error "No article on the current line"))
9721     (gnus-rebuild-thread id)
9722     (gnus-summary-goto-subject article)))
9723
9724 (defun gnus-summary-reparent-thread ()
9725   "Make the current article child of the marked (or previous) article.
9726
9727 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9728 is non-nil or the Subject: of both articles are the same."
9729   (interactive)
9730   (unless (not (gnus-group-read-only-p))
9731     (error "The current newsgroup does not support article editing"))
9732   (unless (<= (length gnus-newsgroup-processable) 1)
9733     (error "No more than one article may be marked"))
9734   (save-window-excursion
9735     (let ((gnus-article-buffer " *reparent*")
9736           (current-article (gnus-summary-article-number))
9737           ;; First grab the marked article, otherwise one line up.
9738           (parent-article (if (not (null gnus-newsgroup-processable))
9739                               (car gnus-newsgroup-processable)
9740                             (save-excursion
9741                               (if (eq (forward-line -1) 0)
9742                                   (gnus-summary-article-number)
9743                                 (error "Beginning of summary buffer"))))))
9744       (unless (not (eq current-article parent-article))
9745         (error "An article may not be self-referential"))
9746       (let ((message-id (mail-header-id
9747                          (gnus-summary-article-header parent-article))))
9748         (unless (and message-id (not (equal message-id "")))
9749           (error "No message-id in desired parent"))
9750         (gnus-with-article current-article
9751           (save-restriction
9752             (goto-char (point-min))
9753             (message-narrow-to-head)
9754             (if (re-search-forward "^References: " nil t)
9755                 (progn
9756                   (re-search-forward "^[^ \t]" nil t)
9757                   (forward-line -1)
9758                   (end-of-line)
9759                   (insert " " message-id))
9760               (insert "References: " message-id "\n"))))
9761         (set-buffer gnus-summary-buffer)
9762         (gnus-summary-unmark-all-processable)
9763         (gnus-summary-update-article current-article)
9764         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9765             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
9766         (gnus-summary-rethread-current)
9767         (gnus-message 3 "Article %d is now the child of article %d"
9768                       current-article parent-article)))))
9769
9770 (defun gnus-summary-toggle-threads (&optional arg)
9771   "Toggle showing conversation threads.
9772 If ARG is positive number, turn showing conversation threads on."
9773   (interactive "P")
9774   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9775     (setq gnus-show-threads
9776           (if (null arg) (not gnus-show-threads)
9777             (> (prefix-numeric-value arg) 0)))
9778     (gnus-summary-prepare)
9779     (gnus-summary-goto-subject current)
9780     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9781     (gnus-summary-position-point)))
9782
9783 (defun gnus-summary-show-all-threads ()
9784   "Show all threads."
9785   (interactive)
9786   (save-excursion
9787     (let ((buffer-read-only nil))
9788       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9789   (gnus-summary-position-point))
9790
9791 (defun gnus-summary-show-thread ()
9792   "Show thread subtrees.
9793 Returns nil if no thread was there to be shown."
9794   (interactive)
9795   (let ((buffer-read-only nil)
9796         (orig (point))
9797         ;; first goto end then to beg, to have point at beg after let
9798         (end (progn (end-of-line) (point)))
9799         (beg (progn (beginning-of-line) (point))))
9800     (prog1
9801         ;; Any hidden lines here?
9802         (search-forward "\r" end t)
9803       (subst-char-in-region beg end ?\^M ?\n t)
9804       (goto-char orig)
9805       (gnus-summary-position-point))))
9806
9807 (defun gnus-summary-maybe-hide-threads ()
9808   "If requested, hide the threads that should be hidden."
9809   (when (and gnus-show-threads
9810              gnus-thread-hide-subtree)
9811     (gnus-summary-hide-all-threads
9812      (if (or (consp gnus-thread-hide-subtree)
9813              (gnus-functionp gnus-thread-hide-subtree))
9814          (gnus-make-predicate gnus-thread-hide-subtree)
9815        nil))))
9816
9817 ;;; Hiding predicates.
9818
9819 (defun gnus-article-unread-p (header)
9820   (memq (mail-header-number header) gnus-newsgroup-unreads))
9821
9822 (defun gnus-article-unseen-p (header)
9823   (memq (mail-header-number header) gnus-newsgroup-unseen))
9824
9825 (defun gnus-map-articles (predicate articles)
9826   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
9827   (apply 'gnus-or (mapcar predicate
9828                           (mapcar 'gnus-summary-article-header articles))))
9829
9830 (defun gnus-summary-hide-all-threads (&optional predicate)
9831   "Hide all thread subtrees.
9832 If PREDICATE is supplied, threads that satisfy this predicate
9833 will not be hidden."
9834   (interactive)
9835   (save-excursion
9836     (goto-char (point-min))
9837     (let ((end nil))
9838       (while (not end)
9839         (when (or (not predicate)
9840                   (gnus-map-articles
9841                    predicate (gnus-summary-article-children)))
9842             (gnus-summary-hide-thread))
9843         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
9844   (gnus-summary-position-point))
9845
9846 (defun gnus-summary-hide-thread ()
9847   "Hide thread subtrees.
9848 If PREDICATE is supplied, threads that satisfy this predicate
9849 will not be hidden.
9850 Returns nil if no threads were there to be hidden."
9851   (interactive)
9852   (let ((buffer-read-only nil)
9853         (start (point))
9854         (article (gnus-summary-article-number)))
9855     (goto-char start)
9856     ;; Go forward until either the buffer ends or the subthread
9857     ;; ends.
9858     (when (and (not (eobp))
9859                (or (zerop (gnus-summary-next-thread 1 t))
9860                    (goto-char (point-max))))
9861       (prog1
9862           (if (and (> (point) start)
9863                    (search-backward "\n" start t))
9864               (progn
9865                 (subst-char-in-region start (point) ?\n ?\^M)
9866                 (gnus-summary-goto-subject article))
9867             (goto-char start)
9868             nil)))))
9869
9870 (defun gnus-summary-go-to-next-thread (&optional previous)
9871   "Go to the same level (or less) next thread.
9872 If PREVIOUS is non-nil, go to previous thread instead.
9873 Return the article number moved to, or nil if moving was impossible."
9874   (let ((level (gnus-summary-thread-level))
9875         (way (if previous -1 1))
9876         (beg (point)))
9877     (forward-line way)
9878     (while (and (not (eobp))
9879                 (< level (gnus-summary-thread-level)))
9880       (forward-line way))
9881     (if (eobp)
9882         (progn
9883           (goto-char beg)
9884           nil)
9885       (setq beg (point))
9886       (prog1
9887           (gnus-summary-article-number)
9888         (goto-char beg)))))
9889
9890 (defun gnus-summary-next-thread (n &optional silent)
9891   "Go to the same level next N'th thread.
9892 If N is negative, search backward instead.
9893 Returns the difference between N and the number of skips actually
9894 done.
9895
9896 If SILENT, don't output messages."
9897   (interactive "p")
9898   (let ((backward (< n 0))
9899         (n (abs n)))
9900     (while (and (> n 0)
9901                 (gnus-summary-go-to-next-thread backward))
9902       (decf n))
9903     (unless silent
9904       (gnus-summary-position-point))
9905     (when (and (not silent) (/= 0 n))
9906       (gnus-message 7 "No more threads"))
9907     n))
9908
9909 (defun gnus-summary-prev-thread (n)
9910   "Go to the same level previous N'th thread.
9911 Returns the difference between N and the number of skips actually
9912 done."
9913   (interactive "p")
9914   (gnus-summary-next-thread (- n)))
9915
9916 (defun gnus-summary-go-down-thread ()
9917   "Go down one level in the current thread."
9918   (let ((children (gnus-summary-article-children)))
9919     (when children
9920       (gnus-summary-goto-subject (car children)))))
9921
9922 (defun gnus-summary-go-up-thread ()
9923   "Go up one level in the current thread."
9924   (let ((parent (gnus-summary-article-parent)))
9925     (when parent
9926       (gnus-summary-goto-subject parent))))
9927
9928 (defun gnus-summary-down-thread (n)
9929   "Go down thread N steps.
9930 If N is negative, go up instead.
9931 Returns the difference between N and how many steps down that were
9932 taken."
9933   (interactive "p")
9934   (let ((up (< n 0))
9935         (n (abs n)))
9936     (while (and (> n 0)
9937                 (if up (gnus-summary-go-up-thread)
9938                   (gnus-summary-go-down-thread)))
9939       (setq n (1- n)))
9940     (gnus-summary-position-point)
9941     (when (/= 0 n)
9942       (gnus-message 7 "Can't go further"))
9943     n))
9944
9945 (defun gnus-summary-up-thread (n)
9946   "Go up thread N steps.
9947 If N is negative, go down instead.
9948 Returns the difference between N and how many steps down that were
9949 taken."
9950   (interactive "p")
9951   (gnus-summary-down-thread (- n)))
9952
9953 (defun gnus-summary-top-thread ()
9954   "Go to the top of the thread."
9955   (interactive)
9956   (while (gnus-summary-go-up-thread))
9957   (gnus-summary-article-number))
9958
9959 (defun gnus-summary-kill-thread (&optional unmark)
9960   "Mark articles under current thread as read.
9961 If the prefix argument is positive, remove any kinds of marks.
9962 If the prefix argument is negative, tick articles instead."
9963   (interactive "P")
9964   (when unmark
9965     (setq unmark (prefix-numeric-value unmark)))
9966   (let ((articles (gnus-summary-articles-in-thread)))
9967     (save-excursion
9968       ;; Expand the thread.
9969       (gnus-summary-show-thread)
9970       ;; Mark all the articles.
9971       (while articles
9972         (gnus-summary-goto-subject (car articles))
9973         (cond ((null unmark)
9974                (gnus-summary-mark-article-as-read gnus-killed-mark))
9975               ((> unmark 0)
9976                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9977               (t
9978                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9979         (setq articles (cdr articles))))
9980     ;; Hide killed subtrees.
9981     (and (null unmark)
9982          gnus-thread-hide-killed
9983          (gnus-summary-hide-thread))
9984     ;; If marked as read, go to next unread subject.
9985     (when (null unmark)
9986       ;; Go to next unread subject.
9987       (gnus-summary-next-subject 1 t)))
9988   (gnus-set-mode-line 'summary))
9989
9990 ;; Summary sorting commands
9991
9992 (defun gnus-summary-sort-by-number (&optional reverse)
9993   "Sort the summary buffer by article number.
9994 Argument REVERSE means reverse order."
9995   (interactive "P")
9996   (gnus-summary-sort 'number reverse))
9997
9998 (defun gnus-summary-sort-by-author (&optional reverse)
9999   "Sort the summary buffer by author name alphabetically.
10000 If `case-fold-search' is non-nil, case of letters is ignored.
10001 Argument REVERSE means reverse order."
10002   (interactive "P")
10003   (gnus-summary-sort 'author reverse))
10004
10005 (defun gnus-summary-sort-by-subject (&optional reverse)
10006   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10007 If `case-fold-search' is non-nil, case of letters is ignored.
10008 Argument REVERSE means reverse order."
10009   (interactive "P")
10010   (gnus-summary-sort 'subject reverse))
10011
10012 (defun gnus-summary-sort-by-date (&optional reverse)
10013   "Sort the summary buffer by date.
10014 Argument REVERSE means reverse order."
10015   (interactive "P")
10016   (gnus-summary-sort 'date reverse))
10017
10018 (defun gnus-summary-sort-by-score (&optional reverse)
10019   "Sort the summary buffer by score.
10020 Argument REVERSE means reverse order."
10021   (interactive "P")
10022   (gnus-summary-sort 'score reverse))
10023
10024 (defun gnus-summary-sort-by-lines (&optional reverse)
10025   "Sort the summary buffer by the number of lines.
10026 Argument REVERSE means reverse order."
10027   (interactive "P")
10028   (gnus-summary-sort 'lines reverse))
10029
10030 (defun gnus-summary-sort-by-chars (&optional reverse)
10031   "Sort the summary buffer by article length.
10032 Argument REVERSE means reverse order."
10033   (interactive "P")
10034   (gnus-summary-sort 'chars reverse))
10035
10036 (defun gnus-summary-sort-by-original (&optional reverse)
10037   "Sort the summary buffer using the default sorting method.
10038 Argument REVERSE means reverse order."
10039   (interactive "P")
10040   (let* ((buffer-read-only)
10041          (gnus-summary-prepare-hook nil))
10042     ;; We do the sorting by regenerating the threads.
10043     (gnus-summary-prepare)
10044     ;; Hide subthreads if needed.
10045     (gnus-summary-maybe-hide-threads)))
10046
10047 (defun gnus-summary-sort (predicate reverse)
10048   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10049   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10050          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10051          (gnus-thread-sort-functions
10052           (if (not reverse)
10053               thread
10054             `(lambda (t1 t2)
10055                (,thread t2 t1))))
10056          (gnus-sort-gathered-threads-function
10057           gnus-thread-sort-functions)
10058          (gnus-article-sort-functions
10059           (if (not reverse)
10060               article
10061             `(lambda (t1 t2)
10062                (,article t2 t1))))
10063          (buffer-read-only)
10064          (gnus-summary-prepare-hook nil))
10065     ;; We do the sorting by regenerating the threads.
10066     (gnus-summary-prepare)
10067     ;; Hide subthreads if needed.
10068     (gnus-summary-maybe-hide-threads)))
10069
10070 ;; Summary saving commands.
10071
10072 (defun gnus-summary-save-article (&optional n not-saved)
10073   "Save the current article using the default saver function.
10074 If N is a positive number, save the N next articles.
10075 If N is a negative number, save the N previous articles.
10076 If N is nil and any articles have been marked with the process mark,
10077 save those articles instead.
10078 The variable `gnus-default-article-saver' specifies the saver function."
10079   (interactive "P")
10080   (let* ((articles (gnus-summary-work-articles n))
10081          (save-buffer (save-excursion
10082                         (nnheader-set-temp-buffer " *Gnus Save*")))
10083          (num (length articles))
10084          header file)
10085     (dolist (article articles)
10086       (setq header (gnus-summary-article-header article))
10087       (if (not (vectorp header))
10088           ;; This is a pseudo-article.
10089           (if (assq 'name header)
10090               (gnus-copy-file (cdr (assq 'name header)))
10091             (gnus-message 1 "Article %d is unsaveable" article))
10092         ;; This is a real article.
10093         (save-window-excursion
10094           (let ((gnus-display-mime-function nil)
10095                 (gnus-article-prepare-hook nil))
10096             (gnus-summary-select-article t nil nil article)))
10097         (save-excursion
10098           (set-buffer save-buffer)
10099           (erase-buffer)
10100           (insert-buffer-substring gnus-original-article-buffer))
10101         (setq file (gnus-article-save save-buffer file num))
10102         (gnus-summary-remove-process-mark article)
10103         (unless not-saved
10104           (gnus-summary-set-saved-mark article))))
10105     (gnus-kill-buffer save-buffer)
10106     (gnus-summary-position-point)
10107     (gnus-set-mode-line 'summary)
10108     n))
10109
10110 (defun gnus-summary-pipe-output (&optional arg)
10111   "Pipe the current article to a subprocess.
10112 If N is a positive number, pipe the N next articles.
10113 If N is a negative number, pipe the N previous articles.
10114 If N is nil and any articles have been marked with the process mark,
10115 pipe those articles instead."
10116   (interactive "P")
10117   (require 'gnus-art)
10118   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
10119     (gnus-summary-save-article arg t))
10120   (let ((buffer (get-buffer "*Shell Command Output*")))
10121     (if (and buffer
10122              (with-current-buffer buffer (> (point-max) (point-min))))
10123         (gnus-configure-windows 'pipe))))
10124
10125 (defun gnus-summary-save-article-mail (&optional arg)
10126   "Append the current article to an mail file.
10127 If N is a positive number, save the N next articles.
10128 If N is a negative number, save the N previous articles.
10129 If N is nil and any articles have been marked with the process mark,
10130 save those articles instead."
10131   (interactive "P")
10132   (require 'gnus-art)
10133   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10134     (gnus-summary-save-article arg)))
10135
10136 (defun gnus-summary-save-article-rmail (&optional arg)
10137   "Append the current article to an rmail file.
10138 If N is a positive number, save the N next articles.
10139 If N is a negative number, save the N previous articles.
10140 If N is nil and any articles have been marked with the process mark,
10141 save those articles instead."
10142   (interactive "P")
10143   (require 'gnus-art)
10144   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10145     (gnus-summary-save-article arg)))
10146
10147 (defun gnus-summary-save-article-file (&optional arg)
10148   "Append the current article to a file.
10149 If N is a positive number, save the N next articles.
10150 If N is a negative number, save the N previous articles.
10151 If N is nil and any articles have been marked with the process mark,
10152 save those articles instead."
10153   (interactive "P")
10154   (require 'gnus-art)
10155   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10156     (gnus-summary-save-article arg)))
10157
10158 (defun gnus-summary-write-article-file (&optional arg)
10159   "Write the current article to a file, deleting the previous file.
10160 If N is a positive number, save the N next articles.
10161 If N is a negative number, save the N previous articles.
10162 If N is nil and any articles have been marked with the process mark,
10163 save those articles instead."
10164   (interactive "P")
10165   (require 'gnus-art)
10166   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10167     (gnus-summary-save-article arg)))
10168
10169 (defun gnus-summary-save-article-body-file (&optional arg)
10170   "Append the current article body to a file.
10171 If N is a positive number, save the N next articles.
10172 If N is a negative number, save the N previous articles.
10173 If N is nil and any articles have been marked with the process mark,
10174 save those articles instead."
10175   (interactive "P")
10176   (require 'gnus-art)
10177   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10178     (gnus-summary-save-article arg)))
10179
10180 (defun gnus-summary-muttprint (&optional arg)
10181   "Print the current article using Muttprint.
10182 If N is a positive number, save the N next articles.
10183 If N is a negative number, save the N previous articles.
10184 If N is nil and any articles have been marked with the process mark,
10185 save those articles instead."
10186   (interactive "P")
10187   (require 'gnus-art)
10188   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10189     (gnus-summary-save-article arg t)))
10190
10191 (defun gnus-summary-pipe-message (program)
10192   "Pipe the current article through PROGRAM."
10193   (interactive "sProgram: ")
10194   (gnus-summary-select-article)
10195   (let ((mail-header-separator ""))
10196     (gnus-eval-in-buffer-window gnus-article-buffer
10197       (save-restriction
10198         (widen)
10199         (let ((start (window-start))
10200               buffer-read-only)
10201           (message-pipe-buffer-body program)
10202           (set-window-start (get-buffer-window (current-buffer)) start))))))
10203
10204 (defun gnus-get-split-value (methods)
10205   "Return a value based on the split METHODS."
10206   (let (split-name method result match)
10207     (when methods
10208       (save-excursion
10209         (set-buffer gnus-original-article-buffer)
10210         (save-restriction
10211           (nnheader-narrow-to-headers)
10212           (while (and methods (not split-name))
10213             (goto-char (point-min))
10214             (setq method (pop methods))
10215             (setq match (car method))
10216             (when (cond
10217                    ((stringp match)
10218                     ;; Regular expression.
10219                     (ignore-errors
10220                       (re-search-forward match nil t)))
10221                    ((gnus-functionp match)
10222                     ;; Function.
10223                     (save-restriction
10224                       (widen)
10225                       (setq result (funcall match gnus-newsgroup-name))))
10226                    ((consp match)
10227                     ;; Form.
10228                     (save-restriction
10229                       (widen)
10230                       (setq result (eval match)))))
10231               (setq split-name (cdr method))
10232               (cond ((stringp result)
10233                      (push (expand-file-name
10234                             result gnus-article-save-directory)
10235                            split-name))
10236                     ((consp result)
10237                      (setq split-name (append result split-name)))))))))
10238     (nreverse split-name)))
10239
10240 (defun gnus-valid-move-group-p (group)
10241   (and (boundp group)
10242        (symbol-name group)
10243        (symbol-value group)
10244        (gnus-get-function (gnus-find-method-for-group
10245                            (symbol-name group)) 'request-accept-article t)))
10246
10247 (defun gnus-read-move-group-name (prompt default articles prefix)
10248   "Read a group name."
10249   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10250          (minibuffer-confirm-incomplete nil) ; XEmacs
10251          (prom
10252           (format "%s %s to:"
10253                   prompt
10254                   (if (> (length articles) 1)
10255                       (format "these %d articles" (length articles))
10256                     "this article")))
10257          (to-newsgroup
10258           (cond
10259            ((null split-name)
10260             (gnus-completing-read-with-default
10261              default prom
10262              gnus-active-hashtb
10263              'gnus-valid-move-group-p
10264              nil prefix
10265              'gnus-group-history))
10266            ((= 1 (length split-name))
10267             (gnus-completing-read-with-default
10268              (car split-name) prom
10269              gnus-active-hashtb
10270              'gnus-valid-move-group-p
10271              nil nil
10272              'gnus-group-history))
10273            (t
10274             (gnus-completing-read-with-default
10275              nil prom
10276              (mapcar (lambda (el) (list el))
10277                      (nreverse split-name))
10278              nil nil nil
10279              'gnus-group-history))))
10280          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10281     (when to-newsgroup
10282       (if (or (string= to-newsgroup "")
10283               (string= to-newsgroup prefix))
10284           (setq to-newsgroup default))
10285       (unless to-newsgroup
10286         (error "No group name entered"))
10287       (or (gnus-active to-newsgroup)
10288           (gnus-activate-group to-newsgroup nil nil to-method)
10289           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10290                                      to-newsgroup))
10291               (or (and (gnus-request-create-group to-newsgroup to-method)
10292                        (gnus-activate-group
10293                         to-newsgroup nil nil to-method)
10294                        (gnus-subscribe-group to-newsgroup))
10295                   (error "Couldn't create group %s" to-newsgroup)))
10296           (error "No such group: %s" to-newsgroup)))
10297     to-newsgroup))
10298
10299 (defun gnus-summary-save-parts (type dir n &optional reverse)
10300   "Save parts matching TYPE to DIR.
10301 If REVERSE, save parts that do not match TYPE."
10302   (interactive
10303    (list (read-string "Save parts of type: "
10304                       (or (car gnus-summary-save-parts-type-history)
10305                           gnus-summary-save-parts-default-mime)
10306                       'gnus-summary-save-parts-type-history)
10307          (setq gnus-summary-save-parts-last-directory
10308                (read-file-name "Save to directory: "
10309                                gnus-summary-save-parts-last-directory
10310                                nil t))
10311          current-prefix-arg))
10312   (gnus-summary-iterate n
10313     (let ((gnus-display-mime-function nil)
10314           (gnus-inhibit-treatment t))
10315       (gnus-summary-select-article))
10316     (save-excursion
10317       (set-buffer gnus-article-buffer)
10318       (let ((handles (or gnus-article-mime-handles
10319                          (mm-dissect-buffer) (mm-uu-dissect))))
10320         (when handles
10321           (gnus-summary-save-parts-1 type dir handles reverse)
10322           (unless gnus-article-mime-handles ;; Don't destroy this case.
10323             (mm-destroy-parts handles)))))))
10324
10325 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10326   (if (stringp (car handle))
10327       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10328               (cdr handle))
10329     (when (if reverse
10330               (not (string-match type (mm-handle-media-type handle)))
10331             (string-match type (mm-handle-media-type handle)))
10332       (let ((file (expand-file-name
10333                    (file-name-nondirectory
10334                     (or
10335                      (mail-content-type-get
10336                       (mm-handle-disposition handle) 'filename)
10337                      (concat gnus-newsgroup-name
10338                              "." (number-to-string
10339                                   (cdr gnus-article-current)))))
10340                    dir)))
10341         (unless (file-exists-p file)
10342           (mm-save-part-to-file handle file))))))
10343
10344 ;; Summary extract commands
10345
10346 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10347   (let ((buffer-read-only nil)
10348         (article (gnus-summary-article-number))
10349         after-article b e)
10350     (unless (gnus-summary-goto-subject article)
10351       (error "No such article: %d" article))
10352     (gnus-summary-position-point)
10353     ;; If all commands are to be bunched up on one line, we collect
10354     ;; them here.
10355     (unless gnus-view-pseudos-separately
10356       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10357             files action)
10358         (while ps
10359           (setq action (cdr (assq 'action (car ps))))
10360           (setq files (list (cdr (assq 'name (car ps)))))
10361           (while (and ps (cdr ps)
10362                       (string= (or action "1")
10363                                (or (cdr (assq 'action (cadr ps))) "2")))
10364             (push (cdr (assq 'name (cadr ps))) files)
10365             (setcdr ps (cddr ps)))
10366           (when files
10367             (when (not (string-match "%s" action))
10368               (push " " files))
10369             (push " " files)
10370             (when (assq 'execute (car ps))
10371               (setcdr (assq 'execute (car ps))
10372                       (funcall (if (string-match "%s" action)
10373                                    'format 'concat)
10374                                action
10375                                (mapconcat
10376                                 (lambda (f)
10377                                   (if (equal f " ")
10378                                       f
10379                                     (mm-quote-arg f)))
10380                                 files " ")))))
10381           (setq ps (cdr ps)))))
10382     (if (and gnus-view-pseudos (not not-view))
10383         (while pslist
10384           (when (assq 'execute (car pslist))
10385             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10386                                   (eq gnus-view-pseudos 'not-confirm)))
10387           (setq pslist (cdr pslist)))
10388       (save-excursion
10389         (while pslist
10390           (setq after-article (or (cdr (assq 'article (car pslist)))
10391                                   (gnus-summary-article-number)))
10392           (gnus-summary-goto-subject after-article)
10393           (forward-line 1)
10394           (setq b (point))
10395           (insert "    " (file-name-nondirectory
10396                           (cdr (assq 'name (car pslist))))
10397                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10398           (setq e (point))
10399           (forward-line -1)             ; back to `b'
10400           (gnus-add-text-properties
10401            b (1- e) (list 'gnus-number gnus-reffed-article-number
10402                           gnus-mouse-face-prop gnus-mouse-face))
10403           (gnus-data-enter
10404            after-article gnus-reffed-article-number
10405            gnus-unread-mark b (car pslist) 0 (- e b))
10406           (push gnus-reffed-article-number gnus-newsgroup-unreads)
10407           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10408           (setq pslist (cdr pslist)))))))
10409
10410 (defun gnus-pseudos< (p1 p2)
10411   (let ((c1 (cdr (assq 'action p1)))
10412         (c2 (cdr (assq 'action p2))))
10413     (and c1 c2 (string< c1 c2))))
10414
10415 (defun gnus-request-pseudo-article (props)
10416   (cond ((assq 'execute props)
10417          (gnus-execute-command (cdr (assq 'execute props)))))
10418   (let ((gnus-current-article (gnus-summary-article-number)))
10419     (gnus-run-hooks 'gnus-mark-article-hook)))
10420
10421 (defun gnus-execute-command (command &optional automatic)
10422   (save-excursion
10423     (gnus-article-setup-buffer)
10424     (set-buffer gnus-article-buffer)
10425     (setq buffer-read-only nil)
10426     (let ((command (if automatic command
10427                      (read-string "Command: " (cons command 0)))))
10428       (erase-buffer)
10429       (insert "$ " command "\n\n")
10430       (if gnus-view-pseudo-asynchronously
10431           (start-process "gnus-execute" (current-buffer) shell-file-name
10432                          shell-command-switch command)
10433         (call-process shell-file-name nil t nil
10434                       shell-command-switch command)))))
10435
10436 ;; Summary kill commands.
10437
10438 (defun gnus-summary-edit-global-kill (article)
10439   "Edit the \"global\" kill file."
10440   (interactive (list (gnus-summary-article-number)))
10441   (gnus-group-edit-global-kill article))
10442
10443 (defun gnus-summary-edit-local-kill ()
10444   "Edit a local kill file applied to the current newsgroup."
10445   (interactive)
10446   (setq gnus-current-headers (gnus-summary-article-header))
10447   (gnus-group-edit-local-kill
10448    (gnus-summary-article-number) gnus-newsgroup-name))
10449
10450 ;;; Header reading.
10451
10452 (defun gnus-read-header (id &optional header)
10453   "Read the headers of article ID and enter them into the Gnus system."
10454   (let ((group gnus-newsgroup-name)
10455         (gnus-override-method
10456          (or
10457           gnus-override-method
10458           (and (gnus-news-group-p gnus-newsgroup-name)
10459                (car (gnus-refer-article-methods)))))
10460         where)
10461     ;; First we check to see whether the header in question is already
10462     ;; fetched.
10463     (if (stringp id)
10464         ;; This is a Message-ID.
10465         (setq header (or header (gnus-id-to-header id)))
10466       ;; This is an article number.
10467       (setq header (or header (gnus-summary-article-header id))))
10468     (if (and header
10469              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10470         ;; We have found the header.
10471         header
10472       ;; If this is a sparse article, we have to nix out its
10473       ;; previous entry in the thread hashtb.
10474       (when (and header
10475                  (gnus-summary-article-sparse-p (mail-header-number header)))
10476         (let* ((parent (gnus-parent-id (mail-header-references header)))
10477                (thread (and parent (gnus-id-to-thread parent))))
10478           (when thread
10479             (delq (assq header thread) thread))))
10480       ;; We have to really fetch the header to this article.
10481       (save-excursion
10482         (set-buffer nntp-server-buffer)
10483         (when (setq where (gnus-request-head id group))
10484           (nnheader-fold-continuation-lines)
10485           (goto-char (point-max))
10486           (insert ".\n")
10487           (goto-char (point-min))
10488           (insert "211 ")
10489           (princ (cond
10490                   ((numberp id) id)
10491                   ((cdr where) (cdr where))
10492                   (header (mail-header-number header))
10493                   (t gnus-reffed-article-number))
10494                  (current-buffer))
10495           (insert " Article retrieved.\n"))
10496         (if (or (not where)
10497                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10498             ()                          ; Malformed head.
10499           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10500             (when (and (stringp id)
10501                        (not (string= (gnus-group-real-name group)
10502                                      (car where))))
10503               ;; If we fetched by Message-ID and the article came
10504               ;; from a different group, we fudge some bogus article
10505               ;; numbers for this article.
10506               (mail-header-set-number header gnus-reffed-article-number))
10507             (save-excursion
10508               (set-buffer gnus-summary-buffer)
10509               (decf gnus-reffed-article-number)
10510               (gnus-remove-header (mail-header-number header))
10511               (push header gnus-newsgroup-headers)
10512               (setq gnus-current-headers header)
10513               (push (mail-header-number header) gnus-newsgroup-limit)))
10514           header)))))
10515
10516 (defun gnus-remove-header (number)
10517   "Remove header NUMBER from `gnus-newsgroup-headers'."
10518   (if (and gnus-newsgroup-headers
10519            (= number (mail-header-number (car gnus-newsgroup-headers))))
10520       (pop gnus-newsgroup-headers)
10521     (let ((headers gnus-newsgroup-headers))
10522       (while (and (cdr headers)
10523                   (not (= number (mail-header-number (cadr headers)))))
10524         (pop headers))
10525       (when (cdr headers)
10526         (setcdr headers (cddr headers))))))
10527
10528 ;;;
10529 ;;; summary highlights
10530 ;;;
10531
10532 (defun gnus-highlight-selected-summary ()
10533   "Highlight selected article in summary buffer."
10534   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10535   (when gnus-summary-selected-face
10536     (save-excursion
10537       (let* ((beg (progn (beginning-of-line) (point)))
10538              (end (progn (end-of-line) (point)))
10539              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10540              (from (if (get-text-property beg gnus-mouse-face-prop)
10541                        beg
10542                      (or (next-single-property-change
10543                           beg gnus-mouse-face-prop nil end)
10544                          beg)))
10545              (to
10546               (if (= from end)
10547                   (- from 2)
10548                 (or (next-single-property-change
10549                      from gnus-mouse-face-prop nil end)
10550                     end))))
10551         ;; If no mouse-face prop on line we will have to = from = end,
10552         ;; so we highlight the entire line instead.
10553         (when (= (+ to 2) from)
10554           (setq from beg)
10555           (setq to end))
10556         (if gnus-newsgroup-selected-overlay
10557             ;; Move old overlay.
10558             (gnus-move-overlay
10559              gnus-newsgroup-selected-overlay from to (current-buffer))
10560           ;; Create new overlay.
10561           (gnus-overlay-put
10562            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10563            'face gnus-summary-selected-face))))))
10564
10565 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10566 (defun gnus-summary-highlight-line ()
10567   "Highlight current line according to `gnus-summary-highlight'."
10568   (let* ((list gnus-summary-highlight)
10569          (p (point))
10570          (end (progn (end-of-line) (point)))
10571          ;; now find out where the line starts and leave point there.
10572          (beg (progn (beginning-of-line) (point)))
10573          (article (gnus-summary-article-number))
10574          (score (or (cdr (assq (or article gnus-current-article)
10575                                gnus-newsgroup-scored))
10576                     gnus-summary-default-score 0))
10577          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10578          (inhibit-read-only t))
10579     ;; Eval the cars of the lists until we find a match.
10580     (let ((default gnus-summary-default-score)
10581           (default-high gnus-summary-default-high-score)
10582           (default-low gnus-summary-default-low-score))
10583       (while (and list
10584                   (not (eval (caar list))))
10585         (setq list (cdr list))))
10586     (let ((face (cdar list)))
10587       (unless (eq face (get-text-property beg 'face))
10588         (gnus-put-text-property-excluding-characters-with-faces
10589          beg end 'face
10590          (setq face (if (boundp face) (symbol-value face) face)))
10591         (when gnus-summary-highlight-line-function
10592           (funcall gnus-summary-highlight-line-function article face))))
10593     (goto-char p)))
10594
10595 (defun gnus-update-read-articles (group unread &optional compute)
10596   "Update the list of read articles in GROUP."
10597   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10598          (entry (gnus-gethash group gnus-newsrc-hashtb))
10599          (info (nth 2 entry))
10600          (prev 1)
10601          (unread (sort (copy-sequence unread) '<))
10602          read)
10603     (if (or (not info) (not active))
10604         ;; There is no info on this group if it was, in fact,
10605         ;; killed.  Gnus stores no information on killed groups, so
10606         ;; there's nothing to be done.
10607         ;; One could store the information somewhere temporarily,
10608         ;; perhaps...  Hmmm...
10609         ()
10610       ;; Remove any negative articles numbers.
10611       (while (and unread (< (car unread) 0))
10612         (setq unread (cdr unread)))
10613       ;; Remove any expired article numbers
10614       (while (and unread (< (car unread) (car active)))
10615         (setq unread (cdr unread)))
10616       ;; Compute the ranges of read articles by looking at the list of
10617       ;; unread articles.
10618       (while unread
10619         (when (/= (car unread) prev)
10620           (push (if (= prev (1- (car unread))) prev
10621                   (cons prev (1- (car unread))))
10622                 read))
10623         (setq prev (1+ (car unread)))
10624         (setq unread (cdr unread)))
10625       (when (<= prev (cdr active))
10626         (push (cons prev (cdr active)) read))
10627       (setq read (if (> (length read) 1) (nreverse read) read))
10628       (if compute
10629           read
10630         (save-excursion
10631           (let (setmarkundo)
10632             ;; Propagate the read marks to the backend.
10633             (when (gnus-check-backend-function 'request-set-mark group)
10634               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10635                     (add (gnus-remove-from-range read (gnus-info-read info))))
10636                 (when (or add del)
10637                   (unless (gnus-check-group group)
10638                     (error "Can't open server for %s" group))
10639                   (gnus-request-set-mark
10640                    group (delq nil (list (if add (list add 'add '(read)))
10641                                          (if del (list del 'del '(read))))))
10642                   (setq setmarkundo
10643                         `(gnus-request-set-mark
10644                           ,group
10645                           ',(delq nil (list
10646                                        (if del (list del 'add '(read)))
10647                                        (if add (list add 'del '(read))))))))))
10648             (set-buffer gnus-group-buffer)
10649             (gnus-undo-register
10650               `(progn
10651                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10652                  (gnus-info-set-read ',info ',(gnus-info-read info))
10653                  (gnus-get-unread-articles-in-group ',info
10654                                                     (gnus-active ,group))
10655                  (gnus-group-update-group ,group t)
10656                  ,setmarkundo))))
10657         ;; Enter this list into the group info.
10658         (gnus-info-set-read info read)
10659         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10660         (gnus-get-unread-articles-in-group info (gnus-active group))
10661         t))))
10662
10663 (defun gnus-offer-save-summaries ()
10664   "Offer to save all active summary buffers."
10665   (let (buffers)
10666     ;; Go through all buffers and find all summaries.
10667     (dolist (buffer (buffer-list))
10668       (when (and (setq buffer (buffer-name buffer))
10669                  (string-match "Summary" buffer)
10670                  (save-excursion
10671                    (set-buffer buffer)
10672                    ;; We check that this is, indeed, a summary buffer.
10673                    (and (eq major-mode 'gnus-summary-mode)
10674                         ;; Also make sure this isn't bogus.
10675                         gnus-newsgroup-prepared
10676                         ;; Also make sure that this isn't a
10677                         ;; dead summary buffer.
10678                         (not gnus-dead-summary-mode))))
10679         (push buffer buffers)))
10680     ;; Go through all these summary buffers and offer to save them.
10681     (when buffers
10682       (save-excursion
10683         (map-y-or-n-p
10684          "Update summary buffer %s? "
10685          (lambda (buf)
10686            (switch-to-buffer buf)
10687            (gnus-summary-exit))
10688          buffers)))))
10689
10690 (defun gnus-summary-setup-default-charset ()
10691   "Setup newsgroup default charset."
10692   (if (equal gnus-newsgroup-name "nndraft:drafts")
10693       (setq gnus-newsgroup-charset nil)
10694     (let* ((ignored-charsets
10695             (or gnus-newsgroup-ephemeral-ignored-charsets
10696                 (append
10697                  (and gnus-newsgroup-name
10698                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10699                  gnus-newsgroup-ignored-charsets))))
10700       (setq gnus-newsgroup-charset
10701             (or gnus-newsgroup-ephemeral-charset
10702                 (and gnus-newsgroup-name
10703                      (gnus-parameter-charset gnus-newsgroup-name))
10704                 gnus-default-charset))
10705       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10706            ignored-charsets))))
10707
10708 ;;;
10709 ;;; Mime Commands
10710 ;;;
10711
10712 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10713   "Display the current article buffer fully MIME-buttonized.
10714 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10715 treated as multipart/mixed."
10716   (interactive "P")
10717   (require 'gnus-art)
10718   (let ((gnus-unbuttonized-mime-types nil)
10719         (gnus-mime-display-multipart-as-mixed show-all-parts))
10720     (gnus-summary-show-article)))
10721
10722 (defun gnus-summary-repair-multipart (article)
10723   "Add a Content-Type header to a multipart article without one."
10724   (interactive (list (gnus-summary-article-number)))
10725   (gnus-with-article article
10726     (message-narrow-to-head)
10727     (message-remove-header "Mime-Version")
10728     (goto-char (point-max))
10729     (insert "Mime-Version: 1.0\n")
10730     (widen)
10731     (when (search-forward "\n--" nil t)
10732       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10733         (message-narrow-to-head)
10734         (message-remove-header "Content-Type")
10735         (goto-char (point-max))
10736         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10737                         separator))
10738         (widen))))
10739   (let (gnus-mark-article-hook)
10740     (gnus-summary-select-article t t nil article)))
10741
10742 (defun gnus-summary-toggle-display-buttonized ()
10743   "Toggle the buttonizing of the article buffer."
10744   (interactive)
10745   (require 'gnus-art)
10746   (if (setq gnus-inhibit-mime-unbuttonizing
10747             (not gnus-inhibit-mime-unbuttonizing))
10748       (let ((gnus-unbuttonized-mime-types nil))
10749         (gnus-summary-show-article))
10750     (gnus-summary-show-article)))
10751
10752 ;;;
10753 ;;; Generic summary marking commands
10754 ;;;
10755
10756 (defvar gnus-summary-marking-alist
10757   '((read gnus-del-mark "d")
10758     (unread gnus-unread-mark "u")
10759     (ticked gnus-ticked-mark "!")
10760     (dormant gnus-dormant-mark "?")
10761     (expirable gnus-expirable-mark "e"))
10762   "An alist of names/marks/keystrokes.")
10763
10764 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10765 (defvar gnus-summary-mark-map)
10766
10767 (defun gnus-summary-make-all-marking-commands ()
10768   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10769   (dolist (elem gnus-summary-marking-alist)
10770     (apply 'gnus-summary-make-marking-command elem)))
10771
10772 (defun gnus-summary-make-marking-command (name mark keystroke)
10773   (let ((map (make-sparse-keymap)))
10774     (define-key gnus-summary-generic-mark-map keystroke map)
10775     (dolist (lway `((next "next" next nil "n")
10776                     (next-unread "next unread" next t "N")
10777                     (prev "previous" prev nil "p")
10778                     (prev-unread "previous unread" prev t "P")
10779                     (nomove "" nil nil ,keystroke)))
10780       (let ((func (gnus-summary-make-marking-command-1
10781                    mark (car lway) lway name)))
10782         (setq func (eval func))
10783         (define-key map (nth 4 lway) func)))))
10784
10785 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10786   `(defun ,(intern
10787             (format "gnus-summary-put-mark-as-%s%s"
10788                     name (if (eq way 'nomove)
10789                              ""
10790                            (concat "-" (symbol-name way)))))
10791      (n)
10792      ,(format
10793        "Mark the current article as %s%s.
10794 If N, the prefix, then repeat N times.
10795 If N is negative, move in reverse order.
10796 The difference between N and the actual number of articles marked is
10797 returned."
10798        name (cadr lway))
10799      (interactive "p")
10800      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10801
10802 (defun gnus-summary-generic-mark (n mark move unread)
10803   "Mark N articles with MARK."
10804   (unless (eq major-mode 'gnus-summary-mode)
10805     (error "This command can only be used in the summary buffer"))
10806   (gnus-summary-show-thread)
10807   (let ((nummove
10808          (cond
10809           ((eq move 'next) 1)
10810           ((eq move 'prev) -1)
10811           (t 0))))
10812     (if (zerop nummove)
10813         (setq n 1)
10814       (when (< n 0)
10815         (setq n (abs n)
10816               nummove (* -1 nummove))))
10817     (while (and (> n 0)
10818                 (gnus-summary-mark-article nil mark)
10819                 (zerop (gnus-summary-next-subject nummove unread t)))
10820       (setq n (1- n)))
10821     (when (/= 0 n)
10822       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10823     (gnus-summary-recenter)
10824     (gnus-summary-position-point)
10825     (gnus-set-mode-line 'summary)
10826     n))
10827
10828 (defun gnus-summary-insert-articles (articles)
10829   (when (setq articles
10830               (gnus-set-difference articles
10831                                    (mapcar (lambda (h) (mail-header-number h))
10832                                            gnus-newsgroup-headers)))
10833     (setq gnus-newsgroup-headers
10834           (merge 'list
10835                  gnus-newsgroup-headers
10836                  (gnus-fetch-headers articles)
10837                  'gnus-article-sort-by-number))
10838     ;; Suppress duplicates?
10839     (when gnus-suppress-duplicates
10840       (gnus-dup-suppress-articles))
10841
10842     ;; We might want to build some more threads first.
10843     (when (and gnus-fetch-old-headers
10844                (eq gnus-headers-retrieved-by 'nov))
10845       (if (eq gnus-fetch-old-headers 'invisible)
10846           (gnus-build-all-threads)
10847         (gnus-build-old-threads)))
10848     ;; Let the Gnus agent mark articles as read.
10849     (when gnus-agent
10850       (gnus-agent-get-undownloaded-list))
10851     ;; Remove list identifiers from subject
10852     (when gnus-list-identifiers
10853       (gnus-summary-remove-list-identifiers))
10854     ;; First and last article in this newsgroup.
10855     (when gnus-newsgroup-headers
10856       (setq gnus-newsgroup-begin
10857             (mail-header-number (car gnus-newsgroup-headers))
10858             gnus-newsgroup-end
10859             (mail-header-number
10860              (gnus-last-element gnus-newsgroup-headers))))
10861     (when gnus-use-scoring
10862       (gnus-possibly-score-headers))))
10863
10864 (defun gnus-summary-insert-old-articles (&optional all)
10865   "Insert all old articles in this group.
10866 If ALL is non-nil, already read articles become readable.
10867 If ALL is a number, fetch this number of articles."
10868   (interactive "P")
10869   (prog1
10870       (let ((old (mapcar 'car gnus-newsgroup-data))
10871             (i (car gnus-newsgroup-active))
10872             older len)
10873         (while (<= i (cdr gnus-newsgroup-active))
10874           (or (memq i old) (push i older))
10875           (incf i))
10876         (setq len (length older))
10877         (cond
10878          ((null older) nil)
10879          ((numberp all)
10880           (if (< all len)
10881               (setq older (subseq older 0 all))))
10882          (all nil)
10883          (t
10884           (if (and (numberp gnus-large-newsgroup)
10885                    (> len gnus-large-newsgroup))
10886               (let ((input
10887                      (read-string
10888                       (format
10889                        "How many articles from %s (default %d): "
10890                        (gnus-limit-string
10891                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10892                        len))))
10893                 (unless (string-match "^[ \t]*$" input)
10894                   (setq all (string-to-number input))
10895                   (if (< all len)
10896                       (setq older (subseq older 0 all))))))))
10897         (if (not older)
10898             (message "No old news.")
10899           (let ((gnus-fetch-old-headers t))
10900             (gnus-summary-insert-articles older))
10901           (gnus-summary-limit (gnus-union older old))))
10902     (gnus-summary-position-point)))
10903
10904 (defun gnus-summary-insert-new-articles ()
10905   "Insert all new articles in this group."
10906   (interactive)
10907   (prog1
10908       (let ((old (mapcar 'car gnus-newsgroup-data))
10909             (old-active gnus-newsgroup-active)
10910             (nnmail-fetched-sources (list t))
10911             i new)
10912         (setq gnus-newsgroup-active
10913               (gnus-activate-group gnus-newsgroup-name 'scan))
10914         (setq i (1+ (cdr old-active)))
10915         (while (<= i (cdr gnus-newsgroup-active))
10916           (push i new)
10917           (incf i))
10918         (if (not new)
10919             (message "No gnus is bad news.")
10920           (setq new (nreverse new))
10921           (gnus-summary-insert-articles new)
10922           (setq gnus-newsgroup-unreads
10923                 (append gnus-newsgroup-unreads new))
10924           (gnus-summary-limit (gnus-union old new))))
10925     (gnus-summary-position-point)))
10926
10927 (gnus-summary-make-all-marking-commands)
10928
10929 (gnus-ems-redefine)
10930
10931 (provide 'gnus-sum)
10932
10933 (run-hooks 'gnus-sum-load-hook)
10934
10935 ;;; gnus-sum.el ends here