*** empty log message ***
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29
30 (require 'gnus)
31 (require 'gnus-group)
32 (require 'gnus-spec)
33 (require 'gnus-range)
34 (require 'gnus-int)
35 (require 'gnus-undo)
36 (require 'gnus-util)
37 (require 'mm-decode)
38 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
39
40 (defcustom gnus-kill-summary-on-exit t
41   "*If non-nil, kill the summary buffer when you exit from it.
42 If nil, the summary will become a \"*Dead Summary*\" buffer, and
43 it will be killed sometime later."
44   :group 'gnus-summary-exit
45   :type 'boolean)
46
47 (defcustom gnus-fetch-old-headers nil
48   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
49 If an unread article in the group refers to an older, already read (or
50 just marked as read) article, the old article will not normally be
51 displayed in the Summary buffer.  If this variable is non-nil, Gnus
52 will attempt to grab the headers to the old articles, and thereby
53 build complete threads.  If it has the value `some', only enough
54 headers to connect otherwise loose threads will be displayed.  This
55 variable can also be a number.  In that case, no more than that number
56 of old headers will be fetched.  If it has the value `invisible', all
57 old headers will be fetched, but none will be displayed.
58
59 The server has to support NOV for any of this to work."
60   :group 'gnus-thread
61   :type '(choice (const :tag "off" nil)
62                  (const some)
63                  number
64                  (sexp :menu-tag "other" t)))
65
66 (defcustom gnus-refer-thread-limit 200
67   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
68 If t, fetch all the available old headers."
69   :group 'gnus-thread
70   :type '(choice number
71                  (sexp :menu-tag "other" t)))
72
73 (defcustom gnus-summary-make-false-root 'adopt
74   "*nil means that Gnus won't gather loose threads.
75 If the root of a thread has expired or been read in a previous
76 session, the information necessary to build a complete thread has been
77 lost.  Instead of having many small sub-threads from this original thread
78 scattered all over the summary buffer, Gnus can gather them.
79
80 If non-nil, Gnus will try to gather all loose sub-threads from an
81 original thread into one large thread.
82
83 If this variable is non-nil, it should be one of `none', `adopt',
84 `dummy' or `empty'.
85
86 If this variable is `none', Gnus will not make a false root, but just
87 present the sub-threads after another.
88 If this variable is `dummy', Gnus will create a dummy root that will
89 have all the sub-threads as children.
90 If this variable is `adopt', Gnus will make one of the \"children\"
91 the parent and mark all the step-children as such.
92 If this variable is `empty', the \"children\" are printed with empty
93 subject fields.  (Or rather, they will be printed with a string
94 given by the `gnus-summary-same-subject' variable.)"
95   :group 'gnus-thread
96   :type '(choice (const :tag "off" nil)
97                  (const none)
98                  (const dummy)
99                  (const adopt)
100                  (const empty)))
101
102 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
103   "*A regexp to match subjects to be excluded from loose thread gathering.
104 As loose thread gathering is done on subjects only, that means that
105 there can be many false gatherings performed.  By rooting out certain
106 common subjects, gathering might become saner."
107   :group 'gnus-thread
108   :type 'regexp)
109
110 (defcustom gnus-summary-gather-subject-limit nil
111   "*Maximum length of subject comparisons when gathering loose threads.
112 Use nil to compare full subjects.  Setting this variable to a low
113 number will help gather threads that have been corrupted by
114 newsreaders chopping off subject lines, but it might also mean that
115 unrelated articles that have subject that happen to begin with the
116 same few characters will be incorrectly gathered.
117
118 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
119 comparing subjects."
120   :group 'gnus-thread
121   :type '(choice (const :tag "off" nil)
122                  (const fuzzy)
123                  (sexp :menu-tag "on" t)))
124
125 (defcustom gnus-simplify-subject-functions nil
126   "List of functions taking a string argument that simplify subjects.
127 The functions are applied recursively.
128
129 Useful functions to put in this list include: `gnus-simplify-subject-re',
130 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
131   :group 'gnus-thread
132   :type '(repeat function))
133
134 (defcustom gnus-simplify-ignored-prefixes nil
135   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
136   :group 'gnus-thread
137   :type '(choice (const :tag "off" nil)
138                  regexp))
139
140 (defcustom gnus-build-sparse-threads nil
141   "*If non-nil, fill in the gaps in threads.
142 If `some', only fill in the gaps that are needed to tie loose threads
143 together.  If `more', fill in all leaf nodes that Gnus can find.  If
144 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
145   :group 'gnus-thread
146   :type '(choice (const :tag "off" nil)
147                  (const some)
148                  (const more)
149                  (sexp :menu-tag "all" t)))
150
151 (defcustom gnus-summary-thread-gathering-function
152   'gnus-gather-threads-by-subject
153   "*Function used for gathering loose threads.
154 There are two pre-defined functions: `gnus-gather-threads-by-subject',
155 which only takes Subjects into consideration; and
156 `gnus-gather-threads-by-references', which compared the References
157 headers of the articles to find matches."
158   :group 'gnus-thread
159   :type '(radio (function-item gnus-gather-threads-by-subject)
160                 (function-item gnus-gather-threads-by-references)
161                 (function :tag "other")))
162
163 (defcustom gnus-summary-same-subject ""
164   "*String indicating that the current article has the same subject as the previous.
165 This variable will only be used if the value of
166 `gnus-summary-make-false-root' is `empty'."
167   :group 'gnus-summary-format
168   :type 'string)
169
170 (defcustom gnus-summary-goto-unread t
171   "*If t, marking commands will go to the next unread article.
172 If `never', commands that usually go to the next unread article, will
173 go to the next article, whether it is read or not.
174 If nil, only the marking commands will go to the next (un)read article."
175   :group 'gnus-summary-marks
176   :link '(custom-manual "(gnus)Setting Marks")
177   :type '(choice (const :tag "off" nil)
178                  (const never)
179                  (sexp :menu-tag "on" t)))
180
181 (defcustom gnus-summary-default-score 0
182   "*Default article score level.
183 All scores generated by the score files will be added to this score.
184 If this variable is nil, scoring will be disabled."
185   :group 'gnus-score-default
186   :type '(choice (const :tag "disable")
187                  integer))
188
189 (defcustom gnus-summary-zcore-fuzz 0
190   "*Fuzziness factor for the zcore in the summary buffer.
191 Articles with scores closer than this to `gnus-summary-default-score'
192 will not be marked."
193   :group 'gnus-summary-format
194   :type 'integer)
195
196 (defcustom gnus-simplify-subject-fuzzy-regexp nil
197   "*Strings to be removed when doing fuzzy matches.
198 This can either be a regular expression or list of regular expressions
199 that will be removed from subject strings if fuzzy subject
200 simplification is selected."
201   :group 'gnus-thread
202   :type '(repeat regexp))
203
204 (defcustom gnus-show-threads t
205   "*If non-nil, display threads in summary mode."
206   :group 'gnus-thread
207   :type 'boolean)
208
209 (defcustom gnus-thread-hide-subtree nil
210   "*If non-nil, hide all threads initially.
211 If threads are hidden, you have to run the command
212 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
213 to expose hidden threads."
214   :group 'gnus-thread
215   :type 'boolean)
216
217 (defcustom gnus-thread-hide-killed t
218   "*If non-nil, hide killed threads automatically."
219   :group 'gnus-thread
220   :type 'boolean)
221
222 (defcustom gnus-thread-ignore-subject t
223   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
224 If nil, articles that have different subjects from their parents will
225 start separate threads."
226   :group 'gnus-thread
227   :type 'boolean)
228
229 (defcustom gnus-thread-operation-ignore-subject t
230   "*If non-nil, subjects will be ignored when doing thread commands.
231 This affects commands like `gnus-summary-kill-thread' and
232 `gnus-summary-lower-thread'.
233
234 If this variable is nil, articles in the same thread with different
235 subjects will not be included in the operation in question.  If this
236 variable is `fuzzy', only articles that have subjects that are fuzzily
237 equal will be included."
238   :group 'gnus-thread
239   :type '(choice (const :tag "off" nil)
240                  (const fuzzy)
241                  (sexp :tag "on" t)))
242
243 (defcustom gnus-thread-indent-level 4
244   "*Number that says how much each sub-thread should be indented."
245   :group 'gnus-thread
246   :type 'integer)
247
248 (defcustom gnus-auto-extend-newsgroup t
249   "*If non-nil, extend newsgroup forward and backward when requested."
250   :group 'gnus-summary-choose
251   :type 'boolean)
252
253 (defcustom gnus-auto-select-first t
254   "*If nil, don't select the first unread article when entering a group.
255 If this variable is `best', select the highest-scored unread article
256 in the group.  If t, select the first unread article.
257
258 This variable can also be a function to place point on a likely
259 subject line.  Useful values include `gnus-summary-first-unread-subject',
260 `gnus-summary-first-unread-article' and
261 `gnus-summary-best-unread-article'.
262
263 If you want to prevent automatic selection of the first unread article
264 in some newsgroups, set the variable to nil in
265 `gnus-select-group-hook'."
266   :group 'gnus-group-select
267   :type '(choice (const :tag "none" nil)
268                  (const best)
269                  (sexp :menu-tag "first" t)
270                  (function-item gnus-summary-first-unread-subject)
271                  (function-item gnus-summary-first-unread-article)
272                  (function-item gnus-summary-best-unread-article)))
273
274 (defcustom gnus-auto-select-next t
275   "*If non-nil, offer to go to the next group from the end of the previous.
276 If the value is t and the next newsgroup is empty, Gnus will exit
277 summary mode and go back to group mode.  If the value is neither nil
278 nor t, Gnus will select the following unread newsgroup.  In
279 particular, if the value is the symbol `quietly', the next unread
280 newsgroup will be selected without any confirmation, and if it is
281 `almost-quietly', the next group will be selected without any
282 confirmation if you are located on the last article in the group.
283 Finally, if this variable is `slightly-quietly', the `Z n' command
284 will go to the next group without confirmation."
285   :group 'gnus-summary-maneuvering
286   :type '(choice (const :tag "off" nil)
287                  (const quietly)
288                  (const almost-quietly)
289                  (const slightly-quietly)
290                  (sexp :menu-tag "on" t)))
291
292 (defcustom gnus-auto-select-same nil
293   "*If non-nil, select the next article with the same subject.
294 If there are no more articles with the same subject, go to
295 the first unread article."
296   :group 'gnus-summary-maneuvering
297   :type 'boolean)
298
299 (defcustom gnus-summary-check-current nil
300   "*If non-nil, consider the current article when moving.
301 The \"unread\" movement commands will stay on the same line if the
302 current article is unread."
303   :group 'gnus-summary-maneuvering
304   :type 'boolean)
305
306 (defcustom gnus-auto-center-summary t
307   "*If non-nil, always center the current summary buffer.
308 In particular, if `vertical' do only vertical recentering.  If non-nil
309 and non-`vertical', do both horizontal and vertical recentering."
310   :group 'gnus-summary-maneuvering
311   :type '(choice (const :tag "none" nil)
312                  (const vertical)
313                  (integer :tag "height")
314                  (sexp :menu-tag "both" t)))
315
316 (defcustom gnus-show-all-headers nil
317   "*If non-nil, don't hide any headers."
318   :group 'gnus-article-hiding
319   :group 'gnus-article-headers
320   :type 'boolean)
321
322 (defcustom gnus-summary-ignore-duplicates nil
323   "*If non-nil, ignore articles with identical Message-ID headers."
324   :group 'gnus-summary
325   :type 'boolean)
326
327 (defcustom gnus-single-article-buffer t
328   "*If non-nil, display all articles in the same buffer.
329 If nil, each group will get its own article buffer."
330   :group 'gnus-article-various
331   :type 'boolean)
332
333 (defcustom gnus-break-pages t
334   "*If non-nil, do page breaking on articles.
335 The page delimiter is specified by the `gnus-page-delimiter'
336 variable."
337   :group 'gnus-article-various
338   :type 'boolean)
339
340 (defcustom gnus-move-split-methods nil
341   "*Variable used to suggest where articles are to be moved to.
342 It uses the same syntax as the `gnus-split-methods' variable."
343   :group 'gnus-summary-mail
344   :type '(repeat (choice (list :value (fun) function)
345                          (cons :value ("" "") regexp (repeat string))
346                          (sexp :value nil))))
347
348 (defcustom gnus-unread-mark ? ;Whitespace
349   "*Mark used for unread articles."
350   :group 'gnus-summary-marks
351   :type 'character)
352
353 (defcustom gnus-ticked-mark ?!
354   "*Mark used for ticked articles."
355   :group 'gnus-summary-marks
356   :type 'character)
357
358 (defcustom gnus-dormant-mark ??
359   "*Mark used for dormant articles."
360   :group 'gnus-summary-marks
361   :type 'character)
362
363 (defcustom gnus-del-mark ?r
364   "*Mark used for del'd articles."
365   :group 'gnus-summary-marks
366   :type 'character)
367
368 (defcustom gnus-read-mark ?R
369   "*Mark used for read articles."
370   :group 'gnus-summary-marks
371   :type 'character)
372
373 (defcustom gnus-expirable-mark ?E
374   "*Mark used for expirable articles."
375   :group 'gnus-summary-marks
376   :type 'character)
377
378 (defcustom gnus-killed-mark ?K
379   "*Mark used for killed articles."
380   :group 'gnus-summary-marks
381   :type 'character)
382
383 (defcustom gnus-souped-mark ?F
384   "*Mark used for killed articles."
385   :group 'gnus-summary-marks
386   :type 'character)
387
388 (defcustom gnus-kill-file-mark ?X
389   "*Mark used for articles killed by kill files."
390   :group 'gnus-summary-marks
391   :type 'character)
392
393 (defcustom gnus-low-score-mark ?Y
394   "*Mark used for articles with a low score."
395   :group 'gnus-summary-marks
396   :type 'character)
397
398 (defcustom gnus-catchup-mark ?C
399   "*Mark used for articles that are caught up."
400   :group 'gnus-summary-marks
401   :type 'character)
402
403 (defcustom gnus-replied-mark ?A
404   "*Mark used for articles that have been replied to."
405   :group 'gnus-summary-marks
406   :type 'character)
407
408 (defcustom gnus-cached-mark ?*
409   "*Mark used for articles that are in the cache."
410   :group 'gnus-summary-marks
411   :type 'character)
412
413 (defcustom gnus-saved-mark ?S
414   "*Mark used for articles that have been saved to."
415   :group 'gnus-summary-marks
416   :type 'character)
417
418 (defcustom gnus-ancient-mark ?O
419   "*Mark used for ancient articles."
420   :group 'gnus-summary-marks
421   :type 'character)
422
423 (defcustom gnus-sparse-mark ?Q
424   "*Mark used for sparsely reffed articles."
425   :group 'gnus-summary-marks
426   :type 'character)
427
428 (defcustom gnus-canceled-mark ?G
429   "*Mark used for canceled articles."
430   :group 'gnus-summary-marks
431   :type 'character)
432
433 (defcustom gnus-duplicate-mark ?M
434   "*Mark used for duplicate articles."
435   :group 'gnus-summary-marks
436   :type 'character)
437
438 (defcustom gnus-undownloaded-mark ?@
439   "*Mark used for articles that weren't downloaded."
440   :group 'gnus-summary-marks
441   :type 'character)
442
443 (defcustom gnus-downloadable-mark ?%
444   "*Mark used for articles that are to be downloaded."
445   :group 'gnus-summary-marks
446   :type 'character)
447
448 (defcustom gnus-unsendable-mark ?=
449   "*Mark used for articles that won't be sent."
450   :group 'gnus-summary-marks
451   :type 'character)
452
453 (defcustom gnus-score-over-mark ?+
454   "*Score mark used for articles with high scores."
455   :group 'gnus-summary-marks
456   :type 'character)
457
458 (defcustom gnus-score-below-mark ?-
459   "*Score mark used for articles with low scores."
460   :group 'gnus-summary-marks
461   :type 'character)
462
463 (defcustom gnus-empty-thread-mark ? ;Whitespace
464   "*There is no thread under the article."
465   :group 'gnus-summary-marks
466   :type 'character)
467
468 (defcustom gnus-not-empty-thread-mark ?=
469   "*There is a thread under the article."
470   :group 'gnus-summary-marks
471   :type 'character)
472
473 (defcustom gnus-view-pseudo-asynchronously nil
474   "*If non-nil, Gnus will view pseudo-articles asynchronously."
475   :group 'gnus-extract-view
476   :type 'boolean)
477
478 (defcustom gnus-auto-expirable-marks
479   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
480         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
481         gnus-souped-mark gnus-duplicate-mark)
482   "*The list of marks converted into expiration if a group is auto-expirable."
483   :group 'gnus-summary
484   :type '(repeat character))
485
486 (defcustom gnus-inhibit-user-auto-expire t
487   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
488   :group 'gnus-summary
489   :type 'boolean)
490
491 (defcustom gnus-view-pseudos nil
492   "*If `automatic', pseudo-articles will be viewed automatically.
493 If `not-confirm', pseudos will be viewed automatically, and the user
494 will not be asked to confirm the command."
495   :group 'gnus-extract-view
496   :type '(choice (const :tag "off" nil)
497                  (const automatic)
498                  (const not-confirm)))
499
500 (defcustom gnus-view-pseudos-separately t
501   "*If non-nil, one pseudo-article will be created for each file to be viewed.
502 If nil, all files that use the same viewing command will be given as a
503 list of parameters to that command."
504   :group 'gnus-extract-view
505   :type 'boolean)
506
507 (defcustom gnus-insert-pseudo-articles t
508   "*If non-nil, insert pseudo-articles when decoding articles."
509   :group 'gnus-extract-view
510   :type 'boolean)
511
512 (defcustom gnus-summary-dummy-line-format
513   "  %(:                          :%) %S\n"
514   "*The format specification for the dummy roots in the summary buffer.
515 It works along the same lines as a normal formatting string,
516 with some simple extensions.
517
518 %S  The subject"
519   :group 'gnus-threading
520   :type 'string)
521
522 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
523   "*The format specification for the summary mode line.
524 It works along the same lines as a normal formatting string,
525 with some simple extensions:
526
527 %G  Group name
528 %p  Unprefixed group name
529 %A  Current article number
530 %z  Current article score
531 %V  Gnus version
532 %U  Number of unread articles in the group
533 %e  Number of unselected articles in the group
534 %Z  A string with unread/unselected article counts
535 %g  Shortish group name
536 %S  Subject of the current article
537 %u  User-defined spec
538 %s  Current score file name
539 %d  Number of dormant articles
540 %r  Number of articles that have been marked as read in this session
541 %E  Number of articles expunged by the score files"
542   :group 'gnus-summary-format
543   :type 'string)
544
545 (defcustom gnus-summary-mark-below 0
546   "*Mark all articles with a score below this variable as read.
547 This variable is local to each summary buffer and usually set by the
548 score file."
549   :group 'gnus-score-default
550   :type 'integer)
551
552 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
553   "*List of functions used for sorting articles in the summary buffer.
554 This variable is only used when not using a threaded display."
555   :group 'gnus-summary-sort
556   :type '(repeat (choice (function-item gnus-article-sort-by-number)
557                          (function-item gnus-article-sort-by-author)
558                          (function-item gnus-article-sort-by-subject)
559                          (function-item gnus-article-sort-by-date)
560                          (function-item gnus-article-sort-by-score)
561                          (function :tag "other"))))
562
563 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
564   "*List of functions used for sorting threads in the summary buffer.
565 By default, threads are sorted by article number.
566
567 Each function takes two threads and return non-nil if the first thread
568 should be sorted before the other.  If you use more than one function,
569 the primary sort function should be the last.  You should probably
570 always include `gnus-thread-sort-by-number' in the list of sorting
571 functions -- preferably first.
572
573 Ready-made functions include `gnus-thread-sort-by-number',
574 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
575 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
576 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
577   :group 'gnus-summary-sort
578   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
579                          (function-item gnus-thread-sort-by-author)
580                          (function-item gnus-thread-sort-by-subject)
581                          (function-item gnus-thread-sort-by-date)
582                          (function-item gnus-thread-sort-by-score)
583                          (function-item gnus-thread-sort-by-total-score)
584                          (function :tag "other"))))
585
586 (defcustom gnus-thread-score-function '+
587   "*Function used for calculating the total score of a thread.
588
589 The function is called with the scores of the article and each
590 subthread and should then return the score of the thread.
591
592 Some functions you can use are `+', `max', or `min'."
593   :group 'gnus-summary-sort
594   :type 'function)
595
596 (defcustom gnus-summary-expunge-below nil
597   "All articles that have a score less than this variable will be expunged.
598 This variable is local to the summary buffers."
599   :group 'gnus-score-default
600   :type '(choice (const :tag "off" nil)
601                  integer))
602
603 (defcustom gnus-thread-expunge-below nil
604   "All threads that have a total score less than this variable will be expunged.
605 See `gnus-thread-score-function' for en explanation of what a
606 \"thread score\" is.
607
608 This variable is local to the summary buffers."
609   :group 'gnus-threading
610   :group 'gnus-score-default
611   :type '(choice (const :tag "off" nil)
612                  integer))
613
614 (defcustom gnus-summary-mode-hook nil
615   "*A hook for Gnus summary mode.
616 This hook is run before any variables are set in the summary buffer."
617   :group 'gnus-summary-various
618   :type 'hook)
619
620 (defcustom gnus-summary-menu-hook nil
621   "*Hook run after the creation of the summary mode menu."
622   :group 'gnus-summary-visual
623   :type 'hook)
624
625 (defcustom gnus-summary-exit-hook nil
626   "*A hook called on exit from the summary buffer.
627 It will be called with point in the group buffer."
628   :group 'gnus-summary-exit
629   :type 'hook)
630
631 (defcustom gnus-summary-prepare-hook nil
632   "*A hook called after the summary buffer has been generated.
633 If you want to modify the summary buffer, you can use this hook."
634   :group 'gnus-summary-various
635   :type 'hook)
636
637 (defcustom gnus-summary-prepared-hook nil
638   "*A hook called as the last thing after the summary buffer has been generated."
639   :group 'gnus-summary-various
640   :type 'hook)
641
642 (defcustom gnus-summary-generate-hook nil
643   "*A hook run just before generating the summary buffer.
644 This hook is commonly used to customize threading variables and the
645 like."
646   :group 'gnus-summary-various
647   :type 'hook)
648
649 (defcustom gnus-select-group-hook nil
650   "*A hook called when a newsgroup is selected.
651
652 If you'd like to simplify subjects like the
653 `gnus-summary-next-same-subject' command does, you can use the
654 following hook:
655
656  (setq gnus-select-group-hook
657       (list
658         (lambda ()
659           (mapcar (lambda (header)
660                      (mail-header-set-subject
661                       header
662                       (gnus-simplify-subject
663                        (mail-header-subject header) 're-only)))
664                   gnus-newsgroup-headers))))"
665   :group 'gnus-group-select
666   :type 'hook)
667
668 (defcustom gnus-select-article-hook nil
669   "*A hook called when an article is selected."
670   :group 'gnus-summary-choose
671   :type 'hook)
672
673 (defcustom gnus-visual-mark-article-hook
674   (list 'gnus-highlight-selected-summary)
675   "*Hook run after selecting an article in the summary buffer.
676 It is meant to be used for highlighting the article in some way.  It
677 is not run if `gnus-visual' is nil."
678   :group 'gnus-summary-visual
679   :type 'hook)
680
681 (defcustom gnus-parse-headers-hook nil
682   "*A hook called before parsing the headers."
683   :group 'gnus-various
684   :type 'hook)
685
686 (defcustom gnus-exit-group-hook nil
687   "*A hook called when exiting (not quitting) summary mode."
688   :group 'gnus-various
689   :type 'hook)
690
691 (defcustom gnus-summary-update-hook
692   (list 'gnus-summary-highlight-line)
693   "*A hook called when a summary line is changed.
694 The hook will not be called if `gnus-visual' is nil.
695
696 The default function `gnus-summary-highlight-line' will
697 highlight the line according to the `gnus-summary-highlight'
698 variable."
699   :group 'gnus-summary-visual
700   :type 'hook)
701
702 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
703   "*A hook called when an article is selected for the first time.
704 The hook is intended to mark an article as read (or unread)
705 automatically when it is selected."
706   :group 'gnus-summary-choose
707   :type 'hook)
708
709 (defcustom gnus-group-no-more-groups-hook nil
710   "*A hook run when returning to group mode having no more (unread) groups."
711   :group 'gnus-group-select
712   :type 'hook)
713
714 (defcustom gnus-ps-print-hook nil
715   "*A hook run before ps-printing something from Gnus."
716   :group 'gnus-summary
717   :type 'hook)
718
719 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
720   "Face used for highlighting the current article in the summary buffer."
721   :group 'gnus-summary-visual
722   :type 'face)
723
724 (defcustom gnus-summary-highlight
725   '(((= mark gnus-canceled-mark)
726      . gnus-summary-cancelled-face)
727     ((and (> score default)
728           (or (= mark gnus-dormant-mark)
729               (= mark gnus-ticked-mark)))
730      . gnus-summary-high-ticked-face)
731     ((and (< score default)
732           (or (= mark gnus-dormant-mark)
733               (= mark gnus-ticked-mark)))
734      . gnus-summary-low-ticked-face)
735     ((or (= mark gnus-dormant-mark)
736          (= mark gnus-ticked-mark))
737      . gnus-summary-normal-ticked-face)
738     ((and (> score default) (= mark gnus-ancient-mark))
739      . gnus-summary-high-ancient-face)
740     ((and (< score default) (= mark gnus-ancient-mark))
741      . gnus-summary-low-ancient-face)
742     ((= mark gnus-ancient-mark)
743      . gnus-summary-normal-ancient-face)
744     ((and (> score default) (= mark gnus-unread-mark))
745      . gnus-summary-high-unread-face)
746     ((and (< score default) (= mark gnus-unread-mark))
747      . gnus-summary-low-unread-face)
748     ((= mark gnus-unread-mark)
749      . gnus-summary-normal-unread-face)
750     ((and (> score default) (memq mark (list gnus-downloadable-mark
751                                              gnus-undownloaded-mark)))
752      . gnus-summary-high-unread-face)
753     ((and (< score default) (memq mark (list gnus-downloadable-mark
754                                              gnus-undownloaded-mark)))
755      . gnus-summary-low-unread-face)
756     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
757      . gnus-summary-normal-unread-face)
758     ((> score default)
759      . gnus-summary-high-read-face)
760     ((< score default)
761      . gnus-summary-low-read-face)
762     (t
763      . gnus-summary-normal-read-face))
764   "*Controls the highlighting of summary buffer lines.
765
766 A list of (FORM . FACE) pairs.  When deciding how a a particular
767 summary line should be displayed, each form is evaluated.  The content
768 of the face field after the first true form is used.  You can change
769 how those summary lines are displayed, by editing the face field.
770
771 You can use the following variables in the FORM field.
772
773 score:   The articles score
774 default: The default article score.
775 below:   The score below which articles are automatically marked as read.
776 mark:    The articles mark."
777   :group 'gnus-summary-visual
778   :type '(repeat (cons (sexp :tag "Form" nil)
779                        face)))
780
781 (defcustom gnus-alter-header-function nil
782   "Function called to allow alteration of article header structures.
783 The function is called with one parameter, the article header vector,
784 which it may alter in any way.")
785
786 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
787   "Variable that says which function should be used to decode a string with encoded words.")
788
789 (defcustom gnus-extra-headers nil
790   "*Extra headers to parse."
791   :group 'gnus-summary
792   :type '(repeat symbol))
793
794 (defcustom gnus-ignored-from-addresses
795   (and user-mail-address (regexp-quote user-mail-address))
796   "*Regexp of From headers that may be suppressed in favor of To headers."
797   :group 'gnus-summary
798   :type 'regexp)
799
800 (defcustom gnus-group-charset-alist
801   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
802     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
803     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
804     ("^relcom\\>" koi8-r)
805     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
806     ("^israel\\>" iso-8859-1)
807     ("^han\\>" euc-kr)
808     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
809     (".*" iso-8859-1))
810   "Alist of regexps (to match group names) and default charsets to be used when reading."
811   :type '(repeat (list (regexp :tag "Group")
812                        (symbol :tag "Charset")))
813   :group 'gnus-charset)
814
815 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit)
816   "List of charsets that should be ignored.
817 When these charsets are used in the \"charset\" parameter, the
818 default charset will be used instead."
819   :type '(repeat symbol)
820   :group 'gnus-charset)
821
822 (defcustom gnus-group-ignored-charsets-alist 
823   '(("alt\\.chinese\\.text" iso-8859-1))
824   "Alist of regexps (to match group names) and charsets that should be ignored.
825 When these charsets are used in the \"charset\" parameter, the
826 default charset will be used instead."
827   :type '(repeat (cons (regexp :tag "Group")
828                        (repeat symbol)))
829   :group 'gnus-charset)
830
831 (defcustom gnus-group-highlight-words-alist nil
832   "Alist of group regexps and highlight regexps.
833 This variable uses the same syntax as `gnus-emphasis-alist'."
834   :type '(repeat (cons (regexp :tag "Group")
835                        (repeat (list (regexp :tag "Highlight regexp")
836                                      (number :tag "Group for entire word" 0)
837                                      (number :tag "Group for displayed part" 0)
838                                      (symbol :tag "Face" 
839                                              gnus-emphasis-highlight-words)))))
840   :group 'gnus-summary-visual)
841
842 ;;; Internal variables
843
844 (defvar gnus-article-mime-handles nil)
845 (defvar gnus-article-decoded-p nil)
846 (defvar gnus-scores-exclude-files nil)
847 (defvar gnus-page-broken nil)
848 (defvar gnus-inhibit-mime-unbuttonizing nil)
849
850 (defvar gnus-original-article nil)
851 (defvar gnus-article-internal-prepare-hook nil)
852 (defvar gnus-newsgroup-process-stack nil)
853
854 (defvar gnus-thread-indent-array nil)
855 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
856
857 ;; Avoid highlighting in kill files.
858 (defvar gnus-summary-inhibit-highlight nil)
859 (defvar gnus-newsgroup-selected-overlay nil)
860 (defvar gnus-inhibit-limiting nil)
861 (defvar gnus-newsgroup-adaptive-score-file nil)
862 (defvar gnus-current-score-file nil)
863 (defvar gnus-current-move-group nil)
864 (defvar gnus-current-copy-group nil)
865 (defvar gnus-current-crosspost-group nil)
866
867 (defvar gnus-newsgroup-dependencies nil)
868 (defvar gnus-newsgroup-adaptive nil)
869 (defvar gnus-summary-display-article-function nil)
870 (defvar gnus-summary-highlight-line-function nil
871   "Function called after highlighting a summary line.")
872
873 (defvar gnus-summary-line-format-alist
874   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
875     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
876     (?s gnus-tmp-subject-or-nil ?s)
877     (?n gnus-tmp-name ?s)
878     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
879         ?s)
880     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
881             gnus-tmp-from) ?s)
882     (?F gnus-tmp-from ?s)
883     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
884     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
885     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
886     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
887     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
888     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
889     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
890     (?L gnus-tmp-lines ?d)
891     (?I gnus-tmp-indentation ?s)
892     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
893     (?R gnus-tmp-replied ?c)
894     (?\[ gnus-tmp-opening-bracket ?c)
895     (?\] gnus-tmp-closing-bracket ?c)
896     (?\> (make-string gnus-tmp-level ? ) ?s)
897     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
898     (?i gnus-tmp-score ?d)
899     (?z gnus-tmp-score-char ?c)
900     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
901     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
902     (?U gnus-tmp-unread ?c)
903     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
904     (?t (gnus-summary-number-of-articles-in-thread
905          (and (boundp 'thread) (car thread)) gnus-tmp-level)
906         ?d)
907     (?e (gnus-summary-number-of-articles-in-thread
908          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
909         ?c)
910     (?u gnus-tmp-user-defined ?s)
911     (?P (gnus-pick-line-number) ?d))
912   "An alist of format specifications that can appear in summary lines,
913 and what variables they correspond with, along with the type of the
914 variable (string, integer, character, etc).")
915
916 (defvar gnus-summary-dummy-line-format-alist
917   `((?S gnus-tmp-subject ?s)
918     (?N gnus-tmp-number ?d)
919     (?u gnus-tmp-user-defined ?s)))
920
921 (defvar gnus-summary-mode-line-format-alist
922   `((?G gnus-tmp-group-name ?s)
923     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
924     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
925     (?A gnus-tmp-article-number ?d)
926     (?Z gnus-tmp-unread-and-unselected ?s)
927     (?V gnus-version ?s)
928     (?U gnus-tmp-unread-and-unticked ?d)
929     (?S gnus-tmp-subject ?s)
930     (?e gnus-tmp-unselected ?d)
931     (?u gnus-tmp-user-defined ?s)
932     (?d (length gnus-newsgroup-dormant) ?d)
933     (?t (length gnus-newsgroup-marked) ?d)
934     (?r (length gnus-newsgroup-reads) ?d)
935     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
936     (?E gnus-newsgroup-expunged-tally ?d)
937     (?s (gnus-current-score-file-nondirectory) ?s)))
938
939 (defvar gnus-last-search-regexp nil
940   "Default regexp for article search command.")
941
942 (defvar gnus-last-shell-command nil
943   "Default shell command on article.")
944
945 (defvar gnus-newsgroup-begin nil)
946 (defvar gnus-newsgroup-end nil)
947 (defvar gnus-newsgroup-last-rmail nil)
948 (defvar gnus-newsgroup-last-mail nil)
949 (defvar gnus-newsgroup-last-folder nil)
950 (defvar gnus-newsgroup-last-file nil)
951 (defvar gnus-newsgroup-auto-expire nil)
952 (defvar gnus-newsgroup-active nil)
953
954 (defvar gnus-newsgroup-data nil)
955 (defvar gnus-newsgroup-data-reverse nil)
956 (defvar gnus-newsgroup-limit nil)
957 (defvar gnus-newsgroup-limits nil)
958
959 (defvar gnus-newsgroup-unreads nil
960   "List of unread articles in the current newsgroup.")
961
962 (defvar gnus-newsgroup-unselected nil
963   "List of unselected unread articles in the current newsgroup.")
964
965 (defvar gnus-newsgroup-reads nil
966   "Alist of read articles and article marks in the current newsgroup.")
967
968 (defvar gnus-newsgroup-expunged-tally nil)
969
970 (defvar gnus-newsgroup-marked nil
971   "List of ticked articles in the current newsgroup (a subset of unread art).")
972
973 (defvar gnus-newsgroup-killed nil
974   "List of ranges of articles that have been through the scoring process.")
975
976 (defvar gnus-newsgroup-cached nil
977   "List of articles that come from the article cache.")
978
979 (defvar gnus-newsgroup-saved nil
980   "List of articles that have been saved.")
981
982 (defvar gnus-newsgroup-kill-headers nil)
983
984 (defvar gnus-newsgroup-replied nil
985   "List of articles that have been replied to in the current newsgroup.")
986
987 (defvar gnus-newsgroup-expirable nil
988   "List of articles in the current newsgroup that can be expired.")
989
990 (defvar gnus-newsgroup-processable nil
991   "List of articles in the current newsgroup that can be processed.")
992
993 (defvar gnus-newsgroup-downloadable nil
994   "List of articles in the current newsgroup that can be processed.")
995
996 (defvar gnus-newsgroup-undownloaded nil
997   "List of articles in the current newsgroup that haven't been downloaded..")
998
999 (defvar gnus-newsgroup-unsendable nil
1000   "List of articles in the current newsgroup that won't be sent.")
1001
1002 (defvar gnus-newsgroup-bookmarks nil
1003   "List of articles in the current newsgroup that have bookmarks.")
1004
1005 (defvar gnus-newsgroup-dormant nil
1006   "List of dormant articles in the current newsgroup.")
1007
1008 (defvar gnus-newsgroup-scored nil
1009   "List of scored articles in the current newsgroup.")
1010
1011 (defvar gnus-newsgroup-headers nil
1012   "List of article headers in the current newsgroup.")
1013
1014 (defvar gnus-newsgroup-threads nil)
1015
1016 (defvar gnus-newsgroup-prepared nil
1017   "Whether the current group has been prepared properly.")
1018
1019 (defvar gnus-newsgroup-ancient nil
1020   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1021
1022 (defvar gnus-newsgroup-sparse nil)
1023
1024 (defvar gnus-current-article nil)
1025 (defvar gnus-article-current nil)
1026 (defvar gnus-current-headers nil)
1027 (defvar gnus-have-all-headers nil)
1028 (defvar gnus-last-article nil)
1029 (defvar gnus-newsgroup-history nil)
1030 (defvar gnus-newsgroup-charset nil)
1031
1032 (defconst gnus-summary-local-variables
1033   '(gnus-newsgroup-name
1034     gnus-newsgroup-begin gnus-newsgroup-end
1035     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1036     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1037     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1038     gnus-newsgroup-unselected gnus-newsgroup-marked
1039     gnus-newsgroup-reads gnus-newsgroup-saved
1040     gnus-newsgroup-replied gnus-newsgroup-expirable
1041     gnus-newsgroup-processable gnus-newsgroup-killed
1042     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1043     gnus-newsgroup-unsendable
1044     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1045     gnus-newsgroup-headers gnus-newsgroup-threads
1046     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1047     gnus-current-article gnus-current-headers gnus-have-all-headers
1048     gnus-last-article gnus-article-internal-prepare-hook
1049     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1050     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1051     gnus-thread-expunge-below
1052     gnus-score-alist gnus-current-score-file
1053     (gnus-summary-expunge-below . global)
1054     (gnus-summary-mark-below . global)
1055     gnus-newsgroup-active gnus-scores-exclude-files
1056     gnus-newsgroup-history gnus-newsgroup-ancient
1057     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1058     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1059     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1060     (gnus-newsgroup-expunged-tally . 0)
1061     gnus-cache-removable-articles gnus-newsgroup-cached
1062     gnus-newsgroup-data gnus-newsgroup-data-reverse
1063     gnus-newsgroup-limit gnus-newsgroup-limits
1064     gnus-newsgroup-charset)
1065   "Variables that are buffer-local to the summary buffers.")
1066
1067 ;; Byte-compiler warning.
1068 (defvar gnus-article-mode-map)
1069
1070 ;; MIME stuff.
1071
1072 (defvar gnus-decode-encoded-word-methods
1073   '(mail-decode-encoded-word-string)
1074   "List of methods used to decode encoded words.
1075
1076 This variable is a list of FUNCTION or (REGEXP . FUNCTION). If item is
1077 FUNCTION, FUNCTION will be apply to all newsgroups. If item is a
1078 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1079 whose names match REGEXP.
1080
1081 For example:
1082 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1083  mail-decode-encoded-word-string
1084  (\"chinese\" . rfc1843-decode-string))
1085 ")
1086
1087 (defvar gnus-decode-encoded-word-methods-cache nil)
1088
1089 (defun gnus-multi-decode-encoded-word-string (string)
1090   "Apply the functions from `gnus-encoded-word-methods' that match."
1091   (unless (and gnus-decode-encoded-word-methods-cache
1092                (eq gnus-newsgroup-name
1093                    (car gnus-decode-encoded-word-methods-cache)))
1094     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1095     (mapc '(lambda (x)
1096              (if (symbolp x)
1097                  (nconc gnus-decode-encoded-word-methods-cache (list x))
1098                (if (and gnus-newsgroup-name
1099                         (string-match (car x) gnus-newsgroup-name))
1100                    (nconc gnus-decode-encoded-word-methods-cache
1101                           (list (cdr x))))))
1102           gnus-decode-encoded-word-methods))
1103   (let ((xlist gnus-decode-encoded-word-methods-cache))
1104     (pop xlist)
1105     (while xlist
1106       (setq string (funcall (pop xlist) string))))
1107   string)
1108
1109 ;; Subject simplification.
1110
1111 (defun gnus-simplify-whitespace (str)
1112   "Remove excessive whitespace."
1113   (let ((mystr str))
1114     ;; Multiple spaces.
1115     (while (string-match "[ \t][ \t]+" mystr)
1116       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1117                           " "
1118                           (substring mystr (match-end 0)))))
1119     ;; Leading spaces.
1120     (when (string-match "^[ \t]+" mystr)
1121       (setq mystr (substring mystr (match-end 0))))
1122     ;; Trailing spaces.
1123     (when (string-match "[ \t]+$" mystr)
1124       (setq mystr (substring mystr 0 (match-beginning 0))))
1125     mystr))
1126
1127 (defsubst gnus-simplify-subject-re (subject)
1128   "Remove \"Re:\" from subject lines."
1129   (if (string-match "^[Rr][Ee]: *" subject)
1130       (substring subject (match-end 0))
1131     subject))
1132
1133 (defun gnus-simplify-subject (subject &optional re-only)
1134   "Remove `Re:' and words in parentheses.
1135 If RE-ONLY is non-nil, strip leading `Re:'s only."
1136   (let ((case-fold-search t))           ;Ignore case.
1137     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1138     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1139       (setq subject (substring subject (match-end 0))))
1140     ;; Remove uninteresting prefixes.
1141     (when (and (not re-only)
1142                gnus-simplify-ignored-prefixes
1143                (string-match gnus-simplify-ignored-prefixes subject))
1144       (setq subject (substring subject (match-end 0))))
1145     ;; Remove words in parentheses from end.
1146     (unless re-only
1147       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1148         (setq subject (substring subject 0 (match-beginning 0)))))
1149     ;; Return subject string.
1150     subject))
1151
1152 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1153 ;; all whitespace.
1154 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1155   (goto-char (point-min))
1156   (while (re-search-forward regexp nil t)
1157       (replace-match (or newtext ""))))
1158
1159 (defun gnus-simplify-buffer-fuzzy ()
1160   "Simplify string in the buffer fuzzily.
1161 The string in the accessible portion of the current buffer is simplified.
1162 It is assumed to be a single-line subject.
1163 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1164 matter is removed.  Additional things can be deleted by setting
1165 gnus-simplify-subject-fuzzy-regexp."
1166   (let ((case-fold-search t)
1167         (modified-tick))
1168     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1169
1170     (while (not (eq modified-tick (buffer-modified-tick)))
1171       (setq modified-tick (buffer-modified-tick))
1172       (cond
1173        ((listp gnus-simplify-subject-fuzzy-regexp)
1174         (mapcar 'gnus-simplify-buffer-fuzzy-step
1175                 gnus-simplify-subject-fuzzy-regexp))
1176        (gnus-simplify-subject-fuzzy-regexp
1177         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1178       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1179       (gnus-simplify-buffer-fuzzy-step
1180        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1181       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1182
1183     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1184     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1185     (gnus-simplify-buffer-fuzzy-step " $")
1186     (gnus-simplify-buffer-fuzzy-step "^ +")))
1187
1188 (defun gnus-simplify-subject-fuzzy (subject)
1189   "Simplify a subject string fuzzily.
1190 See `gnus-simplify-buffer-fuzzy' for details."
1191   (save-excursion
1192     (gnus-set-work-buffer)
1193     (let ((case-fold-search t))
1194       ;; Remove uninteresting prefixes.
1195       (when (and gnus-simplify-ignored-prefixes
1196                  (string-match gnus-simplify-ignored-prefixes subject))
1197         (setq subject (substring subject (match-end 0))))
1198       (insert subject)
1199       (inline (gnus-simplify-buffer-fuzzy))
1200       (buffer-string))))
1201
1202 (defsubst gnus-simplify-subject-fully (subject)
1203   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1204   (cond
1205    (gnus-simplify-subject-functions
1206     (gnus-map-function gnus-simplify-subject-functions subject))
1207    ((null gnus-summary-gather-subject-limit)
1208     (gnus-simplify-subject-re subject))
1209    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1210     (gnus-simplify-subject-fuzzy subject))
1211    ((numberp gnus-summary-gather-subject-limit)
1212     (gnus-limit-string (gnus-simplify-subject-re subject)
1213                        gnus-summary-gather-subject-limit))
1214    (t
1215     subject)))
1216
1217 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1218   "Check whether two subjects are equal.
1219 If optional argument simple-first is t, first argument is already
1220 simplified."
1221   (cond
1222    ((null simple-first)
1223     (equal (gnus-simplify-subject-fully s1)
1224            (gnus-simplify-subject-fully s2)))
1225    (t
1226     (equal s1
1227            (gnus-simplify-subject-fully s2)))))
1228
1229 (defun gnus-summary-bubble-group ()
1230   "Increase the score of the current group.
1231 This is a handy function to add to `gnus-summary-exit-hook' to
1232 increase the score of each group you read."
1233   (gnus-group-add-score gnus-newsgroup-name))
1234
1235 \f
1236 ;;;
1237 ;;; Gnus summary mode
1238 ;;;
1239
1240 (put 'gnus-summary-mode 'mode-class 'special)
1241
1242 (when t
1243   ;; Non-orthogonal keys
1244
1245   (gnus-define-keys gnus-summary-mode-map
1246     " " gnus-summary-next-page
1247     "\177" gnus-summary-prev-page
1248     [delete] gnus-summary-prev-page
1249     [backspace] gnus-summary-prev-page
1250     "\r" gnus-summary-scroll-up
1251     "\M-\r" gnus-summary-scroll-down
1252     "n" gnus-summary-next-unread-article
1253     "p" gnus-summary-prev-unread-article
1254     "N" gnus-summary-next-article
1255     "P" gnus-summary-prev-article
1256     "\M-\C-n" gnus-summary-next-same-subject
1257     "\M-\C-p" gnus-summary-prev-same-subject
1258     "\M-n" gnus-summary-next-unread-subject
1259     "\M-p" gnus-summary-prev-unread-subject
1260     "." gnus-summary-first-unread-article
1261     "," gnus-summary-best-unread-article
1262     "\M-s" gnus-summary-search-article-forward
1263     "\M-r" gnus-summary-search-article-backward
1264     "<" gnus-summary-beginning-of-article
1265     ">" gnus-summary-end-of-article
1266     "j" gnus-summary-goto-article
1267     "^" gnus-summary-refer-parent-article
1268     "\M-^" gnus-summary-refer-article
1269     "u" gnus-summary-tick-article-forward
1270     "!" gnus-summary-tick-article-forward
1271     "U" gnus-summary-tick-article-backward
1272     "d" gnus-summary-mark-as-read-forward
1273     "D" gnus-summary-mark-as-read-backward
1274     "E" gnus-summary-mark-as-expirable
1275     "\M-u" gnus-summary-clear-mark-forward
1276     "\M-U" gnus-summary-clear-mark-backward
1277     "k" gnus-summary-kill-same-subject-and-select
1278     "\C-k" gnus-summary-kill-same-subject
1279     "\M-\C-k" gnus-summary-kill-thread
1280     "\M-\C-l" gnus-summary-lower-thread
1281     "e" gnus-summary-edit-article
1282     "#" gnus-summary-mark-as-processable
1283     "\M-#" gnus-summary-unmark-as-processable
1284     "\M-\C-t" gnus-summary-toggle-threads
1285     "\M-\C-s" gnus-summary-show-thread
1286     "\M-\C-h" gnus-summary-hide-thread
1287     "\M-\C-f" gnus-summary-next-thread
1288     "\M-\C-b" gnus-summary-prev-thread
1289     "\M-\C-u" gnus-summary-up-thread
1290     "\M-\C-d" gnus-summary-down-thread
1291     "&" gnus-summary-execute-command
1292     "c" gnus-summary-catchup-and-exit
1293     "\C-w" gnus-summary-mark-region-as-read
1294     "\C-t" gnus-summary-toggle-truncation
1295     "?" gnus-summary-mark-as-dormant
1296     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1297     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1298     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1299     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1300     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1301     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1302     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1303     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1304     "=" gnus-summary-expand-window
1305     "\C-x\C-s" gnus-summary-reselect-current-group
1306     "\M-g" gnus-summary-rescan-group
1307     "w" gnus-summary-stop-page-breaking
1308     "\C-c\C-r" gnus-summary-caesar-message
1309     "f" gnus-summary-followup
1310     "F" gnus-summary-followup-with-original
1311     "C" gnus-summary-cancel-article
1312     "r" gnus-summary-reply
1313     "R" gnus-summary-reply-with-original
1314     "\C-c\C-f" gnus-summary-mail-forward
1315     "o" gnus-summary-save-article
1316     "\C-o" gnus-summary-save-article-mail
1317     "|" gnus-summary-pipe-output
1318     "\M-k" gnus-summary-edit-local-kill
1319     "\M-K" gnus-summary-edit-global-kill
1320     ;; "V" gnus-version
1321     "\C-c\C-d" gnus-summary-describe-group
1322     "q" gnus-summary-exit
1323     "Q" gnus-summary-exit-no-update
1324     "\C-c\C-i" gnus-info-find-node
1325     gnus-mouse-2 gnus-mouse-pick-article
1326     "m" gnus-summary-mail-other-window
1327     "a" gnus-summary-post-news
1328     "x" gnus-summary-limit-to-unread
1329     "s" gnus-summary-isearch-article
1330     "t" gnus-summary-toggle-header
1331     "g" gnus-summary-show-article
1332     "l" gnus-summary-goto-last-article
1333     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1334     "\C-d" gnus-summary-enter-digest-group
1335     "\M-\C-d" gnus-summary-read-document
1336     "\M-\C-e" gnus-summary-edit-parameters
1337     "\M-\C-g" gnus-summary-customize-parameters
1338     "\C-c\C-b" gnus-bug
1339     "*" gnus-cache-enter-article
1340     "\M-*" gnus-cache-remove-article
1341     "\M-&" gnus-summary-universal-argument
1342     "\C-l" gnus-recenter
1343     "I" gnus-summary-increase-score
1344     "L" gnus-summary-lower-score
1345     "\M-i" gnus-symbolic-argument
1346     "h" gnus-summary-select-article-buffer
1347
1348     "b" gnus-article-view-part
1349     "\M-t" gnus-summary-toggle-display-buttonized
1350
1351     "V" gnus-summary-score-map
1352     "X" gnus-uu-extract-map
1353     "S" gnus-summary-send-map)
1354
1355   ;; Sort of orthogonal keymap
1356   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1357     "t" gnus-summary-tick-article-forward
1358     "!" gnus-summary-tick-article-forward
1359     "d" gnus-summary-mark-as-read-forward
1360     "r" gnus-summary-mark-as-read-forward
1361     "c" gnus-summary-clear-mark-forward
1362     " " gnus-summary-clear-mark-forward
1363     "e" gnus-summary-mark-as-expirable
1364     "x" gnus-summary-mark-as-expirable
1365     "?" gnus-summary-mark-as-dormant
1366     "b" gnus-summary-set-bookmark
1367     "B" gnus-summary-remove-bookmark
1368     "#" gnus-summary-mark-as-processable
1369     "\M-#" gnus-summary-unmark-as-processable
1370     "S" gnus-summary-limit-include-expunged
1371     "C" gnus-summary-catchup
1372     "H" gnus-summary-catchup-to-here
1373     "\C-c" gnus-summary-catchup-all
1374     "k" gnus-summary-kill-same-subject-and-select
1375     "K" gnus-summary-kill-same-subject
1376     "P" gnus-uu-mark-map)
1377
1378   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1379     "c" gnus-summary-clear-above
1380     "u" gnus-summary-tick-above
1381     "m" gnus-summary-mark-above
1382     "k" gnus-summary-kill-below)
1383
1384   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1385     "/" gnus-summary-limit-to-subject
1386     "n" gnus-summary-limit-to-articles
1387     "w" gnus-summary-pop-limit
1388     "s" gnus-summary-limit-to-subject
1389     "a" gnus-summary-limit-to-author
1390     "u" gnus-summary-limit-to-unread
1391     "m" gnus-summary-limit-to-marks
1392     "M" gnus-summary-limit-exclude-marks
1393     "v" gnus-summary-limit-to-score
1394     "*" gnus-summary-limit-include-cached
1395     "D" gnus-summary-limit-include-dormant
1396     "T" gnus-summary-limit-include-thread
1397     "d" gnus-summary-limit-exclude-dormant
1398     "t" gnus-summary-limit-to-age
1399     "x" gnus-summary-limit-to-extra 
1400     "E" gnus-summary-limit-include-expunged
1401     "c" gnus-summary-limit-exclude-childless-dormant
1402     "C" gnus-summary-limit-mark-excluded-as-read)
1403
1404   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1405     "n" gnus-summary-next-unread-article
1406     "p" gnus-summary-prev-unread-article
1407     "N" gnus-summary-next-article
1408     "P" gnus-summary-prev-article
1409     "\C-n" gnus-summary-next-same-subject
1410     "\C-p" gnus-summary-prev-same-subject
1411     "\M-n" gnus-summary-next-unread-subject
1412     "\M-p" gnus-summary-prev-unread-subject
1413     "f" gnus-summary-first-unread-article
1414     "b" gnus-summary-best-unread-article
1415     "j" gnus-summary-goto-article
1416     "g" gnus-summary-goto-subject
1417     "l" gnus-summary-goto-last-article
1418     "o" gnus-summary-pop-article)
1419
1420   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1421     "k" gnus-summary-kill-thread
1422     "l" gnus-summary-lower-thread
1423     "i" gnus-summary-raise-thread
1424     "T" gnus-summary-toggle-threads
1425     "t" gnus-summary-rethread-current
1426     "^" gnus-summary-reparent-thread
1427     "s" gnus-summary-show-thread
1428     "S" gnus-summary-show-all-threads
1429     "h" gnus-summary-hide-thread
1430     "H" gnus-summary-hide-all-threads
1431     "n" gnus-summary-next-thread
1432     "p" gnus-summary-prev-thread
1433     "u" gnus-summary-up-thread
1434     "o" gnus-summary-top-thread
1435     "d" gnus-summary-down-thread
1436     "#" gnus-uu-mark-thread
1437     "\M-#" gnus-uu-unmark-thread)
1438
1439   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1440     "g" gnus-summary-prepare
1441     "c" gnus-summary-insert-cached-articles)
1442
1443   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1444     "c" gnus-summary-catchup-and-exit
1445     "C" gnus-summary-catchup-all-and-exit
1446     "E" gnus-summary-exit-no-update
1447     "Q" gnus-summary-exit
1448     "Z" gnus-summary-exit
1449     "n" gnus-summary-catchup-and-goto-next-group
1450     "R" gnus-summary-reselect-current-group
1451     "G" gnus-summary-rescan-group
1452     "N" gnus-summary-next-group
1453     "s" gnus-summary-save-newsrc
1454     "P" gnus-summary-prev-group)
1455
1456   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1457     " " gnus-summary-next-page
1458     "n" gnus-summary-next-page
1459     "\177" gnus-summary-prev-page
1460     [delete] gnus-summary-prev-page
1461     "p" gnus-summary-prev-page
1462     "\r" gnus-summary-scroll-up
1463     "\M-\r" gnus-summary-scroll-down
1464     "<" gnus-summary-beginning-of-article
1465     ">" gnus-summary-end-of-article
1466     "b" gnus-summary-beginning-of-article
1467     "e" gnus-summary-end-of-article
1468     "^" gnus-summary-refer-parent-article
1469     "r" gnus-summary-refer-parent-article
1470     "D" gnus-summary-enter-digest-group
1471     "R" gnus-summary-refer-references
1472     "T" gnus-summary-refer-thread
1473     "g" gnus-summary-show-article
1474     "s" gnus-summary-isearch-article
1475     "P" gnus-summary-print-article
1476     "t" gnus-article-babel)
1477
1478   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1479     "b" gnus-article-add-buttons
1480     "B" gnus-article-add-buttons-to-head
1481     "o" gnus-article-treat-overstrike
1482     "e" gnus-article-emphasize
1483     "w" gnus-article-fill-cited-article
1484     "Q" gnus-article-fill-long-lines
1485     "C" gnus-article-capitalize-sentences
1486     "c" gnus-article-remove-cr
1487     "q" gnus-article-de-quoted-unreadable
1488     "f" gnus-article-display-x-face
1489     "l" gnus-summary-stop-page-breaking
1490     "r" gnus-summary-caesar-message
1491     "t" gnus-article-hide-headers
1492     "v" gnus-summary-verbose-headers
1493     "h" gnus-article-treat-html
1494     "H" gnus-article-strip-headers-in-body
1495     "d" gnus-article-treat-dumbquotes)
1496
1497   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1498     "a" gnus-article-hide
1499     "h" gnus-article-hide-headers
1500     "b" gnus-article-hide-boring-headers
1501     "s" gnus-article-hide-signature
1502     "c" gnus-article-hide-citation
1503     "C" gnus-article-hide-citation-in-followups
1504     "p" gnus-article-hide-pgp
1505     "B" gnus-article-strip-banner
1506     "P" gnus-article-hide-pem
1507     "\C-c" gnus-article-hide-citation-maybe)
1508
1509   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1510     "a" gnus-article-highlight
1511     "h" gnus-article-highlight-headers
1512     "c" gnus-article-highlight-citation
1513     "s" gnus-article-highlight-signature)
1514
1515   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1516     "w" gnus-article-decode-mime-words
1517     "c" gnus-article-decode-charset
1518     "v" gnus-mime-view-all-parts
1519     "b" gnus-article-view-part)
1520
1521   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1522     "z" gnus-article-date-ut
1523     "u" gnus-article-date-ut
1524     "l" gnus-article-date-local
1525     "e" gnus-article-date-lapsed
1526     "o" gnus-article-date-original
1527     "i" gnus-article-date-iso8601
1528     "s" gnus-article-date-user)
1529
1530   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1531     "t" gnus-article-remove-trailing-blank-lines
1532     "l" gnus-article-strip-leading-blank-lines
1533     "m" gnus-article-strip-multiple-blank-lines
1534     "a" gnus-article-strip-blank-lines
1535     "A" gnus-article-strip-all-blank-lines
1536     "s" gnus-article-strip-leading-space
1537     "e" gnus-article-strip-trailing-space)
1538
1539   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1540     "v" gnus-version
1541     "f" gnus-summary-fetch-faq
1542     "d" gnus-summary-describe-group
1543     "h" gnus-summary-describe-briefly
1544     "i" gnus-info-find-node)
1545
1546   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1547     "e" gnus-summary-expire-articles
1548     "\M-\C-e" gnus-summary-expire-articles-now
1549     "\177" gnus-summary-delete-article
1550     [delete] gnus-summary-delete-article
1551     [backspace] gnus-summary-delete-article
1552     "m" gnus-summary-move-article
1553     "r" gnus-summary-respool-article
1554     "w" gnus-summary-edit-article
1555     "c" gnus-summary-copy-article
1556     "B" gnus-summary-crosspost-article
1557     "q" gnus-summary-respool-query
1558     "t" gnus-summary-respool-trace
1559     "i" gnus-summary-import-article
1560     "p" gnus-summary-article-posted-p)
1561
1562   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1563     "o" gnus-summary-save-article
1564     "m" gnus-summary-save-article-mail
1565     "F" gnus-summary-write-article-file
1566     "r" gnus-summary-save-article-rmail
1567     "f" gnus-summary-save-article-file
1568     "b" gnus-summary-save-article-body-file
1569     "h" gnus-summary-save-article-folder
1570     "v" gnus-summary-save-article-vm
1571     "p" gnus-summary-pipe-output
1572     "s" gnus-soup-add-article)
1573
1574   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1575     "b" gnus-summary-display-buttonized
1576     "m" gnus-summary-repair-multipart
1577     "v" gnus-article-view-part
1578     "o" gnus-article-save-part
1579     "c" gnus-article-copy-part
1580     "e" gnus-article-externalize-part
1581     "i" gnus-article-inline-part
1582     "|" gnus-article-pipe-part)
1583   )
1584
1585 (defun gnus-summary-make-menu-bar ()
1586   (gnus-turn-off-edit-menu 'summary)
1587
1588   (unless (boundp 'gnus-summary-misc-menu)
1589
1590     (easy-menu-define
1591      gnus-summary-kill-menu gnus-summary-mode-map ""
1592      (cons
1593       "Score"
1594       (nconc
1595        (list
1596         ["Enter score..." gnus-summary-score-entry t]
1597         ["Customize" gnus-score-customize t])
1598        (gnus-make-score-map 'increase)
1599        (gnus-make-score-map 'lower)
1600        '(("Mark"
1601           ["Kill below" gnus-summary-kill-below t]
1602           ["Mark above" gnus-summary-mark-above t]
1603           ["Tick above" gnus-summary-tick-above t]
1604           ["Clear above" gnus-summary-clear-above t])
1605          ["Current score" gnus-summary-current-score t]
1606          ["Set score" gnus-summary-set-score t]
1607          ["Switch current score file..." gnus-score-change-score-file t]
1608          ["Set mark below..." gnus-score-set-mark-below t]
1609          ["Set expunge below..." gnus-score-set-expunge-below t]
1610          ["Edit current score file" gnus-score-edit-current-scores t]
1611          ["Edit score file" gnus-score-edit-file t]
1612          ["Trace score" gnus-score-find-trace t]
1613          ["Find words" gnus-score-find-favourite-words t]
1614          ["Rescore buffer" gnus-summary-rescore t]
1615          ["Increase score..." gnus-summary-increase-score t]
1616          ["Lower score..." gnus-summary-lower-score t]))))
1617
1618     ;; Define both the Article menu in the summary buffer and the equivalent
1619     ;; Commands menu in the article buffer here for consistency.
1620     (let ((innards
1621            '(("Hide"
1622               ["All" gnus-article-hide t]
1623               ["Headers" gnus-article-hide-headers t]
1624               ["Signature" gnus-article-hide-signature t]
1625               ["Citation" gnus-article-hide-citation t]
1626               ["PGP" gnus-article-hide-pgp t]
1627               ["Banner" gnus-article-strip-banner t]
1628               ["Boring headers" gnus-article-hide-boring-headers t])
1629              ("Highlight"
1630               ["All" gnus-article-highlight t]
1631               ["Headers" gnus-article-highlight-headers t]
1632               ["Signature" gnus-article-highlight-signature t]
1633               ["Citation" gnus-article-highlight-citation t])
1634              ("MIME"
1635               ["Words" gnus-article-decode-mime-words t]
1636               ["Charset" gnus-article-decode-charset t]
1637               ["QP" gnus-article-de-quoted-unreadable t]
1638               ["View all" gnus-mime-view-all-parts t])
1639              ("Date"
1640               ["Local" gnus-article-date-local t]
1641               ["ISO8601" gnus-article-date-iso8601 t]
1642               ["UT" gnus-article-date-ut t]
1643               ["Original" gnus-article-date-original t]
1644               ["Lapsed" gnus-article-date-lapsed t]
1645               ["User-defined" gnus-article-date-user t])
1646              ("Washing"
1647               ("Remove Blanks"
1648                ["Leading" gnus-article-strip-leading-blank-lines t]
1649                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1650                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1651                ["All of the above" gnus-article-strip-blank-lines t]
1652                ["All" gnus-article-strip-all-blank-lines t]
1653                ["Leading space" gnus-article-strip-leading-space t]
1654                ["Trailing space" gnus-article-strip-trailing-space t])
1655               ["Overstrike" gnus-article-treat-overstrike t]
1656               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1657               ["Emphasis" gnus-article-emphasize t]
1658               ["Word wrap" gnus-article-fill-cited-article t]
1659               ["Fill long lines" gnus-article-fill-long-lines t]
1660               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1661               ["CR" gnus-article-remove-cr t]
1662               ["Show X-Face" gnus-article-display-x-face t]
1663               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1664               ["UnHTMLize" gnus-article-treat-html t]
1665               ["Rot 13" gnus-summary-caesar-message t]
1666               ["Unix pipe" gnus-summary-pipe-message t]
1667               ["Add buttons" gnus-article-add-buttons t]
1668               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1669               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1670               ["Verbose header" gnus-summary-verbose-headers t]
1671               ["Toggle header" gnus-summary-toggle-header t])
1672              ("Output"
1673               ["Save in default format" gnus-summary-save-article t]
1674               ["Save in file" gnus-summary-save-article-file t]
1675               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1676               ["Save in MH folder" gnus-summary-save-article-folder t]
1677               ["Save in VM folder" gnus-summary-save-article-vm t]
1678               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1679               ["Save body in file" gnus-summary-save-article-body-file t]
1680               ["Pipe through a filter" gnus-summary-pipe-output t]
1681               ["Add to SOUP packet" gnus-soup-add-article t]
1682               ["Print" gnus-summary-print-article t])
1683              ("Backend"
1684               ["Respool article..." gnus-summary-respool-article t]
1685               ["Move article..." gnus-summary-move-article
1686                (gnus-check-backend-function
1687                 'request-move-article gnus-newsgroup-name)]
1688               ["Copy article..." gnus-summary-copy-article t]
1689               ["Crosspost article..." gnus-summary-crosspost-article
1690                (gnus-check-backend-function
1691                 'request-replace-article gnus-newsgroup-name)]
1692               ["Import file..." gnus-summary-import-article t]
1693               ["Check if posted" gnus-summary-article-posted-p t]
1694               ["Edit article" gnus-summary-edit-article
1695                (not (gnus-group-read-only-p))]
1696               ["Delete article" gnus-summary-delete-article
1697                (gnus-check-backend-function
1698                 'request-expire-articles gnus-newsgroup-name)]
1699               ["Query respool" gnus-summary-respool-query t]
1700               ["Trace respool" gnus-summary-respool-trace t]
1701               ["Delete expirable articles" gnus-summary-expire-articles-now
1702                (gnus-check-backend-function
1703                 'request-expire-articles gnus-newsgroup-name)])
1704              ("Extract"
1705               ["Uudecode" gnus-uu-decode-uu t]
1706               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1707               ["Unshar" gnus-uu-decode-unshar t]
1708               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1709               ["Save" gnus-uu-decode-save t]
1710               ["Binhex" gnus-uu-decode-binhex t]
1711               ["Postscript" gnus-uu-decode-postscript t])
1712              ("Cache"
1713               ["Enter article" gnus-cache-enter-article t]
1714               ["Remove article" gnus-cache-remove-article t])
1715              ["Translate" gnus-article-babel t]
1716              ["Select article buffer" gnus-summary-select-article-buffer t]
1717              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1718              ["Isearch article..." gnus-summary-isearch-article t]
1719              ["Beginning of the article" gnus-summary-beginning-of-article t]
1720              ["End of the article" gnus-summary-end-of-article t]
1721              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1722              ["Fetch referenced articles" gnus-summary-refer-references t]
1723              ["Fetch current thread" gnus-summary-refer-thread t]
1724              ["Fetch article with id..." gnus-summary-refer-article t]
1725              ["Redisplay" gnus-summary-show-article t])))
1726       (easy-menu-define
1727        gnus-summary-article-menu gnus-summary-mode-map ""
1728        (cons "Article" innards))
1729
1730       (easy-menu-define
1731        gnus-article-commands-menu gnus-article-mode-map ""
1732        (cons "Commands" innards)))
1733
1734     (easy-menu-define
1735      gnus-summary-thread-menu gnus-summary-mode-map ""
1736      '("Threads"
1737        ["Toggle threading" gnus-summary-toggle-threads t]
1738        ["Hide threads" gnus-summary-hide-all-threads t]
1739        ["Show threads" gnus-summary-show-all-threads t]
1740        ["Hide thread" gnus-summary-hide-thread t]
1741        ["Show thread" gnus-summary-show-thread t]
1742        ["Go to next thread" gnus-summary-next-thread t]
1743        ["Go to previous thread" gnus-summary-prev-thread t]
1744        ["Go down thread" gnus-summary-down-thread t]
1745        ["Go up thread" gnus-summary-up-thread t]
1746        ["Top of thread" gnus-summary-top-thread t]
1747        ["Mark thread as read" gnus-summary-kill-thread t]
1748        ["Lower thread score" gnus-summary-lower-thread t]
1749        ["Raise thread score" gnus-summary-raise-thread t]
1750        ["Rethread current" gnus-summary-rethread-current t]
1751        ))
1752
1753     (easy-menu-define
1754      gnus-summary-post-menu gnus-summary-mode-map ""
1755      '("Post"
1756        ["Post an article" gnus-summary-post-news t]
1757        ["Followup" gnus-summary-followup t]
1758        ["Followup and yank" gnus-summary-followup-with-original t]
1759        ["Supersede article" gnus-summary-supersede-article t]
1760        ["Cancel article" gnus-summary-cancel-article t]
1761        ["Reply" gnus-summary-reply t]
1762        ["Reply and yank" gnus-summary-reply-with-original t]
1763        ["Wide reply" gnus-summary-wide-reply t]
1764        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1765        ["Mail forward" gnus-summary-mail-forward t]
1766        ["Post forward" gnus-summary-post-forward t]
1767        ["Digest and mail" gnus-uu-digest-mail-forward t]
1768        ["Digest and post" gnus-uu-digest-post-forward t]
1769        ["Resend message" gnus-summary-resend-message t]
1770        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1771        ["Send a mail" gnus-summary-mail-other-window t]
1772        ["Uuencode and post" gnus-uu-post-news t]
1773        ["Followup via news" gnus-summary-followup-to-mail t]
1774        ["Followup via news and yank"
1775         gnus-summary-followup-to-mail-with-original t]
1776        ;;("Draft"
1777        ;;["Send" gnus-summary-send-draft t]
1778        ;;["Send bounced" gnus-resend-bounced-mail t])
1779        ))
1780
1781     (easy-menu-define
1782      gnus-summary-misc-menu gnus-summary-mode-map ""
1783      '("Misc"
1784        ("Mark Read"
1785         ["Mark as read" gnus-summary-mark-as-read-forward t]
1786         ["Mark same subject and select"
1787          gnus-summary-kill-same-subject-and-select t]
1788         ["Mark same subject" gnus-summary-kill-same-subject t]
1789         ["Catchup" gnus-summary-catchup t]
1790         ["Catchup all" gnus-summary-catchup-all t]
1791         ["Catchup to here" gnus-summary-catchup-to-here t]
1792         ["Catchup region" gnus-summary-mark-region-as-read t]
1793         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1794        ("Mark Various"
1795         ["Tick" gnus-summary-tick-article-forward t]
1796         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1797         ["Remove marks" gnus-summary-clear-mark-forward t]
1798         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1799         ["Set bookmark" gnus-summary-set-bookmark t]
1800         ["Remove bookmark" gnus-summary-remove-bookmark t])
1801        ("Mark Limit"
1802         ["Marks..." gnus-summary-limit-to-marks t]
1803         ["Subject..." gnus-summary-limit-to-subject t]
1804         ["Author..." gnus-summary-limit-to-author t]
1805         ["Age..." gnus-summary-limit-to-age t]
1806         ["Extra..." gnus-summary-limit-to-extra t]
1807         ["Score" gnus-summary-limit-to-score t]
1808         ["Unread" gnus-summary-limit-to-unread t]
1809         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1810         ["Articles" gnus-summary-limit-to-articles t]
1811         ["Pop limit" gnus-summary-pop-limit t]
1812         ["Show dormant" gnus-summary-limit-include-dormant t]
1813         ["Hide childless dormant"
1814          gnus-summary-limit-exclude-childless-dormant t]
1815         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1816         ["Hide marked" gnus-summary-limit-exclude-marks t]
1817         ["Show expunged" gnus-summary-show-all-expunged t])
1818        ("Process Mark"
1819         ["Set mark" gnus-summary-mark-as-processable t]
1820         ["Remove mark" gnus-summary-unmark-as-processable t]
1821         ["Remove all marks" gnus-summary-unmark-all-processable t]
1822         ["Mark above" gnus-uu-mark-over t]
1823         ["Mark series" gnus-uu-mark-series t]
1824         ["Mark region" gnus-uu-mark-region t]
1825         ["Unmark region" gnus-uu-unmark-region t]
1826         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1827         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1828         ["Mark all" gnus-uu-mark-all t]
1829         ["Mark buffer" gnus-uu-mark-buffer t]
1830         ["Mark sparse" gnus-uu-mark-sparse t]
1831         ["Mark thread" gnus-uu-mark-thread t]
1832         ["Unmark thread" gnus-uu-unmark-thread t]
1833         ("Process Mark Sets"
1834          ["Kill" gnus-summary-kill-process-mark t]
1835          ["Yank" gnus-summary-yank-process-mark
1836           gnus-newsgroup-process-stack]
1837          ["Save" gnus-summary-save-process-mark t]))
1838        ("Scroll article"
1839         ["Page forward" gnus-summary-next-page t]
1840         ["Page backward" gnus-summary-prev-page t]
1841         ["Line forward" gnus-summary-scroll-up t])
1842        ("Move"
1843         ["Next unread article" gnus-summary-next-unread-article t]
1844         ["Previous unread article" gnus-summary-prev-unread-article t]
1845         ["Next article" gnus-summary-next-article t]
1846         ["Previous article" gnus-summary-prev-article t]
1847         ["Next unread subject" gnus-summary-next-unread-subject t]
1848         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1849         ["Next article same subject" gnus-summary-next-same-subject t]
1850         ["Previous article same subject" gnus-summary-prev-same-subject t]
1851         ["First unread article" gnus-summary-first-unread-article t]
1852         ["Best unread article" gnus-summary-best-unread-article t]
1853         ["Go to subject number..." gnus-summary-goto-subject t]
1854         ["Go to article number..." gnus-summary-goto-article t]
1855         ["Go to the last article" gnus-summary-goto-last-article t]
1856         ["Pop article off history" gnus-summary-pop-article t])
1857        ("Sort"
1858         ["Sort by number" gnus-summary-sort-by-number t]
1859         ["Sort by author" gnus-summary-sort-by-author t]
1860         ["Sort by subject" gnus-summary-sort-by-subject t]
1861         ["Sort by date" gnus-summary-sort-by-date t]
1862         ["Sort by score" gnus-summary-sort-by-score t]
1863         ["Sort by lines" gnus-summary-sort-by-lines t]
1864         ["Sort by characters" gnus-summary-sort-by-chars t])
1865        ("Help"
1866         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1867         ["Describe group" gnus-summary-describe-group t]
1868         ["Read manual" gnus-info-find-node t])
1869        ("Modes"
1870         ["Pick and read" gnus-pick-mode t]
1871         ["Binary" gnus-binary-mode t])
1872        ("Regeneration"
1873         ["Regenerate" gnus-summary-prepare t]
1874         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1875         ["Toggle threading" gnus-summary-toggle-threads t])
1876        ["Filter articles..." gnus-summary-execute-command t]
1877        ["Run command on subjects..." gnus-summary-universal-argument t]
1878        ["Search articles forward..." gnus-summary-search-article-forward t]
1879        ["Search articles backward..." gnus-summary-search-article-backward t]
1880        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1881        ["Expand window" gnus-summary-expand-window t]
1882        ["Expire expirable articles" gnus-summary-expire-articles
1883         (gnus-check-backend-function
1884          'request-expire-articles gnus-newsgroup-name)]
1885        ["Edit local kill file" gnus-summary-edit-local-kill t]
1886        ["Edit main kill file" gnus-summary-edit-global-kill t]
1887        ["Edit group parameters" gnus-summary-edit-parameters t]
1888        ["Customize group parameters" gnus-summary-customize-parameters t]
1889        ["Send a bug report" gnus-bug t]
1890        ("Exit"
1891         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1892         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1893         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1894         ["Exit group" gnus-summary-exit t]
1895         ["Exit group without updating" gnus-summary-exit-no-update t]
1896         ["Exit and goto next group" gnus-summary-next-group t]
1897         ["Exit and goto prev group" gnus-summary-prev-group t]
1898         ["Reselect group" gnus-summary-reselect-current-group t]
1899         ["Rescan group" gnus-summary-rescan-group t]
1900         ["Update dribble" gnus-summary-save-newsrc t])))
1901
1902     (gnus-run-hooks 'gnus-summary-menu-hook)))
1903
1904 (defun gnus-score-set-default (var value)
1905   "A version of set that updates the GNU Emacs menu-bar."
1906   (set var value)
1907   ;; It is the message that forces the active status to be updated.
1908   (message ""))
1909
1910 (defun gnus-make-score-map (type)
1911   "Make a summary score map of type TYPE."
1912   (if t
1913       nil
1914     (let ((headers '(("author" "from" string)
1915                      ("subject" "subject" string)
1916                      ("article body" "body" string)
1917                      ("article head" "head" string)
1918                      ("xref" "xref" string)
1919                      ("extra header" "extra" string)
1920                      ("lines" "lines" number)
1921                      ("followups to author" "followup" string)))
1922           (types '((number ("less than" <)
1923                            ("greater than" >)
1924                            ("equal" =))
1925                    (string ("substring" s)
1926                            ("exact string" e)
1927                            ("fuzzy string" f)
1928                            ("regexp" r))))
1929           (perms '(("temporary" (current-time-string))
1930                    ("permanent" nil)
1931                    ("immediate" now)))
1932           header)
1933       (list
1934        (apply
1935         'nconc
1936         (list
1937          (if (eq type 'lower)
1938              "Lower score"
1939            "Increase score"))
1940         (let (outh)
1941           (while headers
1942             (setq header (car headers))
1943             (setq outh
1944                   (cons
1945                    (apply
1946                     'nconc
1947                     (list (car header))
1948                     (let ((ts (cdr (assoc (nth 2 header) types)))
1949                           outt)
1950                       (while ts
1951                         (setq outt
1952                               (cons
1953                                (apply
1954                                 'nconc
1955                                 (list (caar ts))
1956                                 (let ((ps perms)
1957                                       outp)
1958                                   (while ps
1959                                     (setq outp
1960                                           (cons
1961                                            (vector
1962                                             (caar ps)
1963                                             (list
1964                                              'gnus-summary-score-entry
1965                                              (nth 1 header)
1966                                              (if (or (string= (nth 1 header)
1967                                                               "head")
1968                                                      (string= (nth 1 header)
1969                                                               "body"))
1970                                                  ""
1971                                                (list 'gnus-summary-header
1972                                                      (nth 1 header)))
1973                                              (list 'quote (nth 1 (car ts)))
1974                                              (list 'gnus-score-default nil)
1975                                              (nth 1 (car ps))
1976                                              t)
1977                                             t)
1978                                            outp))
1979                                     (setq ps (cdr ps)))
1980                                   (list (nreverse outp))))
1981                                outt))
1982                         (setq ts (cdr ts)))
1983                       (list (nreverse outt))))
1984                    outh))
1985             (setq headers (cdr headers)))
1986           (list (nreverse outh))))))))
1987
1988 \f
1989
1990 (defun gnus-summary-mode (&optional group)
1991   "Major mode for reading articles.
1992
1993 All normal editing commands are switched off.
1994 \\<gnus-summary-mode-map>
1995 Each line in this buffer represents one article.  To read an
1996 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
1997 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
1998 respectively.
1999
2000 You can also post articles and send mail from this buffer.  To
2001 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2002 of an article, type `\\[gnus-summary-reply]'.
2003
2004 There are approx. one gazillion commands you can execute in this
2005 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2006
2007 The following commands are available:
2008
2009 \\{gnus-summary-mode-map}"
2010   (interactive)
2011   (when (gnus-visual-p 'summary-menu 'menu)
2012     (gnus-summary-make-menu-bar))
2013   (kill-all-local-variables)
2014   (gnus-summary-make-local-variables)
2015   (gnus-make-thread-indent-array)
2016   (gnus-simplify-mode-line)
2017   (setq major-mode 'gnus-summary-mode)
2018   (setq mode-name "Summary")
2019   (make-local-variable 'minor-mode-alist)
2020   (use-local-map gnus-summary-mode-map)
2021   (buffer-disable-undo)
2022   (setq buffer-read-only t)             ;Disable modification
2023   (setq truncate-lines t)
2024   (setq selective-display t)
2025   (setq selective-display-ellipses t)   ;Display `...'
2026   (gnus-summary-set-display-table)
2027   (gnus-set-default-directory)
2028   (setq gnus-newsgroup-name group)
2029   (make-local-variable 'gnus-summary-line-format)
2030   (make-local-variable 'gnus-summary-line-format-spec)
2031   (make-local-variable 'gnus-summary-dummy-line-format)
2032   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2033   (make-local-variable 'gnus-summary-mark-positions)
2034   (make-local-hook 'pre-command-hook)
2035   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2036   (gnus-run-hooks 'gnus-summary-mode-hook)
2037   (mm-enable-multibyte)
2038   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2039   (gnus-update-summary-mark-positions))
2040
2041 (defun gnus-summary-make-local-variables ()
2042   "Make all the local summary buffer variables."
2043   (let (global)
2044     (dolist (local gnus-summary-local-variables)
2045       (if (consp local)
2046           (progn
2047             (if (eq (cdr local) 'global)
2048                 ;; Copy the global value of the variable.
2049                 (setq global (symbol-value (car local)))
2050               ;; Use the value from the list.
2051               (setq global (eval (cdr local))))
2052             (set (make-local-variable (car local)) global))
2053         ;; Simple nil-valued local variable.
2054         (set (make-local-variable local) nil)))))
2055
2056 (defun gnus-summary-clear-local-variables ()
2057   (let ((locals gnus-summary-local-variables))
2058     (while locals
2059       (if (consp (car locals))
2060           (and (vectorp (caar locals))
2061                (set (caar locals) nil))
2062         (and (vectorp (car locals))
2063              (set (car locals) nil)))
2064       (setq locals (cdr locals)))))
2065
2066 ;; Summary data functions.
2067
2068 (defmacro gnus-data-number (data)
2069   `(car ,data))
2070
2071 (defmacro gnus-data-set-number (data number)
2072   `(setcar ,data ,number))
2073
2074 (defmacro gnus-data-mark (data)
2075   `(nth 1 ,data))
2076
2077 (defmacro gnus-data-set-mark (data mark)
2078   `(setcar (nthcdr 1 ,data) ,mark))
2079
2080 (defmacro gnus-data-pos (data)
2081   `(nth 2 ,data))
2082
2083 (defmacro gnus-data-set-pos (data pos)
2084   `(setcar (nthcdr 2 ,data) ,pos))
2085
2086 (defmacro gnus-data-header (data)
2087   `(nth 3 ,data))
2088
2089 (defmacro gnus-data-set-header (data header)
2090   `(setf (nth 3 ,data) ,header))
2091
2092 (defmacro gnus-data-level (data)
2093   `(nth 4 ,data))
2094
2095 (defmacro gnus-data-unread-p (data)
2096   `(= (nth 1 ,data) gnus-unread-mark))
2097
2098 (defmacro gnus-data-read-p (data)
2099   `(/= (nth 1 ,data) gnus-unread-mark))
2100
2101 (defmacro gnus-data-pseudo-p (data)
2102   `(consp (nth 3 ,data)))
2103
2104 (defmacro gnus-data-find (number)
2105   `(assq ,number gnus-newsgroup-data))
2106
2107 (defmacro gnus-data-find-list (number &optional data)
2108   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2109      (memq (assq ,number bdata)
2110            bdata)))
2111
2112 (defmacro gnus-data-make (number mark pos header level)
2113   `(list ,number ,mark ,pos ,header ,level))
2114
2115 (defun gnus-data-enter (after-article number mark pos header level offset)
2116   (let ((data (gnus-data-find-list after-article)))
2117     (unless data
2118       (error "No such article: %d" after-article))
2119     (setcdr data (cons (gnus-data-make number mark pos header level)
2120                        (cdr data)))
2121     (setq gnus-newsgroup-data-reverse nil)
2122     (gnus-data-update-list (cddr data) offset)))
2123
2124 (defun gnus-data-enter-list (after-article list &optional offset)
2125   (when list
2126     (let ((data (and after-article (gnus-data-find-list after-article)))
2127           (ilist list))
2128       (if (not (or data
2129                    after-article))
2130           (let ((odata gnus-newsgroup-data))
2131             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2132             (when offset
2133               (gnus-data-update-list odata offset)))
2134         ;; Find the last element in the list to be spliced into the main
2135         ;; list.
2136         (while (cdr list)
2137           (setq list (cdr list)))
2138         (if (not data)
2139             (progn
2140               (setcdr list gnus-newsgroup-data)
2141               (setq gnus-newsgroup-data ilist)
2142               (when offset
2143                 (gnus-data-update-list (cdr list) offset)))
2144           (setcdr list (cdr data))
2145           (setcdr data ilist)
2146           (when offset
2147             (gnus-data-update-list (cdr list) offset))))
2148       (setq gnus-newsgroup-data-reverse nil))))
2149
2150 (defun gnus-data-remove (article &optional offset)
2151   (let ((data gnus-newsgroup-data))
2152     (if (= (gnus-data-number (car data)) article)
2153         (progn
2154           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2155                 gnus-newsgroup-data-reverse nil)
2156           (when offset
2157             (gnus-data-update-list gnus-newsgroup-data offset)))
2158       (while (cdr data)
2159         (when (= (gnus-data-number (cadr data)) article)
2160           (setcdr data (cddr data))
2161           (when offset
2162             (gnus-data-update-list (cdr data) offset))
2163           (setq data nil
2164                 gnus-newsgroup-data-reverse nil))
2165         (setq data (cdr data))))))
2166
2167 (defmacro gnus-data-list (backward)
2168   `(if ,backward
2169        (or gnus-newsgroup-data-reverse
2170            (setq gnus-newsgroup-data-reverse
2171                  (reverse gnus-newsgroup-data)))
2172      gnus-newsgroup-data))
2173
2174 (defun gnus-data-update-list (data offset)
2175   "Add OFFSET to the POS of all data entries in DATA."
2176   (setq gnus-newsgroup-data-reverse nil)
2177   (while data
2178     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2179     (setq data (cdr data))))
2180
2181 (defun gnus-summary-article-pseudo-p (article)
2182   "Say whether this article is a pseudo article or not."
2183   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2184
2185 (defmacro gnus-summary-article-sparse-p (article)
2186   "Say whether this article is a sparse article or not."
2187   `(memq ,article gnus-newsgroup-sparse))
2188
2189 (defmacro gnus-summary-article-ancient-p (article)
2190   "Say whether this article is a sparse article or not."
2191   `(memq ,article gnus-newsgroup-ancient))
2192
2193 (defun gnus-article-parent-p (number)
2194   "Say whether this article is a parent or not."
2195   (let ((data (gnus-data-find-list number)))
2196     (and (cdr data)                     ; There has to be an article after...
2197          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2198             (gnus-data-level (nth 1 data))))))
2199
2200 (defun gnus-article-children (number)
2201   "Return a list of all children to NUMBER."
2202   (let* ((data (gnus-data-find-list number))
2203          (level (gnus-data-level (car data)))
2204          children)
2205     (setq data (cdr data))
2206     (while (and data
2207                 (= (gnus-data-level (car data)) (1+ level)))
2208       (push (gnus-data-number (car data)) children)
2209       (setq data (cdr data)))
2210     children))
2211
2212 (defmacro gnus-summary-skip-intangible ()
2213   "If the current article is intangible, then jump to a different article."
2214   '(let ((to (get-text-property (point) 'gnus-intangible)))
2215      (and to (gnus-summary-goto-subject to))))
2216
2217 (defmacro gnus-summary-article-intangible-p ()
2218   "Say whether this article is intangible or not."
2219   '(get-text-property (point) 'gnus-intangible))
2220
2221 (defun gnus-article-read-p (article)
2222   "Say whether ARTICLE is read or not."
2223   (not (or (memq article gnus-newsgroup-marked)
2224            (memq article gnus-newsgroup-unreads)
2225            (memq article gnus-newsgroup-unselected)
2226            (memq article gnus-newsgroup-dormant))))
2227
2228 ;; Some summary mode macros.
2229
2230 (defmacro gnus-summary-article-number ()
2231   "The article number of the article on the current line.
2232 If there isn's an article number here, then we return the current
2233 article number."
2234   '(progn
2235      (gnus-summary-skip-intangible)
2236      (or (get-text-property (point) 'gnus-number)
2237          (gnus-summary-last-subject))))
2238
2239 (defmacro gnus-summary-article-header (&optional number)
2240   "Return the header of article NUMBER."
2241   `(gnus-data-header (gnus-data-find
2242                       ,(or number '(gnus-summary-article-number)))))
2243
2244 (defmacro gnus-summary-thread-level (&optional number)
2245   "Return the level of thread that starts with article NUMBER."
2246   `(if (and (eq gnus-summary-make-false-root 'dummy)
2247             (get-text-property (point) 'gnus-intangible))
2248        0
2249      (gnus-data-level (gnus-data-find
2250                        ,(or number '(gnus-summary-article-number))))))
2251
2252 (defmacro gnus-summary-article-mark (&optional number)
2253   "Return the mark of article NUMBER."
2254   `(gnus-data-mark (gnus-data-find
2255                     ,(or number '(gnus-summary-article-number)))))
2256
2257 (defmacro gnus-summary-article-pos (&optional number)
2258   "Return the position of the line of article NUMBER."
2259   `(gnus-data-pos (gnus-data-find
2260                    ,(or number '(gnus-summary-article-number)))))
2261
2262 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2263 (defmacro gnus-summary-article-subject (&optional number)
2264   "Return current subject string or nil if nothing."
2265   `(let ((headers
2266           ,(if number
2267                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2268              '(gnus-data-header (assq (gnus-summary-article-number)
2269                                       gnus-newsgroup-data)))))
2270      (and headers
2271           (vectorp headers)
2272           (mail-header-subject headers))))
2273
2274 (defmacro gnus-summary-article-score (&optional number)
2275   "Return current article score."
2276   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2277                   gnus-newsgroup-scored))
2278        gnus-summary-default-score 0))
2279
2280 (defun gnus-summary-article-children (&optional number)
2281   "Return a list of article numbers that are children of article NUMBER."
2282   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2283          (level (gnus-data-level (car data)))
2284          l children)
2285     (while (and (setq data (cdr data))
2286                 (> (setq l (gnus-data-level (car data))) level))
2287       (and (= (1+ level) l)
2288            (push (gnus-data-number (car data))
2289                  children)))
2290     (nreverse children)))
2291
2292 (defun gnus-summary-article-parent (&optional number)
2293   "Return the article number of the parent of article NUMBER."
2294   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2295                                     (gnus-data-list t)))
2296          (level (gnus-data-level (car data))))
2297     (if (zerop level)
2298         ()                              ; This is a root.
2299       ;; We search until we find an article with a level less than
2300       ;; this one.  That function has to be the parent.
2301       (while (and (setq data (cdr data))
2302                   (not (< (gnus-data-level (car data)) level))))
2303       (and data (gnus-data-number (car data))))))
2304
2305 (defun gnus-unread-mark-p (mark)
2306   "Say whether MARK is the unread mark."
2307   (= mark gnus-unread-mark))
2308
2309 (defun gnus-read-mark-p (mark)
2310   "Say whether MARK is one of the marks that mark as read.
2311 This is all marks except unread, ticked, dormant, and expirable."
2312   (not (or (= mark gnus-unread-mark)
2313            (= mark gnus-ticked-mark)
2314            (= mark gnus-dormant-mark)
2315            (= mark gnus-expirable-mark))))
2316
2317 (defmacro gnus-article-mark (number)
2318   "Return the MARK of article NUMBER.
2319 This macro should only be used when computing the mark the \"first\"
2320 time; i.e., when generating the summary lines.  After that,
2321 `gnus-summary-article-mark' should be used to examine the
2322 marks of articles."
2323   `(cond
2324     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2325     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2326     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2327     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2328     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2329     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2330     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2331     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2332            gnus-ancient-mark))))
2333
2334 ;; Saving hidden threads.
2335
2336 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2337 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2338
2339 (defmacro gnus-save-hidden-threads (&rest forms)
2340   "Save hidden threads, eval FORMS, and restore the hidden threads."
2341   (let ((config (make-symbol "config")))
2342     `(let ((,config (gnus-hidden-threads-configuration)))
2343        (unwind-protect
2344            (save-excursion
2345              ,@forms)
2346          (gnus-restore-hidden-threads-configuration ,config)))))
2347
2348 (defun gnus-data-compute-positions ()
2349   "Compute the positions of all articles."
2350   (setq gnus-newsgroup-data-reverse nil)
2351   (let ((data gnus-newsgroup-data))
2352     (save-excursion
2353       (gnus-save-hidden-threads
2354         (gnus-summary-show-all-threads)
2355         (goto-char (point-min))
2356         (while data
2357           (while (get-text-property (point) 'gnus-intangible)
2358             (forward-line 1))
2359           (gnus-data-set-pos (car data) (+ (point) 3))
2360           (setq data (cdr data))
2361           (forward-line 1))))))
2362
2363 (defun gnus-hidden-threads-configuration ()
2364   "Return the current hidden threads configuration."
2365   (save-excursion
2366     (let (config)
2367       (goto-char (point-min))
2368       (while (search-forward "\r" nil t)
2369         (push (1- (point)) config))
2370       config)))
2371
2372 (defun gnus-restore-hidden-threads-configuration (config)
2373   "Restore hidden threads configuration from CONFIG."
2374   (let (point buffer-read-only)
2375     (while (setq point (pop config))
2376       (when (and (< point (point-max))
2377                  (goto-char point)
2378                  (eq (char-after) ?\n))
2379         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2380
2381 ;; Various summary mode internalish functions.
2382
2383 (defun gnus-mouse-pick-article (e)
2384   (interactive "e")
2385   (mouse-set-point e)
2386   (gnus-summary-next-page nil t))
2387
2388 (defun gnus-summary-set-display-table ()
2389   ;; Change the display table.  Odd characters have a tendency to mess
2390   ;; up nicely formatted displays - we make all possible glyphs
2391   ;; display only a single character.
2392
2393   ;; We start from the standard display table, if any.
2394   (let ((table (or (copy-sequence standard-display-table)
2395                    (make-display-table)))
2396         (i 32))
2397     ;; Nix out all the control chars...
2398     (while (>= (setq i (1- i)) 0)
2399       (aset table i [??]))
2400     ;; ... but not newline and cr, of course.  (cr is necessary for the
2401     ;; selective display).
2402     (aset table ?\n nil)
2403     (aset table ?\r nil)
2404     ;; We keep TAB as well.
2405     (aset table ?\t nil)
2406     ;; We nix out any glyphs over 126 that are not set already.
2407     (let ((i 256))
2408       (while (>= (setq i (1- i)) 127)
2409         ;; Only modify if the entry is nil.
2410         (unless (aref table i)
2411           (aset table i [??]))))
2412     (setq buffer-display-table table)))
2413
2414 (defun gnus-summary-setup-buffer (group)
2415   "Initialize summary buffer."
2416   (let ((buffer (concat "*Summary " group "*")))
2417     (if (get-buffer buffer)
2418         (progn
2419           (set-buffer buffer)
2420           (setq gnus-summary-buffer (current-buffer))
2421           (not gnus-newsgroup-prepared))
2422       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2423       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2424       (gnus-summary-mode group)
2425       (when gnus-carpal
2426         (gnus-carpal-setup-buffer 'summary))
2427       (unless gnus-single-article-buffer
2428         (make-local-variable 'gnus-article-buffer)
2429         (make-local-variable 'gnus-article-current)
2430         (make-local-variable 'gnus-original-article-buffer))
2431       (setq gnus-newsgroup-name group)
2432       t)))
2433
2434 (defun gnus-set-global-variables ()
2435   ;; Set the global equivalents of the summary buffer-local variables
2436   ;; to the latest values they had.  These reflect the summary buffer
2437   ;; that was in action when the last article was fetched.
2438   (when (eq major-mode 'gnus-summary-mode)
2439     (setq gnus-summary-buffer (current-buffer))
2440     (let ((name gnus-newsgroup-name)
2441           (marked gnus-newsgroup-marked)
2442           (unread gnus-newsgroup-unreads)
2443           (headers gnus-current-headers)
2444           (data gnus-newsgroup-data)
2445           (summary gnus-summary-buffer)
2446           (article-buffer gnus-article-buffer)
2447           (original gnus-original-article-buffer)
2448           (gac gnus-article-current)
2449           (reffed gnus-reffed-article-number)
2450           (score-file gnus-current-score-file)
2451           (default-charset gnus-newsgroup-charset))
2452       (save-excursion
2453         (set-buffer gnus-group-buffer)
2454         (setq gnus-newsgroup-name name
2455               gnus-newsgroup-marked marked
2456               gnus-newsgroup-unreads unread
2457               gnus-current-headers headers
2458               gnus-newsgroup-data data
2459               gnus-article-current gac
2460               gnus-summary-buffer summary
2461               gnus-article-buffer article-buffer
2462               gnus-original-article-buffer original
2463               gnus-reffed-article-number reffed
2464               gnus-current-score-file score-file
2465               gnus-newsgroup-charset default-charset)
2466         ;; The article buffer also has local variables.
2467         (when (gnus-buffer-live-p gnus-article-buffer)
2468           (set-buffer gnus-article-buffer)
2469           (setq gnus-summary-buffer summary))))))
2470
2471 (defun gnus-summary-article-unread-p (article)
2472   "Say whether ARTICLE is unread or not."
2473   (memq article gnus-newsgroup-unreads))
2474
2475 (defun gnus-summary-first-article-p (&optional article)
2476   "Return whether ARTICLE is the first article in the buffer."
2477   (if (not (setq article (or article (gnus-summary-article-number))))
2478       nil
2479     (eq article (caar gnus-newsgroup-data))))
2480
2481 (defun gnus-summary-last-article-p (&optional article)
2482   "Return whether ARTICLE is the last article in the buffer."
2483   (if (not (setq article (or article (gnus-summary-article-number))))
2484       ;; All non-existent numbers are the last article.  :-)
2485       t
2486     (not (cdr (gnus-data-find-list article)))))
2487
2488 (defun gnus-make-thread-indent-array ()
2489   (let ((n 200))
2490     (unless (and gnus-thread-indent-array
2491                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2492       (setq gnus-thread-indent-array (make-vector 201 "")
2493             gnus-thread-indent-array-level gnus-thread-indent-level)
2494       (while (>= n 0)
2495         (aset gnus-thread-indent-array n
2496               (make-string (* n gnus-thread-indent-level) ? ))
2497         (setq n (1- n))))))
2498
2499 (defun gnus-update-summary-mark-positions ()
2500   "Compute where the summary marks are to go."
2501   (save-excursion
2502     (when (gnus-buffer-exists-p gnus-summary-buffer)
2503       (set-buffer gnus-summary-buffer))
2504     (let ((gnus-replied-mark 129)
2505           (gnus-score-below-mark 130)
2506           (gnus-score-over-mark 130)
2507           (gnus-download-mark 131)
2508           (spec gnus-summary-line-format-spec)
2509           gnus-visual pos)
2510       (save-excursion
2511         (gnus-set-work-buffer)
2512         (let ((gnus-summary-line-format-spec spec)
2513               (gnus-newsgroup-downloadable '((0 . t))))
2514           (gnus-summary-insert-line
2515            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2516           (goto-char (point-min))
2517           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2518                                              (- (point) 2)))))
2519           (goto-char (point-min))
2520           (push (cons 'replied (and (search-forward "\201" nil t)
2521                                     (- (point) 2)))
2522                 pos)
2523           (goto-char (point-min))
2524           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2525                 pos)
2526           (goto-char (point-min))
2527           (push (cons 'download
2528                       (and (search-forward "\203" nil t) (- (point) 2)))
2529                 pos)))
2530       (setq gnus-summary-mark-positions pos))))
2531
2532 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2533   "Insert a dummy root in the summary buffer."
2534   (beginning-of-line)
2535   (gnus-add-text-properties
2536    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2537    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2538
2539 (defun gnus-summary-from-or-to-or-newsgroups (header)
2540   (let ((to (cdr (assq 'To (mail-header-extra header))))
2541         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2542         (mail-parse-charset gnus-newsgroup-charset)
2543         (mail-parse-ignored-charsets 
2544          (save-excursion (set-buffer gnus-summary-buffer)
2545                          gnus-newsgroup-ignored-charsets)))
2546     (cond
2547      ((and to
2548            gnus-ignored-from-addresses
2549            (string-match gnus-ignored-from-addresses
2550                          (mail-header-from header)))
2551       (concat "-> "
2552               (or (car (funcall gnus-extract-address-components
2553                                 (funcall
2554                                  gnus-decode-encoded-word-function to)))
2555                   (funcall gnus-decode-encoded-word-function to))))
2556      ((and newsgroups
2557            gnus-ignored-from-addresses
2558            (string-match gnus-ignored-from-addresses
2559                          (mail-header-from header)))
2560       (concat "=> " newsgroups))
2561      (t
2562       (or (car (funcall gnus-extract-address-components
2563                         (mail-header-from header)))
2564           (mail-header-from header))))))
2565
2566 (defun gnus-summary-insert-line (gnus-tmp-header
2567                                  gnus-tmp-level gnus-tmp-current
2568                                  gnus-tmp-unread gnus-tmp-replied
2569                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2570                                  &optional gnus-tmp-dummy gnus-tmp-score
2571                                  gnus-tmp-process)
2572   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2573          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2574          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2575          (gnus-tmp-score-char
2576           (if (or (null gnus-summary-default-score)
2577                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2578                       gnus-summary-zcore-fuzz))
2579               ? ;Whitespace
2580             (if (< gnus-tmp-score gnus-summary-default-score)
2581                 gnus-score-below-mark gnus-score-over-mark)))
2582          (gnus-tmp-replied
2583           (cond (gnus-tmp-process gnus-process-mark)
2584                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2585                  gnus-cached-mark)
2586                 (gnus-tmp-replied gnus-replied-mark)
2587                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2588                  gnus-saved-mark)
2589                 (t gnus-unread-mark)))
2590          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2591          (gnus-tmp-name
2592           (cond
2593            ((string-match "<[^>]+> *$" gnus-tmp-from)
2594             (let ((beg (match-beginning 0)))
2595               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2596                        (substring gnus-tmp-from (1+ (match-beginning 0))
2597                                   (1- (match-end 0))))
2598                   (substring gnus-tmp-from 0 beg))))
2599            ((string-match "(.+)" gnus-tmp-from)
2600             (substring gnus-tmp-from
2601                        (1+ (match-beginning 0)) (1- (match-end 0))))
2602            (t gnus-tmp-from)))
2603          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2604          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2605          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2606          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2607          (buffer-read-only nil))
2608     (when (string= gnus-tmp-name "")
2609       (setq gnus-tmp-name gnus-tmp-from))
2610     (unless (numberp gnus-tmp-lines)
2611       (setq gnus-tmp-lines 0))
2612     (gnus-put-text-property
2613      (point)
2614      (progn (eval gnus-summary-line-format-spec) (point))
2615      'gnus-number gnus-tmp-number)
2616     (when (gnus-visual-p 'summary-highlight 'highlight)
2617       (forward-line -1)
2618       (gnus-run-hooks 'gnus-summary-update-hook)
2619       (forward-line 1))))
2620
2621 (defun gnus-summary-update-line (&optional dont-update)
2622   ;; Update summary line after change.
2623   (when (and gnus-summary-default-score
2624              (not gnus-summary-inhibit-highlight))
2625     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2626            (article (gnus-summary-article-number))
2627            (score (gnus-summary-article-score article)))
2628       (unless dont-update
2629         (if (and gnus-summary-mark-below
2630                  (< (gnus-summary-article-score)
2631                     gnus-summary-mark-below))
2632             ;; This article has a low score, so we mark it as read.
2633             (when (memq article gnus-newsgroup-unreads)
2634               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2635           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2636             ;; This article was previously marked as read on account
2637             ;; of a low score, but now it has risen, so we mark it as
2638             ;; unread.
2639             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2640         (gnus-summary-update-mark
2641          (if (or (null gnus-summary-default-score)
2642                  (<= (abs (- score gnus-summary-default-score))
2643                      gnus-summary-zcore-fuzz))
2644              ? ;Whitespace
2645            (if (< score gnus-summary-default-score)
2646                gnus-score-below-mark gnus-score-over-mark))
2647          'score))
2648       ;; Do visual highlighting.
2649       (when (gnus-visual-p 'summary-highlight 'highlight)
2650         (gnus-run-hooks 'gnus-summary-update-hook)))))
2651
2652 (defvar gnus-tmp-new-adopts nil)
2653
2654 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2655   "Return the number of articles in THREAD.
2656 This may be 0 in some cases -- if none of the articles in
2657 the thread are to be displayed."
2658   (let* ((number
2659           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2660           (cond
2661            ((not (listp thread))
2662             1)
2663            ((and (consp thread) (cdr thread))
2664             (apply
2665              '+ 1 (mapcar
2666                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2667            ((null thread)
2668             1)
2669            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2670             1)
2671            (t 0))))
2672     (when (and level (zerop level) gnus-tmp-new-adopts)
2673       (incf number
2674             (apply '+ (mapcar
2675                        'gnus-summary-number-of-articles-in-thread
2676                        gnus-tmp-new-adopts))))
2677     (if char
2678         (if (> number 1) gnus-not-empty-thread-mark
2679           gnus-empty-thread-mark)
2680       number)))
2681
2682 (defun gnus-summary-set-local-parameters (group)
2683   "Go through the local params of GROUP and set all variable specs in that list."
2684   (let ((params (gnus-group-find-parameter group))
2685         elem)
2686     (while params
2687       (setq elem (car params)
2688             params (cdr params))
2689       (and (consp elem)                 ; Has to be a cons.
2690            (consp (cdr elem))           ; The cdr has to be a list.
2691            (symbolp (car elem))         ; Has to be a symbol in there.
2692            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2693            (ignore-errors               ; So we set it.
2694              (make-local-variable (car elem))
2695              (set (car elem) (eval (nth 1 elem))))))))
2696
2697 (defun gnus-summary-read-group (group &optional show-all no-article
2698                                       kill-buffer no-display backward
2699                                       select-articles)
2700   "Start reading news in newsgroup GROUP.
2701 If SHOW-ALL is non-nil, already read articles are also listed.
2702 If NO-ARTICLE is non-nil, no article is selected initially.
2703 If NO-DISPLAY, don't generate a summary buffer."
2704   (let (result)
2705     (while (and group
2706                 (null (setq result
2707                             (let ((gnus-auto-select-next nil))
2708                               (or (gnus-summary-read-group-1
2709                                    group show-all no-article
2710                                    kill-buffer no-display
2711                                    select-articles)
2712                                   (setq show-all nil
2713                                         select-articles nil)))))
2714                 (eq gnus-auto-select-next 'quietly))
2715       (set-buffer gnus-group-buffer)
2716       ;; The entry function called above goes to the next
2717       ;; group automatically, so we go two groups back
2718       ;; if we are searching for the previous group.
2719       (when backward
2720         (gnus-group-prev-unread-group 2))
2721       (if (not (equal group (gnus-group-group-name)))
2722           (setq group (gnus-group-group-name))
2723         (setq group nil)))
2724     result))
2725
2726 (defun gnus-summary-read-group-1 (group show-all no-article
2727                                         kill-buffer no-display
2728                                         &optional select-articles)
2729   ;; Killed foreign groups can't be entered.
2730   (when (and (not (gnus-group-native-p group))
2731              (not (gnus-gethash group gnus-newsrc-hashtb)))
2732     (error "Dead non-native groups can't be entered"))
2733   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2734   (let* ((new-group (gnus-summary-setup-buffer group))
2735          (quit-config (gnus-group-quit-config group))
2736          (did-select (and new-group (gnus-select-newsgroup
2737                                      group show-all select-articles))))
2738     (cond
2739      ;; This summary buffer exists already, so we just select it.
2740      ((not new-group)
2741       (gnus-set-global-variables)
2742       (when kill-buffer
2743         (gnus-kill-or-deaden-summary kill-buffer))
2744       (gnus-configure-windows 'summary 'force)
2745       (gnus-set-mode-line 'summary)
2746       (gnus-summary-position-point)
2747       (message "")
2748       t)
2749      ;; We couldn't select this group.
2750      ((null did-select)
2751       (when (and (eq major-mode 'gnus-summary-mode)
2752                  (not (equal (current-buffer) kill-buffer)))
2753         (kill-buffer (current-buffer))
2754         (if (not quit-config)
2755             (progn
2756               ;; Update the info -- marks might need to be removed,
2757               ;; for instance.
2758               (gnus-summary-update-info)
2759               (set-buffer gnus-group-buffer)
2760               (gnus-group-jump-to-group group)
2761               (gnus-group-next-unread-group 1))
2762           (gnus-handle-ephemeral-exit quit-config)))
2763       (gnus-message 3 "Can't select group")
2764       nil)
2765      ;; The user did a `C-g' while prompting for number of articles,
2766      ;; so we exit this group.
2767      ((eq did-select 'quit)
2768       (and (eq major-mode 'gnus-summary-mode)
2769            (not (equal (current-buffer) kill-buffer))
2770            (kill-buffer (current-buffer)))
2771       (when kill-buffer
2772         (gnus-kill-or-deaden-summary kill-buffer))
2773       (if (not quit-config)
2774           (progn
2775             (set-buffer gnus-group-buffer)
2776             (gnus-group-jump-to-group group)
2777             (gnus-group-next-unread-group 1)
2778             (gnus-configure-windows 'group 'force))
2779         (gnus-handle-ephemeral-exit quit-config))
2780       ;; Finally signal the quit.
2781       (signal 'quit nil))
2782      ;; The group was successfully selected.
2783      (t
2784       (gnus-set-global-variables)
2785       ;; Save the active value in effect when the group was entered.
2786       (setq gnus-newsgroup-active
2787             (gnus-copy-sequence
2788              (gnus-active gnus-newsgroup-name)))
2789       ;; You can change the summary buffer in some way with this hook.
2790       (gnus-run-hooks 'gnus-select-group-hook)
2791       ;; Set any local variables in the group parameters.
2792       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2793       (gnus-update-format-specifications
2794        nil 'summary 'summary-mode 'summary-dummy)
2795       ;; Do score processing.
2796       (when gnus-use-scoring
2797         (gnus-possibly-score-headers))
2798       ;; Check whether to fill in the gaps in the threads.
2799       (when gnus-build-sparse-threads
2800         (gnus-build-sparse-threads))
2801       ;; Find the initial limit.
2802       (if gnus-show-threads
2803           (if show-all
2804               (let ((gnus-newsgroup-dormant nil))
2805                 (gnus-summary-initial-limit show-all))
2806             (gnus-summary-initial-limit show-all))
2807         (setq gnus-newsgroup-limit
2808               (mapcar
2809                (lambda (header) (mail-header-number header))
2810                gnus-newsgroup-headers)))
2811       ;; Generate the summary buffer.
2812       (unless no-display
2813         (gnus-summary-prepare))
2814       (when gnus-use-trees
2815         (gnus-tree-open group)
2816         (setq gnus-summary-highlight-line-function
2817               'gnus-tree-highlight-article))
2818       ;; If the summary buffer is empty, but there are some low-scored
2819       ;; articles or some excluded dormants, we include these in the
2820       ;; buffer.
2821       (when (and (zerop (buffer-size))
2822                  (not no-display))
2823         (cond (gnus-newsgroup-dormant
2824                (gnus-summary-limit-include-dormant))
2825               ((and gnus-newsgroup-scored show-all)
2826                (gnus-summary-limit-include-expunged t))))
2827       ;; Function `gnus-apply-kill-file' must be called in this hook.
2828       (gnus-run-hooks 'gnus-apply-kill-hook)
2829       (if (and (zerop (buffer-size))
2830                (not no-display))
2831           (progn
2832             ;; This newsgroup is empty.
2833             (gnus-summary-catchup-and-exit nil t)
2834             (gnus-message 6 "No unread news")
2835             (when kill-buffer
2836               (gnus-kill-or-deaden-summary kill-buffer))
2837             ;; Return nil from this function.
2838             nil)
2839         ;; Hide conversation thread subtrees.  We cannot do this in
2840         ;; gnus-summary-prepare-hook since kill processing may not
2841         ;; work with hidden articles.
2842         (and gnus-show-threads
2843              gnus-thread-hide-subtree
2844              (gnus-summary-hide-all-threads))
2845         (when kill-buffer
2846           (gnus-kill-or-deaden-summary kill-buffer))
2847         ;; Show first unread article if requested.
2848         (if (and (not no-article)
2849                  (not no-display)
2850                  gnus-newsgroup-unreads
2851                  gnus-auto-select-first)
2852             (progn
2853               (gnus-configure-windows 'summary)
2854               (cond
2855                ((eq gnus-auto-select-first 'best)
2856                 (gnus-summary-best-unread-article))
2857                ((eq gnus-auto-select-first t)
2858                 (gnus-summary-first-unread-article))
2859                ((gnus-functionp gnus-auto-select-first)
2860                 (funcall gnus-auto-select-first))))
2861           ;; Don't select any articles, just move point to the first
2862           ;; article in the group.
2863           (goto-char (point-min))
2864           (gnus-summary-position-point)
2865           (gnus-configure-windows 'summary 'force)
2866           (gnus-set-mode-line 'summary))
2867         (when (get-buffer-window gnus-group-buffer t)
2868           ;; Gotta use windows, because recenter does weird stuff if
2869           ;; the current buffer ain't the displayed window.
2870           (let ((owin (selected-window)))
2871             (select-window (get-buffer-window gnus-group-buffer t))
2872             (when (gnus-group-goto-group group)
2873               (recenter))
2874             (select-window owin)))
2875         ;; Mark this buffer as "prepared".
2876         (setq gnus-newsgroup-prepared t)
2877         (gnus-run-hooks 'gnus-summary-prepared-hook)
2878         t)))))
2879
2880 (defun gnus-summary-prepare ()
2881   "Generate the summary buffer."
2882   (interactive)
2883   (let ((buffer-read-only nil))
2884     (erase-buffer)
2885     (setq gnus-newsgroup-data nil
2886           gnus-newsgroup-data-reverse nil)
2887     (gnus-run-hooks 'gnus-summary-generate-hook)
2888     ;; Generate the buffer, either with threads or without.
2889     (when gnus-newsgroup-headers
2890       (gnus-summary-prepare-threads
2891        (if gnus-show-threads
2892            (gnus-sort-gathered-threads
2893             (funcall gnus-summary-thread-gathering-function
2894                      (gnus-sort-threads
2895                       (gnus-cut-threads (gnus-make-threads)))))
2896          ;; Unthreaded display.
2897          (gnus-sort-articles gnus-newsgroup-headers))))
2898     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2899     ;; Call hooks for modifying summary buffer.
2900     (goto-char (point-min))
2901     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2902
2903 (defsubst gnus-general-simplify-subject (subject)
2904   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2905   (setq subject
2906         (cond
2907          ;; Truncate the subject.
2908          (gnus-simplify-subject-functions
2909           (gnus-map-function gnus-simplify-subject-functions subject))
2910          ((numberp gnus-summary-gather-subject-limit)
2911           (setq subject (gnus-simplify-subject-re subject))
2912           (if (> (length subject) gnus-summary-gather-subject-limit)
2913               (substring subject 0 gnus-summary-gather-subject-limit)
2914             subject))
2915          ;; Fuzzily simplify it.
2916          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2917           (gnus-simplify-subject-fuzzy subject))
2918          ;; Just remove the leading "Re:".
2919          (t
2920           (gnus-simplify-subject-re subject))))
2921
2922   (if (and gnus-summary-gather-exclude-subject
2923            (string-match gnus-summary-gather-exclude-subject subject))
2924       nil                               ; This article shouldn't be gathered
2925     subject))
2926
2927 (defun gnus-summary-simplify-subject-query ()
2928   "Query where the respool algorithm would put this article."
2929   (interactive)
2930   (gnus-summary-select-article)
2931   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2932
2933 (defun gnus-gather-threads-by-subject (threads)
2934   "Gather threads by looking at Subject headers."
2935   (if (not gnus-summary-make-false-root)
2936       threads
2937     (let ((hashtb (gnus-make-hashtable 1024))
2938           (prev threads)
2939           (result threads)
2940           subject hthread whole-subject)
2941       (while threads
2942         (setq subject (gnus-general-simplify-subject
2943                        (setq whole-subject (mail-header-subject
2944                                             (caar threads)))))
2945         (when subject
2946           (if (setq hthread (gnus-gethash subject hashtb))
2947               (progn
2948                 ;; We enter a dummy root into the thread, if we
2949                 ;; haven't done that already.
2950                 (unless (stringp (caar hthread))
2951                   (setcar hthread (list whole-subject (car hthread))))
2952                 ;; We add this new gathered thread to this gathered
2953                 ;; thread.
2954                 (setcdr (car hthread)
2955                         (nconc (cdar hthread) (list (car threads))))
2956                 ;; Remove it from the list of threads.
2957                 (setcdr prev (cdr threads))
2958                 (setq threads prev))
2959             ;; Enter this thread into the hash table.
2960             (gnus-sethash subject threads hashtb)))
2961         (setq prev threads)
2962         (setq threads (cdr threads)))
2963       result)))
2964
2965 (defun gnus-gather-threads-by-references (threads)
2966   "Gather threads by looking at References headers."
2967   (let ((idhashtb (gnus-make-hashtable 1024))
2968         (thhashtb (gnus-make-hashtable 1024))
2969         (prev threads)
2970         (result threads)
2971         ids references id gthread gid entered ref)
2972     (while threads
2973       (when (setq references (mail-header-references (caar threads)))
2974         (setq id (mail-header-id (caar threads))
2975               ids (gnus-split-references references)
2976               entered nil)
2977         (while (setq ref (pop ids))
2978           (setq ids (delete ref ids))
2979           (if (not (setq gid (gnus-gethash ref idhashtb)))
2980               (progn
2981                 (gnus-sethash ref id idhashtb)
2982                 (gnus-sethash id threads thhashtb))
2983             (setq gthread (gnus-gethash gid thhashtb))
2984             (unless entered
2985               ;; We enter a dummy root into the thread, if we
2986               ;; haven't done that already.
2987               (unless (stringp (caar gthread))
2988                 (setcar gthread (list (mail-header-subject (caar gthread))
2989                                       (car gthread))))
2990               ;; We add this new gathered thread to this gathered
2991               ;; thread.
2992               (setcdr (car gthread)
2993                       (nconc (cdar gthread) (list (car threads)))))
2994             ;; Add it into the thread hash table.
2995             (gnus-sethash id gthread thhashtb)
2996             (setq entered t)
2997             ;; Remove it from the list of threads.
2998             (setcdr prev (cdr threads))
2999             (setq threads prev))))
3000       (setq prev threads)
3001       (setq threads (cdr threads)))
3002     result))
3003
3004 (defun gnus-sort-gathered-threads (threads)
3005   "Sort subtreads inside each gathered thread by article number."
3006   (let ((result threads))
3007     (while threads
3008       (when (stringp (caar threads))
3009         (setcdr (car threads)
3010                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
3011       (setq threads (cdr threads)))
3012     result))
3013
3014 (defun gnus-thread-loop-p (root thread)
3015   "Say whether ROOT is in THREAD."
3016   (let ((stack (list thread))
3017         (infloop 0)
3018         th)
3019     (while (setq thread (pop stack))
3020       (setq th (cdr thread))
3021       (while (and th
3022                   (not (eq (caar th) root)))
3023         (pop th))
3024       (if th
3025           ;; We have found a loop.
3026           (let (ref-dep)
3027             (setcdr thread (delq (car th) (cdr thread)))
3028             (if (boundp (setq ref-dep (intern "none"
3029                                               gnus-newsgroup-dependencies)))
3030                 (setcdr (symbol-value ref-dep)
3031                         (nconc (cdr (symbol-value ref-dep))
3032                                (list (car th))))
3033               (set ref-dep (list nil (car th))))
3034             (setq infloop 1
3035                   stack nil))
3036         ;; Push all the subthreads onto the stack.
3037         (push (cdr thread) stack)))
3038     infloop))
3039
3040 (defun gnus-make-threads ()
3041   "Go through the dependency hashtb and find the roots.  Return all threads."
3042   (let (threads)
3043     (while (catch 'infloop
3044              (mapatoms
3045               (lambda (refs)
3046                 ;; Deal with self-referencing References loops.
3047                 (when (and (car (symbol-value refs))
3048                            (not (zerop
3049                                  (apply
3050                                   '+
3051                                   (mapcar
3052                                    (lambda (thread)
3053                                      (gnus-thread-loop-p
3054                                       (car (symbol-value refs)) thread))
3055                                    (cdr (symbol-value refs)))))))
3056                   (setq threads nil)
3057                   (throw 'infloop t))
3058                 (unless (car (symbol-value refs))
3059                   ;; These threads do not refer back to any other articles,
3060                   ;; so they're roots.
3061                   (setq threads (append (cdr (symbol-value refs)) threads))))
3062               gnus-newsgroup-dependencies)))
3063     threads))
3064
3065 ;; Build the thread tree.
3066 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3067   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3068
3069 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3070 if it was already present.
3071
3072 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3073 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3074 Message-IDs will be renamed be renamed to a unique Message-ID before
3075 being entered.
3076
3077 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3078   (let* ((id (mail-header-id header))
3079          (id-dep (and id (intern id dependencies)))
3080          ref ref-dep ref-header)
3081     ;; Enter this `header' in the `dependencies' table.
3082     (cond
3083      ((not id-dep)
3084       (setq header nil))
3085      ;; The first two cases do the normal part: enter a new `header'
3086      ;; in the `dependencies' table.
3087      ((not (boundp id-dep))
3088       (set id-dep (list header)))
3089      ((null (car (symbol-value id-dep)))
3090       (setcar (symbol-value id-dep) header))
3091
3092      ;; From here the `header' was already present in the
3093      ;; `dependencies' table.
3094      (force-new
3095       ;; Overrides an existing entry;
3096       ;; just set the header part of the entry.
3097       (setcar (symbol-value id-dep) header))
3098
3099      ;; Renames the existing `header' to a unique Message-ID.
3100      ((not gnus-summary-ignore-duplicates)
3101       ;; An article with this Message-ID has already been seen.
3102       ;; We rename the Message-ID.
3103       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3104            (list header))
3105       (mail-header-set-id header id))
3106
3107      ;; The last case ignores an existing entry, except it adds any
3108      ;; additional Xrefs (in case the two articles came from different
3109      ;; servers.
3110      ;; Also sets `header' to `nil' meaning that the `dependencies'
3111      ;; table was *not* modified.
3112      (t
3113       (mail-header-set-xref
3114        (car (symbol-value id-dep))
3115        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3116                    "")
3117                (or (mail-header-xref header) "")))
3118       (setq header nil)))
3119
3120     (when header
3121       ;; First check if that we are not creating a References loop.
3122       (setq ref (gnus-parent-id (mail-header-references header)))
3123       (while (and ref
3124                   (setq ref-dep (intern-soft ref dependencies))
3125                   (boundp ref-dep)
3126                   (setq ref-header (car (symbol-value ref-dep))))
3127         (if (string= id ref)
3128             ;; Yuk!  This is a reference loop.  Make the article be a
3129             ;; root article.
3130             (progn
3131               (mail-header-set-references (car (symbol-value id-dep)) "none")
3132               (setq ref nil))
3133           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3134       (setq ref (gnus-parent-id (mail-header-references header)))
3135       (setq ref-dep (intern (or ref "none") dependencies))
3136       (if (boundp ref-dep)
3137           (setcdr (symbol-value ref-dep)
3138                   (nconc (cdr (symbol-value ref-dep))
3139                          (list (symbol-value id-dep))))
3140         (set ref-dep (list nil (symbol-value id-dep)))))
3141     header))
3142
3143 (defun gnus-build-sparse-threads ()
3144   (let ((headers gnus-newsgroup-headers)
3145         (gnus-summary-ignore-duplicates t)
3146         header references generation relations
3147         subject child end new-child date)
3148     ;; First we create an alist of generations/relations, where
3149     ;; generations is how much we trust the relation, and the relation
3150     ;; is parent/child.
3151     (gnus-message 7 "Making sparse threads...")
3152     (save-excursion
3153       (nnheader-set-temp-buffer " *gnus sparse threads*")
3154       (while (setq header (pop headers))
3155         (when (and (setq references (mail-header-references header))
3156                    (not (string= references "")))
3157           (insert references)
3158           (setq child (mail-header-id header)
3159                 subject (mail-header-subject header)
3160                 date (mail-header-date header)
3161                 generation 0)
3162           (while (search-backward ">" nil t)
3163             (setq end (1+ (point)))
3164             (when (search-backward "<" nil t)
3165               (setq new-child (buffer-substring (point) end))
3166               (push (list (incf generation)
3167                           child (setq child new-child)
3168                           subject date)
3169                     relations)))
3170           (when child
3171             (push (list (1+ generation) child nil subject) relations))
3172           (erase-buffer)))
3173       (kill-buffer (current-buffer)))
3174     ;; Sort over trustworthiness.
3175     (mapcar
3176      (lambda (relation)
3177        (when (gnus-dependencies-add-header
3178               (make-full-mail-header
3179                gnus-reffed-article-number
3180                (nth 3 relation) "" (or (nth 4 relation) "")
3181                (nth 1 relation)
3182                (or (nth 2 relation) "") 0 0 "")
3183               gnus-newsgroup-dependencies nil)
3184          (push gnus-reffed-article-number gnus-newsgroup-limit)
3185          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3186          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3187                gnus-newsgroup-reads)
3188          (decf gnus-reffed-article-number)))
3189      (sort relations 'car-less-than-car))
3190     (gnus-message 7 "Making sparse threads...done")))
3191
3192 (defun gnus-build-old-threads ()
3193   ;; Look at all the articles that refer back to old articles, and
3194   ;; fetch the headers for the articles that aren't there.  This will
3195   ;; build complete threads - if the roots haven't been expired by the
3196   ;; server, that is.
3197   (let (id heads)
3198     (mapatoms
3199      (lambda (refs)
3200        (when (not (car (symbol-value refs)))
3201          (setq heads (cdr (symbol-value refs)))
3202          (while heads
3203            (if (memq (mail-header-number (caar heads))
3204                      gnus-newsgroup-dormant)
3205                (setq heads (cdr heads))
3206              (setq id (symbol-name refs))
3207              (while (and (setq id (gnus-build-get-header id))
3208                          (not (car (gnus-id-to-thread id)))))
3209              (setq heads nil)))))
3210      gnus-newsgroup-dependencies)))
3211
3212 ;; This function has to be called with point after the article number
3213 ;; on the beginning of the line.
3214 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3215   (let ((eol (gnus-point-at-eol))
3216         (buffer (current-buffer))
3217         header)
3218
3219     ;; overview: [num subject from date id refs chars lines misc]
3220     (unwind-protect
3221         (progn
3222           (narrow-to-region (point) eol)
3223           (unless (eobp)
3224             (forward-char))
3225
3226           (setq header
3227                 (make-full-mail-header
3228                  number                         ; number
3229                  (funcall gnus-decode-encoded-word-function
3230                           (nnheader-nov-field)) ; subject
3231                  (funcall gnus-decode-encoded-word-function
3232                           (nnheader-nov-field)) ; from
3233                  (nnheader-nov-field)           ; date
3234                  (nnheader-nov-read-message-id) ; id
3235                  (nnheader-nov-field)           ; refs
3236                  (nnheader-nov-read-integer)    ; chars
3237                  (nnheader-nov-read-integer)    ; lines
3238                  (unless (eobp)
3239                    (nnheader-nov-field))                ; misc
3240                  (nnheader-nov-parse-extra))))  ; extra
3241
3242       (widen))
3243
3244     (when gnus-alter-header-function
3245       (funcall gnus-alter-header-function header))
3246     (gnus-dependencies-add-header header dependencies force-new)))
3247
3248 (defun gnus-build-get-header (id)
3249   ;; Look through the buffer of NOV lines and find the header to
3250   ;; ID.  Enter this line into the dependencies hash table, and return
3251   ;; the id of the parent article (if any).
3252   (let ((deps gnus-newsgroup-dependencies)
3253         found header)
3254     (prog1
3255         (save-excursion
3256           (set-buffer nntp-server-buffer)
3257           (let ((case-fold-search nil))
3258             (goto-char (point-min))
3259             (while (and (not found)
3260                         (search-forward id nil t))
3261               (beginning-of-line)
3262               (setq found (looking-at
3263                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3264                                    (regexp-quote id))))
3265               (or found (beginning-of-line 2)))
3266             (when found
3267               (beginning-of-line)
3268               (and
3269                (setq header (gnus-nov-parse-line
3270                              (read (current-buffer)) deps))
3271                (gnus-parent-id (mail-header-references header))))))
3272       (when header
3273         (let ((number (mail-header-number header)))
3274           (push number gnus-newsgroup-limit)
3275           (push header gnus-newsgroup-headers)
3276           (if (memq number gnus-newsgroup-unselected)
3277               (progn
3278                 (push number gnus-newsgroup-unreads)
3279                 (setq gnus-newsgroup-unselected
3280                       (delq number gnus-newsgroup-unselected)))
3281             (push number gnus-newsgroup-ancient)))))))
3282
3283 (defun gnus-build-all-threads ()
3284   "Read all the headers."
3285   (let ((gnus-summary-ignore-duplicates t)
3286         (dependencies gnus-newsgroup-dependencies)
3287         header article)
3288     (save-excursion
3289       (set-buffer nntp-server-buffer)
3290       (let ((case-fold-search nil))
3291         (goto-char (point-min))
3292         (while (not (eobp))
3293           (ignore-errors
3294             (setq article (read (current-buffer))
3295                   header (gnus-nov-parse-line
3296                           article dependencies)))
3297           (when header
3298             (save-excursion
3299               (set-buffer gnus-summary-buffer)
3300               (push header gnus-newsgroup-headers)
3301               (if (memq (setq article (mail-header-number header))
3302                         gnus-newsgroup-unselected)
3303                   (progn
3304                     (push article gnus-newsgroup-unreads)
3305                     (setq gnus-newsgroup-unselected
3306                           (delq article gnus-newsgroup-unselected)))
3307                 (push article gnus-newsgroup-ancient)))
3308             (forward-line 1)))))))
3309
3310 (defun gnus-summary-update-article-line (article header)
3311   "Update the line for ARTICLE using HEADERS."
3312   (let* ((id (mail-header-id header))
3313          (thread (gnus-id-to-thread id)))
3314     (unless thread
3315       (error "Article in no thread"))
3316     ;; Update the thread.
3317     (setcar thread header)
3318     (gnus-summary-goto-subject article)
3319     (let* ((datal (gnus-data-find-list article))
3320            (data (car datal))
3321            (length (when (cdr datal)
3322                      (- (gnus-data-pos data)
3323                         (gnus-data-pos (cadr datal)))))
3324            (buffer-read-only nil)
3325            (level (gnus-summary-thread-level)))
3326       (gnus-delete-line)
3327       (gnus-summary-insert-line
3328        header level nil (gnus-article-mark article)
3329        (memq article gnus-newsgroup-replied)
3330        (memq article gnus-newsgroup-expirable)
3331        ;; Only insert the Subject string when it's different
3332        ;; from the previous Subject string.
3333        (if (gnus-subject-equal
3334             (condition-case ()
3335                 (mail-header-subject
3336                  (gnus-data-header
3337                   (cadr
3338                    (gnus-data-find-list
3339                     article
3340                     (gnus-data-list t)))))
3341               ;; Error on the side of excessive subjects.
3342               (error ""))
3343             (mail-header-subject header))
3344            ""
3345          (mail-header-subject header))
3346        nil (cdr (assq article gnus-newsgroup-scored))
3347        (memq article gnus-newsgroup-processable))
3348       (when length
3349         (gnus-data-update-list
3350          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3351
3352 (defun gnus-summary-update-article (article &optional iheader)
3353   "Update ARTICLE in the summary buffer."
3354   (set-buffer gnus-summary-buffer)
3355   (let* ((header (gnus-summary-article-header article))
3356          (id (mail-header-id header))
3357          (data (gnus-data-find article))
3358          (thread (gnus-id-to-thread id))
3359          (references (mail-header-references header))
3360          (parent
3361           (gnus-id-to-thread
3362            (or (gnus-parent-id
3363                 (when (and references
3364                            (not (equal "" references)))
3365                   references))
3366                "none")))
3367          (buffer-read-only nil)
3368          (old (car thread)))
3369     (when thread
3370       (unless iheader
3371         (setcar thread nil)
3372         (when parent
3373           (delq thread parent)))
3374       (if (gnus-summary-insert-subject id header)
3375           ;; Set the (possibly) new article number in the data structure.
3376           (gnus-data-set-number data (gnus-id-to-article id))
3377         (setcar thread old)
3378         nil))))
3379
3380 (defun gnus-rebuild-thread (id &optional line)
3381   "Rebuild the thread containing ID.
3382 If LINE, insert the rebuilt thread starting on line LINE."
3383   (let ((buffer-read-only nil)
3384         old-pos current thread data)
3385     (if (not gnus-show-threads)
3386         (setq thread (list (car (gnus-id-to-thread id))))
3387       ;; Get the thread this article is part of.
3388       (setq thread (gnus-remove-thread id)))
3389     (setq old-pos (gnus-point-at-bol))
3390     (setq current (save-excursion
3391                     (and (zerop (forward-line -1))
3392                          (gnus-summary-article-number))))
3393     ;; If this is a gathered thread, we have to go some re-gathering.
3394     (when (stringp (car thread))
3395       (let ((subject (car thread))
3396             roots thr)
3397         (setq thread (cdr thread))
3398         (while thread
3399           (unless (memq (setq thr (gnus-id-to-thread
3400                                    (gnus-root-id
3401                                     (mail-header-id (caar thread)))))
3402                         roots)
3403             (push thr roots))
3404           (setq thread (cdr thread)))
3405         ;; We now have all (unique) roots.
3406         (if (= (length roots) 1)
3407             ;; All the loose roots are now one solid root.
3408             (setq thread (car roots))
3409           (setq thread (cons subject (gnus-sort-threads roots))))))
3410     (let (threads)
3411       ;; We then insert this thread into the summary buffer.
3412       (when line
3413         (goto-char (point-min))
3414         (forward-line (1- line)))
3415       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3416         (if gnus-show-threads
3417             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3418           (gnus-summary-prepare-unthreaded thread))
3419         (setq data (nreverse gnus-newsgroup-data))
3420         (setq threads gnus-newsgroup-threads))
3421       ;; We splice the new data into the data structure.
3422       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3423       ;;!!! then we want to insert at the beginning of the buffer.
3424       ;;!!! That happens to be true with Gnus now, but that may
3425       ;;!!! change in the future.  Perhaps.
3426       (gnus-data-enter-list
3427        (if line nil current) data (- (point) old-pos))
3428       (setq gnus-newsgroup-threads
3429             (nconc threads gnus-newsgroup-threads))
3430       (gnus-data-compute-positions))))
3431
3432 (defun gnus-number-to-header (number)
3433   "Return the header for article NUMBER."
3434   (let ((headers gnus-newsgroup-headers))
3435     (while (and headers
3436                 (not (= number (mail-header-number (car headers)))))
3437       (pop headers))
3438     (when headers
3439       (car headers))))
3440
3441 (defun gnus-parent-headers (in-headers &optional generation)
3442   "Return the headers of the GENERATIONeth parent of HEADERS."
3443   (unless generation
3444     (setq generation 1))
3445   (let ((parent t)
3446         (headers in-headers)
3447         references)
3448     (while (and parent
3449                 (not (zerop generation))
3450                 (setq references (mail-header-references headers)))
3451       (setq headers (if (and references
3452                              (setq parent (gnus-parent-id references)))
3453                         (car (gnus-id-to-thread parent))
3454                       nil))
3455       (decf generation))
3456     (and (not (eq headers in-headers))
3457          headers)))
3458
3459 (defun gnus-id-to-thread (id)
3460   "Return the (sub-)thread where ID appears."
3461   (gnus-gethash id gnus-newsgroup-dependencies))
3462
3463 (defun gnus-id-to-article (id)
3464   "Return the article number of ID."
3465   (let ((thread (gnus-id-to-thread id)))
3466     (when (and thread
3467                (car thread))
3468       (mail-header-number (car thread)))))
3469
3470 (defun gnus-id-to-header (id)
3471   "Return the article headers of ID."
3472   (car (gnus-id-to-thread id)))
3473
3474 (defun gnus-article-displayed-root-p (article)
3475   "Say whether ARTICLE is a root(ish) article."
3476   (let ((level (gnus-summary-thread-level article))
3477         (refs (mail-header-references  (gnus-summary-article-header article)))
3478         particle)
3479     (cond
3480      ((null level) nil)
3481      ((zerop level) t)
3482      ((null refs) t)
3483      ((null (gnus-parent-id refs)) t)
3484      ((and (= 1 level)
3485            (null (setq particle (gnus-id-to-article
3486                                  (gnus-parent-id refs))))
3487            (null (gnus-summary-thread-level particle)))))))
3488
3489 (defun gnus-root-id (id)
3490   "Return the id of the root of the thread where ID appears."
3491   (let (last-id prev)
3492     (while (and id (setq prev (car (gnus-id-to-thread id))))
3493       (setq last-id id
3494             id (gnus-parent-id (mail-header-references prev))))
3495     last-id))
3496
3497 (defun gnus-articles-in-thread (thread)
3498   "Return the list of articles in THREAD."
3499   (cons (mail-header-number (car thread))
3500         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3501
3502 (defun gnus-remove-thread (id &optional dont-remove)
3503   "Remove the thread that has ID in it."
3504   (let (headers thread last-id)
3505     ;; First go up in this thread until we find the root.
3506     (setq last-id (gnus-root-id id)
3507           headers (message-flatten-list (gnus-id-to-thread last-id)))
3508     ;; We have now found the real root of this thread.  It might have
3509     ;; been gathered into some loose thread, so we have to search
3510     ;; through the threads to find the thread we wanted.
3511     (let ((threads gnus-newsgroup-threads)
3512           sub)
3513       (while threads
3514         (setq sub (car threads))
3515         (if (stringp (car sub))
3516             ;; This is a gathered thread, so we look at the roots
3517             ;; below it to find whether this article is in this
3518             ;; gathered root.
3519             (progn
3520               (setq sub (cdr sub))
3521               (while sub
3522                 (when (member (caar sub) headers)
3523                   (setq thread (car threads)
3524                         threads nil
3525                         sub nil))
3526                 (setq sub (cdr sub))))
3527           ;; It's an ordinary thread, so we check it.
3528           (when (eq (car sub) (car headers))
3529             (setq thread sub
3530                   threads nil)))
3531         (setq threads (cdr threads)))
3532       ;; If this article is in no thread, then it's a root.
3533       (if thread
3534           (unless dont-remove
3535             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3536         (setq thread (gnus-id-to-thread last-id)))
3537       (when thread
3538         (prog1
3539             thread                      ; We return this thread.
3540           (unless dont-remove
3541             (if (stringp (car thread))
3542                 (progn
3543                   ;; If we use dummy roots, then we have to remove the
3544                   ;; dummy root as well.
3545                   (when (eq gnus-summary-make-false-root 'dummy)
3546                     ;; We go to the dummy root by going to
3547                     ;; the first sub-"thread", and then one line up.
3548                     (gnus-summary-goto-article
3549                      (mail-header-number (caadr thread)))
3550                     (forward-line -1)
3551                     (gnus-delete-line)
3552                     (gnus-data-compute-positions))
3553                   (setq thread (cdr thread))
3554                   (while thread
3555                     (gnus-remove-thread-1 (car thread))
3556                     (setq thread (cdr thread))))
3557               (gnus-summary-show-all-threads)
3558               (gnus-remove-thread-1 thread))))))))
3559
3560 (defun gnus-remove-thread-1 (thread)
3561   "Remove the thread THREAD recursively."
3562   (let ((number (mail-header-number (pop thread)))
3563         d)
3564     (setq thread (reverse thread))
3565     (while thread
3566       (gnus-remove-thread-1 (pop thread)))
3567     (when (setq d (gnus-data-find number))
3568       (goto-char (gnus-data-pos d))
3569       (gnus-data-remove
3570        number
3571        (- (gnus-point-at-bol)
3572           (prog1
3573               (1+ (gnus-point-at-eol))
3574             (gnus-delete-line)))))))
3575
3576 (defun gnus-sort-threads (threads)
3577   "Sort THREADS."
3578   (if (not gnus-thread-sort-functions)
3579       threads
3580     (gnus-message 8 "Sorting threads...")
3581     (prog1
3582         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3583       (gnus-message 8 "Sorting threads...done"))))
3584
3585 (defun gnus-sort-articles (articles)
3586   "Sort ARTICLES."
3587   (when gnus-article-sort-functions
3588     (gnus-message 7 "Sorting articles...")
3589     (prog1
3590         (setq gnus-newsgroup-headers
3591               (sort articles (gnus-make-sort-function
3592                               gnus-article-sort-functions)))
3593       (gnus-message 7 "Sorting articles...done"))))
3594
3595 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3596 (defmacro gnus-thread-header (thread)
3597   ;; Return header of first article in THREAD.
3598   ;; Note that THREAD must never, ever be anything else than a variable -
3599   ;; using some other form will lead to serious barfage.
3600   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3601   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3602   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3603         (vector thread) 2))
3604
3605 (defsubst gnus-article-sort-by-number (h1 h2)
3606   "Sort articles by article number."
3607   (< (mail-header-number h1)
3608      (mail-header-number h2)))
3609
3610 (defun gnus-thread-sort-by-number (h1 h2)
3611   "Sort threads by root article number."
3612   (gnus-article-sort-by-number
3613    (gnus-thread-header h1) (gnus-thread-header h2)))
3614
3615 (defsubst gnus-article-sort-by-lines (h1 h2)
3616   "Sort articles by article Lines header."
3617   (< (mail-header-lines h1)
3618      (mail-header-lines h2)))
3619
3620 (defun gnus-thread-sort-by-lines (h1 h2)
3621   "Sort threads by root article Lines header."
3622   (gnus-article-sort-by-lines
3623    (gnus-thread-header h1) (gnus-thread-header h2)))
3624
3625 (defsubst gnus-article-sort-by-chars (h1 h2)
3626   "Sort articles by octet length."
3627   (< (mail-header-chars h1)
3628      (mail-header-chars h2)))
3629
3630 (defun gnus-thread-sort-by-chars (h1 h2)
3631   "Sort threads by root article octet length."
3632   (gnus-article-sort-by-chars
3633    (gnus-thread-header h1) (gnus-thread-header h2)))
3634
3635 (defsubst gnus-article-sort-by-author (h1 h2)
3636   "Sort articles by root author."
3637   (string-lessp
3638    (let ((extract (funcall
3639                    gnus-extract-address-components
3640                    (mail-header-from h1))))
3641      (or (car extract) (cadr extract) ""))
3642    (let ((extract (funcall
3643                    gnus-extract-address-components
3644                    (mail-header-from h2))))
3645      (or (car extract) (cadr extract) ""))))
3646
3647 (defun gnus-thread-sort-by-author (h1 h2)
3648   "Sort threads by root author."
3649   (gnus-article-sort-by-author
3650    (gnus-thread-header h1)  (gnus-thread-header h2)))
3651
3652 (defsubst gnus-article-sort-by-subject (h1 h2)
3653   "Sort articles by root subject."
3654   (string-lessp
3655    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3656    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3657
3658 (defun gnus-thread-sort-by-subject (h1 h2)
3659   "Sort threads by root subject."
3660   (gnus-article-sort-by-subject
3661    (gnus-thread-header h1) (gnus-thread-header h2)))
3662
3663 (defsubst gnus-article-sort-by-date (h1 h2)
3664   "Sort articles by root article date."
3665   (time-less-p
3666    (gnus-date-get-time (mail-header-date h1))
3667    (gnus-date-get-time (mail-header-date h2))))
3668
3669 (defun gnus-thread-sort-by-date (h1 h2)
3670   "Sort threads by root article date."
3671   (gnus-article-sort-by-date
3672    (gnus-thread-header h1) (gnus-thread-header h2)))
3673
3674 (defsubst gnus-article-sort-by-score (h1 h2)
3675   "Sort articles by root article score.
3676 Unscored articles will be counted as having a score of zero."
3677   (> (or (cdr (assq (mail-header-number h1)
3678                     gnus-newsgroup-scored))
3679          gnus-summary-default-score 0)
3680      (or (cdr (assq (mail-header-number h2)
3681                     gnus-newsgroup-scored))
3682          gnus-summary-default-score 0)))
3683
3684 (defun gnus-thread-sort-by-score (h1 h2)
3685   "Sort threads by root article score."
3686   (gnus-article-sort-by-score
3687    (gnus-thread-header h1) (gnus-thread-header h2)))
3688
3689 (defun gnus-thread-sort-by-total-score (h1 h2)
3690   "Sort threads by the sum of all scores in the thread.
3691 Unscored articles will be counted as having a score of zero."
3692   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3693
3694 (defun gnus-thread-total-score (thread)
3695   ;; This function find the total score of THREAD.
3696   (cond ((null thread)
3697          0)
3698         ((consp thread)
3699          (if (stringp (car thread))
3700              (apply gnus-thread-score-function 0
3701                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3702            (gnus-thread-total-score-1 thread)))
3703         (t
3704          (gnus-thread-total-score-1 (list thread)))))
3705
3706 (defun gnus-thread-total-score-1 (root)
3707   ;; This function find the total score of the thread below ROOT.
3708   (setq root (car root))
3709   (apply gnus-thread-score-function
3710          (or (append
3711               (mapcar 'gnus-thread-total-score
3712                       (cdr (gnus-id-to-thread (mail-header-id root))))
3713               (when (> (mail-header-number root) 0)
3714                 (list (or (cdr (assq (mail-header-number root)
3715                                      gnus-newsgroup-scored))
3716                           gnus-summary-default-score 0))))
3717              (list gnus-summary-default-score)
3718              '(0))))
3719
3720 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3721 (defvar gnus-tmp-prev-subject nil)
3722 (defvar gnus-tmp-false-parent nil)
3723 (defvar gnus-tmp-root-expunged nil)
3724 (defvar gnus-tmp-dummy-line nil)
3725
3726 (defvar gnus-tmp-header)
3727 (defun gnus-extra-header (type &optional header)
3728   "Return the extra header of TYPE."
3729   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3730       ""))
3731
3732 (defun gnus-summary-prepare-threads (threads)
3733   "Prepare summary buffer from THREADS and indentation LEVEL.
3734 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3735 or a straight list of headers."
3736   (gnus-message 7 "Generating summary...")
3737
3738   (setq gnus-newsgroup-threads threads)
3739   (beginning-of-line)
3740
3741   (let ((gnus-tmp-level 0)
3742         (default-score (or gnus-summary-default-score 0))
3743         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3744         thread number subject stack state gnus-tmp-gathered beg-match
3745         new-roots gnus-tmp-new-adopts thread-end
3746         gnus-tmp-header gnus-tmp-unread
3747         gnus-tmp-replied gnus-tmp-subject-or-nil
3748         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3749         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3750         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3751
3752     (setq gnus-tmp-prev-subject nil)
3753
3754     (if (vectorp (car threads))
3755         ;; If this is a straight (sic) list of headers, then a
3756         ;; threaded summary display isn't required, so we just create
3757         ;; an unthreaded one.
3758         (gnus-summary-prepare-unthreaded threads)
3759
3760       ;; Do the threaded display.
3761
3762       (while (or threads stack gnus-tmp-new-adopts new-roots)
3763
3764         (if (and (= gnus-tmp-level 0)
3765                  (or (not stack)
3766                      (= (caar stack) 0))
3767                  (not gnus-tmp-false-parent)
3768                  (or gnus-tmp-new-adopts new-roots))
3769             (if gnus-tmp-new-adopts
3770                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3771                       thread (list (car gnus-tmp-new-adopts))
3772                       gnus-tmp-header (caar thread)
3773                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3774               (when new-roots
3775                 (setq thread (list (car new-roots))
3776                       gnus-tmp-header (caar thread)
3777                       new-roots (cdr new-roots))))
3778
3779           (if threads
3780               ;; If there are some threads, we do them before the
3781               ;; threads on the stack.
3782               (setq thread threads
3783                     gnus-tmp-header (caar thread))
3784             ;; There were no current threads, so we pop something off
3785             ;; the stack.
3786             (setq state (car stack)
3787                   gnus-tmp-level (car state)
3788                   thread (cdr state)
3789                   stack (cdr stack)
3790                   gnus-tmp-header (caar thread))))
3791
3792         (setq gnus-tmp-false-parent nil)
3793         (setq gnus-tmp-root-expunged nil)
3794         (setq thread-end nil)
3795
3796         (if (stringp gnus-tmp-header)
3797             ;; The header is a dummy root.
3798             (cond
3799              ((eq gnus-summary-make-false-root 'adopt)
3800               ;; We let the first article adopt the rest.
3801               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3802                                                (cddar thread)))
3803               (setq gnus-tmp-gathered
3804                     (nconc (mapcar
3805                             (lambda (h) (mail-header-number (car h)))
3806                             (cddar thread))
3807                            gnus-tmp-gathered))
3808               (setq thread (cons (list (caar thread)
3809                                        (cadar thread))
3810                                  (cdr thread)))
3811               (setq gnus-tmp-level -1
3812                     gnus-tmp-false-parent t))
3813              ((eq gnus-summary-make-false-root 'empty)
3814               ;; We print adopted articles with empty subject fields.
3815               (setq gnus-tmp-gathered
3816                     (nconc (mapcar
3817                             (lambda (h) (mail-header-number (car h)))
3818                             (cddar thread))
3819                            gnus-tmp-gathered))
3820               (setq gnus-tmp-level -1))
3821              ((eq gnus-summary-make-false-root 'dummy)
3822               ;; We remember that we probably want to output a dummy
3823               ;; root.
3824               (setq gnus-tmp-dummy-line gnus-tmp-header)
3825               (setq gnus-tmp-prev-subject gnus-tmp-header))
3826              (t
3827               ;; We do not make a root for the gathered
3828               ;; sub-threads at all.
3829               (setq gnus-tmp-level -1)))
3830
3831           (setq number (mail-header-number gnus-tmp-header)
3832                 subject (mail-header-subject gnus-tmp-header))
3833
3834           (cond
3835            ;; If the thread has changed subject, we might want to make
3836            ;; this subthread into a root.
3837            ((and (null gnus-thread-ignore-subject)
3838                  (not (zerop gnus-tmp-level))
3839                  gnus-tmp-prev-subject
3840                  (not (inline
3841                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3842             (setq new-roots (nconc new-roots (list (car thread)))
3843                   thread-end t
3844                   gnus-tmp-header nil))
3845            ;; If the article lies outside the current limit,
3846            ;; then we do not display it.
3847            ((not (memq number gnus-newsgroup-limit))
3848             (setq gnus-tmp-gathered
3849                   (nconc (mapcar
3850                           (lambda (h) (mail-header-number (car h)))
3851                           (cdar thread))
3852                          gnus-tmp-gathered))
3853             (setq gnus-tmp-new-adopts (if (cdar thread)
3854                                           (append gnus-tmp-new-adopts
3855                                                   (cdar thread))
3856                                         gnus-tmp-new-adopts)
3857                   thread-end t
3858                   gnus-tmp-header nil)
3859             (when (zerop gnus-tmp-level)
3860               (setq gnus-tmp-root-expunged t)))
3861            ;; Perhaps this article is to be marked as read?
3862            ((and gnus-summary-mark-below
3863                  (< (or (cdr (assq number gnus-newsgroup-scored))
3864                         default-score)
3865                     gnus-summary-mark-below)
3866                  ;; Don't touch sparse articles.
3867                  (not (gnus-summary-article-sparse-p number))
3868                  (not (gnus-summary-article-ancient-p number)))
3869             (setq gnus-newsgroup-unreads
3870                   (delq number gnus-newsgroup-unreads))
3871             (if gnus-newsgroup-auto-expire
3872                 (push number gnus-newsgroup-expirable)
3873               (push (cons number gnus-low-score-mark)
3874                     gnus-newsgroup-reads))))
3875
3876           (when gnus-tmp-header
3877             ;; We may have an old dummy line to output before this
3878             ;; article.
3879             (when (and gnus-tmp-dummy-line
3880                        (gnus-subject-equal
3881                         gnus-tmp-dummy-line
3882                         (mail-header-subject gnus-tmp-header)))
3883               (gnus-summary-insert-dummy-line
3884                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3885               (setq gnus-tmp-dummy-line nil))
3886
3887             ;; Compute the mark.
3888             (setq gnus-tmp-unread (gnus-article-mark number))
3889
3890             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3891                                   gnus-tmp-header gnus-tmp-level)
3892                   gnus-newsgroup-data)
3893
3894             ;; Actually insert the line.
3895             (setq
3896              gnus-tmp-subject-or-nil
3897              (cond
3898               ((and gnus-thread-ignore-subject
3899                     gnus-tmp-prev-subject
3900                     (not (inline (gnus-subject-equal
3901                                   gnus-tmp-prev-subject subject))))
3902                subject)
3903               ((zerop gnus-tmp-level)
3904                (if (and (eq gnus-summary-make-false-root 'empty)
3905                         (memq number gnus-tmp-gathered)
3906                         gnus-tmp-prev-subject
3907                         (inline (gnus-subject-equal
3908                                  gnus-tmp-prev-subject subject)))
3909                    gnus-summary-same-subject
3910                  subject))
3911               (t gnus-summary-same-subject)))
3912             (if (and (eq gnus-summary-make-false-root 'adopt)
3913                      (= gnus-tmp-level 1)
3914                      (memq number gnus-tmp-gathered))
3915                 (setq gnus-tmp-opening-bracket ?\<
3916                       gnus-tmp-closing-bracket ?\>)
3917               (setq gnus-tmp-opening-bracket ?\[
3918                     gnus-tmp-closing-bracket ?\]))
3919             (setq
3920              gnus-tmp-indentation
3921              (aref gnus-thread-indent-array gnus-tmp-level)
3922              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3923              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3924                                 gnus-summary-default-score 0)
3925              gnus-tmp-score-char
3926              (if (or (null gnus-summary-default-score)
3927                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3928                          gnus-summary-zcore-fuzz))
3929                  ? ;Whitespace
3930                (if (< gnus-tmp-score gnus-summary-default-score)
3931                    gnus-score-below-mark gnus-score-over-mark))
3932              gnus-tmp-replied
3933              (cond ((memq number gnus-newsgroup-processable)
3934                     gnus-process-mark)
3935                    ((memq number gnus-newsgroup-cached)
3936                     gnus-cached-mark)
3937                    ((memq number gnus-newsgroup-replied)
3938                     gnus-replied-mark)
3939                    ((memq number gnus-newsgroup-saved)
3940                     gnus-saved-mark)
3941                    (t gnus-unread-mark))
3942              gnus-tmp-from (mail-header-from gnus-tmp-header)
3943              gnus-tmp-name
3944              (cond
3945               ((string-match "<[^>]+> *$" gnus-tmp-from)
3946                (setq beg-match (match-beginning 0))
3947                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3948                         (substring gnus-tmp-from (1+ (match-beginning 0))
3949                                    (1- (match-end 0))))
3950                    (substring gnus-tmp-from 0 beg-match)))
3951               ((string-match "(.+)" gnus-tmp-from)
3952                (substring gnus-tmp-from
3953                           (1+ (match-beginning 0)) (1- (match-end 0))))
3954               (t gnus-tmp-from)))
3955             (when (string= gnus-tmp-name "")
3956               (setq gnus-tmp-name gnus-tmp-from))
3957             (unless (numberp gnus-tmp-lines)
3958               (setq gnus-tmp-lines 0))
3959             (gnus-put-text-property
3960              (point)
3961              (progn (eval gnus-summary-line-format-spec) (point))
3962              'gnus-number number)
3963             (when gnus-visual-p
3964               (forward-line -1)
3965               (gnus-run-hooks 'gnus-summary-update-hook)
3966               (forward-line 1))
3967
3968             (setq gnus-tmp-prev-subject subject)))
3969
3970         (when (nth 1 thread)
3971           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
3972         (incf gnus-tmp-level)
3973         (setq threads (if thread-end nil (cdar thread)))
3974         (unless threads
3975           (setq gnus-tmp-level 0)))))
3976   (gnus-message 7 "Generating summary...done"))
3977
3978 (defun gnus-summary-prepare-unthreaded (headers)
3979   "Generate an unthreaded summary buffer based on HEADERS."
3980   (let (header number mark)
3981
3982     (beginning-of-line)
3983
3984     (while headers
3985       ;; We may have to root out some bad articles...
3986       (when (memq (setq number (mail-header-number
3987                                 (setq header (pop headers))))
3988                   gnus-newsgroup-limit)
3989         ;; Mark article as read when it has a low score.
3990         (when (and gnus-summary-mark-below
3991                    (< (or (cdr (assq number gnus-newsgroup-scored))
3992                           gnus-summary-default-score 0)
3993                       gnus-summary-mark-below)
3994                    (not (gnus-summary-article-ancient-p number)))
3995           (setq gnus-newsgroup-unreads
3996                 (delq number gnus-newsgroup-unreads))
3997           (if gnus-newsgroup-auto-expire
3998               (push number gnus-newsgroup-expirable)
3999             (push (cons number gnus-low-score-mark)
4000                   gnus-newsgroup-reads)))
4001
4002         (setq mark (gnus-article-mark number))
4003         (push (gnus-data-make number mark (1+ (point)) header 0)
4004               gnus-newsgroup-data)
4005         (gnus-summary-insert-line
4006          header 0 number
4007          mark (memq number gnus-newsgroup-replied)
4008          (memq number gnus-newsgroup-expirable)
4009          (mail-header-subject header) nil
4010          (cdr (assq number gnus-newsgroup-scored))
4011          (memq number gnus-newsgroup-processable))))))
4012
4013 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4014   "Select newsgroup GROUP.
4015 If READ-ALL is non-nil, all articles in the group are selected.
4016 If SELECT-ARTICLES, only select those articles from GROUP."
4017   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4018          ;;!!! Dirty hack; should be removed.
4019          (gnus-summary-ignore-duplicates
4020           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4021               t
4022             gnus-summary-ignore-duplicates))
4023          (info (nth 2 entry))
4024          articles fetched-articles cached)
4025
4026     (unless (gnus-check-server
4027              (setq gnus-current-select-method
4028                    (gnus-find-method-for-group group)))
4029       (error "Couldn't open server"))
4030
4031     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4032         (gnus-activate-group group)     ; Or we can activate it...
4033         (progn                          ; Or we bug out.
4034           (when (equal major-mode 'gnus-summary-mode)
4035             (kill-buffer (current-buffer)))
4036           (error "Couldn't request group %s: %s"
4037                  group (gnus-status-message group))))
4038
4039     (unless (gnus-request-group group t)
4040       (when (equal major-mode 'gnus-summary-mode)
4041         (kill-buffer (current-buffer)))
4042       (error "Couldn't request group %s: %s"
4043              group (gnus-status-message group)))
4044
4045     (setq gnus-newsgroup-name group)
4046     (setq gnus-newsgroup-unselected nil)
4047     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4048     (gnus-summary-setup-default-charset)
4049
4050     ;; Adjust and set lists of article marks.
4051     (when info
4052       (gnus-adjust-marked-articles info))
4053
4054     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4055     (when (gnus-virtual-group-p group)
4056       (setq cached gnus-newsgroup-cached))
4057
4058     (setq gnus-newsgroup-unreads
4059           (gnus-set-difference
4060            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4061            gnus-newsgroup-dormant))
4062
4063     (setq gnus-newsgroup-processable nil)
4064
4065     (gnus-update-read-articles group gnus-newsgroup-unreads)
4066
4067     (if (setq articles select-articles)
4068         (setq gnus-newsgroup-unselected
4069               (gnus-sorted-intersection
4070                gnus-newsgroup-unreads
4071                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4072       (setq articles (gnus-articles-to-read group read-all)))
4073
4074     (cond
4075      ((null articles)
4076       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4077       'quit)
4078      ((eq articles 0) nil)
4079      (t
4080       ;; Init the dependencies hash table.
4081       (setq gnus-newsgroup-dependencies
4082             (gnus-make-hashtable (length articles)))
4083       (gnus-set-global-variables)
4084       ;; Retrieve the headers and read them in.
4085       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4086       (setq gnus-newsgroup-headers
4087             (if (eq 'nov
4088                     (setq gnus-headers-retrieved-by
4089                           (gnus-retrieve-headers
4090                            articles gnus-newsgroup-name
4091                            ;; We might want to fetch old headers, but
4092                            ;; not if there is only 1 article.
4093                            (and (or (and
4094                                      (not (eq gnus-fetch-old-headers 'some))
4095                                      (not (numberp gnus-fetch-old-headers)))
4096                                     (> (length articles) 1))
4097                                 gnus-fetch-old-headers))))
4098                 (gnus-get-newsgroup-headers-xover
4099                  articles nil nil gnus-newsgroup-name t)
4100               (gnus-get-newsgroup-headers)))
4101       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4102
4103       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4104       (when cached
4105         (setq gnus-newsgroup-cached cached))
4106
4107       ;; Suppress duplicates?
4108       (when gnus-suppress-duplicates
4109         (gnus-dup-suppress-articles))
4110
4111       ;; Set the initial limit.
4112       (setq gnus-newsgroup-limit (copy-sequence articles))
4113       ;; Remove canceled articles from the list of unread articles.
4114       (setq gnus-newsgroup-unreads
4115             (gnus-set-sorted-intersection
4116              gnus-newsgroup-unreads
4117              (setq fetched-articles
4118                    (mapcar (lambda (headers) (mail-header-number headers))
4119                            gnus-newsgroup-headers))))
4120       ;; Removed marked articles that do not exist.
4121       (gnus-update-missing-marks
4122        (gnus-sorted-complement fetched-articles articles))
4123       ;; We might want to build some more threads first.
4124       (when (and gnus-fetch-old-headers
4125                  (eq gnus-headers-retrieved-by 'nov))
4126         (if (eq gnus-fetch-old-headers 'invisible)
4127             (gnus-build-all-threads)
4128           (gnus-build-old-threads)))
4129       ;; Let the Gnus agent mark articles as read.
4130       (when gnus-agent
4131         (gnus-agent-get-undownloaded-list))
4132       ;; Check whether auto-expire is to be done in this group.
4133       (setq gnus-newsgroup-auto-expire
4134             (gnus-group-auto-expirable-p group))
4135       ;; Set up the article buffer now, if necessary.
4136       (unless gnus-single-article-buffer
4137         (gnus-article-setup-buffer))
4138       ;; First and last article in this newsgroup.
4139       (when gnus-newsgroup-headers
4140         (setq gnus-newsgroup-begin
4141               (mail-header-number (car gnus-newsgroup-headers))
4142               gnus-newsgroup-end
4143               (mail-header-number
4144                (gnus-last-element gnus-newsgroup-headers))))
4145       ;; GROUP is successfully selected.
4146       (or gnus-newsgroup-headers t)))))
4147
4148 (defun gnus-articles-to-read (group &optional read-all)
4149   ;; Find out what articles the user wants to read.
4150   (let* ((articles
4151           ;; Select all articles if `read-all' is non-nil, or if there
4152           ;; are no unread articles.
4153           (if (or read-all
4154                   (and (zerop (length gnus-newsgroup-marked))
4155                        (zerop (length gnus-newsgroup-unreads)))
4156                   (eq (gnus-group-find-parameter group 'display)
4157                       'all))
4158               (gnus-uncompress-range (gnus-active group))
4159             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4160                           (copy-sequence gnus-newsgroup-unreads))
4161                   '<)))
4162          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4163          (scored (length scored-list))
4164          (number (length articles))
4165          (marked (+ (length gnus-newsgroup-marked)
4166                     (length gnus-newsgroup-dormant)))
4167          (select
4168           (cond
4169            ((numberp read-all)
4170             read-all)
4171            (t
4172             (condition-case ()
4173                 (cond
4174                  ((and (or (<= scored marked) (= scored number))
4175                        (numberp gnus-large-newsgroup)
4176                        (> number gnus-large-newsgroup))
4177                   (let ((input
4178                          (read-string
4179                           (format
4180                            "How many articles from %s (default %d): "
4181                            (gnus-limit-string gnus-newsgroup-name 35)
4182                            number))))
4183                     (if (string-match "^[ \t]*$" input) number input)))
4184                  ((and (> scored marked) (< scored number)
4185                        (> (- scored number) 20))
4186                   (let ((input
4187                          (read-string
4188                           (format "%s %s (%d scored, %d total): "
4189                                   "How many articles from"
4190                                   group scored number))))
4191                     (if (string-match "^[ \t]*$" input)
4192                         number input)))
4193                  (t number))
4194               (quit nil))))))
4195     (setq select (if (stringp select) (string-to-number select) select))
4196     (if (or (null select) (zerop select))
4197         select
4198       (if (and (not (zerop scored)) (<= (abs select) scored))
4199           (progn
4200             (setq articles (sort scored-list '<))
4201             (setq number (length articles)))
4202         (setq articles (copy-sequence articles)))
4203
4204       (when (< (abs select) number)
4205         (if (< select 0)
4206             ;; Select the N oldest articles.
4207             (setcdr (nthcdr (1- (abs select)) articles) nil)
4208           ;; Select the N most recent articles.
4209           (setq articles (nthcdr (- number select) articles))))
4210       (setq gnus-newsgroup-unselected
4211             (gnus-sorted-intersection
4212              gnus-newsgroup-unreads
4213              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4214       articles)))
4215
4216 (defun gnus-killed-articles (killed articles)
4217   (let (out)
4218     (while articles
4219       (when (inline (gnus-member-of-range (car articles) killed))
4220         (push (car articles) out))
4221       (setq articles (cdr articles)))
4222     out))
4223
4224 (defun gnus-uncompress-marks (marks)
4225   "Uncompress the mark ranges in MARKS."
4226   (let ((uncompressed '(score bookmark))
4227         out)
4228     (while marks
4229       (if (memq (caar marks) uncompressed)
4230           (push (car marks) out)
4231         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4232       (setq marks (cdr marks)))
4233     out))
4234
4235 (defun gnus-adjust-marked-articles (info)
4236   "Set all article lists and remove all marks that are no longer valid."
4237   (let* ((marked-lists (gnus-info-marks info))
4238          (active (gnus-active (gnus-info-group info)))
4239          (min (car active))
4240          (max (cdr active))
4241          (types gnus-article-mark-lists)
4242          (uncompressed '(score bookmark killed))
4243          marks var articles article mark)
4244
4245     (while marked-lists
4246       (setq marks (pop marked-lists))
4247       (set (setq var (intern (format "gnus-newsgroup-%s"
4248                                      (car (rassq (setq mark (car marks))
4249                                                  types)))))
4250            (if (memq (car marks) uncompressed) (cdr marks)
4251              (gnus-uncompress-range (cdr marks))))
4252
4253       (setq articles (symbol-value var))
4254
4255       ;; All articles have to be subsets of the active articles.
4256       (cond
4257        ;; Adjust "simple" lists.
4258        ((memq mark '(tick dormant expire reply save))
4259         (while articles
4260           (when (or (< (setq article (pop articles)) min) (> article max))
4261             (set var (delq article (symbol-value var))))))
4262        ;; Adjust assocs.
4263        ((memq mark uncompressed)
4264         (when (not (listp (cdr (symbol-value var))))
4265           (set var (list (symbol-value var))))
4266         (when (not (listp (cdr articles)))
4267           (setq articles (list articles)))
4268         (while articles
4269           (when (or (not (consp (setq article (pop articles))))
4270                     (< (car article) min)
4271                     (> (car article) max))
4272             (set var (delq article (symbol-value var))))))))))
4273
4274 (defun gnus-update-missing-marks (missing)
4275   "Go through the list of MISSING articles and remove them from the mark lists."
4276   (when missing
4277     (let ((types gnus-article-mark-lists)
4278           var m)
4279       ;; Go through all types.
4280       (while types
4281         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4282         (when (symbol-value var)
4283           ;; This list has articles.  So we delete all missing articles
4284           ;; from it.
4285           (setq m missing)
4286           (while m
4287             (set var (delq (pop m) (symbol-value var)))))))))
4288
4289 (defun gnus-update-marks ()
4290   "Enter the various lists of marked articles into the newsgroup info list."
4291   (let ((types gnus-article-mark-lists)
4292         (info (gnus-get-info gnus-newsgroup-name))
4293         (uncompressed '(score bookmark killed))
4294         type list newmarked symbol delta-marks)
4295     (when info
4296       ;; Add all marks lists to the list of marks lists.
4297       (while (setq type (pop types))
4298         (setq list (symbol-value
4299                           (setq symbol
4300                                 (intern (format "gnus-newsgroup-%s"
4301                                                 (car type))))))
4302
4303         (when list
4304           ;; Get rid of the entries of the articles that have the
4305           ;; default score.
4306           (when (and (eq (cdr type) 'score)
4307                      gnus-save-score
4308                      list)
4309             (let* ((arts list)
4310                    (prev (cons nil list))
4311                    (all prev))
4312               (while arts
4313                 (if (or (not (consp (car arts)))
4314                         (= (cdar arts) gnus-summary-default-score))
4315                     (setcdr prev (cdr arts))
4316                   (setq prev arts))
4317                 (setq arts (cdr arts)))
4318               (setq list (cdr all)))))
4319
4320        (or (memq (cdr type) uncompressed)
4321            (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4322        
4323        (when (gnus-check-backend-function 'request-set-mark
4324                                           gnus-newsgroup-name)
4325          ;; uncompressed:s are not proper flags (they are cons cells)
4326          ;; cache is a internal gnus flag
4327          (unless (memq (cdr type) (cons 'cache uncompressed))
4328            (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4329                   (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4330                   (add (gnus-remove-from-range (gnus-copy-sequence list) old)))
4331              (if add
4332                  (push (list add 'add (list (cdr type))) delta-marks))
4333              (if del
4334                  (push (list del 'del (list (cdr type))) delta-marks)))))
4335           
4336         (when list
4337          (push (cons (cdr type) list) newmarked)))
4338
4339       (when delta-marks
4340         (unless (gnus-check-group gnus-newsgroup-name)
4341           (error "Can't open server for %s" gnus-newsgroup-name))
4342         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4343           
4344       ;; Enter these new marks into the info of the group.
4345       (if (nthcdr 3 info)
4346           (setcar (nthcdr 3 info) newmarked)
4347         ;; Add the marks lists to the end of the info.
4348         (when newmarked
4349           (setcdr (nthcdr 2 info) (list newmarked))))
4350
4351       ;; Cut off the end of the info if there's nothing else there.
4352       (let ((i 5))
4353         (while (and (> i 2)
4354                     (not (nth i info)))
4355           (when (nthcdr (decf i) info)
4356             (setcdr (nthcdr i info) nil)))))))
4357
4358 (defun gnus-set-mode-line (where)
4359   "This function sets the mode line of the article or summary buffers.
4360 If WHERE is `summary', the summary mode line format will be used."
4361   ;; Is this mode line one we keep updated?
4362   (when (and (memq where gnus-updated-mode-lines)
4363              (symbol-value
4364               (intern (format "gnus-%s-mode-line-format-spec" where))))
4365     (let (mode-string)
4366       (save-excursion
4367         ;; We evaluate this in the summary buffer since these
4368         ;; variables are buffer-local to that buffer.
4369         (set-buffer gnus-summary-buffer)
4370         ;; We bind all these variables that are used in the `eval' form
4371         ;; below.
4372         (let* ((mformat (symbol-value
4373                          (intern
4374                           (format "gnus-%s-mode-line-format-spec" where))))
4375                (gnus-tmp-group-name gnus-newsgroup-name)
4376                (gnus-tmp-article-number (or gnus-current-article 0))
4377                (gnus-tmp-unread gnus-newsgroup-unreads)
4378                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4379                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4380                (gnus-tmp-unread-and-unselected
4381                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4382                             (zerop gnus-tmp-unselected))
4383                        "")
4384                       ((zerop gnus-tmp-unselected)
4385                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4386                       (t (format "{%d(+%d) more}"
4387                                  gnus-tmp-unread-and-unticked
4388                                  gnus-tmp-unselected))))
4389                (gnus-tmp-subject
4390                 (if (and gnus-current-headers
4391                          (vectorp gnus-current-headers))
4392                     (gnus-mode-string-quote
4393                      (mail-header-subject gnus-current-headers))
4394                   ""))
4395                bufname-length max-len
4396                gnus-tmp-header);; passed as argument to any user-format-funcs
4397           (setq mode-string (eval mformat))
4398           (setq bufname-length (if (string-match "%b" mode-string)
4399                                    (- (length
4400                                        (buffer-name
4401                                         (if (eq where 'summary)
4402                                             nil
4403                                           (get-buffer gnus-article-buffer))))
4404                                       2)
4405                                  0))
4406           (setq max-len (max 4 (if gnus-mode-non-string-length
4407                                    (- (window-width)
4408                                       gnus-mode-non-string-length
4409                                       bufname-length)
4410                                  (length mode-string))))
4411           ;; We might have to chop a bit of the string off...
4412           (when (> (length mode-string) max-len)
4413             (setq mode-string
4414                   (concat (truncate-string-to-width mode-string (- max-len 3))
4415                           "...")))
4416           ;; Pad the mode string a bit.
4417           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4418       ;; Update the mode line.
4419       (setq mode-line-buffer-identification
4420             (gnus-mode-line-buffer-identification (list mode-string)))
4421       (set-buffer-modified-p t))))
4422
4423 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4424   "Go through the HEADERS list and add all Xrefs to a hash table.
4425 The resulting hash table is returned, or nil if no Xrefs were found."
4426   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4427          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4428          (xref-hashtb (gnus-make-hashtable))
4429          start group entry number xrefs header)
4430     (while headers
4431       (setq header (pop headers))
4432       (when (and (setq xrefs (mail-header-xref header))
4433                  (not (memq (setq number (mail-header-number header))
4434                             unreads)))
4435         (setq start 0)
4436         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4437           (setq start (match-end 0))
4438           (setq group (if prefix
4439                           (concat prefix (substring xrefs (match-beginning 1)
4440                                                     (match-end 1)))
4441                         (substring xrefs (match-beginning 1) (match-end 1))))
4442           (setq number
4443                 (string-to-int (substring xrefs (match-beginning 2)
4444                                           (match-end 2))))
4445           (if (setq entry (gnus-gethash group xref-hashtb))
4446               (setcdr entry (cons number (cdr entry)))
4447             (gnus-sethash group (cons number nil) xref-hashtb)))))
4448     (and start xref-hashtb)))
4449
4450 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4451   "Look through all the headers and mark the Xrefs as read."
4452   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4453         name entry info xref-hashtb idlist method nth4)
4454     (save-excursion
4455       (set-buffer gnus-group-buffer)
4456       (when (setq xref-hashtb
4457                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4458         (mapatoms
4459          (lambda (group)
4460            (unless (string= from-newsgroup (setq name (symbol-name group)))
4461              (setq idlist (symbol-value group))
4462              ;; Dead groups are not updated.
4463              (and (prog1
4464                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4465                             info (nth 2 entry))
4466                     (when (stringp (setq nth4 (gnus-info-method info)))
4467                       (setq nth4 (gnus-server-to-method nth4))))
4468                   ;; Only do the xrefs if the group has the same
4469                   ;; select method as the group we have just read.
4470                   (or (gnus-methods-equal-p
4471                        nth4 (gnus-find-method-for-group from-newsgroup))
4472                       virtual
4473                       (equal nth4 (setq method (gnus-find-method-for-group
4474                                                 from-newsgroup)))
4475                       (and (equal (car nth4) (car method))
4476                            (equal (nth 1 nth4) (nth 1 method))))
4477                   gnus-use-cross-reference
4478                   (or (not (eq gnus-use-cross-reference t))
4479                       virtual
4480                       ;; Only do cross-references on subscribed
4481                       ;; groups, if that is what is wanted.
4482                       (<= (gnus-info-level info) gnus-level-subscribed))
4483                   (gnus-group-make-articles-read name idlist))))
4484          xref-hashtb)))))
4485
4486 (defun gnus-compute-read-articles (group articles)
4487   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4488          (info (nth 2 entry))
4489          (active (gnus-active group))
4490          ninfo)
4491     (when entry
4492       ;; First peel off all invalid article numbers.
4493       (when active
4494         (let ((ids articles)
4495               id first)
4496           (while (setq id (pop ids))
4497             (when (and first (> id (cdr active)))
4498               ;; We'll end up in this situation in one particular
4499               ;; obscure situation.  If you re-scan a group and get
4500               ;; a new article that is cross-posted to a different
4501               ;; group that has not been re-scanned, you might get
4502               ;; crossposted article that has a higher number than
4503               ;; Gnus believes possible.  So we re-activate this
4504               ;; group as well.  This might mean doing the
4505               ;; crossposting thingy will *increase* the number
4506               ;; of articles in some groups.  Tsk, tsk.
4507               (setq active (or (gnus-activate-group group) active)))
4508             (when (or (> id (cdr active))
4509                       (< id (car active)))
4510               (setq articles (delq id articles))))))
4511       ;; If the read list is nil, we init it.
4512       (if (and active
4513                (null (gnus-info-read info))
4514                (> (car active) 1))
4515           (setq ninfo (cons 1 (1- (car active))))
4516         (setq ninfo (gnus-info-read info)))
4517       ;; Then we add the read articles to the range.
4518       (gnus-add-to-range
4519        ninfo (setq articles (sort articles '<))))))
4520
4521 (defun gnus-group-make-articles-read (group articles)
4522   "Update the info of GROUP to say that ARTICLES are read."
4523   (let* ((num 0)
4524          (entry (gnus-gethash group gnus-newsrc-hashtb))
4525          (info (nth 2 entry))
4526          (active (gnus-active group))
4527          range)
4528     (when entry
4529       (setq range (gnus-compute-read-articles group articles))
4530       (save-excursion
4531         (set-buffer gnus-group-buffer)
4532         (gnus-undo-register
4533           `(progn
4534              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4535              (gnus-info-set-read ',info ',(gnus-info-read info))
4536              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4537              (gnus-group-update-group ,group t))))
4538       ;; Add the read articles to the range.
4539       (gnus-info-set-read info range)
4540       ;; Then we have to re-compute how many unread
4541       ;; articles there are in this group.
4542       (when active
4543         (cond
4544          ((not range)
4545           (setq num (- (1+ (cdr active)) (car active))))
4546          ((not (listp (cdr range)))
4547           (setq num (- (cdr active) (- (1+ (cdr range))
4548                                        (car range)))))
4549          (t
4550           (while range
4551             (if (numberp (car range))
4552                 (setq num (1+ num))
4553               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4554             (setq range (cdr range)))
4555           (setq num (- (cdr active) num))))
4556         ;; Update the number of unread articles.
4557         (setcar entry num)
4558         ;; Update the group buffer.
4559         (gnus-group-update-group group t)))))
4560
4561 (defvar gnus-newsgroup-none-id 0)
4562
4563 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4564   (let ((cur nntp-server-buffer)
4565         (dependencies
4566          (or dependencies
4567              (save-excursion (set-buffer gnus-summary-buffer)
4568                              gnus-newsgroup-dependencies)))
4569         headers id end ref
4570         (mail-parse-charset gnus-newsgroup-charset)
4571         (mail-parse-ignored-charsets 
4572              (save-excursion (set-buffer gnus-summary-buffer)
4573                              gnus-newsgroup-ignored-charsets)))
4574     (save-excursion
4575       (set-buffer nntp-server-buffer)
4576       ;; Translate all TAB characters into SPACE characters.
4577       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4578       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4579       (gnus-run-hooks 'gnus-parse-headers-hook)
4580       (let ((case-fold-search t)
4581             in-reply-to header p lines chars)
4582         (goto-char (point-min))
4583         ;; Search to the beginning of the next header.  Error messages
4584         ;; do not begin with 2 or 3.
4585         (while (re-search-forward "^[23][0-9]+ " nil t)
4586           (setq id nil
4587                 ref nil)
4588           ;; This implementation of this function, with nine
4589           ;; search-forwards instead of the one re-search-forward and
4590           ;; a case (which basically was the old function) is actually
4591           ;; about twice as fast, even though it looks messier.  You
4592           ;; can't have everything, I guess.  Speed and elegance
4593           ;; doesn't always go hand in hand.
4594           (setq
4595            header
4596            (vector
4597             ;; Number.
4598             (prog1
4599                 (read cur)
4600               (end-of-line)
4601               (setq p (point))
4602               (narrow-to-region (point)
4603                                 (or (and (search-forward "\n.\n" nil t)
4604                                          (- (point) 2))
4605                                     (point))))
4606             ;; Subject.
4607             (progn
4608               (goto-char p)
4609               (if (search-forward "\nsubject: " nil t)
4610                   (funcall gnus-decode-encoded-word-function
4611                            (nnheader-header-value))
4612                 "(none)"))
4613             ;; From.
4614             (progn
4615               (goto-char p)
4616               (if (search-forward "\nfrom: " nil t)
4617                   (funcall gnus-decode-encoded-word-function
4618                            (nnheader-header-value))
4619                 "(nobody)"))
4620             ;; Date.
4621             (progn
4622               (goto-char p)
4623               (if (search-forward "\ndate: " nil t)
4624                   (nnheader-header-value) ""))
4625             ;; Message-ID.
4626             (progn
4627               (goto-char p)
4628               (setq id (if (re-search-forward
4629                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4630                            ;; We do it this way to make sure the Message-ID
4631                            ;; is (somewhat) syntactically valid.
4632                            (buffer-substring (match-beginning 1)
4633                                              (match-end 1))
4634                          ;; If there was no message-id, we just fake one
4635                          ;; to make subsequent routines simpler.
4636                          (nnheader-generate-fake-message-id))))
4637             ;; References.
4638             (progn
4639               (goto-char p)
4640               (if (search-forward "\nreferences: " nil t)
4641                   (progn
4642                     (setq end (point))
4643                     (prog1
4644                         (nnheader-header-value)
4645                       (setq ref
4646                             (buffer-substring
4647                              (progn
4648                                (end-of-line)
4649                                (search-backward ">" end t)
4650                                (1+ (point)))
4651                              (progn
4652                                (search-backward "<" end t)
4653                                (point))))))
4654                 ;; Get the references from the in-reply-to header if there
4655                 ;; were no references and the in-reply-to header looks
4656                 ;; promising.
4657                 (if (and (search-forward "\nin-reply-to: " nil t)
4658                          (setq in-reply-to (nnheader-header-value))
4659                          (string-match "<[^>]+>" in-reply-to))
4660                     (let (ref2)
4661                       (setq ref (substring in-reply-to (match-beginning 0)
4662                                            (match-end 0)))
4663                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4664                         (setq ref2 (substring in-reply-to (match-beginning 0)
4665                                               (match-end 0)))
4666                         (when (> (length ref2) (length ref))
4667                           (setq ref ref2)))
4668                       ref)
4669                   (setq ref nil))))
4670             ;; Chars.
4671             (progn
4672               (goto-char p)
4673               (if (search-forward "\nchars: " nil t)
4674                   (if (numberp (setq chars (ignore-errors (read cur))))
4675                       chars 0)
4676                 0))
4677             ;; Lines.
4678             (progn
4679               (goto-char p)
4680               (if (search-forward "\nlines: " nil t)
4681                   (if (numberp (setq lines (ignore-errors (read cur))))
4682                       lines 0)
4683                 0))
4684             ;; Xref.
4685             (progn
4686               (goto-char p)
4687               (and (search-forward "\nxref: " nil t)
4688                    (nnheader-header-value)))
4689             ;; Extra.
4690             (when gnus-extra-headers
4691               (let ((extra gnus-extra-headers)
4692                     out)
4693                 (while extra
4694                   (goto-char p)
4695                   (when (search-forward
4696                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4697                     (push (cons (car extra) (nnheader-header-value))
4698                           out))
4699                   (pop extra))
4700                 out))))
4701           (when (equal id ref)
4702             (setq ref nil))
4703
4704           (when gnus-alter-header-function
4705             (funcall gnus-alter-header-function header)
4706             (setq id (mail-header-id header)
4707                   ref (gnus-parent-id (mail-header-references header))))
4708
4709           (when (setq header
4710                       (gnus-dependencies-add-header
4711                        header dependencies force-new))
4712             (push header headers))
4713           (goto-char (point-max))
4714           (widen))
4715         (nreverse headers)))))
4716
4717 ;; Goes through the xover lines and returns a list of vectors
4718 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4719                                                   force-new dependencies
4720                                                   group also-fetch-heads)
4721   "Parse the news overview data in the server buffer, and return a
4722 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4723   ;; Get the Xref when the users reads the articles since most/some
4724   ;; NNTP servers do not include Xrefs when using XOVER.
4725   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4726   (let ((mail-parse-charset gnus-newsgroup-charset)
4727         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4728         (cur nntp-server-buffer)
4729         (dependencies (or dependencies gnus-newsgroup-dependencies))
4730         number headers header)
4731     (save-excursion
4732       (set-buffer nntp-server-buffer)
4733       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4734       ;; Allow the user to mangle the headers before parsing them.
4735       (gnus-run-hooks 'gnus-parse-headers-hook)
4736       (goto-char (point-min))
4737       (while (not (eobp))
4738         (condition-case ()
4739             (while (and sequence (not (eobp)))
4740               (setq number (read cur))
4741               (while (and sequence
4742                           (< (car sequence) number))
4743                 (setq sequence (cdr sequence)))
4744               (and sequence
4745                    (eq number (car sequence))
4746                    (progn
4747                      (setq sequence (cdr sequence))
4748                      (setq header (inline
4749                                     (gnus-nov-parse-line
4750                                      number dependencies force-new))))
4751                    (push header headers))
4752               (forward-line 1))
4753           (error
4754            (gnus-error 4 "Strange nov line (%d)"
4755                        (count-lines (point-min) (point)))))
4756         (forward-line 1))
4757       ;; A common bug in inn is that if you have posted an article and
4758       ;; then retrieves the active file, it will answer correctly --
4759       ;; the new article is included.  However, a NOV entry for the
4760       ;; article may not have been generated yet, so this may fail.
4761       ;; We work around this problem by retrieving the last few
4762       ;; headers using HEAD.
4763       (if (or (not also-fetch-heads)
4764               (not sequence))
4765           ;; We (probably) got all the headers.
4766           (nreverse headers)
4767         (let ((gnus-nov-is-evil t))
4768           (nconc
4769            (nreverse headers)
4770            (when (gnus-retrieve-headers sequence group)
4771              (gnus-get-newsgroup-headers))))))))
4772
4773 (defun gnus-article-get-xrefs ()
4774   "Fill in the Xref value in `gnus-current-headers', if necessary.
4775 This is meant to be called in `gnus-article-internal-prepare-hook'."
4776   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4777                                  gnus-current-headers)))
4778     (or (not gnus-use-cross-reference)
4779         (not headers)
4780         (and (mail-header-xref headers)
4781              (not (string= (mail-header-xref headers) "")))
4782         (let ((case-fold-search t)
4783               xref)
4784           (save-restriction
4785             (nnheader-narrow-to-headers)
4786             (goto-char (point-min))
4787             (when (or (and (eq (downcase (char-after)) ?x)
4788                            (looking-at "Xref:"))
4789                       (search-forward "\nXref:" nil t))
4790               (goto-char (1+ (match-end 0)))
4791               (setq xref (buffer-substring (point)
4792                                            (progn (end-of-line) (point))))
4793               (mail-header-set-xref headers xref)))))))
4794
4795 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4796   "Find article ID and insert the summary line for that article.
4797 OLD-HEADER can either be a header or a line number to insert
4798 the subject line on."
4799   (let* ((line (and (numberp old-header) old-header))
4800          (old-header (and (vectorp old-header) old-header))
4801          (header (cond ((and old-header use-old-header)
4802                         old-header)
4803                        ((and (numberp id)
4804                              (gnus-number-to-header id))
4805                         (gnus-number-to-header id))
4806                        (t
4807                         (gnus-read-header id))))
4808          (number (and (numberp id) id))
4809          d)
4810     (when header
4811       ;; Rebuild the thread that this article is part of and go to the
4812       ;; article we have fetched.
4813       (when (and (not gnus-show-threads)
4814                  old-header)
4815         (when (and number
4816                    (setq d (gnus-data-find (mail-header-number old-header))))
4817           (goto-char (gnus-data-pos d))
4818           (gnus-data-remove
4819            number
4820            (- (gnus-point-at-bol)
4821               (prog1
4822                   (1+ (gnus-point-at-eol))
4823                 (gnus-delete-line))))))
4824       (when old-header
4825         (mail-header-set-number header (mail-header-number old-header)))
4826       (setq gnus-newsgroup-sparse
4827             (delq (setq number (mail-header-number header))
4828                   gnus-newsgroup-sparse))
4829       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4830       (push number gnus-newsgroup-limit)
4831       (gnus-rebuild-thread (mail-header-id header) line)
4832       (gnus-summary-goto-subject number nil t))
4833     (when (and (numberp number)
4834                (> number 0))
4835       ;; We have to update the boundaries even if we can't fetch the
4836       ;; article if ID is a number -- so that the next `P' or `N'
4837       ;; command will fetch the previous (or next) article even
4838       ;; if the one we tried to fetch this time has been canceled.
4839       (when (> number gnus-newsgroup-end)
4840         (setq gnus-newsgroup-end number))
4841       (when (< number gnus-newsgroup-begin)
4842         (setq gnus-newsgroup-begin number))
4843       (setq gnus-newsgroup-unselected
4844             (delq number gnus-newsgroup-unselected)))
4845     ;; Report back a success?
4846     (and header (mail-header-number header))))
4847
4848 ;;; Process/prefix in the summary buffer
4849
4850 (defun gnus-summary-work-articles (n)
4851   "Return a list of articles to be worked upon.
4852 The prefix argument, the list of process marked articles, and the
4853 current article will be taken into consideration."
4854   (save-excursion
4855     (set-buffer gnus-summary-buffer)
4856     (cond
4857      (n
4858       ;; A numerical prefix has been given.
4859       (setq n (prefix-numeric-value n))
4860       (let ((backward (< n 0))
4861             (n (abs (prefix-numeric-value n)))
4862             articles article)
4863         (save-excursion
4864           (while
4865               (and (> n 0)
4866                    (push (setq article (gnus-summary-article-number))
4867                          articles)
4868                    (if backward
4869                        (gnus-summary-find-prev nil article)
4870                      (gnus-summary-find-next nil article)))
4871             (decf n)))
4872         (nreverse articles)))
4873      ((and (gnus-region-active-p) (mark))
4874       (message "region active")
4875       ;; Work on the region between point and mark.
4876       (let ((max (max (point) (mark)))
4877             articles article)
4878         (save-excursion
4879           (goto-char (min (min (point) (mark))))
4880           (while
4881               (and
4882                (push (setq article (gnus-summary-article-number)) articles)
4883                (gnus-summary-find-next nil article)
4884                (< (point) max)))
4885           (nreverse articles))))
4886      (gnus-newsgroup-processable
4887       ;; There are process-marked articles present.
4888       ;; Save current state.
4889       (gnus-summary-save-process-mark)
4890       ;; Return the list.
4891       (reverse gnus-newsgroup-processable))
4892      (t
4893       ;; Just return the current article.
4894       (list (gnus-summary-article-number))))))
4895
4896 (defmacro gnus-summary-iterate (arg &rest forms)
4897   "Iterate over the process/prefixed articles and do FORMS.
4898 ARG is the interactive prefix given to the command.  FORMS will be
4899 executed with point over the summary line of the articles."
4900   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4901     `(let ((,articles (gnus-summary-work-articles ,arg)))
4902        (while ,articles
4903          (gnus-summary-goto-subject (car ,articles))
4904          ,@forms
4905          (pop ,articles)))))
4906
4907 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4908 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4909
4910 (defun gnus-summary-save-process-mark ()
4911   "Push the current set of process marked articles on the stack."
4912   (interactive)
4913   (push (copy-sequence gnus-newsgroup-processable)
4914         gnus-newsgroup-process-stack))
4915
4916 (defun gnus-summary-kill-process-mark ()
4917   "Push the current set of process marked articles on the stack and unmark."
4918   (interactive)
4919   (gnus-summary-save-process-mark)
4920   (gnus-summary-unmark-all-processable))
4921
4922 (defun gnus-summary-yank-process-mark ()
4923   "Pop the last process mark state off the stack and restore it."
4924   (interactive)
4925   (unless gnus-newsgroup-process-stack
4926     (error "Empty mark stack"))
4927   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4928
4929 (defun gnus-summary-process-mark-set (set)
4930   "Make SET into the current process marked articles."
4931   (gnus-summary-unmark-all-processable)
4932   (while set
4933     (gnus-summary-set-process-mark (pop set))))
4934
4935 ;;; Searching and stuff
4936
4937 (defun gnus-summary-search-group (&optional backward use-level)
4938   "Search for next unread newsgroup.
4939 If optional argument BACKWARD is non-nil, search backward instead."
4940   (save-excursion
4941     (set-buffer gnus-group-buffer)
4942     (when (gnus-group-search-forward
4943            backward nil (if use-level (gnus-group-group-level) nil))
4944       (gnus-group-group-name))))
4945
4946 (defun gnus-summary-best-group (&optional exclude-group)
4947   "Find the name of the best unread group.
4948 If EXCLUDE-GROUP, do not go to this group."
4949   (save-excursion
4950     (set-buffer gnus-group-buffer)
4951     (save-excursion
4952       (gnus-group-best-unread-group exclude-group))))
4953
4954 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
4955   (if backward (gnus-summary-find-prev)
4956     (let* ((dummy (gnus-summary-article-intangible-p))
4957            (article (or article (gnus-summary-article-number)))
4958            (arts (gnus-data-find-list article))
4959            result)
4960       (when (and (not dummy)
4961                  (or (not gnus-summary-check-current)
4962                      (not unread)
4963                      (not (gnus-data-unread-p (car arts)))))
4964         (setq arts (cdr arts)))
4965       (when (setq result
4966                   (if unread
4967                       (progn
4968                         (while arts
4969                           (when (or (and undownloaded
4970                                          (eq gnus-undownloaded-mark
4971                                              (gnus-data-mark (car arts))))
4972                                     (gnus-data-unread-p (car arts)))
4973                             (setq result (car arts)
4974                                   arts nil))
4975                           (setq arts (cdr arts)))
4976                         result)
4977                     (car arts)))
4978         (goto-char (gnus-data-pos result))
4979         (gnus-data-number result)))))
4980
4981 (defun gnus-summary-find-prev (&optional unread article)
4982   (let* ((eobp (eobp))
4983          (article (or article (gnus-summary-article-number)))
4984          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
4985          result)
4986     (when (and (not eobp)
4987                (or (not gnus-summary-check-current)
4988                    (not unread)
4989                    (not (gnus-data-unread-p (car arts)))))
4990       (setq arts (cdr arts)))
4991     (when (setq result
4992                 (if unread
4993                     (progn
4994                       (while arts
4995                         (when (gnus-data-unread-p (car arts))
4996                           (setq result (car arts)
4997                                 arts nil))
4998                         (setq arts (cdr arts)))
4999                       result)
5000                   (car arts)))
5001       (goto-char (gnus-data-pos result))
5002       (gnus-data-number result))))
5003
5004 (defun gnus-summary-find-subject (subject &optional unread backward article)
5005   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5006          (article (or article (gnus-summary-article-number)))
5007          (articles (gnus-data-list backward))
5008          (arts (gnus-data-find-list article articles))
5009          result)
5010     (when (or (not gnus-summary-check-current)
5011               (not unread)
5012               (not (gnus-data-unread-p (car arts))))
5013       (setq arts (cdr arts)))
5014     (while arts
5015       (and (or (not unread)
5016                (gnus-data-unread-p (car arts)))
5017            (vectorp (gnus-data-header (car arts)))
5018            (gnus-subject-equal
5019             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5020            (setq result (car arts)
5021                  arts nil))
5022       (setq arts (cdr arts)))
5023     (and result
5024          (goto-char (gnus-data-pos result))
5025          (gnus-data-number result))))
5026
5027 (defun gnus-summary-search-forward (&optional unread subject backward)
5028   "Search forward for an article.
5029 If UNREAD, look for unread articles.  If SUBJECT, look for
5030 articles with that subject.  If BACKWARD, search backward instead."
5031   (cond (subject (gnus-summary-find-subject subject unread backward))
5032         (backward (gnus-summary-find-prev unread))
5033         (t (gnus-summary-find-next unread))))
5034
5035 (defun gnus-recenter (&optional n)
5036   "Center point in window and redisplay frame.
5037 Also do horizontal recentering."
5038   (interactive "P")
5039   (when (and gnus-auto-center-summary
5040              (not (eq gnus-auto-center-summary 'vertical)))
5041     (gnus-horizontal-recenter))
5042   (recenter n))
5043
5044 (defun gnus-summary-recenter ()
5045   "Center point in the summary window.
5046 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5047 displayed, no centering will be performed."
5048   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5049   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5050   (let* ((top (cond ((< (window-height) 4) 0)
5051                     ((< (window-height) 7) 1)
5052                     (t (if (numberp gnus-auto-center-summary)
5053                            gnus-auto-center-summary
5054                          2))))
5055          (height (1- (window-height)))
5056          (bottom (save-excursion (goto-char (point-max))
5057                                  (forward-line (- height))
5058                                  (point)))
5059          (window (get-buffer-window (current-buffer))))
5060     ;; The user has to want it.
5061     (when gnus-auto-center-summary
5062       (when (get-buffer-window gnus-article-buffer)
5063         ;; Only do recentering when the article buffer is displayed,
5064         ;; Set the window start to either `bottom', which is the biggest
5065         ;; possible valid number, or the second line from the top,
5066         ;; whichever is the least.
5067         (set-window-start
5068          window (min bottom (save-excursion
5069                               (forward-line (- top)) (point)))))
5070       ;; Do horizontal recentering while we're at it.
5071       (when (and (get-buffer-window (current-buffer) t)
5072                  (not (eq gnus-auto-center-summary 'vertical)))
5073         (let ((selected (selected-window)))
5074           (select-window (get-buffer-window (current-buffer) t))
5075           (gnus-summary-position-point)
5076           (gnus-horizontal-recenter)
5077           (select-window selected))))))
5078
5079 (defun gnus-summary-jump-to-group (newsgroup)
5080   "Move point to NEWSGROUP in group mode buffer."
5081   ;; Keep update point of group mode buffer if visible.
5082   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5083       (save-window-excursion
5084         ;; Take care of tree window mode.
5085         (when (get-buffer-window gnus-group-buffer)
5086           (pop-to-buffer gnus-group-buffer))
5087         (gnus-group-jump-to-group newsgroup))
5088     (save-excursion
5089       ;; Take care of tree window mode.
5090       (if (get-buffer-window gnus-group-buffer)
5091           (pop-to-buffer gnus-group-buffer)
5092         (set-buffer gnus-group-buffer))
5093       (gnus-group-jump-to-group newsgroup))))
5094
5095 ;; This function returns a list of article numbers based on the
5096 ;; difference between the ranges of read articles in this group and
5097 ;; the range of active articles.
5098 (defun gnus-list-of-unread-articles (group)
5099   (let* ((read (gnus-info-read (gnus-get-info group)))
5100          (active (or (gnus-active group) (gnus-activate-group group)))
5101          (last (cdr active))
5102          first nlast unread)
5103     ;; If none are read, then all are unread.
5104     (if (not read)
5105         (setq first (car active))
5106       ;; If the range of read articles is a single range, then the
5107       ;; first unread article is the article after the last read
5108       ;; article.  Sounds logical, doesn't it?
5109       (if (not (listp (cdr read)))
5110           (setq first (max (car active) (1+ (cdr read))))
5111         ;; `read' is a list of ranges.
5112         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5113                                   (caar read)))
5114                   1)
5115           (setq first (car active)))
5116         (while read
5117           (when first
5118             (while (< first nlast)
5119               (push first unread)
5120               (setq first (1+ first))))
5121           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5122           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5123           (setq read (cdr read)))))
5124     ;; And add the last unread articles.
5125     (while (<= first last)
5126       (push first unread)
5127       (setq first (1+ first)))
5128     ;; Return the list of unread articles.
5129     (delq 0 (nreverse unread))))
5130
5131 (defun gnus-list-of-read-articles (group)
5132   "Return a list of unread, unticked and non-dormant articles."
5133   (let* ((info (gnus-get-info group))
5134          (marked (gnus-info-marks info))
5135          (active (gnus-active group)))
5136     (and info active
5137          (gnus-set-difference
5138           (gnus-sorted-complement
5139            (gnus-uncompress-range active)
5140            (gnus-list-of-unread-articles group))
5141           (append
5142            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5143            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5144
5145 ;; Various summary commands
5146
5147 (defun gnus-summary-select-article-buffer ()
5148   "Reconfigure windows to show article buffer."
5149   (interactive)
5150   (if (not (gnus-buffer-live-p gnus-article-buffer))
5151       (error "There is no article buffer for this summary buffer")
5152     (gnus-configure-windows 'article)
5153     (select-window (get-buffer-window gnus-article-buffer))))
5154
5155 (defun gnus-summary-universal-argument (arg)
5156   "Perform any operation on all articles that are process/prefixed."
5157   (interactive "P")
5158   (let ((articles (gnus-summary-work-articles arg))
5159         func article)
5160     (if (eq
5161          (setq
5162           func
5163           (key-binding
5164            (read-key-sequence
5165             (substitute-command-keys
5166              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
5167              ))))
5168          'undefined)
5169         (gnus-error 1 "Undefined key")
5170       (save-excursion
5171         (while articles
5172           (gnus-summary-goto-subject (setq article (pop articles)))
5173           (let (gnus-newsgroup-processable)
5174             (command-execute func))
5175           (gnus-summary-remove-process-mark article)))))
5176   (gnus-summary-position-point))
5177
5178 (defun gnus-summary-toggle-truncation (&optional arg)
5179   "Toggle truncation of summary lines.
5180 With arg, turn line truncation on iff arg is positive."
5181   (interactive "P")
5182   (setq truncate-lines
5183         (if (null arg) (not truncate-lines)
5184           (> (prefix-numeric-value arg) 0)))
5185   (redraw-display))
5186
5187 (defun gnus-summary-reselect-current-group (&optional all rescan)
5188   "Exit and then reselect the current newsgroup.
5189 The prefix argument ALL means to select all articles."
5190   (interactive "P")
5191   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5192     (error "Ephemeral groups can't be reselected"))
5193   (let ((current-subject (gnus-summary-article-number))
5194         (group gnus-newsgroup-name))
5195     (setq gnus-newsgroup-begin nil)
5196     (gnus-summary-exit)
5197     ;; We have to adjust the point of group mode buffer because
5198     ;; point was moved to the next unread newsgroup by exiting.
5199     (gnus-summary-jump-to-group group)
5200     (when rescan
5201       (save-excursion
5202         (gnus-group-get-new-news-this-group 1)))
5203     (gnus-group-read-group all t)
5204     (gnus-summary-goto-subject current-subject nil t)))
5205
5206 (defun gnus-summary-rescan-group (&optional all)
5207   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5208   (interactive "P")
5209   (gnus-summary-reselect-current-group all t))
5210
5211 (defun gnus-summary-update-info (&optional non-destructive)
5212   (save-excursion
5213     (let ((group gnus-newsgroup-name))
5214       (when group
5215         (when gnus-newsgroup-kill-headers
5216           (setq gnus-newsgroup-killed
5217                 (gnus-compress-sequence
5218                  (nconc
5219                   (gnus-set-sorted-intersection
5220                    (gnus-uncompress-range gnus-newsgroup-killed)
5221                    (setq gnus-newsgroup-unselected
5222                          (sort gnus-newsgroup-unselected '<)))
5223                   (setq gnus-newsgroup-unreads
5224                         (sort gnus-newsgroup-unreads '<)))
5225                  t)))
5226         (unless (listp (cdr gnus-newsgroup-killed))
5227           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5228         (let ((headers gnus-newsgroup-headers))
5229           ;; Set the new ranges of read articles.
5230           (save-excursion
5231             (set-buffer gnus-group-buffer)
5232             (gnus-undo-force-boundary))
5233           (gnus-update-read-articles
5234            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5235           ;; Set the current article marks.
5236           (let ((gnus-newsgroup-scored
5237                  (if (and (not gnus-save-score)
5238                           (not non-destructive))
5239                      nil
5240                    gnus-newsgroup-scored)))
5241             (save-excursion
5242               (gnus-update-marks)))
5243           ;; Do the cross-ref thing.
5244           (when gnus-use-cross-reference
5245             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5246           ;; Do not switch windows but change the buffer to work.
5247           (set-buffer gnus-group-buffer)
5248           (unless (gnus-ephemeral-group-p group)
5249             (gnus-group-update-group group)))))))
5250
5251 (defun gnus-summary-save-newsrc (&optional force)
5252   "Save the current number of read/marked articles in the dribble buffer.
5253 The dribble buffer will then be saved.
5254 If FORCE (the prefix), also save the .newsrc file(s)."
5255   (interactive "P")
5256   (gnus-summary-update-info t)
5257   (if force
5258       (gnus-save-newsrc-file)
5259     (gnus-dribble-save)))
5260
5261 (defun gnus-summary-exit (&optional temporary)
5262   "Exit reading current newsgroup, and then return to group selection mode.
5263 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5264   (interactive)
5265   (gnus-set-global-variables)
5266   (when (gnus-buffer-live-p gnus-article-buffer)
5267     (save-excursion
5268       (set-buffer gnus-article-buffer)
5269       (mm-destroy-parts gnus-article-mime-handles)))
5270   (gnus-kill-save-kill-buffer)
5271   (gnus-async-halt-prefetch)
5272   (let* ((group gnus-newsgroup-name)
5273          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5274          (mode major-mode)
5275          (group-point nil)
5276          (buf (current-buffer)))
5277     (unless quit-config
5278       ;; Do adaptive scoring, and possibly save score files.
5279       (when gnus-newsgroup-adaptive
5280         (gnus-score-adaptive))
5281       (when gnus-use-scoring
5282         (gnus-score-save)))
5283     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5284     ;; If we have several article buffers, we kill them at exit.
5285     (unless gnus-single-article-buffer
5286       (gnus-kill-buffer gnus-original-article-buffer)
5287       (setq gnus-article-current nil))
5288     (when gnus-use-cache
5289       (gnus-cache-possibly-remove-articles)
5290       (gnus-cache-save-buffers))
5291     (gnus-async-prefetch-remove-group group)
5292     (when gnus-suppress-duplicates
5293       (gnus-dup-enter-articles))
5294     (when gnus-use-trees
5295       (gnus-tree-close group))
5296     ;; Remove entries for this group.
5297     (nnmail-purge-split-history (gnus-group-real-name group))
5298     ;; Make all changes in this group permanent.
5299     (unless quit-config
5300       (gnus-run-hooks 'gnus-exit-group-hook)
5301       (gnus-summary-update-info))
5302     (gnus-close-group group)
5303     ;; Make sure where we were, and go to next newsgroup.
5304     (set-buffer gnus-group-buffer)
5305     (unless quit-config
5306       (gnus-group-jump-to-group group))
5307     (gnus-run-hooks 'gnus-summary-exit-hook)
5308     (unless (or quit-config
5309                 ;; If this group has disappeared from the summary
5310                 ;; buffer, don't skip forwards.
5311                 (not (string= group (gnus-group-group-name))))
5312       (gnus-group-next-unread-group 1))
5313     (setq group-point (point))
5314     (if temporary
5315         nil                             ;Nothing to do.
5316       ;; If we have several article buffers, we kill them at exit.
5317       (unless gnus-single-article-buffer
5318         (gnus-kill-buffer gnus-article-buffer)
5319         (gnus-kill-buffer gnus-original-article-buffer)
5320         (setq gnus-article-current nil))
5321       (set-buffer buf)
5322       (if (not gnus-kill-summary-on-exit)
5323           (gnus-deaden-summary)
5324         ;; We set all buffer-local variables to nil.  It is unclear why
5325         ;; this is needed, but if we don't, buffer-local variables are
5326         ;; not garbage-collected, it seems.  This would the lead to en
5327         ;; ever-growing Emacs.
5328         (gnus-summary-clear-local-variables)
5329         (when (get-buffer gnus-article-buffer)
5330           (bury-buffer gnus-article-buffer))
5331         ;; We clear the global counterparts of the buffer-local
5332         ;; variables as well, just to be on the safe side.
5333         (set-buffer gnus-group-buffer)
5334         (gnus-summary-clear-local-variables)
5335         ;; Return to group mode buffer.
5336         (when (eq mode 'gnus-summary-mode)
5337           (gnus-kill-buffer buf)))
5338       (setq gnus-current-select-method gnus-select-method)
5339       (pop-to-buffer gnus-group-buffer)
5340       (if (not quit-config)
5341           (progn
5342             (goto-char group-point)
5343             (gnus-configure-windows 'group 'force))
5344         (gnus-handle-ephemeral-exit quit-config))
5345       ;; Clear the current group name.
5346       (unless quit-config
5347         (setq gnus-newsgroup-name nil)))))
5348
5349 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5350 (defun gnus-summary-exit-no-update (&optional no-questions)
5351   "Quit reading current newsgroup without updating read article info."
5352   (interactive)
5353   (let* ((group gnus-newsgroup-name)
5354          (quit-config (gnus-group-quit-config group)))
5355     (when (or no-questions
5356               gnus-expert-user
5357               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5358       (gnus-async-halt-prefetch)
5359       (mapcar 'funcall
5360               (delq 'gnus-summary-expire-articles
5361                     (copy-list gnus-summary-prepare-exit-hook)))
5362       (when (gnus-buffer-live-p gnus-article-buffer)
5363         (save-excursion
5364           (set-buffer gnus-article-buffer)
5365           (mm-destroy-parts gnus-article-mime-handles)))
5366       ;; If we have several article buffers, we kill them at exit.
5367       (unless gnus-single-article-buffer
5368         (gnus-kill-buffer gnus-article-buffer)
5369         (gnus-kill-buffer gnus-original-article-buffer)
5370         (setq gnus-article-current nil))
5371       (if (not gnus-kill-summary-on-exit)
5372           (gnus-deaden-summary)
5373         (gnus-close-group group)
5374         (gnus-summary-clear-local-variables)
5375         (set-buffer gnus-group-buffer)
5376         (gnus-summary-clear-local-variables)
5377         (when (get-buffer gnus-summary-buffer)
5378           (kill-buffer gnus-summary-buffer)))
5379       (unless gnus-single-article-buffer
5380         (setq gnus-article-current nil))
5381       (when gnus-use-trees
5382         (gnus-tree-close group))
5383       (gnus-async-prefetch-remove-group group)
5384       (when (get-buffer gnus-article-buffer)
5385         (bury-buffer gnus-article-buffer))
5386       ;; Return to the group buffer.
5387       (gnus-configure-windows 'group 'force)
5388       ;; Clear the current group name.
5389       (setq gnus-newsgroup-name nil)
5390       (when (equal (gnus-group-group-name) group)
5391         (gnus-group-next-unread-group 1))
5392       (when quit-config
5393         (gnus-handle-ephemeral-exit quit-config)))))
5394
5395 (defun gnus-handle-ephemeral-exit (quit-config)
5396   "Handle movement when leaving an ephemeral group.
5397 The state which existed when entering the ephemeral is reset."
5398   (if (not (buffer-name (car quit-config)))
5399       (gnus-configure-windows 'group 'force)
5400     (set-buffer (car quit-config))
5401     (cond ((eq major-mode 'gnus-summary-mode)
5402            (gnus-set-global-variables))
5403           ((eq major-mode 'gnus-article-mode)
5404            (save-excursion
5405              ;; The `gnus-summary-buffer' variable may point
5406              ;; to the old summary buffer when using a single
5407              ;; article buffer.
5408              (unless (gnus-buffer-live-p gnus-summary-buffer)
5409                (set-buffer gnus-group-buffer))
5410              (set-buffer gnus-summary-buffer)
5411              (gnus-set-global-variables))))
5412     (if (or (eq (cdr quit-config) 'article)
5413             (eq (cdr quit-config) 'pick))
5414         (progn
5415           ;; The current article may be from the ephemeral group
5416           ;; thus it is best that we reload this article
5417           (gnus-summary-show-article)
5418           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5419               (gnus-configure-windows 'pick 'force)
5420             (gnus-configure-windows (cdr quit-config) 'force)))
5421       (gnus-configure-windows (cdr quit-config) 'force))
5422     (when (eq major-mode 'gnus-summary-mode)
5423       (gnus-summary-next-subject 1 nil t)
5424       (gnus-summary-recenter)
5425       (gnus-summary-position-point))))
5426
5427 ;;; Dead summaries.
5428
5429 (defvar gnus-dead-summary-mode-map nil)
5430
5431 (unless gnus-dead-summary-mode-map
5432   (setq gnus-dead-summary-mode-map (make-keymap))
5433   (suppress-keymap gnus-dead-summary-mode-map)
5434   (substitute-key-definition
5435    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5436   (let ((keys '("\C-d" "\r" "\177" [delete])))
5437     (while keys
5438       (define-key gnus-dead-summary-mode-map
5439         (pop keys) 'gnus-summary-wake-up-the-dead))))
5440
5441 (defvar gnus-dead-summary-mode nil
5442   "Minor mode for Gnus summary buffers.")
5443
5444 (defun gnus-dead-summary-mode (&optional arg)
5445   "Minor mode for Gnus summary buffers."
5446   (interactive "P")
5447   (when (eq major-mode 'gnus-summary-mode)
5448     (make-local-variable 'gnus-dead-summary-mode)
5449     (setq gnus-dead-summary-mode
5450           (if (null arg) (not gnus-dead-summary-mode)
5451             (> (prefix-numeric-value arg) 0)))
5452     (when gnus-dead-summary-mode
5453       (gnus-add-minor-mode
5454        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5455
5456 (defun gnus-deaden-summary ()
5457   "Make the current summary buffer into a dead summary buffer."
5458   ;; Kill any previous dead summary buffer.
5459   (when (and gnus-dead-summary
5460              (buffer-name gnus-dead-summary))
5461     (save-excursion
5462       (set-buffer gnus-dead-summary)
5463       (when gnus-dead-summary-mode
5464         (kill-buffer (current-buffer)))))
5465   ;; Make this the current dead summary.
5466   (setq gnus-dead-summary (current-buffer))
5467   (gnus-dead-summary-mode 1)
5468   (let ((name (buffer-name)))
5469     (when (string-match "Summary" name)
5470       (rename-buffer
5471        (concat (substring name 0 (match-beginning 0)) "Dead "
5472                (substring name (match-beginning 0)))
5473        t))))
5474
5475 (defun gnus-kill-or-deaden-summary (buffer)
5476   "Kill or deaden the summary BUFFER."
5477   (save-excursion
5478     (when (and (buffer-name buffer)
5479                (not gnus-single-article-buffer))
5480       (save-excursion
5481         (set-buffer buffer)
5482         (gnus-kill-buffer gnus-article-buffer)
5483         (gnus-kill-buffer gnus-original-article-buffer)))
5484     (cond (gnus-kill-summary-on-exit
5485            (when (and gnus-use-trees
5486                       (gnus-buffer-exists-p buffer))
5487              (save-excursion
5488                (set-buffer buffer)
5489                (gnus-tree-close gnus-newsgroup-name)))
5490            (gnus-kill-buffer buffer))
5491           ((gnus-buffer-exists-p buffer)
5492            (save-excursion
5493              (set-buffer buffer)
5494              (gnus-deaden-summary))))))
5495
5496 (defun gnus-summary-wake-up-the-dead (&rest args)
5497   "Wake up the dead summary buffer."
5498   (interactive)
5499   (gnus-dead-summary-mode -1)
5500   (let ((name (buffer-name)))
5501     (when (string-match "Dead " name)
5502       (rename-buffer
5503        (concat (substring name 0 (match-beginning 0))
5504                (substring name (match-end 0)))
5505        t)))
5506   (gnus-message 3 "This dead summary is now alive again"))
5507
5508 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5509 (defun gnus-summary-fetch-faq (&optional faq-dir)
5510   "Fetch the FAQ for the current group.
5511 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5512 in."
5513   (interactive
5514    (list
5515     (when current-prefix-arg
5516       (completing-read
5517        "Faq dir: " (and (listp gnus-group-faq-directory)
5518                         (mapcar (lambda (file) (list file))
5519                                 gnus-group-faq-directory))))))
5520   (let (gnus-faq-buffer)
5521     (when (setq gnus-faq-buffer
5522                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5523       (gnus-configure-windows 'summary-faq))))
5524
5525 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5526 (defun gnus-summary-describe-group (&optional force)
5527   "Describe the current newsgroup."
5528   (interactive "P")
5529   (gnus-group-describe-group force gnus-newsgroup-name))
5530
5531 (defun gnus-summary-describe-briefly ()
5532   "Describe summary mode commands briefly."
5533   (interactive)
5534   (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")))
5535
5536 ;; Walking around group mode buffer from summary mode.
5537
5538 (defun gnus-summary-next-group (&optional no-article target-group backward)
5539   "Exit current newsgroup and then select next unread newsgroup.
5540 If prefix argument NO-ARTICLE is non-nil, no article is selected
5541 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5542 previous group instead."
5543   (interactive "P")
5544   ;; Stop pre-fetching.
5545   (gnus-async-halt-prefetch)
5546   (let ((current-group gnus-newsgroup-name)
5547         (current-buffer (current-buffer))
5548         entered)
5549     ;; First we semi-exit this group to update Xrefs and all variables.
5550     ;; We can't do a real exit, because the window conf must remain
5551     ;; the same in case the user is prompted for info, and we don't
5552     ;; want the window conf to change before that...
5553     (gnus-summary-exit t)
5554     (while (not entered)
5555       ;; Then we find what group we are supposed to enter.
5556       (set-buffer gnus-group-buffer)
5557       (gnus-group-jump-to-group current-group)
5558       (setq target-group
5559             (or target-group
5560                 (if (eq gnus-keep-same-level 'best)
5561                     (gnus-summary-best-group gnus-newsgroup-name)
5562                   (gnus-summary-search-group backward gnus-keep-same-level))))
5563       (if (not target-group)
5564           ;; There are no further groups, so we return to the group
5565           ;; buffer.
5566           (progn
5567             (gnus-message 5 "Returning to the group buffer")
5568             (setq entered t)
5569             (when (gnus-buffer-live-p current-buffer)
5570               (set-buffer current-buffer)
5571               (gnus-summary-exit))
5572             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5573         ;; We try to enter the target group.
5574         (gnus-group-jump-to-group target-group)
5575         (let ((unreads (gnus-group-group-unread)))
5576           (if (and (or (eq t unreads)
5577                        (and unreads (not (zerop unreads))))
5578                    (gnus-summary-read-group
5579                     target-group nil no-article
5580                     (and (buffer-name current-buffer) current-buffer)
5581                     nil backward))
5582               (setq entered t)
5583             (setq current-group target-group
5584                   target-group nil)))))))
5585
5586 (defun gnus-summary-prev-group (&optional no-article)
5587   "Exit current newsgroup and then select previous unread newsgroup.
5588 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5589   (interactive "P")
5590   (gnus-summary-next-group no-article nil t))
5591
5592 ;; Walking around summary lines.
5593
5594 (defun gnus-summary-first-subject (&optional unread undownloaded)
5595   "Go to the first unread subject.
5596 If UNREAD is non-nil, go to the first unread article.
5597 Returns the article selected or nil if there are no unread articles."
5598   (interactive "P")
5599   (prog1
5600       (cond
5601        ;; Empty summary.
5602        ((null gnus-newsgroup-data)
5603         (gnus-message 3 "No articles in the group")
5604         nil)
5605        ;; Pick the first article.
5606        ((not unread)
5607         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5608         (gnus-data-number (car gnus-newsgroup-data)))
5609        ;; No unread articles.
5610        ((null gnus-newsgroup-unreads)
5611         (gnus-message 3 "No more unread articles")
5612         nil)
5613        ;; Find the first unread article.
5614        (t
5615         (let ((data gnus-newsgroup-data))
5616           (while (and data
5617                       (and (not (and undownloaded
5618                                      (eq gnus-undownloaded-mark
5619                                          (gnus-data-mark (car data)))))
5620                            (not (gnus-data-unread-p (car data)))))
5621             (setq data (cdr data)))
5622           (when data
5623             (goto-char (gnus-data-pos (car data)))
5624             (gnus-data-number (car data))))))
5625     (gnus-summary-position-point)))
5626
5627 (defun gnus-summary-next-subject (n &optional unread dont-display)
5628   "Go to next N'th summary line.
5629 If N is negative, go to the previous N'th subject line.
5630 If UNREAD is non-nil, only unread articles are selected.
5631 The difference between N and the actual number of steps taken is
5632 returned."
5633   (interactive "p")
5634   (let ((backward (< n 0))
5635         (n (abs n)))
5636     (while (and (> n 0)
5637                 (if backward
5638                     (gnus-summary-find-prev unread)
5639                   (gnus-summary-find-next unread)))
5640       (gnus-summary-show-thread)
5641       (setq n (1- n)))
5642     (when (/= 0 n)
5643       (gnus-message 7 "No more%s articles"
5644                     (if unread " unread" "")))
5645     (unless dont-display
5646       (gnus-summary-recenter)
5647       (gnus-summary-position-point))
5648     n))
5649
5650 (defun gnus-summary-next-unread-subject (n)
5651   "Go to next N'th unread summary line."
5652   (interactive "p")
5653   (gnus-summary-next-subject n t))
5654
5655 (defun gnus-summary-prev-subject (n &optional unread)
5656   "Go to previous N'th summary line.
5657 If optional argument UNREAD is non-nil, only unread article is selected."
5658   (interactive "p")
5659   (gnus-summary-next-subject (- n) unread))
5660
5661 (defun gnus-summary-prev-unread-subject (n)
5662   "Go to previous N'th unread summary line."
5663   (interactive "p")
5664   (gnus-summary-next-subject (- n) t))
5665
5666 (defun gnus-summary-goto-subject (article &optional force silent)
5667   "Go the subject line of ARTICLE.
5668 If FORCE, also allow jumping to articles not currently shown."
5669   (interactive "nArticle number: ")
5670   (let ((b (point))
5671         (data (gnus-data-find article)))
5672     ;; We read in the article if we have to.
5673     (and (not data)
5674          force
5675          (gnus-summary-insert-subject
5676           article
5677           (if (or (numberp force) (vectorp force)) force)
5678           t)
5679          (setq data (gnus-data-find article)))
5680     (goto-char b)
5681     (if (not data)
5682         (progn
5683           (unless silent
5684             (gnus-message 3 "Can't find article %d" article))
5685           nil)
5686       (goto-char (gnus-data-pos data))
5687       (gnus-summary-position-point)
5688       article)))
5689
5690 ;; Walking around summary lines with displaying articles.
5691
5692 (defun gnus-summary-expand-window (&optional arg)
5693   "Make the summary buffer take up the entire Emacs frame.
5694 Given a prefix, will force an `article' buffer configuration."
5695   (interactive "P")
5696   (if arg
5697       (gnus-configure-windows 'article 'force)
5698     (gnus-configure-windows 'summary 'force)))
5699
5700 (defun gnus-summary-display-article (article &optional all-header)
5701   "Display ARTICLE in article buffer."
5702   (gnus-set-global-variables)
5703   (if (null article)
5704       nil
5705     (prog1
5706         (if gnus-summary-display-article-function
5707             (funcall gnus-summary-display-article-function article all-header)
5708           (gnus-article-prepare article all-header))
5709       (gnus-run-hooks 'gnus-select-article-hook)
5710       (when (and gnus-current-article
5711                  (not (zerop gnus-current-article)))
5712         (gnus-summary-goto-subject gnus-current-article))
5713       (gnus-summary-recenter)
5714       (when (and gnus-use-trees gnus-show-threads)
5715         (gnus-possibly-generate-tree article)
5716         (gnus-highlight-selected-tree article))
5717       ;; Successfully display article.
5718       (gnus-article-set-window-start
5719        (cdr (assq article gnus-newsgroup-bookmarks))))))
5720
5721 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5722   "Select the current article.
5723 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5724 non-nil, the article will be re-fetched even if it already present in
5725 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5726 be displayed."
5727   ;; Make sure we are in the summary buffer to work around bbdb bug.
5728   (unless (eq major-mode 'gnus-summary-mode)
5729     (set-buffer gnus-summary-buffer))
5730   (let ((article (or article (gnus-summary-article-number)))
5731         (all-headers (not (not all-headers))) ;Must be T or NIL.
5732         gnus-summary-display-article-function
5733         did)
5734     (and (not pseudo)
5735          (gnus-summary-article-pseudo-p article)
5736          (error "This is a pseudo-article"))
5737     (prog1
5738         (save-excursion
5739           (set-buffer gnus-summary-buffer)
5740           (if (or (and gnus-single-article-buffer
5741                        (or (null gnus-current-article)
5742                            (null gnus-article-current)
5743                            (null (get-buffer gnus-article-buffer))
5744                            (not (eq article (cdr gnus-article-current)))
5745                            (not (equal (car gnus-article-current)
5746                                        gnus-newsgroup-name))))
5747                   (and (not gnus-single-article-buffer)
5748                        (or (null gnus-current-article)
5749                            (not (eq gnus-current-article article))))
5750                   force)
5751               ;; The requested article is different from the current article.
5752               (prog1
5753                   (gnus-summary-display-article article all-headers)
5754                 (setq did article)
5755                 (when (or all-headers gnus-show-all-headers)
5756                   (gnus-article-show-all-headers)))
5757             (when (or all-headers gnus-show-all-headers)
5758               (gnus-article-show-all-headers))
5759             'old))
5760       (when did
5761         (gnus-article-set-window-start
5762          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5763
5764 (defun gnus-summary-set-current-mark (&optional current-mark)
5765   "Obsolete function."
5766   nil)
5767
5768 (defun gnus-summary-next-article (&optional unread subject backward push)
5769   "Select the next article.
5770 If UNREAD, only unread articles are selected.
5771 If SUBJECT, only articles with SUBJECT are selected.
5772 If BACKWARD, the previous article is selected instead of the next."
5773   (interactive "P")
5774   (cond
5775    ;; Is there such an article?
5776    ((and (gnus-summary-search-forward unread subject backward)
5777          (or (gnus-summary-display-article (gnus-summary-article-number))
5778              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5779     (gnus-summary-position-point))
5780    ;; If not, we try the first unread, if that is wanted.
5781    ((and subject
5782          gnus-auto-select-same
5783          (gnus-summary-first-unread-article))
5784     (gnus-summary-position-point)
5785     (gnus-message 6 "Wrapped"))
5786    ;; Try to get next/previous article not displayed in this group.
5787    ((and gnus-auto-extend-newsgroup
5788          (not unread) (not subject))
5789     (gnus-summary-goto-article
5790      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5791      nil (count-lines (point-min) (point))))
5792    ;; Go to next/previous group.
5793    (t
5794     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5795       (gnus-summary-jump-to-group gnus-newsgroup-name))
5796     (let ((cmd last-command-char)
5797           (point
5798            (save-excursion
5799              (set-buffer gnus-group-buffer)
5800              (point)))
5801           (group
5802            (if (eq gnus-keep-same-level 'best)
5803                (gnus-summary-best-group gnus-newsgroup-name)
5804              (gnus-summary-search-group backward gnus-keep-same-level))))
5805       ;; For some reason, the group window gets selected.  We change
5806       ;; it back.
5807       (select-window (get-buffer-window (current-buffer)))
5808       ;; Select next unread newsgroup automagically.
5809       (cond
5810        ((or (not gnus-auto-select-next)
5811             (not cmd))
5812         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5813        ((or (eq gnus-auto-select-next 'quietly)
5814             (and (eq gnus-auto-select-next 'slightly-quietly)
5815                  push)
5816             (and (eq gnus-auto-select-next 'almost-quietly)
5817                  (gnus-summary-last-article-p)))
5818         ;; Select quietly.
5819         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5820             (gnus-summary-exit)
5821           (gnus-message 7 "No more%s articles (%s)..."
5822                         (if unread " unread" "")
5823                         (if group (concat "selecting " group)
5824                           "exiting"))
5825           (gnus-summary-next-group nil group backward)))
5826        (t
5827         (when (gnus-key-press-event-p last-input-event)
5828           (gnus-summary-walk-group-buffer
5829            gnus-newsgroup-name cmd unread backward point))))))))
5830
5831 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5832   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5833                       (?\C-p (gnus-group-prev-unread-group 1))))
5834         (cursor-in-echo-area t)
5835         keve key group ended)
5836     (save-excursion
5837       (set-buffer gnus-group-buffer)
5838       (goto-char start)
5839       (setq group
5840             (if (eq gnus-keep-same-level 'best)
5841                 (gnus-summary-best-group gnus-newsgroup-name)
5842               (gnus-summary-search-group backward gnus-keep-same-level))))
5843     (while (not ended)
5844       (gnus-message
5845        5 "No more%s articles%s" (if unread " unread" "")
5846        (if (and group
5847                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5848            (format " (Type %s for %s [%s])"
5849                    (single-key-description cmd) group
5850                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5851          (format " (Type %s to exit %s)"
5852                  (single-key-description cmd)
5853                  gnus-newsgroup-name)))
5854       ;; Confirm auto selection.
5855       (setq key (car (setq keve (gnus-read-event-char))))
5856       (setq ended t)
5857       (cond
5858        ((assq key keystrokes)
5859         (let ((obuf (current-buffer)))
5860           (switch-to-buffer gnus-group-buffer)
5861           (when group
5862             (gnus-group-jump-to-group group))
5863           (eval (cadr (assq key keystrokes)))
5864           (setq group (gnus-group-group-name))
5865           (switch-to-buffer obuf))
5866         (setq ended nil))
5867        ((equal key cmd)
5868         (if (or (not group)
5869                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5870             (gnus-summary-exit)
5871           (gnus-summary-next-group nil group backward)))
5872        (t
5873         (push (cdr keve) unread-command-events))))))
5874
5875 (defun gnus-summary-next-unread-article ()
5876   "Select unread article after current one."
5877   (interactive)
5878   (gnus-summary-next-article
5879    (or (not (eq gnus-summary-goto-unread 'never))
5880        (gnus-summary-last-article-p (gnus-summary-article-number)))
5881    (and gnus-auto-select-same
5882         (gnus-summary-article-subject))))
5883
5884 (defun gnus-summary-prev-article (&optional unread subject)
5885   "Select the article after the current one.
5886 If UNREAD is non-nil, only unread articles are selected."
5887   (interactive "P")
5888   (gnus-summary-next-article unread subject t))
5889
5890 (defun gnus-summary-prev-unread-article ()
5891   "Select unread article before current one."
5892   (interactive)
5893   (gnus-summary-prev-article
5894    (or (not (eq gnus-summary-goto-unread 'never))
5895        (gnus-summary-first-article-p (gnus-summary-article-number)))
5896    (and gnus-auto-select-same
5897         (gnus-summary-article-subject))))
5898
5899 (defun gnus-summary-next-page (&optional lines circular)
5900   "Show next page of the selected article.
5901 If at the end of the current article, select the next article.
5902 LINES says how many lines should be scrolled up.
5903
5904 If CIRCULAR is non-nil, go to the start of the article instead of
5905 selecting the next article when reaching the end of the current
5906 article."
5907   (interactive "P")
5908   (setq gnus-summary-buffer (current-buffer))
5909   (gnus-set-global-variables)
5910   (let ((article (gnus-summary-article-number))
5911         (article-window (get-buffer-window gnus-article-buffer t))
5912         endp)
5913     ;; If the buffer is empty, we have no article.
5914     (unless article
5915       (error "No article to select"))
5916     (gnus-configure-windows 'article)
5917     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5918         (if (and (eq gnus-summary-goto-unread 'never)
5919                  (not (gnus-summary-last-article-p article)))
5920             (gnus-summary-next-article)
5921           (gnus-summary-next-unread-article))
5922       (if (or (null gnus-current-article)
5923               (null gnus-article-current)
5924               (/= article (cdr gnus-article-current))
5925               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5926           ;; Selected subject is different from current article's.
5927           (gnus-summary-display-article article)
5928         (when article-window
5929           (gnus-eval-in-buffer-window gnus-article-buffer
5930             (setq endp (gnus-article-next-page lines)))
5931           (when endp
5932             (cond (circular
5933                    (gnus-summary-beginning-of-article))
5934                   (lines
5935                    (gnus-message 3 "End of message"))
5936                   ((null lines)
5937                    (if (and (eq gnus-summary-goto-unread 'never)
5938                             (not (gnus-summary-last-article-p article)))
5939                        (gnus-summary-next-article)
5940                      (gnus-summary-next-unread-article))))))))
5941     (gnus-summary-recenter)
5942     (gnus-summary-position-point)))
5943
5944 (defun gnus-summary-prev-page (&optional lines move)
5945   "Show previous page of selected article.
5946 Argument LINES specifies lines to be scrolled down.
5947 If MOVE, move to the previous unread article if point is at
5948 the beginning of the buffer."
5949   (interactive "P")
5950   (let ((article (gnus-summary-article-number))
5951         (article-window (get-buffer-window gnus-article-buffer t))
5952         endp)
5953     (gnus-configure-windows 'article)
5954     (if (or (null gnus-current-article)
5955             (null gnus-article-current)
5956             (/= article (cdr gnus-article-current))
5957             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5958         ;; Selected subject is different from current article's.
5959         (gnus-summary-display-article article)
5960       (gnus-summary-recenter)
5961       (when article-window
5962         (gnus-eval-in-buffer-window gnus-article-buffer
5963           (setq endp (gnus-article-prev-page lines)))
5964         (when (and move endp)
5965           (cond (lines
5966                  (gnus-message 3 "Beginning of message"))
5967                 ((null lines)
5968                  (if (and (eq gnus-summary-goto-unread 'never)
5969                           (not (gnus-summary-first-article-p article)))
5970                      (gnus-summary-prev-article)
5971                    (gnus-summary-prev-unread-article))))))))
5972   (gnus-summary-position-point))
5973
5974 (defun gnus-summary-prev-page-or-article (&optional lines)
5975   "Show previous page of selected article.
5976 Argument LINES specifies lines to be scrolled down.
5977 If at the beginning of the article, go to the next article."
5978   (interactive "P")
5979   (gnus-summary-prev-page lines t))
5980
5981 (defun gnus-summary-scroll-up (lines)
5982   "Scroll up (or down) one line current article.
5983 Argument LINES specifies lines to be scrolled up (or down if negative)."
5984   (interactive "p")
5985   (gnus-configure-windows 'article)
5986   (gnus-summary-show-thread)
5987   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
5988     (gnus-eval-in-buffer-window gnus-article-buffer
5989       (cond ((> lines 0)
5990              (when (gnus-article-next-page lines)
5991                (gnus-message 3 "End of message")))
5992             ((< lines 0)
5993              (gnus-article-prev-page (- lines))))))
5994   (gnus-summary-recenter)
5995   (gnus-summary-position-point))
5996
5997 (defun gnus-summary-scroll-down (lines)
5998   "Scroll down (or up) one line current article.
5999 Argument LINES specifies lines to be scrolled down (or up if negative)."
6000   (interactive "p")
6001   (gnus-summary-scroll-up (- lines)))
6002
6003 (defun gnus-summary-next-same-subject ()
6004   "Select next article which has the same subject as current one."
6005   (interactive)
6006   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6007
6008 (defun gnus-summary-prev-same-subject ()
6009   "Select previous article which has the same subject as current one."
6010   (interactive)
6011   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6012
6013 (defun gnus-summary-next-unread-same-subject ()
6014   "Select next unread article which has the same subject as current one."
6015   (interactive)
6016   (gnus-summary-next-article t (gnus-summary-article-subject)))
6017
6018 (defun gnus-summary-prev-unread-same-subject ()
6019   "Select previous unread article which has the same subject as current one."
6020   (interactive)
6021   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6022
6023 (defun gnus-summary-first-unread-article ()
6024   "Select the first unread article.
6025 Return nil if there are no unread articles."
6026   (interactive)
6027   (prog1
6028       (when (gnus-summary-first-subject t)
6029         (gnus-summary-show-thread)
6030         (gnus-summary-first-subject t)
6031         (gnus-summary-display-article (gnus-summary-article-number)))
6032     (gnus-summary-position-point)))
6033
6034 (defun gnus-summary-first-unread-subject ()
6035   "Place the point on the subject line of the first unread article.
6036 Return nil if there are no unread articles."
6037   (interactive)
6038   (prog1
6039       (when (gnus-summary-first-subject t)
6040         (gnus-summary-show-thread)
6041         (gnus-summary-first-subject t))
6042     (gnus-summary-position-point)))
6043
6044 (defun gnus-summary-first-article ()
6045   "Select the first article.
6046 Return nil if there are no articles."
6047   (interactive)
6048   (prog1
6049       (when (gnus-summary-first-subject)
6050         (gnus-summary-show-thread)
6051         (gnus-summary-first-subject)
6052         (gnus-summary-display-article (gnus-summary-article-number)))
6053     (gnus-summary-position-point)))
6054
6055 (defun gnus-summary-best-unread-article ()
6056   "Select the unread article with the highest score."
6057   (interactive)
6058   (let ((best -1000000)
6059         (data gnus-newsgroup-data)
6060         article score)
6061     (while data
6062       (and (gnus-data-unread-p (car data))
6063            (> (setq score
6064                     (gnus-summary-article-score (gnus-data-number (car data))))
6065               best)
6066            (setq best score
6067                  article (gnus-data-number (car data))))
6068       (setq data (cdr data)))
6069     (prog1
6070         (if article
6071             (gnus-summary-goto-article article)
6072           (error "No unread articles"))
6073       (gnus-summary-position-point))))
6074
6075 (defun gnus-summary-last-subject ()
6076   "Go to the last displayed subject line in the group."
6077   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6078     (when article
6079       (gnus-summary-goto-subject article))))
6080
6081 (defun gnus-summary-goto-article (article &optional all-headers force)
6082   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6083 If ALL-HEADERS is non-nil, no header lines are hidden.
6084 If FORCE, go to the article even if it isn't displayed.  If FORCE
6085 is a number, it is the line the article is to be displayed on."
6086   (interactive
6087    (list
6088     (completing-read
6089      "Article number or Message-ID: "
6090      (mapcar (lambda (number) (list (int-to-string number)))
6091              gnus-newsgroup-limit))
6092     current-prefix-arg
6093     t))
6094   (prog1
6095       (if (and (stringp article)
6096                (string-match "@" article))
6097           (gnus-summary-refer-article article)
6098         (when (stringp article)
6099           (setq article (string-to-number article)))
6100         (if (gnus-summary-goto-subject article force)
6101             (gnus-summary-display-article article all-headers)
6102           (gnus-message 4 "Couldn't go to article %s" article) nil))
6103     (gnus-summary-position-point)))
6104
6105 (defun gnus-summary-goto-last-article ()
6106   "Go to the previously read article."
6107   (interactive)
6108   (prog1
6109       (when gnus-last-article
6110         (gnus-summary-goto-article gnus-last-article nil t))
6111     (gnus-summary-position-point)))
6112
6113 (defun gnus-summary-pop-article (number)
6114   "Pop one article off the history and go to the previous.
6115 NUMBER articles will be popped off."
6116   (interactive "p")
6117   (let (to)
6118     (setq gnus-newsgroup-history
6119           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6120     (if to
6121         (gnus-summary-goto-article (car to) nil t)
6122       (error "Article history empty")))
6123   (gnus-summary-position-point))
6124
6125 ;; Summary commands and functions for limiting the summary buffer.
6126
6127 (defun gnus-summary-limit-to-articles (n)
6128   "Limit the summary buffer to the next N articles.
6129 If not given a prefix, use the process marked articles instead."
6130   (interactive "P")
6131   (prog1
6132       (let ((articles (gnus-summary-work-articles n)))
6133         (setq gnus-newsgroup-processable nil)
6134         (gnus-summary-limit articles))
6135     (gnus-summary-position-point)))
6136
6137 (defun gnus-summary-pop-limit (&optional total)
6138   "Restore the previous limit.
6139 If given a prefix, remove all limits."
6140   (interactive "P")
6141   (when total
6142     (setq gnus-newsgroup-limits
6143           (list (mapcar (lambda (h) (mail-header-number h))
6144                         gnus-newsgroup-headers))))
6145   (unless gnus-newsgroup-limits
6146     (error "No limit to pop"))
6147   (prog1
6148       (gnus-summary-limit nil 'pop)
6149     (gnus-summary-position-point)))
6150
6151 (defun gnus-summary-limit-to-subject (subject &optional header)
6152   "Limit the summary buffer to articles that have subjects that match a regexp."
6153   (interactive "sLimit to subject (regexp): ")
6154   (unless header
6155     (setq header "subject"))
6156   (when (not (equal "" subject))
6157     (prog1
6158         (let ((articles (gnus-summary-find-matching
6159                          (or header "subject") subject 'all)))
6160           (unless articles
6161             (error "Found no matches for \"%s\"" subject))
6162           (gnus-summary-limit articles))
6163       (gnus-summary-position-point))))
6164
6165 (defun gnus-summary-limit-to-author (from)
6166   "Limit the summary buffer to articles that have authors that match a regexp."
6167   (interactive "sLimit to author (regexp): ")
6168   (gnus-summary-limit-to-subject from "from"))
6169
6170 (defun gnus-summary-limit-to-age (age &optional younger-p)
6171   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6172 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6173 articles that are younger than AGE days."
6174   (interactive "nLimit to articles older than (in days): \nP")
6175   (prog1
6176       (let ((data gnus-newsgroup-data)
6177             (cutoff (days-to-time age))
6178             articles d date is-younger)
6179         (while (setq d (pop data))
6180           (when (and (vectorp (gnus-data-header d))
6181                      (setq date (mail-header-date (gnus-data-header d))))
6182             (setq is-younger (time-less-p
6183                               (time-since (date-to-time date))
6184                               cutoff))
6185             (when (if younger-p
6186                       is-younger
6187                     (not is-younger))
6188               (push (gnus-data-number d) articles))))
6189         (gnus-summary-limit (nreverse articles)))
6190     (gnus-summary-position-point)))
6191
6192 (defun gnus-summary-limit-to-extra (header regexp)
6193   "Limit the summary buffer to articles that match an 'extra' header."
6194   (interactive
6195    (let ((header
6196           (intern
6197            (gnus-completing-read
6198             (symbol-name (car gnus-extra-headers))      
6199             "Score extra header:"       
6200             (mapcar (lambda (x) 
6201                       (cons (symbol-name x) x))
6202                     gnus-extra-headers)
6203             nil                 
6204             t))))
6205      (list header
6206            (read-string (format "Limit to header %s (regexp): " header)))))
6207   (when (not (equal "" regexp))
6208     (prog1
6209         (let ((articles (gnus-summary-find-matching
6210                          (cons 'extra header) regexp 'all)))
6211           (unless articles
6212             (error "Found no matches for \"%s\"" regexp))
6213           (gnus-summary-limit articles))
6214       (gnus-summary-position-point))))
6215
6216 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6217 (make-obsolete
6218  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6219
6220 (defun gnus-summary-limit-to-unread (&optional all)
6221   "Limit the summary buffer to articles that are not marked as read.
6222 If ALL is non-nil, limit strictly to unread articles."
6223   (interactive "P")
6224   (if all
6225       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6226     (gnus-summary-limit-to-marks
6227      ;; Concat all the marks that say that an article is read and have
6228      ;; those removed.
6229      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6230            gnus-killed-mark gnus-kill-file-mark
6231            gnus-low-score-mark gnus-expirable-mark
6232            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6233            gnus-duplicate-mark gnus-souped-mark)
6234      'reverse)))
6235
6236 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6237 (make-obsolete 'gnus-summary-delete-marked-with
6238                'gnus-summary-limit-exlude-marks)
6239
6240 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6241   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6242 If REVERSE, limit the summary buffer to articles that are marked
6243 with MARKS.  MARKS can either be a string of marks or a list of marks.
6244 Returns how many articles were removed."
6245   (interactive "sMarks: ")
6246   (gnus-summary-limit-to-marks marks t))
6247
6248 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6249   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6250 If REVERSE (the prefix), limit the summary buffer to articles that are
6251 not marked with MARKS.  MARKS can either be a string of marks or a
6252 list of marks.
6253 Returns how many articles were removed."
6254   (interactive "sMarks: \nP")
6255   (prog1
6256       (let ((data gnus-newsgroup-data)
6257             (marks (if (listp marks) marks
6258                      (append marks nil))) ; Transform to list.
6259             articles)
6260         (while data
6261           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6262                   (memq (gnus-data-mark (car data)) marks))
6263             (push (gnus-data-number (car data)) articles))
6264           (setq data (cdr data)))
6265         (gnus-summary-limit articles))
6266     (gnus-summary-position-point)))
6267
6268 (defun gnus-summary-limit-to-score (&optional score)
6269   "Limit to articles with score at or above SCORE."
6270   (interactive "P")
6271   (setq score (if score
6272                   (prefix-numeric-value score)
6273                 (or gnus-summary-default-score 0)))
6274   (let ((data gnus-newsgroup-data)
6275         articles)
6276     (while data
6277       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6278                 score)
6279         (push (gnus-data-number (car data)) articles))
6280       (setq data (cdr data)))
6281     (prog1
6282         (gnus-summary-limit articles)
6283       (gnus-summary-position-point))))
6284
6285 (defun gnus-summary-limit-include-thread (id)
6286   "Display all the hidden articles that in the current thread."
6287   (interactive (list (mail-header-id (gnus-summary-article-header))))
6288   (let ((articles (gnus-articles-in-thread
6289                    (gnus-id-to-thread (gnus-root-id id)))))
6290     (prog1
6291         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6292       (gnus-summary-position-point))))
6293
6294 (defun gnus-summary-limit-include-dormant ()
6295   "Display all the hidden articles that are marked as dormant.
6296 Note that this command only works on a subset of the articles currently
6297 fetched for this group."
6298   (interactive)
6299   (unless gnus-newsgroup-dormant
6300     (error "There are no dormant articles in this group"))
6301   (prog1
6302       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6303     (gnus-summary-position-point)))
6304
6305 (defun gnus-summary-limit-exclude-dormant ()
6306   "Hide all dormant articles."
6307   (interactive)
6308   (prog1
6309       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6310     (gnus-summary-position-point)))
6311
6312 (defun gnus-summary-limit-exclude-childless-dormant ()
6313   "Hide all dormant articles that have no children."
6314   (interactive)
6315   (let ((data (gnus-data-list t))
6316         articles d children)
6317     ;; Find all articles that are either not dormant or have
6318     ;; children.
6319     (while (setq d (pop data))
6320       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6321                 (and (setq children
6322                            (gnus-article-children (gnus-data-number d)))
6323                      (let (found)
6324                        (while children
6325                          (when (memq (car children) articles)
6326                            (setq children nil
6327                                  found t))
6328                          (pop children))
6329                        found)))
6330         (push (gnus-data-number d) articles)))
6331     ;; Do the limiting.
6332     (prog1
6333         (gnus-summary-limit articles)
6334       (gnus-summary-position-point))))
6335
6336 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6337   "Mark all unread excluded articles as read.
6338 If ALL, mark even excluded ticked and dormants as read."
6339   (interactive "P")
6340   (let ((articles (gnus-sorted-complement
6341                    (sort
6342                     (mapcar (lambda (h) (mail-header-number h))
6343                             gnus-newsgroup-headers)
6344                     '<)
6345                    (sort gnus-newsgroup-limit '<)))
6346         article)
6347     (setq gnus-newsgroup-unreads
6348           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6349     (if all
6350         (setq gnus-newsgroup-dormant nil
6351               gnus-newsgroup-marked nil
6352               gnus-newsgroup-reads
6353               (nconc
6354                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6355                gnus-newsgroup-reads))
6356       (while (setq article (pop articles))
6357         (unless (or (memq article gnus-newsgroup-dormant)
6358                     (memq article gnus-newsgroup-marked))
6359           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6360
6361 (defun gnus-summary-limit (articles &optional pop)
6362   (if pop
6363       ;; We pop the previous limit off the stack and use that.
6364       (setq articles (car gnus-newsgroup-limits)
6365             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6366     ;; We use the new limit, so we push the old limit on the stack.
6367     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6368   ;; Set the limit.
6369   (setq gnus-newsgroup-limit articles)
6370   (let ((total (length gnus-newsgroup-data))
6371         (data (gnus-data-find-list (gnus-summary-article-number)))
6372         (gnus-summary-mark-below nil)   ; Inhibit this.
6373         found)
6374     ;; This will do all the work of generating the new summary buffer
6375     ;; according to the new limit.
6376     (gnus-summary-prepare)
6377     ;; Hide any threads, possibly.
6378     (and gnus-show-threads
6379          gnus-thread-hide-subtree
6380          (gnus-summary-hide-all-threads))
6381     ;; Try to return to the article you were at, or one in the
6382     ;; neighborhood.
6383     (when data
6384       ;; We try to find some article after the current one.
6385       (while data
6386         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6387           (setq data nil
6388                 found t))
6389         (setq data (cdr data))))
6390     (unless found
6391       ;; If there is no data, that means that we were after the last
6392       ;; article.  The same goes when we can't find any articles
6393       ;; after the current one.
6394       (goto-char (point-max))
6395       (gnus-summary-find-prev))
6396     (gnus-set-mode-line 'summary)
6397     ;; We return how many articles were removed from the summary
6398     ;; buffer as a result of the new limit.
6399     (- total (length gnus-newsgroup-data))))
6400
6401 (defsubst gnus-invisible-cut-children (threads)
6402   (let ((num 0))
6403     (while threads
6404       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6405         (incf num))
6406       (pop threads))
6407     (< num 2)))
6408
6409 (defsubst gnus-cut-thread (thread)
6410   "Go forwards in the thread until we find an article that we want to display."
6411   (when (or (eq gnus-fetch-old-headers 'some)
6412             (eq gnus-fetch-old-headers 'invisible)
6413             (eq gnus-build-sparse-threads 'some)
6414             (eq gnus-build-sparse-threads 'more))
6415     ;; Deal with old-fetched headers and sparse threads.
6416     (while (and
6417             thread
6418             (or
6419              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6420              (gnus-summary-article-ancient-p
6421               (mail-header-number (car thread))))
6422             (if (or (<= (length (cdr thread)) 1)
6423                     (eq gnus-fetch-old-headers 'invisible))
6424                 (setq gnus-newsgroup-limit
6425                       (delq (mail-header-number (car thread))
6426                             gnus-newsgroup-limit)
6427                       thread (cadr thread))
6428               (when (gnus-invisible-cut-children (cdr thread))
6429                 (let ((th (cdr thread)))
6430                   (while th
6431                     (if (memq (mail-header-number (caar th))
6432                               gnus-newsgroup-limit)
6433                         (setq thread (car th)
6434                               th nil)
6435                       (setq th (cdr th))))))))))
6436   thread)
6437
6438 (defun gnus-cut-threads (threads)
6439   "Cut off all uninteresting articles from the beginning of threads."
6440   (when (or (eq gnus-fetch-old-headers 'some)
6441             (eq gnus-fetch-old-headers 'invisible)
6442             (eq gnus-build-sparse-threads 'some)
6443             (eq gnus-build-sparse-threads 'more))
6444     (let ((th threads))
6445       (while th
6446         (setcar th (gnus-cut-thread (car th)))
6447         (setq th (cdr th)))))
6448   ;; Remove nixed out threads.
6449   (delq nil threads))
6450
6451 (defun gnus-summary-initial-limit (&optional show-if-empty)
6452   "Figure out what the initial limit is supposed to be on group entry.
6453 This entails weeding out unwanted dormants, low-scored articles,
6454 fetch-old-headers verbiage, and so on."
6455   ;; Most groups have nothing to remove.
6456   (if (or gnus-inhibit-limiting
6457           (and (null gnus-newsgroup-dormant)
6458                (not (eq gnus-fetch-old-headers 'some))
6459                (not (eq gnus-fetch-old-headers 'invisible))
6460                (null gnus-summary-expunge-below)
6461                (not (eq gnus-build-sparse-threads 'some))
6462                (not (eq gnus-build-sparse-threads 'more))
6463                (null gnus-thread-expunge-below)
6464                (not gnus-use-nocem)))
6465       ()                                ; Do nothing.
6466     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6467     (setq gnus-newsgroup-limit nil)
6468     (mapatoms
6469      (lambda (node)
6470        (unless (car (symbol-value node))
6471          ;; These threads have no parents -- they are roots.
6472          (let ((nodes (cdr (symbol-value node)))
6473                thread)
6474            (while nodes
6475              (if (and gnus-thread-expunge-below
6476                       (< (gnus-thread-total-score (car nodes))
6477                          gnus-thread-expunge-below))
6478                  (gnus-expunge-thread (pop nodes))
6479                (setq thread (pop nodes))
6480                (gnus-summary-limit-children thread))))))
6481      gnus-newsgroup-dependencies)
6482     ;; If this limitation resulted in an empty group, we might
6483     ;; pop the previous limit and use it instead.
6484     (when (and (not gnus-newsgroup-limit)
6485                show-if-empty)
6486       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6487     gnus-newsgroup-limit))
6488
6489 (defun gnus-summary-limit-children (thread)
6490   "Return 1 if this subthread is visible and 0 if it is not."
6491   ;; First we get the number of visible children to this thread.  This
6492   ;; is done by recursing down the thread using this function, so this
6493   ;; will really go down to a leaf article first, before slowly
6494   ;; working its way up towards the root.
6495   (when thread
6496     (let ((children
6497            (if (cdr thread)
6498                (apply '+ (mapcar 'gnus-summary-limit-children
6499                                  (cdr thread)))
6500              0))
6501           (number (mail-header-number (car thread)))
6502           score)
6503       (if (and
6504            (not (memq number gnus-newsgroup-marked))
6505            (or
6506             ;; If this article is dormant and has absolutely no visible
6507             ;; children, then this article isn't visible.
6508             (and (memq number gnus-newsgroup-dormant)
6509                  (zerop children))
6510             ;; If this is "fetch-old-headered" and there is no
6511             ;; visible children, then we don't want this article.
6512             (and (eq gnus-fetch-old-headers 'some)
6513                  (gnus-summary-article-ancient-p number)
6514                  (zerop children))
6515             ;; If this is "fetch-old-headered" and `invisible', then
6516             ;; we don't want this article.
6517             (and (eq gnus-fetch-old-headers 'invisible)
6518                  (gnus-summary-article-ancient-p number))
6519             ;; If this is a sparsely inserted article with no children,
6520             ;; we don't want it.
6521             (and (eq gnus-build-sparse-threads 'some)
6522                  (gnus-summary-article-sparse-p number)
6523                  (zerop children))
6524             ;; If we use expunging, and this article is really
6525             ;; low-scored, then we don't want this article.
6526             (when (and gnus-summary-expunge-below
6527                        (< (setq score
6528                                 (or (cdr (assq number gnus-newsgroup-scored))
6529                                     gnus-summary-default-score))
6530                           gnus-summary-expunge-below))
6531               ;; We increase the expunge-tally here, but that has
6532               ;; nothing to do with the limits, really.
6533               (incf gnus-newsgroup-expunged-tally)
6534               ;; We also mark as read here, if that's wanted.
6535               (when (and gnus-summary-mark-below
6536                          (< score gnus-summary-mark-below))
6537                 (setq gnus-newsgroup-unreads
6538                       (delq number gnus-newsgroup-unreads))
6539                 (if gnus-newsgroup-auto-expire
6540                     (push number gnus-newsgroup-expirable)
6541                   (push (cons number gnus-low-score-mark)
6542                         gnus-newsgroup-reads)))
6543               t)
6544             ;; Check NoCeM things.
6545             (if (and gnus-use-nocem
6546                      (gnus-nocem-unwanted-article-p
6547                       (mail-header-id (car thread))))
6548                 (progn
6549                   (setq gnus-newsgroup-unreads
6550                         (delq number gnus-newsgroup-unreads))
6551                   t))))
6552           ;; Nope, invisible article.
6553           0
6554         ;; Ok, this article is to be visible, so we add it to the limit
6555         ;; and return 1.
6556         (push number gnus-newsgroup-limit)
6557         1))))
6558
6559 (defun gnus-expunge-thread (thread)
6560   "Mark all articles in THREAD as read."
6561   (let* ((number (mail-header-number (car thread))))
6562     (incf gnus-newsgroup-expunged-tally)
6563     ;; We also mark as read here, if that's wanted.
6564     (setq gnus-newsgroup-unreads
6565           (delq number gnus-newsgroup-unreads))
6566     (if gnus-newsgroup-auto-expire
6567         (push number gnus-newsgroup-expirable)
6568       (push (cons number gnus-low-score-mark)
6569             gnus-newsgroup-reads)))
6570   ;; Go recursively through all subthreads.
6571   (mapcar 'gnus-expunge-thread (cdr thread)))
6572
6573 ;; Summary article oriented commands
6574
6575 (defun gnus-summary-refer-parent-article (n)
6576   "Refer parent article N times.
6577 If N is negative, go to ancestor -N instead.
6578 The difference between N and the number of articles fetched is returned."
6579   (interactive "p")
6580   (let ((skip 1)
6581         error header ref)
6582     (when (not (natnump n))
6583       (setq skip (abs n)
6584             n 1))
6585     (while (and (> n 0)
6586                 (not error))
6587       (setq header (gnus-summary-article-header))
6588       (if (and (eq (mail-header-number header)
6589                    (cdr gnus-article-current))
6590                (equal gnus-newsgroup-name
6591                       (car gnus-article-current)))
6592           ;; If we try to find the parent of the currently
6593           ;; displayed article, then we take a look at the actual
6594           ;; References header, since this is slightly more
6595           ;; reliable than the References field we got from the
6596           ;; server.
6597           (save-excursion
6598             (set-buffer gnus-original-article-buffer)
6599             (nnheader-narrow-to-headers)
6600             (unless (setq ref (message-fetch-field "references"))
6601               (setq ref (message-fetch-field "in-reply-to")))
6602             (widen))
6603         (setq ref
6604               ;; It's not the current article, so we take a bet on
6605               ;; the value we got from the server.
6606               (mail-header-references header)))
6607       (if (and ref
6608                (not (equal ref "")))
6609           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6610             (gnus-message 1 "Couldn't find parent"))
6611         (gnus-message 1 "No references in article %d"
6612                       (gnus-summary-article-number))
6613         (setq error t))
6614       (decf n))
6615     (gnus-summary-position-point)
6616     n))
6617
6618 (defun gnus-summary-refer-references ()
6619   "Fetch all articles mentioned in the References header.
6620 Return the number of articles fetched."
6621   (interactive)
6622   (let ((ref (mail-header-references (gnus-summary-article-header)))
6623         (current (gnus-summary-article-number))
6624         (n 0))
6625     (if (or (not ref)
6626             (equal ref ""))
6627         (error "No References in the current article")
6628       ;; For each Message-ID in the References header...
6629       (while (string-match "<[^>]*>" ref)
6630         (incf n)
6631         ;; ... fetch that article.
6632         (gnus-summary-refer-article
6633          (prog1 (match-string 0 ref)
6634            (setq ref (substring ref (match-end 0))))))
6635       (gnus-summary-goto-subject current)
6636       (gnus-summary-position-point)
6637       n)))
6638
6639 (defun gnus-summary-refer-thread (&optional limit)
6640   "Fetch all articles in the current thread.
6641 If LIMIT (the numerical prefix), fetch that many old headers instead
6642 of what's specified by the `gnus-refer-thread-limit' variable."
6643   (interactive "P")
6644   (let ((id (mail-header-id (gnus-summary-article-header)))
6645         (limit (if limit (prefix-numeric-value limit)
6646                  gnus-refer-thread-limit)))
6647     ;; We want to fetch LIMIT *old* headers, but we also have to
6648     ;; re-fetch all the headers in the current buffer, because many of
6649     ;; them may be undisplayed.  So we adjust LIMIT.
6650     (when (numberp limit)
6651       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6652     (unless (eq gnus-fetch-old-headers 'invisible)
6653       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6654       ;; Retrieve the headers and read them in.
6655       (if (eq (gnus-retrieve-headers
6656                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6657               'nov)
6658           (gnus-build-all-threads)
6659         (error "Can't fetch thread from backends that don't support NOV"))
6660       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6661     (gnus-summary-limit-include-thread id)))
6662
6663 (defun gnus-summary-refer-article (message-id &optional arg)
6664   "Fetch an article specified by MESSAGE-ID.
6665 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6666 or `gnus-select-method', no matter what backend the article comes from."
6667   (interactive "sMessage-ID: \nP")
6668   (when (and (stringp message-id)
6669              (not (zerop (length message-id))))
6670     ;; Construct the correct Message-ID if necessary.
6671     ;; Suggested by tale@pawl.rpi.edu.
6672     (unless (string-match "^<" message-id)
6673       (setq message-id (concat "<" message-id)))
6674     (unless (string-match ">$" message-id)
6675       (setq message-id (concat message-id ">")))
6676     (let* ((header (gnus-id-to-header message-id))
6677            (sparse (and header
6678                         (gnus-summary-article-sparse-p
6679                          (mail-header-number header))
6680                         (memq (mail-header-number header)
6681                               gnus-newsgroup-limit))))
6682       (cond
6683        ;; If the article is present in the buffer we just go to it.
6684        ((and header
6685              (or (not (gnus-summary-article-sparse-p
6686                        (mail-header-number header)))
6687                  sparse))
6688         (prog1
6689             (gnus-summary-goto-article
6690              (mail-header-number header) nil t)
6691           (when sparse
6692             (gnus-summary-update-article (mail-header-number header)))))
6693        (t
6694         ;; We fetch the article
6695         (let ((gnus-override-method
6696                (cond ((gnus-news-group-p gnus-newsgroup-name)
6697                       gnus-refer-article-method)
6698                      (arg
6699                       (or gnus-refer-article-method gnus-select-method))
6700                      (t nil)))
6701               number)
6702           ;; Start the special refer-article method, if necessary.
6703           (when (and gnus-refer-article-method
6704                      (gnus-news-group-p gnus-newsgroup-name))
6705             (gnus-check-server gnus-refer-article-method))
6706           ;; Fetch the header, and display the article.
6707           (if (setq number (gnus-summary-insert-subject message-id))
6708               (gnus-summary-select-article nil nil nil number)
6709             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6710
6711 (defun gnus-summary-edit-parameters ()
6712   "Edit the group parameters of the current group."
6713   (interactive)
6714   (gnus-group-edit-group gnus-newsgroup-name 'params))
6715
6716 (defun gnus-summary-customize-parameters ()
6717   "Customize the group parameters of the current group."
6718   (interactive)
6719   (gnus-group-customize gnus-newsgroup-name))
6720
6721 (defun gnus-summary-enter-digest-group (&optional force)
6722   "Enter an nndoc group based on the current article.
6723 If FORCE, force a digest interpretation.  If not, try
6724 to guess what the document format is."
6725   (interactive "P")
6726   (let ((conf gnus-current-window-configuration))
6727     (save-excursion
6728       (gnus-summary-select-article))
6729     (setq gnus-current-window-configuration conf)
6730     (let* ((name (format "%s-%d"
6731                          (gnus-group-prefixed-name
6732                           gnus-newsgroup-name (list 'nndoc ""))
6733                          (save-excursion
6734                            (set-buffer gnus-summary-buffer)
6735                            gnus-current-article)))
6736            (ogroup gnus-newsgroup-name)
6737            (params (append (gnus-info-params (gnus-get-info ogroup))
6738                            (list (cons 'to-group ogroup))
6739                            (list (cons 'save-article-group ogroup))))
6740            (case-fold-search t)
6741            (buf (current-buffer))
6742            dig)
6743       (save-excursion
6744         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6745         (insert-buffer-substring gnus-original-article-buffer)
6746         ;; Remove lines that may lead nndoc to misinterpret the
6747         ;; document type.
6748         (narrow-to-region
6749          (goto-char (point-min))
6750          (or (search-forward "\n\n" nil t) (point)))
6751         (goto-char (point-min))
6752         (delete-matching-lines "^\\(Path\\):\\|^From ")
6753         (widen))
6754       (unwind-protect
6755           (if (gnus-group-read-ephemeral-group
6756                name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6757                             (nndoc-article-type
6758                              ,(if force 'digest 'guess))) t)
6759               ;; Make all postings to this group go to the parent group.
6760               (nconc (gnus-info-params (gnus-get-info name))
6761                      params)
6762             ;; Couldn't select this doc group.
6763             (switch-to-buffer buf)
6764             (gnus-set-global-variables)
6765             (gnus-configure-windows 'summary)
6766             (gnus-message 3 "Article couldn't be entered?"))
6767         (kill-buffer dig)))))
6768
6769 (defun gnus-summary-read-document (n)
6770   "Open a new group based on the current article(s).
6771 This will allow you to read digests and other similar
6772 documents as newsgroups.
6773 Obeys the standard process/prefix convention."
6774   (interactive "P")
6775   (let* ((articles (gnus-summary-work-articles n))
6776          (ogroup gnus-newsgroup-name)
6777          (params (append (gnus-info-params (gnus-get-info ogroup))
6778                          (list (cons 'to-group ogroup))))
6779          article group egroup groups vgroup)
6780     (while (setq article (pop articles))
6781       (setq group (format "%s-%d" gnus-newsgroup-name article))
6782       (gnus-summary-remove-process-mark article)
6783       (when (gnus-summary-display-article article)
6784         (save-excursion
6785           (with-temp-buffer
6786             (insert-buffer-substring gnus-original-article-buffer)
6787             ;; Remove some headers that may lead nndoc to make
6788             ;; the wrong guess.
6789             (message-narrow-to-head)
6790             (goto-char (point-min))
6791             (delete-matching-lines "^\\(Path\\):\\|^From ")
6792             (widen)
6793             (if (setq egroup
6794                       (gnus-group-read-ephemeral-group
6795                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6796                                      (nndoc-article-type guess))
6797                        t nil t))
6798                 (progn
6799                   ;; Make all postings to this group go to the parent group.
6800                   (nconc (gnus-info-params (gnus-get-info egroup))
6801                          params)
6802                   (push egroup groups))
6803               ;; Couldn't select this doc group.
6804               (gnus-error 3 "Article couldn't be entered"))))))
6805     ;; Now we have selected all the documents.
6806     (cond
6807      ((not groups)
6808       (error "None of the articles could be interpreted as documents"))
6809      ((gnus-group-read-ephemeral-group
6810        (setq vgroup (format
6811                      "nnvirtual:%s-%s" gnus-newsgroup-name
6812                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6813        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6814        t
6815        (cons (current-buffer) 'summary)))
6816      (t
6817       (error "Couldn't select virtual nndoc group")))))
6818
6819 (defun gnus-summary-isearch-article (&optional regexp-p)
6820   "Do incremental search forward on the current article.
6821 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6822   (interactive "P")
6823   (gnus-summary-select-article)
6824   (gnus-configure-windows 'article)
6825   (gnus-eval-in-buffer-window gnus-article-buffer
6826     (save-restriction
6827       (widen)
6828       (isearch-forward regexp-p))))
6829
6830 (defun gnus-summary-search-article-forward (regexp &optional backward)
6831   "Search for an article containing REGEXP forward.
6832 If BACKWARD, search backward instead."
6833   (interactive
6834    (list (read-string
6835           (format "Search article %s (regexp%s): "
6836                   (if current-prefix-arg "backward" "forward")
6837                   (if gnus-last-search-regexp
6838                       (concat ", default " gnus-last-search-regexp)
6839                     "")))
6840          current-prefix-arg))
6841   (if (string-equal regexp "")
6842       (setq regexp (or gnus-last-search-regexp ""))
6843     (setq gnus-last-search-regexp regexp))
6844   (if (gnus-summary-search-article regexp backward)
6845       (gnus-summary-show-thread)
6846     (error "Search failed: \"%s\"" regexp)))
6847
6848 (defun gnus-summary-search-article-backward (regexp)
6849   "Search for an article containing REGEXP backward."
6850   (interactive
6851    (list (read-string
6852           (format "Search article backward (regexp%s): "
6853                   (if gnus-last-search-regexp
6854                       (concat ", default " gnus-last-search-regexp)
6855                     "")))))
6856   (gnus-summary-search-article-forward regexp 'backward))
6857
6858 (defun gnus-summary-search-article (regexp &optional backward)
6859   "Search for an article containing REGEXP.
6860 Optional argument BACKWARD means do search for backward.
6861 `gnus-select-article-hook' is not called during the search."
6862   ;; We have to require this here to make sure that the following
6863   ;; dynamic binding isn't shadowed by autoloading.
6864   (require 'gnus-async)
6865   (require 'gnus-art)
6866   (let ((gnus-select-article-hook nil)  ;Disable hook.
6867         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6868         (gnus-use-article-prefetch nil)
6869         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6870         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6871         (sum (current-buffer))
6872         (gnus-display-mime-function nil)
6873         (found nil)
6874         point)
6875     (gnus-save-hidden-threads
6876       (gnus-summary-select-article)
6877       (set-buffer gnus-article-buffer)
6878       (when backward
6879         (forward-line -1))
6880       (while (not found)
6881         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6882         (if (if backward
6883                 (re-search-backward regexp nil t)
6884               (re-search-forward regexp nil t))
6885             ;; We found the regexp.
6886             (progn
6887               (setq found 'found)
6888               (beginning-of-line)
6889               (set-window-start
6890                (get-buffer-window (current-buffer))
6891                (point))
6892               (forward-line 1)
6893               (set-buffer sum)
6894               (setq point (point)))
6895           ;; We didn't find it, so we go to the next article.
6896           (set-buffer sum)
6897           (setq found 'not)
6898           (while (eq found 'not)
6899             (if (not (if backward (gnus-summary-find-prev)
6900                        (gnus-summary-find-next)))
6901                 ;; No more articles.
6902                 (setq found t)
6903               ;; Select the next article and adjust point.
6904               (unless (gnus-summary-article-sparse-p
6905                        (gnus-summary-article-number))
6906                 (setq found nil)
6907                 (gnus-summary-select-article)
6908                 (set-buffer gnus-article-buffer)
6909                 (widen)
6910                 (goto-char (if backward (point-max) (point-min))))))))
6911       (gnus-message 7 ""))
6912     ;; Return whether we found the regexp.
6913     (when (eq found 'found)
6914       (goto-char point)
6915       (gnus-summary-show-thread)
6916       (gnus-summary-goto-subject gnus-current-article)
6917       (gnus-summary-position-point)
6918       t)))
6919
6920 (defun gnus-summary-find-matching (header regexp &optional backward unread
6921                                           not-case-fold)
6922   "Return a list of all articles that match REGEXP on HEADER.
6923 The search stars on the current article and goes forwards unless
6924 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
6925 If UNREAD is non-nil, only unread articles will
6926 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
6927 in the comparisons."
6928   (let ((data (if (eq backward 'all) gnus-newsgroup-data
6929                 (gnus-data-find-list
6930                  (gnus-summary-article-number) (gnus-data-list backward))))
6931         (case-fold-search (not not-case-fold))
6932         articles d func)
6933     (if (consp header)
6934         (if (eq (car header) 'extra)
6935             (setq func
6936                   `(lambda (h)
6937                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
6938                          "")))
6939           (error "%s is an invalid header" header))
6940       (unless (fboundp (intern (concat "mail-header-" header)))
6941         (error "%s is not a valid header" header))
6942       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
6943     (while data
6944       (setq d (car data))
6945       (and (or (not unread)             ; We want all articles...
6946                (gnus-data-unread-p d))  ; Or just unreads.
6947            (vectorp (gnus-data-header d)) ; It's not a pseudo.
6948            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
6949            (push (gnus-data-number d) articles)) ; Success!
6950       (setq data (cdr data)))
6951     (nreverse articles)))
6952
6953 (defun gnus-summary-execute-command (header regexp command &optional backward)
6954   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
6955 If HEADER is an empty string (or nil), the match is done on the entire
6956 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
6957   (interactive
6958    (list (let ((completion-ignore-case t))
6959            (completing-read
6960             "Header name: "
6961             (mapcar (lambda (string) (list string))
6962                     '("Number" "Subject" "From" "Lines" "Date"
6963                       "Message-ID" "Xref" "References" "Body"))
6964             nil 'require-match))
6965          (read-string "Regexp: ")
6966          (read-key-sequence "Command: ")
6967          current-prefix-arg))
6968   (when (equal header "Body")
6969     (setq header ""))
6970   ;; Hidden thread subtrees must be searched as well.
6971   (gnus-summary-show-all-threads)
6972   ;; We don't want to change current point nor window configuration.
6973   (save-excursion
6974     (save-window-excursion
6975       (gnus-message 6 "Executing %s..." (key-description command))
6976       ;; We'd like to execute COMMAND interactively so as to give arguments.
6977       (gnus-execute header regexp
6978                     `(call-interactively ',(key-binding command))
6979                     backward)
6980       (gnus-message 6 "Executing %s...done" (key-description command)))))
6981
6982 (defun gnus-summary-beginning-of-article ()
6983   "Scroll the article back to the beginning."
6984   (interactive)
6985   (gnus-summary-select-article)
6986   (gnus-configure-windows 'article)
6987   (gnus-eval-in-buffer-window gnus-article-buffer
6988     (widen)
6989     (goto-char (point-min))
6990     (when gnus-page-broken
6991       (gnus-narrow-to-page))))
6992
6993 (defun gnus-summary-end-of-article ()
6994   "Scroll to the end of the article."
6995   (interactive)
6996   (gnus-summary-select-article)
6997   (gnus-configure-windows 'article)
6998   (gnus-eval-in-buffer-window gnus-article-buffer
6999     (widen)
7000     (goto-char (point-max))
7001     (recenter -3)
7002     (when gnus-page-broken
7003       (gnus-narrow-to-page))))
7004
7005 (defun gnus-summary-print-article (&optional filename n)
7006   "Generate and print a PostScript image of the N next (mail) articles.
7007
7008 If N is negative, print the N previous articles.  If N is nil and articles
7009 have been marked with the process mark, print these instead.
7010
7011 If the optional first argument FILENAME is nil, send the image to the
7012 printer.  If FILENAME is a string, save the PostScript image in a file with
7013 that name.  If FILENAME is a number, prompt the user for the name of the file
7014 to save in."
7015   (interactive (list (ps-print-preprint current-prefix-arg)
7016                      current-prefix-arg))
7017   (dolist (article (gnus-summary-work-articles n))
7018     (gnus-summary-select-article nil nil 'pseudo article)
7019     (gnus-eval-in-buffer-window gnus-article-buffer
7020       (let ((buffer (generate-new-buffer " *print*")))
7021         (unwind-protect
7022             (progn
7023               (copy-to-buffer buffer (point-min) (point-max))
7024               (set-buffer buffer)
7025               (gnus-article-delete-invisible-text)
7026               (let ((ps-left-header
7027                      (list
7028                       (concat "("
7029                               (mail-header-subject gnus-current-headers) ")")
7030                       (concat "("
7031                               (mail-header-from gnus-current-headers) ")")))
7032                     (ps-right-header
7033                      (list
7034                       "/pagenumberstring load"
7035                       (concat "("
7036                               (mail-header-date gnus-current-headers) ")"))))
7037                 (gnus-run-hooks 'gnus-ps-print-hook)
7038                 (save-excursion
7039                   (ps-print-buffer-with-faces filename))))
7040           (kill-buffer buffer))))))
7041
7042 (defun gnus-summary-show-article (&optional arg)
7043   "Force re-fetching of the current article.
7044 If ARG (the prefix) is non-nil, show the raw article without any
7045 article massaging functions being run."
7046   (interactive "P")
7047   (if (not arg)
7048       ;; Select the article the normal way.
7049       (gnus-summary-select-article nil 'force)
7050     ;; We have to require this here to make sure that the following
7051     ;; dynamic binding isn't shadowed by autoloading.
7052     (require 'gnus-async)
7053     (require 'gnus-art)
7054     ;; Bind the article treatment functions to nil.
7055     (let ((gnus-have-all-headers t)
7056           gnus-article-prepare-hook
7057           gnus-article-decode-hook
7058           gnus-display-mime-function
7059           gnus-break-pages
7060           gnus-visual)
7061       ;; Destroy any MIME parts.
7062       (when (gnus-buffer-live-p gnus-article-buffer)
7063         (save-excursion
7064           (set-buffer gnus-article-buffer)
7065           (mm-destroy-parts gnus-article-mime-handles)))
7066       (gnus-summary-select-article nil 'force)))
7067   (gnus-summary-goto-subject gnus-current-article)
7068   (gnus-summary-position-point))
7069
7070 (defun gnus-summary-verbose-headers (&optional arg)
7071   "Toggle permanent full header display.
7072 If ARG is a positive number, turn header display on.
7073 If ARG is a negative number, turn header display off."
7074   (interactive "P")
7075   (setq gnus-show-all-headers
7076         (cond ((or (not (numberp arg))
7077                    (zerop arg))
7078                (not gnus-show-all-headers))
7079               ((natnump arg)
7080                t)))
7081   (gnus-summary-show-article))
7082
7083 (defun gnus-summary-toggle-header (&optional arg)
7084   "Show the headers if they are hidden, or hide them if they are shown.
7085 If ARG is a positive number, show the entire header.
7086 If ARG is a negative number, hide the unwanted header lines."
7087   (interactive "P")
7088   (save-excursion
7089     (set-buffer gnus-article-buffer)
7090     (save-restriction
7091       (let* ((buffer-read-only nil)
7092              (inhibit-point-motion-hooks t)
7093              hidden e)
7094         (save-restriction 
7095           (article-narrow-to-head)
7096           (setq hidden (gnus-article-hidden-text-p 'headers)))
7097         (goto-char (point-min))
7098         (when (search-forward "\n\n" nil t)
7099           (delete-region (point-min) (1- (point))))
7100         (goto-char (point-min))
7101         (save-excursion
7102           (set-buffer gnus-original-article-buffer)
7103           (goto-char (point-min))
7104           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7105         (insert-buffer-substring gnus-original-article-buffer 1 e)
7106         (save-restriction
7107           (narrow-to-region (point-min) (point))
7108           (article-decode-encoded-words)
7109           (if (or hidden
7110                   (and (numberp arg) (< arg 0)))
7111               (let ((gnus-treat-hide-headers nil)
7112                     (gnus-treat-hide-boring-headers nil))
7113                 (gnus-treat-article 'head))
7114             (gnus-treat-article 'head)))))))
7115
7116 (defun gnus-summary-show-all-headers ()
7117   "Make all header lines visible."
7118   (interactive)
7119   (gnus-article-show-all-headers))
7120
7121 (defun gnus-summary-caesar-message (&optional arg)
7122   "Caesar rotate the current article by 13.
7123 The numerical prefix specifies how many places to rotate each letter
7124 forward."
7125   (interactive "P")
7126   (gnus-summary-select-article)
7127   (let ((mail-header-separator ""))
7128     (gnus-eval-in-buffer-window gnus-article-buffer
7129       (save-restriction
7130         (widen)
7131         (let ((start (window-start))
7132               buffer-read-only)
7133           (message-caesar-buffer-body arg)
7134           (set-window-start (get-buffer-window (current-buffer)) start))))))
7135
7136 (defun gnus-summary-stop-page-breaking ()
7137   "Stop page breaking in the current article."
7138   (interactive)
7139   (gnus-summary-select-article)
7140   (gnus-eval-in-buffer-window gnus-article-buffer
7141     (widen)
7142     (when (gnus-visual-p 'page-marker)
7143       (let ((buffer-read-only nil))
7144         (gnus-remove-text-with-property 'gnus-prev)
7145         (gnus-remove-text-with-property 'gnus-next))
7146       (setq gnus-page-broken nil))))
7147
7148 (defun gnus-summary-move-article (&optional n to-newsgroup
7149                                             select-method action)
7150   "Move the current article to a different newsgroup.
7151 If N is a positive number, move the N next articles.
7152 If N is a negative number, move the N previous articles.
7153 If N is nil and any articles have been marked with the process mark,
7154 move those articles instead.
7155 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7156 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7157 re-spool using this method.
7158
7159 For this function to work, both the current newsgroup and the
7160 newsgroup that you want to move to have to support the `request-move'
7161 and `request-accept' functions."
7162   (interactive "P")
7163   (unless action
7164     (setq action 'move))
7165   ;; Disable marking as read.
7166   (let (gnus-mark-article-hook)
7167     (save-window-excursion
7168       (gnus-summary-select-article)))
7169   ;; Check whether the source group supports the required functions.
7170   (cond ((and (eq action 'move)
7171               (not (gnus-check-backend-function
7172                     'request-move-article gnus-newsgroup-name)))
7173          (error "The current group does not support article moving"))
7174         ((and (eq action 'crosspost)
7175               (not (gnus-check-backend-function
7176                     'request-replace-article gnus-newsgroup-name)))
7177          (error "The current group does not support article editing")))
7178   (let ((articles (gnus-summary-work-articles n))
7179         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7180         (names '((move "Move" "Moving")
7181                  (copy "Copy" "Copying")
7182                  (crosspost "Crosspost" "Crossposting")))
7183         (copy-buf (save-excursion
7184                     (nnheader-set-temp-buffer " *copy article*")))
7185         art-group to-method new-xref article to-groups)
7186     (unless (assq action names)
7187       (error "Unknown action %s" action))
7188     ;; Read the newsgroup name.
7189     (when (and (not to-newsgroup)
7190                (not select-method))
7191       (setq to-newsgroup
7192             (gnus-read-move-group-name
7193              (cadr (assq action names))
7194              (symbol-value (intern (format "gnus-current-%s-group" action)))
7195              articles prefix))
7196       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7197     (setq to-method (or select-method
7198                         (gnus-group-name-to-method to-newsgroup)))
7199     ;; Check the method we are to move this article to...
7200     (unless (gnus-check-backend-function
7201              'request-accept-article (car to-method))
7202       (error "%s does not support article copying" (car to-method)))
7203     (unless (gnus-check-server to-method)
7204       (error "Can't open server %s" (car to-method)))
7205     (gnus-message 6 "%s to %s: %s..."
7206                   (caddr (assq action names))
7207                   (or (car select-method) to-newsgroup) articles)
7208     (while articles
7209       (setq article (pop articles))
7210       (setq
7211        art-group
7212        (cond
7213         ;; Move the article.
7214         ((eq action 'move)
7215          ;; Remove this article from future suppression.
7216          (gnus-dup-unsuppress-article article)
7217          (gnus-request-move-article
7218           article                       ; Article to move
7219           gnus-newsgroup-name           ; From newsgroup
7220           (nth 1 (gnus-find-method-for-group
7221                   gnus-newsgroup-name)) ; Server
7222           (list 'gnus-request-accept-article
7223                 to-newsgroup (list 'quote select-method)
7224                 (not articles) t)               ; Accept form
7225           (not articles)))              ; Only save nov last time
7226         ;; Copy the article.
7227         ((eq action 'copy)
7228          (save-excursion
7229            (set-buffer copy-buf)
7230            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7231              (gnus-request-accept-article
7232               to-newsgroup select-method (not articles) t))))
7233         ;; Crosspost the article.
7234         ((eq action 'crosspost)
7235          (let ((xref (message-tokenize-header
7236                       (mail-header-xref (gnus-summary-article-header article))
7237                       " ")))
7238            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7239                                   ":" article))
7240            (unless xref
7241              (setq xref (list (system-name))))
7242            (setq new-xref
7243                  (concat
7244                   (mapconcat 'identity
7245                              (delete "Xref:" (delete new-xref xref))
7246                              " ")
7247                   " " new-xref))
7248            (save-excursion
7249              (set-buffer copy-buf)
7250              ;; First put the article in the destination group.
7251              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7252              (when (consp (setq art-group
7253                                 (gnus-request-accept-article
7254                                  to-newsgroup select-method (not articles))))
7255                (setq new-xref (concat new-xref " " (car art-group)
7256                                       ":" (cdr art-group)))
7257                ;; Now we have the new Xrefs header, so we insert
7258                ;; it and replace the new article.
7259                (nnheader-replace-header "Xref" new-xref)
7260                (gnus-request-replace-article
7261                 (cdr art-group) to-newsgroup (current-buffer))
7262                art-group))))))
7263       (cond
7264        ((not art-group)
7265         (gnus-message 1 "Couldn't %s article %s"
7266                       (cadr (assq action names)) article))
7267        ((and (eq art-group 'junk)
7268              (eq action 'move))
7269         (gnus-summary-mark-article article gnus-canceled-mark)
7270         (gnus-message 4 "Deleted article %s" article))
7271        (t
7272         (let* ((pto-group (gnus-group-prefixed-name
7273                            (car art-group) to-method))
7274                (entry
7275                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7276                (info (nth 2 entry))
7277                (to-group (gnus-info-group info)))
7278           ;; Update the group that has been moved to.
7279           (when (and info
7280                      (memq action '(move copy)))
7281             (unless (member to-group to-groups)
7282               (push to-group to-groups))
7283
7284             (unless (memq article gnus-newsgroup-unreads)
7285               (gnus-info-set-read
7286                info (gnus-add-to-range (gnus-info-read info)
7287                                        (list (cdr art-group)))))
7288
7289             ;; Copy any marks over to the new group.
7290             (let ((marks gnus-article-mark-lists)
7291                   (to-article (cdr art-group)))
7292
7293               ;; See whether the article is to be put in the cache.
7294               (when gnus-use-cache
7295                 (gnus-cache-possibly-enter-article
7296                  to-group to-article
7297                  (memq article gnus-newsgroup-marked)
7298                  (memq article gnus-newsgroup-dormant)
7299                  (memq article gnus-newsgroup-unreads)))
7300
7301               (when (and (equal to-group gnus-newsgroup-name)
7302                          (not (memq article gnus-newsgroup-unreads)))
7303                 ;; Mark this article as read in this group.
7304                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7305                 (setcdr (gnus-active to-group) to-article)
7306                 (setcdr gnus-newsgroup-active to-article))
7307
7308               (while marks
7309                 (when (memq article (symbol-value
7310                                      (intern (format "gnus-newsgroup-%s"
7311                                                      (caar marks)))))
7312                   ;; If the other group is the same as this group,
7313                   ;; then we have to add the mark to the list.
7314                   (when (equal to-group gnus-newsgroup-name)
7315                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7316                          (cons to-article
7317                                (symbol-value
7318                                 (intern (format "gnus-newsgroup-%s"
7319                                                 (caar marks)))))))
7320                   ;; Copy the marks to other group.
7321                   (gnus-add-marked-articles
7322                    to-group (cdar marks) (list to-article) info))
7323                 (setq marks (cdr marks)))
7324
7325               (gnus-dribble-enter
7326                (concat "(gnus-group-set-info '"
7327                        (gnus-prin1-to-string (gnus-get-info to-group))
7328                        ")"))))
7329
7330           ;; Update the Xref header in this article to point to
7331           ;; the new crossposted article we have just created.
7332           (when (eq action 'crosspost)
7333             (save-excursion
7334               (set-buffer copy-buf)
7335               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7336               (nnheader-replace-header "Xref" new-xref)
7337               (gnus-request-replace-article
7338                article gnus-newsgroup-name (current-buffer)))))
7339
7340         ;;;!!!Why is this necessary?
7341         (set-buffer gnus-summary-buffer)
7342
7343         (gnus-summary-goto-subject article)
7344         (when (eq action 'move)
7345           (gnus-summary-mark-article article gnus-canceled-mark))))
7346       (gnus-summary-remove-process-mark article))
7347     ;; Re-activate all groups that have been moved to.
7348     (while to-groups
7349       (save-excursion
7350         (set-buffer gnus-group-buffer)
7351         (when (gnus-group-goto-group (car to-groups) t)
7352           (gnus-group-get-new-news-this-group 1 t))
7353         (pop to-groups)))
7354
7355     (gnus-kill-buffer copy-buf)
7356     (gnus-summary-position-point)
7357     (gnus-set-mode-line 'summary)))
7358
7359 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7360   "Move the current article to a different newsgroup.
7361 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7362 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7363 re-spool using this method."
7364   (interactive "P")
7365   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7366
7367 (defun gnus-summary-crosspost-article (&optional n)
7368   "Crosspost the current article to some other group."
7369   (interactive "P")
7370   (gnus-summary-move-article n nil nil 'crosspost))
7371
7372 (defcustom gnus-summary-respool-default-method nil
7373   "Default method for respooling an article.
7374 If nil, use to the current newsgroup method."
7375   :type '(choice (gnus-select-method :value (nnml ""))
7376                  (const nil))
7377   :group 'gnus-summary-mail)
7378
7379 (defun gnus-summary-respool-article (&optional n method)
7380   "Respool the current article.
7381 The article will be squeezed through the mail spooling process again,
7382 which means that it will be put in some mail newsgroup or other
7383 depending on `nnmail-split-methods'.
7384 If N is a positive number, respool the N next articles.
7385 If N is a negative number, respool the N previous articles.
7386 If N is nil and any articles have been marked with the process mark,
7387 respool those articles instead.
7388
7389 Respooling can be done both from mail groups and \"real\" newsgroups.
7390 In the former case, the articles in question will be moved from the
7391 current group into whatever groups they are destined to.  In the
7392 latter case, they will be copied into the relevant groups."
7393   (interactive
7394    (list current-prefix-arg
7395          (let* ((methods (gnus-methods-using 'respool))
7396                 (methname
7397                  (symbol-name (or gnus-summary-respool-default-method
7398                                   (car (gnus-find-method-for-group
7399                                         gnus-newsgroup-name)))))
7400                 (method
7401                  (gnus-completing-read
7402                   methname "What backend do you want to use when respooling?"
7403                   methods nil t nil 'gnus-mail-method-history))
7404                 ms)
7405            (cond
7406             ((zerop (length (setq ms (gnus-servers-using-backend
7407                                       (intern method)))))
7408              (list (intern method) ""))
7409             ((= 1 (length ms))
7410              (car ms))
7411             (t
7412              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7413                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7414                            ms-alist))))))))
7415   (unless method
7416     (error "No method given for respooling"))
7417   (if (assoc (symbol-name
7418               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7419              (gnus-methods-using 'respool))
7420       (gnus-summary-move-article n nil method)
7421     (gnus-summary-copy-article n nil method)))
7422
7423 (defun gnus-summary-import-article (file)
7424   "Import an arbitrary file into a mail newsgroup."
7425   (interactive "fImport file: ")
7426   (let ((group gnus-newsgroup-name)
7427         (now (current-time))
7428         atts lines)
7429     (unless (gnus-check-backend-function 'request-accept-article group)
7430       (error "%s does not support article importing" group))
7431     (or (file-readable-p file)
7432         (not (file-regular-p file))
7433         (error "Can't read %s" file))
7434     (save-excursion
7435       (set-buffer (gnus-get-buffer-create " *import file*"))
7436       (erase-buffer)
7437       (insert-file-contents file)
7438       (goto-char (point-min))
7439       (unless (nnheader-article-p)
7440         ;; This doesn't look like an article, so we fudge some headers.
7441         (setq atts (file-attributes file)
7442               lines (count-lines (point-min) (point-max)))
7443         (insert "From: " (read-string "From: ") "\n"
7444                 "Subject: " (read-string "Subject: ") "\n"
7445                 "Date: " (message-make-date (nth 5 atts))
7446                 "\n"
7447                 "Message-ID: " (message-make-message-id) "\n"
7448                 "Lines: " (int-to-string lines) "\n"
7449                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7450       (gnus-request-accept-article group nil t)
7451       (kill-buffer (current-buffer)))))
7452
7453 (defun gnus-summary-article-posted-p ()
7454   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7455 This will be the case if the article has both been mailed and posted."
7456   (interactive)
7457   (let ((id (mail-header-references (gnus-summary-article-header)))
7458         (gnus-override-method
7459          (or gnus-refer-article-method gnus-select-method)))
7460     (if (gnus-request-head id "")
7461         (gnus-message 2 "The current message was found on %s"
7462                       gnus-override-method)
7463       (gnus-message 2 "The current message couldn't be found on %s"
7464                     gnus-override-method)
7465       nil)))
7466
7467 (defun gnus-summary-expire-articles (&optional now)
7468   "Expire all articles that are marked as expirable in the current group."
7469   (interactive)
7470   (when (gnus-check-backend-function
7471          'request-expire-articles gnus-newsgroup-name)
7472     ;; This backend supports expiry.
7473     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7474            (expirable (if total
7475                           (progn
7476                             ;; We need to update the info for
7477                             ;; this group for `gnus-list-of-read-articles'
7478                             ;; to give us the right answer.
7479                             (gnus-run-hooks 'gnus-exit-group-hook)
7480                             (gnus-summary-update-info)
7481                             (gnus-list-of-read-articles gnus-newsgroup-name))
7482                         (setq gnus-newsgroup-expirable
7483                               (sort gnus-newsgroup-expirable '<))))
7484            (expiry-wait (if now 'immediate
7485                           (gnus-group-find-parameter
7486                            gnus-newsgroup-name 'expiry-wait)))
7487            es)
7488       (when expirable
7489         ;; There are expirable articles in this group, so we run them
7490         ;; through the expiry process.
7491         (gnus-message 6 "Expiring articles...")
7492         (unless (gnus-check-group gnus-newsgroup-name)
7493           (error "Can't open server for %s" gnus-newsgroup-name))
7494         ;; The list of articles that weren't expired is returned.
7495         (save-excursion
7496           (if expiry-wait
7497               (let ((nnmail-expiry-wait-function nil)
7498                     (nnmail-expiry-wait expiry-wait))
7499                 (setq es (gnus-request-expire-articles
7500                           expirable gnus-newsgroup-name)))
7501             (setq es (gnus-request-expire-articles
7502                       expirable gnus-newsgroup-name))))
7503         (unless total
7504           (setq gnus-newsgroup-expirable es))
7505         ;; We go through the old list of expirable, and mark all
7506         ;; really expired articles as nonexistent.
7507         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7508           (let ((gnus-use-cache nil))
7509             (while expirable
7510               (unless (memq (car expirable) es)
7511                 (when (gnus-data-find (car expirable))
7512                   (gnus-summary-mark-article
7513                    (car expirable) gnus-canceled-mark)))
7514               (setq expirable (cdr expirable)))))
7515         (gnus-message 6 "Expiring articles...done")))))
7516
7517 (defun gnus-summary-expire-articles-now ()
7518   "Expunge all expirable articles in the current group.
7519 This means that *all* articles that are marked as expirable will be
7520 deleted forever, right now."
7521   (interactive)
7522   (or gnus-expert-user
7523       (gnus-yes-or-no-p
7524        "Are you really, really, really sure you want to delete all these messages? ")
7525       (error "Phew!"))
7526   (gnus-summary-expire-articles t))
7527
7528 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7529 (defun gnus-summary-delete-article (&optional n)
7530   "Delete the N next (mail) articles.
7531 This command actually deletes articles.  This is not a marking
7532 command.  The article will disappear forever from your life, never to
7533 return.
7534 If N is negative, delete backwards.
7535 If N is nil and articles have been marked with the process mark,
7536 delete these instead."
7537   (interactive "P")
7538   (unless (gnus-check-backend-function 'request-expire-articles
7539                                        gnus-newsgroup-name)
7540     (error "The current newsgroup does not support article deletion"))
7541   ;; Compute the list of articles to delete.
7542   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7543         not-deleted)
7544     (if (and gnus-novice-user
7545              (not (gnus-yes-or-no-p
7546                    (format "Do you really want to delete %s forever? "
7547                            (if (> (length articles) 1)
7548                                (format "these %s articles" (length articles))
7549                              "this article")))))
7550         ()
7551       ;; Delete the articles.
7552       (setq not-deleted (gnus-request-expire-articles
7553                          articles gnus-newsgroup-name 'force))
7554       (while articles
7555         (gnus-summary-remove-process-mark (car articles))
7556         ;; The backend might not have been able to delete the article
7557         ;; after all.
7558         (unless (memq (car articles) not-deleted)
7559           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7560         (setq articles (cdr articles)))
7561       (when not-deleted
7562         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7563     (gnus-summary-position-point)
7564     (gnus-set-mode-line 'summary)
7565     not-deleted))
7566
7567 (defun gnus-summary-edit-article (&optional force)
7568   "Edit the current article.
7569 This will have permanent effect only in mail groups.
7570 If FORCE is non-nil, allow editing of articles even in read-only
7571 groups."
7572   (interactive "P")
7573   (save-excursion
7574     (set-buffer gnus-summary-buffer)
7575     (let ((mail-parse-charset gnus-newsgroup-charset)
7576           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7577       (gnus-set-global-variables)
7578       (when (and (not force)
7579                  (gnus-group-read-only-p))
7580         (error "The current newsgroup does not support article editing"))
7581       (gnus-summary-show-article t)
7582       (gnus-article-edit-article
7583        'mime-to-mml
7584        `(lambda (no-highlight)
7585           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7586                 (mail-parse-ignored-charsets 
7587                  ',gnus-newsgroup-ignored-charsets))
7588             (mml-to-mime)
7589             (gnus-summary-edit-article-done
7590              ,(or (mail-header-references gnus-current-headers) "")
7591              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7592
7593 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7594
7595 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7596                                                  no-highlight)
7597   "Make edits to the current article permanent."
7598   (interactive)
7599   ;; Replace the article.
7600   (let ((buf (current-buffer)))
7601     (with-temp-buffer
7602       (insert-buffer-substring buf)
7603       (if (and (not read-only)
7604                (not (gnus-request-replace-article
7605                      (cdr gnus-article-current) (car gnus-article-current)
7606                      (current-buffer) t)))
7607           (error "Couldn't replace article")
7608         ;; Update the summary buffer.
7609         (if (and references
7610                  (equal (message-tokenize-header references " ")
7611                         (message-tokenize-header
7612                          (or (message-fetch-field "references") "") " ")))
7613             ;; We only have to update this line.
7614             (save-excursion
7615               (save-restriction
7616                 (message-narrow-to-head)
7617                 (let ((head (buffer-string))
7618                       header)
7619                   (with-temp-buffer
7620                     (insert (format "211 %d Article retrieved.\n"
7621                                     (cdr gnus-article-current)))
7622                     (insert head)
7623                     (insert ".\n")
7624                     (let ((nntp-server-buffer (current-buffer)))
7625                       (setq header (car (gnus-get-newsgroup-headers
7626                                          (save-excursion
7627                                            (set-buffer gnus-summary-buffer)
7628                                            gnus-newsgroup-dependencies)
7629                                          t))))
7630                     (save-excursion
7631                       (set-buffer gnus-summary-buffer)
7632                       (gnus-data-set-header
7633                        (gnus-data-find (cdr gnus-article-current))
7634                        header)
7635                       (gnus-summary-update-article-line
7636                        (cdr gnus-article-current) header))))))
7637           ;; Update threads.
7638           (set-buffer (or buffer gnus-summary-buffer))
7639           (gnus-summary-update-article (cdr gnus-article-current)))
7640         ;; Prettify the article buffer again.
7641         (unless no-highlight
7642           (save-excursion
7643             (set-buffer gnus-article-buffer)
7644             ;;;!!! Fix this -- article should be rehighlighted.
7645             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7646             (set-buffer gnus-original-article-buffer)
7647             (gnus-request-article
7648              (cdr gnus-article-current)
7649              (car gnus-article-current) (current-buffer))))
7650         ;; Prettify the summary buffer line.
7651         (when (gnus-visual-p 'summary-highlight 'highlight)
7652           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7653
7654 (defun gnus-summary-edit-wash (key)
7655   "Perform editing command KEY in the article buffer."
7656   (interactive
7657    (list
7658     (progn
7659       (message "%s" (concat (this-command-keys) "- "))
7660       (read-char))))
7661   (message "")
7662   (gnus-summary-edit-article)
7663   (execute-kbd-macro (concat (this-command-keys) key))
7664   (gnus-article-edit-done))
7665
7666 ;;; Respooling
7667
7668 (defun gnus-summary-respool-query (&optional silent trace)
7669   "Query where the respool algorithm would put this article."
7670   (interactive)
7671   (let (gnus-mark-article-hook)
7672     (gnus-summary-select-article)
7673     (save-excursion
7674       (set-buffer gnus-original-article-buffer)
7675       (save-restriction
7676         (message-narrow-to-head)
7677         (let ((groups (nnmail-article-group 'identity trace)))
7678           (unless silent
7679             (if groups
7680                 (message "This message would go to %s"
7681                          (mapconcat 'car groups ", "))
7682               (message "This message would go to no groups"))
7683             groups))))))
7684
7685 (defun gnus-summary-respool-trace ()
7686   "Trace where the respool algorithm would put this article.
7687 Display a buffer showing all fancy splitting patterns which matched."
7688   (interactive)
7689   (gnus-summary-respool-query nil t))
7690
7691 ;; Summary marking commands.
7692
7693 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7694   "Mark articles which has the same subject as read, and then select the next.
7695 If UNMARK is positive, remove any kind of mark.
7696 If UNMARK is negative, tick articles."
7697   (interactive "P")
7698   (when unmark
7699     (setq unmark (prefix-numeric-value unmark)))
7700   (let ((count
7701          (gnus-summary-mark-same-subject
7702           (gnus-summary-article-subject) unmark)))
7703     ;; Select next unread article.  If auto-select-same mode, should
7704     ;; select the first unread article.
7705     (gnus-summary-next-article t (and gnus-auto-select-same
7706                                       (gnus-summary-article-subject)))
7707     (gnus-message 7 "%d article%s marked as %s"
7708                   count (if (= count 1) " is" "s are")
7709                   (if unmark "unread" "read"))))
7710
7711 (defun gnus-summary-kill-same-subject (&optional unmark)
7712   "Mark articles which has the same subject as read.
7713 If UNMARK is positive, remove any kind of mark.
7714 If UNMARK is negative, tick articles."
7715   (interactive "P")
7716   (when unmark
7717     (setq unmark (prefix-numeric-value unmark)))
7718   (let ((count
7719          (gnus-summary-mark-same-subject
7720           (gnus-summary-article-subject) unmark)))
7721     ;; If marked as read, go to next unread subject.
7722     (when (null unmark)
7723       ;; Go to next unread subject.
7724       (gnus-summary-next-subject 1 t))
7725     (gnus-message 7 "%d articles are marked as %s"
7726                   count (if unmark "unread" "read"))))
7727
7728 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7729   "Mark articles with same SUBJECT as read, and return marked number.
7730 If optional argument UNMARK is positive, remove any kinds of marks.
7731 If optional argument UNMARK is negative, mark articles as unread instead."
7732   (let ((count 1))
7733     (save-excursion
7734       (cond
7735        ((null unmark)                   ; Mark as read.
7736         (while (and
7737                 (progn
7738                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7739                   (gnus-summary-show-thread) t)
7740                 (gnus-summary-find-subject subject))
7741           (setq count (1+ count))))
7742        ((> unmark 0)                    ; Tick.
7743         (while (and
7744                 (progn
7745                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7746                   (gnus-summary-show-thread) t)
7747                 (gnus-summary-find-subject subject))
7748           (setq count (1+ count))))
7749        (t                               ; Mark as unread.
7750         (while (and
7751                 (progn
7752                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7753                   (gnus-summary-show-thread) t)
7754                 (gnus-summary-find-subject subject))
7755           (setq count (1+ count)))))
7756       (gnus-set-mode-line 'summary)
7757       ;; Return the number of marked articles.
7758       count)))
7759
7760 (defun gnus-summary-mark-as-processable (n &optional unmark)
7761   "Set the process mark on the next N articles.
7762 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7763 the process mark instead.  The difference between N and the actual
7764 number of articles marked is returned."
7765   (interactive "p")
7766   (let ((backward (< n 0))
7767         (n (abs n)))
7768     (while (and
7769             (> n 0)
7770             (if unmark
7771                 (gnus-summary-remove-process-mark
7772                  (gnus-summary-article-number))
7773               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7774             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7775       (setq n (1- n)))
7776     (when (/= 0 n)
7777       (gnus-message 7 "No more articles"))
7778     (gnus-summary-recenter)
7779     (gnus-summary-position-point)
7780     n))
7781
7782 (defun gnus-summary-unmark-as-processable (n)
7783   "Remove the process mark from the next N articles.
7784 If N is negative, unmark backward instead.  The difference between N and
7785 the actual number of articles unmarked is returned."
7786   (interactive "p")
7787   (gnus-summary-mark-as-processable n t))
7788
7789 (defun gnus-summary-unmark-all-processable ()
7790   "Remove the process mark from all articles."
7791   (interactive)
7792   (save-excursion
7793     (while gnus-newsgroup-processable
7794       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7795   (gnus-summary-position-point))
7796
7797 (defun gnus-summary-mark-as-expirable (n)
7798   "Mark N articles forward as expirable.
7799 If N is negative, mark backward instead.  The difference between N and
7800 the actual number of articles marked is returned."
7801   (interactive "p")
7802   (gnus-summary-mark-forward n gnus-expirable-mark))
7803
7804 (defun gnus-summary-mark-article-as-replied (article)
7805   "Mark ARTICLE replied and update the summary line."
7806   (push article gnus-newsgroup-replied)
7807   (let ((buffer-read-only nil))
7808     (when (gnus-summary-goto-subject article nil t)
7809       (gnus-summary-update-secondary-mark article))))
7810
7811 (defun gnus-summary-set-bookmark (article)
7812   "Set a bookmark in current article."
7813   (interactive (list (gnus-summary-article-number)))
7814   (when (or (not (get-buffer gnus-article-buffer))
7815             (not gnus-current-article)
7816             (not gnus-article-current)
7817             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7818     (error "No current article selected"))
7819   ;; Remove old bookmark, if one exists.
7820   (let ((old (assq article gnus-newsgroup-bookmarks)))
7821     (when old
7822       (setq gnus-newsgroup-bookmarks
7823             (delq old gnus-newsgroup-bookmarks))))
7824   ;; Set the new bookmark, which is on the form
7825   ;; (article-number . line-number-in-body).
7826   (push
7827    (cons article
7828          (save-excursion
7829            (set-buffer gnus-article-buffer)
7830            (count-lines
7831             (min (point)
7832                  (save-excursion
7833                    (goto-char (point-min))
7834                    (search-forward "\n\n" nil t)
7835                    (point)))
7836             (point))))
7837    gnus-newsgroup-bookmarks)
7838   (gnus-message 6 "A bookmark has been added to the current article."))
7839
7840 (defun gnus-summary-remove-bookmark (article)
7841   "Remove the bookmark from the current article."
7842   (interactive (list (gnus-summary-article-number)))
7843   ;; Remove old bookmark, if one exists.
7844   (let ((old (assq article gnus-newsgroup-bookmarks)))
7845     (if old
7846         (progn
7847           (setq gnus-newsgroup-bookmarks
7848                 (delq old gnus-newsgroup-bookmarks))
7849           (gnus-message 6 "Removed bookmark."))
7850       (gnus-message 6 "No bookmark in current article."))))
7851
7852 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7853 (defun gnus-summary-mark-as-dormant (n)
7854   "Mark N articles forward as dormant.
7855 If N is negative, mark backward instead.  The difference between N and
7856 the actual number of articles marked is returned."
7857   (interactive "p")
7858   (gnus-summary-mark-forward n gnus-dormant-mark))
7859
7860 (defun gnus-summary-set-process-mark (article)
7861   "Set the process mark on ARTICLE and update the summary line."
7862   (setq gnus-newsgroup-processable
7863         (cons article
7864               (delq article gnus-newsgroup-processable)))
7865   (when (gnus-summary-goto-subject article)
7866     (gnus-summary-show-thread)
7867     (gnus-summary-goto-subject article)
7868     (gnus-summary-update-secondary-mark article)))
7869
7870 (defun gnus-summary-remove-process-mark (article)
7871   "Remove the process mark from ARTICLE and update the summary line."
7872   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7873   (when (gnus-summary-goto-subject article)
7874     (gnus-summary-show-thread)
7875     (gnus-summary-goto-subject article)
7876     (gnus-summary-update-secondary-mark article)))
7877
7878 (defun gnus-summary-set-saved-mark (article)
7879   "Set the process mark on ARTICLE and update the summary line."
7880   (push article gnus-newsgroup-saved)
7881   (when (gnus-summary-goto-subject article)
7882     (gnus-summary-update-secondary-mark article)))
7883
7884 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7885   "Mark N articles as read forwards.
7886 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7887 The difference between N and the actual number of articles marked is
7888 returned."
7889   (interactive "p")
7890   (gnus-summary-show-thread)
7891   (let ((backward (< n 0))
7892         (gnus-summary-goto-unread
7893          (and gnus-summary-goto-unread
7894               (not (eq gnus-summary-goto-unread 'never))
7895               (not (memq mark (list gnus-unread-mark
7896                                     gnus-ticked-mark gnus-dormant-mark)))))
7897         (n (abs n))
7898         (mark (or mark gnus-del-mark)))
7899     (while (and (> n 0)
7900                 (gnus-summary-mark-article nil mark no-expire)
7901                 (zerop (gnus-summary-next-subject
7902                         (if backward -1 1)
7903                         (and gnus-summary-goto-unread
7904                              (not (eq gnus-summary-goto-unread 'never)))
7905                         t)))
7906       (setq n (1- n)))
7907     (when (/= 0 n)
7908       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
7909     (gnus-summary-recenter)
7910     (gnus-summary-position-point)
7911     (gnus-set-mode-line 'summary)
7912     n))
7913
7914 (defun gnus-summary-mark-article-as-read (mark)
7915   "Mark the current article quickly as read with MARK."
7916   (let ((article (gnus-summary-article-number)))
7917     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7918     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7919     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7920     (push (cons article mark) gnus-newsgroup-reads)
7921     ;; Possibly remove from cache, if that is used.
7922     (when gnus-use-cache
7923       (gnus-cache-enter-remove-article article))
7924     ;; Allow the backend to change the mark.
7925     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7926     ;; Check for auto-expiry.
7927     (when (and gnus-newsgroup-auto-expire
7928                (memq mark gnus-auto-expirable-marks))
7929       (setq mark gnus-expirable-mark)
7930       ;; Let the backend know about the mark change.
7931       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7932       (push article gnus-newsgroup-expirable))
7933     ;; Set the mark in the buffer.
7934     (gnus-summary-update-mark mark 'unread)
7935     t))
7936
7937 (defun gnus-summary-mark-article-as-unread (mark)
7938   "Mark the current article quickly as unread with MARK."
7939   (let* ((article (gnus-summary-article-number))
7940          (old-mark (gnus-summary-article-mark article)))
7941     ;; Allow the backend to change the mark.
7942     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7943     (if (eq mark old-mark)
7944         t
7945       (if (<= article 0)
7946           (progn
7947             (gnus-error 1 "Can't mark negative article numbers")
7948             nil)
7949         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7950         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7951         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
7952         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
7953         (cond ((= mark gnus-ticked-mark)
7954                (push article gnus-newsgroup-marked))
7955               ((= mark gnus-dormant-mark)
7956                (push article gnus-newsgroup-dormant))
7957               (t
7958                (push article gnus-newsgroup-unreads)))
7959         (gnus-pull article gnus-newsgroup-reads)
7960
7961         ;; See whether the article is to be put in the cache.
7962         (and gnus-use-cache
7963              (vectorp (gnus-summary-article-header article))
7964              (save-excursion
7965                (gnus-cache-possibly-enter-article
7966                 gnus-newsgroup-name article
7967                 (= mark gnus-ticked-mark)
7968                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7969
7970         ;; Fix the mark.
7971         (gnus-summary-update-mark mark 'unread)
7972         t))))
7973
7974 (defun gnus-summary-mark-article (&optional article mark no-expire)
7975   "Mark ARTICLE with MARK.  MARK can be any character.
7976 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
7977 `??' (dormant) and `?E' (expirable).
7978 If MARK is nil, then the default character `?r' is used.
7979 If ARTICLE is nil, then the article on the current line will be
7980 marked."
7981   ;; The mark might be a string.
7982   (when (stringp mark)
7983     (setq mark (aref mark 0)))
7984   ;; If no mark is given, then we check auto-expiring.
7985   (when (null mark)
7986     (setq mark gnus-del-mark))
7987   (when (and (not no-expire)
7988              gnus-newsgroup-auto-expire
7989              (memq mark gnus-auto-expirable-marks))
7990     (setq mark gnus-expirable-mark))
7991   (let ((article (or article (gnus-summary-article-number)))
7992         (old-mark (gnus-summary-article-mark article)))
7993     ;; Allow the backend to change the mark.
7994     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7995     (if (eq mark old-mark)
7996         t
7997       (unless article
7998         (error "No article on current line"))
7999       (if (not (if (or (= mark gnus-unread-mark)
8000                        (= mark gnus-ticked-mark)
8001                        (= mark gnus-dormant-mark))
8002                    (gnus-mark-article-as-unread article mark)
8003                  (gnus-mark-article-as-read article mark)))
8004           t
8005         ;; See whether the article is to be put in the cache.
8006         (and gnus-use-cache
8007              (not (= mark gnus-canceled-mark))
8008              (vectorp (gnus-summary-article-header article))
8009              (save-excursion
8010                (gnus-cache-possibly-enter-article
8011                 gnus-newsgroup-name article
8012                 (= mark gnus-ticked-mark)
8013                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8014
8015         (when (gnus-summary-goto-subject article nil t)
8016           (let ((buffer-read-only nil))
8017             (gnus-summary-show-thread)
8018             ;; Fix the mark.
8019             (gnus-summary-update-mark mark 'unread)
8020             t))))))
8021
8022 (defun gnus-summary-update-secondary-mark (article)
8023   "Update the secondary (read, process, cache) mark."
8024   (gnus-summary-update-mark
8025    (cond ((memq article gnus-newsgroup-processable)
8026           gnus-process-mark)
8027          ((memq article gnus-newsgroup-cached)
8028           gnus-cached-mark)
8029          ((memq article gnus-newsgroup-replied)
8030           gnus-replied-mark)
8031          ((memq article gnus-newsgroup-saved)
8032           gnus-saved-mark)
8033          (t gnus-unread-mark))
8034    'replied)
8035   (when (gnus-visual-p 'summary-highlight 'highlight)
8036     (gnus-run-hooks 'gnus-summary-update-hook))
8037   t)
8038
8039 (defun gnus-summary-update-mark (mark type)
8040   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8041         (buffer-read-only nil))
8042     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8043     (when forward
8044       (when (looking-at "\r")
8045         (incf forward))
8046       (when (<= (+ forward (point)) (point-max))
8047         ;; Go to the right position on the line.
8048         (goto-char (+ forward (point)))
8049         ;; Replace the old mark with the new mark.
8050         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8051         ;; Optionally update the marks by some user rule.
8052         (when (eq type 'unread)
8053           (gnus-data-set-mark
8054            (gnus-data-find (gnus-summary-article-number)) mark)
8055           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8056
8057 (defun gnus-mark-article-as-read (article &optional mark)
8058   "Enter ARTICLE in the pertinent lists and remove it from others."
8059   ;; Make the article expirable.
8060   (let ((mark (or mark gnus-del-mark)))
8061     (if (= mark gnus-expirable-mark)
8062         (push article gnus-newsgroup-expirable)
8063       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8064     ;; Remove from unread and marked lists.
8065     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8066     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8067     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8068     (push (cons article mark) gnus-newsgroup-reads)
8069     ;; Possibly remove from cache, if that is used.
8070     (when gnus-use-cache
8071       (gnus-cache-enter-remove-article article))
8072     t))
8073
8074 (defun gnus-mark-article-as-unread (article &optional mark)
8075   "Enter ARTICLE in the pertinent lists and remove it from others."
8076   (let ((mark (or mark gnus-ticked-mark)))
8077     (if (<= article 0)
8078         (progn
8079           (gnus-error 1 "Can't mark negative article numbers")
8080           nil)
8081       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8082             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8083             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8084             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8085
8086       ;; Unsuppress duplicates?
8087       (when gnus-suppress-duplicates
8088         (gnus-dup-unsuppress-article article))
8089
8090       (cond ((= mark gnus-ticked-mark)
8091              (push article gnus-newsgroup-marked))
8092             ((= mark gnus-dormant-mark)
8093              (push article gnus-newsgroup-dormant))
8094             (t
8095              (push article gnus-newsgroup-unreads)))
8096       (gnus-pull article gnus-newsgroup-reads)
8097       t)))
8098
8099 (defalias 'gnus-summary-mark-as-unread-forward
8100   'gnus-summary-tick-article-forward)
8101 (make-obsolete 'gnus-summary-mark-as-unread-forward
8102                'gnus-summary-tick-article-forward)
8103 (defun gnus-summary-tick-article-forward (n)
8104   "Tick N articles forwards.
8105 If N is negative, tick backwards instead.
8106 The difference between N and the number of articles ticked is returned."
8107   (interactive "p")
8108   (gnus-summary-mark-forward n gnus-ticked-mark))
8109
8110 (defalias 'gnus-summary-mark-as-unread-backward
8111   'gnus-summary-tick-article-backward)
8112 (make-obsolete 'gnus-summary-mark-as-unread-backward
8113                'gnus-summary-tick-article-backward)
8114 (defun gnus-summary-tick-article-backward (n)
8115   "Tick N articles backwards.
8116 The difference between N and the number of articles ticked is returned."
8117   (interactive "p")
8118   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8119
8120 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8121 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8122 (defun gnus-summary-tick-article (&optional article clear-mark)
8123   "Mark current article as unread.
8124 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8125 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8126   (interactive)
8127   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8128                                        gnus-ticked-mark)))
8129
8130 (defun gnus-summary-mark-as-read-forward (n)
8131   "Mark N articles as read forwards.
8132 If N is negative, mark backwards instead.
8133 The difference between N and the actual number of articles marked is
8134 returned."
8135   (interactive "p")
8136   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8137
8138 (defun gnus-summary-mark-as-read-backward (n)
8139   "Mark the N articles as read backwards.
8140 The difference between N and the actual number of articles marked is
8141 returned."
8142   (interactive "p")
8143   (gnus-summary-mark-forward
8144    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8145
8146 (defun gnus-summary-mark-as-read (&optional article mark)
8147   "Mark current article as read.
8148 ARTICLE specifies the article to be marked as read.
8149 MARK specifies a string to be inserted at the beginning of the line."
8150   (gnus-summary-mark-article article mark))
8151
8152 (defun gnus-summary-clear-mark-forward (n)
8153   "Clear marks from N articles forward.
8154 If N is negative, clear backward instead.
8155 The difference between N and the number of marks cleared is returned."
8156   (interactive "p")
8157   (gnus-summary-mark-forward n gnus-unread-mark))
8158
8159 (defun gnus-summary-clear-mark-backward (n)
8160   "Clear marks from N articles backward.
8161 The difference between N and the number of marks cleared is returned."
8162   (interactive "p")
8163   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8164
8165 (defun gnus-summary-mark-unread-as-read ()
8166   "Intended to be used by `gnus-summary-mark-article-hook'."
8167   (when (memq gnus-current-article gnus-newsgroup-unreads)
8168     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8169
8170 (defun gnus-summary-mark-read-and-unread-as-read ()
8171   "Intended to be used by `gnus-summary-mark-article-hook'."
8172   (let ((mark (gnus-summary-article-mark)))
8173     (when (or (gnus-unread-mark-p mark)
8174               (gnus-read-mark-p mark))
8175       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8176
8177 (defun gnus-summary-mark-region-as-read (point mark all)
8178   "Mark all unread articles between point and mark as read.
8179 If given a prefix, mark all articles between point and mark as read,
8180 even ticked and dormant ones."
8181   (interactive "r\nP")
8182   (save-excursion
8183     (let (article)
8184       (goto-char point)
8185       (beginning-of-line)
8186       (while (and
8187               (< (point) mark)
8188               (progn
8189                 (when (or all
8190                           (memq (setq article (gnus-summary-article-number))
8191                                 gnus-newsgroup-unreads))
8192                   (gnus-summary-mark-article article gnus-del-mark))
8193                 t)
8194               (gnus-summary-find-next))))))
8195
8196 (defun gnus-summary-mark-below (score mark)
8197   "Mark articles with score less than SCORE with MARK."
8198   (interactive "P\ncMark: ")
8199   (setq score (if score
8200                   (prefix-numeric-value score)
8201                 (or gnus-summary-default-score 0)))
8202   (save-excursion
8203     (set-buffer gnus-summary-buffer)
8204     (goto-char (point-min))
8205     (while
8206         (progn
8207           (and (< (gnus-summary-article-score) score)
8208                (gnus-summary-mark-article nil mark))
8209           (gnus-summary-find-next)))))
8210
8211 (defun gnus-summary-kill-below (&optional score)
8212   "Mark articles with score below SCORE as read."
8213   (interactive "P")
8214   (gnus-summary-mark-below score gnus-killed-mark))
8215
8216 (defun gnus-summary-clear-above (&optional score)
8217   "Clear all marks from articles with score above SCORE."
8218   (interactive "P")
8219   (gnus-summary-mark-above score gnus-unread-mark))
8220
8221 (defun gnus-summary-tick-above (&optional score)
8222   "Tick all articles with score above SCORE."
8223   (interactive "P")
8224   (gnus-summary-mark-above score gnus-ticked-mark))
8225
8226 (defun gnus-summary-mark-above (score mark)
8227   "Mark articles with score over SCORE with MARK."
8228   (interactive "P\ncMark: ")
8229   (setq score (if score
8230                   (prefix-numeric-value score)
8231                 (or gnus-summary-default-score 0)))
8232   (save-excursion
8233     (set-buffer gnus-summary-buffer)
8234     (goto-char (point-min))
8235     (while (and (progn
8236                   (when (> (gnus-summary-article-score) score)
8237                     (gnus-summary-mark-article nil mark))
8238                   t)
8239                 (gnus-summary-find-next)))))
8240
8241 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8242 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8243 (defun gnus-summary-limit-include-expunged (&optional no-error)
8244   "Display all the hidden articles that were expunged for low scores."
8245   (interactive)
8246   (let ((buffer-read-only nil))
8247     (let ((scored gnus-newsgroup-scored)
8248           headers h)
8249       (while scored
8250         (unless (gnus-summary-goto-subject (caar scored))
8251           (and (setq h (gnus-summary-article-header (caar scored)))
8252                (< (cdar scored) gnus-summary-expunge-below)
8253                (push h headers)))
8254         (setq scored (cdr scored)))
8255       (if (not headers)
8256           (when (not no-error)
8257             (error "No expunged articles hidden"))
8258         (goto-char (point-min))
8259         (gnus-summary-prepare-unthreaded (nreverse headers))
8260         (goto-char (point-min))
8261         (gnus-summary-position-point)
8262         t))))
8263
8264 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8265   "Mark all unread articles in this newsgroup as read.
8266 If prefix argument ALL is non-nil, ticked and dormant articles will
8267 also be marked as read.
8268 If QUIETLY is non-nil, no questions will be asked.
8269 If TO-HERE is non-nil, it should be a point in the buffer.  All
8270 articles before this point will be marked as read.
8271 Note that this function will only catch up the unread article
8272 in the current summary buffer limitation.
8273 The number of articles marked as read is returned."
8274   (interactive "P")
8275   (prog1
8276       (save-excursion
8277         (when (or quietly
8278                   (not gnus-interactive-catchup) ;Without confirmation?
8279                   gnus-expert-user
8280                   (gnus-y-or-n-p
8281                    (if all
8282                        "Mark absolutely all articles as read? "
8283                      "Mark all unread articles as read? ")))
8284           (if (and not-mark
8285                    (not gnus-newsgroup-adaptive)
8286                    (not gnus-newsgroup-auto-expire)
8287                    (not gnus-suppress-duplicates)
8288                    (or (not gnus-use-cache)
8289                        (eq gnus-use-cache 'passive)))
8290               (progn
8291                 (when all
8292                   (setq gnus-newsgroup-marked nil
8293                         gnus-newsgroup-dormant nil))
8294                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8295             ;; We actually mark all articles as canceled, which we
8296             ;; have to do when using auto-expiry or adaptive scoring.
8297             (gnus-summary-show-all-threads)
8298             (when (gnus-summary-first-subject (not all) t)
8299               (while (and
8300                       (if to-here (< (point) to-here) t)
8301                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8302                       (gnus-summary-find-next (not all) nil nil t))))
8303             (gnus-set-mode-line 'summary))
8304           t))
8305     (gnus-summary-position-point)))
8306
8307 (defun gnus-summary-catchup-to-here (&optional all)
8308   "Mark all unticked articles before the current one as read.
8309 If ALL is non-nil, also mark ticked and dormant articles as read."
8310   (interactive "P")
8311   (save-excursion
8312     (gnus-save-hidden-threads
8313       (let ((beg (point)))
8314         ;; We check that there are unread articles.
8315         (when (or all (gnus-summary-find-prev))
8316           (gnus-summary-catchup all t beg)))))
8317   (gnus-summary-position-point))
8318
8319 (defun gnus-summary-catchup-all (&optional quietly)
8320   "Mark all articles in this newsgroup as read."
8321   (interactive "P")
8322   (gnus-summary-catchup t quietly))
8323
8324 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8325   "Mark all articles not marked as unread in this newsgroup as read, then exit.
8326 If prefix argument ALL is non-nil, all articles are marked as read."
8327   (interactive "P")
8328   (when (gnus-summary-catchup all quietly nil 'fast)
8329     ;; Select next newsgroup or exit.
8330     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8331              (eq gnus-auto-select-next 'quietly))
8332         (gnus-summary-next-group nil)
8333       (gnus-summary-exit))))
8334
8335 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8336   "Mark all articles in this newsgroup as read, and then exit."
8337   (interactive "P")
8338   (gnus-summary-catchup-and-exit t quietly))
8339
8340 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8341 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8342   "Mark all articles in this group as read and select the next group.
8343 If given a prefix, mark all articles, unread as well as ticked, as
8344 read."
8345   (interactive "P")
8346   (save-excursion
8347     (gnus-summary-catchup all))
8348   (gnus-summary-next-article t nil nil t))
8349
8350 ;; Thread-based commands.
8351
8352 (defun gnus-summary-articles-in-thread (&optional article)
8353   "Return a list of all articles in the current thread.
8354 If ARTICLE is non-nil, return all articles in the thread that starts
8355 with that article."
8356   (let* ((article (or article (gnus-summary-article-number)))
8357          (data (gnus-data-find-list article))
8358          (top-level (gnus-data-level (car data)))
8359          (top-subject
8360           (cond ((null gnus-thread-operation-ignore-subject)
8361                  (gnus-simplify-subject-re
8362                   (mail-header-subject (gnus-data-header (car data)))))
8363                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8364                  (gnus-simplify-subject-fuzzy
8365                   (mail-header-subject (gnus-data-header (car data)))))
8366                 (t nil)))
8367          (end-point (save-excursion
8368                       (if (gnus-summary-go-to-next-thread)
8369                           (point) (point-max))))
8370          articles)
8371     (while (and data
8372                 (< (gnus-data-pos (car data)) end-point))
8373       (when (or (not top-subject)
8374                 (string= top-subject
8375                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8376                              (gnus-simplify-subject-fuzzy
8377                               (mail-header-subject
8378                                (gnus-data-header (car data))))
8379                            (gnus-simplify-subject-re
8380                             (mail-header-subject
8381                              (gnus-data-header (car data)))))))
8382         (push (gnus-data-number (car data)) articles))
8383       (unless (and (setq data (cdr data))
8384                    (> (gnus-data-level (car data)) top-level))
8385         (setq data nil)))
8386     ;; Return the list of articles.
8387     (nreverse articles)))
8388
8389 (defun gnus-summary-rethread-current ()
8390   "Rethread the thread the current article is part of."
8391   (interactive)
8392   (let* ((gnus-show-threads t)
8393          (article (gnus-summary-article-number))
8394          (id (mail-header-id (gnus-summary-article-header)))
8395          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8396     (unless id
8397       (error "No article on the current line"))
8398     (gnus-rebuild-thread id)
8399     (gnus-summary-goto-subject article)))
8400
8401 (defun gnus-summary-reparent-thread ()
8402   "Make the current article child of the marked (or previous) article.
8403
8404 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8405 is non-nil or the Subject: of both articles are the same."
8406   (interactive)
8407   (unless (not (gnus-group-read-only-p))
8408     (error "The current newsgroup does not support article editing"))
8409   (unless (<= (length gnus-newsgroup-processable) 1)
8410     (error "No more than one article may be marked"))
8411   (save-window-excursion
8412     (let ((gnus-article-buffer " *reparent*")
8413           (current-article (gnus-summary-article-number))
8414           ;; First grab the marked article, otherwise one line up.
8415           (parent-article (if (not (null gnus-newsgroup-processable))
8416                               (car gnus-newsgroup-processable)
8417                             (save-excursion
8418                               (if (eq (forward-line -1) 0)
8419                                   (gnus-summary-article-number)
8420                                 (error "Beginning of summary buffer"))))))
8421       (unless (not (eq current-article parent-article))
8422         (error "An article may not be self-referential"))
8423       (let ((message-id (mail-header-id
8424                          (gnus-summary-article-header parent-article))))
8425         (unless (and message-id (not (equal message-id "")))
8426           (error "No message-id in desired parent"))
8427         (gnus-with-article current-article
8428           (goto-char (point-min))
8429           (if (re-search-forward "^References: " nil t)
8430               (progn
8431                 (re-search-forward "^[^ \t]" nil t)
8432                 (forward-line -1)
8433                 (end-of-line)
8434                 (insert " " message-id))
8435             (insert "References: " message-id "\n")))
8436         (set-buffer gnus-summary-buffer)
8437         (gnus-summary-unmark-all-processable)
8438         (gnus-summary-update-article current-article)
8439         (gnus-summary-rethread-current)
8440         (gnus-message 3 "Article %d is now the child of article %d"
8441                       current-article parent-article)))))
8442
8443 (defun gnus-summary-toggle-threads (&optional arg)
8444   "Toggle showing conversation threads.
8445 If ARG is positive number, turn showing conversation threads on."
8446   (interactive "P")
8447   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8448     (setq gnus-show-threads
8449           (if (null arg) (not gnus-show-threads)
8450             (> (prefix-numeric-value arg) 0)))
8451     (gnus-summary-prepare)
8452     (gnus-summary-goto-subject current)
8453     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8454     (gnus-summary-position-point)))
8455
8456 (defun gnus-summary-show-all-threads ()
8457   "Show all threads."
8458   (interactive)
8459   (save-excursion
8460     (let ((buffer-read-only nil))
8461       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8462   (gnus-summary-position-point))
8463
8464 (defun gnus-summary-show-thread ()
8465   "Show thread subtrees.
8466 Returns nil if no thread was there to be shown."
8467   (interactive)
8468   (let ((buffer-read-only nil)
8469         (orig (point))
8470         ;; first goto end then to beg, to have point at beg after let
8471         (end (progn (end-of-line) (point)))
8472         (beg (progn (beginning-of-line) (point))))
8473     (prog1
8474         ;; Any hidden lines here?
8475         (search-forward "\r" end t)
8476       (subst-char-in-region beg end ?\^M ?\n t)
8477       (goto-char orig)
8478       (gnus-summary-position-point))))
8479
8480 (defun gnus-summary-hide-all-threads ()
8481   "Hide all thread subtrees."
8482   (interactive)
8483   (save-excursion
8484     (goto-char (point-min))
8485     (gnus-summary-hide-thread)
8486     (while (zerop (gnus-summary-next-thread 1 t))
8487       (gnus-summary-hide-thread)))
8488   (gnus-summary-position-point))
8489
8490 (defun gnus-summary-hide-thread ()
8491   "Hide thread subtrees.
8492 Returns nil if no threads were there to be hidden."
8493   (interactive)
8494   (let ((buffer-read-only nil)
8495         (start (point))
8496         (article (gnus-summary-article-number)))
8497     (goto-char start)
8498     ;; Go forward until either the buffer ends or the subthread
8499     ;; ends.
8500     (when (and (not (eobp))
8501                (or (zerop (gnus-summary-next-thread 1 t))
8502                    (goto-char (point-max))))
8503       (prog1
8504           (if (and (> (point) start)
8505                    (search-backward "\n" start t))
8506               (progn
8507                 (subst-char-in-region start (point) ?\n ?\^M)
8508                 (gnus-summary-goto-subject article))
8509             (goto-char start)
8510             nil)
8511         ;;(gnus-summary-position-point)
8512         ))))
8513
8514 (defun gnus-summary-go-to-next-thread (&optional previous)
8515   "Go to the same level (or less) next thread.
8516 If PREVIOUS is non-nil, go to previous thread instead.
8517 Return the article number moved to, or nil if moving was impossible."
8518   (let ((level (gnus-summary-thread-level))
8519         (way (if previous -1 1))
8520         (beg (point)))
8521     (forward-line way)
8522     (while (and (not (eobp))
8523                 (< level (gnus-summary-thread-level)))
8524       (forward-line way))
8525     (if (eobp)
8526         (progn
8527           (goto-char beg)
8528           nil)
8529       (setq beg (point))
8530       (prog1
8531           (gnus-summary-article-number)
8532         (goto-char beg)))))
8533
8534 (defun gnus-summary-next-thread (n &optional silent)
8535   "Go to the same level next N'th thread.
8536 If N is negative, search backward instead.
8537 Returns the difference between N and the number of skips actually
8538 done.
8539
8540 If SILENT, don't output messages."
8541   (interactive "p")
8542   (let ((backward (< n 0))
8543         (n (abs n)))
8544     (while (and (> n 0)
8545                 (gnus-summary-go-to-next-thread backward))
8546       (decf n))
8547     (unless silent
8548       (gnus-summary-position-point))
8549     (when (and (not silent) (/= 0 n))
8550       (gnus-message 7 "No more threads"))
8551     n))
8552
8553 (defun gnus-summary-prev-thread (n)
8554   "Go to the same level previous N'th thread.
8555 Returns the difference between N and the number of skips actually
8556 done."
8557   (interactive "p")
8558   (gnus-summary-next-thread (- n)))
8559
8560 (defun gnus-summary-go-down-thread ()
8561   "Go down one level in the current thread."
8562   (let ((children (gnus-summary-article-children)))
8563     (when children
8564       (gnus-summary-goto-subject (car children)))))
8565
8566 (defun gnus-summary-go-up-thread ()
8567   "Go up one level in the current thread."
8568   (let ((parent (gnus-summary-article-parent)))
8569     (when parent
8570       (gnus-summary-goto-subject parent))))
8571
8572 (defun gnus-summary-down-thread (n)
8573   "Go down thread N steps.
8574 If N is negative, go up instead.
8575 Returns the difference between N and how many steps down that were
8576 taken."
8577   (interactive "p")
8578   (let ((up (< n 0))
8579         (n (abs n)))
8580     (while (and (> n 0)
8581                 (if up (gnus-summary-go-up-thread)
8582                   (gnus-summary-go-down-thread)))
8583       (setq n (1- n)))
8584     (gnus-summary-position-point)
8585     (when (/= 0 n)
8586       (gnus-message 7 "Can't go further"))
8587     n))
8588
8589 (defun gnus-summary-up-thread (n)
8590   "Go up thread N steps.
8591 If N is negative, go up instead.
8592 Returns the difference between N and how many steps down that were
8593 taken."
8594   (interactive "p")
8595   (gnus-summary-down-thread (- n)))
8596
8597 (defun gnus-summary-top-thread ()
8598   "Go to the top of the thread."
8599   (interactive)
8600   (while (gnus-summary-go-up-thread))
8601   (gnus-summary-article-number))
8602
8603 (defun gnus-summary-kill-thread (&optional unmark)
8604   "Mark articles under current thread as read.
8605 If the prefix argument is positive, remove any kinds of marks.
8606 If the prefix argument is negative, tick articles instead."
8607   (interactive "P")
8608   (when unmark
8609     (setq unmark (prefix-numeric-value unmark)))
8610   (let ((articles (gnus-summary-articles-in-thread)))
8611     (save-excursion
8612       ;; Expand the thread.
8613       (gnus-summary-show-thread)
8614       ;; Mark all the articles.
8615       (while articles
8616         (gnus-summary-goto-subject (car articles))
8617         (cond ((null unmark)
8618                (gnus-summary-mark-article-as-read gnus-killed-mark))
8619               ((> unmark 0)
8620                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8621               (t
8622                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8623         (setq articles (cdr articles))))
8624     ;; Hide killed subtrees.
8625     (and (null unmark)
8626          gnus-thread-hide-killed
8627          (gnus-summary-hide-thread))
8628     ;; If marked as read, go to next unread subject.
8629     (when (null unmark)
8630       ;; Go to next unread subject.
8631       (gnus-summary-next-subject 1 t)))
8632   (gnus-set-mode-line 'summary))
8633
8634 ;; Summary sorting commands
8635
8636 (defun gnus-summary-sort-by-number (&optional reverse)
8637   "Sort the summary buffer by article number.
8638 Argument REVERSE means reverse order."
8639   (interactive "P")
8640   (gnus-summary-sort 'number reverse))
8641
8642 (defun gnus-summary-sort-by-author (&optional reverse)
8643   "Sort the summary buffer by author name alphabetically.
8644 If case-fold-search is non-nil, case of letters is ignored.
8645 Argument REVERSE means reverse order."
8646   (interactive "P")
8647   (gnus-summary-sort 'author reverse))
8648
8649 (defun gnus-summary-sort-by-subject (&optional reverse)
8650   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8651 If case-fold-search is non-nil, case of letters is ignored.
8652 Argument REVERSE means reverse order."
8653   (interactive "P")
8654   (gnus-summary-sort 'subject reverse))
8655
8656 (defun gnus-summary-sort-by-date (&optional reverse)
8657   "Sort the summary buffer by date.
8658 Argument REVERSE means reverse order."
8659   (interactive "P")
8660   (gnus-summary-sort 'date reverse))
8661
8662 (defun gnus-summary-sort-by-score (&optional reverse)
8663   "Sort the summary buffer by score.
8664 Argument REVERSE means reverse order."
8665   (interactive "P")
8666   (gnus-summary-sort 'score reverse))
8667
8668 (defun gnus-summary-sort-by-lines (&optional reverse)
8669   "Sort the summary buffer by the number of lines.
8670 Argument REVERSE means reverse order."
8671   (interactive "P")
8672   (gnus-summary-sort 'lines reverse))
8673
8674 (defun gnus-summary-sort-by-chars (&optional reverse)
8675   "Sort the summary buffer by article length.
8676 Argument REVERSE means reverse order."
8677   (interactive "P")
8678   (gnus-summary-sort 'chars reverse))   
8679
8680 (defun gnus-summary-sort (predicate reverse)
8681   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8682   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8683          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8684          (gnus-thread-sort-functions
8685           (if (not reverse)
8686               thread
8687             `(lambda (t1 t2)
8688                (,thread t2 t1))))
8689          (gnus-article-sort-functions
8690           (if (not reverse)
8691               article
8692             `(lambda (t1 t2)
8693                (,article t2 t1))))
8694          (buffer-read-only)
8695          (gnus-summary-prepare-hook nil))
8696     ;; We do the sorting by regenerating the threads.
8697     (gnus-summary-prepare)
8698     ;; Hide subthreads if needed.
8699     (when (and gnus-show-threads gnus-thread-hide-subtree)
8700       (gnus-summary-hide-all-threads))))
8701
8702 ;; Summary saving commands.
8703
8704 (defun gnus-summary-save-article (&optional n not-saved)
8705   "Save the current article using the default saver function.
8706 If N is a positive number, save the N next articles.
8707 If N is a negative number, save the N previous articles.
8708 If N is nil and any articles have been marked with the process mark,
8709 save those articles instead.
8710 The variable `gnus-default-article-saver' specifies the saver function."
8711   (interactive "P")
8712   (let* ((articles (gnus-summary-work-articles n))
8713          (save-buffer (save-excursion
8714                         (nnheader-set-temp-buffer " *Gnus Save*")))
8715          (num (length articles))
8716          header file)
8717     (dolist (article articles)
8718       (setq header (gnus-summary-article-header article))
8719       (if (not (vectorp header))
8720           ;; This is a pseudo-article.
8721           (if (assq 'name header)
8722               (gnus-copy-file (cdr (assq 'name header)))
8723             (gnus-message 1 "Article %d is unsaveable" article))
8724         ;; This is a real article.
8725         (save-window-excursion
8726           (gnus-summary-select-article t nil nil article))
8727         (save-excursion
8728           (set-buffer save-buffer)
8729           (erase-buffer)
8730           (insert-buffer-substring gnus-original-article-buffer))
8731         (setq file (gnus-article-save save-buffer file num))
8732         (gnus-summary-remove-process-mark article)
8733         (unless not-saved
8734           (gnus-summary-set-saved-mark article))))
8735     (gnus-kill-buffer save-buffer)
8736     (gnus-summary-position-point)
8737     (gnus-set-mode-line 'summary)
8738     n))
8739
8740 (defun gnus-summary-pipe-output (&optional arg)
8741   "Pipe the current article to a subprocess.
8742 If N is a positive number, pipe the N next articles.
8743 If N is a negative number, pipe the N previous articles.
8744 If N is nil and any articles have been marked with the process mark,
8745 pipe those articles instead."
8746   (interactive "P")
8747   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8748     (gnus-summary-save-article arg t))
8749   (gnus-configure-windows 'pipe))
8750
8751 (defun gnus-summary-save-article-mail (&optional arg)
8752   "Append the current article to an mail file.
8753 If N is a positive number, save the N next articles.
8754 If N is a negative number, save the N previous articles.
8755 If N is nil and any articles have been marked with the process mark,
8756 save those articles instead."
8757   (interactive "P")
8758   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8759     (gnus-summary-save-article arg)))
8760
8761 (defun gnus-summary-save-article-rmail (&optional arg)
8762   "Append the current article to an rmail file.
8763 If N is a positive number, save the N next articles.
8764 If N is a negative number, save the N previous articles.
8765 If N is nil and any articles have been marked with the process mark,
8766 save those articles instead."
8767   (interactive "P")
8768   (let ((gnus-default-article-saver 'rmail-output-to-rmail-file))
8769     (gnus-summary-save-article arg)))
8770
8771 (defun gnus-summary-save-article-file (&optional arg)
8772   "Append the current article to a file.
8773 If N is a positive number, save the N next articles.
8774 If N is a negative number, save the N previous articles.
8775 If N is nil and any articles have been marked with the process mark,
8776 save those articles instead."
8777   (interactive "P")
8778   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8779     (gnus-summary-save-article arg)))
8780
8781 (defun gnus-summary-write-article-file (&optional arg)
8782   "Write the current article to a file, deleting the previous file.
8783 If N is a positive number, save the N next articles.
8784 If N is a negative number, save the N previous articles.
8785 If N is nil and any articles have been marked with the process mark,
8786 save those articles instead."
8787   (interactive "P")
8788   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8789     (gnus-summary-save-article arg)))
8790
8791 (defun gnus-summary-save-article-body-file (&optional arg)
8792   "Append the current article body to a file.
8793 If N is a positive number, save the N next articles.
8794 If N is a negative number, save the N previous articles.
8795 If N is nil and any articles have been marked with the process mark,
8796 save those articles instead."
8797   (interactive "P")
8798   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8799     (gnus-summary-save-article arg)))
8800
8801 (defun gnus-summary-pipe-message (program)
8802   "Pipe the current article through PROGRAM."
8803   (interactive "sProgram: ")
8804   (gnus-summary-select-article)
8805   (let ((mail-header-separator ""))
8806     (gnus-eval-in-buffer-window gnus-article-buffer
8807       (save-restriction
8808         (widen)
8809         (let ((start (window-start))
8810               buffer-read-only)
8811           (message-pipe-buffer-body program)
8812           (set-window-start (get-buffer-window (current-buffer)) start))))))
8813
8814 (defun gnus-get-split-value (methods)
8815   "Return a value based on the split METHODS."
8816   (let (split-name method result match)
8817     (when methods
8818       (save-excursion
8819         (set-buffer gnus-original-article-buffer)
8820         (save-restriction
8821           (nnheader-narrow-to-headers)
8822           (while methods
8823             (goto-char (point-min))
8824             (setq method (pop methods))
8825             (setq match (car method))
8826             (when (cond
8827                    ((stringp match)
8828                     ;; Regular expression.
8829                     (ignore-errors
8830                       (re-search-forward match nil t)))
8831                    ((gnus-functionp match)
8832                     ;; Function.
8833                     (save-restriction
8834                       (widen)
8835                       (setq result (funcall match gnus-newsgroup-name))))
8836                    ((consp match)
8837                     ;; Form.
8838                     (save-restriction
8839                       (widen)
8840                       (setq result (eval match)))))
8841               (setq split-name (append (cdr method) split-name))
8842               (cond ((stringp result)
8843                      (push (expand-file-name
8844                             result gnus-article-save-directory)
8845                            split-name))
8846                     ((consp result)
8847                      (setq split-name (append result split-name)))))))))
8848     (nreverse split-name)))
8849
8850 (defun gnus-valid-move-group-p (group)
8851   (and (boundp group)
8852        (symbol-name group)
8853        (symbol-value group)
8854        (gnus-get-function (gnus-find-method-for-group
8855                            (symbol-name group)) 'request-accept-article t)))
8856
8857 (defun gnus-read-move-group-name (prompt default articles prefix)
8858   "Read a group name."
8859   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8860          (minibuffer-confirm-incomplete nil) ; XEmacs
8861          (prom
8862           (format "%s %s to:"
8863                   prompt
8864                   (if (> (length articles) 1)
8865                       (format "these %d articles" (length articles))
8866                     "this article")))
8867          (to-newsgroup
8868           (cond
8869            ((null split-name)
8870             (gnus-completing-read default prom
8871                                   gnus-active-hashtb
8872                                   'gnus-valid-move-group-p
8873                                   nil prefix
8874                                   'gnus-group-history))
8875            ((= 1 (length split-name))
8876             (gnus-completing-read (car split-name) prom
8877                                   gnus-active-hashtb
8878                                   'gnus-valid-move-group-p
8879                                   nil nil
8880                                   'gnus-group-history))
8881            (t
8882             (gnus-completing-read nil prom
8883                                   (mapcar (lambda (el) (list el))
8884                                           (nreverse split-name))
8885                                   nil nil nil
8886                                   'gnus-group-history)))))
8887     (when to-newsgroup
8888       (if (or (string= to-newsgroup "")
8889               (string= to-newsgroup prefix))
8890           (setq to-newsgroup default))
8891       (unless to-newsgroup
8892         (error "No group name entered"))
8893       (or (gnus-active to-newsgroup)
8894           (gnus-activate-group to-newsgroup)
8895           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
8896                                      to-newsgroup))
8897               (or (and (gnus-request-create-group
8898                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
8899                        (gnus-activate-group to-newsgroup nil nil
8900                                             (gnus-group-name-to-method
8901                                              to-newsgroup)))
8902                   (error "Couldn't create group %s" to-newsgroup)))
8903           (error "No such group: %s" to-newsgroup)))
8904     to-newsgroup))
8905
8906 (defun gnus-summary-save-parts (type dir n reverse)
8907   "Save parts matching TYPE to DIR.
8908 If REVERSE, save parts that do not match TYPE."
8909   (interactive
8910    (list (read-string "Save parts of type: " "image/.*")
8911          (read-file-name "Save to directory: " t nil t)
8912          current-prefix-arg))
8913   (gnus-summary-iterate n
8914     (let ((gnus-display-mime-function nil)
8915           (gnus-inhibit-treatment t))
8916       (gnus-summary-select-article))
8917     (save-excursion
8918       (set-buffer gnus-article-buffer)
8919       (let ((handles (or (mm-dissect-buffer) (mm-uu-dissect))))
8920         (when handles
8921           (gnus-summary-save-parts-1 type dir handles reverse)
8922           (mm-destroy-parts handles))))))
8923
8924 (defun gnus-summary-save-parts-1 (type dir handle reverse)
8925   (if (stringp (car handle))
8926       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
8927               (cdr handle))
8928     (when (if reverse
8929               (not (string-match type (car (mm-handle-type handle))))
8930             (string-match type (car (mm-handle-type handle))))
8931       (let ((file (expand-file-name
8932                    (file-name-nondirectory
8933                     (or
8934                      (mail-content-type-get
8935                       (mm-handle-disposition handle) 'filename)
8936                      (concat gnus-newsgroup-name "." gnus-current-article)))
8937                    dir)))
8938         (unless (file-exists-p file)
8939           (mm-save-part-to-file handle file))))))
8940
8941 ;; Summary extract commands
8942
8943 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
8944   (let ((buffer-read-only nil)
8945         (article (gnus-summary-article-number))
8946         after-article b e)
8947     (unless (gnus-summary-goto-subject article)
8948       (error "No such article: %d" article))
8949     (gnus-summary-position-point)
8950     ;; If all commands are to be bunched up on one line, we collect
8951     ;; them here.
8952     (unless gnus-view-pseudos-separately
8953       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
8954             files action)
8955         (while ps
8956           (setq action (cdr (assq 'action (car ps))))
8957           (setq files (list (cdr (assq 'name (car ps)))))
8958           (while (and ps (cdr ps)
8959                       (string= (or action "1")
8960                                (or (cdr (assq 'action (cadr ps))) "2")))
8961             (push (cdr (assq 'name (cadr ps))) files)
8962             (setcdr ps (cddr ps)))
8963           (when files
8964             (when (not (string-match "%s" action))
8965               (push " " files))
8966             (push " " files)
8967             (when (assq 'execute (car ps))
8968               (setcdr (assq 'execute (car ps))
8969                       (funcall (if (string-match "%s" action)
8970                                    'format 'concat)
8971                                action
8972                                (mapconcat
8973                                 (lambda (f)
8974                                   (if (equal f " ")
8975                                       f
8976                                     (mm-quote-arg f)))
8977                                 files " ")))))
8978           (setq ps (cdr ps)))))
8979     (if (and gnus-view-pseudos (not not-view))
8980         (while pslist
8981           (when (assq 'execute (car pslist))
8982             (gnus-execute-command (cdr (assq 'execute (car pslist)))
8983                                   (eq gnus-view-pseudos 'not-confirm)))
8984           (setq pslist (cdr pslist)))
8985       (save-excursion
8986         (while pslist
8987           (setq after-article (or (cdr (assq 'article (car pslist)))
8988                                   (gnus-summary-article-number)))
8989           (gnus-summary-goto-subject after-article)
8990           (forward-line 1)
8991           (setq b (point))
8992           (insert "    " (file-name-nondirectory
8993                           (cdr (assq 'name (car pslist))))
8994                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
8995           (setq e (point))
8996           (forward-line -1)             ; back to `b'
8997           (gnus-add-text-properties
8998            b (1- e) (list 'gnus-number gnus-reffed-article-number
8999                           gnus-mouse-face-prop gnus-mouse-face))
9000           (gnus-data-enter
9001            after-article gnus-reffed-article-number
9002            gnus-unread-mark b (car pslist) 0 (- e b))
9003           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9004           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9005           (setq pslist (cdr pslist)))))))
9006
9007 (defun gnus-pseudos< (p1 p2)
9008   (let ((c1 (cdr (assq 'action p1)))
9009         (c2 (cdr (assq 'action p2))))
9010     (and c1 c2 (string< c1 c2))))
9011
9012 (defun gnus-request-pseudo-article (props)
9013   (cond ((assq 'execute props)
9014          (gnus-execute-command (cdr (assq 'execute props)))))
9015   (let ((gnus-current-article (gnus-summary-article-number)))
9016     (gnus-run-hooks 'gnus-mark-article-hook)))
9017
9018 (defun gnus-execute-command (command &optional automatic)
9019   (save-excursion
9020     (gnus-article-setup-buffer)
9021     (set-buffer gnus-article-buffer)
9022     (setq buffer-read-only nil)
9023     (let ((command (if automatic command
9024                      (read-string "Command: " (cons command 0)))))
9025       (erase-buffer)
9026       (insert "$ " command "\n\n")
9027       (if gnus-view-pseudo-asynchronously
9028           (start-process "gnus-execute" (current-buffer) shell-file-name
9029                          shell-command-switch command)
9030         (call-process shell-file-name nil t nil
9031                       shell-command-switch command)))))
9032
9033 ;; Summary kill commands.
9034
9035 (defun gnus-summary-edit-global-kill (article)
9036   "Edit the \"global\" kill file."
9037   (interactive (list (gnus-summary-article-number)))
9038   (gnus-group-edit-global-kill article))
9039
9040 (defun gnus-summary-edit-local-kill ()
9041   "Edit a local kill file applied to the current newsgroup."
9042   (interactive)
9043   (setq gnus-current-headers (gnus-summary-article-header))
9044   (gnus-group-edit-local-kill
9045    (gnus-summary-article-number) gnus-newsgroup-name))
9046
9047 ;;; Header reading.
9048
9049 (defun gnus-read-header (id &optional header)
9050   "Read the headers of article ID and enter them into the Gnus system."
9051   (let ((group gnus-newsgroup-name)
9052         (gnus-override-method
9053          (and (gnus-news-group-p gnus-newsgroup-name)
9054               gnus-refer-article-method))
9055         where)
9056     ;; First we check to see whether the header in question is already
9057     ;; fetched.
9058     (if (stringp id)
9059         ;; This is a Message-ID.
9060         (setq header (or header (gnus-id-to-header id)))
9061       ;; This is an article number.
9062       (setq header (or header (gnus-summary-article-header id))))
9063     (if (and header
9064              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9065         ;; We have found the header.
9066         header
9067       ;; If this is a sparse article, we have to nix out its
9068       ;; previous entry in the thread hashtb.
9069       (when (and header
9070                  (gnus-summary-article-sparse-p (mail-header-number header)))
9071         (let* ((parent (gnus-parent-id (mail-header-references header)))
9072                (thread (and parent (gnus-id-to-thread parent))))
9073           (when thread
9074             (delq (assq header thread) thread))))
9075       ;; We have to really fetch the header to this article.
9076       (save-excursion
9077         (set-buffer nntp-server-buffer)
9078         (when (setq where (gnus-request-head id group))
9079           (nnheader-fold-continuation-lines)
9080           (goto-char (point-max))
9081           (insert ".\n")
9082           (goto-char (point-min))
9083           (insert "211 ")
9084           (princ (cond
9085                   ((numberp id) id)
9086                   ((cdr where) (cdr where))
9087                   (header (mail-header-number header))
9088                   (t gnus-reffed-article-number))
9089                  (current-buffer))
9090           (insert " Article retrieved.\n"))
9091         (if (or (not where)
9092                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9093             ()                          ; Malformed head.
9094           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9095             (when (and (stringp id)
9096                        (not (string= (gnus-group-real-name group)
9097                                      (car where))))
9098               ;; If we fetched by Message-ID and the article came
9099               ;; from a different group, we fudge some bogus article
9100               ;; numbers for this article.
9101               (mail-header-set-number header gnus-reffed-article-number))
9102             (save-excursion
9103               (set-buffer gnus-summary-buffer)
9104               (decf gnus-reffed-article-number)
9105               (gnus-remove-header (mail-header-number header))
9106               (push header gnus-newsgroup-headers)
9107               (setq gnus-current-headers header)
9108               (push (mail-header-number header) gnus-newsgroup-limit)))
9109           header)))))
9110
9111 (defun gnus-remove-header (number)
9112   "Remove header NUMBER from `gnus-newsgroup-headers'."
9113   (if (and gnus-newsgroup-headers
9114            (= number (mail-header-number (car gnus-newsgroup-headers))))
9115       (pop gnus-newsgroup-headers)
9116     (let ((headers gnus-newsgroup-headers))
9117       (while (and (cdr headers)
9118                   (not (= number (mail-header-number (cadr headers)))))
9119         (pop headers))
9120       (when (cdr headers)
9121         (setcdr headers (cddr headers))))))
9122
9123 ;;;
9124 ;;; summary highlights
9125 ;;;
9126
9127 (defun gnus-highlight-selected-summary ()
9128   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9129   ;; Highlight selected article in summary buffer
9130   (when gnus-summary-selected-face
9131     (save-excursion
9132       (let* ((beg (progn (beginning-of-line) (point)))
9133              (end (progn (end-of-line) (point)))
9134              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9135              (from (if (get-text-property beg gnus-mouse-face-prop)
9136                        beg
9137                      (or (next-single-property-change
9138                           beg gnus-mouse-face-prop nil end)
9139                          beg)))
9140              (to
9141               (if (= from end)
9142                   (- from 2)
9143                 (or (next-single-property-change
9144                      from gnus-mouse-face-prop nil end)
9145                     end))))
9146         ;; If no mouse-face prop on line we will have to = from = end,
9147         ;; so we highlight the entire line instead.
9148         (when (= (+ to 2) from)
9149           (setq from beg)
9150           (setq to end))
9151         (if gnus-newsgroup-selected-overlay
9152             ;; Move old overlay.
9153             (gnus-move-overlay
9154              gnus-newsgroup-selected-overlay from to (current-buffer))
9155           ;; Create new overlay.
9156           (gnus-overlay-put
9157            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9158            'face gnus-summary-selected-face))))))
9159
9160 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9161 (defun gnus-summary-highlight-line ()
9162   "Highlight current line according to `gnus-summary-highlight'."
9163   (let* ((list gnus-summary-highlight)
9164          (p (point))
9165          (end (progn (end-of-line) (point)))
9166          ;; now find out where the line starts and leave point there.
9167          (beg (progn (beginning-of-line) (point)))
9168          (article (gnus-summary-article-number))
9169          (score (or (cdr (assq (or article gnus-current-article)
9170                                gnus-newsgroup-scored))
9171                     gnus-summary-default-score 0))
9172          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9173          (inhibit-read-only t))
9174     ;; Eval the cars of the lists until we find a match.
9175     (let ((default gnus-summary-default-score))
9176       (while (and list
9177                   (not (eval (caar list))))
9178         (setq list (cdr list))))
9179     (let ((face (cdar list)))
9180       (unless (eq face (get-text-property beg 'face))
9181         (gnus-put-text-property-excluding-characters-with-faces
9182          beg end 'face
9183          (setq face (if (boundp face) (symbol-value face) face)))
9184         (when gnus-summary-highlight-line-function
9185           (funcall gnus-summary-highlight-line-function article face))))
9186     (goto-char p)))
9187
9188 (defun gnus-update-read-articles (group unread &optional compute)
9189   "Update the list of read articles in GROUP."
9190   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9191          (entry (gnus-gethash group gnus-newsrc-hashtb))
9192          (info (nth 2 entry))
9193          (prev 1)
9194          (unread (sort (copy-sequence unread) '<))
9195          read)
9196     (if (or (not info) (not active))
9197         ;; There is no info on this group if it was, in fact,
9198         ;; killed.  Gnus stores no information on killed groups, so
9199         ;; there's nothing to be done.
9200         ;; One could store the information somewhere temporarily,
9201         ;; perhaps...  Hmmm...
9202         ()
9203       ;; Remove any negative articles numbers.
9204       (while (and unread (< (car unread) 0))
9205         (setq unread (cdr unread)))
9206       ;; Remove any expired article numbers
9207       (while (and unread (< (car unread) (car active)))
9208         (setq unread (cdr unread)))
9209       ;; Compute the ranges of read articles by looking at the list of
9210       ;; unread articles.
9211       (while unread
9212         (when (/= (car unread) prev)
9213           (push (if (= prev (1- (car unread))) prev
9214                   (cons prev (1- (car unread))))
9215                 read))
9216         (setq prev (1+ (car unread)))
9217         (setq unread (cdr unread)))
9218       (when (<= prev (cdr active))
9219         (push (cons prev (cdr active)) read))
9220       (setq read (if (> (length read) 1) (nreverse read) read))
9221       (if compute
9222           read
9223         (save-excursion
9224           (set-buffer gnus-group-buffer)
9225           (gnus-undo-register
9226             `(progn
9227                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9228                (gnus-info-set-read ',info ',(gnus-info-read info))
9229                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9230                (gnus-group-update-group ,group t))))
9231        ;; Propagate the read marks to the backend.
9232        (if (gnus-check-backend-function 'request-set-mark group)
9233            (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9234                  (add (gnus-remove-from-range read (gnus-info-read info))))
9235              (when (or add del)
9236                (unless (gnus-check-group group)
9237                  (error "Can't open server for %s" group))
9238                (gnus-request-set-mark
9239                 group (delq nil (list (if add (list add 'add '(read)))
9240                                       (if del (list del 'del '(read)))))))))
9241         ;; Enter this list into the group info.
9242         (gnus-info-set-read info read)
9243         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9244         (gnus-get-unread-articles-in-group info (gnus-active group))
9245         t))))
9246
9247 (defun gnus-offer-save-summaries ()
9248   "Offer to save all active summary buffers."
9249   (save-excursion
9250     (let ((buflist (buffer-list))
9251           buffers bufname)
9252       ;; Go through all buffers and find all summaries.
9253       (while buflist
9254         (and (setq bufname (buffer-name (car buflist)))
9255              (string-match "Summary" bufname)
9256              (save-excursion
9257                (set-buffer bufname)
9258                ;; We check that this is, indeed, a summary buffer.
9259                (and (eq major-mode 'gnus-summary-mode)
9260                     ;; Also make sure this isn't bogus.
9261                     gnus-newsgroup-prepared
9262                     ;; Also make sure that this isn't a dead summary buffer.
9263                     (not gnus-dead-summary-mode)))
9264              (push bufname buffers))
9265         (setq buflist (cdr buflist)))
9266       ;; Go through all these summary buffers and offer to save them.
9267       (when buffers
9268         (map-y-or-n-p
9269          "Update summary buffer %s? "
9270          (lambda (buf)
9271            (switch-to-buffer buf)
9272            (gnus-summary-exit))
9273          buffers)))))
9274
9275 (defun gnus-summary-setup-default-charset ()
9276   "Setup newsgroup default charset."
9277   (let* ((name (and gnus-newsgroup-name
9278                    (gnus-group-real-name gnus-newsgroup-name)))
9279          (ignored-charsets 
9280           (append
9281            (and gnus-newsgroup-name
9282                 (or (gnus-group-find-parameter gnus-newsgroup-name
9283                                                'ignored-charsets t)
9284                     (let ((alist gnus-group-ignored-charsets-alist)
9285                           elem (charsets nil))
9286                       (while (setq elem (pop alist))
9287                         (when (and name
9288                                    (string-match (car elem) name))
9289                          (setq alist nil
9290                                charsets (cdr elem))))
9291                       charsets)))
9292            gnus-newsgroup-ignored-charsets)))
9293     (setq gnus-newsgroup-charset
9294           (or (and gnus-newsgroup-name
9295                    (or (gnus-group-find-parameter gnus-newsgroup-name
9296                                                   'charset)
9297                        (let ((alist gnus-group-charset-alist)
9298                              elem (charset nil))
9299                          (while (setq elem (pop alist))
9300                            (when (and name
9301                                       (string-match (car elem) name))
9302                              (setq alist nil
9303                                    charset (cadr elem))))
9304                          charset)))
9305               gnus-default-charset))
9306     (set (make-local-variable 'gnus-newsgroup-ignored-charsets) 
9307          ignored-charsets)))
9308
9309 ;;;
9310 ;;; Mime Commands
9311 ;;;
9312
9313 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9314   "Display the current article buffer fully MIME-buttonized.
9315 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9316 treated as multipart/mixed."
9317   (interactive "P")
9318   (require 'gnus-art)
9319   (let ((gnus-unbuttonized-mime-types nil)
9320         (gnus-mime-display-multipart-as-mixed show-all-parts))
9321     (gnus-summary-show-article)))
9322
9323 (defun gnus-summary-repair-multipart (article)
9324   "Add a Content-Type header to a multipart article without one."
9325   (interactive (list (gnus-summary-article-number)))
9326   (gnus-with-article article
9327     (message-narrow-to-head)
9328     (goto-char (point-max))
9329     (widen)
9330     (when (search-forward "\n--" nil t)
9331       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9332         (message-narrow-to-head)
9333         (message-remove-header "Mime-Version")
9334         (message-remove-header "Content-Type")
9335         (goto-char (point-max))
9336         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9337                         separator))
9338         (insert "Mime-Version: 1.0\n")
9339         (widen))))
9340   (let (gnus-mark-article-hook)
9341     (gnus-summary-select-article t t nil article)))
9342
9343 (defun gnus-summary-toggle-display-buttonized ()
9344   "Toggle the buttonizing of the article buffer."
9345   (interactive)
9346   (require 'gnus-art)
9347   (if (setq gnus-inhibit-mime-unbuttonizing
9348             (not gnus-inhibit-mime-unbuttonizing))
9349       (let ((gnus-unbuttonized-mime-types nil))
9350         (gnus-summary-show-article))
9351     (gnus-summary-show-article)))
9352
9353 ;;;
9354 ;;; with article
9355 ;;;
9356
9357 (defmacro gnus-with-article (article &rest forms)
9358   "Select ARTICLE and perform FORMS in the original article buffer.
9359 Then replace the article with the result."
9360   `(progn
9361      ;; We don't want the article to be marked as read.
9362      (let (gnus-mark-article-hook)
9363        (gnus-summary-select-article t t nil ,article))
9364      (set-buffer gnus-original-article-buffer)
9365      ,@forms
9366      (if (not (gnus-check-backend-function
9367                'request-replace-article (car gnus-article-current)))
9368          (gnus-message 5 "Read-only group; not replacing")
9369        (unless (gnus-request-replace-article
9370                 ,article (car gnus-article-current)
9371                 (current-buffer) t)
9372          (error "Couldn't replace article")))
9373      ;; The cache and backlog have to be flushed somewhat.
9374      (when gnus-keep-backlog
9375        (gnus-backlog-remove-article
9376         (car gnus-article-current) (cdr gnus-article-current)))
9377      (when gnus-use-cache
9378        (gnus-cache-update-article
9379         (car gnus-article-current) (cdr gnus-article-current)))))
9380
9381 (put 'gnus-with-article 'lisp-indent-function 1)
9382 (put 'gnus-with-article 'edebug-form-spec '(form body))
9383
9384 ;;;
9385 ;;; Generic summary marking commands
9386 ;;;
9387
9388 (defvar gnus-summary-marking-alist
9389   '((read gnus-del-mark "d")
9390     (unread gnus-unread-mark "u")
9391     (ticked gnus-ticked-mark "!")
9392     (dormant gnus-dormant-mark "?")
9393     (expirable gnus-expirable-mark "e"))
9394   "An alist of names/marks/keystrokes.")
9395
9396 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9397 (defvar gnus-summary-mark-map)
9398
9399 (defun gnus-summary-make-all-marking-commands ()
9400   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9401   (dolist (elem gnus-summary-marking-alist)
9402     (apply 'gnus-summary-make-marking-command elem)))
9403
9404 (defun gnus-summary-make-marking-command (name mark keystroke)
9405   (let ((map (make-sparse-keymap)))
9406     (define-key gnus-summary-generic-mark-map keystroke map)
9407     (dolist (lway `((next "next" next nil "n")
9408                     (next-unread "next unread" next t "N")
9409                     (prev "previous" prev nil "p")
9410                     (prev-unread "previous unread" prev t "P")
9411                     (nomove "" nil nil ,keystroke)))
9412       (let ((func (gnus-summary-make-marking-command-1
9413                    mark (car lway) lway name)))
9414         (setq func (eval func))
9415         (define-key map (nth 4 lway) func)))))
9416       
9417 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9418   `(defun ,(intern
9419             (format "gnus-summary-put-mark-as-%s%s"
9420                     name (if (eq way 'nomove)
9421                              ""
9422                            (concat "-" (symbol-name way)))))
9423      (n)
9424      ,(format
9425        "Mark the current article as %s%s.
9426 If N, the prefix, then repeat N times.
9427 If N is negative, move in reverse order.
9428 The difference between N and the actual number of articles marked is
9429 returned."
9430        name (cadr lway))
9431      (interactive "p")
9432      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9433     
9434 (defun gnus-summary-generic-mark (n mark move unread)
9435   "Mark N articles with MARK."
9436   (unless (eq major-mode 'gnus-summary-mode)
9437     (error "This command can only be used in the summary buffer"))
9438   (gnus-summary-show-thread)
9439   (let ((nummove
9440          (cond
9441           ((eq move 'next) 1)
9442           ((eq move 'prev) -1)
9443           (t 0))))
9444     (if (zerop nummove)
9445         (setq n 1)
9446       (when (< n 0)
9447         (setq n (abs n)
9448               nummove (* -1 nummove))))
9449     (while (and (> n 0)
9450                 (gnus-summary-mark-article nil mark)
9451                 (zerop (gnus-summary-next-subject nummove unread t)))
9452       (setq n (1- n)))
9453     (when (/= 0 n)
9454       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9455     (gnus-summary-recenter)
9456     (gnus-summary-position-point)
9457     (gnus-set-mode-line 'summary)
9458     n))
9459
9460 (gnus-summary-make-all-marking-commands)
9461
9462 (gnus-ems-redefine)
9463
9464 (provide 'gnus-sum)
9465
9466 (run-hooks 'gnus-sum-load-hook)
9467
9468 ;;; gnus-sum.el ends here