* gnus-xmas.el (gnus-xmas-image-type-available-p): Assume that
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mm-decode)
39 (require 'nnoo)
40
41 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
42 (autoload 'gnus-cache-write-active "gnus-cache")
43 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
44 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
45 (autoload 'mm-uu-dissect "mm-uu")
46
47 (defcustom gnus-kill-summary-on-exit t
48   "*If non-nil, kill the summary buffer when you exit from it.
49 If nil, the summary will become a \"*Dead Summary*\" buffer, and
50 it will be killed sometime later."
51   :group 'gnus-summary-exit
52   :type 'boolean)
53
54 (defcustom gnus-fetch-old-headers nil
55   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
56 If an unread article in the group refers to an older, already read (or
57 just marked as read) article, the old article will not normally be
58 displayed in the Summary buffer.  If this variable is non-nil, Gnus
59 will attempt to grab the headers to the old articles, and thereby
60 build complete threads.  If it has the value `some', only enough
61 headers to connect otherwise loose threads will be displayed.  This
62 variable can also be a number.  In that case, no more than that number
63 of old headers will be fetched.  If it has the value `invisible', all
64 old headers will be fetched, but none will be displayed.
65
66 The server has to support NOV for any of this to work."
67   :group 'gnus-thread
68   :type '(choice (const :tag "off" nil)
69                  (const some)
70                  number
71                  (sexp :menu-tag "other" t)))
72
73 (defcustom gnus-refer-thread-limit 200
74   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
75 If t, fetch all the available old headers."
76   :group 'gnus-thread
77   :type '(choice number
78                  (sexp :menu-tag "other" t)))
79
80 (defcustom gnus-summary-make-false-root 'adopt
81   "*nil means that Gnus won't gather loose threads.
82 If the root of a thread has expired or been read in a previous
83 session, the information necessary to build a complete thread has been
84 lost.  Instead of having many small sub-threads from this original thread
85 scattered all over the summary buffer, Gnus can gather them.
86
87 If non-nil, Gnus will try to gather all loose sub-threads from an
88 original thread into one large thread.
89
90 If this variable is non-nil, it should be one of `none', `adopt',
91 `dummy' or `empty'.
92
93 If this variable is `none', Gnus will not make a false root, but just
94 present the sub-threads after another.
95 If this variable is `dummy', Gnus will create a dummy root that will
96 have all the sub-threads as children.
97 If this variable is `adopt', Gnus will make one of the \"children\"
98 the parent and mark all the step-children as such.
99 If this variable is `empty', the \"children\" are printed with empty
100 subject fields.  (Or rather, they will be printed with a string
101 given by the `gnus-summary-same-subject' variable.)"
102   :group 'gnus-thread
103   :type '(choice (const :tag "off" nil)
104                  (const none)
105                  (const dummy)
106                  (const adopt)
107                  (const empty)))
108
109 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
110   "*A regexp to match subjects to be excluded from loose thread gathering.
111 As loose thread gathering is done on subjects only, that means that
112 there can be many false gatherings performed.  By rooting out certain
113 common subjects, gathering might become saner."
114   :group 'gnus-thread
115   :type 'regexp)
116
117 (defcustom gnus-summary-gather-subject-limit nil
118   "*Maximum length of subject comparisons when gathering loose threads.
119 Use nil to compare full subjects.  Setting this variable to a low
120 number will help gather threads that have been corrupted by
121 newsreaders chopping off subject lines, but it might also mean that
122 unrelated articles that have subject that happen to begin with the
123 same few characters will be incorrectly gathered.
124
125 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
126 comparing subjects."
127   :group 'gnus-thread
128   :type '(choice (const :tag "off" nil)
129                  (const fuzzy)
130                  (sexp :menu-tag "on" t)))
131
132 (defcustom gnus-simplify-subject-functions nil
133   "List of functions taking a string argument that simplify subjects.
134 The functions are applied recursively.
135
136 Useful functions to put in this list include: `gnus-simplify-subject-re',
137 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
138   :group 'gnus-thread
139   :type '(repeat function))
140
141 (defcustom gnus-simplify-ignored-prefixes nil
142   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
143   :group 'gnus-thread
144   :type '(choice (const :tag "off" nil)
145                  regexp))
146
147 (defcustom gnus-build-sparse-threads nil
148   "*If non-nil, fill in the gaps in threads.
149 If `some', only fill in the gaps that are needed to tie loose threads
150 together.  If `more', fill in all leaf nodes that Gnus can find.  If
151 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
152   :group 'gnus-thread
153   :type '(choice (const :tag "off" nil)
154                  (const some)
155                  (const more)
156                  (sexp :menu-tag "all" t)))
157
158 (defcustom gnus-summary-thread-gathering-function
159   'gnus-gather-threads-by-subject
160   "*Function used for gathering loose threads.
161 There are two pre-defined functions: `gnus-gather-threads-by-subject',
162 which only takes Subjects into consideration; and
163 `gnus-gather-threads-by-references', which compared the References
164 headers of the articles to find matches."
165   :group 'gnus-thread
166   :type '(radio (function-item gnus-gather-threads-by-subject)
167                 (function-item gnus-gather-threads-by-references)
168                 (function :tag "other")))
169
170 (defcustom gnus-summary-same-subject ""
171   "*String indicating that the current article has the same subject as the previous.
172 This variable will only be used if the value of
173 `gnus-summary-make-false-root' is `empty'."
174   :group 'gnus-summary-format
175   :type 'string)
176
177 (defcustom gnus-summary-goto-unread t
178   "*If t, many commands will go to the next unread article.
179 This applies to marking commands as well as other commands that
180 \"naturally\" select the next article, like, for instance, `SPC' at
181 the end of an article.
182
183 If nil, the marking commands do NOT go to the next unread article
184 (they go to the next article instead).  If `never', commands that
185 usually go to the next unread article, will go to the next article,
186 whether it is read or not."
187   :group 'gnus-summary-marks
188   :link '(custom-manual "(gnus)Setting Marks")
189   :type '(choice (const :tag "off" nil)
190                  (const never)
191                  (sexp :menu-tag "on" t)))
192
193 (defcustom gnus-summary-default-score 0
194   "*Default article score level.
195 All scores generated by the score files will be added to this score.
196 If this variable is nil, scoring will be disabled."
197   :group 'gnus-score-default
198   :type '(choice (const :tag "disable")
199                  integer))
200
201 (defcustom gnus-summary-default-high-score 0
202   "*Default threshold for a high scored article.
203 An article will be highlighted as high scored if its score is greater
204 than this score."
205   :group 'gnus-score-default
206   :type 'integer)
207
208 (defcustom gnus-summary-default-low-score 0
209   "*Default threshold for a low scored article.
210 An article will be highlighted as low scored if its score is smaller
211 than this score."
212   :group 'gnus-score-default
213   :type 'integer)
214
215 (defcustom gnus-summary-zcore-fuzz 0
216   "*Fuzziness factor for the zcore in the summary buffer.
217 Articles with scores closer than this to `gnus-summary-default-score'
218 will not be marked."
219   :group 'gnus-summary-format
220   :type 'integer)
221
222 (defcustom gnus-simplify-subject-fuzzy-regexp nil
223   "*Strings to be removed when doing fuzzy matches.
224 This can either be a regular expression or list of regular expressions
225 that will be removed from subject strings if fuzzy subject
226 simplification is selected."
227   :group 'gnus-thread
228   :type '(repeat regexp))
229
230 (defcustom gnus-show-threads t
231   "*If non-nil, display threads in summary mode."
232   :group 'gnus-thread
233   :type 'boolean)
234
235 (defcustom gnus-thread-hide-subtree nil
236   "*If non-nil, hide all threads initially.
237 This can be a predicate specifier which says which threads to hide.
238 If threads are hidden, you have to run the command
239 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
240 to expose hidden threads."
241   :group 'gnus-thread
242   :type 'boolean)
243
244 (defcustom gnus-thread-hide-killed t
245   "*If non-nil, hide killed threads automatically."
246   :group 'gnus-thread
247   :type 'boolean)
248
249 (defcustom gnus-thread-ignore-subject t
250   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
251 If nil, articles that have different subjects from their parents will
252 start separate threads."
253   :group 'gnus-thread
254   :type 'boolean)
255
256 (defcustom gnus-thread-operation-ignore-subject t
257   "*If non-nil, subjects will be ignored when doing thread commands.
258 This affects commands like `gnus-summary-kill-thread' and
259 `gnus-summary-lower-thread'.
260
261 If this variable is nil, articles in the same thread with different
262 subjects will not be included in the operation in question.  If this
263 variable is `fuzzy', only articles that have subjects that are fuzzily
264 equal will be included."
265   :group 'gnus-thread
266   :type '(choice (const :tag "off" nil)
267                  (const fuzzy)
268                  (sexp :tag "on" t)))
269
270 (defcustom gnus-thread-indent-level 4
271   "*Number that says how much each sub-thread should be indented."
272   :group 'gnus-thread
273   :type 'integer)
274
275 (defcustom gnus-auto-extend-newsgroup t
276   "*If non-nil, extend newsgroup forward and backward when requested."
277   :group 'gnus-summary-choose
278   :type 'boolean)
279
280 (defcustom gnus-auto-select-first t
281   "*If non-nil, select the article under point.
282 Which article this is is controlled by the `gnus-auto-select-subject'
283 variable.
284
285 If you want to prevent automatic selection of articles in some
286 newsgroups, set the variable to nil in `gnus-select-group-hook'."
287   :group 'gnus-group-select
288   :type '(choice (const :tag "none" nil)
289                  (sexp :menu-tag "first" t)))
290
291 (defcustom gnus-auto-select-subject 'unread
292   "*Says what subject to place under point when entering a group.
293
294 This variable can either be the symbols `first' (place point on the
295 first subject), `unread' (place point on the subject line of the first
296 unread article), `best' (place point on the subject line of the
297 higest-scored article), `unseen' (place point on the subject line of
298 the first unseen article), 'unseen-or-unread' (place point on the subject
299 line of the first unseen article or, if all article have been seen, on the
300 subject line of the first unread article), or a function to be called to
301 place point on some subject line.."
302   :group 'gnus-group-select
303   :type '(choice (const best)
304                  (const unread)
305                  (const first)
306                  (const unseen)
307                  (const unseen-or-unread)))
308
309 (defcustom gnus-auto-select-next t
310   "*If non-nil, offer to go to the next group from the end of the previous.
311 If the value is t and the next newsgroup is empty, Gnus will exit
312 summary mode and go back to group mode.  If the value is neither nil
313 nor t, Gnus will select the following unread newsgroup.  In
314 particular, if the value is the symbol `quietly', the next unread
315 newsgroup will be selected without any confirmation, and if it is
316 `almost-quietly', the next group will be selected without any
317 confirmation if you are located on the last article in the group.
318 Finally, if this variable is `slightly-quietly', the `Z n' command
319 will go to the next group without confirmation."
320   :group 'gnus-summary-maneuvering
321   :type '(choice (const :tag "off" nil)
322                  (const quietly)
323                  (const almost-quietly)
324                  (const slightly-quietly)
325                  (sexp :menu-tag "on" t)))
326
327 (defcustom gnus-auto-select-same nil
328   "*If non-nil, select the next article with the same subject.
329 If there are no more articles with the same subject, go to
330 the first unread article."
331   :group 'gnus-summary-maneuvering
332   :type 'boolean)
333
334 (defcustom gnus-summary-check-current nil
335   "*If non-nil, consider the current article when moving.
336 The \"unread\" movement commands will stay on the same line if the
337 current article is unread."
338   :group 'gnus-summary-maneuvering
339   :type 'boolean)
340
341 (defcustom gnus-auto-center-summary t
342   "*If non-nil, always center the current summary buffer.
343 In particular, if `vertical' do only vertical recentering.  If non-nil
344 and non-`vertical', do both horizontal and vertical recentering."
345   :group 'gnus-summary-maneuvering
346   :type '(choice (const :tag "none" nil)
347                  (const vertical)
348                  (integer :tag "height")
349                  (sexp :menu-tag "both" t)))
350
351 (defcustom gnus-show-all-headers nil
352   "*If non-nil, don't hide any headers."
353   :group 'gnus-article-hiding
354   :group 'gnus-article-headers
355   :type 'boolean)
356
357 (defcustom gnus-summary-ignore-duplicates nil
358   "*If non-nil, ignore articles with identical Message-ID headers."
359   :group 'gnus-summary
360   :type 'boolean)
361
362 (defcustom gnus-single-article-buffer t
363   "*If non-nil, display all articles in the same buffer.
364 If nil, each group will get its own article buffer."
365   :group 'gnus-article-various
366   :type 'boolean)
367
368 (defcustom gnus-break-pages t
369   "*If non-nil, do page breaking on articles.
370 The page delimiter is specified by the `gnus-page-delimiter'
371 variable."
372   :group 'gnus-article-various
373   :type 'boolean)
374
375 (defcustom gnus-move-split-methods nil
376   "*Variable used to suggest where articles are to be moved to.
377 It uses the same syntax as the `gnus-split-methods' variable.
378 However, whereas `gnus-split-methods' specifies file names as targets,
379 this variable specifies group names."
380   :group 'gnus-summary-mail
381   :type '(repeat (choice (list :value (fun) function)
382                          (cons :value ("" "") regexp (repeat string))
383                          (sexp :value nil))))
384
385 (defcustom gnus-unread-mark ?           ;Whitespace
386   "*Mark used for unread articles."
387   :group 'gnus-summary-marks
388   :type 'character)
389
390 (defcustom gnus-ticked-mark ?!
391   "*Mark used for ticked articles."
392   :group 'gnus-summary-marks
393   :type 'character)
394
395 (defcustom gnus-dormant-mark ??
396   "*Mark used for dormant articles."
397   :group 'gnus-summary-marks
398   :type 'character)
399
400 (defcustom gnus-del-mark ?r
401   "*Mark used for del'd articles."
402   :group 'gnus-summary-marks
403   :type 'character)
404
405 (defcustom gnus-read-mark ?R
406   "*Mark used for read articles."
407   :group 'gnus-summary-marks
408   :type 'character)
409
410 (defcustom gnus-expirable-mark ?E
411   "*Mark used for expirable articles."
412   :group 'gnus-summary-marks
413   :type 'character)
414
415 (defcustom gnus-killed-mark ?K
416   "*Mark used for killed articles."
417   :group 'gnus-summary-marks
418   :type 'character)
419
420 (defcustom gnus-souped-mark ?F
421   "*Mark used for souped articles."
422   :group 'gnus-summary-marks
423   :type 'character)
424
425 (defcustom gnus-kill-file-mark ?X
426   "*Mark used for articles killed by kill files."
427   :group 'gnus-summary-marks
428   :type 'character)
429
430 (defcustom gnus-low-score-mark ?Y
431   "*Mark used for articles with a low score."
432   :group 'gnus-summary-marks
433   :type 'character)
434
435 (defcustom gnus-catchup-mark ?C
436   "*Mark used for articles that are caught up."
437   :group 'gnus-summary-marks
438   :type 'character)
439
440 (defcustom gnus-replied-mark ?A
441   "*Mark used for articles that have been replied to."
442   :group 'gnus-summary-marks
443   :type 'character)
444
445 (defcustom gnus-forwarded-mark ?F
446   "*Mark used for articles that have been forwarded."
447   :group 'gnus-summary-marks
448   :type 'character)
449
450 (defcustom gnus-recent-mark ?N
451   "*Mark used for articles that are recent."
452   :group 'gnus-summary-marks
453   :type 'character)
454
455 (defcustom gnus-cached-mark ?*
456   "*Mark used for articles that are in the cache."
457   :group 'gnus-summary-marks
458   :type 'character)
459
460 (defcustom gnus-saved-mark ?S
461   "*Mark used for articles that have been saved."
462   :group 'gnus-summary-marks
463   :type 'character)
464
465 (defcustom gnus-unseen-mark ?.
466   "*Mark used for articles that haven't been seen."
467   :group 'gnus-summary-marks
468   :type 'character)
469
470 (defcustom gnus-no-mark ?               ;Whitespace
471   "*Mark used for articles that have no other secondary mark."
472   :group 'gnus-summary-marks
473   :type 'character)
474
475 (defcustom gnus-ancient-mark ?O
476   "*Mark used for ancient articles."
477   :group 'gnus-summary-marks
478   :type 'character)
479
480 (defcustom gnus-sparse-mark ?Q
481   "*Mark used for sparsely reffed articles."
482   :group 'gnus-summary-marks
483   :type 'character)
484
485 (defcustom gnus-canceled-mark ?G
486   "*Mark used for canceled articles."
487   :group 'gnus-summary-marks
488   :type 'character)
489
490 (defcustom gnus-duplicate-mark ?M
491   "*Mark used for duplicate articles."
492   :group 'gnus-summary-marks
493   :type 'character)
494
495 (defcustom gnus-undownloaded-mark ?@
496   "*Mark used for articles that weren't downloaded."
497   :group 'gnus-summary-marks
498   :type 'character)
499
500 (defcustom gnus-downloadable-mark ?%
501   "*Mark used for articles that are to be downloaded."
502   :group 'gnus-summary-marks
503   :type 'character)
504
505 (defcustom gnus-unsendable-mark ?=
506   "*Mark used for articles that won't be sent."
507   :group 'gnus-summary-marks
508   :type 'character)
509
510 (defcustom gnus-score-over-mark ?+
511   "*Score mark used for articles with high scores."
512   :group 'gnus-summary-marks
513   :type 'character)
514
515 (defcustom gnus-score-below-mark ?-
516   "*Score mark used for articles with low scores."
517   :group 'gnus-summary-marks
518   :type 'character)
519
520 (defcustom gnus-empty-thread-mark ?     ;Whitespace
521   "*There is no thread under the article."
522   :group 'gnus-summary-marks
523   :type 'character)
524
525 (defcustom gnus-not-empty-thread-mark ?=
526   "*There is a thread under the article."
527   :group 'gnus-summary-marks
528   :type 'character)
529
530 (defcustom gnus-view-pseudo-asynchronously nil
531   "*If non-nil, Gnus will view pseudo-articles asynchronously."
532   :group 'gnus-extract-view
533   :type 'boolean)
534
535 (defcustom gnus-auto-expirable-marks
536   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
537         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
538         gnus-souped-mark gnus-duplicate-mark)
539   "*The list of marks converted into expiration if a group is auto-expirable."
540   :version "21.1"
541   :group 'gnus-summary
542   :type '(repeat character))
543
544 (defcustom gnus-inhibit-user-auto-expire t
545   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
546   :version "21.1"
547   :group 'gnus-summary
548   :type 'boolean)
549
550 (defcustom gnus-view-pseudos nil
551   "*If `automatic', pseudo-articles will be viewed automatically.
552 If `not-confirm', pseudos will be viewed automatically, and the user
553 will not be asked to confirm the command."
554   :group 'gnus-extract-view
555   :type '(choice (const :tag "off" nil)
556                  (const automatic)
557                  (const not-confirm)))
558
559 (defcustom gnus-view-pseudos-separately t
560   "*If non-nil, one pseudo-article will be created for each file to be viewed.
561 If nil, all files that use the same viewing command will be given as a
562 list of parameters to that command."
563   :group 'gnus-extract-view
564   :type 'boolean)
565
566 (defcustom gnus-insert-pseudo-articles t
567   "*If non-nil, insert pseudo-articles when decoding articles."
568   :group 'gnus-extract-view
569   :type 'boolean)
570
571 (defcustom gnus-summary-dummy-line-format
572   "  %(:                          :%) %S\n"
573   "*The format specification for the dummy roots in the summary buffer.
574 It works along the same lines as a normal formatting string,
575 with some simple extensions.
576
577 %S  The subject
578
579 General format specifiers can also be used.  
580 See (gnus)Formatting Variables."
581   :link '(custom-manual "(gnus)Formatting Variables")
582   :group 'gnus-threading
583   :type 'string)
584
585 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
586   "*The format specification for the summary mode line.
587 It works along the same lines as a normal formatting string,
588 with some simple extensions:
589
590 %G  Group name
591 %p  Unprefixed group name
592 %A  Current article number
593 %z  Current article score
594 %V  Gnus version
595 %U  Number of unread articles in the group
596 %e  Number of unselected articles in the group
597 %Z  A string with unread/unselected article counts
598 %g  Shortish group name
599 %S  Subject of the current article
600 %u  User-defined spec
601 %s  Current score file name
602 %d  Number of dormant articles
603 %r  Number of articles that have been marked as read in this session
604 %E  Number of articles expunged by the score files"
605   :group 'gnus-summary-format
606   :type 'string)
607
608 (defcustom gnus-list-identifiers nil
609   "Regexp that matches list identifiers to be removed from subject.
610 This can also be a list of regexps."
611   :version "21.1"
612   :group 'gnus-summary-format
613   :group 'gnus-article-hiding
614   :type '(choice (const :tag "none" nil)
615                  (regexp :value ".*")
616                  (repeat :value (".*") regexp)))
617
618 (defcustom gnus-summary-mark-below 0
619   "*Mark all articles with a score below this variable as read.
620 This variable is local to each summary buffer and usually set by the
621 score file."
622   :group 'gnus-score-default
623   :type 'integer)
624
625 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
626   "*List of functions used for sorting articles in the summary buffer.
627
628 Each function takes two articles and returns non-nil if the first
629 article should be sorted before the other.  If you use more than one
630 function, the primary sort function should be the last.  You should
631 probably always include `gnus-article-sort-by-number' in the list of
632 sorting functions -- preferably first.  Also note that sorting by date
633 is often much slower than sorting by number, and the sorting order is
634 very similar.  (Sorting by date means sorting by the time the message
635 was sent, sorting by number means sorting by arrival time.)
636
637 Ready-made functions include `gnus-article-sort-by-number',
638 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
639 `gnus-article-sort-by-date' and `gnus-article-sort-by-score'.
640
641 When threading is turned on, the variable `gnus-thread-sort-functions'
642 controls how articles are sorted."
643   :group 'gnus-summary-sort
644   :type '(repeat (choice (function-item gnus-article-sort-by-number)
645                          (function-item gnus-article-sort-by-author)
646                          (function-item gnus-article-sort-by-subject)
647                          (function-item gnus-article-sort-by-date)
648                          (function-item gnus-article-sort-by-score)
649                          (function :tag "other"))))
650
651 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
652   "*List of functions used for sorting threads in the summary buffer.
653 By default, threads are sorted by article number.
654
655 Each function takes two threads and returns non-nil if the first
656 thread should be sorted before the other.  If you use more than one
657 function, the primary sort function should be the last.  You should
658 probably always include `gnus-thread-sort-by-number' in the list of
659 sorting functions -- preferably first.  Also note that sorting by date
660 is often much slower than sorting by number, and the sorting order is
661 very similar.  (Sorting by date means sorting by the time the message
662 was sent, sorting by number means sorting by arrival time.)
663
664 Ready-made functions include `gnus-thread-sort-by-number',
665 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
666 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
667 `gnus-thread-sort-by-most-recent-number',
668 `gnus-thread-sort-by-most-recent-date', and
669 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
670
671 When threading is turned off, the variable
672 `gnus-article-sort-functions' controls how articles are sorted."
673   :group 'gnus-summary-sort
674   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
675                          (function-item gnus-thread-sort-by-author)
676                          (function-item gnus-thread-sort-by-subject)
677                          (function-item gnus-thread-sort-by-date)
678                          (function-item gnus-thread-sort-by-score)
679                          (function-item gnus-thread-sort-by-total-score)
680                          (function :tag "other"))))
681
682 (defcustom gnus-thread-score-function '+
683   "*Function used for calculating the total score of a thread.
684
685 The function is called with the scores of the article and each
686 subthread and should then return the score of the thread.
687
688 Some functions you can use are `+', `max', or `min'."
689   :group 'gnus-summary-sort
690   :type 'function)
691
692 (defcustom gnus-summary-expunge-below nil
693   "All articles that have a score less than this variable will be expunged.
694 This variable is local to the summary buffers."
695   :group 'gnus-score-default
696   :type '(choice (const :tag "off" nil)
697                  integer))
698
699 (defcustom gnus-thread-expunge-below nil
700   "All threads that have a total score less than this variable will be expunged.
701 See `gnus-thread-score-function' for en explanation of what a
702 \"thread score\" is.
703
704 This variable is local to the summary buffers."
705   :group 'gnus-threading
706   :group 'gnus-score-default
707   :type '(choice (const :tag "off" nil)
708                  integer))
709
710 (defcustom gnus-summary-mode-hook nil
711   "*A hook for Gnus summary mode.
712 This hook is run before any variables are set in the summary buffer."
713   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
714   :group 'gnus-summary-various
715   :type 'hook)
716
717 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
718 (when (featurep 'xemacs)
719   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
720   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
721   (add-hook 'gnus-summary-mode-hook
722             'gnus-xmas-switch-horizontal-scrollbar-off))
723
724 (defcustom gnus-summary-menu-hook nil
725   "*Hook run after the creation of the summary mode menu."
726   :group 'gnus-summary-visual
727   :type 'hook)
728
729 (defcustom gnus-summary-exit-hook nil
730   "*A hook called on exit from the summary buffer.
731 It will be called with point in the group buffer."
732   :group 'gnus-summary-exit
733   :type 'hook)
734
735 (defcustom gnus-summary-prepare-hook nil
736   "*A hook called after the summary buffer has been generated.
737 If you want to modify the summary buffer, you can use this hook."
738   :group 'gnus-summary-various
739   :type 'hook)
740
741 (defcustom gnus-summary-prepared-hook nil
742   "*A hook called as the last thing after the summary buffer has been generated."
743   :group 'gnus-summary-various
744   :type 'hook)
745
746 (defcustom gnus-summary-generate-hook nil
747   "*A hook run just before generating the summary buffer.
748 This hook is commonly used to customize threading variables and the
749 like."
750   :group 'gnus-summary-various
751   :type 'hook)
752
753 (defcustom gnus-select-group-hook nil
754   "*A hook called when a newsgroup is selected.
755
756 If you'd like to simplify subjects like the
757 `gnus-summary-next-same-subject' command does, you can use the
758 following hook:
759
760  (add-hook gnus-select-group-hook
761            (lambda ()
762              (mapcar (lambda (header)
763                        (mail-header-set-subject
764                         header
765                         (gnus-simplify-subject
766                          (mail-header-subject header) 're-only)))
767                      gnus-newsgroup-headers)))"
768   :group 'gnus-group-select
769   :type 'hook)
770
771 (defcustom gnus-select-article-hook nil
772   "*A hook called when an article is selected."
773   :group 'gnus-summary-choose
774   :type 'hook)
775
776 (defcustom gnus-visual-mark-article-hook
777   (list 'gnus-highlight-selected-summary)
778   "*Hook run after selecting an article in the summary buffer.
779 It is meant to be used for highlighting the article in some way.  It
780 is not run if `gnus-visual' is nil."
781   :group 'gnus-summary-visual
782   :type 'hook)
783
784 (defcustom gnus-parse-headers-hook nil
785   "*A hook called before parsing the headers."
786   :group 'gnus-various
787   :type 'hook)
788
789 (defcustom gnus-exit-group-hook nil
790   "*A hook called when exiting summary mode.
791 This hook is not called from the non-updating exit commands like `Q'."
792   :group 'gnus-various
793   :type 'hook)
794
795 (defcustom gnus-summary-update-hook
796   (list 'gnus-summary-highlight-line)
797   "*A hook called when a summary line is changed.
798 The hook will not be called if `gnus-visual' is nil.
799
800 The default function `gnus-summary-highlight-line' will
801 highlight the line according to the `gnus-summary-highlight'
802 variable."
803   :group 'gnus-summary-visual
804   :type 'hook)
805
806 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
807   "*A hook called when an article is selected for the first time.
808 The hook is intended to mark an article as read (or unread)
809 automatically when it is selected."
810   :group 'gnus-summary-choose
811   :type 'hook)
812
813 (defcustom gnus-group-no-more-groups-hook nil
814   "*A hook run when returning to group mode having no more (unread) groups."
815   :group 'gnus-group-select
816   :type 'hook)
817
818 (defcustom gnus-ps-print-hook nil
819   "*A hook run before ps-printing something from Gnus."
820   :group 'gnus-summary
821   :type 'hook)
822
823 (defcustom gnus-summary-display-arrow
824   (and (fboundp 'display-graphic-p)
825        (display-graphic-p))
826   "*If non-nil, display an arrow highlighting the current article."
827   :version "21.1"
828   :group 'gnus-summary
829   :type 'boolean)
830
831 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
832   "Face used for highlighting the current article in the summary buffer."
833   :group 'gnus-summary-visual
834   :type 'face)
835
836 (defcustom gnus-summary-highlight
837   '(((= mark gnus-canceled-mark)
838      . gnus-summary-cancelled-face)
839     ((and (> score default-high)
840           (or (= mark gnus-dormant-mark)
841               (= mark gnus-ticked-mark)))
842      . gnus-summary-high-ticked-face)
843     ((and (< score default-low)
844           (or (= mark gnus-dormant-mark)
845               (= mark gnus-ticked-mark)))
846      . gnus-summary-low-ticked-face)
847     ((or (= mark gnus-dormant-mark)
848          (= mark gnus-ticked-mark))
849      . gnus-summary-normal-ticked-face)
850     ((and (> score default-high) (= mark gnus-ancient-mark))
851      . gnus-summary-high-ancient-face)
852     ((and (< score default-low) (= mark gnus-ancient-mark))
853      . gnus-summary-low-ancient-face)
854     ((= mark gnus-ancient-mark)
855      . gnus-summary-normal-ancient-face)
856     ((and (> score default-high) (= mark gnus-unread-mark))
857      . gnus-summary-high-unread-face)
858     ((and (< score default-low) (= mark gnus-unread-mark))
859      . gnus-summary-low-unread-face)
860     ((= mark gnus-unread-mark)
861      . gnus-summary-normal-unread-face)
862     ((and (> score default-high) (memq mark (list gnus-downloadable-mark
863                                                   gnus-undownloaded-mark)))
864      . gnus-summary-high-unread-face)
865     ((and (< score default-low) (memq mark (list gnus-downloadable-mark
866                                                  gnus-undownloaded-mark)))
867      . gnus-summary-low-unread-face)
868     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
869           (memq article gnus-newsgroup-unreads))
870      . gnus-summary-normal-unread-face)
871     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
872      . gnus-summary-normal-read-face)
873     ((> score default-high)
874      . gnus-summary-high-read-face)
875     ((< score default-low)
876      . gnus-summary-low-read-face)
877     (t
878      . gnus-summary-normal-read-face))
879   "*Controls the highlighting of summary buffer lines.
880
881 A list of (FORM . FACE) pairs.  When deciding how a a particular
882 summary line should be displayed, each form is evaluated.  The content
883 of the face field after the first true form is used.  You can change
884 how those summary lines are displayed, by editing the face field.
885
886 You can use the following variables in the FORM field.
887
888 score:        The article's score
889 default:      The default article score.
890 default-high: The default score for high scored articles.
891 default-low:  The default score for low scored articles.
892 below:        The score below which articles are automatically marked as read.
893 mark:         The articles mark."
894   :group 'gnus-summary-visual
895   :type '(repeat (cons (sexp :tag "Form" nil)
896                        face)))
897
898 (defcustom gnus-alter-header-function nil
899   "Function called to allow alteration of article header structures.
900 The function is called with one parameter, the article header vector,
901 which it may alter in any way.")
902
903 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
904   "Variable that says which function should be used to decode a string with encoded words.")
905
906 (defcustom gnus-extra-headers '(To Newsgroups)
907   "*Extra headers to parse."
908   :version "21.1"
909   :group 'gnus-summary
910   :type '(repeat symbol))
911
912 (defcustom gnus-ignored-from-addresses
913   (and user-mail-address (regexp-quote user-mail-address))
914   "*Regexp of From headers that may be suppressed in favor of To headers."
915   :version "21.1"
916   :group 'gnus-summary
917   :type 'regexp)
918
919 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
920   "List of charsets that should be ignored.
921 When these charsets are used in the \"charset\" parameter, the
922 default charset will be used instead."
923   :version "21.1"
924   :type '(repeat symbol)
925   :group 'gnus-charset)
926
927 (gnus-define-group-parameter
928  ignored-charsets
929  :type list
930  :function-document
931  "Return the ignored charsets of GROUP."
932  :variable gnus-group-ignored-charsets-alist
933  :variable-default
934  '(("alt\\.chinese\\.text" iso-8859-1))
935  :variable-document
936  "Alist of regexps (to match group names) and charsets that should be ignored.
937 When these charsets are used in the \"charset\" parameter, the
938 default charset will be used instead."
939  :variable-group gnus-charset
940  :variable-type '(repeat (cons (regexp :tag "Group")
941                                (repeat symbol)))
942  :parameter-type '(choice :tag "Ignored charsets"
943                           :value nil
944                           (repeat (symbol)))
945  :parameter-document       "\
946 List of charsets that should be ignored.
947
948 When these charsets are used in the \"charset\" parameter, the
949 default charset will be used instead.")
950
951 (defcustom gnus-group-highlight-words-alist nil
952   "Alist of group regexps and highlight regexps.
953 This variable uses the same syntax as `gnus-emphasis-alist'."
954   :version "21.1"
955   :type '(repeat (cons (regexp :tag "Group")
956                        (repeat (list (regexp :tag "Highlight regexp")
957                                      (number :tag "Group for entire word" 0)
958                                      (number :tag "Group for displayed part" 0)
959                                      (symbol :tag "Face"
960                                              gnus-emphasis-highlight-words)))))
961   :group 'gnus-summary-visual)
962
963 (defcustom gnus-summary-show-article-charset-alist
964   nil
965   "Alist of number and charset.
966 The article will be shown with the charset corresponding to the
967 numbered argument.
968 For example: ((1 . cn-gb-2312) (2 . big5))."
969   :version "21.1"
970   :type '(repeat (cons (number :tag "Argument" 1)
971                        (symbol :tag "Charset")))
972   :group 'gnus-charset)
973
974 (defcustom gnus-preserve-marks t
975   "Whether marks are preserved when moving, copying and respooling messages."
976   :version "21.1"
977   :type 'boolean
978   :group 'gnus-summary-marks)
979
980 (defcustom gnus-alter-articles-to-read-function nil
981   "Function to be called to alter the list of articles to be selected."
982   :type '(choice (const nil) function)
983   :group 'gnus-summary)
984
985 (defcustom gnus-orphan-score nil
986   "*All orphans get this score added.  Set in the score file."
987   :group 'gnus-score-default
988   :type '(choice (const nil)
989                  integer))
990
991 (defcustom gnus-summary-save-parts-default-mime "image/.*"
992   "*A regexp to match MIME parts when saving multiple parts of a message
993 with gnus-summary-save-parts (X m). This regexp will be used by default
994 when prompting the user for which type of files to save."
995   :group 'gnus-summary
996   :type 'regexp)
997
998
999 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1000   "*A regexp to match MIME parts when saving multiple parts of a message
1001 with gnus-summary-save-parts (X m). This regexp will be used by default
1002 when prompting the user for which type of files to save."
1003   :group 'gnus-summary
1004   :type 'regexp)
1005
1006 (defcustom gnus-read-all-available-headers nil
1007   "Whether Gnus should parse all headers made available to it.
1008 This is mostly relevant for slow backends where the user may
1009 wish to widen the summary buffer to include all headers
1010 that were fetched.  Say, for nnultimate groups."
1011   :group 'gnus-summary
1012   :type '(choice boolean regexp))
1013
1014 (defcustom gnus-summary-muttprint-program "muttprint"
1015   "Command (and optional arguments) used to run Muttprint."
1016   :version "21.3"
1017   :group 'gnus-summary
1018   :type 'string)
1019
1020 (defcustom gnus-article-no-strict-mime nil
1021   "If non-nil, don't require strict MIME.
1022 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1023 supply the MIME-Version header or deliberately strip it From the mail.
1024 Set it to non-nil, Gnus will treat some articles as MIME even if
1025 the MIME-Version header is missed."
1026   :version "21.3"
1027   :type 'boolean
1028   :group 'gnus-article)
1029
1030 ;;; Internal variables
1031
1032 (defvar gnus-summary-display-cache nil)
1033 (defvar gnus-article-mime-handles nil)
1034 (defvar gnus-article-decoded-p nil)
1035 (defvar gnus-article-charset nil)
1036 (defvar gnus-article-ignored-charsets nil)
1037 (defvar gnus-scores-exclude-files nil)
1038 (defvar gnus-page-broken nil)
1039 (defvar gnus-inhibit-mime-unbuttonizing nil)
1040
1041 (defvar gnus-original-article nil)
1042 (defvar gnus-article-internal-prepare-hook nil)
1043 (defvar gnus-newsgroup-process-stack nil)
1044
1045 (defvar gnus-thread-indent-array nil)
1046 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1047 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1048   "Function called to sort the articles within a thread after it has been gathered together.")
1049
1050 (defvar gnus-summary-save-parts-type-history nil)
1051 (defvar gnus-summary-save-parts-last-directory nil)
1052
1053 (defvar gnus-summary-save-parts-type-history nil)
1054 (defvar gnus-summary-save-parts-last-directory nil)
1055
1056 ;; Avoid highlighting in kill files.
1057 (defvar gnus-summary-inhibit-highlight nil)
1058 (defvar gnus-newsgroup-selected-overlay nil)
1059 (defvar gnus-inhibit-limiting nil)
1060 (defvar gnus-newsgroup-adaptive-score-file nil)
1061 (defvar gnus-current-score-file nil)
1062 (defvar gnus-current-move-group nil)
1063 (defvar gnus-current-copy-group nil)
1064 (defvar gnus-current-crosspost-group nil)
1065 (defvar gnus-newsgroup-display nil)
1066
1067 (defvar gnus-newsgroup-dependencies nil)
1068 (defvar gnus-newsgroup-adaptive nil)
1069 (defvar gnus-summary-display-article-function nil)
1070 (defvar gnus-summary-highlight-line-function nil
1071   "Function called after highlighting a summary line.")
1072
1073 (defvar gnus-summary-line-format-alist
1074   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1075     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1076     (?s gnus-tmp-subject-or-nil ?s)
1077     (?n gnus-tmp-name ?s)
1078     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1079         ?s)
1080     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1081             gnus-tmp-from) ?s)
1082     (?F gnus-tmp-from ?s)
1083     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1084     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1085     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1086     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1087     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1088     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1089     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1090     (?L gnus-tmp-lines ?s)
1091     (?I gnus-tmp-indentation ?s)
1092     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1093     (?R gnus-tmp-replied ?c)
1094     (?\[ gnus-tmp-opening-bracket ?c)
1095     (?\] gnus-tmp-closing-bracket ?c)
1096     (?\> (make-string gnus-tmp-level ? ) ?s)
1097     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1098     (?i gnus-tmp-score ?d)
1099     (?z gnus-tmp-score-char ?c)
1100     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1101     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1102     (?U gnus-tmp-unread ?c)
1103     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1104         ?s)
1105     (?t (gnus-summary-number-of-articles-in-thread
1106          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1107         ?d)
1108     (?e (gnus-summary-number-of-articles-in-thread
1109          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1110         ?c)
1111     (?u gnus-tmp-user-defined ?s)
1112     (?P (gnus-pick-line-number) ?d)
1113     (?B gnus-tmp-thread-tree-header-string ?s)
1114     (user-date (gnus-user-date
1115                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1116   "An alist of format specifications that can appear in summary lines.
1117 These are paired with what variables they correspond with, along with
1118 the type of the variable (string, integer, character, etc).")
1119
1120 (defvar gnus-summary-dummy-line-format-alist
1121   `((?S gnus-tmp-subject ?s)
1122     (?N gnus-tmp-number ?d)
1123     (?u gnus-tmp-user-defined ?s)))
1124
1125 (defvar gnus-summary-mode-line-format-alist
1126   `((?G gnus-tmp-group-name ?s)
1127     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1128     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1129     (?A gnus-tmp-article-number ?d)
1130     (?Z gnus-tmp-unread-and-unselected ?s)
1131     (?V gnus-version ?s)
1132     (?U gnus-tmp-unread-and-unticked ?d)
1133     (?S gnus-tmp-subject ?s)
1134     (?e gnus-tmp-unselected ?d)
1135     (?u gnus-tmp-user-defined ?s)
1136     (?d (length gnus-newsgroup-dormant) ?d)
1137     (?t (length gnus-newsgroup-marked) ?d)
1138     (?r (length gnus-newsgroup-reads) ?d)
1139     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1140     (?E gnus-newsgroup-expunged-tally ?d)
1141     (?s (gnus-current-score-file-nondirectory) ?s)))
1142
1143 (defvar gnus-last-search-regexp nil
1144   "Default regexp for article search command.")
1145
1146 (defvar gnus-last-shell-command nil
1147   "Default shell command on article.")
1148
1149 (defvar gnus-newsgroup-begin nil)
1150 (defvar gnus-newsgroup-end nil)
1151 (defvar gnus-newsgroup-last-rmail nil)
1152 (defvar gnus-newsgroup-last-mail nil)
1153 (defvar gnus-newsgroup-last-folder nil)
1154 (defvar gnus-newsgroup-last-file nil)
1155 (defvar gnus-newsgroup-auto-expire nil)
1156 (defvar gnus-newsgroup-active nil)
1157
1158 (defvar gnus-newsgroup-data nil)
1159 (defvar gnus-newsgroup-data-reverse nil)
1160 (defvar gnus-newsgroup-limit nil)
1161 (defvar gnus-newsgroup-limits nil)
1162
1163 (defvar gnus-newsgroup-unreads nil
1164   "Sorted list of unread articles in the current newsgroup.")
1165
1166 (defvar gnus-newsgroup-unselected nil
1167   "Sorted list of unselected unread articles in the current newsgroup.")
1168
1169 (defvar gnus-newsgroup-reads nil
1170   "Alist of read articles and article marks in the current newsgroup.")
1171
1172 (defvar gnus-newsgroup-expunged-tally nil)
1173
1174 (defvar gnus-newsgroup-marked nil
1175   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1176
1177 (defvar gnus-newsgroup-killed nil
1178   "List of ranges of articles that have been through the scoring process.")
1179
1180 (defvar gnus-newsgroup-cached nil
1181   "Sorted list of articles that come from the article cache.")
1182
1183 (defvar gnus-newsgroup-saved nil
1184   "List of articles that have been saved.")
1185
1186 (defvar gnus-newsgroup-kill-headers nil)
1187
1188 (defvar gnus-newsgroup-replied nil
1189   "List of articles that have been replied to in the current newsgroup.")
1190
1191 (defvar gnus-newsgroup-forwarded nil
1192   "List of articles that have been forwarded in the current newsgroup.")
1193
1194 (defvar gnus-newsgroup-recent nil
1195   "List of articles that have are recent in the current newsgroup.")
1196
1197 (defvar gnus-newsgroup-expirable nil
1198   "Sorted list of articles in the current newsgroup that can be expired.")
1199
1200 (defvar gnus-newsgroup-processable nil
1201   "List of articles in the current newsgroup that can be processed.")
1202
1203 (defvar gnus-newsgroup-downloadable nil
1204   "Sorted list of articles in the current newsgroup that can be processed.")
1205
1206 (defvar gnus-newsgroup-undownloaded nil
1207   "List of articles in the current newsgroup that haven't been downloaded..")
1208
1209 (defvar gnus-newsgroup-unsendable nil
1210   "List of articles in the current newsgroup that won't be sent.")
1211
1212 (defvar gnus-newsgroup-bookmarks nil
1213   "List of articles in the current newsgroup that have bookmarks.")
1214
1215 (defvar gnus-newsgroup-dormant nil
1216   "Sorted list of dormant articles in the current newsgroup.")
1217
1218 (defvar gnus-newsgroup-unseen nil
1219   "List of unseen articles in the current newsgroup.")
1220
1221 (defvar gnus-newsgroup-seen nil
1222   "Range of seen articles in the current newsgroup.")
1223
1224 (defvar gnus-newsgroup-articles nil
1225   "List of articles in the current newsgroup.")
1226
1227 (defvar gnus-newsgroup-scored nil
1228   "List of scored articles in the current newsgroup.")
1229
1230 (defvar gnus-newsgroup-headers nil
1231   "List of article headers in the current newsgroup.")
1232
1233 (defvar gnus-newsgroup-threads nil)
1234
1235 (defvar gnus-newsgroup-prepared nil
1236   "Whether the current group has been prepared properly.")
1237
1238 (defvar gnus-newsgroup-ancient nil
1239   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1240
1241 (defvar gnus-newsgroup-sparse nil)
1242
1243 (defvar gnus-current-article nil)
1244 (defvar gnus-article-current nil)
1245 (defvar gnus-current-headers nil)
1246 (defvar gnus-have-all-headers nil)
1247 (defvar gnus-last-article nil)
1248 (defvar gnus-newsgroup-history nil)
1249 (defvar gnus-newsgroup-charset nil)
1250 (defvar gnus-newsgroup-ephemeral-charset nil)
1251 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1252
1253 (defvar gnus-article-before-search nil)
1254
1255 (defconst gnus-summary-local-variables
1256   '(gnus-newsgroup-name
1257     gnus-newsgroup-begin gnus-newsgroup-end
1258     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1259     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1260     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1261     gnus-newsgroup-unselected gnus-newsgroup-marked
1262     gnus-newsgroup-reads gnus-newsgroup-saved
1263     gnus-newsgroup-replied gnus-newsgroup-forwarded
1264     gnus-newsgroup-recent
1265     gnus-newsgroup-expirable
1266     gnus-newsgroup-processable gnus-newsgroup-killed
1267     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1268     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1269     gnus-newsgroup-seen gnus-newsgroup-articles
1270     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1271     gnus-newsgroup-headers gnus-newsgroup-threads
1272     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1273     gnus-current-article gnus-current-headers gnus-have-all-headers
1274     gnus-last-article gnus-article-internal-prepare-hook
1275     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1276     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1277     gnus-thread-expunge-below
1278     gnus-score-alist gnus-current-score-file
1279     (gnus-summary-expunge-below . global)
1280     (gnus-summary-mark-below . global)
1281     (gnus-orphan-score . global)
1282     gnus-newsgroup-active gnus-scores-exclude-files
1283     gnus-newsgroup-history gnus-newsgroup-ancient
1284     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1285     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1286     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1287     (gnus-newsgroup-expunged-tally . 0)
1288     gnus-cache-removable-articles gnus-newsgroup-cached
1289     gnus-newsgroup-data gnus-newsgroup-data-reverse
1290     gnus-newsgroup-limit gnus-newsgroup-limits
1291     gnus-newsgroup-charset gnus-newsgroup-display)
1292   "Variables that are buffer-local to the summary buffers.")
1293
1294 (defvar gnus-newsgroup-variables nil
1295   "A list of variables that have separate values in different newsgroups.
1296 A list of newsgroup (summary buffer) local variables, or cons of
1297 variables and their default values (when the default values are not
1298 nil), that should be made global while the summary buffer is active.
1299 These variables can be used to set variables in the group parameters
1300 while still allowing them to affect operations done in other
1301 buffers. For example:
1302
1303 \(setq gnus-newsgroup-variables
1304      '(message-use-followup-to
1305        (gnus-visible-headers .
1306          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1307 ")
1308
1309 ;; Byte-compiler warning.
1310 (eval-when-compile (defvar gnus-article-mode-map))
1311
1312 ;; MIME stuff.
1313
1314 (defvar gnus-decode-encoded-word-methods
1315   '(mail-decode-encoded-word-string)
1316   "List of methods used to decode encoded words.
1317
1318 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1319 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1320 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1321 whose names match REGEXP.
1322
1323 For example:
1324 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1325  mail-decode-encoded-word-string
1326  (\"chinese\" . rfc1843-decode-string))")
1327
1328 (defvar gnus-decode-encoded-word-methods-cache nil)
1329
1330 (defun gnus-multi-decode-encoded-word-string (string)
1331   "Apply the functions from `gnus-encoded-word-methods' that match."
1332   (unless (and gnus-decode-encoded-word-methods-cache
1333                (eq gnus-newsgroup-name
1334                    (car gnus-decode-encoded-word-methods-cache)))
1335     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1336     (mapcar (lambda (x)
1337               (if (symbolp x)
1338                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1339                 (if (and gnus-newsgroup-name
1340                          (string-match (car x) gnus-newsgroup-name))
1341                     (nconc gnus-decode-encoded-word-methods-cache
1342                            (list (cdr x))))))
1343             gnus-decode-encoded-word-methods))
1344   (let ((xlist gnus-decode-encoded-word-methods-cache))
1345     (pop xlist)
1346     (while xlist
1347       (setq string (funcall (pop xlist) string))))
1348   string)
1349
1350 ;; Subject simplification.
1351
1352 (defun gnus-simplify-whitespace (str)
1353   "Remove excessive whitespace from STR."
1354   (let ((mystr str))
1355     ;; Multiple spaces.
1356     (while (string-match "[ \t][ \t]+" mystr)
1357       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1358                           " "
1359                           (substring mystr (match-end 0)))))
1360     ;; Leading spaces.
1361     (when (string-match "^[ \t]+" mystr)
1362       (setq mystr (substring mystr (match-end 0))))
1363     ;; Trailing spaces.
1364     (when (string-match "[ \t]+$" mystr)
1365       (setq mystr (substring mystr 0 (match-beginning 0))))
1366     mystr))
1367
1368 (defsubst gnus-simplify-subject-re (subject)
1369   "Remove \"Re:\" from subject lines."
1370   (if (string-match message-subject-re-regexp subject)
1371       (substring subject (match-end 0))
1372     subject))
1373
1374 (defun gnus-simplify-subject (subject &optional re-only)
1375   "Remove `Re:' and words in parentheses.
1376 If RE-ONLY is non-nil, strip leading `Re:'s only."
1377   (let ((case-fold-search t))           ;Ignore case.
1378     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1379     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1380       (setq subject (substring subject (match-end 0))))
1381     ;; Remove uninteresting prefixes.
1382     (when (and (not re-only)
1383                gnus-simplify-ignored-prefixes
1384                (string-match gnus-simplify-ignored-prefixes subject))
1385       (setq subject (substring subject (match-end 0))))
1386     ;; Remove words in parentheses from end.
1387     (unless re-only
1388       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1389         (setq subject (substring subject 0 (match-beginning 0)))))
1390     ;; Return subject string.
1391     subject))
1392
1393 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1394 ;; all whitespace.
1395 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1396   (goto-char (point-min))
1397   (while (re-search-forward regexp nil t)
1398     (replace-match (or newtext ""))))
1399
1400 (defun gnus-simplify-buffer-fuzzy ()
1401   "Simplify string in the buffer fuzzily.
1402 The string in the accessible portion of the current buffer is simplified.
1403 It is assumed to be a single-line subject.
1404 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1405 matter is removed.  Additional things can be deleted by setting
1406 `gnus-simplify-subject-fuzzy-regexp'."
1407   (let ((case-fold-search t)
1408         (modified-tick))
1409     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1410
1411     (while (not (eq modified-tick (buffer-modified-tick)))
1412       (setq modified-tick (buffer-modified-tick))
1413       (cond
1414        ((listp gnus-simplify-subject-fuzzy-regexp)
1415         (mapcar 'gnus-simplify-buffer-fuzzy-step
1416                 gnus-simplify-subject-fuzzy-regexp))
1417        (gnus-simplify-subject-fuzzy-regexp
1418         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1419       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1420       (gnus-simplify-buffer-fuzzy-step
1421        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1422       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1423
1424     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1425     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1426     (gnus-simplify-buffer-fuzzy-step " $")
1427     (gnus-simplify-buffer-fuzzy-step "^ +")))
1428
1429 (defun gnus-simplify-subject-fuzzy (subject)
1430   "Simplify a subject string fuzzily.
1431 See `gnus-simplify-buffer-fuzzy' for details."
1432   (save-excursion
1433     (gnus-set-work-buffer)
1434     (let ((case-fold-search t))
1435       ;; Remove uninteresting prefixes.
1436       (when (and gnus-simplify-ignored-prefixes
1437                  (string-match gnus-simplify-ignored-prefixes subject))
1438         (setq subject (substring subject (match-end 0))))
1439       (insert subject)
1440       (inline (gnus-simplify-buffer-fuzzy))
1441       (buffer-string))))
1442
1443 (defsubst gnus-simplify-subject-fully (subject)
1444   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1445   (cond
1446    (gnus-simplify-subject-functions
1447     (gnus-map-function gnus-simplify-subject-functions subject))
1448    ((null gnus-summary-gather-subject-limit)
1449     (gnus-simplify-subject-re subject))
1450    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1451     (gnus-simplify-subject-fuzzy subject))
1452    ((numberp gnus-summary-gather-subject-limit)
1453     (gnus-limit-string (gnus-simplify-subject-re subject)
1454                        gnus-summary-gather-subject-limit))
1455    (t
1456     subject)))
1457
1458 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1459   "Check whether two subjects are equal.
1460 If optional argument simple-first is t, first argument is already
1461 simplified."
1462   (cond
1463    ((null simple-first)
1464     (equal (gnus-simplify-subject-fully s1)
1465            (gnus-simplify-subject-fully s2)))
1466    (t
1467     (equal s1
1468            (gnus-simplify-subject-fully s2)))))
1469
1470 (defun gnus-summary-bubble-group ()
1471   "Increase the score of the current group.
1472 This is a handy function to add to `gnus-summary-exit-hook' to
1473 increase the score of each group you read."
1474   (gnus-group-add-score gnus-newsgroup-name))
1475
1476 \f
1477 ;;;
1478 ;;; Gnus summary mode
1479 ;;;
1480
1481 (put 'gnus-summary-mode 'mode-class 'special)
1482
1483 (defvar gnus-article-commands-menu)
1484
1485 (when t
1486   ;; Non-orthogonal keys
1487
1488   (gnus-define-keys gnus-summary-mode-map
1489     " " gnus-summary-next-page
1490     "\177" gnus-summary-prev-page
1491     [delete] gnus-summary-prev-page
1492     [backspace] gnus-summary-prev-page
1493     "\r" gnus-summary-scroll-up
1494     "\M-\r" gnus-summary-scroll-down
1495     "n" gnus-summary-next-unread-article
1496     "p" gnus-summary-prev-unread-article
1497     "N" gnus-summary-next-article
1498     "P" gnus-summary-prev-article
1499     "\M-\C-n" gnus-summary-next-same-subject
1500     "\M-\C-p" gnus-summary-prev-same-subject
1501     "\M-n" gnus-summary-next-unread-subject
1502     "\M-p" gnus-summary-prev-unread-subject
1503     "." gnus-summary-first-unread-article
1504     "," gnus-summary-best-unread-article
1505     "\M-s" gnus-summary-search-article-forward
1506     "\M-r" gnus-summary-search-article-backward
1507     "<" gnus-summary-beginning-of-article
1508     ">" gnus-summary-end-of-article
1509     "j" gnus-summary-goto-article
1510     "^" gnus-summary-refer-parent-article
1511     "\M-^" gnus-summary-refer-article
1512     "u" gnus-summary-tick-article-forward
1513     "!" gnus-summary-tick-article-forward
1514     "U" gnus-summary-tick-article-backward
1515     "d" gnus-summary-mark-as-read-forward
1516     "D" gnus-summary-mark-as-read-backward
1517     "E" gnus-summary-mark-as-expirable
1518     "\M-u" gnus-summary-clear-mark-forward
1519     "\M-U" gnus-summary-clear-mark-backward
1520     "k" gnus-summary-kill-same-subject-and-select
1521     "\C-k" gnus-summary-kill-same-subject
1522     "\M-\C-k" gnus-summary-kill-thread
1523     "\M-\C-l" gnus-summary-lower-thread
1524     "e" gnus-summary-edit-article
1525     "#" gnus-summary-mark-as-processable
1526     "\M-#" gnus-summary-unmark-as-processable
1527     "\M-\C-t" gnus-summary-toggle-threads
1528     "\M-\C-s" gnus-summary-show-thread
1529     "\M-\C-h" gnus-summary-hide-thread
1530     "\M-\C-f" gnus-summary-next-thread
1531     "\M-\C-b" gnus-summary-prev-thread
1532     [(meta down)] gnus-summary-next-thread
1533     [(meta up)] gnus-summary-prev-thread
1534     "\M-\C-u" gnus-summary-up-thread
1535     "\M-\C-d" gnus-summary-down-thread
1536     "&" gnus-summary-execute-command
1537     "c" gnus-summary-catchup-and-exit
1538     "\C-w" gnus-summary-mark-region-as-read
1539     "\C-t" gnus-summary-toggle-truncation
1540     "?" gnus-summary-mark-as-dormant
1541     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1542     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1543     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1544     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1545     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1546     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1547     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1548     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1549     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1550     "=" gnus-summary-expand-window
1551     "\C-x\C-s" gnus-summary-reselect-current-group
1552     "\M-g" gnus-summary-rescan-group
1553     "w" gnus-summary-stop-page-breaking
1554     "\C-c\C-r" gnus-summary-caesar-message
1555     "f" gnus-summary-followup
1556     "F" gnus-summary-followup-with-original
1557     "C" gnus-summary-cancel-article
1558     "r" gnus-summary-reply
1559     "R" gnus-summary-reply-with-original
1560     "\C-c\C-f" gnus-summary-mail-forward
1561     "o" gnus-summary-save-article
1562     "\C-o" gnus-summary-save-article-mail
1563     "|" gnus-summary-pipe-output
1564     "\M-k" gnus-summary-edit-local-kill
1565     "\M-K" gnus-summary-edit-global-kill
1566     ;; "V" gnus-version
1567     "\C-c\C-d" gnus-summary-describe-group
1568     "q" gnus-summary-exit
1569     "Q" gnus-summary-exit-no-update
1570     "\C-c\C-i" gnus-info-find-node
1571     gnus-mouse-2 gnus-mouse-pick-article
1572     "m" gnus-summary-mail-other-window
1573     "a" gnus-summary-post-news
1574     "i" gnus-summary-news-other-window
1575     "x" gnus-summary-limit-to-unread
1576     "s" gnus-summary-isearch-article
1577     "t" gnus-summary-toggle-header
1578     "g" gnus-summary-show-article
1579     "l" gnus-summary-goto-last-article
1580     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1581     "\C-d" gnus-summary-enter-digest-group
1582     "\M-\C-d" gnus-summary-read-document
1583     "\M-\C-e" gnus-summary-edit-parameters
1584     "\M-\C-a" gnus-summary-customize-parameters
1585     "\C-c\C-b" gnus-bug
1586     "*" gnus-cache-enter-article
1587     "\M-*" gnus-cache-remove-article
1588     "\M-&" gnus-summary-universal-argument
1589     "\C-l" gnus-recenter
1590     "I" gnus-summary-increase-score
1591     "L" gnus-summary-lower-score
1592     "\M-i" gnus-symbolic-argument
1593     "h" gnus-summary-select-article-buffer
1594
1595     "b" gnus-article-view-part
1596     "\M-t" gnus-summary-toggle-display-buttonized
1597
1598     "V" gnus-summary-score-map
1599     "X" gnus-uu-extract-map
1600     "S" gnus-summary-send-map)
1601
1602   ;; Sort of orthogonal keymap
1603   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1604     "t" gnus-summary-tick-article-forward
1605     "!" gnus-summary-tick-article-forward
1606     "d" gnus-summary-mark-as-read-forward
1607     "r" gnus-summary-mark-as-read-forward
1608     "c" gnus-summary-clear-mark-forward
1609     " " gnus-summary-clear-mark-forward
1610     "e" gnus-summary-mark-as-expirable
1611     "x" gnus-summary-mark-as-expirable
1612     "?" gnus-summary-mark-as-dormant
1613     "b" gnus-summary-set-bookmark
1614     "B" gnus-summary-remove-bookmark
1615     "#" gnus-summary-mark-as-processable
1616     "\M-#" gnus-summary-unmark-as-processable
1617     "S" gnus-summary-limit-include-expunged
1618     "C" gnus-summary-catchup
1619     "H" gnus-summary-catchup-to-here
1620     "h" gnus-summary-catchup-from-here
1621     "\C-c" gnus-summary-catchup-all
1622     "k" gnus-summary-kill-same-subject-and-select
1623     "K" gnus-summary-kill-same-subject
1624     "P" gnus-uu-mark-map)
1625
1626   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1627     "c" gnus-summary-clear-above
1628     "u" gnus-summary-tick-above
1629     "m" gnus-summary-mark-above
1630     "k" gnus-summary-kill-below)
1631
1632   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1633     "/" gnus-summary-limit-to-subject
1634     "n" gnus-summary-limit-to-articles
1635     "w" gnus-summary-pop-limit
1636     "s" gnus-summary-limit-to-subject
1637     "a" gnus-summary-limit-to-author
1638     "u" gnus-summary-limit-to-unread
1639     "m" gnus-summary-limit-to-marks
1640     "M" gnus-summary-limit-exclude-marks
1641     "v" gnus-summary-limit-to-score
1642     "*" gnus-summary-limit-include-cached
1643     "D" gnus-summary-limit-include-dormant
1644     "T" gnus-summary-limit-include-thread
1645     "d" gnus-summary-limit-exclude-dormant
1646     "t" gnus-summary-limit-to-age
1647     "x" gnus-summary-limit-to-extra
1648     "p" gnus-summary-limit-to-display-predicate
1649     "E" gnus-summary-limit-include-expunged
1650     "c" gnus-summary-limit-exclude-childless-dormant
1651     "C" gnus-summary-limit-mark-excluded-as-read
1652     "o" gnus-summary-insert-old-articles
1653     "N" gnus-summary-insert-new-articles)
1654
1655   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1656     "n" gnus-summary-next-unread-article
1657     "p" gnus-summary-prev-unread-article
1658     "N" gnus-summary-next-article
1659     "P" gnus-summary-prev-article
1660     "\C-n" gnus-summary-next-same-subject
1661     "\C-p" gnus-summary-prev-same-subject
1662     "\M-n" gnus-summary-next-unread-subject
1663     "\M-p" gnus-summary-prev-unread-subject
1664     "f" gnus-summary-first-unread-article
1665     "b" gnus-summary-best-unread-article
1666     "j" gnus-summary-goto-article
1667     "g" gnus-summary-goto-subject
1668     "l" gnus-summary-goto-last-article
1669     "o" gnus-summary-pop-article)
1670
1671   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1672     "k" gnus-summary-kill-thread
1673     "l" gnus-summary-lower-thread
1674     "i" gnus-summary-raise-thread
1675     "T" gnus-summary-toggle-threads
1676     "t" gnus-summary-rethread-current
1677     "^" gnus-summary-reparent-thread
1678     "s" gnus-summary-show-thread
1679     "S" gnus-summary-show-all-threads
1680     "h" gnus-summary-hide-thread
1681     "H" gnus-summary-hide-all-threads
1682     "n" gnus-summary-next-thread
1683     "p" gnus-summary-prev-thread
1684     "u" gnus-summary-up-thread
1685     "o" gnus-summary-top-thread
1686     "d" gnus-summary-down-thread
1687     "#" gnus-uu-mark-thread
1688     "\M-#" gnus-uu-unmark-thread)
1689
1690   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1691     "g" gnus-summary-prepare
1692     "c" gnus-summary-insert-cached-articles)
1693
1694   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1695     "c" gnus-summary-catchup-and-exit
1696     "C" gnus-summary-catchup-all-and-exit
1697     "E" gnus-summary-exit-no-update
1698     "Q" gnus-summary-exit
1699     "Z" gnus-summary-exit
1700     "n" gnus-summary-catchup-and-goto-next-group
1701     "R" gnus-summary-reselect-current-group
1702     "G" gnus-summary-rescan-group
1703     "N" gnus-summary-next-group
1704     "s" gnus-summary-save-newsrc
1705     "P" gnus-summary-prev-group)
1706
1707   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1708     " " gnus-summary-next-page
1709     "n" gnus-summary-next-page
1710     "\177" gnus-summary-prev-page
1711     [delete] gnus-summary-prev-page
1712     "p" gnus-summary-prev-page
1713     "\r" gnus-summary-scroll-up
1714     "\M-\r" gnus-summary-scroll-down
1715     "<" gnus-summary-beginning-of-article
1716     ">" gnus-summary-end-of-article
1717     "b" gnus-summary-beginning-of-article
1718     "e" gnus-summary-end-of-article
1719     "^" gnus-summary-refer-parent-article
1720     "r" gnus-summary-refer-parent-article
1721     "D" gnus-summary-enter-digest-group
1722     "R" gnus-summary-refer-references
1723     "T" gnus-summary-refer-thread
1724     "g" gnus-summary-show-article
1725     "s" gnus-summary-isearch-article
1726     "P" gnus-summary-print-article
1727     "M" gnus-mailing-list-insinuate
1728     "t" gnus-article-babel)
1729
1730   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1731     "b" gnus-article-add-buttons
1732     "B" gnus-article-add-buttons-to-head
1733     "o" gnus-article-treat-overstrike
1734     "e" gnus-article-emphasize
1735     "w" gnus-article-fill-cited-article
1736     "Q" gnus-article-fill-long-lines
1737     "C" gnus-article-capitalize-sentences
1738     "c" gnus-article-remove-cr
1739     "q" gnus-article-de-quoted-unreadable
1740     "6" gnus-article-de-base64-unreadable
1741     "Z" gnus-article-decode-HZ
1742     "h" gnus-article-wash-html
1743     "u" gnus-article-unsplit-urls
1744     "s" gnus-summary-force-verify-and-decrypt
1745     "f" gnus-article-display-x-face
1746     "l" gnus-summary-stop-page-breaking
1747     "r" gnus-summary-caesar-message
1748     "t" gnus-summary-toggle-header
1749     "g" gnus-treat-smiley
1750     "v" gnus-summary-verbose-headers
1751     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1752     "p" gnus-article-verify-x-pgp-sig
1753     "d" gnus-article-treat-dumbquotes)
1754
1755   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1756     "a" gnus-article-hide
1757     "h" gnus-article-hide-headers
1758     "b" gnus-article-hide-boring-headers
1759     "s" gnus-article-hide-signature
1760     "c" gnus-article-hide-citation
1761     "C" gnus-article-hide-citation-in-followups
1762     "l" gnus-article-hide-list-identifiers
1763     "p" gnus-article-hide-pgp
1764     "B" gnus-article-strip-banner
1765     "P" gnus-article-hide-pem
1766     "\C-c" gnus-article-hide-citation-maybe)
1767
1768   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1769     "a" gnus-article-highlight
1770     "h" gnus-article-highlight-headers
1771     "c" gnus-article-highlight-citation
1772     "s" gnus-article-highlight-signature)
1773
1774   (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1775     "f" gnus-article-treat-fold-headers
1776     "u" gnus-article-treat-unfold-headers
1777     "n" gnus-article-treat-fold-newsgroups)
1778
1779   (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1780     "x" gnus-article-display-x-face
1781     "s" gnus-treat-smiley
1782     "D" gnus-article-remove-images
1783     "f" gnus-treat-from-picon
1784     "m" gnus-treat-mail-picon
1785     "n" gnus-treat-newsgroups-picon)
1786
1787   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1788     "w" gnus-article-decode-mime-words
1789     "c" gnus-article-decode-charset
1790     "v" gnus-mime-view-all-parts
1791     "b" gnus-article-view-part)
1792
1793   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1794     "z" gnus-article-date-ut
1795     "u" gnus-article-date-ut
1796     "l" gnus-article-date-local
1797     "p" gnus-article-date-english
1798     "e" gnus-article-date-lapsed
1799     "o" gnus-article-date-original
1800     "i" gnus-article-date-iso8601
1801     "s" gnus-article-date-user)
1802
1803   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1804     "t" gnus-article-remove-trailing-blank-lines
1805     "l" gnus-article-strip-leading-blank-lines
1806     "m" gnus-article-strip-multiple-blank-lines
1807     "a" gnus-article-strip-blank-lines
1808     "A" gnus-article-strip-all-blank-lines
1809     "s" gnus-article-strip-leading-space
1810     "e" gnus-article-strip-trailing-space
1811     "w" gnus-article-remove-leading-whitespace)
1812
1813   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1814     "v" gnus-version
1815     "f" gnus-summary-fetch-faq
1816     "d" gnus-summary-describe-group
1817     "h" gnus-summary-describe-briefly
1818     "i" gnus-info-find-node)
1819
1820   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1821     "e" gnus-summary-expire-articles
1822     "\M-\C-e" gnus-summary-expire-articles-now
1823     "\177" gnus-summary-delete-article
1824     [delete] gnus-summary-delete-article
1825     [backspace] gnus-summary-delete-article
1826     "m" gnus-summary-move-article
1827     "r" gnus-summary-respool-article
1828     "w" gnus-summary-edit-article
1829     "c" gnus-summary-copy-article
1830     "B" gnus-summary-crosspost-article
1831     "q" gnus-summary-respool-query
1832     "t" gnus-summary-respool-trace
1833     "i" gnus-summary-import-article
1834     "I" gnus-summary-create-article
1835     "p" gnus-summary-article-posted-p)
1836
1837   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1838     "o" gnus-summary-save-article
1839     "m" gnus-summary-save-article-mail
1840     "F" gnus-summary-write-article-file
1841     "r" gnus-summary-save-article-rmail
1842     "f" gnus-summary-save-article-file
1843     "b" gnus-summary-save-article-body-file
1844     "h" gnus-summary-save-article-folder
1845     "v" gnus-summary-save-article-vm
1846     "p" gnus-summary-pipe-output
1847     "P" gnus-summary-muttprint
1848     "s" gnus-soup-add-article)
1849
1850   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1851     "b" gnus-summary-display-buttonized
1852     "m" gnus-summary-repair-multipart
1853     "v" gnus-article-view-part
1854     "o" gnus-article-save-part
1855     "c" gnus-article-copy-part
1856     "C" gnus-article-view-part-as-charset
1857     "e" gnus-article-externalize-part
1858     "E" gnus-article-encrypt-body
1859     "i" gnus-article-inline-part
1860     "|" gnus-article-pipe-part))
1861
1862 (defvar gnus-article-post-menu nil)
1863
1864 (defun gnus-summary-make-menu-bar ()
1865   (gnus-turn-off-edit-menu 'summary)
1866
1867   (unless (boundp 'gnus-summary-misc-menu)
1868
1869     (easy-menu-define
1870       gnus-summary-kill-menu gnus-summary-mode-map ""
1871       (cons
1872        "Score"
1873        (nconc
1874         (list
1875          ["Customize" gnus-score-customize t])
1876         (gnus-make-score-map 'increase)
1877         (gnus-make-score-map 'lower)
1878         '(("Mark"
1879            ["Kill below" gnus-summary-kill-below t]
1880            ["Mark above" gnus-summary-mark-above t]
1881            ["Tick above" gnus-summary-tick-above t]
1882            ["Clear above" gnus-summary-clear-above t])
1883           ["Current score" gnus-summary-current-score t]
1884           ["Set score" gnus-summary-set-score t]
1885           ["Switch current score file..." gnus-score-change-score-file t]
1886           ["Set mark below..." gnus-score-set-mark-below t]
1887           ["Set expunge below..." gnus-score-set-expunge-below t]
1888           ["Edit current score file" gnus-score-edit-current-scores t]
1889           ["Edit score file" gnus-score-edit-file t]
1890           ["Trace score" gnus-score-find-trace t]
1891           ["Find words" gnus-score-find-favourite-words t]
1892           ["Rescore buffer" gnus-summary-rescore t]
1893           ["Increase score..." gnus-summary-increase-score t]
1894           ["Lower score..." gnus-summary-lower-score t]))))
1895
1896     ;; Define both the Article menu in the summary buffer and the
1897     ;; equivalent Commands menu in the article buffer here for
1898     ;; consistency.
1899     (let ((innards
1900            `(("Hide"
1901               ["All" gnus-article-hide t]
1902               ["Headers" gnus-article-hide-headers t]
1903               ["Signature" gnus-article-hide-signature t]
1904               ["Citation" gnus-article-hide-citation t]
1905               ["List identifiers" gnus-article-hide-list-identifiers t]
1906               ["PGP" gnus-article-hide-pgp t]
1907               ["Banner" gnus-article-strip-banner t]
1908               ["Boring headers" gnus-article-hide-boring-headers t])
1909              ("Highlight"
1910               ["All" gnus-article-highlight t]
1911               ["Headers" gnus-article-highlight-headers t]
1912               ["Signature" gnus-article-highlight-signature t]
1913               ["Citation" gnus-article-highlight-citation t])
1914              ("MIME"
1915               ["Words" gnus-article-decode-mime-words t]
1916               ["Charset" gnus-article-decode-charset t]
1917               ["QP" gnus-article-de-quoted-unreadable t]
1918               ["Base64" gnus-article-de-base64-unreadable t]
1919               ["View all" gnus-mime-view-all-parts t]
1920               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
1921               ["Encrypt body" gnus-article-encrypt-body t])
1922              ("Date"
1923               ["Local" gnus-article-date-local t]
1924               ["ISO8601" gnus-article-date-iso8601 t]
1925               ["UT" gnus-article-date-ut t]
1926               ["Original" gnus-article-date-original t]
1927               ["Lapsed" gnus-article-date-lapsed t]
1928               ["User-defined" gnus-article-date-user t])
1929              ("Display"
1930               ["Remove images" gnus-article-remove-images t]
1931               ["Toggle smiley" gnus-treat-smiley t]
1932               ["Show X-Face" gnus-article-display-x-face t]
1933               ["Show picons in From" gnus-treat-from-picon t]
1934               ["Show picons in mail headers" gnus-treat-mail-picon t]
1935               ["Show picons in news headers" gnus-treat-newsgroups-picon t])
1936              ("Washing"
1937               ("Remove Blanks"
1938                ["Leading" gnus-article-strip-leading-blank-lines t]
1939                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1940                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1941                ["All of the above" gnus-article-strip-blank-lines t]
1942                ["All" gnus-article-strip-all-blank-lines t]
1943                ["Leading space" gnus-article-strip-leading-space t]
1944                ["Trailing space" gnus-article-strip-trailing-space t]
1945                ["Leading space in headers"
1946                 gnus-article-remove-leading-whitespace t])
1947               ["Overstrike" gnus-article-treat-overstrike t]
1948               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1949               ["Emphasis" gnus-article-emphasize t]
1950               ["Word wrap" gnus-article-fill-cited-article t]
1951               ["Fill long lines" gnus-article-fill-long-lines t]
1952               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1953               ["CR" gnus-article-remove-cr t]
1954               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1955               ["Base64" gnus-article-de-base64-unreadable t]
1956               ["Rot 13" gnus-summary-caesar-message
1957                ,@(if (featurep 'xemacs) '(t)
1958                    '(:help "\"Caesar rotate\" article by 13"))]
1959               ["Unix pipe" gnus-summary-pipe-message t]
1960               ["Add buttons" gnus-article-add-buttons t]
1961               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1962               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1963               ["Verbose header" gnus-summary-verbose-headers t]
1964               ["Toggle header" gnus-summary-toggle-header t]
1965               ["Unfold headers" gnus-article-treat-unfold-headers t]
1966               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
1967               ["Html" gnus-article-wash-html t]
1968               ["URLs" gnus-article-unsplit-urls t]
1969               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1970               ["HZ" gnus-article-decode-HZ t])
1971              ("Output"
1972               ["Save in default format" gnus-summary-save-article
1973                ,@(if (featurep 'xemacs) '(t)
1974                    '(:help "Save article using default method"))]
1975               ["Save in file" gnus-summary-save-article-file
1976                ,@(if (featurep 'xemacs) '(t)
1977                    '(:help "Save article in file"))]
1978               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1979               ["Save in MH folder" gnus-summary-save-article-folder t]
1980               ["Save in VM folder" gnus-summary-save-article-vm t]
1981               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1982               ["Save body in file" gnus-summary-save-article-body-file t]
1983               ["Pipe through a filter" gnus-summary-pipe-output t]
1984               ["Add to SOUP packet" gnus-soup-add-article t]
1985               ["Print with Muttprint" gnus-summary-muttprint t]
1986               ["Print" gnus-summary-print-article t])
1987              ("Backend"
1988               ["Respool article..." gnus-summary-respool-article t]
1989               ["Move article..." gnus-summary-move-article
1990                (gnus-check-backend-function
1991                 'request-move-article gnus-newsgroup-name)]
1992               ["Copy article..." gnus-summary-copy-article t]
1993               ["Crosspost article..." gnus-summary-crosspost-article
1994                (gnus-check-backend-function
1995                 'request-replace-article gnus-newsgroup-name)]
1996               ["Import file..." gnus-summary-import-article t]
1997               ["Create article..." gnus-summary-create-article t]
1998               ["Check if posted" gnus-summary-article-posted-p t]
1999               ["Edit article" gnus-summary-edit-article
2000                (not (gnus-group-read-only-p))]
2001               ["Delete article" gnus-summary-delete-article
2002                (gnus-check-backend-function
2003                 'request-expire-articles gnus-newsgroup-name)]
2004               ["Query respool" gnus-summary-respool-query t]
2005               ["Trace respool" gnus-summary-respool-trace t]
2006               ["Delete expirable articles" gnus-summary-expire-articles-now
2007                (gnus-check-backend-function
2008                 'request-expire-articles gnus-newsgroup-name)])
2009              ("Extract"
2010               ["Uudecode" gnus-uu-decode-uu
2011                ,@(if (featurep 'xemacs) '(t)
2012                    '(:help "Decode uuencoded article(s)"))]
2013               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2014               ["Unshar" gnus-uu-decode-unshar t]
2015               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2016               ["Save" gnus-uu-decode-save t]
2017               ["Binhex" gnus-uu-decode-binhex t]
2018               ["Postscript" gnus-uu-decode-postscript t])
2019              ("Cache"
2020               ["Enter article" gnus-cache-enter-article t]
2021               ["Remove article" gnus-cache-remove-article t])
2022              ["Translate" gnus-article-babel t]
2023              ["Select article buffer" gnus-summary-select-article-buffer t]
2024              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2025              ["Isearch article..." gnus-summary-isearch-article t]
2026              ["Beginning of the article" gnus-summary-beginning-of-article t]
2027              ["End of the article" gnus-summary-end-of-article t]
2028              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2029              ["Fetch referenced articles" gnus-summary-refer-references t]
2030              ["Fetch current thread" gnus-summary-refer-thread t]
2031              ["Fetch article with id..." gnus-summary-refer-article t]
2032              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2033              ["Redisplay" gnus-summary-show-article t]
2034              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2035       (easy-menu-define
2036         gnus-summary-article-menu gnus-summary-mode-map ""
2037         (cons "Article" innards))
2038
2039       (if (not (keymapp gnus-summary-article-menu))
2040           (easy-menu-define
2041             gnus-article-commands-menu gnus-article-mode-map ""
2042             (cons "Commands" innards))
2043         ;; in Emacs, don't share menu.
2044         (setq gnus-article-commands-menu
2045               (copy-keymap gnus-summary-article-menu))
2046         (define-key gnus-article-mode-map [menu-bar commands]
2047           (cons "Commands" gnus-article-commands-menu))))
2048
2049     (easy-menu-define
2050       gnus-summary-thread-menu gnus-summary-mode-map ""
2051       '("Threads"
2052         ["Toggle threading" gnus-summary-toggle-threads t]
2053         ["Hide threads" gnus-summary-hide-all-threads t]
2054         ["Show threads" gnus-summary-show-all-threads t]
2055         ["Hide thread" gnus-summary-hide-thread t]
2056         ["Show thread" gnus-summary-show-thread t]
2057         ["Go to next thread" gnus-summary-next-thread t]
2058         ["Go to previous thread" gnus-summary-prev-thread t]
2059         ["Go down thread" gnus-summary-down-thread t]
2060         ["Go up thread" gnus-summary-up-thread t]
2061         ["Top of thread" gnus-summary-top-thread t]
2062         ["Mark thread as read" gnus-summary-kill-thread t]
2063         ["Lower thread score" gnus-summary-lower-thread t]
2064         ["Raise thread score" gnus-summary-raise-thread t]
2065         ["Rethread current" gnus-summary-rethread-current t]))
2066
2067     (easy-menu-define
2068       gnus-summary-post-menu gnus-summary-mode-map ""
2069       `("Post"
2070         ["Send a message (mail or news)" gnus-summary-post-news
2071          ,@(if (featurep 'xemacs) '(t)
2072              '(:help "Post an article"))]
2073         ["Followup" gnus-summary-followup
2074          ,@(if (featurep 'xemacs) '(t)
2075              '(:help "Post followup to this article"))]
2076         ["Followup and yank" gnus-summary-followup-with-original
2077          ,@(if (featurep 'xemacs) '(t)
2078              '(:help "Post followup to this article, quoting its contents"))]
2079         ["Supersede article" gnus-summary-supersede-article t]
2080         ["Cancel article" gnus-summary-cancel-article
2081          ,@(if (featurep 'xemacs) '(t)
2082              '(:help "Cancel an article you posted"))]
2083         ["Reply" gnus-summary-reply t]
2084         ["Reply and yank" gnus-summary-reply-with-original t]
2085         ["Wide reply" gnus-summary-wide-reply t]
2086         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2087          ,@(if (featurep 'xemacs) '(t)
2088              '(:help "Mail a reply, quoting this article"))]
2089         ["Very wide reply" gnus-summary-very-wide-reply t]
2090         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2091         ,@(if (featurep 'xemacs) '(t)
2092             '(:help "Mail a very wide reply, quoting this article"))]
2093         ["Mail forward" gnus-summary-mail-forward t]
2094         ["Post forward" gnus-summary-post-forward t]
2095         ["Digest and mail" gnus-uu-digest-mail-forward t]
2096         ["Digest and post" gnus-uu-digest-post-forward t]
2097         ["Resend message" gnus-summary-resend-message t]
2098         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2099         ["Send a mail" gnus-summary-mail-other-window t]
2100         ["Create a local message" gnus-summary-news-other-window t]
2101         ["Uuencode and post" gnus-uu-post-news
2102          ,@(if (featurep 'xemacs) '(t)
2103              '(:help "Post a uuencoded article"))]
2104         ["Followup via news" gnus-summary-followup-to-mail t]
2105         ["Followup via news and yank"
2106          gnus-summary-followup-to-mail-with-original t]
2107         ;;("Draft"
2108         ;;["Send" gnus-summary-send-draft t]
2109         ;;["Send bounced" gnus-resend-bounced-mail t])
2110         ))
2111
2112     (cond
2113      ((not (keymapp gnus-summary-post-menu))
2114       (setq gnus-article-post-menu gnus-summary-post-menu))
2115      ((not gnus-article-post-menu)
2116       ;; Don't share post menu.
2117       (setq gnus-article-post-menu
2118             (copy-keymap gnus-summary-post-menu))))
2119     (define-key gnus-article-mode-map [menu-bar post]
2120       (cons "Post" gnus-article-post-menu))
2121
2122     (easy-menu-define
2123       gnus-summary-misc-menu gnus-summary-mode-map ""
2124       `("Gnus"
2125         ("Mark Read"
2126          ["Mark as read" gnus-summary-mark-as-read-forward t]
2127          ["Mark same subject and select"
2128           gnus-summary-kill-same-subject-and-select t]
2129          ["Mark same subject" gnus-summary-kill-same-subject t]
2130          ["Catchup" gnus-summary-catchup
2131           ,@(if (featurep 'xemacs) '(t)
2132               '(:help "Mark unread articles in this group as read"))]
2133          ["Catchup all" gnus-summary-catchup-all t]
2134          ["Catchup to here" gnus-summary-catchup-to-here t]
2135          ["Catchup from here" gnus-summary-catchup-from-here t]
2136          ["Catchup region" gnus-summary-mark-region-as-read t]
2137          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2138         ("Mark Various"
2139          ["Tick" gnus-summary-tick-article-forward t]
2140          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2141          ["Remove marks" gnus-summary-clear-mark-forward t]
2142          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2143          ["Set bookmark" gnus-summary-set-bookmark t]
2144          ["Remove bookmark" gnus-summary-remove-bookmark t])
2145         ("Limit to"
2146          ["Marks..." gnus-summary-limit-to-marks t]
2147          ["Subject..." gnus-summary-limit-to-subject t]
2148          ["Author..." gnus-summary-limit-to-author t]
2149          ["Age..." gnus-summary-limit-to-age t]
2150          ["Extra..." gnus-summary-limit-to-extra t]
2151          ["Score" gnus-summary-limit-to-score t]
2152          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2153          ["Unread" gnus-summary-limit-to-unread t]
2154          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2155          ["Articles" gnus-summary-limit-to-articles t]
2156          ["Pop limit" gnus-summary-pop-limit t]
2157          ["Show dormant" gnus-summary-limit-include-dormant t]
2158          ["Hide childless dormant"
2159           gnus-summary-limit-exclude-childless-dormant t]
2160          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2161          ["Hide marked" gnus-summary-limit-exclude-marks t]
2162          ["Show expunged" gnus-summary-limit-include-expunged t])
2163         ("Process Mark"
2164          ["Set mark" gnus-summary-mark-as-processable t]
2165          ["Remove mark" gnus-summary-unmark-as-processable t]
2166          ["Remove all marks" gnus-summary-unmark-all-processable t]
2167          ["Mark above" gnus-uu-mark-over t]
2168          ["Mark series" gnus-uu-mark-series t]
2169          ["Mark region" gnus-uu-mark-region t]
2170          ["Unmark region" gnus-uu-unmark-region t]
2171          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2172          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2173          ["Mark all" gnus-uu-mark-all t]
2174          ["Mark buffer" gnus-uu-mark-buffer t]
2175          ["Mark sparse" gnus-uu-mark-sparse t]
2176          ["Mark thread" gnus-uu-mark-thread t]
2177          ["Unmark thread" gnus-uu-unmark-thread t]
2178          ("Process Mark Sets"
2179           ["Kill" gnus-summary-kill-process-mark t]
2180           ["Yank" gnus-summary-yank-process-mark
2181            gnus-newsgroup-process-stack]
2182           ["Save" gnus-summary-save-process-mark t]))
2183         ("Scroll article"
2184          ["Page forward" gnus-summary-next-page
2185           ,@(if (featurep 'xemacs) '(t)
2186               '(:help "Show next page of article"))]
2187          ["Page backward" gnus-summary-prev-page
2188           ,@(if (featurep 'xemacs) '(t)
2189               '(:help "Show previous page of article"))]
2190          ["Line forward" gnus-summary-scroll-up t])
2191         ("Move"
2192          ["Next unread article" gnus-summary-next-unread-article t]
2193          ["Previous unread article" gnus-summary-prev-unread-article t]
2194          ["Next article" gnus-summary-next-article t]
2195          ["Previous article" gnus-summary-prev-article t]
2196          ["Next unread subject" gnus-summary-next-unread-subject t]
2197          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2198          ["Next article same subject" gnus-summary-next-same-subject t]
2199          ["Previous article same subject" gnus-summary-prev-same-subject t]
2200          ["First unread article" gnus-summary-first-unread-article t]
2201          ["Best unread article" gnus-summary-best-unread-article t]
2202          ["Go to subject number..." gnus-summary-goto-subject t]
2203          ["Go to article number..." gnus-summary-goto-article t]
2204          ["Go to the last article" gnus-summary-goto-last-article t]
2205          ["Pop article off history" gnus-summary-pop-article t])
2206         ("Sort"
2207          ["Sort by number" gnus-summary-sort-by-number t]
2208          ["Sort by author" gnus-summary-sort-by-author t]
2209          ["Sort by subject" gnus-summary-sort-by-subject t]
2210          ["Sort by date" gnus-summary-sort-by-date t]
2211          ["Sort by score" gnus-summary-sort-by-score t]
2212          ["Sort by lines" gnus-summary-sort-by-lines t]
2213          ["Sort by characters" gnus-summary-sort-by-chars t]
2214          ["Original sort" gnus-summary-sort-by-original t])
2215         ("Help"
2216          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2217          ["Describe group" gnus-summary-describe-group t]
2218          ["Read manual" gnus-info-find-node t])
2219         ("Modes"
2220          ["Pick and read" gnus-pick-mode t]
2221          ["Binary" gnus-binary-mode t])
2222         ("Regeneration"
2223          ["Regenerate" gnus-summary-prepare t]
2224          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2225          ["Toggle threading" gnus-summary-toggle-threads t])
2226         ["See old articles" gnus-summary-insert-old-articles t]
2227         ["See new articles" gnus-summary-insert-new-articles t]
2228         ["Filter articles..." gnus-summary-execute-command t]
2229         ["Run command on subjects..." gnus-summary-universal-argument t]
2230         ["Search articles forward..." gnus-summary-search-article-forward t]
2231         ["Search articles backward..." gnus-summary-search-article-backward t]
2232         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2233         ["Expand window" gnus-summary-expand-window t]
2234         ["Expire expirable articles" gnus-summary-expire-articles
2235          (gnus-check-backend-function
2236           'request-expire-articles gnus-newsgroup-name)]
2237         ["Edit local kill file" gnus-summary-edit-local-kill t]
2238         ["Edit main kill file" gnus-summary-edit-global-kill t]
2239         ["Edit group parameters" gnus-summary-edit-parameters t]
2240         ["Customize group parameters" gnus-summary-customize-parameters t]
2241         ["Send a bug report" gnus-bug t]
2242         ("Exit"
2243          ["Catchup and exit" gnus-summary-catchup-and-exit
2244           ,@(if (featurep 'xemacs) '(t)
2245               '(:help "Mark unread articles in this group as read, then exit"))]
2246          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2247          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2248          ["Exit group" gnus-summary-exit
2249           ,@(if (featurep 'xemacs) '(t)
2250               '(:help "Exit current group, return to group selection mode"))]
2251          ["Exit group without updating" gnus-summary-exit-no-update t]
2252          ["Exit and goto next group" gnus-summary-next-group t]
2253          ["Exit and goto prev group" gnus-summary-prev-group t]
2254          ["Reselect group" gnus-summary-reselect-current-group t]
2255          ["Rescan group" gnus-summary-rescan-group t]
2256          ["Update dribble" gnus-summary-save-newsrc t])))
2257
2258     (gnus-run-hooks 'gnus-summary-menu-hook)))
2259
2260 (defvar gnus-summary-tool-bar-map nil)
2261
2262 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2263 (defun gnus-summary-make-tool-bar ()
2264   (if (and (fboundp 'tool-bar-add-item-from-menu)
2265            (default-value 'tool-bar-mode)
2266            (not gnus-summary-tool-bar-map))
2267       (setq gnus-summary-tool-bar-map
2268             (let ((tool-bar-map (make-sparse-keymap))
2269                   (load-path (mm-image-load-path)))
2270               (tool-bar-add-item-from-menu
2271                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2272               (tool-bar-add-item-from-menu
2273                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2274               (tool-bar-add-item-from-menu
2275                'gnus-summary-post-news "post" gnus-summary-mode-map)
2276               (tool-bar-add-item-from-menu
2277                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2278               (tool-bar-add-item-from-menu
2279                'gnus-summary-followup "followup" gnus-summary-mode-map)
2280               (tool-bar-add-item-from-menu
2281                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2282               (tool-bar-add-item-from-menu
2283                'gnus-summary-reply "reply" gnus-summary-mode-map)
2284               (tool-bar-add-item-from-menu
2285                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2286               (tool-bar-add-item-from-menu
2287                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2288               (tool-bar-add-item-from-menu
2289                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2290               (tool-bar-add-item-from-menu
2291                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2292               (tool-bar-add-item-from-menu
2293                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2294               (tool-bar-add-item-from-menu
2295                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2296               (tool-bar-add-item-from-menu
2297                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2298               (tool-bar-add-item-from-menu
2299                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2300               tool-bar-map)))
2301   (if gnus-summary-tool-bar-map
2302       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2303
2304 (defun gnus-score-set-default (var value)
2305   "A version of set that updates the GNU Emacs menu-bar."
2306   (set var value)
2307   ;; It is the message that forces the active status to be updated.
2308   (message ""))
2309
2310 (defun gnus-make-score-map (type)
2311   "Make a summary score map of type TYPE."
2312   (if t
2313       nil
2314     (let ((headers '(("author" "from" string)
2315                      ("subject" "subject" string)
2316                      ("article body" "body" string)
2317                      ("article head" "head" string)
2318                      ("xref" "xref" string)
2319                      ("extra header" "extra" string)
2320                      ("lines" "lines" number)
2321                      ("followups to author" "followup" string)))
2322           (types '((number ("less than" <)
2323                            ("greater than" >)
2324                            ("equal" =))
2325                    (string ("substring" s)
2326                            ("exact string" e)
2327                            ("fuzzy string" f)
2328                            ("regexp" r))))
2329           (perms '(("temporary" (current-time-string))
2330                    ("permanent" nil)
2331                    ("immediate" now)))
2332           header)
2333       (list
2334        (apply
2335         'nconc
2336         (list
2337          (if (eq type 'lower)
2338              "Lower score"
2339            "Increase score"))
2340         (let (outh)
2341           (while headers
2342             (setq header (car headers))
2343             (setq outh
2344                   (cons
2345                    (apply
2346                     'nconc
2347                     (list (car header))
2348                     (let ((ts (cdr (assoc (nth 2 header) types)))
2349                           outt)
2350                       (while ts
2351                         (setq outt
2352                               (cons
2353                                (apply
2354                                 'nconc
2355                                 (list (caar ts))
2356                                 (let ((ps perms)
2357                                       outp)
2358                                   (while ps
2359                                     (setq outp
2360                                           (cons
2361                                            (vector
2362                                             (caar ps)
2363                                             (list
2364                                              'gnus-summary-score-entry
2365                                              (nth 1 header)
2366                                              (if (or (string= (nth 1 header)
2367                                                               "head")
2368                                                      (string= (nth 1 header)
2369                                                               "body"))
2370                                                  ""
2371                                                (list 'gnus-summary-header
2372                                                      (nth 1 header)))
2373                                              (list 'quote (nth 1 (car ts)))
2374                                              (list 'gnus-score-delta-default
2375                                                    nil)
2376                                              (nth 1 (car ps))
2377                                              t)
2378                                             t)
2379                                            outp))
2380                                     (setq ps (cdr ps)))
2381                                   (list (nreverse outp))))
2382                                outt))
2383                         (setq ts (cdr ts)))
2384                       (list (nreverse outt))))
2385                    outh))
2386             (setq headers (cdr headers)))
2387           (list (nreverse outh))))))))
2388
2389 \f
2390
2391 (defun gnus-summary-mode (&optional group)
2392   "Major mode for reading articles.
2393
2394 All normal editing commands are switched off.
2395 \\<gnus-summary-mode-map>
2396 Each line in this buffer represents one article.  To read an
2397 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2398 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2399 respectively.
2400
2401 You can also post articles and send mail from this buffer.  To
2402 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2403 of an article, type `\\[gnus-summary-reply]'.
2404
2405 There are approx. one gazillion commands you can execute in this
2406 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2407
2408 The following commands are available:
2409
2410 \\{gnus-summary-mode-map}"
2411   (interactive)
2412   (kill-all-local-variables)
2413   (when (gnus-visual-p 'summary-menu 'menu)
2414     (gnus-summary-make-menu-bar)
2415     (gnus-summary-make-tool-bar))
2416   (gnus-summary-make-local-variables)
2417   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2418     (gnus-summary-make-local-variables))
2419   (gnus-make-thread-indent-array)
2420   (gnus-simplify-mode-line)
2421   (setq major-mode 'gnus-summary-mode)
2422   (setq mode-name "Summary")
2423   (make-local-variable 'minor-mode-alist)
2424   (use-local-map gnus-summary-mode-map)
2425   (buffer-disable-undo)
2426   (setq buffer-read-only t)             ;Disable modification
2427   (setq truncate-lines t)
2428   (setq selective-display t)
2429   (setq selective-display-ellipses t)   ;Display `...'
2430   (gnus-summary-set-display-table)
2431   (gnus-set-default-directory)
2432   (setq gnus-newsgroup-name group)
2433   (make-local-variable 'gnus-summary-line-format)
2434   (make-local-variable 'gnus-summary-line-format-spec)
2435   (make-local-variable 'gnus-summary-dummy-line-format)
2436   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2437   (make-local-variable 'gnus-summary-mark-positions)
2438   (make-local-hook 'pre-command-hook)
2439   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2440   (gnus-run-hooks 'gnus-summary-mode-hook)
2441   (turn-on-gnus-mailing-list-mode)
2442   (mm-enable-multibyte)
2443   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2444   (gnus-update-summary-mark-positions))
2445
2446 (defun gnus-summary-make-local-variables ()
2447   "Make all the local summary buffer variables."
2448   (let (global)
2449     (dolist (local gnus-summary-local-variables)
2450       (if (consp local)
2451           (progn
2452             (if (eq (cdr local) 'global)
2453                 ;; Copy the global value of the variable.
2454                 (setq global (symbol-value (car local)))
2455               ;; Use the value from the list.
2456               (setq global (eval (cdr local))))
2457             (set (make-local-variable (car local)) global))
2458         ;; Simple nil-valued local variable.
2459         (set (make-local-variable local) nil)))))
2460
2461 (defun gnus-summary-clear-local-variables ()
2462   (let ((locals gnus-summary-local-variables))
2463     (while locals
2464       (if (consp (car locals))
2465           (and (vectorp (caar locals))
2466                (set (caar locals) nil))
2467         (and (vectorp (car locals))
2468              (set (car locals) nil)))
2469       (setq locals (cdr locals)))))
2470
2471 ;; Summary data functions.
2472
2473 (defmacro gnus-data-number (data)
2474   `(car ,data))
2475
2476 (defmacro gnus-data-set-number (data number)
2477   `(setcar ,data ,number))
2478
2479 (defmacro gnus-data-mark (data)
2480   `(nth 1 ,data))
2481
2482 (defmacro gnus-data-set-mark (data mark)
2483   `(setcar (nthcdr 1 ,data) ,mark))
2484
2485 (defmacro gnus-data-pos (data)
2486   `(nth 2 ,data))
2487
2488 (defmacro gnus-data-set-pos (data pos)
2489   `(setcar (nthcdr 2 ,data) ,pos))
2490
2491 (defmacro gnus-data-header (data)
2492   `(nth 3 ,data))
2493
2494 (defmacro gnus-data-set-header (data header)
2495   `(setf (nth 3 ,data) ,header))
2496
2497 (defmacro gnus-data-level (data)
2498   `(nth 4 ,data))
2499
2500 (defmacro gnus-data-unread-p (data)
2501   `(= (nth 1 ,data) gnus-unread-mark))
2502
2503 (defmacro gnus-data-read-p (data)
2504   `(/= (nth 1 ,data) gnus-unread-mark))
2505
2506 (defmacro gnus-data-pseudo-p (data)
2507   `(consp (nth 3 ,data)))
2508
2509 (defmacro gnus-data-find (number)
2510   `(assq ,number gnus-newsgroup-data))
2511
2512 (defmacro gnus-data-find-list (number &optional data)
2513   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2514      (memq (assq ,number bdata)
2515            bdata)))
2516
2517 (defmacro gnus-data-make (number mark pos header level)
2518   `(list ,number ,mark ,pos ,header ,level))
2519
2520 (defun gnus-data-enter (after-article number mark pos header level offset)
2521   (let ((data (gnus-data-find-list after-article)))
2522     (unless data
2523       (error "No such article: %d" after-article))
2524     (setcdr data (cons (gnus-data-make number mark pos header level)
2525                        (cdr data)))
2526     (setq gnus-newsgroup-data-reverse nil)
2527     (gnus-data-update-list (cddr data) offset)))
2528
2529 (defun gnus-data-enter-list (after-article list &optional offset)
2530   (when list
2531     (let ((data (and after-article (gnus-data-find-list after-article)))
2532           (ilist list))
2533       (if (not (or data
2534                    after-article))
2535           (let ((odata gnus-newsgroup-data))
2536             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2537             (when offset
2538               (gnus-data-update-list odata offset)))
2539       ;; Find the last element in the list to be spliced into the main
2540         ;; list.
2541         (while (cdr list)
2542           (setq list (cdr list)))
2543         (if (not data)
2544             (progn
2545               (setcdr list gnus-newsgroup-data)
2546               (setq gnus-newsgroup-data ilist)
2547               (when offset
2548                 (gnus-data-update-list (cdr list) offset)))
2549           (setcdr list (cdr data))
2550           (setcdr data ilist)
2551           (when offset
2552             (gnus-data-update-list (cdr list) offset))))
2553       (setq gnus-newsgroup-data-reverse nil))))
2554
2555 (defun gnus-data-remove (article &optional offset)
2556   (let ((data gnus-newsgroup-data))
2557     (if (= (gnus-data-number (car data)) article)
2558         (progn
2559           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2560                 gnus-newsgroup-data-reverse nil)
2561           (when offset
2562             (gnus-data-update-list gnus-newsgroup-data offset)))
2563       (while (cdr data)
2564         (when (= (gnus-data-number (cadr data)) article)
2565           (setcdr data (cddr data))
2566           (when offset
2567             (gnus-data-update-list (cdr data) offset))
2568           (setq data nil
2569                 gnus-newsgroup-data-reverse nil))
2570         (setq data (cdr data))))))
2571
2572 (defmacro gnus-data-list (backward)
2573   `(if ,backward
2574        (or gnus-newsgroup-data-reverse
2575            (setq gnus-newsgroup-data-reverse
2576                  (reverse gnus-newsgroup-data)))
2577      gnus-newsgroup-data))
2578
2579 (defun gnus-data-update-list (data offset)
2580   "Add OFFSET to the POS of all data entries in DATA."
2581   (setq gnus-newsgroup-data-reverse nil)
2582   (while data
2583     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2584     (setq data (cdr data))))
2585
2586 (defun gnus-summary-article-pseudo-p (article)
2587   "Say whether this article is a pseudo article or not."
2588   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2589
2590 (defmacro gnus-summary-article-sparse-p (article)
2591   "Say whether this article is a sparse article or not."
2592   `(memq ,article gnus-newsgroup-sparse))
2593
2594 (defmacro gnus-summary-article-ancient-p (article)
2595   "Say whether this article is a sparse article or not."
2596   `(memq ,article gnus-newsgroup-ancient))
2597
2598 (defun gnus-article-parent-p (number)
2599   "Say whether this article is a parent or not."
2600   (let ((data (gnus-data-find-list number)))
2601     (and (cdr data)              ; There has to be an article after...
2602          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2603             (gnus-data-level (nth 1 data))))))
2604
2605 (defun gnus-article-children (number)
2606   "Return a list of all children to NUMBER."
2607   (let* ((data (gnus-data-find-list number))
2608          (level (gnus-data-level (car data)))
2609          children)
2610     (setq data (cdr data))
2611     (while (and data
2612                 (= (gnus-data-level (car data)) (1+ level)))
2613       (push (gnus-data-number (car data)) children)
2614       (setq data (cdr data)))
2615     children))
2616
2617 (defmacro gnus-summary-skip-intangible ()
2618   "If the current article is intangible, then jump to a different article."
2619   '(let ((to (get-text-property (point) 'gnus-intangible)))
2620      (and to (gnus-summary-goto-subject to))))
2621
2622 (defmacro gnus-summary-article-intangible-p ()
2623   "Say whether this article is intangible or not."
2624   '(get-text-property (point) 'gnus-intangible))
2625
2626 (defun gnus-article-read-p (article)
2627   "Say whether ARTICLE is read or not."
2628   (not (or (memq article gnus-newsgroup-marked)
2629            (memq article gnus-newsgroup-unreads)
2630            (memq article gnus-newsgroup-unselected)
2631            (memq article gnus-newsgroup-dormant))))
2632
2633 ;; Some summary mode macros.
2634
2635 (defmacro gnus-summary-article-number ()
2636   "The article number of the article on the current line.
2637 If there isn's an article number here, then we return the current
2638 article number."
2639   '(progn
2640      (gnus-summary-skip-intangible)
2641      (or (get-text-property (point) 'gnus-number)
2642          (gnus-summary-last-subject))))
2643
2644 (defmacro gnus-summary-article-header (&optional number)
2645   "Return the header of article NUMBER."
2646   `(gnus-data-header (gnus-data-find
2647                       ,(or number '(gnus-summary-article-number)))))
2648
2649 (defmacro gnus-summary-thread-level (&optional number)
2650   "Return the level of thread that starts with article NUMBER."
2651   `(if (and (eq gnus-summary-make-false-root 'dummy)
2652             (get-text-property (point) 'gnus-intangible))
2653        0
2654      (gnus-data-level (gnus-data-find
2655                        ,(or number '(gnus-summary-article-number))))))
2656
2657 (defmacro gnus-summary-article-mark (&optional number)
2658   "Return the mark of article NUMBER."
2659   `(gnus-data-mark (gnus-data-find
2660                     ,(or number '(gnus-summary-article-number)))))
2661
2662 (defmacro gnus-summary-article-pos (&optional number)
2663   "Return the position of the line of article NUMBER."
2664   `(gnus-data-pos (gnus-data-find
2665                    ,(or number '(gnus-summary-article-number)))))
2666
2667 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2668 (defmacro gnus-summary-article-subject (&optional number)
2669   "Return current subject string or nil if nothing."
2670   `(let ((headers
2671           ,(if number
2672                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2673              '(gnus-data-header (assq (gnus-summary-article-number)
2674                                       gnus-newsgroup-data)))))
2675      (and headers
2676           (vectorp headers)
2677           (mail-header-subject headers))))
2678
2679 (defmacro gnus-summary-article-score (&optional number)
2680   "Return current article score."
2681   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2682                   gnus-newsgroup-scored))
2683        gnus-summary-default-score 0))
2684
2685 (defun gnus-summary-article-children (&optional number)
2686   "Return a list of article numbers that are children of article NUMBER."
2687   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2688          (level (gnus-data-level (car data)))
2689          l children)
2690     (while (and (setq data (cdr data))
2691                 (> (setq l (gnus-data-level (car data))) level))
2692       (and (= (1+ level) l)
2693            (push (gnus-data-number (car data))
2694                  children)))
2695     (nreverse children)))
2696
2697 (defun gnus-summary-article-parent (&optional number)
2698   "Return the article number of the parent of article NUMBER."
2699   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2700                                     (gnus-data-list t)))
2701          (level (gnus-data-level (car data))))
2702     (if (zerop level)
2703         ()                              ; This is a root.
2704       ;; We search until we find an article with a level less than
2705       ;; this one.  That function has to be the parent.
2706       (while (and (setq data (cdr data))
2707                   (not (< (gnus-data-level (car data)) level))))
2708       (and data (gnus-data-number (car data))))))
2709
2710 (defun gnus-unread-mark-p (mark)
2711   "Say whether MARK is the unread mark."
2712   (= mark gnus-unread-mark))
2713
2714 (defun gnus-read-mark-p (mark)
2715   "Say whether MARK is one of the marks that mark as read.
2716 This is all marks except unread, ticked, dormant, and expirable."
2717   (not (or (= mark gnus-unread-mark)
2718            (= mark gnus-ticked-mark)
2719            (= mark gnus-dormant-mark)
2720            (= mark gnus-expirable-mark))))
2721
2722 (defmacro gnus-article-mark (number)
2723   "Return the MARK of article NUMBER.
2724 This macro should only be used when computing the mark the \"first\"
2725 time; i.e., when generating the summary lines.  After that,
2726 `gnus-summary-article-mark' should be used to examine the
2727 marks of articles."
2728   `(cond
2729     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2730     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2731     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2732     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2733     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2734     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2735     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2736     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2737            gnus-ancient-mark))))
2738
2739 ;; Saving hidden threads.
2740
2741 (defmacro gnus-save-hidden-threads (&rest forms)
2742   "Save hidden threads, eval FORMS, and restore the hidden threads."
2743   (let ((config (make-symbol "config")))
2744     `(let ((,config (gnus-hidden-threads-configuration)))
2745        (unwind-protect
2746            (save-excursion
2747              ,@forms)
2748          (gnus-restore-hidden-threads-configuration ,config)))))
2749 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2750 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2751
2752 (defun gnus-data-compute-positions ()
2753   "Compute the positions of all articles."
2754   (setq gnus-newsgroup-data-reverse nil)
2755   (let ((data gnus-newsgroup-data))
2756     (save-excursion
2757       (gnus-save-hidden-threads
2758         (gnus-summary-show-all-threads)
2759         (goto-char (point-min))
2760         (while data
2761           (while (get-text-property (point) 'gnus-intangible)
2762             (forward-line 1))
2763           (gnus-data-set-pos (car data) (+ (point) 3))
2764           (setq data (cdr data))
2765           (forward-line 1))))))
2766
2767 (defun gnus-hidden-threads-configuration ()
2768   "Return the current hidden threads configuration."
2769   (save-excursion
2770     (let (config)
2771       (goto-char (point-min))
2772       (while (search-forward "\r" nil t)
2773         (push (1- (point)) config))
2774       config)))
2775
2776 (defun gnus-restore-hidden-threads-configuration (config)
2777   "Restore hidden threads configuration from CONFIG."
2778   (save-excursion
2779     (let (point buffer-read-only)
2780       (while (setq point (pop config))
2781         (when (and (< point (point-max))
2782                    (goto-char point)
2783                    (eq (char-after) ?\n))
2784           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2785
2786 ;; Various summary mode internalish functions.
2787
2788 (defun gnus-mouse-pick-article (e)
2789   (interactive "e")
2790   (mouse-set-point e)
2791   (gnus-summary-next-page nil t))
2792
2793 (defun gnus-summary-set-display-table ()
2794   "Change the display table.
2795 Odd characters have a tendency to mess
2796 up nicely formatted displays - we make all possible glyphs
2797 display only a single character."
2798
2799   ;; We start from the standard display table, if any.
2800   (let ((table (or (copy-sequence standard-display-table)
2801                    (make-display-table)))
2802         (i 32))
2803     ;; Nix out all the control chars...
2804     (while (>= (setq i (1- i)) 0)
2805       (aset table i [??]))
2806    ;; ... but not newline and cr, of course.  (cr is necessary for the
2807     ;; selective display).
2808     (aset table ?\n nil)
2809     (aset table ?\r nil)
2810     ;; We keep TAB as well.
2811     (aset table ?\t nil)
2812     ;; We nix out any glyphs over 126 that are not set already.
2813     (let ((i 256))
2814       (while (>= (setq i (1- i)) 127)
2815         ;; Only modify if the entry is nil.
2816         (unless (aref table i)
2817           (aset table i [??]))))
2818     (setq buffer-display-table table)))
2819
2820 (defun gnus-summary-set-article-display-arrow (pos)
2821   "Update the overlay arrow to point to line at position POS."
2822   (when (and gnus-summary-display-arrow
2823              (boundp 'overlay-arrow-position)
2824              (boundp 'overlay-arrow-string))
2825     (save-excursion
2826       (goto-char pos)
2827       (beginning-of-line)
2828       (unless overlay-arrow-position
2829         (setq overlay-arrow-position (make-marker)))
2830       (setq overlay-arrow-string "=>"
2831             overlay-arrow-position (set-marker overlay-arrow-position
2832                                                (point)
2833                                                (current-buffer))))))
2834
2835 (defun gnus-summary-buffer-name (group)
2836   "Return the summary buffer name of GROUP."
2837   (concat "*Summary " (gnus-group-decoded-name group) "*"))
2838
2839 (defun gnus-summary-setup-buffer (group)
2840   "Initialize summary buffer."
2841   (let ((buffer (gnus-summary-buffer-name group))
2842         (dead-name (concat "*Dead Summary "
2843                            (gnus-group-decoded-name group) "*")))
2844     ;; If a dead summary buffer exists, we kill it.
2845     (when (gnus-buffer-live-p dead-name)
2846       (gnus-kill-buffer dead-name))
2847     (if (get-buffer buffer)
2848         (progn
2849           (set-buffer buffer)
2850           (setq gnus-summary-buffer (current-buffer))
2851           (not gnus-newsgroup-prepared))
2852       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2853       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2854       (gnus-summary-mode group)
2855       (when gnus-carpal
2856         (gnus-carpal-setup-buffer 'summary))
2857       (unless gnus-single-article-buffer
2858         (make-local-variable 'gnus-article-buffer)
2859         (make-local-variable 'gnus-article-current)
2860         (make-local-variable 'gnus-original-article-buffer))
2861       (setq gnus-newsgroup-name group)
2862       ;; Set any local variables in the group parameters.
2863       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2864       t)))
2865
2866 (defun gnus-set-global-variables ()
2867   "Set the global equivalents of the buffer-local variables.
2868 They are set to the latest values they had.  These reflect the summary
2869 buffer that was in action when the last article was fetched."
2870   (when (eq major-mode 'gnus-summary-mode)
2871     (setq gnus-summary-buffer (current-buffer))
2872     (let ((name gnus-newsgroup-name)
2873           (marked gnus-newsgroup-marked)
2874           (unread gnus-newsgroup-unreads)
2875           (headers gnus-current-headers)
2876           (data gnus-newsgroup-data)
2877           (summary gnus-summary-buffer)
2878           (article-buffer gnus-article-buffer)
2879           (original gnus-original-article-buffer)
2880           (gac gnus-article-current)
2881           (reffed gnus-reffed-article-number)
2882           (score-file gnus-current-score-file)
2883           (default-charset gnus-newsgroup-charset)
2884           vlist)
2885       (let ((locals gnus-newsgroup-variables))
2886         (while locals
2887           (if (consp (car locals))
2888               (push (eval (caar locals)) vlist)
2889             (push (eval (car locals)) vlist))
2890           (setq locals (cdr locals)))
2891         (setq vlist (nreverse vlist)))
2892       (save-excursion
2893         (set-buffer gnus-group-buffer)
2894         (setq gnus-newsgroup-name name
2895               gnus-newsgroup-marked marked
2896               gnus-newsgroup-unreads unread
2897               gnus-current-headers headers
2898               gnus-newsgroup-data data
2899               gnus-article-current gac
2900               gnus-summary-buffer summary
2901               gnus-article-buffer article-buffer
2902               gnus-original-article-buffer original
2903               gnus-reffed-article-number reffed
2904               gnus-current-score-file score-file
2905               gnus-newsgroup-charset default-charset)
2906         (let ((locals gnus-newsgroup-variables))
2907           (while locals
2908             (if (consp (car locals))
2909                 (set (caar locals) (pop vlist))
2910               (set (car locals) (pop vlist)))
2911             (setq locals (cdr locals))))
2912         ;; The article buffer also has local variables.
2913         (when (gnus-buffer-live-p gnus-article-buffer)
2914           (set-buffer gnus-article-buffer)
2915           (setq gnus-summary-buffer summary))))))
2916
2917 (defun gnus-summary-article-unread-p (article)
2918   "Say whether ARTICLE is unread or not."
2919   (memq article gnus-newsgroup-unreads))
2920
2921 (defun gnus-summary-first-article-p (&optional article)
2922   "Return whether ARTICLE is the first article in the buffer."
2923   (if (not (setq article (or article (gnus-summary-article-number))))
2924       nil
2925     (eq article (caar gnus-newsgroup-data))))
2926
2927 (defun gnus-summary-last-article-p (&optional article)
2928   "Return whether ARTICLE is the last article in the buffer."
2929   (if (not (setq article (or article (gnus-summary-article-number))))
2930       ;; All non-existent numbers are the last article.  :-)
2931       t
2932     (not (cdr (gnus-data-find-list article)))))
2933
2934 (defun gnus-make-thread-indent-array ()
2935   (let ((n 200))
2936     (unless (and gnus-thread-indent-array
2937                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2938       (setq gnus-thread-indent-array (make-vector 201 "")
2939             gnus-thread-indent-array-level gnus-thread-indent-level)
2940       (while (>= n 0)
2941         (aset gnus-thread-indent-array n
2942               (make-string (* n gnus-thread-indent-level) ? ))
2943         (setq n (1- n))))))
2944
2945 (defun gnus-update-summary-mark-positions ()
2946   "Compute where the summary marks are to go."
2947   (save-excursion
2948     (when (gnus-buffer-exists-p gnus-summary-buffer)
2949       (set-buffer gnus-summary-buffer))
2950     (let ((gnus-replied-mark 129)
2951           (gnus-score-below-mark 130)
2952           (gnus-score-over-mark 130)
2953           (gnus-download-mark 131)
2954           (spec gnus-summary-line-format-spec)
2955           gnus-visual pos)
2956       (save-excursion
2957         (gnus-set-work-buffer)
2958         (let ((gnus-summary-line-format-spec spec)
2959               (gnus-newsgroup-downloadable '((0 . t))))
2960           (gnus-summary-insert-line
2961            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
2962            0 nil 128 t nil "" nil 1)
2963           (goto-char (point-min))
2964           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2965                                              (- (point) 2)))))
2966           (goto-char (point-min))
2967           (push (cons 'replied (and (search-forward "\201" nil t)
2968                                     (- (point) 2)))
2969                 pos)
2970           (goto-char (point-min))
2971           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2972                 pos)
2973           (goto-char (point-min))
2974           (push (cons 'download
2975                       (and (search-forward "\203" nil t) (- (point) 2)))
2976                 pos)))
2977       (setq gnus-summary-mark-positions pos))))
2978
2979 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2980   "Insert a dummy root in the summary buffer."
2981   (beginning-of-line)
2982   (gnus-add-text-properties
2983    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2984    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2985
2986 (defun gnus-summary-extract-address-component (from)
2987   (or (car (funcall gnus-extract-address-components from))
2988       from))
2989
2990 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
2991   (let ((mail-parse-charset gnus-newsgroup-charset)
2992         ; Is it really necessary to do this next part for each summary line?
2993         ; Luckily, doesn't seem to slow things down much.
2994         (mail-parse-ignored-charsets
2995          (save-excursion (set-buffer gnus-summary-buffer)
2996                          gnus-newsgroup-ignored-charsets)))
2997     (or
2998      (and gnus-ignored-from-addresses
2999           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3000           (let ((extra-headers (mail-header-extra header))
3001                 to
3002                 newsgroups)
3003             (cond
3004              ((setq to (cdr (assq 'To extra-headers)))
3005               (concat "-> "
3006                       (inline
3007                         (gnus-summary-extract-address-component
3008                          (funcall gnus-decode-encoded-word-function to)))))
3009              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3010               (concat "=> " newsgroups)))))
3011      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3012
3013 (defun gnus-summary-insert-line (gnus-tmp-header
3014                                  gnus-tmp-level gnus-tmp-current
3015                                  gnus-tmp-unread gnus-tmp-replied
3016                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3017                                  &optional gnus-tmp-dummy gnus-tmp-score
3018                                  gnus-tmp-process)
3019   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3020          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3021          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3022          (gnus-tmp-score-char
3023           (if (or (null gnus-summary-default-score)
3024                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3025                       gnus-summary-zcore-fuzz))
3026               ?                         ;Whitespace
3027             (if (< gnus-tmp-score gnus-summary-default-score)
3028                 gnus-score-below-mark gnus-score-over-mark)))
3029          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3030          (gnus-tmp-replied
3031           (cond (gnus-tmp-process gnus-process-mark)
3032                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3033                  gnus-cached-mark)
3034                 (gnus-tmp-replied gnus-replied-mark)
3035                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3036                  gnus-forwarded-mark)
3037                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3038                  gnus-saved-mark)
3039                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3040                  gnus-recent-mark)
3041                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3042                  gnus-unseen-mark)
3043                 (t gnus-no-mark)))
3044          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3045          (gnus-tmp-name
3046           (cond
3047            ((string-match "<[^>]+> *$" gnus-tmp-from)
3048             (let ((beg (match-beginning 0)))
3049               (or (and (string-match "^\".+\"" gnus-tmp-from)
3050                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3051                   (substring gnus-tmp-from 0 beg))))
3052            ((string-match "(.+)" gnus-tmp-from)
3053             (substring gnus-tmp-from
3054                        (1+ (match-beginning 0)) (1- (match-end 0))))
3055            (t gnus-tmp-from)))
3056          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3057          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3058          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3059          (buffer-read-only nil))
3060     (when (string= gnus-tmp-name "")
3061       (setq gnus-tmp-name gnus-tmp-from))
3062     (unless (numberp gnus-tmp-lines)
3063       (setq gnus-tmp-lines -1))
3064     (if (= gnus-tmp-lines -1)
3065         (setq gnus-tmp-lines "?")
3066       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3067     (gnus-put-text-property
3068      (point)
3069      (progn (eval gnus-summary-line-format-spec) (point))
3070      'gnus-number gnus-tmp-number)
3071     (when (gnus-visual-p 'summary-highlight 'highlight)
3072       (forward-line -1)
3073       (gnus-run-hooks 'gnus-summary-update-hook)
3074       (forward-line 1))))
3075
3076 (defun gnus-summary-update-line (&optional dont-update)
3077   "Update summary line after change."
3078   (when (and gnus-summary-default-score
3079              (not gnus-summary-inhibit-highlight))
3080     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3081            (article (gnus-summary-article-number))
3082            (score (gnus-summary-article-score article)))
3083       (unless dont-update
3084         (if (and gnus-summary-mark-below
3085                  (< (gnus-summary-article-score)
3086                     gnus-summary-mark-below))
3087             ;; This article has a low score, so we mark it as read.
3088             (when (memq article gnus-newsgroup-unreads)
3089               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3090           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3091             ;; This article was previously marked as read on account
3092             ;; of a low score, but now it has risen, so we mark it as
3093             ;; unread.
3094             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3095         (gnus-summary-update-mark
3096          (if (or (null gnus-summary-default-score)
3097                  (<= (abs (- score gnus-summary-default-score))
3098                      gnus-summary-zcore-fuzz))
3099              ?                          ;Whitespace
3100            (if (< score gnus-summary-default-score)
3101                gnus-score-below-mark gnus-score-over-mark))
3102          'score))
3103       ;; Do visual highlighting.
3104       (when (gnus-visual-p 'summary-highlight 'highlight)
3105         (gnus-run-hooks 'gnus-summary-update-hook)))))
3106
3107 (defvar gnus-tmp-new-adopts nil)
3108
3109 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3110   "Return the number of articles in THREAD.
3111 This may be 0 in some cases -- if none of the articles in
3112 the thread are to be displayed."
3113   (let* ((number
3114          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3115           (cond
3116            ((not (listp thread))
3117             1)
3118            ((and (consp thread) (cdr thread))
3119             (apply
3120              '+ 1 (mapcar
3121                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3122            ((null thread)
3123             1)
3124            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3125             1)
3126            (t 0))))
3127     (when (and level (zerop level) gnus-tmp-new-adopts)
3128       (incf number
3129             (apply '+ (mapcar
3130                        'gnus-summary-number-of-articles-in-thread
3131                        gnus-tmp-new-adopts))))
3132     (if char
3133         (if (> number 1) gnus-not-empty-thread-mark
3134           gnus-empty-thread-mark)
3135       number)))
3136
3137 (defun gnus-summary-set-local-parameters (group)
3138   "Go through the local params of GROUP and set all variable specs in that list."
3139   (let ((params (gnus-group-find-parameter group))
3140         (vars '(quit-config))           ; Ignore quit-config.
3141         elem)
3142     (while params
3143       (setq elem (car params)
3144             params (cdr params))
3145       (and (consp elem)                 ; Has to be a cons.
3146            (consp (cdr elem))           ; The cdr has to be a list.
3147            (symbolp (car elem))         ; Has to be a symbol in there.
3148            (not (memq (car elem) vars))
3149            (ignore-errors               ; So we set it.
3150              (push (car elem) vars)
3151              (make-local-variable (car elem))
3152              (set (car elem) (eval (nth 1 elem))))))))
3153
3154 (defun gnus-summary-read-group (group &optional show-all no-article
3155                                       kill-buffer no-display backward
3156                                       select-articles)
3157   "Start reading news in newsgroup GROUP.
3158 If SHOW-ALL is non-nil, already read articles are also listed.
3159 If NO-ARTICLE is non-nil, no article is selected initially.
3160 If NO-DISPLAY, don't generate a summary buffer."
3161   (let (result)
3162     (while (and group
3163                 (null (setq result
3164                             (let ((gnus-auto-select-next nil))
3165                               (or (gnus-summary-read-group-1
3166                                    group show-all no-article
3167                                    kill-buffer no-display
3168                                    select-articles)
3169                                   (setq show-all nil
3170                                         select-articles nil)))))
3171                 (eq gnus-auto-select-next 'quietly))
3172       (set-buffer gnus-group-buffer)
3173       ;; The entry function called above goes to the next
3174       ;; group automatically, so we go two groups back
3175       ;; if we are searching for the previous group.
3176       (when backward
3177         (gnus-group-prev-unread-group 2))
3178       (if (not (equal group (gnus-group-group-name)))
3179           (setq group (gnus-group-group-name))
3180         (setq group nil)))
3181     result))
3182
3183 (defun gnus-summary-read-group-1 (group show-all no-article
3184                                         kill-buffer no-display
3185                                         &optional select-articles)
3186   ;; Killed foreign groups can't be entered.
3187   ;;  (when (and (not (gnus-group-native-p group))
3188   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3189   ;;    (error "Dead non-native groups can't be entered"))
3190   (gnus-message 5 "Retrieving newsgroup: %s..."
3191                 (gnus-group-decoded-name group))
3192   (let* ((new-group (gnus-summary-setup-buffer group))
3193          (quit-config (gnus-group-quit-config group))
3194          (did-select (and new-group (gnus-select-newsgroup
3195                                      group show-all select-articles))))
3196     (cond
3197      ;; This summary buffer exists already, so we just select it.
3198      ((not new-group)
3199       (gnus-set-global-variables)
3200       (when kill-buffer
3201         (gnus-kill-or-deaden-summary kill-buffer))
3202       (gnus-configure-windows 'summary 'force)
3203       (gnus-set-mode-line 'summary)
3204       (gnus-summary-position-point)
3205       (message "")
3206       t)
3207      ;; We couldn't select this group.
3208      ((null did-select)
3209       (when (and (eq major-mode 'gnus-summary-mode)
3210                  (not (equal (current-buffer) kill-buffer)))
3211         (kill-buffer (current-buffer))
3212         (if (not quit-config)
3213             (progn
3214               ;; Update the info -- marks might need to be removed,
3215               ;; for instance.
3216               (gnus-summary-update-info)
3217               (set-buffer gnus-group-buffer)
3218               (gnus-group-jump-to-group group)
3219               (gnus-group-next-unread-group 1))
3220           (gnus-handle-ephemeral-exit quit-config)))
3221       (let ((grpinfo (gnus-get-info group)))
3222         (if (null (gnus-info-read grpinfo))
3223             (gnus-message 3 "Group %s contains no messages"
3224                           (gnus-group-decoded-name group))
3225           (gnus-message 3 "Can't select group")))
3226       nil)
3227      ;; The user did a `C-g' while prompting for number of articles,
3228      ;; so we exit this group.
3229      ((eq did-select 'quit)
3230       (and (eq major-mode 'gnus-summary-mode)
3231            (not (equal (current-buffer) kill-buffer))
3232            (kill-buffer (current-buffer)))
3233       (when kill-buffer
3234         (gnus-kill-or-deaden-summary kill-buffer))
3235       (if (not quit-config)
3236           (progn
3237             (set-buffer gnus-group-buffer)
3238             (gnus-group-jump-to-group group)
3239             (gnus-group-next-unread-group 1)
3240             (gnus-configure-windows 'group 'force))
3241         (gnus-handle-ephemeral-exit quit-config))
3242       ;; Finally signal the quit.
3243       (signal 'quit nil))
3244      ;; The group was successfully selected.
3245      (t
3246       (gnus-set-global-variables)
3247       ;; Save the active value in effect when the group was entered.
3248       (setq gnus-newsgroup-active
3249             (gnus-copy-sequence
3250              (gnus-active gnus-newsgroup-name)))
3251       ;; You can change the summary buffer in some way with this hook.
3252       (gnus-run-hooks 'gnus-select-group-hook)
3253       (gnus-update-format-specifications
3254        nil 'summary 'summary-mode 'summary-dummy)
3255       (gnus-update-summary-mark-positions)
3256       ;; Do score processing.
3257       (when gnus-use-scoring
3258         (gnus-possibly-score-headers))
3259       ;; Check whether to fill in the gaps in the threads.
3260       (when gnus-build-sparse-threads
3261         (gnus-build-sparse-threads))
3262       ;; Find the initial limit.
3263       (if gnus-show-threads
3264           (if show-all
3265               (let ((gnus-newsgroup-dormant nil))
3266                 (gnus-summary-initial-limit show-all))
3267             (gnus-summary-initial-limit show-all))
3268         ;; When untreaded, all articles are always shown.
3269         (setq gnus-newsgroup-limit
3270               (mapcar
3271                (lambda (header) (mail-header-number header))
3272                gnus-newsgroup-headers)))
3273       ;; Generate the summary buffer.
3274       (unless no-display
3275         (gnus-summary-prepare))
3276       (when gnus-use-trees
3277         (gnus-tree-open group)
3278         (setq gnus-summary-highlight-line-function
3279               'gnus-tree-highlight-article))
3280       ;; If the summary buffer is empty, but there are some low-scored
3281       ;; articles or some excluded dormants, we include these in the
3282       ;; buffer.
3283       (when (and (zerop (buffer-size))
3284                  (not no-display))
3285         (cond (gnus-newsgroup-dormant
3286                (gnus-summary-limit-include-dormant))
3287               ((and gnus-newsgroup-scored show-all)
3288                (gnus-summary-limit-include-expunged t))))
3289       ;; Function `gnus-apply-kill-file' must be called in this hook.
3290       (gnus-run-hooks 'gnus-apply-kill-hook)
3291       (if (and (zerop (buffer-size))
3292                (not no-display))
3293           (progn
3294             ;; This newsgroup is empty.
3295             (gnus-summary-catchup-and-exit nil t)
3296             (gnus-message 6 "No unread news")
3297             (when kill-buffer
3298               (gnus-kill-or-deaden-summary kill-buffer))
3299             ;; Return nil from this function.
3300             nil)
3301         ;; Hide conversation thread subtrees.  We cannot do this in
3302         ;; gnus-summary-prepare-hook since kill processing may not
3303         ;; work with hidden articles.
3304         (gnus-summary-maybe-hide-threads)
3305         (when kill-buffer
3306           (gnus-kill-or-deaden-summary kill-buffer))
3307         (gnus-summary-auto-select-subject)
3308         ;; Show first unread article if requested.
3309         (if (and (not no-article)
3310                  (not no-display)
3311                  gnus-newsgroup-unreads
3312                  gnus-auto-select-first)
3313             (progn
3314               (gnus-configure-windows 'summary)
3315               (let ((art (gnus-summary-article-number)))
3316                 (unless (or (memq art gnus-newsgroup-undownloaded)
3317                             (memq art gnus-newsgroup-downloadable))
3318                   (gnus-summary-goto-article art))))
3319           ;; Don't select any articles.
3320           (gnus-summary-position-point)
3321           (gnus-configure-windows 'summary 'force)
3322           (gnus-set-mode-line 'summary))
3323         (when (get-buffer-window gnus-group-buffer t)
3324           ;; Gotta use windows, because recenter does weird stuff if
3325           ;; the current buffer ain't the displayed window.
3326           (let ((owin (selected-window)))
3327             (select-window (get-buffer-window gnus-group-buffer t))
3328             (when (gnus-group-goto-group group)
3329               (recenter))
3330             (select-window owin)))
3331         ;; Mark this buffer as "prepared".
3332         (setq gnus-newsgroup-prepared t)
3333         (gnus-run-hooks 'gnus-summary-prepared-hook)
3334         t)))))
3335
3336 (defun gnus-summary-auto-select-subject ()
3337   "Select the subject line on initial group entry."
3338   (goto-char (point-min))
3339   (cond
3340    ((eq gnus-auto-select-subject 'best)
3341     (gnus-summary-best-unread-subject))
3342    ((eq gnus-auto-select-subject 'unread)
3343     (gnus-summary-first-unread-subject))
3344    ((eq gnus-auto-select-subject 'unseen)
3345     (gnus-summary-first-unseen-subject))
3346    ((eq gnus-auto-select-subject 'unseen-or-unread)
3347     (gnus-summary-first-unseen-or-unread-subject))
3348    ((eq gnus-auto-select-subject 'first)
3349     ;; Do nothing.
3350     )
3351    ((gnus-functionp gnus-auto-select-subject)
3352     (funcall gnus-auto-select-subject))))
3353
3354 (defun gnus-summary-prepare ()
3355   "Generate the summary buffer."
3356   (interactive)
3357   (let ((buffer-read-only nil))
3358     (erase-buffer)
3359     (setq gnus-newsgroup-data nil
3360           gnus-newsgroup-data-reverse nil)
3361     (gnus-run-hooks 'gnus-summary-generate-hook)
3362     ;; Generate the buffer, either with threads or without.
3363     (when gnus-newsgroup-headers
3364       (gnus-summary-prepare-threads
3365        (if gnus-show-threads
3366            (gnus-sort-gathered-threads
3367             (funcall gnus-summary-thread-gathering-function
3368                      (gnus-sort-threads
3369                       (gnus-cut-threads (gnus-make-threads)))))
3370          ;; Unthreaded display.
3371          (gnus-sort-articles gnus-newsgroup-headers))))
3372     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3373     ;; Call hooks for modifying summary buffer.
3374     (goto-char (point-min))
3375     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3376
3377 (defsubst gnus-general-simplify-subject (subject)
3378   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3379   (setq subject
3380         (cond
3381          ;; Truncate the subject.
3382          (gnus-simplify-subject-functions
3383           (gnus-map-function gnus-simplify-subject-functions subject))
3384          ((numberp gnus-summary-gather-subject-limit)
3385           (setq subject (gnus-simplify-subject-re subject))
3386           (if (> (length subject) gnus-summary-gather-subject-limit)
3387               (substring subject 0 gnus-summary-gather-subject-limit)
3388             subject))
3389          ;; Fuzzily simplify it.
3390          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3391           (gnus-simplify-subject-fuzzy subject))
3392          ;; Just remove the leading "Re:".
3393          (t
3394           (gnus-simplify-subject-re subject))))
3395
3396   (if (and gnus-summary-gather-exclude-subject
3397            (string-match gnus-summary-gather-exclude-subject subject))
3398       nil                         ; This article shouldn't be gathered
3399     subject))
3400
3401 (defun gnus-summary-simplify-subject-query ()
3402   "Query where the respool algorithm would put this article."
3403   (interactive)
3404   (gnus-summary-select-article)
3405   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3406
3407 (defun gnus-gather-threads-by-subject (threads)
3408   "Gather threads by looking at Subject headers."
3409   (if (not gnus-summary-make-false-root)
3410       threads
3411     (let ((hashtb (gnus-make-hashtable 1024))
3412           (prev threads)
3413           (result threads)
3414           subject hthread whole-subject)
3415       (while threads
3416         (setq subject (gnus-general-simplify-subject
3417                        (setq whole-subject (mail-header-subject
3418                                             (caar threads)))))
3419         (when subject
3420           (if (setq hthread (gnus-gethash subject hashtb))
3421               (progn
3422                 ;; We enter a dummy root into the thread, if we
3423                 ;; haven't done that already.
3424                 (unless (stringp (caar hthread))
3425                   (setcar hthread (list whole-subject (car hthread))))
3426                 ;; We add this new gathered thread to this gathered
3427                 ;; thread.
3428                 (setcdr (car hthread)
3429                         (nconc (cdar hthread) (list (car threads))))
3430                 ;; Remove it from the list of threads.
3431                 (setcdr prev (cdr threads))
3432                 (setq threads prev))
3433             ;; Enter this thread into the hash table.
3434             (gnus-sethash subject threads hashtb)))
3435         (setq prev threads)
3436         (setq threads (cdr threads)))
3437       result)))
3438
3439 (defun gnus-gather-threads-by-references (threads)
3440   "Gather threads by looking at References headers."
3441   (let ((idhashtb (gnus-make-hashtable 1024))
3442         (thhashtb (gnus-make-hashtable 1024))
3443         (prev threads)
3444         (result threads)
3445         ids references id gthread gid entered ref)
3446     (while threads
3447       (when (setq references (mail-header-references (caar threads)))
3448         (setq id (mail-header-id (caar threads))
3449               ids (inline (gnus-split-references references))
3450               entered nil)
3451         (while (setq ref (pop ids))
3452           (setq ids (delete ref ids))
3453           (if (not (setq gid (gnus-gethash ref idhashtb)))
3454               (progn
3455                 (gnus-sethash ref id idhashtb)
3456                 (gnus-sethash id threads thhashtb))
3457             (setq gthread (gnus-gethash gid thhashtb))
3458             (unless entered
3459               ;; We enter a dummy root into the thread, if we
3460               ;; haven't done that already.
3461               (unless (stringp (caar gthread))
3462                 (setcar gthread (list (mail-header-subject (caar gthread))
3463                                       (car gthread))))
3464               ;; We add this new gathered thread to this gathered
3465               ;; thread.
3466               (setcdr (car gthread)
3467                       (nconc (cdar gthread) (list (car threads)))))
3468             ;; Add it into the thread hash table.
3469             (gnus-sethash id gthread thhashtb)
3470             (setq entered t)
3471             ;; Remove it from the list of threads.
3472             (setcdr prev (cdr threads))
3473             (setq threads prev))))
3474       (setq prev threads)
3475       (setq threads (cdr threads)))
3476     result))
3477
3478 (defun gnus-sort-gathered-threads (threads)
3479   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3480   (let ((result threads))
3481     (while threads
3482       (when (stringp (caar threads))
3483         (setcdr (car threads)
3484                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3485       (setq threads (cdr threads)))
3486     result))
3487
3488 (defun gnus-thread-loop-p (root thread)
3489   "Say whether ROOT is in THREAD."
3490   (let ((stack (list thread))
3491         (infloop 0)
3492         th)
3493     (while (setq thread (pop stack))
3494       (setq th (cdr thread))
3495       (while (and th
3496                   (not (eq (caar th) root)))
3497         (pop th))
3498       (if th
3499           ;; We have found a loop.
3500           (let (ref-dep)
3501             (setcdr thread (delq (car th) (cdr thread)))
3502             (if (boundp (setq ref-dep (intern "none"
3503                                               gnus-newsgroup-dependencies)))
3504                 (setcdr (symbol-value ref-dep)
3505                         (nconc (cdr (symbol-value ref-dep))
3506                                (list (car th))))
3507               (set ref-dep (list nil (car th))))
3508             (setq infloop 1
3509                   stack nil))
3510         ;; Push all the subthreads onto the stack.
3511         (push (cdr thread) stack)))
3512     infloop))
3513
3514 (defun gnus-make-threads ()
3515   "Go through the dependency hashtb and find the roots.  Return all threads."
3516   (let (threads)
3517     (while (catch 'infloop
3518              (mapatoms
3519               (lambda (refs)
3520                 ;; Deal with self-referencing References loops.
3521                 (when (and (car (symbol-value refs))
3522                            (not (zerop
3523                                  (apply
3524                                   '+
3525                                   (mapcar
3526                                    (lambda (thread)
3527                                      (gnus-thread-loop-p
3528                                       (car (symbol-value refs)) thread))
3529                                    (cdr (symbol-value refs)))))))
3530                   (setq threads nil)
3531                   (throw 'infloop t))
3532                 (unless (car (symbol-value refs))
3533              ;; These threads do not refer back to any other articles,
3534                   ;; so they're roots.
3535                   (setq threads (append (cdr (symbol-value refs)) threads))))
3536               gnus-newsgroup-dependencies)))
3537     threads))
3538
3539 ;; Build the thread tree.
3540 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3541   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3542
3543 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3544 if it was already present.
3545
3546 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3547 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3548 Message-IDs will be renamed to a unique Message-ID before being
3549 entered.
3550
3551 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3552   (let* ((id (mail-header-id header))
3553          (id-dep (and id (intern id dependencies)))
3554          ref ref-dep ref-header)
3555     ;; Enter this `header' in the `dependencies' table.
3556     (cond
3557      ((not id-dep)
3558       (setq header nil))
3559      ;; The first two cases do the normal part: enter a new `header'
3560      ;; in the `dependencies' table.
3561      ((not (boundp id-dep))
3562       (set id-dep (list header)))
3563      ((null (car (symbol-value id-dep)))
3564       (setcar (symbol-value id-dep) header))
3565
3566      ;; From here the `header' was already present in the
3567      ;; `dependencies' table.
3568      (force-new
3569       ;; Overrides an existing entry;
3570       ;; just set the header part of the entry.
3571       (setcar (symbol-value id-dep) header))
3572
3573      ;; Renames the existing `header' to a unique Message-ID.
3574      ((not gnus-summary-ignore-duplicates)
3575       ;; An article with this Message-ID has already been seen.
3576       ;; We rename the Message-ID.
3577       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3578            (list header))
3579       (mail-header-set-id header id))
3580
3581      ;; The last case ignores an existing entry, except it adds any
3582      ;; additional Xrefs (in case the two articles came from different
3583      ;; servers.
3584      ;; Also sets `header' to `nil' meaning that the `dependencies'
3585      ;; table was *not* modified.
3586      (t
3587       (mail-header-set-xref
3588        (car (symbol-value id-dep))
3589        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3590                    "")
3591                (or (mail-header-xref header) "")))
3592       (setq header nil)))
3593
3594     (when header
3595       ;; First check that we are not creating a References loop.
3596       (setq ref (gnus-parent-id (mail-header-references header)))
3597       (while (and ref
3598                   (setq ref-dep (intern-soft ref dependencies))
3599                   (boundp ref-dep)
3600                   (setq ref-header (car (symbol-value ref-dep))))
3601         (if (string= id ref)
3602             ;; Yuk!  This is a reference loop.  Make the article be a
3603             ;; root article.
3604             (progn
3605               (mail-header-set-references (car (symbol-value id-dep)) "none")
3606               (setq ref nil))
3607           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3608       (setq ref (gnus-parent-id (mail-header-references header)))
3609       (setq ref-dep (intern (or ref "none") dependencies))
3610       (if (boundp ref-dep)
3611           (setcdr (symbol-value ref-dep)
3612                   (nconc (cdr (symbol-value ref-dep))
3613                          (list (symbol-value id-dep))))
3614         (set ref-dep (list nil (symbol-value id-dep)))))
3615     header))
3616
3617 (defun gnus-extract-message-id-from-in-reply-to (string)
3618   (if (string-match "<[^>]+>" string)
3619       (substring string (match-beginning 0) (match-end 0))
3620     nil))
3621
3622 (defun gnus-build-sparse-threads ()
3623   (let ((headers gnus-newsgroup-headers)
3624         (mail-parse-charset gnus-newsgroup-charset)
3625         (gnus-summary-ignore-duplicates t)
3626         header references generation relations
3627         subject child end new-child date)
3628     ;; First we create an alist of generations/relations, where
3629     ;; generations is how much we trust the relation, and the relation
3630     ;; is parent/child.
3631     (gnus-message 7 "Making sparse threads...")
3632     (save-excursion
3633       (nnheader-set-temp-buffer " *gnus sparse threads*")
3634       (while (setq header (pop headers))
3635         (when (and (setq references (mail-header-references header))
3636                    (not (string= references "")))
3637           (insert references)
3638           (setq child (mail-header-id header)
3639                 subject (mail-header-subject header)
3640                 date (mail-header-date header)
3641                 generation 0)
3642           (while (search-backward ">" nil t)
3643             (setq end (1+ (point)))
3644             (when (search-backward "<" nil t)
3645               (setq new-child (buffer-substring (point) end))
3646               (push (list (incf generation)
3647                           child (setq child new-child)
3648                           subject date)
3649                     relations)))
3650           (when child
3651             (push (list (1+ generation) child nil subject) relations))
3652           (erase-buffer)))
3653       (kill-buffer (current-buffer)))
3654     ;; Sort over trustworthiness.
3655     (mapcar
3656      (lambda (relation)
3657        (when (gnus-dependencies-add-header
3658               (make-full-mail-header
3659                gnus-reffed-article-number
3660                (nth 3 relation) "" (or (nth 4 relation) "")
3661                (nth 1 relation)
3662                (or (nth 2 relation) "") 0 0 "")
3663               gnus-newsgroup-dependencies nil)
3664          (push gnus-reffed-article-number gnus-newsgroup-limit)
3665          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3666          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3667                gnus-newsgroup-reads)
3668          (decf gnus-reffed-article-number)))
3669      (sort relations 'car-less-than-car))
3670     (gnus-message 7 "Making sparse threads...done")))
3671
3672 (defun gnus-build-old-threads ()
3673   ;; Look at all the articles that refer back to old articles, and
3674   ;; fetch the headers for the articles that aren't there.  This will
3675   ;; build complete threads - if the roots haven't been expired by the
3676   ;; server, that is.
3677   (let ((mail-parse-charset gnus-newsgroup-charset)
3678         id heads)
3679     (mapatoms
3680      (lambda (refs)
3681        (when (not (car (symbol-value refs)))
3682          (setq heads (cdr (symbol-value refs)))
3683          (while heads
3684            (if (memq (mail-header-number (caar heads))
3685                      gnus-newsgroup-dormant)
3686                (setq heads (cdr heads))
3687              (setq id (symbol-name refs))
3688              (while (and (setq id (gnus-build-get-header id))
3689                          (not (car (gnus-id-to-thread id)))))
3690              (setq heads nil)))))
3691      gnus-newsgroup-dependencies)))
3692
3693 ;; This function has to be called with point after the article number
3694 ;; on the beginning of the line.
3695 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3696   (let ((eol (gnus-point-at-eol))
3697         (buffer (current-buffer))
3698         header references in-reply-to)
3699
3700     ;; overview: [num subject from date id refs chars lines misc]
3701     (unwind-protect
3702         (progn
3703           (narrow-to-region (point) eol)
3704           (unless (eobp)
3705             (forward-char))
3706
3707           (setq header
3708                 (make-full-mail-header
3709                  number                 ; number
3710                  (funcall gnus-decode-encoded-word-function
3711                           (nnheader-nov-field)) ; subject
3712                  (funcall gnus-decode-encoded-word-function
3713                           (nnheader-nov-field)) ; from
3714                  (nnheader-nov-field)   ; date
3715                  (nnheader-nov-read-message-id) ; id
3716                  (setq references (nnheader-nov-field)) ; refs
3717                  (nnheader-nov-read-integer) ; chars
3718                  (nnheader-nov-read-integer) ; lines
3719                  (unless (eobp)
3720                    (if (looking-at "Xref: ")
3721                        (goto-char (match-end 0)))
3722                    (nnheader-nov-field)) ; Xref
3723                  (nnheader-nov-parse-extra)))) ; extra
3724
3725       (widen))
3726
3727     (when (and (string= references "")
3728                (setq in-reply-to (mail-header-extra header))
3729                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3730       (mail-header-set-references
3731        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3732
3733     (when gnus-alter-header-function
3734       (funcall gnus-alter-header-function header))
3735     (gnus-dependencies-add-header header dependencies force-new)))
3736
3737 (defun gnus-build-get-header (id)
3738   "Look through the buffer of NOV lines and find the header to ID.
3739 Enter this line into the dependencies hash table, and return
3740 the id of the parent article (if any)."
3741   (let ((deps gnus-newsgroup-dependencies)
3742         found header)
3743     (prog1
3744         (save-excursion
3745           (set-buffer nntp-server-buffer)
3746           (let ((case-fold-search nil))
3747             (goto-char (point-min))
3748             (while (and (not found)
3749                         (search-forward id nil t))
3750               (beginning-of-line)
3751               (setq found (looking-at
3752                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3753                                    (regexp-quote id))))
3754               (or found (beginning-of-line 2)))
3755             (when found
3756               (beginning-of-line)
3757               (and
3758                (setq header (gnus-nov-parse-line
3759                              (read (current-buffer)) deps))
3760                (gnus-parent-id (mail-header-references header))))))
3761       (when header
3762         (let ((number (mail-header-number header)))
3763           (push number gnus-newsgroup-limit)
3764           (push header gnus-newsgroup-headers)
3765           (if (memq number gnus-newsgroup-unselected)
3766               (progn
3767                 (setq gnus-newsgroup-unreads
3768                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
3769                                                number))
3770                 (setq gnus-newsgroup-unselected
3771                       (delq number gnus-newsgroup-unselected)))
3772             (push number gnus-newsgroup-ancient)))))))
3773
3774 (defun gnus-build-all-threads ()
3775   "Read all the headers."
3776   (let ((gnus-summary-ignore-duplicates t)
3777         (mail-parse-charset gnus-newsgroup-charset)
3778         (dependencies gnus-newsgroup-dependencies)
3779         header article)
3780     (save-excursion
3781       (set-buffer nntp-server-buffer)
3782       (let ((case-fold-search nil))
3783         (goto-char (point-min))
3784         (while (not (eobp))
3785           (ignore-errors
3786             (setq article (read (current-buffer))
3787                   header (gnus-nov-parse-line article dependencies)))
3788           (when header
3789             (save-excursion
3790               (set-buffer gnus-summary-buffer)
3791               (push header gnus-newsgroup-headers)
3792               (if (memq (setq article (mail-header-number header))
3793                         gnus-newsgroup-unselected)
3794                   (progn
3795                     (setq gnus-newsgroup-unreads
3796                           (gnus-add-to-sorted-list
3797                            gnus-newsgroup-unreads article))
3798                     (setq gnus-newsgroup-unselected
3799                           (delq article gnus-newsgroup-unselected)))
3800                 (push article gnus-newsgroup-ancient)))
3801             (forward-line 1)))))))
3802
3803 (defun gnus-summary-update-article-line (article header)
3804   "Update the line for ARTICLE using HEADERS."
3805   (let* ((id (mail-header-id header))
3806          (thread (gnus-id-to-thread id)))
3807     (unless thread
3808       (error "Article in no thread"))
3809     ;; Update the thread.
3810     (setcar thread header)
3811     (gnus-summary-goto-subject article)
3812     (let* ((datal (gnus-data-find-list article))
3813            (data (car datal))
3814            (length (when (cdr datal)
3815                      (- (gnus-data-pos data)
3816                         (gnus-data-pos (cadr datal)))))
3817            (buffer-read-only nil)
3818            (level (gnus-summary-thread-level)))
3819       (gnus-delete-line)
3820       (gnus-summary-insert-line
3821        header level nil (gnus-article-mark article)
3822        (memq article gnus-newsgroup-replied)
3823        (memq article gnus-newsgroup-expirable)
3824        ;; Only insert the Subject string when it's different
3825        ;; from the previous Subject string.
3826        (if (and
3827             gnus-show-threads
3828             (gnus-subject-equal
3829              (condition-case ()
3830                  (mail-header-subject
3831                   (gnus-data-header
3832                    (cadr
3833                     (gnus-data-find-list
3834                      article
3835                      (gnus-data-list t)))))
3836                ;; Error on the side of excessive subjects.
3837                (error ""))
3838              (mail-header-subject header)))
3839            ""
3840          (mail-header-subject header))
3841        nil (cdr (assq article gnus-newsgroup-scored))
3842        (memq article gnus-newsgroup-processable))
3843       (when length
3844         (gnus-data-update-list
3845          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3846
3847 (defun gnus-summary-update-article (article &optional iheader)
3848   "Update ARTICLE in the summary buffer."
3849   (set-buffer gnus-summary-buffer)
3850   (let* ((header (gnus-summary-article-header article))
3851          (id (mail-header-id header))
3852          (data (gnus-data-find article))
3853          (thread (gnus-id-to-thread id))
3854          (references (mail-header-references header))
3855          (parent
3856           (gnus-id-to-thread
3857            (or (gnus-parent-id
3858                 (when (and references
3859                            (not (equal "" references)))
3860                   references))
3861                "none")))
3862          (buffer-read-only nil)
3863          (old (car thread)))
3864     (when thread
3865       (unless iheader
3866         (setcar thread nil)
3867         (when parent
3868           (delq thread parent)))
3869       (if (gnus-summary-insert-subject id header)
3870        ;; Set the (possibly) new article number in the data structure.
3871           (gnus-data-set-number data (gnus-id-to-article id))
3872         (setcar thread old)
3873         nil))))
3874
3875 (defun gnus-rebuild-thread (id &optional line)
3876   "Rebuild the thread containing ID.
3877 If LINE, insert the rebuilt thread starting on line LINE."
3878   (let ((buffer-read-only nil)
3879         old-pos current thread data)
3880     (if (not gnus-show-threads)
3881         (setq thread (list (car (gnus-id-to-thread id))))
3882       ;; Get the thread this article is part of.
3883       (setq thread (gnus-remove-thread id)))
3884     (setq old-pos (gnus-point-at-bol))
3885     (setq current (save-excursion
3886                     (and (re-search-backward "[\r\n]" nil t)
3887                          (gnus-summary-article-number))))
3888     ;; If this is a gathered thread, we have to go some re-gathering.
3889     (when (stringp (car thread))
3890       (let ((subject (car thread))
3891             roots thr)
3892         (setq thread (cdr thread))
3893         (while thread
3894           (unless (memq (setq thr (gnus-id-to-thread
3895                                    (gnus-root-id
3896                                     (mail-header-id (caar thread)))))
3897                         roots)
3898             (push thr roots))
3899           (setq thread (cdr thread)))
3900         ;; We now have all (unique) roots.
3901         (if (= (length roots) 1)
3902             ;; All the loose roots are now one solid root.
3903             (setq thread (car roots))
3904           (setq thread (cons subject (gnus-sort-threads roots))))))
3905     (let (threads)
3906       ;; We then insert this thread into the summary buffer.
3907       (when line
3908         (goto-char (point-min))
3909         (forward-line (1- line)))
3910       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3911         (if gnus-show-threads
3912             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3913           (gnus-summary-prepare-unthreaded thread))
3914         (setq data (nreverse gnus-newsgroup-data))
3915         (setq threads gnus-newsgroup-threads))
3916       ;; We splice the new data into the data structure.
3917       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3918       ;;!!! then we want to insert at the beginning of the buffer.
3919       ;;!!! That happens to be true with Gnus now, but that may
3920       ;;!!! change in the future.  Perhaps.
3921       (gnus-data-enter-list
3922        (if line nil current) data (- (point) old-pos))
3923       (setq gnus-newsgroup-threads
3924             (nconc threads gnus-newsgroup-threads))
3925       (gnus-data-compute-positions))))
3926
3927 (defun gnus-number-to-header (number)
3928   "Return the header for article NUMBER."
3929   (let ((headers gnus-newsgroup-headers))
3930     (while (and headers
3931                 (not (= number (mail-header-number (car headers)))))
3932       (pop headers))
3933     (when headers
3934       (car headers))))
3935
3936 (defun gnus-parent-headers (in-headers &optional generation)
3937   "Return the headers of the GENERATIONeth parent of HEADERS."
3938   (unless generation
3939     (setq generation 1))
3940   (let ((parent t)
3941         (headers in-headers)
3942         references)
3943     (while (and parent
3944                 (not (zerop generation))
3945                 (setq references (mail-header-references headers)))
3946       (setq headers (if (and references
3947                              (setq parent (gnus-parent-id references)))
3948                         (car (gnus-id-to-thread parent))
3949                       nil))
3950       (decf generation))
3951     (and (not (eq headers in-headers))
3952          headers)))
3953
3954 (defun gnus-id-to-thread (id)
3955   "Return the (sub-)thread where ID appears."
3956   (gnus-gethash id gnus-newsgroup-dependencies))
3957
3958 (defun gnus-id-to-article (id)
3959   "Return the article number of ID."
3960   (let ((thread (gnus-id-to-thread id)))
3961     (when (and thread
3962                (car thread))
3963       (mail-header-number (car thread)))))
3964
3965 (defun gnus-id-to-header (id)
3966   "Return the article headers of ID."
3967   (car (gnus-id-to-thread id)))
3968
3969 (defun gnus-article-displayed-root-p (article)
3970   "Say whether ARTICLE is a root(ish) article."
3971   (let ((level (gnus-summary-thread-level article))
3972         (refs (mail-header-references  (gnus-summary-article-header article)))
3973         particle)
3974     (cond
3975      ((null level) nil)
3976      ((zerop level) t)
3977      ((null refs) t)
3978      ((null (gnus-parent-id refs)) t)
3979      ((and (= 1 level)
3980            (null (setq particle (gnus-id-to-article
3981                                  (gnus-parent-id refs))))
3982            (null (gnus-summary-thread-level particle)))))))
3983
3984 (defun gnus-root-id (id)
3985   "Return the id of the root of the thread where ID appears."
3986   (let (last-id prev)
3987     (while (and id (setq prev (car (gnus-id-to-thread id))))
3988       (setq last-id id
3989             id (gnus-parent-id (mail-header-references prev))))
3990     last-id))
3991
3992 (defun gnus-articles-in-thread (thread)
3993   "Return the list of articles in THREAD."
3994   (cons (mail-header-number (car thread))
3995         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3996
3997 (defun gnus-remove-thread (id &optional dont-remove)
3998   "Remove the thread that has ID in it."
3999   (let (headers thread last-id)
4000     ;; First go up in this thread until we find the root.
4001     (setq last-id (gnus-root-id id)
4002           headers (message-flatten-list (gnus-id-to-thread last-id)))
4003     ;; We have now found the real root of this thread.  It might have
4004     ;; been gathered into some loose thread, so we have to search
4005     ;; through the threads to find the thread we wanted.
4006     (let ((threads gnus-newsgroup-threads)
4007           sub)
4008       (while threads
4009         (setq sub (car threads))
4010         (if (stringp (car sub))
4011             ;; This is a gathered thread, so we look at the roots
4012             ;; below it to find whether this article is in this
4013             ;; gathered root.
4014             (progn
4015               (setq sub (cdr sub))
4016               (while sub
4017                 (when (member (caar sub) headers)
4018                   (setq thread (car threads)
4019                         threads nil
4020                         sub nil))
4021                 (setq sub (cdr sub))))
4022           ;; It's an ordinary thread, so we check it.
4023           (when (eq (car sub) (car headers))
4024             (setq thread sub
4025                   threads nil)))
4026         (setq threads (cdr threads)))
4027       ;; If this article is in no thread, then it's a root.
4028       (if thread
4029           (unless dont-remove
4030             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4031         (setq thread (gnus-id-to-thread last-id)))
4032       (when thread
4033         (prog1
4034             thread                      ; We return this thread.
4035           (unless dont-remove
4036             (if (stringp (car thread))
4037                 (progn
4038                   ;; If we use dummy roots, then we have to remove the
4039                   ;; dummy root as well.
4040                   (when (eq gnus-summary-make-false-root 'dummy)
4041                     ;; We go to the dummy root by going to
4042                     ;; the first sub-"thread", and then one line up.
4043                     (gnus-summary-goto-article
4044                      (mail-header-number (caadr thread)))
4045                     (forward-line -1)
4046                     (gnus-delete-line)
4047                     (gnus-data-compute-positions))
4048                   (setq thread (cdr thread))
4049                   (while thread
4050                     (gnus-remove-thread-1 (car thread))
4051                     (setq thread (cdr thread))))
4052               (gnus-remove-thread-1 thread))))))))
4053
4054 (defun gnus-remove-thread-1 (thread)
4055   "Remove the thread THREAD recursively."
4056   (let ((number (mail-header-number (pop thread)))
4057         d)
4058     (setq thread (reverse thread))
4059     (while thread
4060       (gnus-remove-thread-1 (pop thread)))
4061     (when (setq d (gnus-data-find number))
4062       (goto-char (gnus-data-pos d))
4063       (gnus-summary-show-thread)
4064       (gnus-data-remove
4065        number
4066        (- (gnus-point-at-bol)
4067           (prog1
4068               (1+ (gnus-point-at-eol))
4069             (gnus-delete-line)))))))
4070
4071 (defun gnus-sort-threads-1 (threads func)
4072   (sort (mapcar (lambda (thread)
4073                   (cons (car thread)
4074                         (and (cdr thread)
4075                              (gnus-sort-threads-1 (cdr thread) func))))
4076                 threads) func))
4077
4078 (defun gnus-sort-threads (threads)
4079   "Sort THREADS."
4080   (if (not gnus-thread-sort-functions)
4081       threads
4082     (gnus-message 8 "Sorting threads...")
4083     (prog1
4084         (gnus-sort-threads-1
4085          threads
4086          (gnus-make-sort-function gnus-thread-sort-functions))
4087       (gnus-message 8 "Sorting threads...done"))))
4088
4089 (defun gnus-sort-articles (articles)
4090   "Sort ARTICLES."
4091   (when gnus-article-sort-functions
4092     (gnus-message 7 "Sorting articles...")
4093     (prog1
4094         (setq gnus-newsgroup-headers
4095               (sort articles (gnus-make-sort-function
4096                               gnus-article-sort-functions)))
4097       (gnus-message 7 "Sorting articles...done"))))
4098
4099 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4100 (defmacro gnus-thread-header (thread)
4101   "Return header of first article in THREAD.
4102 Note that THREAD must never, ever be anything else than a variable -
4103 using some other form will lead to serious barfage."
4104   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4105   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4106   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4107         (vector thread) 2))
4108
4109 (defsubst gnus-article-sort-by-number (h1 h2)
4110   "Sort articles by article number."
4111   (< (mail-header-number h1)
4112      (mail-header-number h2)))
4113
4114 (defun gnus-thread-sort-by-number (h1 h2)
4115   "Sort threads by root article number."
4116   (gnus-article-sort-by-number
4117    (gnus-thread-header h1) (gnus-thread-header h2)))
4118
4119 (defsubst gnus-article-sort-by-lines (h1 h2)
4120   "Sort articles by article Lines header."
4121   (< (mail-header-lines h1)
4122      (mail-header-lines h2)))
4123
4124 (defun gnus-thread-sort-by-lines (h1 h2)
4125   "Sort threads by root article Lines header."
4126   (gnus-article-sort-by-lines
4127    (gnus-thread-header h1) (gnus-thread-header h2)))
4128
4129 (defsubst gnus-article-sort-by-chars (h1 h2)
4130   "Sort articles by octet length."
4131   (< (mail-header-chars h1)
4132      (mail-header-chars h2)))
4133
4134 (defun gnus-thread-sort-by-chars (h1 h2)
4135   "Sort threads by root article octet length."
4136   (gnus-article-sort-by-chars
4137    (gnus-thread-header h1) (gnus-thread-header h2)))
4138
4139 (defsubst gnus-article-sort-by-author (h1 h2)
4140   "Sort articles by root author."
4141   (string-lessp
4142    (let ((extract (funcall
4143                    gnus-extract-address-components
4144                    (mail-header-from h1))))
4145      (or (car extract) (cadr extract) ""))
4146    (let ((extract (funcall
4147                    gnus-extract-address-components
4148                    (mail-header-from h2))))
4149      (or (car extract) (cadr extract) ""))))
4150
4151 (defun gnus-thread-sort-by-author (h1 h2)
4152   "Sort threads by root author."
4153   (gnus-article-sort-by-author
4154    (gnus-thread-header h1)  (gnus-thread-header h2)))
4155
4156 (defsubst gnus-article-sort-by-subject (h1 h2)
4157   "Sort articles by root subject."
4158   (string-lessp
4159    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4160    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4161
4162 (defun gnus-thread-sort-by-subject (h1 h2)
4163   "Sort threads by root subject."
4164   (gnus-article-sort-by-subject
4165    (gnus-thread-header h1) (gnus-thread-header h2)))
4166
4167 (defsubst gnus-article-sort-by-date (h1 h2)
4168   "Sort articles by root article date."
4169   (time-less-p
4170    (gnus-date-get-time (mail-header-date h1))
4171    (gnus-date-get-time (mail-header-date h2))))
4172
4173 (defun gnus-thread-sort-by-date (h1 h2)
4174   "Sort threads by root article date."
4175   (gnus-article-sort-by-date
4176    (gnus-thread-header h1) (gnus-thread-header h2)))
4177
4178 (defsubst gnus-article-sort-by-score (h1 h2)
4179   "Sort articles by root article score.
4180 Unscored articles will be counted as having a score of zero."
4181   (> (or (cdr (assq (mail-header-number h1)
4182                     gnus-newsgroup-scored))
4183          gnus-summary-default-score 0)
4184      (or (cdr (assq (mail-header-number h2)
4185                     gnus-newsgroup-scored))
4186          gnus-summary-default-score 0)))
4187
4188 (defun gnus-thread-sort-by-score (h1 h2)
4189   "Sort threads by root article score."
4190   (gnus-article-sort-by-score
4191    (gnus-thread-header h1) (gnus-thread-header h2)))
4192
4193 (defun gnus-thread-sort-by-total-score (h1 h2)
4194   "Sort threads by the sum of all scores in the thread.
4195 Unscored articles will be counted as having a score of zero."
4196   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4197
4198 (defun gnus-thread-total-score (thread)
4199   ;; This function find the total score of THREAD.
4200   (cond
4201    ((null thread)
4202     0)
4203    ((consp thread)
4204     (if (stringp (car thread))
4205         (apply gnus-thread-score-function 0
4206                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4207       (gnus-thread-total-score-1 thread)))
4208    (t
4209     (gnus-thread-total-score-1 (list thread)))))
4210
4211 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4212   "Sort threads such that the thread with the most recently arrived article comes first."
4213   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4214
4215 (defun gnus-thread-highest-number (thread)
4216   "Return the highest article number in THREAD."
4217   (apply 'max (mapcar (lambda (header)
4218                         (mail-header-number header))
4219                       (message-flatten-list thread))))
4220
4221 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4222   "Sort threads such that the thread with the most recently dated article comes first."
4223   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4224
4225 (defun gnus-thread-latest-date (thread)
4226   "Return the highest article date in THREAD."
4227   (let ((previous-time 0))
4228     (apply 'max (mapcar
4229                  (lambda (header)
4230                    (setq previous-time
4231                          (time-to-seconds
4232                           (mail-header-parse-date
4233                            (condition-case ()
4234                                (mail-header-date header)
4235                              (error previous-time))))))
4236                  (sort
4237                   (message-flatten-list thread)
4238                   (lambda (h1 h2)
4239                     (< (mail-header-number h1)
4240                        (mail-header-number h2))))))))
4241
4242 (defun gnus-thread-total-score-1 (root)
4243   ;; This function find the total score of the thread below ROOT.
4244   (setq root (car root))
4245   (apply gnus-thread-score-function
4246          (or (append
4247               (mapcar 'gnus-thread-total-score
4248                       (cdr (gnus-id-to-thread (mail-header-id root))))
4249               (when (> (mail-header-number root) 0)
4250                 (list (or (cdr (assq (mail-header-number root)
4251                                      gnus-newsgroup-scored))
4252                           gnus-summary-default-score 0))))
4253              (list gnus-summary-default-score)
4254              '(0))))
4255
4256 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4257 (defvar gnus-tmp-prev-subject nil)
4258 (defvar gnus-tmp-false-parent nil)
4259 (defvar gnus-tmp-root-expunged nil)
4260 (defvar gnus-tmp-dummy-line nil)
4261
4262 (eval-when-compile (defvar gnus-tmp-header))
4263 (defun gnus-extra-header (type &optional header)
4264   "Return the extra header of TYPE."
4265   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4266       ""))
4267
4268 (defvar gnus-tmp-thread-tree-header-string "")
4269
4270 (defvar gnus-sum-thread-tree-root "> "
4271   "With %B spec, used for the root of a thread.
4272 If nil, use subject instead.")
4273 (defvar gnus-sum-thread-tree-single-indent ""
4274   "With %B spec, used for a thread with just one message.
4275 If nil, use subject instead.")
4276 (defvar gnus-sum-thread-tree-vertical "| "
4277   "With %B spec, used for drawing a vertical line.")
4278 (defvar gnus-sum-thread-tree-indent "  "
4279   "With %B spec, used for indenting.")
4280 (defvar gnus-sum-thread-tree-leaf-with-other "+-> "
4281   "With %B spec, used for a leaf with brothers.")
4282 (defvar gnus-sum-thread-tree-single-leaf "\\-> "
4283   "With %B spec, used for a leaf without brothers.")
4284
4285 (defun gnus-summary-prepare-threads (threads)
4286   "Prepare summary buffer from THREADS and indentation LEVEL.
4287 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4288 or a straight list of headers."
4289   (gnus-message 7 "Generating summary...")
4290
4291   (setq gnus-newsgroup-threads threads)
4292   (beginning-of-line)
4293
4294   (let ((gnus-tmp-level 0)
4295         (default-score (or gnus-summary-default-score 0))
4296         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4297         thread number subject stack state gnus-tmp-gathered beg-match
4298         new-roots gnus-tmp-new-adopts thread-end
4299         gnus-tmp-header gnus-tmp-unread
4300         gnus-tmp-replied gnus-tmp-subject-or-nil
4301         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4302         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4303         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4304         tree-stack)
4305
4306     (setq gnus-tmp-prev-subject nil)
4307
4308     (if (vectorp (car threads))
4309         ;; If this is a straight (sic) list of headers, then a
4310         ;; threaded summary display isn't required, so we just create
4311         ;; an unthreaded one.
4312         (gnus-summary-prepare-unthreaded threads)
4313
4314       ;; Do the threaded display.
4315
4316       (while (or threads stack gnus-tmp-new-adopts new-roots)
4317
4318         (if (and (= gnus-tmp-level 0)
4319                  (or (not stack)
4320                      (= (caar stack) 0))
4321                  (not gnus-tmp-false-parent)
4322                  (or gnus-tmp-new-adopts new-roots))
4323             (if gnus-tmp-new-adopts
4324                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4325                       thread (list (car gnus-tmp-new-adopts))
4326                       gnus-tmp-header (caar thread)
4327                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4328               (when new-roots
4329                 (setq thread (list (car new-roots))
4330                       gnus-tmp-header (caar thread)
4331                       new-roots (cdr new-roots))))
4332
4333           (if threads
4334               ;; If there are some threads, we do them before the
4335               ;; threads on the stack.
4336               (setq thread threads
4337                     gnus-tmp-header (caar thread))
4338             ;; There were no current threads, so we pop something off
4339             ;; the stack.
4340             (setq state (car stack)
4341                   gnus-tmp-level (car state)
4342                   tree-stack (cadr state)
4343                   thread (caddr state)
4344                   stack (cdr stack)
4345                   gnus-tmp-header (caar thread))))
4346
4347         (setq gnus-tmp-false-parent nil)
4348         (setq gnus-tmp-root-expunged nil)
4349         (setq thread-end nil)
4350
4351         (if (stringp gnus-tmp-header)
4352             ;; The header is a dummy root.
4353             (cond
4354              ((eq gnus-summary-make-false-root 'adopt)
4355               ;; We let the first article adopt the rest.
4356               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4357                                                (cddar thread)))
4358               (setq gnus-tmp-gathered
4359                     (nconc (mapcar
4360                             (lambda (h) (mail-header-number (car h)))
4361                             (cddar thread))
4362                            gnus-tmp-gathered))
4363               (setq thread (cons (list (caar thread)
4364                                        (cadar thread))
4365                                  (cdr thread)))
4366               (setq gnus-tmp-level -1
4367                     gnus-tmp-false-parent t))
4368              ((eq gnus-summary-make-false-root 'empty)
4369               ;; We print adopted articles with empty subject fields.
4370               (setq gnus-tmp-gathered
4371                     (nconc (mapcar
4372                             (lambda (h) (mail-header-number (car h)))
4373                             (cddar thread))
4374                            gnus-tmp-gathered))
4375               (setq gnus-tmp-level -1))
4376              ((eq gnus-summary-make-false-root 'dummy)
4377               ;; We remember that we probably want to output a dummy
4378               ;; root.
4379               (setq gnus-tmp-dummy-line gnus-tmp-header)
4380               (setq gnus-tmp-prev-subject gnus-tmp-header))
4381              (t
4382               ;; We do not make a root for the gathered
4383               ;; sub-threads at all.
4384               (setq gnus-tmp-level -1)))
4385
4386           (setq number (mail-header-number gnus-tmp-header)
4387                 subject (mail-header-subject gnus-tmp-header))
4388
4389           (cond
4390            ;; If the thread has changed subject, we might want to make
4391            ;; this subthread into a root.
4392            ((and (null gnus-thread-ignore-subject)
4393                  (not (zerop gnus-tmp-level))
4394                  gnus-tmp-prev-subject
4395                  (not (inline
4396                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4397             (setq new-roots (nconc new-roots (list (car thread)))
4398                   thread-end t
4399                   gnus-tmp-header nil))
4400            ;; If the article lies outside the current limit,
4401            ;; then we do not display it.
4402            ((not (memq number gnus-newsgroup-limit))
4403             (setq gnus-tmp-gathered
4404                   (nconc (mapcar
4405                           (lambda (h) (mail-header-number (car h)))
4406                           (cdar thread))
4407                          gnus-tmp-gathered))
4408             (setq gnus-tmp-new-adopts (if (cdar thread)
4409                                           (append gnus-tmp-new-adopts
4410                                                   (cdar thread))
4411                                         gnus-tmp-new-adopts)
4412                   thread-end t
4413                   gnus-tmp-header nil)
4414             (when (zerop gnus-tmp-level)
4415               (setq gnus-tmp-root-expunged t)))
4416            ;; Perhaps this article is to be marked as read?
4417            ((and gnus-summary-mark-below
4418                  (< (or (cdr (assq number gnus-newsgroup-scored))
4419                         default-score)
4420                     gnus-summary-mark-below)
4421                  ;; Don't touch sparse articles.
4422                  (not (gnus-summary-article-sparse-p number))
4423                  (not (gnus-summary-article-ancient-p number)))
4424             (setq gnus-newsgroup-unreads
4425                   (delq number gnus-newsgroup-unreads))
4426             (if gnus-newsgroup-auto-expire
4427                 (setq gnus-newsgroup-expirable
4428                       (gnus-add-to-sorted-list 
4429                        gnus-newsgroup-expirable number))
4430               (push (cons number gnus-low-score-mark)
4431                     gnus-newsgroup-reads))))
4432
4433           (when gnus-tmp-header
4434             ;; We may have an old dummy line to output before this
4435             ;; article.
4436             (when (and gnus-tmp-dummy-line
4437                        (gnus-subject-equal
4438                         gnus-tmp-dummy-line
4439                         (mail-header-subject gnus-tmp-header)))
4440               (gnus-summary-insert-dummy-line
4441                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4442               (setq gnus-tmp-dummy-line nil))
4443
4444             ;; Compute the mark.
4445             (setq gnus-tmp-unread (gnus-article-mark number))
4446
4447             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4448                                   gnus-tmp-header gnus-tmp-level)
4449                   gnus-newsgroup-data)
4450
4451             ;; Actually insert the line.
4452             (setq
4453              gnus-tmp-subject-or-nil
4454              (cond
4455               ((and gnus-thread-ignore-subject
4456                     gnus-tmp-prev-subject
4457                     (not (inline (gnus-subject-equal
4458                                   gnus-tmp-prev-subject subject))))
4459                subject)
4460               ((zerop gnus-tmp-level)
4461                (if (and (eq gnus-summary-make-false-root 'empty)
4462                         (memq number gnus-tmp-gathered)
4463                         gnus-tmp-prev-subject
4464                         (inline (gnus-subject-equal
4465                                  gnus-tmp-prev-subject subject)))
4466                    gnus-summary-same-subject
4467                  subject))
4468               (t gnus-summary-same-subject)))
4469             (if (and (eq gnus-summary-make-false-root 'adopt)
4470                      (= gnus-tmp-level 1)
4471                      (memq number gnus-tmp-gathered))
4472                 (setq gnus-tmp-opening-bracket ?\<
4473                       gnus-tmp-closing-bracket ?\>)
4474               (setq gnus-tmp-opening-bracket ?\[
4475                     gnus-tmp-closing-bracket ?\]))
4476             (setq
4477              gnus-tmp-indentation
4478              (aref gnus-thread-indent-array gnus-tmp-level)
4479              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4480              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4481                                 gnus-summary-default-score 0)
4482              gnus-tmp-score-char
4483              (if (or (null gnus-summary-default-score)
4484                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4485                          gnus-summary-zcore-fuzz))
4486                  ?                      ;Whitespace
4487                (if (< gnus-tmp-score gnus-summary-default-score)
4488                    gnus-score-below-mark gnus-score-over-mark))
4489              gnus-tmp-replied
4490              (cond ((memq number gnus-newsgroup-processable)
4491                     gnus-process-mark)
4492                    ((memq number gnus-newsgroup-cached)
4493                     gnus-cached-mark)
4494                    ((memq number gnus-newsgroup-replied)
4495                     gnus-replied-mark)
4496                    ((memq number gnus-newsgroup-forwarded)
4497                     gnus-forwarded-mark)
4498                    ((memq number gnus-newsgroup-saved)
4499                     gnus-saved-mark)
4500                    ((memq number gnus-newsgroup-recent)
4501                     gnus-recent-mark)
4502                    ((memq number gnus-newsgroup-unseen)
4503                     gnus-unseen-mark)
4504                    (t gnus-no-mark))
4505              gnus-tmp-from (mail-header-from gnus-tmp-header)
4506              gnus-tmp-name
4507              (cond
4508               ((string-match "<[^>]+> *$" gnus-tmp-from)
4509                (setq beg-match (match-beginning 0))
4510                (or (and (string-match "^\".+\"" gnus-tmp-from)
4511                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4512                    (substring gnus-tmp-from 0 beg-match)))
4513               ((string-match "(.+)" gnus-tmp-from)
4514                (substring gnus-tmp-from
4515                           (1+ (match-beginning 0)) (1- (match-end 0))))
4516               (t gnus-tmp-from))
4517              gnus-tmp-thread-tree-header-string
4518              (cond
4519               ((not gnus-show-threads) "")
4520               ((zerop gnus-tmp-level)
4521                (if (cdar thread)
4522                    (or gnus-sum-thread-tree-root subject)
4523                  (or gnus-sum-thread-tree-single-indent subject)))
4524               (t
4525                (concat (apply 'concat
4526                               (mapcar (lambda (item)
4527                                         (if (= item 1)
4528                                             gnus-sum-thread-tree-vertical
4529                                           gnus-sum-thread-tree-indent))
4530                                       (cdr (reverse tree-stack))))
4531                        (if (nth 1 thread)
4532                            gnus-sum-thread-tree-leaf-with-other
4533                          gnus-sum-thread-tree-single-leaf)))))
4534             (when (string= gnus-tmp-name "")
4535               (setq gnus-tmp-name gnus-tmp-from))
4536             (unless (numberp gnus-tmp-lines)
4537               (setq gnus-tmp-lines -1))
4538             (if (= gnus-tmp-lines -1)
4539                 (setq gnus-tmp-lines "?")
4540               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4541             (gnus-put-text-property
4542              (point)
4543              (progn (eval gnus-summary-line-format-spec) (point))
4544              'gnus-number number)
4545             (when gnus-visual-p
4546               (forward-line -1)
4547               (gnus-run-hooks 'gnus-summary-update-hook)
4548               (forward-line 1))
4549
4550             (setq gnus-tmp-prev-subject subject)))
4551
4552         (when (nth 1 thread)
4553           (push (list (max 0 gnus-tmp-level)
4554                       (copy-list tree-stack)
4555                       (nthcdr 1 thread))
4556                 stack))
4557         (push (if (nth 1 thread) 1 0) tree-stack)
4558         (incf gnus-tmp-level)
4559         (setq threads (if thread-end nil (cdar thread)))
4560         (unless threads
4561           (setq gnus-tmp-level 0)))))
4562   (gnus-message 7 "Generating summary...done"))
4563
4564 (defun gnus-summary-prepare-unthreaded (headers)
4565   "Generate an unthreaded summary buffer based on HEADERS."
4566   (let (header number mark)
4567
4568     (beginning-of-line)
4569
4570     (while headers
4571       ;; We may have to root out some bad articles...
4572       (when (memq (setq number (mail-header-number
4573                                 (setq header (pop headers))))
4574                   gnus-newsgroup-limit)
4575         ;; Mark article as read when it has a low score.
4576         (when (and gnus-summary-mark-below
4577                    (< (or (cdr (assq number gnus-newsgroup-scored))
4578                           gnus-summary-default-score 0)
4579                       gnus-summary-mark-below)
4580                    (not (gnus-summary-article-ancient-p number)))
4581           (setq gnus-newsgroup-unreads
4582                 (delq number gnus-newsgroup-unreads))
4583           (if gnus-newsgroup-auto-expire
4584               (push number gnus-newsgroup-expirable)
4585             (push (cons number gnus-low-score-mark)
4586                   gnus-newsgroup-reads)))
4587
4588         (setq mark (gnus-article-mark number))
4589         (push (gnus-data-make number mark (1+ (point)) header 0)
4590               gnus-newsgroup-data)
4591         (gnus-summary-insert-line
4592          header 0 number
4593          mark (memq number gnus-newsgroup-replied)
4594          (memq number gnus-newsgroup-expirable)
4595          (mail-header-subject header) nil
4596          (cdr (assq number gnus-newsgroup-scored))
4597          (memq number gnus-newsgroup-processable))))))
4598
4599 (defun gnus-summary-remove-list-identifiers ()
4600   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4601   (let ((regexp (if (consp gnus-list-identifiers)
4602                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4603                   gnus-list-identifiers))
4604         changed subject)
4605     (when regexp
4606       (dolist (header gnus-newsgroup-headers)
4607         (setq subject (mail-header-subject header)
4608               changed nil)
4609         (while (string-match
4610                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4611                 subject)
4612           (setq subject
4613                 (concat (substring subject 0 (match-beginning 2))
4614                         (substring subject (match-end 0)))
4615                 changed t))
4616         (when (and changed
4617                    (string-match
4618                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4619           (setq subject
4620                 (concat (substring subject 0 (match-beginning 1))
4621                         (substring subject (match-end 1)))))
4622         (when changed
4623           (mail-header-set-subject header subject))))))
4624
4625 (defun gnus-fetch-headers (articles)
4626   "Fetch headers of ARTICLES."
4627   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4628     (gnus-message 5 "Fetching headers for %s..." name)
4629     (prog1
4630         (if (eq 'nov
4631                 (setq gnus-headers-retrieved-by
4632                       (gnus-retrieve-headers
4633                        articles gnus-newsgroup-name
4634                        ;; We might want to fetch old headers, but
4635                        ;; not if there is only 1 article.
4636                        (and (or (and
4637                                  (not (eq gnus-fetch-old-headers 'some))
4638                                  (not (numberp gnus-fetch-old-headers)))
4639                                 (> (length articles) 1))
4640                             gnus-fetch-old-headers))))
4641             (gnus-get-newsgroup-headers-xover
4642              articles nil nil gnus-newsgroup-name t)
4643           (gnus-get-newsgroup-headers))
4644       (gnus-message 5 "Fetching headers for %s...done" name))))
4645
4646 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4647   "Select newsgroup GROUP.
4648 If READ-ALL is non-nil, all articles in the group are selected.
4649 If SELECT-ARTICLES, only select those articles from GROUP."
4650   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4651          ;;!!! Dirty hack; should be removed.
4652          (gnus-summary-ignore-duplicates
4653           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4654               t
4655             gnus-summary-ignore-duplicates))
4656          (info (nth 2 entry))
4657          articles fetched-articles cached)
4658
4659     (unless (gnus-check-server
4660              (set (make-local-variable 'gnus-current-select-method)
4661                   (gnus-find-method-for-group group)))
4662       (error "Couldn't open server"))
4663
4664     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4665         (gnus-activate-group group)     ; Or we can activate it...
4666         (progn                          ; Or we bug out.
4667           (when (equal major-mode 'gnus-summary-mode)
4668             (kill-buffer (current-buffer)))
4669           (error "Couldn't activate group %s: %s"
4670                  group (gnus-status-message group))))
4671
4672     (unless (gnus-request-group group t)
4673       (when (equal major-mode 'gnus-summary-mode)
4674         (kill-buffer (current-buffer)))
4675       (error "Couldn't request group %s: %s"
4676              group (gnus-status-message group)))
4677
4678     (setq gnus-newsgroup-name group
4679           gnus-newsgroup-unselected nil
4680           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4681
4682     (let ((display (gnus-group-find-parameter group 'display)))
4683       (setq gnus-newsgroup-display
4684             (cond
4685              ((not (zerop (or (car-safe read-all) 0)))
4686               ;; The user entered the group with C-u SPC/RET, let's show
4687               ;; all articles.
4688               'gnus-not-ignore)
4689              ((eq display 'all)
4690               'gnus-not-ignore)
4691              ((arrayp display)
4692               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4693              ((numberp display)
4694               ;; The following is probably the "correct" solution, but
4695               ;; it makes Gnus fetch all headers and then limit the
4696               ;; articles (which is slow), so instead we hack the
4697               ;; select-articles parameter instead. -- Simon Josefsson
4698               ;; <jas@kth.se>
4699               ;;
4700               ;; (gnus-byte-compile
4701               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4702               ;;                         display)))))
4703               (setq select-articles
4704                     (gnus-uncompress-range
4705                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4706                              (if (> tmp 0)
4707                                  tmp
4708                                1))
4709                            (cdr (gnus-active group)))))
4710               nil)
4711              (t
4712               nil))))
4713
4714     (gnus-summary-setup-default-charset)
4715
4716     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4717     (when (gnus-virtual-group-p group)
4718       (setq cached gnus-newsgroup-cached))
4719
4720     (setq gnus-newsgroup-unreads
4721           (gnus-sorted-ndifference
4722            (gnus-sorted-ndifference gnus-newsgroup-unreads
4723                                     gnus-newsgroup-marked)
4724            gnus-newsgroup-dormant))
4725
4726     (setq gnus-newsgroup-processable nil)
4727
4728     (gnus-update-read-articles group gnus-newsgroup-unreads)
4729
4730     ;; Adjust and set lists of article marks.
4731     (when info
4732       (gnus-adjust-marked-articles info))
4733
4734     (if (setq articles select-articles)
4735         (setq gnus-newsgroup-unselected
4736               (gnus-sorted-difference 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-sorted-nintersection
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-difference articles fetched-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 '((unread . unread)
4817                             (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                   ;; Fetch all if the predicate is non-nil.
4876                   gnus-newsgroup-display)
4877               ;; We want to select the headers for all the articles in
4878               ;; the group, so we select either all the active
4879               ;; articles in the group, or (if that's nil), the
4880               ;; articles in the cache.
4881               (or
4882                (gnus-uncompress-range (gnus-active group))
4883                (gnus-cache-articles-in-group group))
4884             ;; Select only the "normal" subset of articles.
4885             (gnus-sorted-nunion  
4886              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
4887              gnus-newsgroup-unreads)))
4888          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4889          (scored (length scored-list))
4890          (number (length articles))
4891          (marked (+ (length gnus-newsgroup-marked)
4892                     (length gnus-newsgroup-dormant)))
4893          (select
4894           (cond
4895            ((numberp read-all)
4896             read-all)
4897            ((numberp gnus-newsgroup-display)
4898             gnus-newsgroup-display)
4899            (t
4900             (condition-case ()
4901                 (cond
4902                  ((and (or (<= scored marked) (= scored number))
4903                        (numberp gnus-large-newsgroup)
4904                        (> number gnus-large-newsgroup))
4905                   (let ((input
4906                          (read-string
4907                           (format
4908                            "How many articles from %s (default %d): "
4909                            (gnus-limit-string
4910                             (gnus-group-decoded-name gnus-newsgroup-name)
4911                             35)
4912                            number))))
4913                     (if (string-match "^[ \t]*$" input) number input)))
4914                  ((and (> scored marked) (< scored number)
4915                        (> (- scored number) 20))
4916                   (let ((input
4917                          (read-string
4918                           (format "%s %s (%d scored, %d total): "
4919                                   "How many articles from"
4920                                   (gnus-group-decoded-name group)
4921                                   scored number))))
4922                     (if (string-match "^[ \t]*$" input)
4923                         number input)))
4924                  (t number))
4925               (quit
4926                (message "Quit getting the articles to read")
4927                nil))))))
4928     (setq select (if (stringp select) (string-to-number select) select))
4929     (if (or (null select) (zerop select))
4930         select
4931       (if (and (not (zerop scored)) (<= (abs select) scored))
4932           (progn
4933             (setq articles (sort scored-list '<))
4934             (setq number (length articles)))
4935         (setq articles (copy-sequence articles)))
4936
4937       (when (< (abs select) number)
4938         (if (< select 0)
4939             ;; Select the N oldest articles.
4940             (setcdr (nthcdr (1- (abs select)) articles) nil)
4941           ;; Select the N most recent articles.
4942           (setq articles (nthcdr (- number select) articles))))
4943       (setq gnus-newsgroup-unselected
4944             (gnus-sorted-difference gnus-newsgroup-unreads articles))
4945       (when gnus-alter-articles-to-read-function
4946         (setq gnus-newsgroup-unreads
4947               (sort
4948                (funcall gnus-alter-articles-to-read-function
4949                         gnus-newsgroup-name gnus-newsgroup-unreads)
4950                '<)))
4951       articles)))
4952
4953 (defun gnus-killed-articles (killed articles)
4954   (let (out)
4955     (while articles
4956       (when (inline (gnus-member-of-range (car articles) killed))
4957         (push (car articles) out))
4958       (setq articles (cdr articles)))
4959     out))
4960
4961 (defun gnus-uncompress-marks (marks)
4962   "Uncompress the mark ranges in MARKS."
4963   (let ((uncompressed '(score bookmark))
4964         out)
4965     (while marks
4966       (if (memq (caar marks) uncompressed)
4967           (push (car marks) out)
4968         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4969       (setq marks (cdr marks)))
4970     out))
4971
4972 (defun gnus-article-mark-to-type (mark)
4973   "Return the type of MARK."
4974   (or (cadr (assq mark gnus-article-special-mark-lists))
4975       'list))
4976
4977 (defun gnus-article-unpropagatable-p (mark)
4978   "Return whether MARK should be propagated to backend."
4979   (memq mark gnus-article-unpropagated-mark-lists))
4980
4981 (defun gnus-adjust-marked-articles (info)
4982   "Set all article lists and remove all marks that are no longer valid."
4983   (let* ((marked-lists (gnus-info-marks info))
4984          (active (gnus-active (gnus-info-group info)))
4985          (min (car active))
4986          (max (cdr active))
4987          (types gnus-article-mark-lists)
4988          marks var articles article mark mark-type)
4989
4990     (dolist (marks marked-lists)
4991       (setq mark (car marks)
4992             mark-type (gnus-article-mark-to-type mark)
4993             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
4994
4995       ;; We set the variable according to the type of the marks list,
4996       ;; and then adjust the marks to a subset of the active articles.
4997       (cond
4998        ;; Adjust "simple" lists.
4999        ((eq mark-type 'list)
5000         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5001         (when (memq mark '(tick dormant expire reply save))
5002           (while articles
5003             (when (or (< (setq article (pop articles)) min) (> article max))
5004               (set var (delq article (symbol-value var)))))))
5005        ;; Adjust assocs.
5006        ((eq mark-type 'tuple)
5007         (set var (setq articles (cdr marks)))
5008         (when (not (listp (cdr (symbol-value var))))
5009           (set var (list (symbol-value var))))
5010         (when (not (listp (cdr articles)))
5011           (setq articles (list articles)))
5012         (while articles
5013           (when (or (not (consp (setq article (pop articles))))
5014                     (< (car article) min)
5015                     (> (car article) max))
5016             (set var (delq article (symbol-value var))))))
5017        ;; Adjust ranges (sloppily).
5018        ((eq mark-type 'range)
5019         (cond
5020          ((eq mark 'seen)
5021           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5022           ;; It should be (seen (NUM1 . NUM2)).
5023           (when (numberp (cddr marks))
5024             (setcdr marks (list (cdr marks))))
5025           (setq articles (cdr marks))
5026           (while (and articles
5027                       (or (and (consp (car articles))
5028                                (> min (cdar articles)))
5029                           (and (numberp (car articles))
5030                                (> min (car articles)))))
5031             (pop articles))
5032           (set var articles))))))))
5033
5034 (defun gnus-update-missing-marks (missing)
5035   "Go through the list of MISSING articles and remove them from the mark lists."
5036   (when missing
5037     (let (var m)
5038       ;; Go through all types.
5039       (dolist (elem gnus-article-mark-lists)
5040         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5041           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5042           (when (symbol-value var)
5043             ;; This list has articles.  So we delete all missing
5044             ;; articles from it.
5045             (setq m missing)
5046             (while m
5047               (set var (delq (pop m) (symbol-value var))))))))))
5048
5049 (defun gnus-update-marks ()
5050   "Enter the various lists of marked articles into the newsgroup info list."
5051   (let ((types gnus-article-mark-lists)
5052         (info (gnus-get-info gnus-newsgroup-name))
5053         type list newmarked symbol delta-marks)
5054     (when info
5055       ;; Add all marks lists to the list of marks lists.
5056       (while (setq type (pop types))
5057         (setq list (symbol-value
5058                     (setq symbol
5059                           (intern (format "gnus-newsgroup-%s" (car type))))))
5060
5061         (when list
5062           ;; Get rid of the entries of the articles that have the
5063           ;; default score.
5064           (when (and (eq (cdr type) 'score)
5065                      gnus-save-score
5066                      list)
5067             (let* ((arts list)
5068                    (prev (cons nil list))
5069                    (all prev))
5070               (while arts
5071                 (if (or (not (consp (car arts)))
5072                         (= (cdar arts) gnus-summary-default-score))
5073                     (setcdr prev (cdr arts))
5074                   (setq prev arts))
5075                 (setq arts (cdr arts)))
5076               (setq list (cdr all)))))
5077
5078         (when (eq (cdr type) 'seen)
5079           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5080
5081         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5082           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5083
5084         (when (and (gnus-check-backend-function
5085                     'request-set-mark gnus-newsgroup-name)
5086                    (not (gnus-article-unpropagatable-p (cdr type))))
5087           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5088                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5089                  (add (gnus-remove-from-range
5090                        (gnus-copy-sequence list) old)))
5091             (when add
5092               (push (list add 'add (list (cdr type))) delta-marks))
5093             (when del
5094               (push (list del 'del (list (cdr type))) delta-marks))))
5095
5096         (when list
5097           (push (cons (cdr type) list) newmarked)))
5098
5099       (when delta-marks
5100         (unless (gnus-check-group gnus-newsgroup-name)
5101           (error "Can't open server for %s" gnus-newsgroup-name))
5102         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5103
5104       ;; Enter these new marks into the info of the group.
5105       (if (nthcdr 3 info)
5106           (setcar (nthcdr 3 info) newmarked)
5107         ;; Add the marks lists to the end of the info.
5108         (when newmarked
5109           (setcdr (nthcdr 2 info) (list newmarked))))
5110
5111       ;; Cut off the end of the info if there's nothing else there.
5112       (let ((i 5))
5113         (while (and (> i 2)
5114                     (not (nth i info)))
5115           (when (nthcdr (decf i) info)
5116             (setcdr (nthcdr i info) nil)))))))
5117
5118 (defun gnus-set-mode-line (where)
5119   "Set the mode line of the article or summary buffers.
5120 If WHERE is `summary', the summary mode line format will be used."
5121   ;; Is this mode line one we keep updated?
5122   (when (and (memq where gnus-updated-mode-lines)
5123              (symbol-value
5124               (intern (format "gnus-%s-mode-line-format-spec" where))))
5125     (let (mode-string)
5126       (save-excursion
5127         ;; We evaluate this in the summary buffer since these
5128         ;; variables are buffer-local to that buffer.
5129         (set-buffer gnus-summary-buffer)
5130        ;; We bind all these variables that are used in the `eval' form
5131         ;; below.
5132         (let* ((mformat (symbol-value
5133                          (intern
5134                           (format "gnus-%s-mode-line-format-spec" where))))
5135                (gnus-tmp-group-name (gnus-group-decoded-name
5136                                      gnus-newsgroup-name))
5137                (gnus-tmp-article-number (or gnus-current-article 0))
5138                (gnus-tmp-unread gnus-newsgroup-unreads)
5139                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5140                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5141                (gnus-tmp-unread-and-unselected
5142                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5143                             (zerop gnus-tmp-unselected))
5144                        "")
5145                       ((zerop gnus-tmp-unselected)
5146                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5147                       (t (format "{%d(+%d) more}"
5148                                  gnus-tmp-unread-and-unticked
5149                                  gnus-tmp-unselected))))
5150                (gnus-tmp-subject
5151                 (if (and gnus-current-headers
5152                          (vectorp gnus-current-headers))
5153                     (gnus-mode-string-quote
5154                      (mail-header-subject gnus-current-headers))
5155                   ""))
5156                bufname-length max-len
5157                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5158           (setq mode-string (eval mformat))
5159           (setq bufname-length (if (string-match "%b" mode-string)
5160                                    (- (length
5161                                        (buffer-name
5162                                         (if (eq where 'summary)
5163                                             nil
5164                                           (get-buffer gnus-article-buffer))))
5165                                       2)
5166                                  0))
5167           (setq max-len (max 4 (if gnus-mode-non-string-length
5168                                    (- (window-width)
5169                                       gnus-mode-non-string-length
5170                                       bufname-length)
5171                                  (length mode-string))))
5172           ;; We might have to chop a bit of the string off...
5173           (when (> (length mode-string) max-len)
5174             (setq mode-string
5175                   (concat (truncate-string-to-width mode-string (- max-len 3))
5176                           "...")))
5177           ;; Pad the mode string a bit.
5178           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5179       ;; Update the mode line.
5180       (setq mode-line-buffer-identification
5181             (gnus-mode-line-buffer-identification (list mode-string)))
5182       (set-buffer-modified-p t))))
5183
5184 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5185   "Go through the HEADERS list and add all Xrefs to a hash table.
5186 The resulting hash table is returned, or nil if no Xrefs were found."
5187   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5188          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5189          (xref-hashtb (gnus-make-hashtable))
5190          start group entry number xrefs header)
5191     (while headers
5192       (setq header (pop headers))
5193       (when (and (setq xrefs (mail-header-xref header))
5194                  (not (memq (setq number (mail-header-number header))
5195                             unreads)))
5196         (setq start 0)
5197         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5198           (setq start (match-end 0))
5199           (setq group (if prefix
5200                           (concat prefix (substring xrefs (match-beginning 1)
5201                                                     (match-end 1)))
5202                         (substring xrefs (match-beginning 1) (match-end 1))))
5203           (setq number
5204                 (string-to-int (substring xrefs (match-beginning 2)
5205                                           (match-end 2))))
5206           (if (setq entry (gnus-gethash group xref-hashtb))
5207               (setcdr entry (cons number (cdr entry)))
5208             (gnus-sethash group (cons number nil) xref-hashtb)))))
5209     (and start xref-hashtb)))
5210
5211 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5212   "Look through all the headers and mark the Xrefs as read."
5213   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5214         name entry info xref-hashtb idlist method nth4)
5215     (save-excursion
5216       (set-buffer gnus-group-buffer)
5217       (when (setq xref-hashtb
5218                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5219         (mapatoms
5220          (lambda (group)
5221            (unless (string= from-newsgroup (setq name (symbol-name group)))
5222              (setq idlist (symbol-value group))
5223              ;; Dead groups are not updated.
5224              (and (prog1
5225                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5226                             info (nth 2 entry))
5227                     (when (stringp (setq nth4 (gnus-info-method info)))
5228                       (setq nth4 (gnus-server-to-method nth4))))
5229                   ;; Only do the xrefs if the group has the same
5230                   ;; select method as the group we have just read.
5231                   (or (gnus-methods-equal-p
5232                        nth4 (gnus-find-method-for-group from-newsgroup))
5233                       virtual
5234                       (equal nth4 (setq method (gnus-find-method-for-group
5235                                                 from-newsgroup)))
5236                       (and (equal (car nth4) (car method))
5237                            (equal (nth 1 nth4) (nth 1 method))))
5238                   gnus-use-cross-reference
5239                   (or (not (eq gnus-use-cross-reference t))
5240                       virtual
5241                       ;; Only do cross-references on subscribed
5242                       ;; groups, if that is what is wanted.
5243                       (<= (gnus-info-level info) gnus-level-subscribed))
5244                   (gnus-group-make-articles-read name idlist))))
5245          xref-hashtb)))))
5246
5247 (defun gnus-compute-read-articles (group articles)
5248   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5249          (info (nth 2 entry))
5250          (active (gnus-active group))
5251          ninfo)
5252     (when entry
5253       ;; First peel off all invalid article numbers.
5254       (when active
5255         (let ((ids articles)
5256               id first)
5257           (while (setq id (pop ids))
5258             (when (and first (> id (cdr active)))
5259               ;; We'll end up in this situation in one particular
5260               ;; obscure situation.  If you re-scan a group and get
5261               ;; a new article that is cross-posted to a different
5262               ;; group that has not been re-scanned, you might get
5263               ;; crossposted article that has a higher number than
5264               ;; Gnus believes possible.  So we re-activate this
5265               ;; group as well.  This might mean doing the
5266               ;; crossposting thingy will *increase* the number
5267               ;; of articles in some groups.  Tsk, tsk.
5268               (setq active (or (gnus-activate-group group) active)))
5269             (when (or (> id (cdr active))
5270                       (< id (car active)))
5271               (setq articles (delq id articles))))))
5272       ;; If the read list is nil, we init it.
5273       (if (and active
5274                (null (gnus-info-read info))
5275                (> (car active) 1))
5276           (setq ninfo (cons 1 (1- (car active))))
5277         (setq ninfo (gnus-info-read info)))
5278       ;; Then we add the read articles to the range.
5279       (gnus-add-to-range
5280        ninfo (setq articles (sort articles '<))))))
5281
5282 (defun gnus-group-make-articles-read (group articles)
5283   "Update the info of GROUP to say that ARTICLES are read."
5284   (let* ((num 0)
5285          (entry (gnus-gethash group gnus-newsrc-hashtb))
5286          (info (nth 2 entry))
5287          (active (gnus-active group))
5288          range)
5289     (when entry
5290       (setq range (gnus-compute-read-articles group articles))
5291       (save-excursion
5292         (set-buffer gnus-group-buffer)
5293         (gnus-undo-register
5294           `(progn
5295              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5296              (gnus-info-set-read ',info ',(gnus-info-read info))
5297              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5298              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5299              (gnus-group-update-group ,group t))))
5300       ;; Add the read articles to the range.
5301       (gnus-info-set-read info range)
5302       (gnus-request-set-mark group (list (list range 'add '(read))))
5303       ;; Then we have to re-compute how many unread
5304       ;; articles there are in this group.
5305       (when active
5306         (cond
5307          ((not range)
5308           (setq num (- (1+ (cdr active)) (car active))))
5309          ((not (listp (cdr range)))
5310           (setq num (- (cdr active) (- (1+ (cdr range))
5311                                        (car range)))))
5312          (t
5313           (while range
5314             (if (numberp (car range))
5315                 (setq num (1+ num))
5316               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5317             (setq range (cdr range)))
5318           (setq num (- (cdr active) num))))
5319         ;; Update the number of unread articles.
5320         (setcar entry num)
5321         ;; Update the group buffer.
5322         (gnus-group-update-group group t)))))
5323
5324 (defvar gnus-newsgroup-none-id 0)
5325
5326 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5327   (let ((cur nntp-server-buffer)
5328         (dependencies
5329          (or dependencies
5330              (save-excursion (set-buffer gnus-summary-buffer)
5331                              gnus-newsgroup-dependencies)))
5332         headers id end ref
5333         (mail-parse-charset gnus-newsgroup-charset)
5334         (mail-parse-ignored-charsets
5335          (save-excursion (condition-case nil
5336                              (set-buffer gnus-summary-buffer)
5337                            (error))
5338                          gnus-newsgroup-ignored-charsets)))
5339     (save-excursion
5340       (set-buffer nntp-server-buffer)
5341       ;; Translate all TAB characters into SPACE characters.
5342       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5343       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5344       (gnus-run-hooks 'gnus-parse-headers-hook)
5345       (let ((case-fold-search t)
5346             in-reply-to header p lines chars)
5347         (goto-char (point-min))
5348         ;; Search to the beginning of the next header.  Error messages
5349         ;; do not begin with 2 or 3.
5350         (while (re-search-forward "^[23][0-9]+ " nil t)
5351           (setq id nil
5352                 ref nil)
5353           ;; This implementation of this function, with nine
5354           ;; search-forwards instead of the one re-search-forward and
5355           ;; a case (which basically was the old function) is actually
5356           ;; about twice as fast, even though it looks messier.  You
5357           ;; can't have everything, I guess.  Speed and elegance
5358           ;; doesn't always go hand in hand.
5359           (setq
5360            header
5361            (vector
5362             ;; Number.
5363             (prog1
5364                 (read cur)
5365               (end-of-line)
5366               (setq p (point))
5367               (narrow-to-region (point)
5368                                 (or (and (search-forward "\n.\n" nil t)
5369                                          (- (point) 2))
5370                                     (point))))
5371             ;; Subject.
5372             (progn
5373               (goto-char p)
5374               (if (search-forward "\nsubject:" nil t)
5375                   (funcall gnus-decode-encoded-word-function
5376                            (nnheader-header-value))
5377                 "(none)"))
5378             ;; From.
5379             (progn
5380               (goto-char p)
5381               (if (search-forward "\nfrom:" nil t)
5382                   (funcall gnus-decode-encoded-word-function
5383                            (nnheader-header-value))
5384                 "(nobody)"))
5385             ;; Date.
5386             (progn
5387               (goto-char p)
5388               (if (search-forward "\ndate:" nil t)
5389                   (nnheader-header-value) ""))
5390             ;; Message-ID.
5391             (progn
5392               (goto-char p)
5393               (setq id (if (re-search-forward
5394                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5395                            ;; We do it this way to make sure the Message-ID
5396                            ;; is (somewhat) syntactically valid.
5397                            (buffer-substring (match-beginning 1)
5398                                              (match-end 1))
5399                          ;; If there was no message-id, we just fake one
5400                          ;; to make subsequent routines simpler.
5401                          (nnheader-generate-fake-message-id))))
5402             ;; References.
5403             (progn
5404               (goto-char p)
5405               (if (search-forward "\nreferences:" nil t)
5406                   (progn
5407                     (setq end (point))
5408                     (prog1
5409                         (nnheader-header-value)
5410                       (setq ref
5411                             (buffer-substring
5412                              (progn
5413                                (end-of-line)
5414                                (search-backward ">" end t)
5415                                (1+ (point)))
5416                              (progn
5417                                (search-backward "<" end t)
5418                                (point))))))
5419                 ;; Get the references from the in-reply-to header if there
5420                 ;; were no references and the in-reply-to header looks
5421                 ;; promising.
5422                 (if (and (search-forward "\nin-reply-to:" nil t)
5423                          (setq in-reply-to (nnheader-header-value))
5424                          (string-match "<[^>]+>" in-reply-to))
5425                     (let (ref2)
5426                       (setq ref (substring in-reply-to (match-beginning 0)
5427                                            (match-end 0)))
5428                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5429                         (setq ref2 (substring in-reply-to (match-beginning 0)
5430                                               (match-end 0)))
5431                         (when (> (length ref2) (length ref))
5432                           (setq ref ref2)))
5433                       ref)
5434                   (setq ref nil))))
5435             ;; Chars.
5436             (progn
5437               (goto-char p)
5438               (if (search-forward "\nchars: " nil t)
5439                   (if (numberp (setq chars (ignore-errors (read cur))))
5440                       chars -1)
5441                 -1))
5442             ;; Lines.
5443             (progn
5444               (goto-char p)
5445               (if (search-forward "\nlines: " nil t)
5446                   (if (numberp (setq lines (ignore-errors (read cur))))
5447                       lines -1)
5448                 -1))
5449             ;; Xref.
5450             (progn
5451               (goto-char p)
5452               (and (search-forward "\nxref:" nil t)
5453                    (nnheader-header-value)))
5454             ;; Extra.
5455             (when gnus-extra-headers
5456               (let ((extra gnus-extra-headers)
5457                     out)
5458                 (while extra
5459                   (goto-char p)
5460                   (when (search-forward
5461                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5462                     (push (cons (car extra) (nnheader-header-value))
5463                           out))
5464                   (pop extra))
5465                 out))))
5466           (when (equal id ref)
5467             (setq ref nil))
5468
5469           (when gnus-alter-header-function
5470             (funcall gnus-alter-header-function header)
5471             (setq id (mail-header-id header)
5472                   ref (gnus-parent-id (mail-header-references header))))
5473
5474           (when (setq header
5475                       (gnus-dependencies-add-header
5476                        header dependencies force-new))
5477             (push header headers))
5478           (goto-char (point-max))
5479           (widen))
5480         (nreverse headers)))))
5481
5482 ;; Goes through the xover lines and returns a list of vectors
5483 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5484                                                   force-new dependencies
5485                                                   group also-fetch-heads)
5486   "Parse the news overview data in the server buffer.
5487 Return a list of headers that match SEQUENCE (see
5488 `nntp-retrieve-headers')."
5489   ;; Get the Xref when the users reads the articles since most/some
5490   ;; NNTP servers do not include Xrefs when using XOVER.
5491   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5492   (let ((mail-parse-charset gnus-newsgroup-charset)
5493         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5494         (cur nntp-server-buffer)
5495         (dependencies (or dependencies gnus-newsgroup-dependencies))
5496         (allp (cond
5497                ((eq gnus-read-all-available-headers t)
5498                 t)
5499                ((stringp gnus-read-all-available-headers)
5500                 (string-match gnus-read-all-available-headers group))
5501                (t
5502                 nil)))
5503         number headers header)
5504     (save-excursion
5505       (set-buffer nntp-server-buffer)
5506       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5507       ;; Allow the user to mangle the headers before parsing them.
5508       (gnus-run-hooks 'gnus-parse-headers-hook)
5509       (goto-char (point-min))
5510       (gnus-parse-without-error
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       ;; A common bug in inn is that if you have posted an article and
5529       ;; then retrieves the active file, it will answer correctly --
5530       ;; the new article is included.  However, a NOV entry for the
5531       ;; article may not have been generated yet, so this may fail.
5532       ;; We work around this problem by retrieving the last few
5533       ;; headers using HEAD.
5534       (if (or (not also-fetch-heads)
5535               (not sequence))
5536           ;; We (probably) got all the headers.
5537           (nreverse headers)
5538         (let ((gnus-nov-is-evil t))
5539           (nconc
5540            (nreverse headers)
5541            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5542              (gnus-get-newsgroup-headers))))))))
5543
5544 (defun gnus-article-get-xrefs ()
5545   "Fill in the Xref value in `gnus-current-headers', if necessary.
5546 This is meant to be called in `gnus-article-internal-prepare-hook'."
5547   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5548                                  gnus-current-headers)))
5549     (or (not gnus-use-cross-reference)
5550         (not headers)
5551         (and (mail-header-xref headers)
5552              (not (string= (mail-header-xref headers) "")))
5553         (let ((case-fold-search t)
5554               xref)
5555           (save-restriction
5556             (nnheader-narrow-to-headers)
5557             (goto-char (point-min))
5558             (when (or (and (not (eobp))
5559                            (eq (downcase (char-after)) ?x)
5560                            (looking-at "Xref:"))
5561                       (search-forward "\nXref:" nil t))
5562               (goto-char (1+ (match-end 0)))
5563               (setq xref (buffer-substring (point)
5564                                            (progn (end-of-line) (point))))
5565               (mail-header-set-xref headers xref)))))))
5566
5567 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5568   "Find article ID and insert the summary line for that article.
5569 OLD-HEADER can either be a header or a line number to insert
5570 the subject line on."
5571   (let* ((line (and (numberp old-header) old-header))
5572          (old-header (and (vectorp old-header) old-header))
5573          (header (cond ((and old-header use-old-header)
5574                         old-header)
5575                        ((and (numberp id)
5576                              (gnus-number-to-header id))
5577                         (gnus-number-to-header id))
5578                        (t
5579                         (gnus-read-header id))))
5580          (number (and (numberp id) id))
5581          d)
5582     (when header
5583       ;; Rebuild the thread that this article is part of and go to the
5584       ;; article we have fetched.
5585       (when (and (not gnus-show-threads)
5586                  old-header)
5587         (when (and number
5588                    (setq d (gnus-data-find (mail-header-number old-header))))
5589           (goto-char (gnus-data-pos d))
5590           (gnus-data-remove
5591            number
5592            (- (gnus-point-at-bol)
5593               (prog1
5594                   (1+ (gnus-point-at-eol))
5595                 (gnus-delete-line))))))
5596       (when old-header
5597         (mail-header-set-number header (mail-header-number old-header)))
5598       (setq gnus-newsgroup-sparse
5599             (delq (setq number (mail-header-number header))
5600                   gnus-newsgroup-sparse))
5601       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5602       (push number gnus-newsgroup-limit)
5603       (gnus-rebuild-thread (mail-header-id header) line)
5604       (gnus-summary-goto-subject number nil t))
5605     (when (and (numberp number)
5606                (> number 0))
5607       ;; We have to update the boundaries even if we can't fetch the
5608       ;; article if ID is a number -- so that the next `P' or `N'
5609       ;; command will fetch the previous (or next) article even
5610       ;; if the one we tried to fetch this time has been canceled.
5611       (when (> number gnus-newsgroup-end)
5612         (setq gnus-newsgroup-end number))
5613       (when (< number gnus-newsgroup-begin)
5614         (setq gnus-newsgroup-begin number))
5615       (setq gnus-newsgroup-unselected
5616             (delq number gnus-newsgroup-unselected)))
5617     ;; Report back a success?
5618     (and header (mail-header-number header))))
5619
5620 ;;; Process/prefix in the summary buffer
5621
5622 (defun gnus-summary-work-articles (n)
5623   "Return a list of articles to be worked upon.
5624 The prefix argument, the list of process marked articles, and the
5625 current article will be taken into consideration."
5626   (save-excursion
5627     (set-buffer gnus-summary-buffer)
5628     (cond
5629      (n
5630       ;; A numerical prefix has been given.
5631       (setq n (prefix-numeric-value n))
5632       (let ((backward (< n 0))
5633             (n (abs (prefix-numeric-value n)))
5634             articles article)
5635         (save-excursion
5636           (while
5637               (and (> n 0)
5638                    (push (setq article (gnus-summary-article-number))
5639                          articles)
5640                    (if backward
5641                        (gnus-summary-find-prev nil article)
5642                      (gnus-summary-find-next nil article)))
5643             (decf n)))
5644         (nreverse articles)))
5645      ((and (gnus-region-active-p) (mark))
5646       (message "region active")
5647       ;; Work on the region between point and mark.
5648       (let ((max (max (point) (mark)))
5649             articles article)
5650         (save-excursion
5651           (goto-char (min (min (point) (mark))))
5652           (while
5653               (and
5654                (push (setq article (gnus-summary-article-number)) articles)
5655                (gnus-summary-find-next nil article)
5656                (< (point) max)))
5657           (nreverse articles))))
5658      (gnus-newsgroup-processable
5659       ;; There are process-marked articles present.
5660       ;; Save current state.
5661       (gnus-summary-save-process-mark)
5662       ;; Return the list.
5663       (reverse gnus-newsgroup-processable))
5664      (t
5665       ;; Just return the current article.
5666       (list (gnus-summary-article-number))))))
5667
5668 (defmacro gnus-summary-iterate (arg &rest forms)
5669   "Iterate over the process/prefixed articles and do FORMS.
5670 ARG is the interactive prefix given to the command.  FORMS will be
5671 executed with point over the summary line of the articles."
5672   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5673     `(let ((,articles (gnus-summary-work-articles ,arg)))
5674        (while ,articles
5675          (gnus-summary-goto-subject (car ,articles))
5676          ,@forms
5677          (pop ,articles)))))
5678
5679 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5680 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5681
5682 (defun gnus-summary-save-process-mark ()
5683   "Push the current set of process marked articles on the stack."
5684   (interactive)
5685   (push (copy-sequence gnus-newsgroup-processable)
5686         gnus-newsgroup-process-stack))
5687
5688 (defun gnus-summary-kill-process-mark ()
5689   "Push the current set of process marked articles on the stack and unmark."
5690   (interactive)
5691   (gnus-summary-save-process-mark)
5692   (gnus-summary-unmark-all-processable))
5693
5694 (defun gnus-summary-yank-process-mark ()
5695   "Pop the last process mark state off the stack and restore it."
5696   (interactive)
5697   (unless gnus-newsgroup-process-stack
5698     (error "Empty mark stack"))
5699   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5700
5701 (defun gnus-summary-process-mark-set (set)
5702   "Make SET into the current process marked articles."
5703   (gnus-summary-unmark-all-processable)
5704   (while set
5705     (gnus-summary-set-process-mark (pop set))))
5706
5707 ;;; Searching and stuff
5708
5709 (defun gnus-summary-search-group (&optional backward use-level)
5710   "Search for next unread newsgroup.
5711 If optional argument BACKWARD is non-nil, search backward instead."
5712   (save-excursion
5713     (set-buffer gnus-group-buffer)
5714     (when (gnus-group-search-forward
5715            backward nil (if use-level (gnus-group-group-level) nil))
5716       (gnus-group-group-name))))
5717
5718 (defun gnus-summary-best-group (&optional exclude-group)
5719   "Find the name of the best unread group.
5720 If EXCLUDE-GROUP, do not go to this group."
5721   (save-excursion
5722     (set-buffer gnus-group-buffer)
5723     (save-excursion
5724       (gnus-group-best-unread-group exclude-group))))
5725
5726 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5727   (if backward (gnus-summary-find-prev)
5728     (let* ((dummy (gnus-summary-article-intangible-p))
5729            (article (or article (gnus-summary-article-number)))
5730            (arts (gnus-data-find-list article))
5731            result)
5732       (when (and (not dummy)
5733                  (or (not gnus-summary-check-current)
5734                      (not unread)
5735                      (not (gnus-data-unread-p (car arts)))))
5736         (setq arts (cdr arts)))
5737       (when (setq result
5738                   (if unread
5739                       (progn
5740                         (while arts
5741                           (when (or (and undownloaded
5742                                          (eq gnus-undownloaded-mark
5743                                              (gnus-data-mark (car arts))))
5744                                     (gnus-data-unread-p (car arts)))
5745                             (setq result (car arts)
5746                                   arts nil))
5747                           (setq arts (cdr arts)))
5748                         result)
5749                     (car arts)))
5750         (goto-char (gnus-data-pos result))
5751         (gnus-data-number result)))))
5752
5753 (defun gnus-summary-find-prev (&optional unread article)
5754   (let* ((eobp (eobp))
5755          (article (or article (gnus-summary-article-number)))
5756          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5757          result)
5758     (when (and (not eobp)
5759                (or (not gnus-summary-check-current)
5760                    (not unread)
5761                    (not (gnus-data-unread-p (car arts)))))
5762       (setq arts (cdr arts)))
5763     (when (setq result
5764                 (if unread
5765                     (progn
5766                       (while arts
5767                         (when (gnus-data-unread-p (car arts))
5768                           (setq result (car arts)
5769                                 arts nil))
5770                         (setq arts (cdr arts)))
5771                       result)
5772                   (car arts)))
5773       (goto-char (gnus-data-pos result))
5774       (gnus-data-number result))))
5775
5776 (defun gnus-summary-find-subject (subject &optional unread backward article)
5777   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5778          (article (or article (gnus-summary-article-number)))
5779          (articles (gnus-data-list backward))
5780          (arts (gnus-data-find-list article articles))
5781          result)
5782     (when (or (not gnus-summary-check-current)
5783               (not unread)
5784               (not (gnus-data-unread-p (car arts))))
5785       (setq arts (cdr arts)))
5786     (while arts
5787       (and (or (not unread)
5788                (gnus-data-unread-p (car arts)))
5789            (vectorp (gnus-data-header (car arts)))
5790            (gnus-subject-equal
5791             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5792            (setq result (car arts)
5793                  arts nil))
5794       (setq arts (cdr arts)))
5795     (and result
5796          (goto-char (gnus-data-pos result))
5797          (gnus-data-number result))))
5798
5799 (defun gnus-summary-search-forward (&optional unread subject backward)
5800   "Search forward for an article.
5801 If UNREAD, look for unread articles.  If SUBJECT, look for
5802 articles with that subject.  If BACKWARD, search backward instead."
5803   (cond (subject (gnus-summary-find-subject subject unread backward))
5804         (backward (gnus-summary-find-prev unread))
5805         (t (gnus-summary-find-next unread))))
5806
5807 (defun gnus-recenter (&optional n)
5808   "Center point in window and redisplay frame.
5809 Also do horizontal recentering."
5810   (interactive "P")
5811   (when (and gnus-auto-center-summary
5812              (not (eq gnus-auto-center-summary 'vertical)))
5813     (gnus-horizontal-recenter))
5814   (recenter n))
5815
5816 (defun gnus-summary-recenter ()
5817   "Center point in the summary window.
5818 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5819 displayed, no centering will be performed."
5820   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5821 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5822   (interactive)
5823   (let* ((top (cond ((< (window-height) 4) 0)
5824                     ((< (window-height) 7) 1)
5825                     (t (if (numberp gnus-auto-center-summary)
5826                            gnus-auto-center-summary
5827                          2))))
5828          (height (1- (window-height)))
5829          (bottom (save-excursion (goto-char (point-max))
5830                                  (forward-line (- height))
5831                                  (point)))
5832          (window (get-buffer-window (current-buffer))))
5833     ;; The user has to want it.
5834     (when gnus-auto-center-summary
5835       (when (get-buffer-window gnus-article-buffer)
5836         ;; Only do recentering when the article buffer is displayed,
5837       ;; Set the window start to either `bottom', which is the biggest
5838         ;; possible valid number, or the second line from the top,
5839         ;; whichever is the least.
5840         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5841           (if (> bottom top-pos)
5842               ;; Keep the second line from the top visible
5843               (set-window-start window top-pos t)
5844             ;; Try to keep the bottom line visible; if it's partially
5845             ;; obscured, either scroll one more line to make it fully
5846             ;; visible, or revert to using TOP-POS.
5847             (save-excursion
5848               (goto-char (point-max))
5849               (forward-line -1)
5850               (let ((last-line-start (point)))
5851                 (goto-char bottom)
5852                 (set-window-start window (point) t)
5853                 (when (not (pos-visible-in-window-p last-line-start window))
5854                   (forward-line 1)
5855                   (set-window-start window (min (point) top-pos) t)))))))
5856       ;; Do horizontal recentering while we're at it.
5857       (when (and (get-buffer-window (current-buffer) t)
5858                  (not (eq gnus-auto-center-summary 'vertical)))
5859         (let ((selected (selected-window)))
5860           (select-window (get-buffer-window (current-buffer) t))
5861           (gnus-summary-position-point)
5862           (gnus-horizontal-recenter)
5863           (select-window selected))))))
5864
5865 (defun gnus-summary-jump-to-group (newsgroup)
5866   "Move point to NEWSGROUP in group mode buffer."
5867   ;; Keep update point of group mode buffer if visible.
5868   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5869       (save-window-excursion
5870         ;; Take care of tree window mode.
5871         (when (get-buffer-window gnus-group-buffer)
5872           (pop-to-buffer gnus-group-buffer))
5873         (gnus-group-jump-to-group newsgroup))
5874     (save-excursion
5875       ;; Take care of tree window mode.
5876       (if (get-buffer-window gnus-group-buffer)
5877           (pop-to-buffer gnus-group-buffer)
5878         (set-buffer gnus-group-buffer))
5879       (gnus-group-jump-to-group newsgroup))))
5880
5881 ;; This function returns a list of article numbers based on the
5882 ;; difference between the ranges of read articles in this group and
5883 ;; the range of active articles.
5884 (defun gnus-list-of-unread-articles (group)
5885   (let* ((read (gnus-info-read (gnus-get-info group)))
5886          (active (or (gnus-active group) (gnus-activate-group group)))
5887          (last (cdr active))
5888          first nlast unread)
5889     ;; If none are read, then all are unread.
5890     (if (not read)
5891         (setq first (car active))
5892       ;; If the range of read articles is a single range, then the
5893       ;; first unread article is the article after the last read
5894       ;; article.  Sounds logical, doesn't it?
5895       (if (and (not (listp (cdr read)))
5896                (or (< (car read) (car active))
5897                    (progn (setq read (list read))
5898                           nil)))
5899           (setq first (max (car active) (1+ (cdr read))))
5900         ;; `read' is a list of ranges.
5901         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5902                                   (caar read)))
5903                   1)
5904           (setq first (car active)))
5905         (while read
5906           (when first
5907             (while (< first nlast)
5908               (push first unread)
5909               (setq first (1+ first))))
5910           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5911           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5912           (setq read (cdr read)))))
5913     ;; And add the last unread articles.
5914     (while (<= first last)
5915       (push first unread)
5916       (setq first (1+ first)))
5917     ;; Return the list of unread articles.
5918     (delq 0 (nreverse unread))))
5919
5920 (defun gnus-list-of-read-articles (group)
5921   "Return a list of unread, unticked and non-dormant articles."
5922   (let* ((info (gnus-get-info group))
5923          (marked (gnus-info-marks info))
5924          (active (gnus-active group)))
5925     (and info active
5926          (gnus-list-range-difference
5927           (gnus-list-range-difference
5928            (gnus-sorted-complement
5929             (gnus-uncompress-range active)
5930             (gnus-list-of-unread-articles group))
5931            (cdr (assq 'dormant marked)))
5932           (cdr (assq 'tick marked))))))
5933
5934 ;; Various summary commands
5935
5936 (defun gnus-summary-select-article-buffer ()
5937   "Reconfigure windows to show article buffer."
5938   (interactive)
5939   (if (not (gnus-buffer-live-p gnus-article-buffer))
5940       (error "There is no article buffer for this summary buffer")
5941     (gnus-configure-windows 'article)
5942     (select-window (get-buffer-window gnus-article-buffer))))
5943
5944 (defun gnus-summary-universal-argument (arg)
5945   "Perform any operation on all articles that are process/prefixed."
5946   (interactive "P")
5947   (let ((articles (gnus-summary-work-articles arg))
5948         func article)
5949     (if (eq
5950          (setq
5951           func
5952           (key-binding
5953            (read-key-sequence
5954             (substitute-command-keys
5955              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5956          'undefined)
5957         (gnus-error 1 "Undefined key")
5958       (save-excursion
5959         (while articles
5960           (gnus-summary-goto-subject (setq article (pop articles)))
5961           (let (gnus-newsgroup-processable)
5962             (command-execute func))
5963           (gnus-summary-remove-process-mark article)))))
5964   (gnus-summary-position-point))
5965
5966 (defun gnus-summary-toggle-truncation (&optional arg)
5967   "Toggle truncation of summary lines.
5968 With arg, turn line truncation on iff arg is positive."
5969   (interactive "P")
5970   (setq truncate-lines
5971         (if (null arg) (not truncate-lines)
5972           (> (prefix-numeric-value arg) 0)))
5973   (redraw-display))
5974
5975 (defun gnus-summary-reselect-current-group (&optional all rescan)
5976   "Exit and then reselect the current newsgroup.
5977 The prefix argument ALL means to select all articles."
5978   (interactive "P")
5979   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5980     (error "Ephemeral groups can't be reselected"))
5981   (let ((current-subject (gnus-summary-article-number))
5982         (group gnus-newsgroup-name))
5983     (setq gnus-newsgroup-begin nil)
5984     (gnus-summary-exit)
5985     ;; We have to adjust the point of group mode buffer because
5986     ;; point was moved to the next unread newsgroup by exiting.
5987     (gnus-summary-jump-to-group group)
5988     (when rescan
5989       (save-excursion
5990         (gnus-group-get-new-news-this-group 1)))
5991     (gnus-group-read-group all t)
5992     (gnus-summary-goto-subject current-subject nil t)))
5993
5994 (defun gnus-summary-rescan-group (&optional all)
5995   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5996   (interactive "P")
5997   (gnus-summary-reselect-current-group all t))
5998
5999 (defun gnus-summary-update-info (&optional non-destructive)
6000   (save-excursion
6001     (let ((group gnus-newsgroup-name))
6002       (when group
6003         (when gnus-newsgroup-kill-headers
6004           (setq gnus-newsgroup-killed
6005                 (gnus-compress-sequence
6006                  (gnus-sorted-union
6007                   (gnus-list-range-intersection
6008                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6009                   gnus-newsgroup-unreads)
6010                  t)))
6011         (unless (listp (cdr gnus-newsgroup-killed))
6012           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6013         (let ((headers gnus-newsgroup-headers))
6014           ;; Set the new ranges of read articles.
6015           (save-excursion
6016             (set-buffer gnus-group-buffer)
6017             (gnus-undo-force-boundary))
6018           (gnus-update-read-articles
6019            group (gnus-sorted-union 
6020                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6021           ;; Set the current article marks.
6022           (let ((gnus-newsgroup-scored
6023                  (if (and (not gnus-save-score)
6024                           (not non-destructive))
6025                      nil
6026                    gnus-newsgroup-scored)))
6027             (save-excursion
6028               (gnus-update-marks)))
6029           ;; Do the cross-ref thing.
6030           (when gnus-use-cross-reference
6031             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6032           ;; Do not switch windows but change the buffer to work.
6033           (set-buffer gnus-group-buffer)
6034           (unless (gnus-ephemeral-group-p group)
6035             (gnus-group-update-group group)))))))
6036
6037 (defun gnus-summary-save-newsrc (&optional force)
6038   "Save the current number of read/marked articles in the dribble buffer.
6039 The dribble buffer will then be saved.
6040 If FORCE (the prefix), also save the .newsrc file(s)."
6041   (interactive "P")
6042   (gnus-summary-update-info t)
6043   (if force
6044       (gnus-save-newsrc-file)
6045     (gnus-dribble-save)))
6046
6047 (defun gnus-summary-exit (&optional temporary)
6048   "Exit reading current newsgroup, and then return to group selection mode.
6049 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6050   (interactive)
6051   (gnus-set-global-variables)
6052   (when (gnus-buffer-live-p gnus-article-buffer)
6053     (save-excursion
6054       (set-buffer gnus-article-buffer)
6055       (mm-destroy-parts gnus-article-mime-handles)
6056       ;; Set it to nil for safety reason.
6057       (setq gnus-article-mime-handle-alist nil)
6058       (setq gnus-article-mime-handles nil)))
6059   (gnus-kill-save-kill-buffer)
6060   (gnus-async-halt-prefetch)
6061   (let* ((group gnus-newsgroup-name)
6062          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6063          (mode major-mode)
6064          (group-point nil)
6065          (buf (current-buffer)))
6066     (unless quit-config
6067       ;; Do adaptive scoring, and possibly save score files.
6068       (when gnus-newsgroup-adaptive
6069         (gnus-score-adaptive))
6070       (when gnus-use-scoring
6071         (gnus-score-save)))
6072     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6073     ;; If we have several article buffers, we kill them at exit.
6074     (unless gnus-single-article-buffer
6075       (gnus-kill-buffer gnus-original-article-buffer)
6076       (setq gnus-article-current nil))
6077     (when gnus-use-cache
6078       (gnus-cache-possibly-remove-articles)
6079       (gnus-cache-save-buffers))
6080     (gnus-async-prefetch-remove-group group)
6081     (when gnus-suppress-duplicates
6082       (gnus-dup-enter-articles))
6083     (when gnus-use-trees
6084       (gnus-tree-close group))
6085     (when gnus-use-cache
6086       (gnus-cache-write-active))
6087     ;; Remove entries for this group.
6088     (nnmail-purge-split-history (gnus-group-real-name group))
6089     ;; Make all changes in this group permanent.
6090     (unless quit-config
6091       (gnus-run-hooks 'gnus-exit-group-hook)
6092       (gnus-summary-update-info))
6093     (gnus-close-group group)
6094     ;; Make sure where we were, and go to next newsgroup.
6095     (set-buffer gnus-group-buffer)
6096     (unless quit-config
6097       (gnus-group-jump-to-group group))
6098     (gnus-run-hooks 'gnus-summary-exit-hook)
6099     (unless (or quit-config
6100                 ;; If this group has disappeared from the summary
6101                 ;; buffer, don't skip forwards.
6102                 (not (string= group (gnus-group-group-name))))
6103       (gnus-group-next-unread-group 1))
6104     (setq group-point (point))
6105     (if temporary
6106         nil                             ;Nothing to do.
6107       ;; If we have several article buffers, we kill them at exit.
6108       (unless gnus-single-article-buffer
6109         (gnus-kill-buffer gnus-article-buffer)
6110         (gnus-kill-buffer gnus-original-article-buffer)
6111         (setq gnus-article-current nil))
6112       (set-buffer buf)
6113       (if (not gnus-kill-summary-on-exit)
6114           (progn
6115             (gnus-deaden-summary)
6116             (setq mode nil))
6117        ;; We set all buffer-local variables to nil.  It is unclear why
6118         ;; this is needed, but if we don't, buffer-local variables are
6119         ;; not garbage-collected, it seems.  This would the lead to en
6120         ;; ever-growing Emacs.
6121         (gnus-summary-clear-local-variables)
6122         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6123           (gnus-summary-clear-local-variables))
6124         (when (get-buffer gnus-article-buffer)
6125           (bury-buffer gnus-article-buffer))
6126         ;; We clear the global counterparts of the buffer-local
6127         ;; variables as well, just to be on the safe side.
6128         (set-buffer gnus-group-buffer)
6129         (gnus-summary-clear-local-variables)
6130         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6131           (gnus-summary-clear-local-variables)))
6132       (setq gnus-current-select-method gnus-select-method)
6133       (pop-to-buffer gnus-group-buffer)
6134       (if (not quit-config)
6135           (progn
6136             (goto-char group-point)
6137             (gnus-configure-windows 'group 'force))
6138         (gnus-handle-ephemeral-exit quit-config))
6139       ;; Return to group mode buffer.
6140       (when (eq mode 'gnus-summary-mode)
6141         (gnus-kill-buffer buf))
6142       ;; Clear the current group name.
6143       (unless quit-config
6144         (setq gnus-newsgroup-name nil)))))
6145
6146 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6147 (defun gnus-summary-exit-no-update (&optional no-questions)
6148   "Quit reading current newsgroup without updating read article info."
6149   (interactive)
6150   (let* ((group gnus-newsgroup-name)
6151          (quit-config (gnus-group-quit-config group)))
6152     (when (or no-questions
6153               gnus-expert-user
6154               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6155       (gnus-async-halt-prefetch)
6156       (mapcar 'funcall
6157               (delq 'gnus-summary-expire-articles
6158                     (copy-sequence gnus-summary-prepare-exit-hook)))
6159       (when (gnus-buffer-live-p gnus-article-buffer)
6160         (save-excursion
6161           (set-buffer gnus-article-buffer)
6162           (mm-destroy-parts gnus-article-mime-handles)
6163           ;; Set it to nil for safety reason.
6164           (setq gnus-article-mime-handle-alist nil)
6165           (setq gnus-article-mime-handles nil)))
6166       ;; If we have several article buffers, we kill them at exit.
6167       (unless gnus-single-article-buffer
6168         (gnus-kill-buffer gnus-article-buffer)
6169         (gnus-kill-buffer gnus-original-article-buffer)
6170         (setq gnus-article-current nil))
6171       (if (not gnus-kill-summary-on-exit)
6172           (gnus-deaden-summary)
6173         (gnus-close-group group)
6174         (gnus-summary-clear-local-variables)
6175         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6176           (gnus-summary-clear-local-variables))
6177         (set-buffer gnus-group-buffer)
6178         (gnus-summary-clear-local-variables)
6179         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6180           (gnus-summary-clear-local-variables))
6181         (when (get-buffer gnus-summary-buffer)
6182           (kill-buffer gnus-summary-buffer)))
6183       (unless gnus-single-article-buffer
6184         (setq gnus-article-current nil))
6185       (when gnus-use-trees
6186         (gnus-tree-close group))
6187       (gnus-async-prefetch-remove-group group)
6188       (when (get-buffer gnus-article-buffer)
6189         (bury-buffer gnus-article-buffer))
6190       ;; Return to the group buffer.
6191       (gnus-configure-windows 'group 'force)
6192       ;; Clear the current group name.
6193       (setq gnus-newsgroup-name nil)
6194       (when (equal (gnus-group-group-name) group)
6195         (gnus-group-next-unread-group 1))
6196       (when quit-config
6197         (gnus-handle-ephemeral-exit quit-config)))))
6198
6199 (defun gnus-handle-ephemeral-exit (quit-config)
6200   "Handle movement when leaving an ephemeral group.
6201 The state which existed when entering the ephemeral is reset."
6202   (if (not (buffer-name (car quit-config)))
6203       (gnus-configure-windows 'group 'force)
6204     (set-buffer (car quit-config))
6205     (cond ((eq major-mode 'gnus-summary-mode)
6206            (gnus-set-global-variables))
6207           ((eq major-mode 'gnus-article-mode)
6208            (save-excursion
6209              ;; The `gnus-summary-buffer' variable may point
6210              ;; to the old summary buffer when using a single
6211              ;; article buffer.
6212              (unless (gnus-buffer-live-p gnus-summary-buffer)
6213                (set-buffer gnus-group-buffer))
6214              (set-buffer gnus-summary-buffer)
6215              (gnus-set-global-variables))))
6216     (if (or (eq (cdr quit-config) 'article)
6217             (eq (cdr quit-config) 'pick))
6218         (progn
6219           ;; The current article may be from the ephemeral group
6220           ;; thus it is best that we reload this article
6221           (gnus-summary-show-article)
6222           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6223               (gnus-configure-windows 'pick 'force)
6224             (gnus-configure-windows (cdr quit-config) 'force)))
6225       (gnus-configure-windows (cdr quit-config) 'force))
6226     (when (eq major-mode 'gnus-summary-mode)
6227       (gnus-summary-next-subject 1 nil t)
6228       (gnus-summary-recenter)
6229       (gnus-summary-position-point))))
6230
6231 ;;; Dead summaries.
6232
6233 (defvar gnus-dead-summary-mode-map nil)
6234
6235 (unless gnus-dead-summary-mode-map
6236   (setq gnus-dead-summary-mode-map (make-keymap))
6237   (suppress-keymap gnus-dead-summary-mode-map)
6238   (substitute-key-definition
6239    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6240   (dolist (key '("\C-d" "\r" "\177" [delete]))
6241     (define-key gnus-dead-summary-mode-map
6242       key 'gnus-summary-wake-up-the-dead))
6243   (dolist (key '("q" "Q"))
6244     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6245
6246 (defvar gnus-dead-summary-mode nil
6247   "Minor mode for Gnus summary buffers.")
6248
6249 (defun gnus-dead-summary-mode (&optional arg)
6250   "Minor mode for Gnus summary buffers."
6251   (interactive "P")
6252   (when (eq major-mode 'gnus-summary-mode)
6253     (make-local-variable 'gnus-dead-summary-mode)
6254     (setq gnus-dead-summary-mode
6255           (if (null arg) (not gnus-dead-summary-mode)
6256             (> (prefix-numeric-value arg) 0)))
6257     (when gnus-dead-summary-mode
6258       (gnus-add-minor-mode
6259        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6260
6261 (defun gnus-deaden-summary ()
6262   "Make the current summary buffer into a dead summary buffer."
6263   ;; Kill any previous dead summary buffer.
6264   (when (and gnus-dead-summary
6265              (buffer-name gnus-dead-summary))
6266     (save-excursion
6267       (set-buffer gnus-dead-summary)
6268       (when gnus-dead-summary-mode
6269         (kill-buffer (current-buffer)))))
6270   ;; Make this the current dead summary.
6271   (setq gnus-dead-summary (current-buffer))
6272   (gnus-dead-summary-mode 1)
6273   (let ((name (buffer-name)))
6274     (when (string-match "Summary" name)
6275       (rename-buffer
6276        (concat (substring name 0 (match-beginning 0)) "Dead "
6277                (substring name (match-beginning 0)))
6278        t)
6279       (bury-buffer))))
6280
6281 (defun gnus-kill-or-deaden-summary (buffer)
6282   "Kill or deaden the summary BUFFER."
6283   (save-excursion
6284     (when (and (buffer-name buffer)
6285                (not gnus-single-article-buffer))
6286       (save-excursion
6287         (set-buffer buffer)
6288         (gnus-kill-buffer gnus-article-buffer)
6289         (gnus-kill-buffer gnus-original-article-buffer)))
6290     (cond
6291      ;; Kill the buffer.
6292      (gnus-kill-summary-on-exit
6293       (when (and gnus-use-trees
6294                  (gnus-buffer-exists-p buffer))
6295         (save-excursion
6296           (set-buffer buffer)
6297           (gnus-tree-close gnus-newsgroup-name)))
6298       (gnus-kill-buffer buffer))
6299      ;; Deaden the buffer.
6300      ((gnus-buffer-exists-p buffer)
6301       (save-excursion
6302         (set-buffer buffer)
6303         (gnus-deaden-summary))))))
6304
6305 (defun gnus-summary-wake-up-the-dead (&rest args)
6306   "Wake up the dead summary buffer."
6307   (interactive)
6308   (gnus-dead-summary-mode -1)
6309   (let ((name (buffer-name)))
6310     (when (string-match "Dead " name)
6311       (rename-buffer
6312        (concat (substring name 0 (match-beginning 0))
6313                (substring name (match-end 0)))
6314        t)))
6315   (gnus-message 3 "This dead summary is now alive again"))
6316
6317 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6318 (defun gnus-summary-fetch-faq (&optional faq-dir)
6319   "Fetch the FAQ for the current group.
6320 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6321 in."
6322   (interactive
6323    (list
6324     (when current-prefix-arg
6325       (completing-read
6326        "Faq dir: " (and (listp gnus-group-faq-directory)
6327                         (mapcar (lambda (file) (list file))
6328                                 gnus-group-faq-directory))))))
6329   (let (gnus-faq-buffer)
6330     (when (setq gnus-faq-buffer
6331                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6332       (gnus-configure-windows 'summary-faq))))
6333
6334 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6335 (defun gnus-summary-describe-group (&optional force)
6336   "Describe the current newsgroup."
6337   (interactive "P")
6338   (gnus-group-describe-group force gnus-newsgroup-name))
6339
6340 (defun gnus-summary-describe-briefly ()
6341   "Describe summary mode commands briefly."
6342   (interactive)
6343   (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")))
6344
6345 ;; Walking around group mode buffer from summary mode.
6346
6347 (defun gnus-summary-next-group (&optional no-article target-group backward)
6348   "Exit current newsgroup and then select next unread newsgroup.
6349 If prefix argument NO-ARTICLE is non-nil, no article is selected
6350 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6351 previous group instead."
6352   (interactive "P")
6353   ;; Stop pre-fetching.
6354   (gnus-async-halt-prefetch)
6355   (let ((current-group gnus-newsgroup-name)
6356         (current-buffer (current-buffer))
6357         entered)
6358    ;; First we semi-exit this group to update Xrefs and all variables.
6359     ;; We can't do a real exit, because the window conf must remain
6360     ;; the same in case the user is prompted for info, and we don't
6361     ;; want the window conf to change before that...
6362     (gnus-summary-exit t)
6363     (while (not entered)
6364       ;; Then we find what group we are supposed to enter.
6365       (set-buffer gnus-group-buffer)
6366       (gnus-group-jump-to-group current-group)
6367       (setq target-group
6368             (or target-group
6369                 (if (eq gnus-keep-same-level 'best)
6370                     (gnus-summary-best-group gnus-newsgroup-name)
6371                   (gnus-summary-search-group backward gnus-keep-same-level))))
6372       (if (not target-group)
6373           ;; There are no further groups, so we return to the group
6374           ;; buffer.
6375           (progn
6376             (gnus-message 5 "Returning to the group buffer")
6377             (setq entered t)
6378             (when (gnus-buffer-live-p current-buffer)
6379               (set-buffer current-buffer)
6380               (gnus-summary-exit))
6381             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6382         ;; We try to enter the target group.
6383         (gnus-group-jump-to-group target-group)
6384         (let ((unreads (gnus-group-group-unread)))
6385           (if (and (or (eq t unreads)
6386                        (and unreads (not (zerop unreads))))
6387                    (gnus-summary-read-group
6388                     target-group nil no-article
6389                     (and (buffer-name current-buffer) current-buffer)
6390                     nil backward))
6391               (setq entered t)
6392             (setq current-group target-group
6393                   target-group nil)))))))
6394
6395 (defun gnus-summary-prev-group (&optional no-article)
6396   "Exit current newsgroup and then select previous unread newsgroup.
6397 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6398   (interactive "P")
6399   (gnus-summary-next-group no-article nil t))
6400
6401 ;; Walking around summary lines.
6402
6403 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6404   "Go to the first unread subject.
6405 If UNREAD is non-nil, go to the first unread article.
6406 Returns the article selected or nil if there are no unread articles."
6407   (interactive "P")
6408   (prog1
6409       (cond
6410        ;; Empty summary.
6411        ((null gnus-newsgroup-data)
6412         (gnus-message 3 "No articles in the group")
6413         nil)
6414        ;; Pick the first article.
6415        ((not unread)
6416         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6417         (gnus-data-number (car gnus-newsgroup-data)))
6418        ;; No unread articles.
6419        ((null gnus-newsgroup-unreads)
6420         (gnus-message 3 "No more unread articles")
6421         nil)
6422        ;; Find the first unread article.
6423        (t
6424         (let ((data gnus-newsgroup-data))
6425           (while (and data
6426                       (and (not (and undownloaded
6427                                      (eq gnus-undownloaded-mark
6428                                          (gnus-data-mark (car data)))))
6429                            (if unseen
6430                                (or (not (memq
6431                                          (gnus-data-number (car data))
6432                                          gnus-newsgroup-unseen))
6433                                    (not (gnus-data-unread-p (car data))))
6434                              (not (gnus-data-unread-p (car data))))))
6435             (setq data (cdr data)))
6436           (when data
6437             (goto-char (gnus-data-pos (car data)))
6438             (gnus-data-number (car data))))))
6439     (gnus-summary-position-point)))
6440
6441 (defun gnus-summary-next-subject (n &optional unread dont-display)
6442   "Go to next N'th summary line.
6443 If N is negative, go to the previous N'th subject line.
6444 If UNREAD is non-nil, only unread articles are selected.
6445 The difference between N and the actual number of steps taken is
6446 returned."
6447   (interactive "p")
6448   (let ((backward (< n 0))
6449         (n (abs n)))
6450     (while (and (> n 0)
6451                 (if backward
6452                     (gnus-summary-find-prev unread)
6453                   (gnus-summary-find-next unread)))
6454       (unless (zerop (setq n (1- n)))
6455         (gnus-summary-show-thread)))
6456     (when (/= 0 n)
6457       (gnus-message 7 "No more%s articles"
6458                     (if unread " unread" "")))
6459     (unless dont-display
6460       (gnus-summary-recenter)
6461       (gnus-summary-position-point))
6462     n))
6463
6464 (defun gnus-summary-next-unread-subject (n)
6465   "Go to next N'th unread summary line."
6466   (interactive "p")
6467   (gnus-summary-next-subject n t))
6468
6469 (defun gnus-summary-prev-subject (n &optional unread)
6470   "Go to previous N'th summary line.
6471 If optional argument UNREAD is non-nil, only unread article is selected."
6472   (interactive "p")
6473   (gnus-summary-next-subject (- n) unread))
6474
6475 (defun gnus-summary-prev-unread-subject (n)
6476   "Go to previous N'th unread summary line."
6477   (interactive "p")
6478   (gnus-summary-next-subject (- n) t))
6479
6480 (defun gnus-summary-goto-subject (article &optional force silent)
6481   "Go the subject line of ARTICLE.
6482 If FORCE, also allow jumping to articles not currently shown."
6483   (interactive "nArticle number: ")
6484   (unless (numberp article)
6485     (error "Article %s is not a number" article))
6486   (let ((b (point))
6487         (data (gnus-data-find article)))
6488     ;; We read in the article if we have to.
6489     (and (not data)
6490          force
6491          (gnus-summary-insert-subject
6492           article
6493           (if (or (numberp force) (vectorp force)) force)
6494           t)
6495          (setq data (gnus-data-find article)))
6496     (goto-char b)
6497     (if (not data)
6498         (progn
6499           (unless silent
6500             (gnus-message 3 "Can't find article %d" article))
6501           nil)
6502       (let ((pt (gnus-data-pos data)))
6503         (goto-char pt)
6504         (gnus-summary-set-article-display-arrow pt))
6505       (gnus-summary-position-point)
6506       article)))
6507
6508 ;; Walking around summary lines with displaying articles.
6509
6510 (defun gnus-summary-expand-window (&optional arg)
6511   "Make the summary buffer take up the entire Emacs frame.
6512 Given a prefix, will force an `article' buffer configuration."
6513   (interactive "P")
6514   (if arg
6515       (gnus-configure-windows 'article 'force)
6516     (gnus-configure-windows 'summary 'force)))
6517
6518 (defun gnus-summary-display-article (article &optional all-header)
6519   "Display ARTICLE in article buffer."
6520   (when (gnus-buffer-live-p gnus-article-buffer)
6521     (with-current-buffer gnus-article-buffer
6522       (mm-enable-multibyte)))
6523   (gnus-set-global-variables)
6524   (when (gnus-buffer-live-p gnus-article-buffer)
6525     (with-current-buffer gnus-article-buffer
6526       (setq gnus-article-charset gnus-newsgroup-charset)
6527       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6528       (mm-enable-multibyte)))
6529   (if (null article)
6530       nil
6531     (prog1
6532         (if gnus-summary-display-article-function
6533             (funcall gnus-summary-display-article-function article all-header)
6534           (gnus-article-prepare article all-header))
6535       (gnus-run-hooks 'gnus-select-article-hook)
6536       (when (and gnus-current-article
6537                  (not (zerop gnus-current-article)))
6538         (gnus-summary-goto-subject gnus-current-article))
6539       (gnus-summary-recenter)
6540       (when (and gnus-use-trees gnus-show-threads)
6541         (gnus-possibly-generate-tree article)
6542         (gnus-highlight-selected-tree article))
6543       ;; Successfully display article.
6544       (gnus-article-set-window-start
6545        (cdr (assq article gnus-newsgroup-bookmarks))))))
6546
6547 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6548   "Select the current article.
6549 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6550 non-nil, the article will be re-fetched even if it already present in
6551 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6552 be displayed."
6553   ;; Make sure we are in the summary buffer to work around bbdb bug.
6554   (unless (eq major-mode 'gnus-summary-mode)
6555     (set-buffer gnus-summary-buffer))
6556   (let ((article (or article (gnus-summary-article-number)))
6557         (all-headers (not (not all-headers))) ;Must be t or nil.
6558         gnus-summary-display-article-function)
6559     (and (not pseudo)
6560          (gnus-summary-article-pseudo-p article)
6561          (error "This is a pseudo-article"))
6562     (save-excursion
6563       (set-buffer gnus-summary-buffer)
6564       (if (or (and gnus-single-article-buffer
6565                    (or (null gnus-current-article)
6566                        (null gnus-article-current)
6567                        (null (get-buffer gnus-article-buffer))
6568                        (not (eq article (cdr gnus-article-current)))
6569                        (not (equal (car gnus-article-current)
6570                                    gnus-newsgroup-name))))
6571               (and (not gnus-single-article-buffer)
6572                    (or (null gnus-current-article)
6573                        (not (eq gnus-current-article article))))
6574               force)
6575           ;; The requested article is different from the current article.
6576           (progn
6577             (gnus-summary-display-article article all-headers)
6578             (when (gnus-buffer-live-p gnus-article-buffer)
6579               (with-current-buffer gnus-article-buffer
6580                 (if (not gnus-article-decoded-p) ;; a local variable
6581                     (mm-disable-multibyte))))
6582 ;;; Hidden headers are not hidden text any more.
6583 ;;          (when (or all-headers gnus-show-all-headers)
6584 ;;            (gnus-article-show-all-headers))
6585             (gnus-article-set-window-start
6586              (cdr (assq article gnus-newsgroup-bookmarks)))
6587             article)
6588 ;;      (when (or all-headers gnus-show-all-headers)
6589 ;;        (gnus-article-show-all-headers))
6590         'old))))
6591
6592 (defun gnus-summary-force-verify-and-decrypt ()
6593   (interactive)
6594   (let ((mm-verify-option 'known)
6595         (mm-decrypt-option 'known))
6596     (gnus-summary-select-article nil 'force)))
6597
6598 (defun gnus-summary-set-current-mark (&optional current-mark)
6599   "Obsolete function."
6600   nil)
6601
6602 (defun gnus-summary-next-article (&optional unread subject backward push)
6603   "Select the next article.
6604 If UNREAD, only unread articles are selected.
6605 If SUBJECT, only articles with SUBJECT are selected.
6606 If BACKWARD, the previous article is selected instead of the next."
6607   (interactive "P")
6608   (cond
6609    ;; Is there such an article?
6610    ((and (gnus-summary-search-forward unread subject backward)
6611          (or (gnus-summary-display-article (gnus-summary-article-number))
6612              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6613     (gnus-summary-position-point))
6614    ;; If not, we try the first unread, if that is wanted.
6615    ((and subject
6616          gnus-auto-select-same
6617          (gnus-summary-first-unread-article))
6618     (gnus-summary-position-point)
6619     (gnus-message 6 "Wrapped"))
6620    ;; Try to get next/previous article not displayed in this group.
6621    ((and gnus-auto-extend-newsgroup
6622          (not unread) (not subject))
6623     (gnus-summary-goto-article
6624      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6625      nil (count-lines (point-min) (point))))
6626    ;; Go to next/previous group.
6627    (t
6628     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6629       (gnus-summary-jump-to-group gnus-newsgroup-name))
6630     (let ((cmd last-command-char)
6631           (point
6632            (save-excursion
6633              (set-buffer gnus-group-buffer)
6634              (point)))
6635           (group
6636            (if (eq gnus-keep-same-level 'best)
6637                (gnus-summary-best-group gnus-newsgroup-name)
6638              (gnus-summary-search-group backward gnus-keep-same-level))))
6639       ;; For some reason, the group window gets selected.  We change
6640       ;; it back.
6641       (select-window (get-buffer-window (current-buffer)))
6642       ;; Select next unread newsgroup automagically.
6643       (cond
6644        ((or (not gnus-auto-select-next)
6645             (not cmd))
6646         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6647        ((or (eq gnus-auto-select-next 'quietly)
6648             (and (eq gnus-auto-select-next 'slightly-quietly)
6649                  push)
6650             (and (eq gnus-auto-select-next 'almost-quietly)
6651                  (gnus-summary-last-article-p)))
6652         ;; Select quietly.
6653         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6654             (gnus-summary-exit)
6655           (gnus-message 7 "No more%s articles (%s)..."
6656                         (if unread " unread" "")
6657                         (if group (concat "selecting " group)
6658                           "exiting"))
6659           (gnus-summary-next-group nil group backward)))
6660        (t
6661         (when (gnus-key-press-event-p last-input-event)
6662           (gnus-summary-walk-group-buffer
6663            gnus-newsgroup-name cmd unread backward point))))))))
6664
6665 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6666   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6667                       (?\C-p (gnus-group-prev-unread-group 1))))
6668         (cursor-in-echo-area t)
6669         keve key group ended)
6670     (save-excursion
6671       (set-buffer gnus-group-buffer)
6672       (goto-char start)
6673       (setq group
6674             (if (eq gnus-keep-same-level 'best)
6675                 (gnus-summary-best-group gnus-newsgroup-name)
6676               (gnus-summary-search-group backward gnus-keep-same-level))))
6677     (while (not ended)
6678       (gnus-message
6679        5 "No more%s articles%s" (if unread " unread" "")
6680        (if (and group
6681                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6682            (format " (Type %s for %s [%s])"
6683                    (single-key-description cmd) group
6684                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6685          (format " (Type %s to exit %s)"
6686                  (single-key-description cmd)
6687                  gnus-newsgroup-name)))
6688       ;; Confirm auto selection.
6689       (setq key (car (setq keve (gnus-read-event-char))))
6690       (setq ended t)
6691       (cond
6692        ((assq key keystrokes)
6693         (let ((obuf (current-buffer)))
6694           (switch-to-buffer gnus-group-buffer)
6695           (when group
6696             (gnus-group-jump-to-group group))
6697           (eval (cadr (assq key keystrokes)))
6698           (setq group (gnus-group-group-name))
6699           (switch-to-buffer obuf))
6700         (setq ended nil))
6701        ((equal key cmd)
6702         (if (or (not group)
6703                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6704             (gnus-summary-exit)
6705           (gnus-summary-next-group nil group backward)))
6706        (t
6707         (push (cdr keve) unread-command-events))))))
6708
6709 (defun gnus-summary-next-unread-article ()
6710   "Select unread article after current one."
6711   (interactive)
6712   (gnus-summary-next-article
6713    (or (not (eq gnus-summary-goto-unread 'never))
6714        (gnus-summary-last-article-p (gnus-summary-article-number)))
6715    (and gnus-auto-select-same
6716         (gnus-summary-article-subject))))
6717
6718 (defun gnus-summary-prev-article (&optional unread subject)
6719   "Select the article after the current one.
6720 If UNREAD is non-nil, only unread articles are selected."
6721   (interactive "P")
6722   (gnus-summary-next-article unread subject t))
6723
6724 (defun gnus-summary-prev-unread-article ()
6725   "Select unread article before current one."
6726   (interactive)
6727   (gnus-summary-prev-article
6728    (or (not (eq gnus-summary-goto-unread 'never))
6729        (gnus-summary-first-article-p (gnus-summary-article-number)))
6730    (and gnus-auto-select-same
6731         (gnus-summary-article-subject))))
6732
6733 (defun gnus-summary-next-page (&optional lines circular)
6734   "Show next page of the selected article.
6735 If at the end of the current article, select the next article.
6736 LINES says how many lines should be scrolled up.
6737
6738 If CIRCULAR is non-nil, go to the start of the article instead of
6739 selecting the next article when reaching the end of the current
6740 article."
6741   (interactive "P")
6742   (setq gnus-summary-buffer (current-buffer))
6743   (gnus-set-global-variables)
6744   (let ((article (gnus-summary-article-number))
6745         (article-window (get-buffer-window gnus-article-buffer t))
6746         endp)
6747     ;; If the buffer is empty, we have no article.
6748     (unless article
6749       (error "No article to select"))
6750     (gnus-configure-windows 'article)
6751     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6752         (if (and (eq gnus-summary-goto-unread 'never)
6753                  (not (gnus-summary-last-article-p article)))
6754             (gnus-summary-next-article)
6755           (gnus-summary-next-unread-article))
6756       (if (or (null gnus-current-article)
6757               (null gnus-article-current)
6758               (/= article (cdr gnus-article-current))
6759               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6760           ;; Selected subject is different from current article's.
6761           (gnus-summary-display-article article)
6762         (when article-window
6763           (gnus-eval-in-buffer-window gnus-article-buffer
6764             (setq endp (gnus-article-next-page lines)))
6765           (when endp
6766             (cond (circular
6767                    (gnus-summary-beginning-of-article))
6768                   (lines
6769                    (gnus-message 3 "End of message"))
6770                   ((null lines)
6771                    (if (and (eq gnus-summary-goto-unread 'never)
6772                             (not (gnus-summary-last-article-p article)))
6773                        (gnus-summary-next-article)
6774                      (gnus-summary-next-unread-article))))))))
6775     (gnus-summary-recenter)
6776     (gnus-summary-position-point)))
6777
6778 (defun gnus-summary-prev-page (&optional lines move)
6779   "Show previous page of selected article.
6780 Argument LINES specifies lines to be scrolled down.
6781 If MOVE, move to the previous unread article if point is at
6782 the beginning of the buffer."
6783   (interactive "P")
6784   (let ((article (gnus-summary-article-number))
6785         (article-window (get-buffer-window gnus-article-buffer t))
6786         endp)
6787     (gnus-configure-windows 'article)
6788     (if (or (null gnus-current-article)
6789             (null gnus-article-current)
6790             (/= article (cdr gnus-article-current))
6791             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6792         ;; Selected subject is different from current article's.
6793         (gnus-summary-display-article article)
6794       (gnus-summary-recenter)
6795       (when article-window
6796         (gnus-eval-in-buffer-window gnus-article-buffer
6797           (setq endp (gnus-article-prev-page lines)))
6798         (when (and move endp)
6799           (cond (lines
6800                  (gnus-message 3 "Beginning of message"))
6801                 ((null lines)
6802                  (if (and (eq gnus-summary-goto-unread 'never)
6803                           (not (gnus-summary-first-article-p article)))
6804                      (gnus-summary-prev-article)
6805                    (gnus-summary-prev-unread-article))))))))
6806   (gnus-summary-position-point))
6807
6808 (defun gnus-summary-prev-page-or-article (&optional lines)
6809   "Show previous page of selected article.
6810 Argument LINES specifies lines to be scrolled down.
6811 If at the beginning of the article, go to the next article."
6812   (interactive "P")
6813   (gnus-summary-prev-page lines t))
6814
6815 (defun gnus-summary-scroll-up (lines)
6816   "Scroll up (or down) one line current article.
6817 Argument LINES specifies lines to be scrolled up (or down if negative)."
6818   (interactive "p")
6819   (gnus-configure-windows 'article)
6820   (gnus-summary-show-thread)
6821   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6822     (gnus-eval-in-buffer-window gnus-article-buffer
6823       (cond ((> lines 0)
6824              (when (gnus-article-next-page lines)
6825                (gnus-message 3 "End of message")))
6826             ((< lines 0)
6827              (gnus-article-prev-page (- lines))))))
6828   (gnus-summary-recenter)
6829   (gnus-summary-position-point))
6830
6831 (defun gnus-summary-scroll-down (lines)
6832   "Scroll down (or up) one line current article.
6833 Argument LINES specifies lines to be scrolled down (or up if negative)."
6834   (interactive "p")
6835   (gnus-summary-scroll-up (- lines)))
6836
6837 (defun gnus-summary-next-same-subject ()
6838   "Select next article which has the same subject as current one."
6839   (interactive)
6840   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6841
6842 (defun gnus-summary-prev-same-subject ()
6843   "Select previous article which has the same subject as current one."
6844   (interactive)
6845   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6846
6847 (defun gnus-summary-next-unread-same-subject ()
6848   "Select next unread article which has the same subject as current one."
6849   (interactive)
6850   (gnus-summary-next-article t (gnus-summary-article-subject)))
6851
6852 (defun gnus-summary-prev-unread-same-subject ()
6853   "Select previous unread article which has the same subject as current one."
6854   (interactive)
6855   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6856
6857 (defun gnus-summary-first-unread-article ()
6858   "Select the first unread article.
6859 Return nil if there are no unread articles."
6860   (interactive)
6861   (prog1
6862       (when (gnus-summary-first-subject t)
6863         (gnus-summary-show-thread)
6864         (gnus-summary-first-subject t)
6865         (gnus-summary-display-article (gnus-summary-article-number)))
6866     (gnus-summary-position-point)))
6867
6868 (defun gnus-summary-first-unread-subject ()
6869   "Place the point on the subject line of the first unread article.
6870 Return nil if there are no unread articles."
6871   (interactive)
6872   (prog1
6873       (when (gnus-summary-first-subject t)
6874         (gnus-summary-show-thread)
6875         (gnus-summary-first-subject t))
6876     (gnus-summary-position-point)))
6877
6878 (defun gnus-summary-first-unseen-subject ()
6879   "Place the point on the subject line of the first unseen article.
6880 Return nil if there are no unseen articles."
6881   (interactive)
6882   (prog1
6883       (when (gnus-summary-first-subject t t t)
6884         (gnus-summary-show-thread)
6885         (gnus-summary-first-subject t t t))
6886     (gnus-summary-position-point)))
6887
6888 (defun gnus-summary-first-unseen-or-unread-subject ()
6889   "Place the point on the subject line of the first unseen article.
6890 Return nil if there are no unseen articles."
6891   (interactive)
6892   (prog1
6893       (unless (when (gnus-summary-first-subject t t t)
6894                 (gnus-summary-show-thread)
6895                 (gnus-summary-first-subject t t t))
6896         (when (gnus-summary-first-subject t)
6897           (gnus-summary-show-thread)
6898           (gnus-summary-first-subject t)))
6899     (gnus-summary-position-point)))
6900
6901 (defun gnus-summary-first-article ()
6902   "Select the first article.
6903 Return nil if there are no articles."
6904   (interactive)
6905   (prog1
6906       (when (gnus-summary-first-subject)
6907         (gnus-summary-show-thread)
6908         (gnus-summary-first-subject)
6909         (gnus-summary-display-article (gnus-summary-article-number)))
6910     (gnus-summary-position-point)))
6911
6912 (defun gnus-summary-best-unread-article (&optional arg)
6913   "Select the unread article with the highest score.
6914 If given a prefix argument, select the next unread article that has a
6915 score higher than the default score."
6916   (interactive "P")
6917   (let ((article (if arg
6918                      (gnus-summary-better-unread-subject)
6919                    (gnus-summary-best-unread-subject))))
6920     (if article
6921         (gnus-summary-goto-article article)
6922       (error "No unread articles"))))
6923
6924 (defun gnus-summary-best-unread-subject ()
6925   "Select the unread subject with the highest score."
6926   (interactive)
6927   (let ((best -1000000)
6928         (data gnus-newsgroup-data)
6929         article score)
6930     (while data
6931       (and (gnus-data-unread-p (car data))
6932            (> (setq score
6933                     (gnus-summary-article-score (gnus-data-number (car data))))
6934               best)
6935            (setq best score
6936                  article (gnus-data-number (car data))))
6937       (setq data (cdr data)))
6938     (when article
6939       (gnus-summary-goto-subject article))
6940     (gnus-summary-position-point)
6941     article))
6942
6943 (defun gnus-summary-better-unread-subject ()
6944   "Select the first unread subject that has a score over the default score."
6945   (interactive)
6946   (let ((data gnus-newsgroup-data)
6947         article score)
6948     (while (and (setq article (gnus-data-number (car data)))
6949                 (or (gnus-data-read-p (car data))
6950                     (not (> (gnus-summary-article-score article)
6951                             gnus-summary-default-score))))
6952       (setq data (cdr data)))
6953     (when article
6954       (gnus-summary-goto-subject article))
6955     (gnus-summary-position-point)
6956     article))
6957
6958 (defun gnus-summary-last-subject ()
6959   "Go to the last displayed subject line in the group."
6960   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6961     (when article
6962       (gnus-summary-goto-subject article))))
6963
6964 (defun gnus-summary-goto-article (article &optional all-headers force)
6965   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6966 If ALL-HEADERS is non-nil, no header lines are hidden.
6967 If FORCE, go to the article even if it isn't displayed.  If FORCE
6968 is a number, it is the line the article is to be displayed on."
6969   (interactive
6970    (list
6971     (completing-read
6972      "Article number or Message-ID: "
6973      (mapcar (lambda (number) (list (int-to-string number)))
6974              gnus-newsgroup-limit))
6975     current-prefix-arg
6976     t))
6977   (prog1
6978       (if (and (stringp article)
6979                (string-match "@" article))
6980           (gnus-summary-refer-article article)
6981         (when (stringp article)
6982           (setq article (string-to-number article)))
6983         (if (gnus-summary-goto-subject article force)
6984             (gnus-summary-display-article article all-headers)
6985           (gnus-message 4 "Couldn't go to article %s" article) nil))
6986     (gnus-summary-position-point)))
6987
6988 (defun gnus-summary-goto-last-article ()
6989   "Go to the previously read article."
6990   (interactive)
6991   (prog1
6992       (when gnus-last-article
6993         (gnus-summary-goto-article gnus-last-article nil t))
6994     (gnus-summary-position-point)))
6995
6996 (defun gnus-summary-pop-article (number)
6997   "Pop one article off the history and go to the previous.
6998 NUMBER articles will be popped off."
6999   (interactive "p")
7000   (let (to)
7001     (setq gnus-newsgroup-history
7002           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7003     (if to
7004         (gnus-summary-goto-article (car to) nil t)
7005       (error "Article history empty")))
7006   (gnus-summary-position-point))
7007
7008 ;; Summary commands and functions for limiting the summary buffer.
7009
7010 (defun gnus-summary-limit-to-articles (n)
7011   "Limit the summary buffer to the next N articles.
7012 If not given a prefix, use the process marked articles instead."
7013   (interactive "P")
7014   (prog1
7015       (let ((articles (gnus-summary-work-articles n)))
7016         (setq gnus-newsgroup-processable nil)
7017         (gnus-summary-limit articles))
7018     (gnus-summary-position-point)))
7019
7020 (defun gnus-summary-pop-limit (&optional total)
7021   "Restore the previous limit.
7022 If given a prefix, remove all limits."
7023   (interactive "P")
7024   (when total
7025     (setq gnus-newsgroup-limits
7026           (list (mapcar (lambda (h) (mail-header-number h))
7027                         gnus-newsgroup-headers))))
7028   (unless gnus-newsgroup-limits
7029     (error "No limit to pop"))
7030   (prog1
7031       (gnus-summary-limit nil 'pop)
7032     (gnus-summary-position-point)))
7033
7034 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7035   "Limit the summary buffer to articles that have subjects that match a regexp.
7036 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7037   (interactive
7038    (list (read-string (if current-prefix-arg
7039                           "Exclude subject (regexp): "
7040                         "Limit to subject (regexp): "))
7041          nil current-prefix-arg))
7042   (unless header
7043     (setq header "subject"))
7044   (when (not (equal "" subject))
7045     (prog1
7046         (let ((articles (gnus-summary-find-matching
7047                          (or header "subject") subject 'all nil nil
7048                          not-matching)))
7049           (unless articles
7050             (error "Found no matches for \"%s\"" subject))
7051           (gnus-summary-limit articles))
7052       (gnus-summary-position-point))))
7053
7054 (defun gnus-summary-limit-to-author (from &optional not-matching)
7055   "Limit the summary buffer to articles that have authors that match a regexp.
7056 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7057   (interactive
7058    (list (read-string (if current-prefix-arg
7059                           "Exclude author (regexp): "
7060                         "Limit to author (regexp): "))
7061          current-prefix-arg))
7062   (gnus-summary-limit-to-subject from "from" not-matching))
7063
7064 (defun gnus-summary-limit-to-age (age &optional younger-p)
7065   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7066 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7067 articles that are younger than AGE days."
7068   (interactive
7069    (let ((younger current-prefix-arg)
7070          (days-got nil)
7071          days)
7072      (while (not days-got)
7073        (setq days (if younger
7074                       (read-string "Limit to articles within (in days): ")
7075                     (read-string "Limit to articles older than (in days): ")))
7076        (when (> (length days) 0)
7077          (setq days (read days)))
7078        (if (numberp days)
7079            (progn 
7080              (setq days-got t)
7081              (if (< days 0)
7082                  (progn 
7083                    (setq younger (not younger))
7084                    (setq days (* days -1)))))
7085          (message "Please enter a number.")
7086          (sleep-for 1)))
7087      (list days younger)))
7088   (prog1
7089       (let ((data gnus-newsgroup-data)
7090             (cutoff (days-to-time age))
7091             articles d date is-younger)
7092         (while (setq d (pop data))
7093           (when (and (vectorp (gnus-data-header d))
7094                      (setq date (mail-header-date (gnus-data-header d))))
7095             (setq is-younger (time-less-p
7096                               (time-since (condition-case ()
7097                                               (date-to-time date)
7098                                             (error '(0 0))))
7099                               cutoff))
7100             (when (if younger-p
7101                       is-younger
7102                     (not is-younger))
7103               (push (gnus-data-number d) articles))))
7104         (gnus-summary-limit (nreverse articles)))
7105     (gnus-summary-position-point)))
7106
7107 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7108   "Limit the summary buffer to articles that match an 'extra' header."
7109   (interactive
7110    (let ((header
7111           (intern
7112            (gnus-completing-read-with-default
7113             (symbol-name (car gnus-extra-headers))
7114             (if current-prefix-arg
7115                 "Exclude extra header:"
7116               "Limit extra header:")
7117             (mapcar (lambda (x)
7118                       (cons (symbol-name x) x))
7119                     gnus-extra-headers)
7120             nil
7121             t))))
7122      (list header
7123            (read-string (format "%s header %s (regexp): "
7124                                 (if current-prefix-arg "Exclude" "Limit to")
7125                                 header))
7126            current-prefix-arg)))
7127   (when (not (equal "" regexp))
7128     (prog1
7129         (let ((articles (gnus-summary-find-matching
7130                          (cons 'extra header) regexp 'all nil nil
7131                          not-matching)))
7132           (unless articles
7133             (error "Found no matches for \"%s\"" regexp))
7134           (gnus-summary-limit articles))
7135       (gnus-summary-position-point))))
7136
7137 (defun gnus-summary-limit-to-display-predicate ()
7138   "Limit the summary buffer to the predicated in the `display' group parameter."
7139   (interactive)
7140   (unless gnus-newsgroup-display
7141     (error "There is no `display' group parameter"))
7142   (let (articles)
7143     (dolist (number gnus-newsgroup-articles)
7144       (when (funcall gnus-newsgroup-display)
7145         (push number articles)))
7146     (gnus-summary-limit articles))
7147   (gnus-summary-position-point))
7148
7149 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7150 (make-obsolete
7151  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7152
7153 (defun gnus-summary-limit-to-unread (&optional all)
7154   "Limit the summary buffer to articles that are not marked as read.
7155 If ALL is non-nil, limit strictly to unread articles."
7156   (interactive "P")
7157   (if all
7158       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7159     (gnus-summary-limit-to-marks
7160      ;; Concat all the marks that say that an article is read and have
7161      ;; those removed.
7162      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7163            gnus-killed-mark gnus-kill-file-mark
7164            gnus-low-score-mark gnus-expirable-mark
7165            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7166            gnus-duplicate-mark gnus-souped-mark)
7167      'reverse)))
7168
7169 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7170 (make-obsolete 'gnus-summary-delete-marked-with
7171                'gnus-summary-limit-exlude-marks)
7172
7173 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7174   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7175 If REVERSE, limit the summary buffer to articles that are marked
7176 with MARKS.  MARKS can either be a string of marks or a list of marks.
7177 Returns how many articles were removed."
7178   (interactive "sMarks: ")
7179   (gnus-summary-limit-to-marks marks t))
7180
7181 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7182   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7183 If REVERSE (the prefix), limit the summary buffer to articles that are
7184 not marked with MARKS.  MARKS can either be a string of marks or a
7185 list of marks.
7186 Returns how many articles were removed."
7187   (interactive "sMarks: \nP")
7188   (prog1
7189       (let ((data gnus-newsgroup-data)
7190             (marks (if (listp marks) marks
7191                      (append marks nil))) ; Transform to list.
7192             articles)
7193         (while data
7194           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7195                   (memq (gnus-data-mark (car data)) marks))
7196             (push (gnus-data-number (car data)) articles))
7197           (setq data (cdr data)))
7198         (gnus-summary-limit articles))
7199     (gnus-summary-position-point)))
7200
7201 (defun gnus-summary-limit-to-score (score)
7202   "Limit to articles with score at or above SCORE."
7203   (interactive "NLimit to articles with score of at least: ")
7204   (let ((data gnus-newsgroup-data)
7205         articles)
7206     (while data
7207       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7208                 score)
7209         (push (gnus-data-number (car data)) articles))
7210       (setq data (cdr data)))
7211     (prog1
7212         (gnus-summary-limit articles)
7213       (gnus-summary-position-point))))
7214
7215 (defun gnus-summary-limit-include-thread (id)
7216   "Display all the hidden articles that is in the thread with ID in it.
7217 When called interactively, ID is the Message-ID of the current
7218 article."
7219   (interactive (list (mail-header-id (gnus-summary-article-header))))
7220   (let ((articles (gnus-articles-in-thread
7221                    (gnus-id-to-thread (gnus-root-id id)))))
7222     (prog1
7223         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7224       (gnus-summary-limit-include-matching-articles
7225        "subject"
7226        (regexp-quote (gnus-simplify-subject-re
7227                       (mail-header-subject (gnus-id-to-header id)))))
7228       (gnus-summary-position-point))))
7229
7230 (defun gnus-summary-limit-include-matching-articles (header regexp)
7231   "Display all the hidden articles that have HEADERs that match REGEXP."
7232   (interactive (list (read-string "Match on header: ")
7233                      (read-string "Regexp: ")))
7234   (let ((articles (gnus-find-matching-articles header regexp)))
7235     (prog1
7236         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7237       (gnus-summary-position-point))))
7238
7239 (defun gnus-summary-limit-include-dormant ()
7240   "Display all the hidden articles that are marked as dormant.
7241 Note that this command only works on a subset of the articles currently
7242 fetched for this group."
7243   (interactive)
7244   (unless gnus-newsgroup-dormant
7245     (error "There are no dormant articles in this group"))
7246   (prog1
7247       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7248     (gnus-summary-position-point)))
7249
7250 (defun gnus-summary-limit-exclude-dormant ()
7251   "Hide all dormant articles."
7252   (interactive)
7253   (prog1
7254       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7255     (gnus-summary-position-point)))
7256
7257 (defun gnus-summary-limit-exclude-childless-dormant ()
7258   "Hide all dormant articles that have no children."
7259   (interactive)
7260   (let ((data (gnus-data-list t))
7261         articles d children)
7262     ;; Find all articles that are either not dormant or have
7263     ;; children.
7264     (while (setq d (pop data))
7265       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7266                 (and (setq children
7267                            (gnus-article-children (gnus-data-number d)))
7268                      (let (found)
7269                        (while children
7270                          (when (memq (car children) articles)
7271                            (setq children nil
7272                                  found t))
7273                          (pop children))
7274                        found)))
7275         (push (gnus-data-number d) articles)))
7276     ;; Do the limiting.
7277     (prog1
7278         (gnus-summary-limit articles)
7279       (gnus-summary-position-point))))
7280
7281 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7282   "Mark all unread excluded articles as read.
7283 If ALL, mark even excluded ticked and dormants as read."
7284   (interactive "P")
7285   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7286   (let ((articles (gnus-sorted-ndifference
7287                    (sort
7288                     (mapcar (lambda (h) (mail-header-number h))
7289                             gnus-newsgroup-headers)
7290                     '<)
7291                    gnus-newsgroup-limit))
7292         article)
7293     (setq gnus-newsgroup-unreads
7294           (gnus-sorted-intersection gnus-newsgroup-unreads
7295                                     gnus-newsgroup-limit))
7296     (if all
7297         (setq gnus-newsgroup-dormant nil
7298               gnus-newsgroup-marked nil
7299               gnus-newsgroup-reads
7300               (nconc
7301                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7302                gnus-newsgroup-reads))
7303       (while (setq article (pop articles))
7304         (unless (or (memq article gnus-newsgroup-dormant)
7305                     (memq article gnus-newsgroup-marked))
7306           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7307
7308 (defun gnus-summary-limit (articles &optional pop)
7309   (if pop
7310       ;; We pop the previous limit off the stack and use that.
7311       (setq articles (car gnus-newsgroup-limits)
7312             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7313     ;; We use the new limit, so we push the old limit on the stack.
7314     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7315   ;; Set the limit.
7316   (setq gnus-newsgroup-limit articles)
7317   (let ((total (length gnus-newsgroup-data))
7318         (data (gnus-data-find-list (gnus-summary-article-number)))
7319         (gnus-summary-mark-below nil)   ; Inhibit this.
7320         found)
7321     ;; This will do all the work of generating the new summary buffer
7322     ;; according to the new limit.
7323     (gnus-summary-prepare)
7324     ;; Hide any threads, possibly.
7325     (gnus-summary-maybe-hide-threads)
7326     ;; Try to return to the article you were at, or one in the
7327     ;; neighborhood.
7328     (when data
7329       ;; We try to find some article after the current one.
7330       (while data
7331         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7332           (setq data nil
7333                 found t))
7334         (setq data (cdr data))))
7335     (unless found
7336       ;; If there is no data, that means that we were after the last
7337       ;; article.  The same goes when we can't find any articles
7338       ;; after the current one.
7339       (goto-char (point-max))
7340       (gnus-summary-find-prev))
7341     (gnus-set-mode-line 'summary)
7342     ;; We return how many articles were removed from the summary
7343     ;; buffer as a result of the new limit.
7344     (- total (length gnus-newsgroup-data))))
7345
7346 (defsubst gnus-invisible-cut-children (threads)
7347   (let ((num 0))
7348     (while threads
7349       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7350         (incf num))
7351       (pop threads))
7352     (< num 2)))
7353
7354 (defsubst gnus-cut-thread (thread)
7355   "Go forwards in the thread until we find an article that we want to display."
7356   (when (or (eq gnus-fetch-old-headers 'some)
7357             (eq gnus-fetch-old-headers 'invisible)
7358             (numberp gnus-fetch-old-headers)
7359             (eq gnus-build-sparse-threads 'some)
7360             (eq gnus-build-sparse-threads 'more))
7361     ;; Deal with old-fetched headers and sparse threads.
7362     (while (and
7363             thread
7364             (or
7365              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7366              (gnus-summary-article-ancient-p
7367               (mail-header-number (car thread))))
7368             (if (or (<= (length (cdr thread)) 1)
7369                     (eq gnus-fetch-old-headers 'invisible))
7370                 (setq gnus-newsgroup-limit
7371                       (delq (mail-header-number (car thread))
7372                             gnus-newsgroup-limit)
7373                       thread (cadr thread))
7374               (when (gnus-invisible-cut-children (cdr thread))
7375                 (let ((th (cdr thread)))
7376                   (while th
7377                     (if (memq (mail-header-number (caar th))
7378                               gnus-newsgroup-limit)
7379                         (setq thread (car th)
7380                               th nil)
7381                       (setq th (cdr th))))))))))
7382   thread)
7383
7384 (defun gnus-cut-threads (threads)
7385   "Cut off all uninteresting articles from the beginning of threads."
7386   (when (or (eq gnus-fetch-old-headers 'some)
7387             (eq gnus-fetch-old-headers 'invisible)
7388             (numberp gnus-fetch-old-headers)
7389             (eq gnus-build-sparse-threads 'some)
7390             (eq gnus-build-sparse-threads 'more))
7391     (let ((th threads))
7392       (while th
7393         (setcar th (gnus-cut-thread (car th)))
7394         (setq th (cdr th)))))
7395   ;; Remove nixed out threads.
7396   (delq nil threads))
7397
7398 (defun gnus-summary-initial-limit (&optional show-if-empty)
7399   "Figure out what the initial limit is supposed to be on group entry.
7400 This entails weeding out unwanted dormants, low-scored articles,
7401 fetch-old-headers verbiage, and so on."
7402   ;; Most groups have nothing to remove.
7403   (if (or gnus-inhibit-limiting
7404           (and (null gnus-newsgroup-dormant)
7405                (eq gnus-newsgroup-display 'gnus-not-ignore)
7406                (not (eq gnus-fetch-old-headers 'some))
7407                (not (numberp gnus-fetch-old-headers))
7408                (not (eq gnus-fetch-old-headers 'invisible))
7409                (null gnus-summary-expunge-below)
7410                (not (eq gnus-build-sparse-threads 'some))
7411                (not (eq gnus-build-sparse-threads 'more))
7412                (null gnus-thread-expunge-below)
7413                (not gnus-use-nocem)))
7414       ()                                ; Do nothing.
7415     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7416     (setq gnus-newsgroup-limit nil)
7417     (mapatoms
7418      (lambda (node)
7419        (unless (car (symbol-value node))
7420          ;; These threads have no parents -- they are roots.
7421          (let ((nodes (cdr (symbol-value node)))
7422                thread)
7423            (while nodes
7424              (if (and gnus-thread-expunge-below
7425                       (< (gnus-thread-total-score (car nodes))
7426                          gnus-thread-expunge-below))
7427                  (gnus-expunge-thread (pop nodes))
7428                (setq thread (pop nodes))
7429                (gnus-summary-limit-children thread))))))
7430      gnus-newsgroup-dependencies)
7431     ;; If this limitation resulted in an empty group, we might
7432     ;; pop the previous limit and use it instead.
7433     (when (and (not gnus-newsgroup-limit)
7434                show-if-empty)
7435       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7436     gnus-newsgroup-limit))
7437
7438 (defun gnus-summary-limit-children (thread)
7439   "Return 1 if this subthread is visible and 0 if it is not."
7440   ;; First we get the number of visible children to this thread.  This
7441   ;; is done by recursing down the thread using this function, so this
7442   ;; will really go down to a leaf article first, before slowly
7443   ;; working its way up towards the root.
7444   (when thread
7445     (let ((children
7446            (if (cdr thread)
7447                (apply '+ (mapcar 'gnus-summary-limit-children
7448                                  (cdr thread)))
7449              0))
7450           (number (mail-header-number (car thread)))
7451           score)
7452       (if (and
7453            (not (memq number gnus-newsgroup-marked))
7454            (or
7455             ;; If this article is dormant and has absolutely no visible
7456             ;; children, then this article isn't visible.
7457             (and (memq number gnus-newsgroup-dormant)
7458                  (zerop children))
7459             ;; If this is "fetch-old-headered" and there is no
7460             ;; visible children, then we don't want this article.
7461             (and (or (eq gnus-fetch-old-headers 'some)
7462                      (numberp gnus-fetch-old-headers))
7463                  (gnus-summary-article-ancient-p number)
7464                  (zerop children))
7465             ;; If this is "fetch-old-headered" and `invisible', then
7466             ;; we don't want this article.
7467             (and (eq gnus-fetch-old-headers 'invisible)
7468                  (gnus-summary-article-ancient-p number))
7469             ;; If this is a sparsely inserted article with no children,
7470             ;; we don't want it.
7471             (and (eq gnus-build-sparse-threads 'some)
7472                  (gnus-summary-article-sparse-p number)
7473                  (zerop children))
7474             ;; If we use expunging, and this article is really
7475             ;; low-scored, then we don't want this article.
7476             (when (and gnus-summary-expunge-below
7477                        (< (setq score
7478                                 (or (cdr (assq number gnus-newsgroup-scored))
7479                                     gnus-summary-default-score))
7480                           gnus-summary-expunge-below))
7481               ;; We increase the expunge-tally here, but that has
7482               ;; nothing to do with the limits, really.
7483               (incf gnus-newsgroup-expunged-tally)
7484               ;; We also mark as read here, if that's wanted.
7485               (when (and gnus-summary-mark-below
7486                          (< score gnus-summary-mark-below))
7487                 (setq gnus-newsgroup-unreads
7488                       (delq number gnus-newsgroup-unreads))
7489                 (if gnus-newsgroup-auto-expire
7490                     (push number gnus-newsgroup-expirable)
7491                   (push (cons number gnus-low-score-mark)
7492                         gnus-newsgroup-reads)))
7493               t)
7494             ;; Do the `display' group parameter.
7495             (and gnus-newsgroup-display
7496                  (not (funcall gnus-newsgroup-display)))
7497             ;; Check NoCeM things.
7498             (if (and gnus-use-nocem
7499                      (gnus-nocem-unwanted-article-p
7500                       (mail-header-id (car thread))))
7501                 (progn
7502                   (setq gnus-newsgroup-unreads
7503                         (delq number gnus-newsgroup-unreads))
7504                   t))))
7505           ;; Nope, invisible article.
7506           0
7507         ;; Ok, this article is to be visible, so we add it to the limit
7508         ;; and return 1.
7509         (push number gnus-newsgroup-limit)
7510         1))))
7511
7512 (defun gnus-expunge-thread (thread)
7513   "Mark all articles in THREAD as read."
7514   (let* ((number (mail-header-number (car thread))))
7515     (incf gnus-newsgroup-expunged-tally)
7516     ;; We also mark as read here, if that's wanted.
7517     (setq gnus-newsgroup-unreads
7518           (delq number gnus-newsgroup-unreads))
7519     (if gnus-newsgroup-auto-expire
7520         (push number gnus-newsgroup-expirable)
7521       (push (cons number gnus-low-score-mark)
7522             gnus-newsgroup-reads)))
7523   ;; Go recursively through all subthreads.
7524   (mapcar 'gnus-expunge-thread (cdr thread)))
7525
7526 ;; Summary article oriented commands
7527
7528 (defun gnus-summary-refer-parent-article (n)
7529   "Refer parent article N times.
7530 If N is negative, go to ancestor -N instead.
7531 The difference between N and the number of articles fetched is returned."
7532   (interactive "p")
7533   (let ((skip 1)
7534         error header ref)
7535     (when (not (natnump n))
7536       (setq skip (abs n)
7537             n 1))
7538     (while (and (> n 0)
7539                 (not error))
7540       (setq header (gnus-summary-article-header))
7541       (if (and (eq (mail-header-number header)
7542                    (cdr gnus-article-current))
7543                (equal gnus-newsgroup-name
7544                       (car gnus-article-current)))
7545           ;; If we try to find the parent of the currently
7546           ;; displayed article, then we take a look at the actual
7547           ;; References header, since this is slightly more
7548           ;; reliable than the References field we got from the
7549           ;; server.
7550           (save-excursion
7551             (set-buffer gnus-original-article-buffer)
7552             (nnheader-narrow-to-headers)
7553             (unless (setq ref (message-fetch-field "references"))
7554               (setq ref (message-fetch-field "in-reply-to")))
7555             (widen))
7556         (setq ref
7557               ;; It's not the current article, so we take a bet on
7558               ;; the value we got from the server.
7559               (mail-header-references header)))
7560       (if (and ref
7561                (not (equal ref "")))
7562           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7563             (gnus-message 1 "Couldn't find parent"))
7564         (gnus-message 1 "No references in article %d"
7565                       (gnus-summary-article-number))
7566         (setq error t))
7567       (decf n))
7568     (gnus-summary-position-point)
7569     n))
7570
7571 (defun gnus-summary-refer-references ()
7572   "Fetch all articles mentioned in the References header.
7573 Return the number of articles fetched."
7574   (interactive)
7575   (let ((ref (mail-header-references (gnus-summary-article-header)))
7576         (current (gnus-summary-article-number))
7577         (n 0))
7578     (if (or (not ref)
7579             (equal ref ""))
7580         (error "No References in the current article")
7581       ;; For each Message-ID in the References header...
7582       (while (string-match "<[^>]*>" ref)
7583         (incf n)
7584         ;; ... fetch that article.
7585         (gnus-summary-refer-article
7586          (prog1 (match-string 0 ref)
7587            (setq ref (substring ref (match-end 0))))))
7588       (gnus-summary-goto-subject current)
7589       (gnus-summary-position-point)
7590       n)))
7591
7592 (defun gnus-summary-refer-thread (&optional limit)
7593   "Fetch all articles in the current thread.
7594 If LIMIT (the numerical prefix), fetch that many old headers instead
7595 of what's specified by the `gnus-refer-thread-limit' variable."
7596   (interactive "P")
7597   (let ((id (mail-header-id (gnus-summary-article-header)))
7598         (limit (if limit (prefix-numeric-value limit)
7599                  gnus-refer-thread-limit)))
7600     ;; We want to fetch LIMIT *old* headers, but we also have to
7601     ;; re-fetch all the headers in the current buffer, because many of
7602     ;; them may be undisplayed.  So we adjust LIMIT.
7603     (when (numberp limit)
7604       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7605     (unless (eq gnus-fetch-old-headers 'invisible)
7606       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7607       ;; Retrieve the headers and read them in.
7608       (if (eq (gnus-retrieve-headers
7609                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7610               'nov)
7611           (gnus-build-all-threads)
7612         (error "Can't fetch thread from backends that don't support NOV"))
7613       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7614     (gnus-summary-limit-include-thread id)))
7615
7616 (defun gnus-summary-refer-article (message-id)
7617   "Fetch an article specified by MESSAGE-ID."
7618   (interactive "sMessage-ID: ")
7619   (when (and (stringp message-id)
7620              (not (zerop (length message-id))))
7621     ;; Construct the correct Message-ID if necessary.
7622     ;; Suggested by tale@pawl.rpi.edu.
7623     (unless (string-match "^<" message-id)
7624       (setq message-id (concat "<" message-id)))
7625     (unless (string-match ">$" message-id)
7626       (setq message-id (concat message-id ">")))
7627     (let* ((header (gnus-id-to-header message-id))
7628            (sparse (and header
7629                         (gnus-summary-article-sparse-p
7630                          (mail-header-number header))
7631                         (memq (mail-header-number header)
7632                               gnus-newsgroup-limit)))
7633            number)
7634       (cond
7635        ;; If the article is present in the buffer we just go to it.
7636        ((and header
7637              (or (not (gnus-summary-article-sparse-p
7638                        (mail-header-number header)))
7639                  sparse))
7640         (prog1
7641             (gnus-summary-goto-article
7642              (mail-header-number header) nil t)
7643           (when sparse
7644             (gnus-summary-update-article (mail-header-number header)))))
7645        (t
7646         ;; We fetch the article.
7647         (catch 'found
7648           (dolist (gnus-override-method (gnus-refer-article-methods))
7649             (gnus-check-server gnus-override-method)
7650             ;; Fetch the header, and display the article.
7651             (when (setq number (gnus-summary-insert-subject message-id))
7652               (gnus-summary-select-article nil nil nil number)
7653               (throw 'found t)))
7654           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7655
7656 (defun gnus-refer-article-methods ()
7657   "Return a list of referrable methods."
7658   (cond
7659    ;; No method, so we default to current and native.
7660    ((null gnus-refer-article-method)
7661     (list gnus-current-select-method gnus-select-method))
7662    ;; Current.
7663    ((eq 'current gnus-refer-article-method)
7664     (list gnus-current-select-method))
7665    ;; List of select methods.
7666    ((not (and (symbolp (car gnus-refer-article-method))
7667               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7668     (let (out)
7669       (dolist (method gnus-refer-article-method)
7670         (push (if (eq 'current method)
7671                   gnus-current-select-method
7672                 method)
7673               out))
7674       (nreverse out)))
7675    ;; One single select method.
7676    (t
7677     (list gnus-refer-article-method))))
7678
7679 (defun gnus-summary-edit-parameters ()
7680   "Edit the group parameters of the current group."
7681   (interactive)
7682   (gnus-group-edit-group gnus-newsgroup-name 'params))
7683
7684 (defun gnus-summary-customize-parameters ()
7685   "Customize the group parameters of the current group."
7686   (interactive)
7687   (gnus-group-customize gnus-newsgroup-name))
7688
7689 (defun gnus-summary-enter-digest-group (&optional force)
7690   "Enter an nndoc group based on the current article.
7691 If FORCE, force a digest interpretation.  If not, try
7692 to guess what the document format is."
7693   (interactive "P")
7694   (let ((conf gnus-current-window-configuration))
7695     (save-excursion
7696       (gnus-summary-select-article))
7697     (setq gnus-current-window-configuration conf)
7698     (let* ((name (format "%s-%d"
7699                          (gnus-group-prefixed-name
7700                           gnus-newsgroup-name (list 'nndoc ""))
7701                          (save-excursion
7702                            (set-buffer gnus-summary-buffer)
7703                            gnus-current-article)))
7704            (ogroup gnus-newsgroup-name)
7705            (params (append (gnus-info-params (gnus-get-info ogroup))
7706                            (list (cons 'to-group ogroup))
7707                            (list (cons 'save-article-group ogroup))))
7708            (case-fold-search t)
7709            (buf (current-buffer))
7710            dig to-address)
7711       (save-excursion
7712         (set-buffer gnus-original-article-buffer)
7713         ;; Have the digest group inherit the main mail address of
7714         ;; the parent article.
7715         (when (setq to-address (or (message-fetch-field "reply-to")
7716                                    (message-fetch-field "from")))
7717           (setq params (append
7718                         (list (cons 'to-address
7719                                     (funcall gnus-decode-encoded-word-function
7720                                              to-address))))))
7721         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7722         (insert-buffer-substring gnus-original-article-buffer)
7723         ;; Remove lines that may lead nndoc to misinterpret the
7724         ;; document type.
7725         (narrow-to-region
7726          (goto-char (point-min))
7727          (or (search-forward "\n\n" nil t) (point)))
7728         (goto-char (point-min))
7729         (delete-matching-lines "^Path:\\|^From ")
7730         (widen))
7731       (unwind-protect
7732           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7733                     (gnus-newsgroup-ephemeral-ignored-charsets
7734                      gnus-newsgroup-ignored-charsets))
7735                 (gnus-group-read-ephemeral-group
7736                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7737                               (nndoc-article-type
7738                                ,(if force 'mbox 'guess)))
7739                  t nil nil nil
7740                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
7741                                                         "ADAPT")))))
7742               ;; Make all postings to this group go to the parent group.
7743               (nconc (gnus-info-params (gnus-get-info name))
7744                      params)
7745             ;; Couldn't select this doc group.
7746             (switch-to-buffer buf)
7747             (gnus-set-global-variables)
7748             (gnus-configure-windows 'summary)
7749             (gnus-message 3 "Article couldn't be entered?"))
7750         (kill-buffer dig)))))
7751
7752 (defun gnus-summary-read-document (n)
7753   "Open a new group based on the current article(s).
7754 This will allow you to read digests and other similar
7755 documents as newsgroups.
7756 Obeys the standard process/prefix convention."
7757   (interactive "P")
7758   (let* ((articles (gnus-summary-work-articles n))
7759          (ogroup gnus-newsgroup-name)
7760          (params (append (gnus-info-params (gnus-get-info ogroup))
7761                          (list (cons 'to-group ogroup))))
7762          article group egroup groups vgroup)
7763     (while (setq article (pop articles))
7764       (setq group (format "%s-%d" gnus-newsgroup-name article))
7765       (gnus-summary-remove-process-mark article)
7766       (when (gnus-summary-display-article article)
7767         (save-excursion
7768           (with-temp-buffer
7769             (insert-buffer-substring gnus-original-article-buffer)
7770             ;; Remove some headers that may lead nndoc to make
7771             ;; the wrong guess.
7772             (message-narrow-to-head)
7773             (goto-char (point-min))
7774             (delete-matching-lines "^\\(Path\\):\\|^From ")
7775             (widen)
7776             (if (setq egroup
7777                       (gnus-group-read-ephemeral-group
7778                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7779                                      (nndoc-article-type guess))
7780                        t nil t))
7781                 (progn
7782             ;; Make all postings to this group go to the parent group.
7783                   (nconc (gnus-info-params (gnus-get-info egroup))
7784                          params)
7785                   (push egroup groups))
7786               ;; Couldn't select this doc group.
7787               (gnus-error 3 "Article couldn't be entered"))))))
7788     ;; Now we have selected all the documents.
7789     (cond
7790      ((not groups)
7791       (error "None of the articles could be interpreted as documents"))
7792      ((gnus-group-read-ephemeral-group
7793        (setq vgroup (format
7794                      "nnvirtual:%s-%s" gnus-newsgroup-name
7795                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7796        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7797        t
7798        (cons (current-buffer) 'summary)))
7799      (t
7800       (error "Couldn't select virtual nndoc group")))))
7801
7802 (defun gnus-summary-isearch-article (&optional regexp-p)
7803   "Do incremental search forward on the current article.
7804 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7805   (interactive "P")
7806   (gnus-summary-select-article)
7807   (gnus-configure-windows 'article)
7808   (gnus-eval-in-buffer-window gnus-article-buffer
7809     (save-restriction
7810       (widen)
7811       (isearch-forward regexp-p))))
7812
7813 (defun gnus-summary-search-article-forward (regexp &optional backward)
7814   "Search for an article containing REGEXP forward.
7815 If BACKWARD, search backward instead."
7816   (interactive
7817    (list (read-string
7818           (format "Search article %s (regexp%s): "
7819                   (if current-prefix-arg "backward" "forward")
7820                   (if gnus-last-search-regexp
7821                       (concat ", default " gnus-last-search-regexp)
7822                     "")))
7823          current-prefix-arg))
7824   (if (string-equal regexp "")
7825       (setq regexp (or gnus-last-search-regexp ""))
7826     (setq gnus-last-search-regexp regexp)
7827     (setq gnus-article-before-search gnus-current-article))
7828   ;; Intentionally set gnus-last-article.
7829   (setq gnus-last-article gnus-article-before-search)
7830   (let ((gnus-last-article gnus-last-article))
7831     (if (gnus-summary-search-article regexp backward)
7832         (gnus-summary-show-thread)
7833       (error "Search failed: \"%s\"" regexp))))
7834
7835 (defun gnus-summary-search-article-backward (regexp)
7836   "Search for an article containing REGEXP backward."
7837   (interactive
7838    (list (read-string
7839           (format "Search article backward (regexp%s): "
7840                   (if gnus-last-search-regexp
7841                       (concat ", default " gnus-last-search-regexp)
7842                     "")))))
7843   (gnus-summary-search-article-forward regexp 'backward))
7844
7845 (defun gnus-summary-search-article (regexp &optional backward)
7846   "Search for an article containing REGEXP.
7847 Optional argument BACKWARD means do search for backward.
7848 `gnus-select-article-hook' is not called during the search."
7849   ;; We have to require this here to make sure that the following
7850   ;; dynamic binding isn't shadowed by autoloading.
7851   (require 'gnus-async)
7852   (require 'gnus-art)
7853   (let ((gnus-select-article-hook nil)  ;Disable hook.
7854         (gnus-article-prepare-hook nil)
7855         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7856         (gnus-use-article-prefetch nil)
7857         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7858         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7859         (sum (current-buffer))
7860         (gnus-display-mime-function nil)
7861         (found nil)
7862         point)
7863     (gnus-save-hidden-threads
7864       (gnus-summary-select-article)
7865       (set-buffer gnus-article-buffer)
7866       (goto-char (window-point (get-buffer-window (current-buffer))))
7867       (when backward
7868         (forward-line -1))
7869       (while (not found)
7870         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7871         (if (if backward
7872                 (re-search-backward regexp nil t)
7873               (re-search-forward regexp nil t))
7874             ;; We found the regexp.
7875             (progn
7876               (setq found 'found)
7877               (beginning-of-line)
7878               (set-window-start
7879                (get-buffer-window (current-buffer))
7880                (point))
7881               (forward-line 1)
7882               (set-window-point
7883                (get-buffer-window (current-buffer))
7884                (point))
7885               (set-buffer sum)
7886               (setq point (point)))
7887           ;; We didn't find it, so we go to the next article.
7888           (set-buffer sum)
7889           (setq found 'not)
7890           (while (eq found 'not)
7891             (if (not (if backward (gnus-summary-find-prev)
7892                        (gnus-summary-find-next)))
7893                 ;; No more articles.
7894                 (setq found t)
7895               ;; Select the next article and adjust point.
7896               (unless (gnus-summary-article-sparse-p
7897                        (gnus-summary-article-number))
7898                 (setq found nil)
7899                 (gnus-summary-select-article)
7900                 (set-buffer gnus-article-buffer)
7901                 (widen)
7902                 (goto-char (if backward (point-max) (point-min))))))))
7903       (gnus-message 7 ""))
7904     ;; Return whether we found the regexp.
7905     (when (eq found 'found)
7906       (goto-char point)
7907       (gnus-summary-show-thread)
7908       (gnus-summary-goto-subject gnus-current-article)
7909       (gnus-summary-position-point)
7910       t)))
7911
7912 (defun gnus-find-matching-articles (header regexp)
7913   "Return a list of all articles that match REGEXP on HEADER.
7914 This search includes all articles in the current group that Gnus has
7915 fetched headers for, whether they are displayed or not."
7916   (let ((articles nil)
7917         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7918         (case-fold-search t))
7919     (dolist (header gnus-newsgroup-headers)
7920       (when (string-match regexp (funcall func header))
7921         (push (mail-header-number header) articles)))
7922     (nreverse articles)))
7923
7924 (defun gnus-summary-find-matching (header regexp &optional backward unread
7925                                           not-case-fold not-matching)
7926   "Return a list of all articles that match REGEXP on HEADER.
7927 The search stars on the current article and goes forwards unless
7928 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7929 If UNREAD is non-nil, only unread articles will
7930 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7931 in the comparisons. If NOT-MATCHING, return a list of all articles that
7932 not match REGEXP on HEADER."
7933   (let ((case-fold-search (not not-case-fold))
7934         articles d func)
7935     (if (consp header)
7936         (if (eq (car header) 'extra)
7937             (setq func
7938                   `(lambda (h)
7939                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7940                          "")))
7941           (error "%s is an invalid header" header))
7942       (unless (fboundp (intern (concat "mail-header-" header)))
7943         (error "%s is not a valid header" header))
7944       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7945     (dolist (d (if (eq backward 'all)
7946                    gnus-newsgroup-data
7947                  (gnus-data-find-list
7948                   (gnus-summary-article-number)
7949                   (gnus-data-list backward))))
7950       (when (and (or (not unread)       ; We want all articles...
7951                      (gnus-data-unread-p d)) ; Or just unreads.
7952                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7953                  (if not-matching
7954                      (not (string-match
7955                            regexp
7956                            (funcall func (gnus-data-header d))))
7957                    (string-match regexp
7958                                  (funcall func (gnus-data-header d)))))
7959         (push (gnus-data-number d) articles))) ; Success!
7960     (nreverse articles)))
7961
7962 (defun gnus-summary-execute-command (header regexp command &optional backward)
7963   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7964 If HEADER is an empty string (or nil), the match is done on the entire
7965 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7966   (interactive
7967    (list (let ((completion-ignore-case t))
7968            (completing-read
7969             "Header name: "
7970             (mapcar (lambda (header) (list (format "%s" header)))
7971                     (append
7972                      '("Number" "Subject" "From" "Lines" "Date"
7973                        "Message-ID" "Xref" "References" "Body")
7974                      gnus-extra-headers))
7975             nil 'require-match))
7976          (read-string "Regexp: ")
7977          (read-key-sequence "Command: ")
7978          current-prefix-arg))
7979   (when (equal header "Body")
7980     (setq header ""))
7981   ;; Hidden thread subtrees must be searched as well.
7982   (gnus-summary-show-all-threads)
7983   ;; We don't want to change current point nor window configuration.
7984   (save-excursion
7985     (save-window-excursion
7986       (gnus-message 6 "Executing %s..." (key-description command))
7987 ;; We'd like to execute COMMAND interactively so as to give arguments.
7988       (gnus-execute header regexp
7989                     `(call-interactively ',(key-binding command))
7990                     backward)
7991       (gnus-message 6 "Executing %s...done" (key-description command)))))
7992
7993 (defun gnus-summary-beginning-of-article ()
7994   "Scroll the article back to the beginning."
7995   (interactive)
7996   (gnus-summary-select-article)
7997   (gnus-configure-windows 'article)
7998   (gnus-eval-in-buffer-window gnus-article-buffer
7999     (widen)
8000     (goto-char (point-min))
8001     (when gnus-page-broken
8002       (gnus-narrow-to-page))))
8003
8004 (defun gnus-summary-end-of-article ()
8005   "Scroll to the end of the article."
8006   (interactive)
8007   (gnus-summary-select-article)
8008   (gnus-configure-windows 'article)
8009   (gnus-eval-in-buffer-window gnus-article-buffer
8010     (widen)
8011     (goto-char (point-max))
8012     (recenter -3)
8013     (when gnus-page-broken
8014       (gnus-narrow-to-page))))
8015
8016 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8017   "Truncate to LEN and quote all \"(\"'s in STRING."
8018   (gnus-replace-in-string (if (and len (> (length string) len))
8019                               (substring string 0 len)
8020                             string)
8021                           "[()]" "\\\\\\&"))
8022
8023 (defun gnus-summary-print-article (&optional filename n)
8024   "Generate and print a PostScript image of the N next (mail) articles.
8025
8026 If N is negative, print the N previous articles.  If N is nil and articles
8027 have been marked with the process mark, print these instead.
8028
8029 If the optional first argument FILENAME is nil, send the image to the
8030 printer.  If FILENAME is a string, save the PostScript image in a file with
8031 that name.  If FILENAME is a number, prompt the user for the name of the file
8032 to save in."
8033   (interactive (list (ps-print-preprint current-prefix-arg)))
8034   (dolist (article (gnus-summary-work-articles n))
8035     (gnus-summary-select-article nil nil 'pseudo article)
8036     (gnus-eval-in-buffer-window gnus-article-buffer
8037       (gnus-print-buffer))
8038     (gnus-summary-remove-process-mark article))
8039   (ps-despool filename))
8040
8041 (defun gnus-print-buffer ()
8042   (let ((buffer (generate-new-buffer " *print*")))
8043     (unwind-protect
8044         (progn
8045           (copy-to-buffer buffer (point-min) (point-max))
8046           (set-buffer buffer)
8047           (gnus-article-delete-invisible-text)
8048           (when (gnus-visual-p 'article-highlight 'highlight)
8049             ;; Copy-to-buffer doesn't copy overlay.  So redo
8050             ;; highlight.
8051             (let ((gnus-article-buffer buffer))
8052               (gnus-article-highlight-citation t)
8053               (gnus-article-highlight-signature)))
8054           (let ((ps-left-header
8055                  (list
8056                   (concat "("
8057                           (gnus-summary-print-truncate-and-quote
8058                            (mail-header-subject gnus-current-headers)
8059                            66) ")")
8060                   (concat "("
8061                           (gnus-summary-print-truncate-and-quote
8062                            (mail-header-from gnus-current-headers)
8063                            45) ")")))
8064                 (ps-right-header
8065                  (list
8066                   "/pagenumberstring load"
8067                   (concat "("
8068                           (mail-header-date gnus-current-headers) ")"))))
8069             (gnus-run-hooks 'gnus-ps-print-hook)
8070             (save-excursion
8071               (if window-system
8072                   (ps-spool-buffer-with-faces)
8073                 (ps-spool-buffer)))))
8074       (kill-buffer buffer))))
8075
8076 (defun gnus-summary-show-article (&optional arg)
8077   "Force redisplaying of the current article.
8078 If ARG (the prefix) is a number, show the article with the charset
8079 defined in `gnus-summary-show-article-charset-alist', or the charset
8080 input.
8081 If ARG (the prefix) is non-nil and not a number, show the raw article
8082 without any article massaging functions being run.  Normally, the key strokes 
8083 are `C-u g'."
8084   (interactive "P")
8085   (cond
8086    ((numberp arg)
8087     (gnus-summary-show-article t)
8088     (let ((gnus-newsgroup-charset
8089            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8090                (mm-read-coding-system
8091                 "View as charset: " ;; actually it is coding system.
8092                 (save-excursion
8093                   (set-buffer gnus-article-buffer)
8094                   (mm-detect-coding-region (point) (point-max))))))
8095           (gnus-newsgroup-ignored-charsets 'gnus-all))
8096       (gnus-summary-select-article nil 'force)
8097       (let ((deps gnus-newsgroup-dependencies)
8098             head header lines)
8099         (save-excursion
8100           (set-buffer gnus-original-article-buffer)
8101           (save-restriction
8102             (message-narrow-to-head)
8103             (setq head (buffer-string))
8104             (goto-char (point-min))
8105             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8106               (goto-char (point-max))
8107               (widen)
8108               (setq lines (1- (count-lines (point) (point-max))))))
8109           (with-temp-buffer
8110             (insert (format "211 %d Article retrieved.\n"
8111                             (cdr gnus-article-current)))
8112             (insert head)
8113             (if lines (insert (format "Lines: %d\n" lines)))
8114             (insert ".\n")
8115             (let ((nntp-server-buffer (current-buffer)))
8116               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8117         (gnus-data-set-header
8118          (gnus-data-find (cdr gnus-article-current))
8119          header)
8120         (gnus-summary-update-article-line
8121          (cdr gnus-article-current) header)
8122         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8123           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8124    ((not arg)
8125     ;; Select the article the normal way.
8126     (gnus-summary-select-article nil 'force))
8127    (t
8128     ;; We have to require this here to make sure that the following
8129     ;; dynamic binding isn't shadowed by autoloading.
8130     (require 'gnus-async)
8131     (require 'gnus-art)
8132     ;; Bind the article treatment functions to nil.
8133     (let ((gnus-have-all-headers t)
8134           gnus-article-prepare-hook
8135           gnus-article-decode-hook
8136           gnus-display-mime-function
8137           gnus-break-pages)
8138       ;; Destroy any MIME parts.
8139       (when (gnus-buffer-live-p gnus-article-buffer)
8140         (save-excursion
8141           (set-buffer gnus-article-buffer)
8142           (mm-destroy-parts gnus-article-mime-handles)
8143           ;; Set it to nil for safety reason.
8144           (setq gnus-article-mime-handle-alist nil)
8145           (setq gnus-article-mime-handles nil)))
8146       (gnus-summary-select-article nil 'force))))
8147   (gnus-summary-goto-subject gnus-current-article)
8148   (gnus-summary-position-point))
8149
8150 (defun gnus-summary-show-raw-article ()
8151   "Show the raw article without any article massaging functions being run."
8152   (interactive)
8153   (gnus-summary-show-article t))
8154
8155 (defun gnus-summary-verbose-headers (&optional arg)
8156   "Toggle permanent full header display.
8157 If ARG is a positive number, turn header display on.
8158 If ARG is a negative number, turn header display off."
8159   (interactive "P")
8160   (setq gnus-show-all-headers
8161         (cond ((or (not (numberp arg))
8162                    (zerop arg))
8163                (not gnus-show-all-headers))
8164               ((natnump arg)
8165                t)))
8166   (gnus-summary-show-article))
8167
8168 (defun gnus-summary-toggle-header (&optional arg)
8169   "Show the headers if they are hidden, or hide them if they are shown.
8170 If ARG is a positive number, show the entire header.
8171 If ARG is a negative number, hide the unwanted header lines."
8172   (interactive "P")
8173   (save-excursion
8174     (set-buffer gnus-article-buffer)
8175     (save-restriction
8176       (let* ((buffer-read-only nil)
8177              (inhibit-point-motion-hooks t)
8178              hidden e)
8179         (setq hidden
8180               (if (numberp arg)
8181                   (>= arg 0)
8182                 (save-restriction
8183                   (article-narrow-to-head)
8184                   (gnus-article-hidden-text-p 'headers))))
8185         (goto-char (point-min))
8186         (when (search-forward "\n\n" nil t)
8187           (delete-region (point-min) (1- (point))))
8188         (goto-char (point-min))
8189         (save-excursion
8190           (set-buffer gnus-original-article-buffer)
8191           (goto-char (point-min))
8192           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
8193         (insert-buffer-substring gnus-original-article-buffer 1 e)
8194         (save-restriction
8195           (narrow-to-region (point-min) (point))
8196           (article-decode-encoded-words)
8197           (if  hidden
8198               (let ((gnus-treat-hide-headers nil)
8199                     (gnus-treat-hide-boring-headers nil))
8200                 (gnus-delete-wash-type 'headers)
8201                 (gnus-treat-article 'head))
8202             (gnus-treat-article 'head)))
8203         (gnus-set-mode-line 'article)))))
8204
8205 (defun gnus-summary-show-all-headers ()
8206   "Make all header lines visible."
8207   (interactive)
8208   (gnus-summary-toggle-header 1))
8209
8210 (defun gnus-summary-caesar-message (&optional arg)
8211   "Caesar rotate the current article by 13.
8212 The numerical prefix specifies how many places to rotate each letter
8213 forward."
8214   (interactive "P")
8215   (gnus-summary-select-article)
8216   (let ((mail-header-separator ""))
8217     (gnus-eval-in-buffer-window gnus-article-buffer
8218       (save-restriction
8219         (widen)
8220         (let ((start (window-start))
8221               buffer-read-only)
8222           (message-caesar-buffer-body arg)
8223           (set-window-start (get-buffer-window (current-buffer)) start))))))
8224
8225 (defun gnus-summary-stop-page-breaking ()
8226   "Stop page breaking in the current article."
8227   (interactive)
8228   (gnus-summary-select-article)
8229   (gnus-eval-in-buffer-window gnus-article-buffer
8230     (widen)
8231     (when (gnus-visual-p 'page-marker)
8232       (let ((buffer-read-only nil))
8233         (gnus-remove-text-with-property 'gnus-prev)
8234         (gnus-remove-text-with-property 'gnus-next))
8235       (setq gnus-page-broken nil))))
8236
8237 (defun gnus-summary-move-article (&optional n to-newsgroup
8238                                             select-method action)
8239   "Move the current article to a different newsgroup.
8240 If N is a positive number, move the N next articles.
8241 If N is a negative number, move the N previous articles.
8242 If N is nil and any articles have been marked with the process mark,
8243 move those articles instead.
8244 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8245 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8246 re-spool using this method.
8247
8248 For this function to work, both the current newsgroup and the
8249 newsgroup that you want to move to have to support the `request-move'
8250 and `request-accept' functions.
8251
8252 ACTION can be either `move' (the default), `crosspost' or `copy'."
8253   (interactive "P")
8254   (unless action
8255     (setq action 'move))
8256   ;; Check whether the source group supports the required functions.
8257   (cond ((and (eq action 'move)
8258               (not (gnus-check-backend-function
8259                     'request-move-article gnus-newsgroup-name)))
8260          (error "The current group does not support article moving"))
8261         ((and (eq action 'crosspost)
8262               (not (gnus-check-backend-function
8263                     'request-replace-article gnus-newsgroup-name)))
8264          (error "The current group does not support article editing")))
8265   (let ((articles (gnus-summary-work-articles n))
8266         (prefix (if (gnus-check-backend-function
8267                      'request-move-article gnus-newsgroup-name)
8268                     (gnus-group-real-prefix gnus-newsgroup-name)
8269                   ""))
8270         (names '((move "Move" "Moving")
8271                  (copy "Copy" "Copying")
8272                  (crosspost "Crosspost" "Crossposting")))
8273         (copy-buf (save-excursion
8274                     (nnheader-set-temp-buffer " *copy article*")))
8275         art-group to-method new-xref article to-groups)
8276     (unless (assq action names)
8277       (error "Unknown action %s" action))
8278     ;; We have to select an article to give
8279     ;; `gnus-read-move-group-name' an opportunity to suggest an
8280     ;; appropriate default.
8281     (unless (gnus-buffer-live-p gnus-original-article-buffer)
8282       (let ((gnus-display-mime-function nil)
8283             (gnus-article-prepare-hook nil))
8284         (gnus-summary-select-article nil nil nil (car articles))))
8285     ;; Read the newsgroup name.
8286     (when (and (not to-newsgroup)
8287                (not select-method))
8288       (setq to-newsgroup
8289             (gnus-read-move-group-name
8290              (cadr (assq action names))
8291              (symbol-value (intern (format "gnus-current-%s-group" action)))
8292              articles prefix))
8293       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8294     (setq to-method (or select-method
8295                         (gnus-server-to-method
8296                          (gnus-group-method to-newsgroup))))
8297     ;; Check the method we are to move this article to...
8298     (unless (gnus-check-backend-function
8299              'request-accept-article (car to-method))
8300       (error "%s does not support article copying" (car to-method)))
8301     (unless (gnus-check-server to-method)
8302       (error "Can't open server %s" (car to-method)))
8303     (gnus-message 6 "%s to %s: %s..."
8304                   (caddr (assq action names))
8305                   (or (car select-method) to-newsgroup) articles)
8306     (while articles
8307       (setq article (pop articles))
8308       (setq
8309        art-group
8310        (cond
8311         ;; Move the article.
8312         ((eq action 'move)
8313          ;; Remove this article from future suppression.
8314          (gnus-dup-unsuppress-article article)
8315          (gnus-request-move-article
8316           article                       ; Article to move
8317           gnus-newsgroup-name           ; From newsgroup
8318           (nth 1 (gnus-find-method-for-group
8319                   gnus-newsgroup-name)) ; Server
8320           (list 'gnus-request-accept-article
8321                 to-newsgroup (list 'quote select-method)
8322                 (not articles) t)       ; Accept form
8323           (not articles)))              ; Only save nov last time
8324         ;; Copy the article.
8325         ((eq action 'copy)
8326          (save-excursion
8327            (set-buffer copy-buf)
8328            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8329              (gnus-request-accept-article
8330               to-newsgroup select-method (not articles) t))))
8331         ;; Crosspost the article.
8332         ((eq action 'crosspost)
8333          (let ((xref (message-tokenize-header
8334                       (mail-header-xref (gnus-summary-article-header article))
8335                       " ")))
8336            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8337                                   ":" (number-to-string article)))
8338            (unless xref
8339              (setq xref (list (system-name))))
8340            (setq new-xref
8341                  (concat
8342                   (mapconcat 'identity
8343                              (delete "Xref:" (delete new-xref xref))
8344                              " ")
8345                   " " new-xref))
8346            (save-excursion
8347              (set-buffer copy-buf)
8348              ;; First put the article in the destination group.
8349              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8350              (when (consp (setq art-group
8351                                 (gnus-request-accept-article
8352                                  to-newsgroup select-method (not articles))))
8353                (setq new-xref (concat new-xref " " (car art-group)
8354                                       ":"
8355                                       (number-to-string (cdr art-group))))
8356                ;; Now we have the new Xrefs header, so we insert
8357                ;; it and replace the new article.
8358                (nnheader-replace-header "Xref" new-xref)
8359                (gnus-request-replace-article
8360                 (cdr art-group) to-newsgroup (current-buffer))
8361                art-group))))))
8362       (cond
8363        ((not art-group)
8364         (gnus-message 1 "Couldn't %s article %s: %s"
8365                       (cadr (assq action names)) article
8366                       (nnheader-get-report (car to-method))))
8367        ((eq art-group 'junk)
8368         (when (eq action 'move)
8369           (gnus-summary-mark-article article gnus-canceled-mark)
8370           (gnus-message 4 "Deleted article %s" article)))
8371        (t
8372         (let* ((pto-group (gnus-group-prefixed-name
8373                            (car art-group) to-method))
8374                (entry
8375                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8376                (info (nth 2 entry))
8377                (to-group (gnus-info-group info))
8378                to-marks)
8379           ;; Update the group that has been moved to.
8380           (when (and info
8381                      (memq action '(move copy)))
8382             (unless (member to-group to-groups)
8383               (push to-group to-groups))
8384
8385             (unless (memq article gnus-newsgroup-unreads)
8386               (push 'read to-marks)
8387               (gnus-info-set-read
8388                info (gnus-add-to-range (gnus-info-read info)
8389                                        (list (cdr art-group)))))
8390
8391             ;; See whether the article is to be put in the cache.
8392             (let ((marks gnus-article-mark-lists)
8393                   (to-article (cdr art-group)))
8394
8395               ;; Enter the article into the cache in the new group,
8396               ;; if that is required.
8397               (when gnus-use-cache
8398                 (gnus-cache-possibly-enter-article
8399                  to-group to-article
8400                  (memq article gnus-newsgroup-marked)
8401                  (memq article gnus-newsgroup-dormant)
8402                  (memq article gnus-newsgroup-unreads)))
8403
8404               (when gnus-preserve-marks
8405                 ;; Copy any marks over to the new group.
8406                 (when (and (equal to-group gnus-newsgroup-name)
8407                            (not (memq article gnus-newsgroup-unreads)))
8408                   ;; Mark this article as read in this group.
8409                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8410                   (setcdr (gnus-active to-group) to-article)
8411                   (setcdr gnus-newsgroup-active to-article))
8412
8413                 (while marks
8414                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8415                     (when (memq article (symbol-value
8416                                          (intern (format "gnus-newsgroup-%s"
8417                                                          (caar marks)))))
8418                       (push (cdar marks) to-marks)
8419                       ;; If the other group is the same as this group,
8420                       ;; then we have to add the mark to the list.
8421                       (when (equal to-group gnus-newsgroup-name)
8422                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8423                              (cons to-article
8424                                    (symbol-value
8425                                     (intern (format "gnus-newsgroup-%s"
8426                                                     (caar marks)))))))
8427                       ;; Copy the marks to other group.
8428                       (gnus-add-marked-articles
8429                        to-group (cdar marks) (list to-article) info)))
8430                   (setq marks (cdr marks)))
8431
8432                 (gnus-request-set-mark to-group (list (list (list to-article)
8433                                                             'add
8434                                                             to-marks))))
8435
8436               (gnus-dribble-enter
8437                (concat "(gnus-group-set-info '"
8438                        (gnus-prin1-to-string (gnus-get-info to-group))
8439                        ")"))))
8440
8441           ;; Update the Xref header in this article to point to
8442           ;; the new crossposted article we have just created.
8443           (when (eq action 'crosspost)
8444             (save-excursion
8445               (set-buffer copy-buf)
8446               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8447               (nnheader-replace-header "Xref" new-xref)
8448               (gnus-request-replace-article
8449                article gnus-newsgroup-name (current-buffer)))))
8450
8451         ;;;!!!Why is this necessary?
8452         (set-buffer gnus-summary-buffer)
8453
8454         (gnus-summary-goto-subject article)
8455         (when (eq action 'move)
8456           (gnus-summary-mark-article article gnus-canceled-mark))))
8457       (gnus-summary-remove-process-mark article))
8458     ;; Re-activate all groups that have been moved to.
8459     (save-excursion
8460       (set-buffer gnus-group-buffer)
8461       (let ((gnus-group-marked to-groups))
8462         (gnus-group-get-new-news-this-group nil t)))
8463
8464     (gnus-kill-buffer copy-buf)
8465     (gnus-summary-position-point)
8466     (gnus-set-mode-line 'summary)))
8467
8468 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8469   "Move the current article to a different newsgroup.
8470 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8471 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8472 re-spool using this method."
8473   (interactive "P")
8474   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8475
8476 (defun gnus-summary-crosspost-article (&optional n)
8477   "Crosspost the current article to some other group."
8478   (interactive "P")
8479   (gnus-summary-move-article n nil nil 'crosspost))
8480
8481 (defcustom gnus-summary-respool-default-method nil
8482   "Default method type for respooling an article.
8483 If nil, use to the current newsgroup method."
8484   :type 'symbol
8485   :group 'gnus-summary-mail)
8486
8487 (defun gnus-summary-respool-article (&optional n method)
8488   "Respool the current article.
8489 The article will be squeezed through the mail spooling process again,
8490 which means that it will be put in some mail newsgroup or other
8491 depending on `nnmail-split-methods'.
8492 If N is a positive number, respool the N next articles.
8493 If N is a negative number, respool the N previous articles.
8494 If N is nil and any articles have been marked with the process mark,
8495 respool those articles instead.
8496
8497 Respooling can be done both from mail groups and \"real\" newsgroups.
8498 In the former case, the articles in question will be moved from the
8499 current group into whatever groups they are destined to.  In the
8500 latter case, they will be copied into the relevant groups."
8501   (interactive
8502    (list current-prefix-arg
8503          (let* ((methods (gnus-methods-using 'respool))
8504                 (methname
8505                  (symbol-name (or gnus-summary-respool-default-method
8506                                   (car (gnus-find-method-for-group
8507                                         gnus-newsgroup-name)))))
8508                 (method
8509                  (gnus-completing-read-with-default
8510                   methname "What backend do you want to use when respooling?"
8511                   methods nil t nil 'gnus-mail-method-history))
8512                 ms)
8513            (cond
8514             ((zerop (length (setq ms (gnus-servers-using-backend
8515                                       (intern method)))))
8516              (list (intern method) ""))
8517             ((= 1 (length ms))
8518              (car ms))
8519             (t
8520              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8521                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8522                            ms-alist))))))))
8523   (unless method
8524     (error "No method given for respooling"))
8525   (if (assoc (symbol-name
8526               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8527              (gnus-methods-using 'respool))
8528       (gnus-summary-move-article n nil method)
8529     (gnus-summary-copy-article n nil method)))
8530
8531 (defun gnus-summary-import-article (file &optional edit)
8532   "Import an arbitrary file into a mail newsgroup."
8533   (interactive "fImport file: \nP")
8534   (let ((group gnus-newsgroup-name)
8535         (now (current-time))
8536         atts lines group-art)
8537     (unless (gnus-check-backend-function 'request-accept-article group)
8538       (error "%s does not support article importing" group))
8539     (or (file-readable-p file)
8540         (not (file-regular-p file))
8541         (error "Can't read %s" file))
8542     (save-excursion
8543       (set-buffer (gnus-get-buffer-create " *import file*"))
8544       (erase-buffer)
8545       (nnheader-insert-file-contents file)
8546       (goto-char (point-min))
8547       (if (nnheader-article-p)
8548           (save-restriction
8549             (goto-char (point-min))
8550             (search-forward "\n\n" nil t)
8551             (narrow-to-region (point-min) (1- (point)))
8552             (goto-char (point-min))
8553             (unless (re-search-forward "^date:" nil t)
8554               (goto-char (point-max))
8555               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8556        ;; This doesn't look like an article, so we fudge some headers.
8557         (setq atts (file-attributes file)
8558               lines (count-lines (point-min) (point-max)))
8559         (insert "From: " (read-string "From: ") "\n"
8560                 "Subject: " (read-string "Subject: ") "\n"
8561                 "Date: " (message-make-date (nth 5 atts)) "\n"
8562                 "Message-ID: " (message-make-message-id) "\n"
8563                 "Lines: " (int-to-string lines) "\n"
8564                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8565       (setq group-art (gnus-request-accept-article group nil t))
8566       (kill-buffer (current-buffer)))
8567     (setq gnus-newsgroup-active (gnus-activate-group group))
8568     (forward-line 1)
8569     (gnus-summary-goto-article (cdr group-art) nil t)
8570     (when edit
8571       (gnus-summary-edit-article))))
8572
8573 (defun gnus-summary-create-article ()
8574   "Create an article in a mail newsgroup."
8575   (interactive)
8576   (let ((group gnus-newsgroup-name)
8577         (now (current-time))
8578         group-art)
8579     (unless (gnus-check-backend-function 'request-accept-article group)
8580       (error "%s does not support article importing" group))
8581     (save-excursion
8582       (set-buffer (gnus-get-buffer-create " *import file*"))
8583       (erase-buffer)
8584       (goto-char (point-min))
8585       ;; This doesn't look like an article, so we fudge some headers.
8586       (insert "From: " (read-string "From: ") "\n"
8587               "Subject: " (read-string "Subject: ") "\n"
8588               "Date: " (message-make-date now) "\n"
8589               "Message-ID: " (message-make-message-id) "\n")
8590       (setq group-art (gnus-request-accept-article group nil t))
8591       (kill-buffer (current-buffer)))
8592     (setq gnus-newsgroup-active (gnus-activate-group group))
8593     (forward-line 1)
8594     (gnus-summary-goto-article (cdr group-art) nil t)
8595     (gnus-summary-edit-article)))
8596
8597 (defun gnus-summary-article-posted-p ()
8598   "Say whether the current (mail) article is available from news as well.
8599 This will be the case if the article has both been mailed and posted."
8600   (interactive)
8601   (let ((id (mail-header-references (gnus-summary-article-header)))
8602         (gnus-override-method (car (gnus-refer-article-methods))))
8603     (if (gnus-request-head id "")
8604         (gnus-message 2 "The current message was found on %s"
8605                       gnus-override-method)
8606       (gnus-message 2 "The current message couldn't be found on %s"
8607                     gnus-override-method)
8608       nil)))
8609
8610 (defun gnus-summary-expire-articles (&optional now)
8611   "Expire all articles that are marked as expirable in the current group."
8612   (interactive)
8613   (when (gnus-check-backend-function
8614          'request-expire-articles gnus-newsgroup-name)
8615     ;; This backend supports expiry.
8616     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8617            (expirable (if total
8618                           (progn
8619                             ;; We need to update the info for
8620                             ;; this group for `gnus-list-of-read-articles'
8621                             ;; to give us the right answer.
8622                             (gnus-run-hooks 'gnus-exit-group-hook)
8623                             (gnus-summary-update-info)
8624                             (gnus-list-of-read-articles gnus-newsgroup-name))
8625                         (setq gnus-newsgroup-expirable
8626                               (sort gnus-newsgroup-expirable '<))))
8627            (expiry-wait (if now 'immediate
8628                           (gnus-group-find-parameter
8629                            gnus-newsgroup-name 'expiry-wait)))
8630            (nnmail-expiry-target
8631             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8632                 nnmail-expiry-target))
8633            es)
8634       (when expirable
8635         ;; There are expirable articles in this group, so we run them
8636         ;; through the expiry process.
8637         (gnus-message 6 "Expiring articles...")
8638         (unless (gnus-check-group gnus-newsgroup-name)
8639           (error "Can't open server for %s" gnus-newsgroup-name))
8640         ;; The list of articles that weren't expired is returned.
8641         (save-excursion
8642           (if expiry-wait
8643               (let ((nnmail-expiry-wait-function nil)
8644                     (nnmail-expiry-wait expiry-wait))
8645                 (setq es (gnus-request-expire-articles
8646                           expirable gnus-newsgroup-name)))
8647             (setq es (gnus-request-expire-articles
8648                       expirable gnus-newsgroup-name)))
8649           (unless total
8650             (setq gnus-newsgroup-expirable es))
8651           ;; We go through the old list of expirable, and mark all
8652           ;; really expired articles as nonexistent.
8653           (unless (eq es expirable) ;If nothing was expired, we don't mark.
8654             (let ((gnus-use-cache nil))
8655               (dolist (article expirable)
8656                 (when (and (not (memq article es))
8657                            (gnus-data-find article))
8658                   (gnus-summary-mark-article article gnus-canceled-mark))))))
8659         (gnus-message 6 "Expiring articles...done")))))
8660
8661 (defun gnus-summary-expire-articles-now ()
8662   "Expunge all expirable articles in the current group.
8663 This means that *all* articles that are marked as expirable will be
8664 deleted forever, right now."
8665   (interactive)
8666   (or gnus-expert-user
8667       (gnus-yes-or-no-p
8668        "Are you really, really, really sure you want to delete all these messages? ")
8669       (error "Phew!"))
8670   (gnus-summary-expire-articles t))
8671
8672 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8673 (defun gnus-summary-delete-article (&optional n)
8674   "Delete the N next (mail) articles.
8675 This command actually deletes articles.  This is not a marking
8676 command.  The article will disappear forever from your life, never to
8677 return.
8678 If N is negative, delete backwards.
8679 If N is nil and articles have been marked with the process mark,
8680 delete these instead."
8681   (interactive "P")
8682   (unless (gnus-check-backend-function 'request-expire-articles
8683                                        gnus-newsgroup-name)
8684     (error "The current newsgroup does not support article deletion"))
8685   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8686     (error "Couldn't open server"))
8687   ;; Compute the list of articles to delete.
8688   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8689         not-deleted)
8690     (if (and gnus-novice-user
8691              (not (gnus-yes-or-no-p
8692                    (format "Do you really want to delete %s forever? "
8693                            (if (> (length articles) 1)
8694                                (format "these %s articles" (length articles))
8695                              "this article")))))
8696         ()
8697       ;; Delete the articles.
8698       (setq not-deleted (gnus-request-expire-articles
8699                          articles gnus-newsgroup-name 'force))
8700       (while articles
8701         (gnus-summary-remove-process-mark (car articles))
8702         ;; The backend might not have been able to delete the article
8703         ;; after all.
8704         (unless (memq (car articles) not-deleted)
8705           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8706         (setq articles (cdr articles)))
8707       (when not-deleted
8708         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8709     (gnus-summary-position-point)
8710     (gnus-set-mode-line 'summary)
8711     not-deleted))
8712
8713 (defun gnus-summary-edit-article (&optional arg)
8714   "Edit the current article.
8715 This will have permanent effect only in mail groups.
8716 If ARG is nil, edit the decoded articles.
8717 If ARG is 1, edit the raw articles.
8718 If ARG is 2, edit the raw articles even in read-only groups.
8719 If ARG is 3, edit the articles with the current handles.
8720 Otherwise, allow editing of articles even in read-only
8721 groups."
8722   (interactive "P")
8723   (let (force raw current-handles)
8724     (cond
8725      ((null arg))
8726      ((eq arg 1)
8727       (setq raw t))
8728      ((eq arg 2)
8729       (setq raw t
8730             force t))
8731      ((eq arg 3)
8732       (setq current-handles
8733             (and (gnus-buffer-live-p gnus-article-buffer)
8734                  (with-current-buffer gnus-article-buffer
8735                    (prog1
8736                        gnus-article-mime-handles
8737                      (setq gnus-article-mime-handles nil))))))
8738      (t
8739       (setq force t)))
8740     (when (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
8741       (error "Can't edit the raw article in group nndraft:drafts"))
8742     (save-excursion
8743       (set-buffer gnus-summary-buffer)
8744       (let ((mail-parse-charset gnus-newsgroup-charset)
8745             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8746         (gnus-set-global-variables)
8747         (when (and (not force)
8748                    (gnus-group-read-only-p))
8749           (error "The current newsgroup does not support article editing"))
8750         (gnus-summary-show-article t)
8751         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
8752           (with-current-buffer gnus-article-buffer
8753             (mm-enable-multibyte)))
8754         (if (equal gnus-newsgroup-name "nndraft:drafts")
8755             (setq raw t))
8756         (gnus-article-edit-article
8757          (if raw 'ignore
8758            `(lambda ()
8759               (let ((mbl mml-buffer-list))
8760                 (setq mml-buffer-list nil)
8761                 (mime-to-mml ,'current-handles)
8762                 (let ((mbl1 mml-buffer-list))
8763                   (setq mml-buffer-list mbl)
8764                   (set (make-local-variable 'mml-buffer-list) mbl1))
8765                 (make-local-hook 'kill-buffer-hook)
8766                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
8767          `(lambda (no-highlight)
8768             (let ((mail-parse-charset ',gnus-newsgroup-charset)
8769                   (message-options message-options)
8770                   (message-options-set-recipient)
8771                   (mail-parse-ignored-charsets
8772                    ',gnus-newsgroup-ignored-charsets))
8773               ,(if (not raw) '(progn
8774                                 (mml-to-mime)
8775                                 (mml-destroy-buffers)
8776                                 (remove-hook 'kill-buffer-hook
8777                                              'mml-destroy-buffers t)
8778                                 (kill-local-variable 'mml-buffer-list)))
8779               (gnus-summary-edit-article-done
8780                ,(or (mail-header-references gnus-current-headers) "")
8781                ,(gnus-group-read-only-p)
8782                ,gnus-summary-buffer no-highlight))))))))
8783
8784 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8785
8786 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8787                                                  no-highlight)
8788   "Make edits to the current article permanent."
8789   (interactive)
8790   (save-excursion
8791    ;; The buffer restriction contains the entire article if it exists.
8792     (when (article-goto-body)
8793       (let ((lines (count-lines (point) (point-max)))
8794             (length (- (point-max) (point)))
8795             (case-fold-search t)
8796             (body (copy-marker (point))))
8797         (goto-char (point-min))
8798         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8799           (delete-region (match-beginning 1) (match-end 1))
8800           (insert (number-to-string length)))
8801         (goto-char (point-min))
8802         (when (re-search-forward
8803                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8804           (delete-region (match-beginning 1) (match-end 1))
8805           (insert (number-to-string length)))
8806         (goto-char (point-min))
8807         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8808           (delete-region (match-beginning 1) (match-end 1))
8809           (insert (number-to-string lines))))))
8810   ;; Replace the article.
8811   (let ((buf (current-buffer)))
8812     (with-temp-buffer
8813       (insert-buffer-substring buf)
8814
8815       (if (and (not read-only)
8816                (not (gnus-request-replace-article
8817                      (cdr gnus-article-current) (car gnus-article-current)
8818                      (current-buffer) t)))
8819           (error "Couldn't replace article")
8820         ;; Update the summary buffer.
8821         (if (and references
8822                  (equal (message-tokenize-header references " ")
8823                         (message-tokenize-header
8824                          (or (message-fetch-field "references") "") " ")))
8825             ;; We only have to update this line.
8826             (save-excursion
8827               (save-restriction
8828                 (message-narrow-to-head)
8829                 (let ((head (buffer-string))
8830                       header)
8831                   (with-temp-buffer
8832                     (insert (format "211 %d Article retrieved.\n"
8833                                     (cdr gnus-article-current)))
8834                     (insert head)
8835                     (insert ".\n")
8836                     (let ((nntp-server-buffer (current-buffer)))
8837                       (setq header (car (gnus-get-newsgroup-headers
8838                                          (save-excursion
8839                                            (set-buffer gnus-summary-buffer)
8840                                            gnus-newsgroup-dependencies)
8841                                          t))))
8842                     (save-excursion
8843                       (set-buffer gnus-summary-buffer)
8844                       (gnus-data-set-header
8845                        (gnus-data-find (cdr gnus-article-current))
8846                        header)
8847                       (gnus-summary-update-article-line
8848                        (cdr gnus-article-current) header)
8849                       (if (gnus-summary-goto-subject
8850                            (cdr gnus-article-current) nil t)
8851                           (gnus-summary-update-secondary-mark
8852                            (cdr gnus-article-current))))))))
8853           ;; Update threads.
8854           (set-buffer (or buffer gnus-summary-buffer))
8855           (gnus-summary-update-article (cdr gnus-article-current))
8856           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8857               (gnus-summary-update-secondary-mark
8858                (cdr gnus-article-current))))
8859         ;; Prettify the article buffer again.
8860         (unless no-highlight
8861           (save-excursion
8862             (set-buffer gnus-article-buffer)
8863             ;;;!!! Fix this -- article should be rehighlighted.
8864             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8865             (set-buffer gnus-original-article-buffer)
8866             (gnus-request-article
8867              (cdr gnus-article-current)
8868              (car gnus-article-current) (current-buffer))))
8869         ;; Prettify the summary buffer line.
8870         (when (gnus-visual-p 'summary-highlight 'highlight)
8871           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8872
8873 (defun gnus-summary-edit-wash (key)
8874   "Perform editing command KEY in the article buffer."
8875   (interactive
8876    (list
8877     (progn
8878       (message "%s" (concat (this-command-keys) "- "))
8879       (read-char))))
8880   (message "")
8881   (gnus-summary-edit-article)
8882   (execute-kbd-macro (concat (this-command-keys) key))
8883   (gnus-article-edit-done))
8884
8885 ;;; Respooling
8886
8887 (defun gnus-summary-respool-query (&optional silent trace)
8888   "Query where the respool algorithm would put this article."
8889   (interactive)
8890   (let (gnus-mark-article-hook)
8891     (gnus-summary-select-article)
8892     (save-excursion
8893       (set-buffer gnus-original-article-buffer)
8894       (save-restriction
8895         (message-narrow-to-head)
8896         (let ((groups (nnmail-article-group 'identity trace)))
8897           (unless silent
8898             (if groups
8899                 (message "This message would go to %s"
8900                          (mapconcat 'car groups ", "))
8901               (message "This message would go to no groups"))
8902             groups))))))
8903
8904 (defun gnus-summary-respool-trace ()
8905   "Trace where the respool algorithm would put this article.
8906 Display a buffer showing all fancy splitting patterns which matched."
8907   (interactive)
8908   (gnus-summary-respool-query nil t))
8909
8910 ;; Summary marking commands.
8911
8912 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8913   "Mark articles which has the same subject as read, and then select the next.
8914 If UNMARK is positive, remove any kind of mark.
8915 If UNMARK is negative, tick articles."
8916   (interactive "P")
8917   (when unmark
8918     (setq unmark (prefix-numeric-value unmark)))
8919   (let ((count
8920          (gnus-summary-mark-same-subject
8921           (gnus-summary-article-subject) unmark)))
8922     ;; Select next unread article.  If auto-select-same mode, should
8923     ;; select the first unread article.
8924     (gnus-summary-next-article t (and gnus-auto-select-same
8925                                       (gnus-summary-article-subject)))
8926     (gnus-message 7 "%d article%s marked as %s"
8927                   count (if (= count 1) " is" "s are")
8928                   (if unmark "unread" "read"))))
8929
8930 (defun gnus-summary-kill-same-subject (&optional unmark)
8931   "Mark articles which has the same subject as read.
8932 If UNMARK is positive, remove any kind of mark.
8933 If UNMARK is negative, tick articles."
8934   (interactive "P")
8935   (when unmark
8936     (setq unmark (prefix-numeric-value unmark)))
8937   (let ((count
8938          (gnus-summary-mark-same-subject
8939           (gnus-summary-article-subject) unmark)))
8940     ;; If marked as read, go to next unread subject.
8941     (when (null unmark)
8942       ;; Go to next unread subject.
8943       (gnus-summary-next-subject 1 t))
8944     (gnus-message 7 "%d articles are marked as %s"
8945                   count (if unmark "unread" "read"))))
8946
8947 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8948   "Mark articles with same SUBJECT as read, and return marked number.
8949 If optional argument UNMARK is positive, remove any kinds of marks.
8950 If optional argument UNMARK is negative, mark articles as unread instead."
8951   (let ((count 1))
8952     (save-excursion
8953       (cond
8954        ((null unmark)                   ; Mark as read.
8955         (while (and
8956                 (progn
8957                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8958                   (gnus-summary-show-thread) t)
8959                 (gnus-summary-find-subject subject))
8960           (setq count (1+ count))))
8961        ((> unmark 0)                    ; Tick.
8962         (while (and
8963                 (progn
8964                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8965                   (gnus-summary-show-thread) t)
8966                 (gnus-summary-find-subject subject))
8967           (setq count (1+ count))))
8968        (t                               ; Mark as unread.
8969         (while (and
8970                 (progn
8971                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8972                   (gnus-summary-show-thread) t)
8973                 (gnus-summary-find-subject subject))
8974           (setq count (1+ count)))))
8975       (gnus-set-mode-line 'summary)
8976       ;; Return the number of marked articles.
8977       count)))
8978
8979 (defun gnus-summary-mark-as-processable (n &optional unmark)
8980   "Set the process mark on the next N articles.
8981 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8982 the process mark instead.  The difference between N and the actual
8983 number of articles marked is returned."
8984   (interactive "P")
8985   (if (and (null n) (gnus-region-active-p))
8986       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8987     (setq n (prefix-numeric-value n))
8988     (let ((backward (< n 0))
8989           (n (abs n)))
8990       (while (and
8991               (> n 0)
8992               (if unmark
8993                   (gnus-summary-remove-process-mark
8994                    (gnus-summary-article-number))
8995                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8996               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8997         (setq n (1- n)))
8998       (when (/= 0 n)
8999         (gnus-message 7 "No more articles"))
9000       (gnus-summary-recenter)
9001       (gnus-summary-position-point)
9002       n)))
9003
9004 (defun gnus-summary-unmark-as-processable (n)
9005   "Remove the process mark from the next N articles.
9006 If N is negative, unmark backward instead.  The difference between N and
9007 the actual number of articles unmarked is returned."
9008   (interactive "P")
9009   (gnus-summary-mark-as-processable n t))
9010
9011 (defun gnus-summary-unmark-all-processable ()
9012   "Remove the process mark from all articles."
9013   (interactive)
9014   (save-excursion
9015     (while gnus-newsgroup-processable
9016       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9017   (gnus-summary-position-point))
9018
9019 (defun gnus-summary-add-mark (article type)
9020   "Mark ARTICLE with a mark of TYPE."
9021   (let ((vtype (car (assq type gnus-article-mark-lists)))
9022         var)
9023     (if (not vtype)
9024         (error "No such mark type: %s" type)
9025       (setq var (intern (format "gnus-newsgroup-%s" type)))
9026       (set var (cons article (symbol-value var)))
9027       (if (memq type '(processable cached replied forwarded recent saved))
9028           (gnus-summary-update-secondary-mark article)
9029         ;;; !!! This is bogus.  We should find out what primary
9030         ;;; !!! mark we want to set.
9031         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9032
9033 (defun gnus-summary-mark-as-expirable (n)
9034   "Mark N articles forward as expirable.
9035 If N is negative, mark backward instead.  The difference between N and
9036 the actual number of articles marked is returned."
9037   (interactive "p")
9038   (gnus-summary-mark-forward n gnus-expirable-mark))
9039
9040 (defun gnus-summary-mark-article-as-replied (article)
9041   "Mark ARTICLE as replied to and update the summary line.
9042 ARTICLE can also be a list of articles."
9043   (interactive (list (gnus-summary-article-number)))
9044   (let ((articles (if (listp article) article (list article))))
9045     (dolist (article articles)
9046       (push article gnus-newsgroup-replied)
9047       (let ((buffer-read-only nil))
9048         (when (gnus-summary-goto-subject article nil t)
9049           (gnus-summary-update-secondary-mark article))))))
9050
9051 (defun gnus-summary-mark-article-as-forwarded (article)
9052   "Mark ARTICLE as forwarded and update the summary line.
9053 ARTICLE can also be a list of articles."
9054   (let ((articles (if (listp article) article (list article))))
9055     (dolist (article articles)
9056       (push article gnus-newsgroup-forwarded)
9057       (let ((buffer-read-only nil))
9058         (when (gnus-summary-goto-subject article nil t)
9059           (gnus-summary-update-secondary-mark article))))))
9060
9061 (defun gnus-summary-set-bookmark (article)
9062   "Set a bookmark in current article."
9063   (interactive (list (gnus-summary-article-number)))
9064   (when (or (not (get-buffer gnus-article-buffer))
9065             (not gnus-current-article)
9066             (not gnus-article-current)
9067             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9068     (error "No current article selected"))
9069   ;; Remove old bookmark, if one exists.
9070   (let ((old (assq article gnus-newsgroup-bookmarks)))
9071     (when old
9072       (setq gnus-newsgroup-bookmarks
9073             (delq old gnus-newsgroup-bookmarks))))
9074   ;; Set the new bookmark, which is on the form
9075   ;; (article-number . line-number-in-body).
9076   (push
9077    (cons article
9078          (save-excursion
9079            (set-buffer gnus-article-buffer)
9080            (count-lines
9081             (min (point)
9082                  (save-excursion
9083                    (goto-char (point-min))
9084                    (search-forward "\n\n" nil t)
9085                    (point)))
9086             (point))))
9087    gnus-newsgroup-bookmarks)
9088   (gnus-message 6 "A bookmark has been added to the current article."))
9089
9090 (defun gnus-summary-remove-bookmark (article)
9091   "Remove the bookmark from the current article."
9092   (interactive (list (gnus-summary-article-number)))
9093   ;; Remove old bookmark, if one exists.
9094   (let ((old (assq article gnus-newsgroup-bookmarks)))
9095     (if old
9096         (progn
9097           (setq gnus-newsgroup-bookmarks
9098                 (delq old gnus-newsgroup-bookmarks))
9099           (gnus-message 6 "Removed bookmark."))
9100       (gnus-message 6 "No bookmark in current article."))))
9101
9102 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9103 (defun gnus-summary-mark-as-dormant (n)
9104   "Mark N articles forward as dormant.
9105 If N is negative, mark backward instead.  The difference between N and
9106 the actual number of articles marked is returned."
9107   (interactive "p")
9108   (gnus-summary-mark-forward n gnus-dormant-mark))
9109
9110 (defun gnus-summary-set-process-mark (article)
9111   "Set the process mark on ARTICLE and update the summary line."
9112   (setq gnus-newsgroup-processable
9113         (cons article
9114               (delq article gnus-newsgroup-processable)))
9115   (when (gnus-summary-goto-subject article)
9116     (gnus-summary-show-thread)
9117     (gnus-summary-goto-subject article)
9118     (gnus-summary-update-secondary-mark article)))
9119
9120 (defun gnus-summary-remove-process-mark (article)
9121   "Remove the process mark from ARTICLE and update the summary line."
9122   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9123   (when (gnus-summary-goto-subject article)
9124     (gnus-summary-show-thread)
9125     (gnus-summary-goto-subject article)
9126     (gnus-summary-update-secondary-mark article)))
9127
9128 (defun gnus-summary-set-saved-mark (article)
9129   "Set the process mark on ARTICLE and update the summary line."
9130   (push article gnus-newsgroup-saved)
9131   (when (gnus-summary-goto-subject article)
9132     (gnus-summary-update-secondary-mark article)))
9133
9134 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9135   "Mark N articles as read forwards.
9136 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9137 The difference between N and the actual number of articles marked is
9138 returned.
9139 Iff NO-EXPIRE, auto-expiry will be inhibited."
9140   (interactive "p")
9141   (gnus-summary-show-thread)
9142   (let ((backward (< n 0))
9143         (gnus-summary-goto-unread
9144          (and gnus-summary-goto-unread
9145               (not (eq gnus-summary-goto-unread 'never))
9146               (not (memq mark (list gnus-unread-mark
9147                                     gnus-ticked-mark gnus-dormant-mark)))))
9148         (n (abs n))
9149         (mark (or mark gnus-del-mark)))
9150     (while (and (> n 0)
9151                 (gnus-summary-mark-article nil mark no-expire)
9152                 (zerop (gnus-summary-next-subject
9153                         (if backward -1 1)
9154                         (and gnus-summary-goto-unread
9155                              (not (eq gnus-summary-goto-unread 'never)))
9156                         t)))
9157       (setq n (1- n)))
9158     (when (/= 0 n)
9159       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9160     (gnus-summary-recenter)
9161     (gnus-summary-position-point)
9162     (gnus-set-mode-line 'summary)
9163     n))
9164
9165 (defun gnus-summary-mark-article-as-read (mark)
9166   "Mark the current article quickly as read with MARK."
9167   (let ((article (gnus-summary-article-number)))
9168     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9169     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9170     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9171     (push (cons article mark) gnus-newsgroup-reads)
9172     ;; Possibly remove from cache, if that is used.
9173     (when gnus-use-cache
9174       (gnus-cache-enter-remove-article article))
9175     ;; Allow the backend to change the mark.
9176     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9177     ;; Check for auto-expiry.
9178     (when (and gnus-newsgroup-auto-expire
9179                (memq mark gnus-auto-expirable-marks))
9180       (setq mark gnus-expirable-mark)
9181       ;; Let the backend know about the mark change.
9182       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9183       (push article gnus-newsgroup-expirable))
9184     ;; Set the mark in the buffer.
9185     (gnus-summary-update-mark mark 'unread)
9186     t))
9187
9188 (defun gnus-summary-mark-article-as-unread (mark)
9189   "Mark the current article quickly as unread with MARK."
9190   (let* ((article (gnus-summary-article-number))
9191          (old-mark (gnus-summary-article-mark article)))
9192     ;; Allow the backend to change the mark.
9193     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9194     (if (eq mark old-mark)
9195         t
9196       (if (<= article 0)
9197           (progn
9198             (gnus-error 1 "Can't mark negative article numbers")
9199             nil)
9200         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9201         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9202         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9203         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9204         (cond ((= mark gnus-ticked-mark)
9205                (setq gnus-newsgroup-marked
9206                      (gnus-add-to-sorted-list gnus-newsgroup-marked 
9207                                               article)))
9208               ((= mark gnus-dormant-mark)
9209                (setq gnus-newsgroup-dormant
9210                      (gnus-add-to-sorted-list gnus-newsgroup-dormant 
9211                                               article)))
9212               (t
9213                (setq gnus-newsgroup-unreads
9214                      (gnus-add-to-sorted-list gnus-newsgroup-unreads 
9215                                               article))))
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     (setq gnus-newsgroup-expirable
9326           (if (= mark gnus-expirable-mark)
9327               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9328             (delq article gnus-newsgroup-expirable)))
9329     ;; Remove from unread and marked lists.
9330     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9331     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9332     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9333     (push (cons article mark) gnus-newsgroup-reads)
9334     ;; Possibly remove from cache, if that is used.
9335     (when gnus-use-cache
9336       (gnus-cache-enter-remove-article article))
9337     t))
9338
9339 (defun gnus-mark-article-as-unread (article &optional mark)
9340   "Enter ARTICLE in the pertinent lists and remove it from others."
9341   (let ((mark (or mark gnus-ticked-mark)))
9342     (if (<= article 0)
9343         (progn
9344           (gnus-error 1 "Can't mark negative article numbers")
9345           nil)
9346       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9347             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9348             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9349             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9350
9351       ;; Unsuppress duplicates?
9352       (when gnus-suppress-duplicates
9353         (gnus-dup-unsuppress-article article))
9354
9355       (cond ((= mark gnus-ticked-mark)
9356              (setq gnus-newsgroup-marked
9357                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9358             ((= mark gnus-dormant-mark)
9359              (setq gnus-newsgroup-dormant
9360                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9361             (t
9362              (setq gnus-newsgroup-unreads
9363                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9364       (gnus-pull article gnus-newsgroup-reads)
9365       t)))
9366
9367 (defalias 'gnus-summary-mark-as-unread-forward
9368   'gnus-summary-tick-article-forward)
9369 (make-obsolete 'gnus-summary-mark-as-unread-forward
9370                'gnus-summary-tick-article-forward)
9371 (defun gnus-summary-tick-article-forward (n)
9372   "Tick N articles forwards.
9373 If N is negative, tick backwards instead.
9374 The difference between N and the number of articles ticked is returned."
9375   (interactive "p")
9376   (gnus-summary-mark-forward n gnus-ticked-mark))
9377
9378 (defalias 'gnus-summary-mark-as-unread-backward
9379   'gnus-summary-tick-article-backward)
9380 (make-obsolete 'gnus-summary-mark-as-unread-backward
9381                'gnus-summary-tick-article-backward)
9382 (defun gnus-summary-tick-article-backward (n)
9383   "Tick N articles backwards.
9384 The difference between N and the number of articles ticked is returned."
9385   (interactive "p")
9386   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9387
9388 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9389 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9390 (defun gnus-summary-tick-article (&optional article clear-mark)
9391   "Mark current article as unread.
9392 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9393 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9394   (interactive)
9395   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9396                                        gnus-ticked-mark)))
9397
9398 (defun gnus-summary-mark-as-read-forward (n)
9399   "Mark N articles as read forwards.
9400 If N is negative, mark backwards instead.
9401 The difference between N and the actual number of articles marked is
9402 returned."
9403   (interactive "p")
9404   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9405
9406 (defun gnus-summary-mark-as-read-backward (n)
9407   "Mark the N articles as read backwards.
9408 The difference between N and the actual number of articles marked is
9409 returned."
9410   (interactive "p")
9411   (gnus-summary-mark-forward
9412    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9413
9414 (defun gnus-summary-mark-as-read (&optional article mark)
9415   "Mark current article as read.
9416 ARTICLE specifies the article to be marked as read.
9417 MARK specifies a string to be inserted at the beginning of the line."
9418   (gnus-summary-mark-article article mark))
9419
9420 (defun gnus-summary-clear-mark-forward (n)
9421   "Clear marks from N articles forward.
9422 If N is negative, clear backward instead.
9423 The difference between N and the number of marks cleared is returned."
9424   (interactive "p")
9425   (gnus-summary-mark-forward n gnus-unread-mark))
9426
9427 (defun gnus-summary-clear-mark-backward (n)
9428   "Clear marks from N articles backward.
9429 The difference between N and the number of marks cleared is returned."
9430   (interactive "p")
9431   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9432
9433 (defun gnus-summary-mark-unread-as-read ()
9434   "Intended to be used by `gnus-summary-mark-article-hook'."
9435   (when (memq gnus-current-article gnus-newsgroup-unreads)
9436     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9437
9438 (defun gnus-summary-mark-read-and-unread-as-read ()
9439   "Intended to be used by `gnus-summary-mark-article-hook'."
9440   (let ((mark (gnus-summary-article-mark)))
9441     (when (or (gnus-unread-mark-p mark)
9442               (gnus-read-mark-p mark))
9443       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9444
9445 (defun gnus-summary-mark-unread-as-ticked ()
9446   "Intended to be used by `gnus-summary-mark-article-hook'."
9447   (when (memq gnus-current-article gnus-newsgroup-unreads)
9448     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9449
9450 (defun gnus-summary-mark-region-as-read (point mark all)
9451   "Mark all unread articles between point and mark as read.
9452 If given a prefix, mark all articles between point and mark as read,
9453 even ticked and dormant ones."
9454   (interactive "r\nP")
9455   (save-excursion
9456     (let (article)
9457       (goto-char point)
9458       (beginning-of-line)
9459       (while (and
9460               (< (point) mark)
9461               (progn
9462                 (when (or all
9463                           (memq (setq article (gnus-summary-article-number))
9464                                 gnus-newsgroup-unreads))
9465                   (gnus-summary-mark-article article gnus-del-mark))
9466                 t)
9467               (gnus-summary-find-next))))))
9468
9469 (defun gnus-summary-mark-below (score mark)
9470   "Mark articles with score less than SCORE with MARK."
9471   (interactive "P\ncMark: ")
9472   (setq score (if score
9473                   (prefix-numeric-value score)
9474                 (or gnus-summary-default-score 0)))
9475   (save-excursion
9476     (set-buffer gnus-summary-buffer)
9477     (goto-char (point-min))
9478     (while
9479         (progn
9480           (and (< (gnus-summary-article-score) score)
9481                (gnus-summary-mark-article nil mark))
9482           (gnus-summary-find-next)))))
9483
9484 (defun gnus-summary-kill-below (&optional score)
9485   "Mark articles with score below SCORE as read."
9486   (interactive "P")
9487   (gnus-summary-mark-below score gnus-killed-mark))
9488
9489 (defun gnus-summary-clear-above (&optional score)
9490   "Clear all marks from articles with score above SCORE."
9491   (interactive "P")
9492   (gnus-summary-mark-above score gnus-unread-mark))
9493
9494 (defun gnus-summary-tick-above (&optional score)
9495   "Tick all articles with score above SCORE."
9496   (interactive "P")
9497   (gnus-summary-mark-above score gnus-ticked-mark))
9498
9499 (defun gnus-summary-mark-above (score mark)
9500   "Mark articles with score over SCORE with MARK."
9501   (interactive "P\ncMark: ")
9502   (setq score (if score
9503                   (prefix-numeric-value score)
9504                 (or gnus-summary-default-score 0)))
9505   (save-excursion
9506     (set-buffer gnus-summary-buffer)
9507     (goto-char (point-min))
9508     (while (and (progn
9509                   (when (> (gnus-summary-article-score) score)
9510                     (gnus-summary-mark-article nil mark))
9511                   t)
9512                 (gnus-summary-find-next)))))
9513
9514 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9515 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9516 (defun gnus-summary-limit-include-expunged (&optional no-error)
9517   "Display all the hidden articles that were expunged for low scores."
9518   (interactive)
9519   (let ((buffer-read-only nil))
9520     (let ((scored gnus-newsgroup-scored)
9521           headers h)
9522       (while scored
9523         (unless (gnus-summary-article-header (caar scored))
9524           (and (setq h (gnus-number-to-header (caar scored)))
9525                (< (cdar scored) gnus-summary-expunge-below)
9526                (push h headers)))
9527         (setq scored (cdr scored)))
9528       (if (not headers)
9529           (when (not no-error)
9530             (error "No expunged articles hidden"))
9531         (goto-char (point-min))
9532         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9533         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9534         (mapcar (lambda (x) (push (mail-header-number x)
9535                                   gnus-newsgroup-limit))
9536                 headers)
9537         (gnus-summary-prepare-unthreaded (nreverse headers))
9538         (goto-char (point-min))
9539         (gnus-summary-position-point)
9540         t))))
9541
9542 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9543   "Mark all unread articles in this newsgroup as read.
9544 If prefix argument ALL is non-nil, ticked and dormant articles will
9545 also be marked as read.
9546 If QUIETLY is non-nil, no questions will be asked.
9547 If TO-HERE is non-nil, it should be a point in the buffer.  All
9548 articles before (after, if REVERSE is set) this point will be marked as read.
9549 Note that this function will only catch up the unread article
9550 in the current summary buffer limitation.
9551 The number of articles marked as read is returned."
9552   (interactive "P")
9553   (prog1
9554       (save-excursion
9555         (when (or quietly
9556                   (not gnus-interactive-catchup) ;Without confirmation?
9557                   gnus-expert-user
9558                   (gnus-y-or-n-p
9559                    (if all
9560                        "Mark absolutely all articles as read? "
9561                      "Mark all unread articles as read? ")))
9562           (if (and not-mark
9563                    (not gnus-newsgroup-adaptive)
9564                    (not gnus-newsgroup-auto-expire)
9565                    (not gnus-suppress-duplicates)
9566                    (or (not gnus-use-cache)
9567                        (eq gnus-use-cache 'passive)))
9568               (progn
9569                 (when all
9570                   (setq gnus-newsgroup-marked nil
9571                         gnus-newsgroup-dormant nil))
9572                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9573             ;; We actually mark all articles as canceled, which we
9574             ;; have to do when using auto-expiry or adaptive scoring.
9575             (gnus-summary-show-all-threads)
9576             (if (and to-here reverse)
9577                 (progn
9578                   (goto-char to-here)
9579                   (while (and
9580                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9581                           (gnus-summary-find-next (not all) nil nil t))))
9582               (when (gnus-summary-first-subject (not all) t)
9583                 (while (and
9584                         (if to-here (< (point) to-here) t)
9585                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9586                         (gnus-summary-find-next (not all) nil nil t)))))
9587             (gnus-set-mode-line 'summary))
9588           t))
9589     (gnus-summary-position-point)))
9590
9591 (defun gnus-summary-catchup-to-here (&optional all)
9592   "Mark all unticked articles before the current one as read.
9593 If ALL is non-nil, also mark ticked and dormant articles as read."
9594   (interactive "P")
9595   (save-excursion
9596     (gnus-save-hidden-threads
9597       (let ((beg (point)))
9598         ;; We check that there are unread articles.
9599         (when (or all (gnus-summary-find-prev))
9600           (gnus-summary-catchup all t beg)))))
9601   (gnus-summary-position-point))
9602
9603 (defun gnus-summary-catchup-from-here (&optional all)
9604   "Mark all unticked articles after the current one as read.
9605 If ALL is non-nil, also mark ticked and dormant articles as read."
9606   (interactive "P")
9607   (save-excursion
9608     (gnus-save-hidden-threads
9609       (let ((beg (point)))
9610         ;; We check that there are unread articles.
9611         (when (or all (gnus-summary-find-next))
9612           (gnus-summary-catchup all t beg nil t)))))
9613
9614   (gnus-summary-position-point))
9615 (defun gnus-summary-catchup-all (&optional quietly)
9616   "Mark all articles in this newsgroup as read."
9617   (interactive "P")
9618   (gnus-summary-catchup t quietly))
9619
9620 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9621   "Mark all unread articles in this group as read, then exit.
9622 If prefix argument ALL is non-nil, all articles are marked as read.
9623 If QUIETLY is non-nil, no questions will be asked."
9624   (interactive "P")
9625   (when (gnus-summary-catchup all quietly nil 'fast)
9626     ;; Select next newsgroup or exit.
9627     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9628              (eq gnus-auto-select-next 'quietly))
9629         (gnus-summary-next-group nil)
9630       (gnus-summary-exit))))
9631
9632 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9633   "Mark all articles in this newsgroup as read, and then exit."
9634   (interactive "P")
9635   (gnus-summary-catchup-and-exit t quietly))
9636
9637 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9638   "Mark all articles in this group as read and select the next group.
9639 If given a prefix, mark all articles, unread as well as ticked, as
9640 read."
9641   (interactive "P")
9642   (save-excursion
9643     (gnus-summary-catchup all))
9644   (gnus-summary-next-group))
9645
9646 ;;;
9647 ;;; with article
9648 ;;;
9649
9650 (defmacro gnus-with-article (article &rest forms)
9651   "Select ARTICLE and perform FORMS in the original article buffer.
9652 Then replace the article with the result."
9653   `(progn
9654      ;; We don't want the article to be marked as read.
9655      (let (gnus-mark-article-hook)
9656        (gnus-summary-select-article t t nil ,article))
9657      (set-buffer gnus-original-article-buffer)
9658      ,@forms
9659      (if (not (gnus-check-backend-function
9660                'request-replace-article (car gnus-article-current)))
9661          (gnus-message 5 "Read-only group; not replacing")
9662        (unless (gnus-request-replace-article
9663                 ,article (car gnus-article-current)
9664                 (current-buffer) t)
9665          (error "Couldn't replace article")))
9666      ;; The cache and backlog have to be flushed somewhat.
9667      (when gnus-keep-backlog
9668        (gnus-backlog-remove-article
9669         (car gnus-article-current) (cdr gnus-article-current)))
9670      (when gnus-use-cache
9671        (gnus-cache-update-article
9672         (car gnus-article-current) (cdr gnus-article-current)))))
9673
9674 (put 'gnus-with-article 'lisp-indent-function 1)
9675 (put 'gnus-with-article 'edebug-form-spec '(form body))
9676
9677 ;; Thread-based commands.
9678
9679 (defun gnus-summary-articles-in-thread (&optional article)
9680   "Return a list of all articles in the current thread.
9681 If ARTICLE is non-nil, return all articles in the thread that starts
9682 with that article."
9683   (let* ((article (or article (gnus-summary-article-number)))
9684          (data (gnus-data-find-list article))
9685          (top-level (gnus-data-level (car data)))
9686          (top-subject
9687           (cond ((null gnus-thread-operation-ignore-subject)
9688                  (gnus-simplify-subject-re
9689                   (mail-header-subject (gnus-data-header (car data)))))
9690                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9691                  (gnus-simplify-subject-fuzzy
9692                   (mail-header-subject (gnus-data-header (car data)))))
9693                 (t nil)))
9694          (end-point (save-excursion
9695                       (if (gnus-summary-go-to-next-thread)
9696                           (point) (point-max))))
9697          articles)
9698     (while (and data
9699                 (< (gnus-data-pos (car data)) end-point))
9700       (when (or (not top-subject)
9701                 (string= top-subject
9702                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9703                              (gnus-simplify-subject-fuzzy
9704                               (mail-header-subject
9705                                (gnus-data-header (car data))))
9706                            (gnus-simplify-subject-re
9707                             (mail-header-subject
9708                              (gnus-data-header (car data)))))))
9709         (push (gnus-data-number (car data)) articles))
9710       (unless (and (setq data (cdr data))
9711                    (> (gnus-data-level (car data)) top-level))
9712         (setq data nil)))
9713     ;; Return the list of articles.
9714     (nreverse articles)))
9715
9716 (defun gnus-summary-rethread-current ()
9717   "Rethread the thread the current article is part of."
9718   (interactive)
9719   (let* ((gnus-show-threads t)
9720          (article (gnus-summary-article-number))
9721          (id (mail-header-id (gnus-summary-article-header)))
9722          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9723     (unless id
9724       (error "No article on the current line"))
9725     (gnus-rebuild-thread id)
9726     (gnus-summary-goto-subject article)))
9727
9728 (defun gnus-summary-reparent-thread ()
9729   "Make the current article child of the marked (or previous) article.
9730
9731 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9732 is non-nil or the Subject: of both articles are the same."
9733   (interactive)
9734   (unless (not (gnus-group-read-only-p))
9735     (error "The current newsgroup does not support article editing"))
9736   (unless (<= (length gnus-newsgroup-processable) 1)
9737     (error "No more than one article may be marked"))
9738   (save-window-excursion
9739     (let ((gnus-article-buffer " *reparent*")
9740           (current-article (gnus-summary-article-number))
9741           ;; First grab the marked article, otherwise one line up.
9742           (parent-article (if (not (null gnus-newsgroup-processable))
9743                               (car gnus-newsgroup-processable)
9744                             (save-excursion
9745                               (if (eq (forward-line -1) 0)
9746                                   (gnus-summary-article-number)
9747                                 (error "Beginning of summary buffer"))))))
9748       (unless (not (eq current-article parent-article))
9749         (error "An article may not be self-referential"))
9750       (let ((message-id (mail-header-id
9751                          (gnus-summary-article-header parent-article))))
9752         (unless (and message-id (not (equal message-id "")))
9753           (error "No message-id in desired parent"))
9754         (gnus-with-article current-article
9755           (save-restriction
9756             (goto-char (point-min))
9757             (message-narrow-to-head)
9758             (if (re-search-forward "^References: " nil t)
9759                 (progn
9760                   (re-search-forward "^[^ \t]" nil t)
9761                   (forward-line -1)
9762                   (end-of-line)
9763                   (insert " " message-id))
9764               (insert "References: " message-id "\n"))))
9765         (set-buffer gnus-summary-buffer)
9766         (gnus-summary-unmark-all-processable)
9767         (gnus-summary-update-article current-article)
9768         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9769             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
9770         (gnus-summary-rethread-current)
9771         (gnus-message 3 "Article %d is now the child of article %d"
9772                       current-article parent-article)))))
9773
9774 (defun gnus-summary-toggle-threads (&optional arg)
9775   "Toggle showing conversation threads.
9776 If ARG is positive number, turn showing conversation threads on."
9777   (interactive "P")
9778   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9779     (setq gnus-show-threads
9780           (if (null arg) (not gnus-show-threads)
9781             (> (prefix-numeric-value arg) 0)))
9782     (gnus-summary-prepare)
9783     (gnus-summary-goto-subject current)
9784     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9785     (gnus-summary-position-point)))
9786
9787 (defun gnus-summary-show-all-threads ()
9788   "Show all threads."
9789   (interactive)
9790   (save-excursion
9791     (let ((buffer-read-only nil))
9792       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9793   (gnus-summary-position-point))
9794
9795 (defun gnus-summary-show-thread ()
9796   "Show thread subtrees.
9797 Returns nil if no thread was there to be shown."
9798   (interactive)
9799   (let ((buffer-read-only nil)
9800         (orig (point))
9801         ;; first goto end then to beg, to have point at beg after let
9802         (end (progn (end-of-line) (point)))
9803         (beg (progn (beginning-of-line) (point))))
9804     (prog1
9805         ;; Any hidden lines here?
9806         (search-forward "\r" end t)
9807       (subst-char-in-region beg end ?\^M ?\n t)
9808       (goto-char orig)
9809       (gnus-summary-position-point))))
9810
9811 (defun gnus-summary-maybe-hide-threads ()
9812   "If requested, hide the threads that should be hidden."
9813   (when (and gnus-show-threads
9814              gnus-thread-hide-subtree)
9815     (gnus-summary-hide-all-threads
9816      (if (or (consp gnus-thread-hide-subtree)
9817              (gnus-functionp gnus-thread-hide-subtree))
9818          (gnus-make-predicate gnus-thread-hide-subtree)
9819        nil))))
9820
9821 ;;; Hiding predicates.
9822
9823 (defun gnus-article-unread-p (header)
9824   (memq (mail-header-number header) gnus-newsgroup-unreads))
9825
9826 (defun gnus-article-unseen-p (header)
9827   (memq (mail-header-number header) gnus-newsgroup-unseen))
9828
9829 (defun gnus-map-articles (predicate articles)
9830   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
9831   (apply 'gnus-or (mapcar predicate
9832                           (mapcar 'gnus-summary-article-header articles))))
9833
9834 (defun gnus-summary-hide-all-threads (&optional predicate)
9835   "Hide all thread subtrees.
9836 If PREDICATE is supplied, threads that satisfy this predicate
9837 will not be hidden."
9838   (interactive)
9839   (save-excursion
9840     (goto-char (point-min))
9841     (let ((end nil))
9842       (while (not end)
9843         (when (or (not predicate)
9844                   (gnus-map-articles
9845                    predicate (gnus-summary-article-children)))
9846             (gnus-summary-hide-thread))
9847         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
9848   (gnus-summary-position-point))
9849
9850 (defun gnus-summary-hide-thread ()
9851   "Hide thread subtrees.
9852 If PREDICATE is supplied, threads that satisfy this predicate
9853 will not be hidden.
9854 Returns nil if no threads were there to be hidden."
9855   (interactive)
9856   (let ((buffer-read-only nil)
9857         (start (point))
9858         (article (gnus-summary-article-number)))
9859     (goto-char start)
9860     ;; Go forward until either the buffer ends or the subthread
9861     ;; ends.
9862     (when (and (not (eobp))
9863                (or (zerop (gnus-summary-next-thread 1 t))
9864                    (goto-char (point-max))))
9865       (prog1
9866           (if (and (> (point) start)
9867                    (search-backward "\n" start t))
9868               (progn
9869                 (subst-char-in-region start (point) ?\n ?\^M)
9870                 (gnus-summary-goto-subject article))
9871             (goto-char start)
9872             nil)))))
9873
9874 (defun gnus-summary-go-to-next-thread (&optional previous)
9875   "Go to the same level (or less) next thread.
9876 If PREVIOUS is non-nil, go to previous thread instead.
9877 Return the article number moved to, or nil if moving was impossible."
9878   (let ((level (gnus-summary-thread-level))
9879         (way (if previous -1 1))
9880         (beg (point)))
9881     (forward-line way)
9882     (while (and (not (eobp))
9883                 (< level (gnus-summary-thread-level)))
9884       (forward-line way))
9885     (if (eobp)
9886         (progn
9887           (goto-char beg)
9888           nil)
9889       (setq beg (point))
9890       (prog1
9891           (gnus-summary-article-number)
9892         (goto-char beg)))))
9893
9894 (defun gnus-summary-next-thread (n &optional silent)
9895   "Go to the same level next N'th thread.
9896 If N is negative, search backward instead.
9897 Returns the difference between N and the number of skips actually
9898 done.
9899
9900 If SILENT, don't output messages."
9901   (interactive "p")
9902   (let ((backward (< n 0))
9903         (n (abs n)))
9904     (while (and (> n 0)
9905                 (gnus-summary-go-to-next-thread backward))
9906       (decf n))
9907     (unless silent
9908       (gnus-summary-position-point))
9909     (when (and (not silent) (/= 0 n))
9910       (gnus-message 7 "No more threads"))
9911     n))
9912
9913 (defun gnus-summary-prev-thread (n)
9914   "Go to the same level previous N'th thread.
9915 Returns the difference between N and the number of skips actually
9916 done."
9917   (interactive "p")
9918   (gnus-summary-next-thread (- n)))
9919
9920 (defun gnus-summary-go-down-thread ()
9921   "Go down one level in the current thread."
9922   (let ((children (gnus-summary-article-children)))
9923     (when children
9924       (gnus-summary-goto-subject (car children)))))
9925
9926 (defun gnus-summary-go-up-thread ()
9927   "Go up one level in the current thread."
9928   (let ((parent (gnus-summary-article-parent)))
9929     (when parent
9930       (gnus-summary-goto-subject parent))))
9931
9932 (defun gnus-summary-down-thread (n)
9933   "Go down thread N steps.
9934 If N is negative, go up instead.
9935 Returns the difference between N and how many steps down that were
9936 taken."
9937   (interactive "p")
9938   (let ((up (< n 0))
9939         (n (abs n)))
9940     (while (and (> n 0)
9941                 (if up (gnus-summary-go-up-thread)
9942                   (gnus-summary-go-down-thread)))
9943       (setq n (1- n)))
9944     (gnus-summary-position-point)
9945     (when (/= 0 n)
9946       (gnus-message 7 "Can't go further"))
9947     n))
9948
9949 (defun gnus-summary-up-thread (n)
9950   "Go up thread N steps.
9951 If N is negative, go down instead.
9952 Returns the difference between N and how many steps down that were
9953 taken."
9954   (interactive "p")
9955   (gnus-summary-down-thread (- n)))
9956
9957 (defun gnus-summary-top-thread ()
9958   "Go to the top of the thread."
9959   (interactive)
9960   (while (gnus-summary-go-up-thread))
9961   (gnus-summary-article-number))
9962
9963 (defun gnus-summary-kill-thread (&optional unmark)
9964   "Mark articles under current thread as read.
9965 If the prefix argument is positive, remove any kinds of marks.
9966 If the prefix argument is negative, tick articles instead."
9967   (interactive "P")
9968   (when unmark
9969     (setq unmark (prefix-numeric-value unmark)))
9970   (let ((articles (gnus-summary-articles-in-thread)))
9971     (save-excursion
9972       ;; Expand the thread.
9973       (gnus-summary-show-thread)
9974       ;; Mark all the articles.
9975       (while articles
9976         (gnus-summary-goto-subject (car articles))
9977         (cond ((null unmark)
9978                (gnus-summary-mark-article-as-read gnus-killed-mark))
9979               ((> unmark 0)
9980                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9981               (t
9982                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9983         (setq articles (cdr articles))))
9984     ;; Hide killed subtrees.
9985     (and (null unmark)
9986          gnus-thread-hide-killed
9987          (gnus-summary-hide-thread))
9988     ;; If marked as read, go to next unread subject.
9989     (when (null unmark)
9990       ;; Go to next unread subject.
9991       (gnus-summary-next-subject 1 t)))
9992   (gnus-set-mode-line 'summary))
9993
9994 ;; Summary sorting commands
9995
9996 (defun gnus-summary-sort-by-number (&optional reverse)
9997   "Sort the summary buffer by article number.
9998 Argument REVERSE means reverse order."
9999   (interactive "P")
10000   (gnus-summary-sort 'number reverse))
10001
10002 (defun gnus-summary-sort-by-author (&optional reverse)
10003   "Sort the summary buffer by author name alphabetically.
10004 If `case-fold-search' is non-nil, case of letters is ignored.
10005 Argument REVERSE means reverse order."
10006   (interactive "P")
10007   (gnus-summary-sort 'author reverse))
10008
10009 (defun gnus-summary-sort-by-subject (&optional reverse)
10010   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10011 If `case-fold-search' is non-nil, case of letters is ignored.
10012 Argument REVERSE means reverse order."
10013   (interactive "P")
10014   (gnus-summary-sort 'subject reverse))
10015
10016 (defun gnus-summary-sort-by-date (&optional reverse)
10017   "Sort the summary buffer by date.
10018 Argument REVERSE means reverse order."
10019   (interactive "P")
10020   (gnus-summary-sort 'date reverse))
10021
10022 (defun gnus-summary-sort-by-score (&optional reverse)
10023   "Sort the summary buffer by score.
10024 Argument REVERSE means reverse order."
10025   (interactive "P")
10026   (gnus-summary-sort 'score reverse))
10027
10028 (defun gnus-summary-sort-by-lines (&optional reverse)
10029   "Sort the summary buffer by the number of lines.
10030 Argument REVERSE means reverse order."
10031   (interactive "P")
10032   (gnus-summary-sort 'lines reverse))
10033
10034 (defun gnus-summary-sort-by-chars (&optional reverse)
10035   "Sort the summary buffer by article length.
10036 Argument REVERSE means reverse order."
10037   (interactive "P")
10038   (gnus-summary-sort 'chars reverse))
10039
10040 (defun gnus-summary-sort-by-original (&optional reverse)
10041   "Sort the summary buffer using the default sorting method.
10042 Argument REVERSE means reverse order."
10043   (interactive "P")
10044   (let* ((buffer-read-only)
10045          (gnus-summary-prepare-hook nil))
10046     ;; We do the sorting by regenerating the threads.
10047     (gnus-summary-prepare)
10048     ;; Hide subthreads if needed.
10049     (gnus-summary-maybe-hide-threads)))
10050
10051 (defun gnus-summary-sort (predicate reverse)
10052   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10053   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10054          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10055          (gnus-thread-sort-functions
10056           (if (not reverse)
10057               thread
10058             `(lambda (t1 t2)
10059                (,thread t2 t1))))
10060          (gnus-sort-gathered-threads-function
10061           gnus-thread-sort-functions)
10062          (gnus-article-sort-functions
10063           (if (not reverse)
10064               article
10065             `(lambda (t1 t2)
10066                (,article t2 t1))))
10067          (buffer-read-only)
10068          (gnus-summary-prepare-hook nil))
10069     ;; We do the sorting by regenerating the threads.
10070     (gnus-summary-prepare)
10071     ;; Hide subthreads if needed.
10072     (gnus-summary-maybe-hide-threads)))
10073
10074 ;; Summary saving commands.
10075
10076 (defun gnus-summary-save-article (&optional n not-saved)
10077   "Save the current article using the default saver function.
10078 If N is a positive number, save the N next articles.
10079 If N is a negative number, save the N previous articles.
10080 If N is nil and any articles have been marked with the process mark,
10081 save those articles instead.
10082 The variable `gnus-default-article-saver' specifies the saver function."
10083   (interactive "P")
10084   (let* ((articles (gnus-summary-work-articles n))
10085          (save-buffer (save-excursion
10086                         (nnheader-set-temp-buffer " *Gnus Save*")))
10087          (num (length articles))
10088          header file)
10089     (dolist (article articles)
10090       (setq header (gnus-summary-article-header article))
10091       (if (not (vectorp header))
10092           ;; This is a pseudo-article.
10093           (if (assq 'name header)
10094               (gnus-copy-file (cdr (assq 'name header)))
10095             (gnus-message 1 "Article %d is unsaveable" article))
10096         ;; This is a real article.
10097         (save-window-excursion
10098           (let ((gnus-display-mime-function nil)
10099                 (gnus-article-prepare-hook nil))
10100             (gnus-summary-select-article t nil nil article)))
10101         (save-excursion
10102           (set-buffer save-buffer)
10103           (erase-buffer)
10104           (insert-buffer-substring gnus-original-article-buffer))
10105         (setq file (gnus-article-save save-buffer file num))
10106         (gnus-summary-remove-process-mark article)
10107         (unless not-saved
10108           (gnus-summary-set-saved-mark article))))
10109     (gnus-kill-buffer save-buffer)
10110     (gnus-summary-position-point)
10111     (gnus-set-mode-line 'summary)
10112     n))
10113
10114 (defun gnus-summary-pipe-output (&optional arg)
10115   "Pipe the current article to a subprocess.
10116 If N is a positive number, pipe the N next articles.
10117 If N is a negative number, pipe the N previous articles.
10118 If N is nil and any articles have been marked with the process mark,
10119 pipe those articles instead."
10120   (interactive "P")
10121   (require 'gnus-art)
10122   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
10123     (gnus-summary-save-article arg t))
10124   (let ((buffer (get-buffer "*Shell Command Output*")))
10125     (if (and buffer
10126              (with-current-buffer buffer (> (point-max) (point-min))))
10127         (gnus-configure-windows 'pipe))))
10128
10129 (defun gnus-summary-save-article-mail (&optional arg)
10130   "Append the current article to an mail file.
10131 If N is a positive number, save the N next articles.
10132 If N is a negative number, save the N previous articles.
10133 If N is nil and any articles have been marked with the process mark,
10134 save those articles instead."
10135   (interactive "P")
10136   (require 'gnus-art)
10137   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10138     (gnus-summary-save-article arg)))
10139
10140 (defun gnus-summary-save-article-rmail (&optional arg)
10141   "Append the current article to an rmail file.
10142 If N is a positive number, save the N next articles.
10143 If N is a negative number, save the N previous articles.
10144 If N is nil and any articles have been marked with the process mark,
10145 save those articles instead."
10146   (interactive "P")
10147   (require 'gnus-art)
10148   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10149     (gnus-summary-save-article arg)))
10150
10151 (defun gnus-summary-save-article-file (&optional arg)
10152   "Append the current article to a file.
10153 If N is a positive number, save the N next articles.
10154 If N is a negative number, save the N previous articles.
10155 If N is nil and any articles have been marked with the process mark,
10156 save those articles instead."
10157   (interactive "P")
10158   (require 'gnus-art)
10159   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10160     (gnus-summary-save-article arg)))
10161
10162 (defun gnus-summary-write-article-file (&optional arg)
10163   "Write the current article to a file, deleting the previous file.
10164 If N is a positive number, save the N next articles.
10165 If N is a negative number, save the N previous articles.
10166 If N is nil and any articles have been marked with the process mark,
10167 save those articles instead."
10168   (interactive "P")
10169   (require 'gnus-art)
10170   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10171     (gnus-summary-save-article arg)))
10172
10173 (defun gnus-summary-save-article-body-file (&optional arg)
10174   "Append the current article body to a file.
10175 If N is a positive number, save the N next articles.
10176 If N is a negative number, save the N previous articles.
10177 If N is nil and any articles have been marked with the process mark,
10178 save those articles instead."
10179   (interactive "P")
10180   (require 'gnus-art)
10181   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10182     (gnus-summary-save-article arg)))
10183
10184 (defun gnus-summary-muttprint (&optional arg)
10185   "Print the current article using Muttprint.
10186 If N is a positive number, save the N next articles.
10187 If N is a negative number, save the N previous articles.
10188 If N is nil and any articles have been marked with the process mark,
10189 save those articles instead."
10190   (interactive "P")
10191   (require 'gnus-art)
10192   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10193     (gnus-summary-save-article arg t)))
10194
10195 (defun gnus-summary-pipe-message (program)
10196   "Pipe the current article through PROGRAM."
10197   (interactive "sProgram: ")
10198   (gnus-summary-select-article)
10199   (let ((mail-header-separator ""))
10200     (gnus-eval-in-buffer-window gnus-article-buffer
10201       (save-restriction
10202         (widen)
10203         (let ((start (window-start))
10204               buffer-read-only)
10205           (message-pipe-buffer-body program)
10206           (set-window-start (get-buffer-window (current-buffer)) start))))))
10207
10208 (defun gnus-get-split-value (methods)
10209   "Return a value based on the split METHODS."
10210   (let (split-name method result match)
10211     (when methods
10212       (save-excursion
10213         (set-buffer gnus-original-article-buffer)
10214         (save-restriction
10215           (nnheader-narrow-to-headers)
10216           (while (and methods (not split-name))
10217             (goto-char (point-min))
10218             (setq method (pop methods))
10219             (setq match (car method))
10220             (when (cond
10221                    ((stringp match)
10222                     ;; Regular expression.
10223                     (ignore-errors
10224                       (re-search-forward match nil t)))
10225                    ((gnus-functionp match)
10226                     ;; Function.
10227                     (save-restriction
10228                       (widen)
10229                       (setq result (funcall match gnus-newsgroup-name))))
10230                    ((consp match)
10231                     ;; Form.
10232                     (save-restriction
10233                       (widen)
10234                       (setq result (eval match)))))
10235               (setq split-name (cdr method))
10236               (cond ((stringp result)
10237                      (push (expand-file-name
10238                             result gnus-article-save-directory)
10239                            split-name))
10240                     ((consp result)
10241                      (setq split-name (append result split-name)))))))))
10242     (nreverse split-name)))
10243
10244 (defun gnus-valid-move-group-p (group)
10245   (and (boundp group)
10246        (symbol-name group)
10247        (symbol-value group)
10248        (gnus-get-function (gnus-find-method-for-group
10249                            (symbol-name group)) 'request-accept-article t)))
10250
10251 (defun gnus-read-move-group-name (prompt default articles prefix)
10252   "Read a group name."
10253   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10254          (minibuffer-confirm-incomplete nil) ; XEmacs
10255          (prom
10256           (format "%s %s to:"
10257                   prompt
10258                   (if (> (length articles) 1)
10259                       (format "these %d articles" (length articles))
10260                     "this article")))
10261          (to-newsgroup
10262           (cond
10263            ((null split-name)
10264             (gnus-completing-read-with-default
10265              default prom
10266              gnus-active-hashtb
10267              'gnus-valid-move-group-p
10268              nil prefix
10269              'gnus-group-history))
10270            ((= 1 (length split-name))
10271             (gnus-completing-read-with-default
10272              (car split-name) prom
10273              gnus-active-hashtb
10274              'gnus-valid-move-group-p
10275              nil nil
10276              'gnus-group-history))
10277            (t
10278             (gnus-completing-read-with-default
10279              nil prom
10280              (mapcar (lambda (el) (list el))
10281                      (nreverse split-name))
10282              nil nil nil
10283              'gnus-group-history))))
10284          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10285     (when to-newsgroup
10286       (if (or (string= to-newsgroup "")
10287               (string= to-newsgroup prefix))
10288           (setq to-newsgroup default))
10289       (unless to-newsgroup
10290         (error "No group name entered"))
10291       (or (gnus-active to-newsgroup)
10292           (gnus-activate-group to-newsgroup nil nil to-method)
10293           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10294                                      to-newsgroup))
10295               (or (and (gnus-request-create-group to-newsgroup to-method)
10296                        (gnus-activate-group
10297                         to-newsgroup nil nil to-method)
10298                        (gnus-subscribe-group to-newsgroup))
10299                   (error "Couldn't create group %s" to-newsgroup)))
10300           (error "No such group: %s" to-newsgroup)))
10301     to-newsgroup))
10302
10303 (defun gnus-summary-save-parts (type dir n &optional reverse)
10304   "Save parts matching TYPE to DIR.
10305 If REVERSE, save parts that do not match TYPE."
10306   (interactive
10307    (list (read-string "Save parts of type: "
10308                       (or (car gnus-summary-save-parts-type-history)
10309                           gnus-summary-save-parts-default-mime)
10310                       'gnus-summary-save-parts-type-history)
10311          (setq gnus-summary-save-parts-last-directory
10312                (read-file-name "Save to directory: "
10313                                gnus-summary-save-parts-last-directory
10314                                nil t))
10315          current-prefix-arg))
10316   (gnus-summary-iterate n
10317     (let ((gnus-display-mime-function nil)
10318           (gnus-inhibit-treatment t))
10319       (gnus-summary-select-article))
10320     (save-excursion
10321       (set-buffer gnus-article-buffer)
10322       (let ((handles (or gnus-article-mime-handles
10323                          (mm-dissect-buffer
10324                           gnus-article-no-strict-mime)
10325                          (mm-uu-dissect))))
10326         (when handles
10327           (gnus-summary-save-parts-1 type dir handles reverse)
10328           (unless gnus-article-mime-handles ;; Don't destroy this case.
10329             (mm-destroy-parts handles)))))))
10330
10331 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10332   (if (stringp (car handle))
10333       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10334               (cdr handle))
10335     (when (if reverse
10336               (not (string-match type (mm-handle-media-type handle)))
10337             (string-match type (mm-handle-media-type handle)))
10338       (let ((file (expand-file-name
10339                    (file-name-nondirectory
10340                     (or
10341                      (mail-content-type-get
10342                       (mm-handle-disposition handle) 'filename)
10343                      (concat gnus-newsgroup-name
10344                              "." (number-to-string
10345                                   (cdr gnus-article-current)))))
10346                    dir)))
10347         (unless (file-exists-p file)
10348           (mm-save-part-to-file handle file))))))
10349
10350 ;; Summary extract commands
10351
10352 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10353   (let ((buffer-read-only nil)
10354         (article (gnus-summary-article-number))
10355         after-article b e)
10356     (unless (gnus-summary-goto-subject article)
10357       (error "No such article: %d" article))
10358     (gnus-summary-position-point)
10359     ;; If all commands are to be bunched up on one line, we collect
10360     ;; them here.
10361     (unless gnus-view-pseudos-separately
10362       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10363             files action)
10364         (while ps
10365           (setq action (cdr (assq 'action (car ps))))
10366           (setq files (list (cdr (assq 'name (car ps)))))
10367           (while (and ps (cdr ps)
10368                       (string= (or action "1")
10369                                (or (cdr (assq 'action (cadr ps))) "2")))
10370             (push (cdr (assq 'name (cadr ps))) files)
10371             (setcdr ps (cddr ps)))
10372           (when files
10373             (when (not (string-match "%s" action))
10374               (push " " files))
10375             (push " " files)
10376             (when (assq 'execute (car ps))
10377               (setcdr (assq 'execute (car ps))
10378                       (funcall (if (string-match "%s" action)
10379                                    'format 'concat)
10380                                action
10381                                (mapconcat
10382                                 (lambda (f)
10383                                   (if (equal f " ")
10384                                       f
10385                                     (mm-quote-arg f)))
10386                                 files " ")))))
10387           (setq ps (cdr ps)))))
10388     (if (and gnus-view-pseudos (not not-view))
10389         (while pslist
10390           (when (assq 'execute (car pslist))
10391             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10392                                   (eq gnus-view-pseudos 'not-confirm)))
10393           (setq pslist (cdr pslist)))
10394       (save-excursion
10395         (while pslist
10396           (setq after-article (or (cdr (assq 'article (car pslist)))
10397                                   (gnus-summary-article-number)))
10398           (gnus-summary-goto-subject after-article)
10399           (forward-line 1)
10400           (setq b (point))
10401           (insert "    " (file-name-nondirectory
10402                           (cdr (assq 'name (car pslist))))
10403                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10404           (setq e (point))
10405           (forward-line -1)             ; back to `b'
10406           (gnus-add-text-properties
10407            b (1- e) (list 'gnus-number gnus-reffed-article-number
10408                           gnus-mouse-face-prop gnus-mouse-face))
10409           (gnus-data-enter
10410            after-article gnus-reffed-article-number
10411            gnus-unread-mark b (car pslist) 0 (- e b))
10412           (setq gnus-newsgroup-unreads
10413                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10414                                          gnus-reffed-article-number))
10415           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10416           (setq pslist (cdr pslist)))))))
10417
10418 (defun gnus-pseudos< (p1 p2)
10419   (let ((c1 (cdr (assq 'action p1)))
10420         (c2 (cdr (assq 'action p2))))
10421     (and c1 c2 (string< c1 c2))))
10422
10423 (defun gnus-request-pseudo-article (props)
10424   (cond ((assq 'execute props)
10425          (gnus-execute-command (cdr (assq 'execute props)))))
10426   (let ((gnus-current-article (gnus-summary-article-number)))
10427     (gnus-run-hooks 'gnus-mark-article-hook)))
10428
10429 (defun gnus-execute-command (command &optional automatic)
10430   (save-excursion
10431     (gnus-article-setup-buffer)
10432     (set-buffer gnus-article-buffer)
10433     (setq buffer-read-only nil)
10434     (let ((command (if automatic command
10435                      (read-string "Command: " (cons command 0)))))
10436       (erase-buffer)
10437       (insert "$ " command "\n\n")
10438       (if gnus-view-pseudo-asynchronously
10439           (start-process "gnus-execute" (current-buffer) shell-file-name
10440                          shell-command-switch command)
10441         (call-process shell-file-name nil t nil
10442                       shell-command-switch command)))))
10443
10444 ;; Summary kill commands.
10445
10446 (defun gnus-summary-edit-global-kill (article)
10447   "Edit the \"global\" kill file."
10448   (interactive (list (gnus-summary-article-number)))
10449   (gnus-group-edit-global-kill article))
10450
10451 (defun gnus-summary-edit-local-kill ()
10452   "Edit a local kill file applied to the current newsgroup."
10453   (interactive)
10454   (setq gnus-current-headers (gnus-summary-article-header))
10455   (gnus-group-edit-local-kill
10456    (gnus-summary-article-number) gnus-newsgroup-name))
10457
10458 ;;; Header reading.
10459
10460 (defun gnus-read-header (id &optional header)
10461   "Read the headers of article ID and enter them into the Gnus system."
10462   (let ((group gnus-newsgroup-name)
10463         (gnus-override-method
10464          (or
10465           gnus-override-method
10466           (and (gnus-news-group-p gnus-newsgroup-name)
10467                (car (gnus-refer-article-methods)))))
10468         where)
10469     ;; First we check to see whether the header in question is already
10470     ;; fetched.
10471     (if (stringp id)
10472         ;; This is a Message-ID.
10473         (setq header (or header (gnus-id-to-header id)))
10474       ;; This is an article number.
10475       (setq header (or header (gnus-summary-article-header id))))
10476     (if (and header
10477              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10478         ;; We have found the header.
10479         header
10480       ;; If this is a sparse article, we have to nix out its
10481       ;; previous entry in the thread hashtb.
10482       (when (and header
10483                  (gnus-summary-article-sparse-p (mail-header-number header)))
10484         (let* ((parent (gnus-parent-id (mail-header-references header)))
10485                (thread (and parent (gnus-id-to-thread parent))))
10486           (when thread
10487             (delq (assq header thread) thread))))
10488       ;; We have to really fetch the header to this article.
10489       (save-excursion
10490         (set-buffer nntp-server-buffer)
10491         (when (setq where (gnus-request-head id group))
10492           (nnheader-fold-continuation-lines)
10493           (goto-char (point-max))
10494           (insert ".\n")
10495           (goto-char (point-min))
10496           (insert "211 ")
10497           (princ (cond
10498                   ((numberp id) id)
10499                   ((cdr where) (cdr where))
10500                   (header (mail-header-number header))
10501                   (t gnus-reffed-article-number))
10502                  (current-buffer))
10503           (insert " Article retrieved.\n"))
10504         (if (or (not where)
10505                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10506             ()                          ; Malformed head.
10507           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10508             (when (and (stringp id)
10509                        (not (string= (gnus-group-real-name group)
10510                                      (car where))))
10511               ;; If we fetched by Message-ID and the article came
10512               ;; from a different group, we fudge some bogus article
10513               ;; numbers for this article.
10514               (mail-header-set-number header gnus-reffed-article-number))
10515             (save-excursion
10516               (set-buffer gnus-summary-buffer)
10517               (decf gnus-reffed-article-number)
10518               (gnus-remove-header (mail-header-number header))
10519               (push header gnus-newsgroup-headers)
10520               (setq gnus-current-headers header)
10521               (push (mail-header-number header) gnus-newsgroup-limit)))
10522           header)))))
10523
10524 (defun gnus-remove-header (number)
10525   "Remove header NUMBER from `gnus-newsgroup-headers'."
10526   (if (and gnus-newsgroup-headers
10527            (= number (mail-header-number (car gnus-newsgroup-headers))))
10528       (pop gnus-newsgroup-headers)
10529     (let ((headers gnus-newsgroup-headers))
10530       (while (and (cdr headers)
10531                   (not (= number (mail-header-number (cadr headers)))))
10532         (pop headers))
10533       (when (cdr headers)
10534         (setcdr headers (cddr headers))))))
10535
10536 ;;;
10537 ;;; summary highlights
10538 ;;;
10539
10540 (defun gnus-highlight-selected-summary ()
10541   "Highlight selected article in summary buffer."
10542   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10543   (when gnus-summary-selected-face
10544     (save-excursion
10545       (let* ((beg (progn (beginning-of-line) (point)))
10546              (end (progn (end-of-line) (point)))
10547              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10548              (from (if (get-text-property beg gnus-mouse-face-prop)
10549                        beg
10550                      (or (next-single-property-change
10551                           beg gnus-mouse-face-prop nil end)
10552                          beg)))
10553              (to
10554               (if (= from end)
10555                   (- from 2)
10556                 (or (next-single-property-change
10557                      from gnus-mouse-face-prop nil end)
10558                     end))))
10559         ;; If no mouse-face prop on line we will have to = from = end,
10560         ;; so we highlight the entire line instead.
10561         (when (= (+ to 2) from)
10562           (setq from beg)
10563           (setq to end))
10564         (if gnus-newsgroup-selected-overlay
10565             ;; Move old overlay.
10566             (gnus-move-overlay
10567              gnus-newsgroup-selected-overlay from to (current-buffer))
10568           ;; Create new overlay.
10569           (gnus-overlay-put
10570            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10571            'face gnus-summary-selected-face))))))
10572
10573 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10574 (defun gnus-summary-highlight-line ()
10575   "Highlight current line according to `gnus-summary-highlight'."
10576   (let* ((list gnus-summary-highlight)
10577          (p (point))
10578          (end (progn (end-of-line) (point)))
10579          ;; now find out where the line starts and leave point there.
10580          (beg (progn (beginning-of-line) (point)))
10581          (article (gnus-summary-article-number))
10582          (score (or (cdr (assq (or article gnus-current-article)
10583                                gnus-newsgroup-scored))
10584                     gnus-summary-default-score 0))
10585          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10586          (inhibit-read-only t))
10587     ;; Eval the cars of the lists until we find a match.
10588     (let ((default gnus-summary-default-score)
10589           (default-high gnus-summary-default-high-score)
10590           (default-low gnus-summary-default-low-score))
10591       (while (and list
10592                   (not (eval (caar list))))
10593         (setq list (cdr list))))
10594     (let ((face (cdar list)))
10595       (unless (eq face (get-text-property beg 'face))
10596         (gnus-put-text-property-excluding-characters-with-faces
10597          beg end 'face
10598          (setq face (if (boundp face) (symbol-value face) face)))
10599         (when gnus-summary-highlight-line-function
10600           (funcall gnus-summary-highlight-line-function article face))))
10601     (goto-char p)))
10602
10603 (defun gnus-update-read-articles (group unread &optional compute)
10604   "Update the list of read articles in GROUP.
10605 UNREAD is a sorted list."
10606   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10607          (entry (gnus-gethash group gnus-newsrc-hashtb))
10608          (info (nth 2 entry))
10609          (prev 1)
10610          read)
10611     (if (or (not info) (not active))
10612         ;; There is no info on this group if it was, in fact,
10613         ;; killed.  Gnus stores no information on killed groups, so
10614         ;; there's nothing to be done.
10615         ;; One could store the information somewhere temporarily,
10616         ;; perhaps...  Hmmm...
10617         ()
10618       ;; Remove any negative articles numbers.
10619       (while (and unread (< (car unread) 0))
10620         (setq unread (cdr unread)))
10621       ;; Remove any expired article numbers
10622       (while (and unread (< (car unread) (car active)))
10623         (setq unread (cdr unread)))
10624       ;; Compute the ranges of read articles by looking at the list of
10625       ;; unread articles.
10626       (while unread
10627         (when (/= (car unread) prev)
10628           (push (if (= prev (1- (car unread))) prev
10629                   (cons prev (1- (car unread))))
10630                 read))
10631         (setq prev (1+ (car unread)))
10632         (setq unread (cdr unread)))
10633       (when (<= prev (cdr active))
10634         (push (cons prev (cdr active)) read))
10635       (setq read (if (> (length read) 1) (nreverse read) read))
10636       (if compute
10637           read
10638         (save-excursion
10639           (let (setmarkundo)
10640             ;; Propagate the read marks to the backend.
10641             (when (gnus-check-backend-function 'request-set-mark group)
10642               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10643                     (add (gnus-remove-from-range read (gnus-info-read info))))
10644                 (when (or add del)
10645                   (unless (gnus-check-group group)
10646                     (error "Can't open server for %s" group))
10647                   (gnus-request-set-mark
10648                    group (delq nil (list (if add (list add 'add '(read)))
10649                                          (if del (list del 'del '(read))))))
10650                   (setq setmarkundo
10651                         `(gnus-request-set-mark
10652                           ,group
10653                           ',(delq nil (list
10654                                        (if del (list del 'add '(read)))
10655                                        (if add (list add 'del '(read))))))))))
10656             (set-buffer gnus-group-buffer)
10657             (gnus-undo-register
10658               `(progn
10659                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10660                  (gnus-info-set-read ',info ',(gnus-info-read info))
10661                  (gnus-get-unread-articles-in-group ',info
10662                                                     (gnus-active ,group))
10663                  (gnus-group-update-group ,group t)
10664                  ,setmarkundo))))
10665         ;; Enter this list into the group info.
10666         (gnus-info-set-read info read)
10667         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10668         (gnus-get-unread-articles-in-group info (gnus-active group))
10669         t))))
10670
10671 (defun gnus-offer-save-summaries ()
10672   "Offer to save all active summary buffers."
10673   (let (buffers)
10674     ;; Go through all buffers and find all summaries.
10675     (dolist (buffer (buffer-list))
10676       (when (and (setq buffer (buffer-name buffer))
10677                  (string-match "Summary" buffer)
10678                  (save-excursion
10679                    (set-buffer buffer)
10680                    ;; We check that this is, indeed, a summary buffer.
10681                    (and (eq major-mode 'gnus-summary-mode)
10682                         ;; Also make sure this isn't bogus.
10683                         gnus-newsgroup-prepared
10684                         ;; Also make sure that this isn't a
10685                         ;; dead summary buffer.
10686                         (not gnus-dead-summary-mode))))
10687         (push buffer buffers)))
10688     ;; Go through all these summary buffers and offer to save them.
10689     (when buffers
10690       (save-excursion
10691         (map-y-or-n-p
10692          "Update summary buffer %s? "
10693          (lambda (buf)
10694            (switch-to-buffer buf)
10695            (gnus-summary-exit))
10696          buffers)))))
10697
10698 (defun gnus-summary-setup-default-charset ()
10699   "Setup newsgroup default charset."
10700   (if (equal gnus-newsgroup-name "nndraft:drafts")
10701       (setq gnus-newsgroup-charset nil)
10702     (let* ((ignored-charsets
10703             (or gnus-newsgroup-ephemeral-ignored-charsets
10704                 (append
10705                  (and gnus-newsgroup-name
10706                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10707                  gnus-newsgroup-ignored-charsets))))
10708       (setq gnus-newsgroup-charset
10709             (or gnus-newsgroup-ephemeral-charset
10710                 (and gnus-newsgroup-name
10711                      (gnus-parameter-charset gnus-newsgroup-name))
10712                 gnus-default-charset))
10713       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10714            ignored-charsets))))
10715
10716 ;;;
10717 ;;; Mime Commands
10718 ;;;
10719
10720 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10721   "Display the current article buffer fully MIME-buttonized.
10722 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10723 treated as multipart/mixed."
10724   (interactive "P")
10725   (require 'gnus-art)
10726   (let ((gnus-unbuttonized-mime-types nil)
10727         (gnus-mime-display-multipart-as-mixed show-all-parts))
10728     (gnus-summary-show-article)))
10729
10730 (defun gnus-summary-repair-multipart (article)
10731   "Add a Content-Type header to a multipart article without one."
10732   (interactive (list (gnus-summary-article-number)))
10733   (gnus-with-article article
10734     (message-narrow-to-head)
10735     (message-remove-header "Mime-Version")
10736     (goto-char (point-max))
10737     (insert "Mime-Version: 1.0\n")
10738     (widen)
10739     (when (search-forward "\n--" nil t)
10740       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10741         (message-narrow-to-head)
10742         (message-remove-header "Content-Type")
10743         (goto-char (point-max))
10744         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10745                         separator))
10746         (widen))))
10747   (let (gnus-mark-article-hook)
10748     (gnus-summary-select-article t t nil article)))
10749
10750 (defun gnus-summary-toggle-display-buttonized ()
10751   "Toggle the buttonizing of the article buffer."
10752   (interactive)
10753   (require 'gnus-art)
10754   (if (setq gnus-inhibit-mime-unbuttonizing
10755             (not gnus-inhibit-mime-unbuttonizing))
10756       (let ((gnus-unbuttonized-mime-types nil))
10757         (gnus-summary-show-article))
10758     (gnus-summary-show-article)))
10759
10760 ;;;
10761 ;;; Generic summary marking commands
10762 ;;;
10763
10764 (defvar gnus-summary-marking-alist
10765   '((read gnus-del-mark "d")
10766     (unread gnus-unread-mark "u")
10767     (ticked gnus-ticked-mark "!")
10768     (dormant gnus-dormant-mark "?")
10769     (expirable gnus-expirable-mark "e"))
10770   "An alist of names/marks/keystrokes.")
10771
10772 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10773 (defvar gnus-summary-mark-map)
10774
10775 (defun gnus-summary-make-all-marking-commands ()
10776   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10777   (dolist (elem gnus-summary-marking-alist)
10778     (apply 'gnus-summary-make-marking-command elem)))
10779
10780 (defun gnus-summary-make-marking-command (name mark keystroke)
10781   (let ((map (make-sparse-keymap)))
10782     (define-key gnus-summary-generic-mark-map keystroke map)
10783     (dolist (lway `((next "next" next nil "n")
10784                     (next-unread "next unread" next t "N")
10785                     (prev "previous" prev nil "p")
10786                     (prev-unread "previous unread" prev t "P")
10787                     (nomove "" nil nil ,keystroke)))
10788       (let ((func (gnus-summary-make-marking-command-1
10789                    mark (car lway) lway name)))
10790         (setq func (eval func))
10791         (define-key map (nth 4 lway) func)))))
10792
10793 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10794   `(defun ,(intern
10795             (format "gnus-summary-put-mark-as-%s%s"
10796                     name (if (eq way 'nomove)
10797                              ""
10798                            (concat "-" (symbol-name way)))))
10799      (n)
10800      ,(format
10801        "Mark the current article as %s%s.
10802 If N, the prefix, then repeat N times.
10803 If N is negative, move in reverse order.
10804 The difference between N and the actual number of articles marked is
10805 returned."
10806        name (cadr lway))
10807      (interactive "p")
10808      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10809
10810 (defun gnus-summary-generic-mark (n mark move unread)
10811   "Mark N articles with MARK."
10812   (unless (eq major-mode 'gnus-summary-mode)
10813     (error "This command can only be used in the summary buffer"))
10814   (gnus-summary-show-thread)
10815   (let ((nummove
10816          (cond
10817           ((eq move 'next) 1)
10818           ((eq move 'prev) -1)
10819           (t 0))))
10820     (if (zerop nummove)
10821         (setq n 1)
10822       (when (< n 0)
10823         (setq n (abs n)
10824               nummove (* -1 nummove))))
10825     (while (and (> n 0)
10826                 (gnus-summary-mark-article nil mark)
10827                 (zerop (gnus-summary-next-subject nummove unread t)))
10828       (setq n (1- n)))
10829     (when (/= 0 n)
10830       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10831     (gnus-summary-recenter)
10832     (gnus-summary-position-point)
10833     (gnus-set-mode-line 'summary)
10834     n))
10835
10836 (defun gnus-summary-insert-articles (articles)
10837   (when (setq articles
10838               (gnus-sorted-difference articles
10839                                       (mapcar (lambda (h) 
10840                                                 (mail-header-number h))
10841                                               gnus-newsgroup-headers)))
10842     (setq gnus-newsgroup-headers
10843           (merge 'list
10844                  gnus-newsgroup-headers
10845                  (gnus-fetch-headers articles)
10846                  'gnus-article-sort-by-number))
10847     ;; Suppress duplicates?
10848     (when gnus-suppress-duplicates
10849       (gnus-dup-suppress-articles))
10850
10851     ;; We might want to build some more threads first.
10852     (when (and gnus-fetch-old-headers
10853                (eq gnus-headers-retrieved-by 'nov))
10854       (if (eq gnus-fetch-old-headers 'invisible)
10855           (gnus-build-all-threads)
10856         (gnus-build-old-threads)))
10857     ;; Let the Gnus agent mark articles as read.
10858     (when gnus-agent
10859       (gnus-agent-get-undownloaded-list))
10860     ;; Remove list identifiers from subject
10861     (when gnus-list-identifiers
10862       (gnus-summary-remove-list-identifiers))
10863     ;; First and last article in this newsgroup.
10864     (when gnus-newsgroup-headers
10865       (setq gnus-newsgroup-begin
10866             (mail-header-number (car gnus-newsgroup-headers))
10867             gnus-newsgroup-end
10868             (mail-header-number
10869              (gnus-last-element gnus-newsgroup-headers))))
10870     (when gnus-use-scoring
10871       (gnus-possibly-score-headers))))
10872
10873 (defun gnus-summary-insert-old-articles (&optional all)
10874   "Insert all old articles in this group.
10875 If ALL is non-nil, already read articles become readable.
10876 If ALL is a number, fetch this number of articles."
10877   (interactive "P")
10878   (prog1
10879       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
10880             older len)
10881         (setq older
10882               (gnus-sorted-difference
10883                (gnus-uncompress-range (list gnus-newsgroup-active))
10884                old))
10885         (setq len (length older))
10886         (cond
10887          ((null older) nil)
10888          ((numberp all)
10889           (if (< all len)
10890               (setq older (last older all))))
10891          (all nil)
10892          (t
10893           (if (and (numberp gnus-large-newsgroup)
10894                    (> len gnus-large-newsgroup))
10895               (let ((input
10896                      (read-string
10897                       (format
10898                        "How many articles from %s (default %d): "
10899                        (gnus-limit-string
10900                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10901                        len))))
10902                 (unless (string-match "^[ \t]*$" input)
10903                   (setq all (string-to-number input))
10904                   (if (< all len)
10905                       (setq older (last older all))))))))
10906         (if (not older)
10907             (message "No old news.")
10908           (gnus-summary-insert-articles older)
10909           (gnus-summary-limit (gnus-sorted-nunion old older))))
10910     (gnus-summary-position-point)))
10911
10912 (defun gnus-summary-insert-new-articles ()
10913   "Insert all new articles in this group."
10914   (interactive)
10915   (prog1
10916       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
10917             (old-active gnus-newsgroup-active)
10918             (nnmail-fetched-sources (list t))
10919             i new)
10920         (setq gnus-newsgroup-active
10921               (gnus-activate-group gnus-newsgroup-name 'scan))
10922         (setq i (1+ (cdr old-active)))
10923         (while (<= i (cdr gnus-newsgroup-active))
10924           (push i new)
10925           (incf i))
10926         (if (not new)
10927             (message "No gnus is bad news.")
10928           (setq new (nreverse new))
10929           (gnus-summary-insert-articles new)
10930           (setq gnus-newsgroup-unreads
10931                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
10932           (gnus-summary-limit (gnus-sorted-nunion old new))))
10933     (gnus-summary-position-point)))
10934
10935 (gnus-summary-make-all-marking-commands)
10936
10937 (gnus-ems-redefine)
10938
10939 (provide 'gnus-sum)
10940
10941 (run-hooks 'gnus-sum-load-hook)
10942
10943 ;;; gnus-sum.el ends here